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