forked from danieleff/STM32GENERIC
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathstm32_PWM.c
250 lines (198 loc) · 8.47 KB
/
stm32_PWM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
Copyright (c) 2017 Daniel Fekete
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
modify by [email protected] 2018.2.2
*/
#include "stm32_gpio.h"
TIM_HandleTypeDef *handle;
#define min(a,b) ((a)<(b)?(a):(b))
#define PWM_FREQUENCY_HZ 1000
static uint32_t counter;
static uint32_t waitCycles;
static uint8_t analogWriteResolutionBits = 8;
static uint32_t pwmFrequecyHz = PWM_FREQUENCY_HZ; //add by [email protected] 2017.8.2
const uint32_t TIMER_MAX_CYCLES = UINT16_MAX;
extern void pinMode(uint8_t, uint8_t);
stm32_pwm_disable_callback_func stm32_pwm_disable_callback = NULL;
void (*pwm_callback_func)();
void pwm_callback();
typedef struct {
GPIO_TypeDef *port;
void (*callback)();
uint32_t pinMask;
uint32_t waveLengthCycles;
uint32_t dutyCycle;
int32_t counterCycles;
} stm32_pwm_type;
static stm32_pwm_type pwm_config[sizeof(variant_pin_list) / sizeof(variant_pin_list[0])];
void stm32_pwm_disable(GPIO_TypeDef *port, uint32_t pin);
void analogWriteResolution(int bits) {
analogWriteResolutionBits = bits;
}
uint8_t getAnalogWriteResolution(void) {
return analogWriteResolutionBits;
}
static void initTimer(){
static TIM_HandleTypeDef staticHandle;
if (handle == NULL) {
handle = &staticHandle;
pwm_callback_func = &pwm_callback;
stm32_pwm_disable_callback = &stm32_pwm_disable;
#ifdef TIM3 /*some TIM2 is 32bit, priority to use TIM3. [email protected] 2018.2.2*/
__HAL_RCC_TIM3_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM3_IRQn, TIM_PRIORITY, 0); //TIM_PRIORITY define in stm32_def.h [email protected] 2017.12
HAL_NVIC_EnableIRQ(TIM3_IRQn);
handle->Instance = TIM3;
#else //99% of chips have TIM2
__HAL_RCC_TIM2_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM2_IRQn, TIM_PRIORITY, 0); //TIM_PRIORITY define in stm32_def.h [email protected] 2017.12
HAL_NVIC_EnableIRQ(TIM2_IRQn);
handle->Instance = TIM2;
#endif
handle->Init.Prescaler = 0;
handle->Init.CounterMode = TIM_COUNTERMODE_UP;
waitCycles = TIMER_MAX_CYCLES;
handle->Init.Period = waitCycles;
handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(handle);
HAL_TIM_Base_Start_IT(handle);
}
}
void pwmWrite(uint8_t pin, int dutyCycle, int frequency, int durationMillis) {
initTimer();
for(size_t i=0; i<sizeof(pwm_config) / sizeof(pwm_config[0]); i++) {
if (pwm_config[i].port == NULL ||
(pwm_config[i].port == variant_pin_list[pin].port
&& pwm_config[i].pinMask == variant_pin_list[pin].pinMask)) {
if (pwm_config[i].port == NULL) {
pinMode(pin, OUTPUT);
}
#if defined(STM32F0)||defined(GD32F1x0) // there is no pclk2
uint32_t timerFreq = HAL_RCC_GetPCLK1Freq();
#else
uint32_t timerFreq = HAL_RCC_GetPCLK2Freq();
#endif
pwm_config[i].port = variant_pin_list[pin].port;
pwm_config[i].pinMask = variant_pin_list[pin].pinMask;
pwm_config[i].waveLengthCycles = timerFreq / frequency;
pwm_config[i].dutyCycle = (uint64_t)pwm_config[i].waveLengthCycles * dutyCycle >> 16;
if (durationMillis > 0) {
pwm_config[i].counterCycles = timerFreq / 1000 * durationMillis;
}
break;
}
}
}
extern void tone(uint8_t pin, unsigned int frequency, unsigned long durationMillis) {
pwmWrite(pin, 1 << 15, frequency, durationMillis);
}
extern void noTone(uint8_t pin){
stm32_port_pin_type port_pin = variant_pin_list[pin];
stm32_pwm_disable(port_pin.port,port_pin.pinMask);
}
void analogWrite(uint8_t pin, int value) {
pwmWrite(pin, ((uint32_t)value << 16) >> analogWriteResolutionBits, pwmFrequecyHz, 0); //add by [email protected] 2017.8.2
}
/*add [email protected] 2018.8.2*/
void setPwmFrequency(uint32_t freqHz){
pwmFrequecyHz = freqHz;
}
uint32_t getPwmFrequency(void){
return pwmFrequecyHz;
}
void stm32ScheduleMicros(uint32_t microseconds, void (*callback)()) {
initTimer();
for(size_t i=0; i<sizeof(pwm_config) / sizeof(pwm_config[0]); i++) {
if (pwm_config[i].port == NULL && pwm_config[i].callback == NULL) {
pwm_config[i].callback = callback;
#if defined(STM32F0)||defined(GD32F1x0) // there is no pclk2
pwm_config[i].waveLengthCycles = HAL_RCC_GetPCLK1Freq() * (uint64_t)microseconds / 1000000;
#else
pwm_config[i].waveLengthCycles = HAL_RCC_GetPCLK2Freq() * (uint64_t)microseconds / 1000000;
#endif
pwm_config[i].counterCycles = pwm_config[i].waveLengthCycles;
break;
}
}
}
void stm32_pwm_disable(GPIO_TypeDef *port, uint32_t pinMask) {
for(size_t i=0; i<sizeof(pwm_config) / sizeof(pwm_config[0]); i++) {
if (pwm_config[i].port == NULL) {
return;
}
if (pwm_config[i].port == port && pwm_config[i].pinMask == pinMask) {
for(size_t j = i + 1; j < sizeof(pwm_config) / sizeof(pwm_config[0]); j++) {
if (pwm_config[j].port == NULL) {
pwm_config[i].port = pwm_config[j - 1].port;
pwm_config[i].pinMask = pwm_config[j - 1].pinMask;
pwm_config[j - 1].port = NULL;
break;
}
}
break;
}
}
}
void pwm_callback() {
if(__HAL_TIM_GET_FLAG(handle, TIM_FLAG_UPDATE) != RESET) {
if(__HAL_TIM_GET_IT_SOURCE(handle, TIM_IT_UPDATE) !=RESET) {
__HAL_TIM_CLEAR_IT(handle, TIM_IT_UPDATE);
counter += waitCycles;
uint32_t nextWaitCycles = TIMER_MAX_CYCLES;
for(size_t i=0; i<sizeof(pwm_config); i++) {
if (pwm_config[i].port != NULL) {
if (pwm_config[i].dutyCycle > counter % pwm_config[i].waveLengthCycles) {
pwm_config[i].port->BSRR = pwm_config[i].pinMask;
nextWaitCycles = min(nextWaitCycles, pwm_config[i].dutyCycle - (counter % pwm_config[i].waveLengthCycles));
} else {
pwm_config[i].port->BSRR = pwm_config[i].pinMask << 16;
nextWaitCycles = min(nextWaitCycles, pwm_config[i].waveLengthCycles - counter % pwm_config[i].waveLengthCycles);
}
if (pwm_config[i].counterCycles > 0) {
if (pwm_config[i].counterCycles <= (int)waitCycles) {
stm32_pwm_disable(pwm_config[i].port, pwm_config[i].pinMask);
} else {
pwm_config[i].counterCycles -= waitCycles;
}
}
} else if (pwm_config[i].callback != NULL) {
pwm_config[i].counterCycles -= waitCycles;
if (pwm_config[i].counterCycles < 0) {
pwm_config[i].callback();
pwm_config[i].counterCycles += pwm_config[i].waveLengthCycles;
}
} else {
break;
}
}
if (!nextWaitCycles || nextWaitCycles > TIMER_MAX_CYCLES) {
nextWaitCycles = TIMER_MAX_CYCLES;
}
waitCycles = nextWaitCycles;
__HAL_TIM_SET_AUTORELOAD(handle, waitCycles);
}
}
}
#ifdef TIM3 /*priority to use TIM3. [email protected] 2018.2.2*/
extern void TIM3_IRQHandler(void) {
#else
extern void TIM2_IRQHandler(void) {
#endif
if (pwm_callback_func != NULL) {
(*pwm_callback_func)();
}
}