]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/pwmout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F0 / 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 defined(TIM1_BASE)
52     if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
53 #endif
54 #if defined(TIM2_BASE)
55     if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
56 #endif
57     if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
58     if (obj->pwm == PWM_14) __TIM14_CLK_ENABLE();
59     if (obj->pwm == PWM_15) __TIM15_CLK_ENABLE();
60     if (obj->pwm == PWM_16) __TIM16_CLK_ENABLE();
61     if (obj->pwm == PWM_17) __TIM17_CLK_ENABLE();
62
63     // Configure GPIO
64     pinmap_pinout(pin, PinMap_PWM);
65
66     obj->pin = pin;
67     obj->period = 0;
68     obj->pulse = 0;
69
70     pwmout_period_us(obj, 20000); // 20 ms per default
71 }
72
73 void pwmout_free(pwmout_t* obj)
74 {
75     // Configure GPIO
76     pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
77 }
78
79 void pwmout_write(pwmout_t* obj, float value)
80 {
81     TIM_OC_InitTypeDef sConfig;
82     int channel = 0;
83     int complementary_channel = 0;
84
85     TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
86
87     if (value < (float)0.0) {
88         value = 0.0;
89     } else if (value > (float)1.0) {
90         value = 1.0;
91     }
92
93     obj->pulse = (uint32_t)((float)obj->period * value);
94
95     // Configure channels
96     sConfig.OCMode       = TIM_OCMODE_PWM1;
97     sConfig.Pulse        = obj->pulse;
98     sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
99     sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
100     sConfig.OCFastMode   = TIM_OCFAST_DISABLE;
101     sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
102     sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
103
104 #if defined (TARGET_STM32F030R8) || defined (TARGET_STM32F051R8)
105     switch (obj->pin) {
106         // Channels 1
107         case PA_4:
108         case PA_6:
109         case PB_1:
110         case PB_4:
111         case PB_8:
112         case PB_9:
113         case PB_14:
114         case PC_6:
115             channel = TIM_CHANNEL_1;
116             break;
117         // Channels 1N
118         case PB_6:
119         case PB_7:
120             channel = TIM_CHANNEL_1;
121             complementary_channel = 1;
122             break;
123         // Channels 2
124         case PA_7:
125         case PB_5:
126         case PB_15:
127         case PC_7:
128             channel = TIM_CHANNEL_2;
129             break;
130         // Channels 3
131         case PB_0:
132         case PC_8:
133             channel = TIM_CHANNEL_3;
134             break;
135         // Channels 4
136         case PC_9:
137             channel = TIM_CHANNEL_4;
138             break;
139         default:
140             return;
141     }
142
143 #else
144     switch (obj->pin) {
145         // Channels 1
146         case PA_2:
147         case PA_4:
148         case PA_6:
149         case PA_7:
150         case PA_8:
151         case PB_1:
152         case PB_4:
153         case PB_8:
154         case PB_9:
155         case PB_14:
156         case PC_6:
157             channel = TIM_CHANNEL_1;
158             break;
159         // Channels 1N
160         case PA_1:
161         case PB_6:
162         case PB_7:
163         case PB_13:
164             channel = TIM_CHANNEL_1;
165             complementary_channel = 1;
166             break;
167         // Channels 2
168         case PA_3:
169         case PA_9:
170         case PB_5:
171         case PB_15:
172         case PC_7:
173             channel = TIM_CHANNEL_2;
174             break;
175         // Channels 3
176         case PA_10:
177         case PB_0:
178         case PC_8:
179             channel = TIM_CHANNEL_3;
180             break;
181         // Channels 4
182         case PA_11:
183         case PC_9:
184             channel = TIM_CHANNEL_4;
185             break;
186         default:
187             return;
188     }
189
190 #endif
191
192     HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
193
194     if (complementary_channel) {
195         HAL_TIMEx_PWMN_Start(&TimHandle, channel);
196     } else {
197         HAL_TIM_PWM_Start(&TimHandle, channel);
198     }
199 }
200
201 float pwmout_read(pwmout_t* obj)
202 {
203     float value = 0;
204     if (obj->period > 0) {
205         value = (float)(obj->pulse) / (float)(obj->period);
206     }
207     return ((value > (float)1.0) ? (float)(1.0) : (value));
208 }
209
210 void pwmout_period(pwmout_t* obj, float seconds)
211 {
212     pwmout_period_us(obj, seconds * 1000000.0f);
213 }
214
215 void pwmout_period_ms(pwmout_t* obj, int ms)
216 {
217     pwmout_period_us(obj, ms * 1000);
218 }
219
220 void pwmout_period_us(pwmout_t* obj, int us)
221 {
222     TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
223
224     float dc = pwmout_read(obj);
225
226     __HAL_TIM_DISABLE(&TimHandle);
227
228     // Update the SystemCoreClock variable
229     SystemCoreClockUpdate();
230
231     TimHandle.Init.Period        = us - 1;
232     TimHandle.Init.Prescaler     = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
233     TimHandle.Init.ClockDivision = 0;
234     TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
235     HAL_TIM_PWM_Init(&TimHandle);
236
237     // Set duty cycle again
238     pwmout_write(obj, dc);
239
240     // Save for future use
241     obj->period = us;
242
243     __HAL_TIM_ENABLE(&TimHandle);
244 }
245
246 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
247 {
248     pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
249 }
250
251 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
252 {
253     pwmout_pulsewidth_us(obj, ms * 1000);
254 }
255
256 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
257 {
258     float value = (float)us / (float)obj->period;
259     pwmout_write(obj, value);
260 }
261
262 #endif