]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F4/pwmout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F4 / 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     // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object
47     uint32_t function = pinmap_function(pin, PinMap_PWM);
48     MBED_ASSERT(function != (uint32_t)NC);
49     obj->channel  = STM_PIN_CHANNEL(function);
50     obj->inverted = STM_PIN_INVERTED(function);
51
52     if (obj->pwm == (PWMName)NC) {
53         error("PWM error: pinout mapping failed.");
54     }
55
56     // Enable TIM clock
57     if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
58     if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
59     if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
60     if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
61 #if defined(TIM8_BASE)
62     if (obj->pwm == PWM_8) __TIM8_CLK_ENABLE();
63 #endif
64     if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
65     if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
66     if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();
67 #if defined(TIM13_BASE)
68     if (obj->pwm == PWM_13) __TIM13_CLK_ENABLE();
69 #endif
70 #if defined(TIM14_BASE)
71     if (obj->pwm == PWM_14) __TIM14_CLK_ENABLE();
72 #endif
73
74     // Configure GPIO
75     pinmap_pinout(pin, PinMap_PWM);
76
77     obj->pin = pin;
78     obj->period = 0;
79     obj->pulse = 0;
80
81     pwmout_period_us(obj, 20000); // 20 ms per default
82 }
83
84 void pwmout_free(pwmout_t* obj)
85 {
86     // Configure GPIO
87     pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
88 }
89
90 void pwmout_write(pwmout_t* obj, float value)
91 {
92     TIM_OC_InitTypeDef sConfig;
93     int channel = 0;
94     int complementary_channel = 0;
95
96     TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
97
98     if (value < (float)0.0) {
99         value = 0.0;
100     } else if (value > (float)1.0) {
101         value = 1.0;
102     }
103
104     obj->pulse = (uint32_t)((float)obj->period * value);
105
106     // Configure channels
107     sConfig.OCMode       = TIM_OCMODE_PWM1;
108     sConfig.Pulse        = obj->pulse;
109     sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
110     sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
111     sConfig.OCFastMode   = TIM_OCFAST_DISABLE;
112     sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
113     sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
114
115     complementary_channel = obj->inverted;
116     switch (obj->channel) {
117
118         case 1:
119             channel = TIM_CHANNEL_1;
120             break;
121
122         case 2:
123             channel = TIM_CHANNEL_2;
124             break;
125
126         case 3:
127             channel = TIM_CHANNEL_3;
128             break;
129
130         case 4:
131             channel = TIM_CHANNEL_4;
132             break;
133
134         default:
135             return;
136     }
137
138     HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel);
139     if (complementary_channel) {
140         HAL_TIMEx_PWMN_Start(&TimHandle, channel);
141     } else {
142         HAL_TIM_PWM_Start(&TimHandle, channel);
143     }
144 }
145
146 float pwmout_read(pwmout_t* obj)
147 {
148     float value = 0;
149     if (obj->period > 0) {
150         value = (float)(obj->pulse) / (float)(obj->period);
151     }
152     return ((value > (float)1.0) ? (float)(1.0) : (value));
153 }
154
155 void pwmout_period(pwmout_t* obj, float seconds)
156 {
157     pwmout_period_us(obj, seconds * 1000000.0f);
158 }
159
160 void pwmout_period_ms(pwmout_t* obj, int ms)
161 {
162     pwmout_period_us(obj, ms * 1000);
163 }
164
165 void pwmout_period_us(pwmout_t* obj, int us)
166 {
167     TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
168
169     float dc = pwmout_read(obj);
170
171     __HAL_TIM_DISABLE(&TimHandle);
172
173     // Update the SystemCoreClock variable
174     SystemCoreClockUpdate();
175
176     TimHandle.Init.Period        = us - 1;
177     TimHandle.Init.Prescaler     = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
178     TimHandle.Init.ClockDivision = 0;
179     TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
180     HAL_TIM_PWM_Init(&TimHandle);
181
182     // Set duty cycle again
183     pwmout_write(obj, dc);
184
185     // Save for future use
186     obj->period = us;
187
188     __HAL_TIM_ENABLE(&TimHandle);
189 }
190
191 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
192 {
193     pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
194 }
195
196 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
197 {
198     pwmout_pulsewidth_us(obj, ms * 1000);
199 }
200
201 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
202 {
203     float value = (float)us / (float)obj->period;
204     pwmout_write(obj, value);
205 }
206
207 #endif