]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/pwmout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32L0 / pwmout_api.c
1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *******************************************************************************
29 */
30 #include "pwmout_api.h"
31
32 #if DEVICE_PWMOUT
33
34 #include "cmsis.h"
35 #include "pinmap.h"
36 #include "mbed_error.h"
37 #include "PeripheralPins.h"
38
39 static TIM_HandleTypeDef TimHandle;
40
41 void pwmout_init(pwmout_t* obj, PinName pin)
42 {
43 // Get the peripheral name from the pin and assign it to the object
44 obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
45
46 if (obj->pwm == (PWMName)NC) {
47 error("PWM error: pinout mapping failed.");
48 }
49
50 // Enable TIM clock
51 if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
52 #if defined(TIM3_BASE)
53 if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
54 #endif
55 if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE();
56 if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE();
57
58 // Configure GPIO
59 pinmap_pinout(pin, PinMap_PWM);
60
61 obj->pin = pin;
62 obj->period = 0;
63 obj->pulse = 0;
64
65 pwmout_period_us(obj, 20000); // 20 ms per default
66 }
67
68 void pwmout_free(pwmout_t* obj)
69 {
70 // Configure GPIO
71 pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
72 }
73
74 void pwmout_write(pwmout_t* obj, float value)
75 {
76 TIM_OC_InitTypeDef sConfig;
77 int channel = 0;
78
79 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
80
81 if (value < (float)0.0) {
82 value = 0.0;
83 } else if (value > (float)1.0) {
84 value = 1.0;
85 }
86
87 obj->pulse = (uint32_t)((float)obj->period * value);
88
89 // Configure channels
90 sConfig.OCMode = TIM_OCMODE_PWM1;
91 sConfig.Pulse = obj->pulse;
92 sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
93 sConfig.OCFastMode = TIM_OCFAST_ENABLE;
94
95 switch (obj->pin) {
96 // Channels 1
97 case PA_0:
98 case PA_5:
99 case PA_6:
100 case PA_15:
101 case PB_4:
102 case PC_6:
103 channel = TIM_CHANNEL_1;
104 break;
105 // Channels 2
106 case PA_1:
107 case PA_7:
108 case PB_3:
109 case PB_5:
110 case PC_7:
111 channel = TIM_CHANNEL_2;
112 break;
113 // Channels 3
114 case PA_2:
115 case PB_0:
116 case PB_10:
117 case PC_8:
118 channel = TIM_CHANNEL_3;
119 break;
120 // Channels 4
121 case PA_3:
122 case PB_1:
123 case PB_11:
124 case PC_9:
125 channel = TIM_CHANNEL_4;
126 break;
127 default:
128 return;
129 }
130
131 HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
132 HAL_TIM_PWM_Start(&TimHandle, channel);
133 }
134
135 float pwmout_read(pwmout_t* obj)
136 {
137 float value = 0;
138 if (obj->period > 0) {
139 value = (float)(obj->pulse) / (float)(obj->period);
140 }
141 return ((value > (float)1.0) ? (float)(1.0) : (value));
142 }
143
144 void pwmout_period(pwmout_t* obj, float seconds)
145 {
146 pwmout_period_us(obj, seconds * 1000000.0f);
147 }
148
149 void pwmout_period_ms(pwmout_t* obj, int ms)
150 {
151 pwmout_period_us(obj, ms * 1000);
152 }
153
154 void pwmout_period_us(pwmout_t* obj, int us)
155 {
156 TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
157
158 float dc = pwmout_read(obj);
159
160 __HAL_TIM_DISABLE(&TimHandle);
161
162 TimHandle.Init.Period = us - 1;
163 TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
164 TimHandle.Init.ClockDivision = 0;
165 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
166 HAL_TIM_PWM_Init(&TimHandle);
167
168 // Set duty cycle again
169 pwmout_write(obj, dc);
170
171 // Save for future use
172 obj->period = us;
173
174 __HAL_TIM_ENABLE(&TimHandle);
175 }
176
177 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
178 {
179 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
180 }
181
182 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
183 {
184 pwmout_pulsewidth_us(obj, ms * 1000);
185 }
186
187 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
188 {
189 float value = (float)us / (float)obj->period;
190 pwmout_write(obj, value);
191 }
192
193 #endif
Imprint / Impressum