-
Notifications
You must be signed in to change notification settings - Fork 28
/
ecPWM_student.c
282 lines (225 loc) · 8.47 KB
/
ecPWM_student.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
******************************************************************************
* @author SSSLAB
* @Mod 2023-10-18 by YKKIM
* @brief Embedded Controller: EC_HAL_for_student_exercise
*
******************************************************************************
*/
#include "stm32f4xx.h"
#include "ecPWM.h"
#include "math.h"
/* PWM Configuration using PinName_t Structure */
/* PWM initialization */
// Default: 84MHz PLL, 1MHz CK_CNT, 50% duty ratio, 1msec period
void PWM_init(PinName_t pinName){
// 0. Match TIMx from Port and Pin
GPIO_TypeDef *port;
unsigned int pin;
ecPinmap(pinName, &port, &pin);
TIM_TypeDef *TIMx;
int chN;
PWM_pinmap(pinName, &TIMx, &chN);
// 1. Initialize GPIO port and pin as AF
GPIO_init(port, pin, EC_AF); // AF=2
// GPIO_otype(port, pin, EC_PUSH_PULL); //if necessary
// GPIO_pupd(port\, pin, EC_PU); //if necessary
// 2. Configure GPIO AFR by Pin num.
// AFR[0] for pin: 0~7, AFR[1] for pin 8~15
// AFR=1 for TIM1,TIM2 AFR=2 for TIM3 etc
// YOUR CODE GOES HERE
// YOUR CODE GOES HERE
// YOUR CODE GOES HERE
// 3. Initialize Timer
TIM_init(TIMx, 1); // with default msec=1msec value.
TIMx->CR1 &= ~TIM_CR1_CEN;
// 3-2. Direction of Counter
//YOUR CODE GOES HERE
TIMx->CR1 &= ~TIM_CR1_DIR; // Counting direction: 0 = up-counting, 1 = down-counting
// 4. Configure Timer Output mode as PWM
uint32_t ccVal = TIMx->ARR/2; // default value CC=ARR/2
if(chN == 1){
TIMx->CCMR1 &= ~TIM_CCMR1_OC1M; // Clear ouput compare mode bits for channel 1
TIMx->CCMR1 |= TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2; // OC1M = 110 for PWM Mode 1 output on ch1. #define TIM_CCMR1_OC1M_1 (0x2UL << TIM_CCMR1_OC1M_Pos)
TIMx->CCMR1 |= TIM_CCMR1_OC1PE; // Output 1 preload enable (make CCR1 value changable)
TIMx->CCR1 = ccVal; // Output Compare Register for channel 1 (default duty ratio = 50%)
TIMx->CCER &= ~TIM_CCER_CC1P; // select output polarity: active high
TIMx->CCER |= TIM_CCER_CC1E; // Enable output for ch1
}
else if(chN == 2){
TIMx->CCMR1 &= ~TIM_CCMR1_OC2M; // Clear ouput compare mode bits for channel 2
// YOUR CODE GOES HERE // OC1M = 110 for PWM Mode 1 output on ch2
// YOUR CODE GOES HERE // Output 1 preload enable (make CCR2 value changable)
// YOUR CODE GOES HERE // Output Compare Register for channel 2 (default duty ratio = 50%)
// YOUR CODE GOES HERE // select output polarity: active high
// YOUR CODE GOES HERE // Enable output for ch2
}
else if(chN == 3){
TIMx->CCMR2 &= ~TIM_CCMR2_OC3M; // Clear ouput compare mode bits for channel 3
// YOUR CODE GOES HERE // OC1M = 110 for PWM Mode 1 output on ch3
// YOUR CODE GOES HERE // Output 1 preload enable (make CCR3 value changable)
// YOUR CODE GOES HERE // Output Compare Register for channel 3 (default duty ratio = 50%)
// YOUR CODE GOES HERE // select output polarity: active high
// YOUR CODE GOES HERE // Enable output for ch3
}
else if(chN == 4){
// YOUR CODE GOES HERE
// YOUR CODE GOES HERE
// YOUR CODE GOES HERE
// YOUR CODE GOES HERE
// YOUR CODE GOES HERE
}
// 5. Enable Timer Counter
// For TIM1 ONLY
if(TIMx == TIM1) TIMx->BDTR |= TIM_BDTR_MOE; // Main output enable (MOE): 0 = Disable, 1 = Enable
// Enable timers
TIMx->CR1 |= TIM_CR1_CEN; // Enable counter
}
/* PWM PERIOD SETUP */
// allowable range for msec: 1~2,000
void PWM_period_ms(PinName_t pinName, uint32_t msec){
// 0. Match TIMx from Port and Pin
GPIO_TypeDef *port;
unsigned int pin;
ecPinmap(pinName, &port, &pin);
TIM_TypeDef *TIMx;
int chN;
PWM_pinmap(pinName, &TIMx, &chN);
// 1. Set Counter Period in msec
TIM_period_ms(TIMx, msec);
}
// allowable range for msec: 1~2,000
void PWM_period(PinName_t pinName, uint32_t msec){
PWM_period_ms(pinName, msec);
}
// allowable range for usec: 1~1,000
void PWM_period_us(PinName_t pinName, uint32_t usec){
// 0. Match TIMx from Port and Pin
GPIO_TypeDef *port;
unsigned int pin;
ecPinmap(pinName, &port, &pin);
TIM_TypeDef *TIMx;
int chN;
PWM_pinmap(pinName, &TIMx, &chN);
// 1. Set Counter Period in usec
TIM_period_us(___, _____); //YOUR CODE GOES HERE
}
/* DUTY RATIO SETUP */
// High Pulse width in msec
void PWM_pulsewidth(PinName_t pinName, uint32_t pulse_width_ms){
// 0. Match TIMx from Port and Pin
GPIO_TypeDef *port;
unsigned int pin;
ecPinmap(pinName, &port, &pin);
TIM_TypeDef *TIMx;
int chN;
PWM_pinmap(pinName, &TIMx, &chN);
// 1. Declaration System Frequency and Prescaler
uint32_t fsys = 0;
uint32_t psc = TIMx->PSC;
// 2. Check System CLK: PLL or HSI
if((RCC->CFGR & RCC_CFGR_SW_PLL) == RCC_CFGR_SW_PLL) fsys = 84000; // for msec 84MHz/1000 [msec]
else if((RCC->CFGR & RCC_CFGR_SW_HSI) == RCC_CFGR_SW_HSI) fsys = 16000;
// 3. Configure prescaler PSC
float fclk = _______________ // fclk=fsys/(psc+1);
uint32_t value = ____________ // pulse_width_ms *fclk - 1;
switch(chN){
case 1: TIMx->CCR1 = value; break;
// REPEAT for CHn=2, 3, 4
// REPEAT for CHn=2, 3, 4
// REPEAT for CHn=2, 3, 4
default: break;
}
}
// High Pulse width in msec
void PWM_pulsewidth_ms(PinName_t pinName, uint32_t pulse_width_ms){
PWM_pulsewidth(pinName, pulse_width_ms);
}
// High Pulse width in usec
void PWM_pulsewidth_us(PinName_t pinName, uint32_t pulse_width_us){
// 0. Match TIMx from Port and Pin
GPIO_TypeDef *port;
unsigned int pin;
ecPinmap(pinName, &port, &pin);
TIM_TypeDef *TIMx;
int chN;
PWM_pinmap(pinName, &TIMx, &chN);
// 1. Declaration system frequency and prescaler
uint32_t fsys = 0;
uint32_t psc = TIMx->PSC;
// 2. Check System CLK: PLL or HSI
if((RCC->CFGR & RCC_CFGR_SW_PLL) == RCC_CFGR_SW_PLL) fsys = 84; // for msec 84MHz/1000000 [usec]
else if((RCC->CFGR & RCC_CFGR_SW_HSI) == RCC_CFGR_SW_HSI) fsys = 16;
// 3. Configure prescaler PSC
float fclk = _______________ // fclk=fsys/(psc+1);
uint32_t value = ____________ // pulse_width_ms *fclk - 1;
switch(chN){
case 1: TIMx->CCR1 = value; break;
// REPEAT for CHn=2, 3, 4
// REPEAT for CHn=2, 3, 4
// REPEAT for CHn=2, 3, 4
default: break;
}
}
// Dutry Ratio from 0 to 1
void PWM_duty(PinName_t pinName, float duty){
// 0. Match TIMx from Port and Pin
GPIO_TypeDef *port;
unsigned int pin;
ecPinmap(pinName, &port, &pin);
TIM_TypeDef *TIMx;
int chN;
PWM_pinmap(pinName, &TIMx, &chN);
// 1. Configure prescaler PSC
float value = ___________________; // (ARR+1)*dutyRatio + 1
if(chN == 1) { TIMx->CCR1 = value; } //set channel
// REPEAT for CHn=2, 3, 4
// REPEAT for CHn=2, 3, 4
// REPEAT for CHn=2, 3, 4
}
// DO NOT MODIFY HERE
void PWM_pinmap(PinName_t pinName, TIM_TypeDef **TIMx, int *chN)
{
GPIO_TypeDef *port;
unsigned int pin;
ecPinmap(pinName, &port, &pin);
if(port == GPIOA) {
switch(pin){
case 0 : *TIMx = TIM2; *chN = 1; break;
case 1 : *TIMx = TIM2; *chN = 2; break;
case 5 : *TIMx = TIM2; *chN = 1; break;
case 6 : *TIMx = TIM3; *chN = 1; break;
//case 7: TIMx = TIM1; *chN = 1N; break;
case 8 : *TIMx = TIM1; *chN = 1; break;
case 9 : *TIMx = TIM1; *chN = 2; break;
case 10: *TIMx = TIM1; *chN = 3; break;
case 15: *TIMx = TIM2; *chN = 1; break;
default: break;
}
}
else if(port == GPIOB) {
switch(pin){
//case 0: TIMx = TIM1; *chN = 2N; break;
//case 1: TIMx = TIM1; *chN = 3N; break;
case 3 : *TIMx = TIM2; *chN = 2; break;
case 4 : *TIMx = TIM3; *chN = 1; break;
case 5 : *TIMx = TIM3; *chN = 2; break;
case 6 : *TIMx = TIM4; *chN = 1; break;
case 7 : *TIMx = TIM4; *chN = 2; break;
case 8 : *TIMx = TIM4; *chN = 3; break;
case 9 : *TIMx = TIM4; *chN = 4; break;
case 10: *TIMx = TIM2; *chN = 3; break;
default: break;
}
}
else if(port == GPIOC) {
switch(pin){
case 6 : *TIMx = TIM3; *chN = 1; break;
case 7 : *TIMx = TIM3; *chN = 2; break;
case 8 : *TIMx = TIM3; *chN = 3; break;
case 9 : *TIMx = TIM3; *chN = 4; break;
default: break;
}
}
// TIM5 needs to be added, if used.
}