1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************
30 #include "pwmout_api.h"
36 #include "mbed_error.h"
37 #include "PeripheralPins.h"
39 static TIM_HandleTypeDef TimHandle
;
41 void pwmout_init(pwmout_t
* obj
, PinName pin
)
43 // Get the peripheral name from the pin and assign it to the object
44 obj
->pwm
= (PWMName
)pinmap_peripheral(pin
, PinMap_PWM
);
46 if (obj
->pwm
== (PWMName
)NC
) {
47 error("PWM error: pinout mapping failed.");
51 if (obj
->pwm
== PWM_1
) __TIM1_CLK_ENABLE();
52 if (obj
->pwm
== PWM_2
) __TIM2_CLK_ENABLE();
53 if (obj
->pwm
== PWM_3
) __TIM3_CLK_ENABLE();
54 if (obj
->pwm
== PWM_15
) __TIM15_CLK_ENABLE();
55 if (obj
->pwm
== PWM_16
) __TIM16_CLK_ENABLE();
56 if (obj
->pwm
== PWM_17
) __TIM17_CLK_ENABLE();
59 pinmap_pinout(pin
, PinMap_PWM
);
65 pwmout_period_us(obj
, 20000); // 20 ms per default
68 void pwmout_free(pwmout_t
* obj
)
71 pin_function(obj
->pin
, STM_PIN_DATA(STM_MODE_INPUT
, GPIO_NOPULL
, 0));
74 void pwmout_write(pwmout_t
* obj
, float value
)
76 TIM_OC_InitTypeDef sConfig
;
78 int complementary_channel
= 0;
80 TimHandle
.Instance
= (TIM_TypeDef
*)(obj
->pwm
);
82 if (value
< (float)0.0) {
84 } else if (value
> (float)1.0) {
88 obj
->pulse
= (uint32_t)((float)obj
->period
* value
);
91 sConfig
.OCMode
= TIM_OCMODE_PWM1
;
92 sConfig
.Pulse
= obj
->pulse
;
93 sConfig
.OCPolarity
= TIM_OCPOLARITY_HIGH
;
94 sConfig
.OCNPolarity
= TIM_OCNPOLARITY_HIGH
;
95 sConfig
.OCFastMode
= TIM_OCFAST_DISABLE
;
96 sConfig
.OCIdleState
= TIM_OCIDLESTATE_RESET
;
97 sConfig
.OCNIdleState
= TIM_OCNIDLESTATE_RESET
;
114 channel
= TIM_CHANNEL_1
;
123 channel
= TIM_CHANNEL_1
;
124 complementary_channel
= 1;
134 channel
= TIM_CHANNEL_2
;
142 channel
= TIM_CHANNEL_3
;
147 channel
= TIM_CHANNEL_3
;
148 complementary_channel
= 1;
157 channel
= TIM_CHANNEL_4
;
164 HAL_TIM_PWM_ConfigChannel(&TimHandle
, &sConfig
, channel
);
166 if (complementary_channel
) {
167 HAL_TIMEx_PWMN_Start(&TimHandle
, channel
);
169 HAL_TIM_PWM_Start(&TimHandle
, channel
);
173 float pwmout_read(pwmout_t
* obj
)
176 if (obj
->period
> 0) {
177 value
= (float)(obj
->pulse
) / (float)(obj
->period
);
179 return ((value
> (float)1.0) ? (float)(1.0) : (value
));
182 void pwmout_period(pwmout_t
* obj
, float seconds
)
184 pwmout_period_us(obj
, seconds
* 1000000.0f
);
187 void pwmout_period_ms(pwmout_t
* obj
, int ms
)
189 pwmout_period_us(obj
, ms
* 1000);
192 void pwmout_period_us(pwmout_t
* obj
, int us
)
194 TimHandle
.Instance
= (TIM_TypeDef
*)(obj
->pwm
);
196 float dc
= pwmout_read(obj
);
198 __HAL_TIM_DISABLE(&TimHandle
);
200 // Update the SystemCoreClock variable
201 SystemCoreClockUpdate();
203 TimHandle
.Init
.Period
= us
- 1;
204 TimHandle
.Init
.Prescaler
= (uint16_t)(SystemCoreClock
/ 1000000) - 1; // 1 µs tick
205 TimHandle
.Init
.ClockDivision
= 0;
206 TimHandle
.Init
.CounterMode
= TIM_COUNTERMODE_UP
;
207 HAL_TIM_PWM_Init(&TimHandle
);
209 // Set duty cycle again
210 pwmout_write(obj
, dc
);
212 // Save for future use
215 __HAL_TIM_ENABLE(&TimHandle
);
218 void pwmout_pulsewidth(pwmout_t
* obj
, float seconds
)
220 pwmout_pulsewidth_us(obj
, seconds
* 1000000.0f
);
223 void pwmout_pulsewidth_ms(pwmout_t
* obj
, int ms
)
225 pwmout_pulsewidth_us(obj
, ms
* 1000);
228 void pwmout_pulsewidth_us(pwmout_t
* obj
, int us
)
230 float value
= (float)us
/ (float)obj
->period
;
231 pwmout_write(obj
, value
);