-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_timer.c
423 lines (370 loc) · 10.7 KB
/
my_timer.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "my_timer.h"
#include "my_clock.h"
#include "my_dma.h"
#include "my_gpio.h"
#include "stm32f10x.h"
#include <complex.h>
#include <stdlib.h>
#include <stdint.h>
extern uint16_t PCLK2;
TIM_TypeDef* getTIMER(const uint8_t timer)
{
switch (timer)
{
case T1 : return TIM1;
case T2 : return TIM2;
case T3 : return TIM3;
case T4 : return TIM4;
}
}
uint8_t getTIMER_CH_NO(const uint8_t timer_ch)
{
switch (timer_ch)
{
case TIM1_CH1 :
case TIM2_CH1 :
case TIM3_CH1 :
case TIM4_CH1 : return 1;
case TIM1_CH2 :
case TIM2_CH2 :
case TIM3_CH2 :
case TIM4_CH2 : return 2;
case TIM1_CH3 :
case TIM2_CH3 :
case TIM3_CH3 :
case TIM4_CH3 : return 3;
case TIM1_CH4 :
case TIM2_CH4 :
case TIM3_CH4 :
case TIM4_CH4 : return 4;
}
}
uint8_t getDMA_CH(const uint8_t timer_ch)
{
switch (timer_ch)
{
case TIM1_CH1 : return DMA1_CH2;
case TIM1_CH3 : return DMA1_CH6;
case TIM1_CH4 : return DMA1_CH4;
case TIM2_CH1 : return DMA1_CH5;
case TIM2_CH2 : return DMA1_CH7;
case TIM2_CH3 : return DMA1_CH1;
case TIM2_CH4 : return DMA1_CH7;
case TIM3_CH1 : return DMA1_CH6;
case TIM3_CH3 : return DMA1_CH2;
case TIM3_CH4 : return DMA1_CH3;
case TIM4_CH1 : return DMA1_CH1;
case TIM4_CH2 : return DMA1_CH4;
case TIM4_CH3 : return DMA1_CH5;
}
}
void timerCONFIG(const uint8_t timer,const uint16_t psc,const uint16_t arr)
{
if(timer==T1)
RCC->APB2ENR|=(0x01<<11);
else
RCC->APB1ENR|=(0x01<<(timer-1));
TIM_TypeDef* tim=getTIMER(timer);
tim->PSC=psc-1;
tim->ARR=arr;
}
void startTIMER(const uint8_t timer)
{
TIM_TypeDef* tim=getTIMER(timer);
tim->CNT=0;
tim->CR1|=(0x01);
}
void stopTIMER(const uint8_t timer)
{
TIM_TypeDef* tim=getTIMER(timer);
tim->CR1&=~(0x01);
}
void initTDELAY(const uint8_t timer)
{
timerCONFIG(timer,1,PCLK2);
TIM_TypeDef* tim=getTIMER(timer);
tim->CR1|=(0x01<<3); //set OPM
}
void timerDELAYUS(const uint8_t timer,uint16_t us)
{
TIM_TypeDef* tim=getTIMER(timer);
tim->CNT=0;
tim->CR1|=(0x01); //start counter
while(us)
{
while((tim->CR1&0x01)==0x01); //wait for counter to stop
tim->CR1|=(0x01); //restart counter
us--;
}
}
void timerDELAYMS(const uint8_t timer,uint16_t ms)
{
while(ms)
{
timerDELAYUS(timer,1000);
ms--;
}
}
void timerINT_50MS(const uint8_t timer,uint8_t ms)
{
/*if(PCLK2==S72MHZ)
timerCONFIG(timer,2,ms*(PCLK2/2*1000));
else
timerCONFIG(timer,1,PCLK2*1000);*/
timerCONFIG(timer,PCLK2,ms*1000); //at 1Mhz
TIM_TypeDef* tim=getTIMER(timer);
tim->CR1|=(0x01<<2); //Set update request source
tim->DIER|=(0x01); //Enable update interrupt
__disable_irq();
switch (timer) //NOTE: Can increase the timers later
{
case T1 : NVIC_EnableIRQ(TIM1_UP_IRQn);
break;
case T2 : NVIC_EnableIRQ(TIM2_IRQn);
break;
case T3 : NVIC_EnableIRQ(TIM3_IRQn);
break;
case T4 : NVIC_EnableIRQ(TIM4_IRQn);
break;
}
__enable_irq();
}
void timerINT_US(const uint8_t timer,uint8_t us)
{
//timerCONFIG(timer,1,PCLK2);
timerCONFIG(timer,PCLK2,us*PCLK2);
TIM_TypeDef* tim=getTIMER(timer);
tim->CR1|=(0x01<<2); //Set update request source
tim->DIER|=(0x01); //Enable update interrupt
__disable_irq();
switch (timer) //NOTE: Can increase the timers later
{
case T1 : NVIC_EnableIRQ(TIM1_UP_IRQn);
break;
case T2 : NVIC_EnableIRQ(TIM2_IRQn);
break;
case T3 : NVIC_EnableIRQ(TIM3_IRQn);
break;
case T4 : NVIC_EnableIRQ(TIM4_IRQn);
break;
}
__enable_irq();
}
void ccmCONFIG(const uint8_t timer_ch,uint8_t mode,uint16_t ccr,uint16_t arr,bool dma_burst)
{
uint8_t timer;
switch (timer_ch)
{
case TIM1_CH1 :
case TIM1_CH2 :
case TIM1_CH3 :
case TIM1_CH4 : timer=T1;
break;
case TIM2_CH1 :
case TIM2_CH2 :
case TIM2_CH3 :
case TIM2_CH4 : timer=T2;
break;
case TIM3_CH1 :
case TIM3_CH2 :
case TIM3_CH3 :
case TIM3_CH4 : timer=T3;
break;
case TIM4_CH1 :
case TIM4_CH2 :
case TIM4_CH3 :
case TIM4_CH4 : timer=T4;
}
timerCONFIG(timer,1,arr);
TIM_TypeDef* tim;
if (timer_ch>=TIM1_CH1&&timer_ch<=TIM1_CH4)
tim=getTIMER(T1);
else if (timer_ch>=TIM2_CH1&&timer_ch<=TIM2_CH4)
tim=getTIMER(T2);
else if ( timer_ch>=TIM3_CH1&&timer_ch<=TIM3_CH2)
tim=getTIMER(T3);
else if (timer_ch>=TIM3_CH3&&timer_ch<=TIM3_CH4)
tim=getTIMER(T3);
else if (timer_ch>=TIM4_CH1&&timer_ch<=TIM4_CH4)
tim=getTIMER(T4);
uint8_t ch=getTIMER_CH_NO(timer_ch);
/***************Config of capture/compare register******************/
//Preload enabled
if(ch==1)
{
tim->CCMR1&=~TIM_CCMR1_CC1S;
tim->CCMR1&=~(0x07<<4);
tim->CCMR1|=(mode<<4);
tim->CCMR1|=(0x01<<3);
tim->CCR1&=~(0xffff);
tim->CCR1|=ccr;
}
else if (ch==2)
{
tim->CCMR1&=~TIM_CCMR1_CC2S;
tim->CCMR1&=~(0x07<<12);
tim->CCMR1|=(mode<<12);
tim->CCMR1|=(0x01<<11);
tim->CCR2&=~(0xffff);
tim->CCR2|=ccr;
}
else if (ch==3)
{
tim->CCMR2&=~TIM_CCMR2_CC3S;
tim->CCMR2&=~(0x07<<4);
tim->CCMR2|=(mode<<4);
tim->CCMR2|=(0x01<<3);
tim->CCR3&=~(0xffff);
tim->CCR3|=ccr;
}
else if(ch==4)
{
tim->CCMR2&=~TIM_CCMR2_CC4S;
tim->CCMR2&=~(0x07<<12);
tim->CCMR2|=(mode<<12);
tim->CCMR2|=(0x01<<11);
tim->CCR4&=~(0xffff);
tim->CCR4|=ccr;
}
tim->CCER|=(0x01<<4*(ch-1)); //enable channel as output on pin
tim->CR1|=(0x01<<7); //auto reload preload enabled
/*************DMA config*****************/
if(dma_burst)
{
//tim->CR1|=(0x01<<3); //set OPM
tim->CR1|=(0x01<<2); //Set update request source
tim->DIER|=(0x01<<8); //Update DMA request enabled
tim->DIER|=(0x01<<(9+(ch-1)) ); //Enable channel DMA request
tim->DCR&=~(0x01F);
tim->DCR|=(0x0D+(ch-1)); //set DMA base address
//init2DMA(getDMA_CH(timer_ch),28,M_addr,tim->DMAR);
tim->CR2|=(0x01<<3); //DMA requests when update occurs
}
//pin config
outAF(timer_ch);
}
void timer_ch_PWMH(const uint8_t timer_ch,uint16_t ccr,uint16_t arr,bool dma_burst)
{
ccmCONFIG(timer_ch,PWM1,ccr,arr,dma_burst);
}
void timer_ch_PWML(const uint8_t timer_ch,uint16_t ccr,uint16_t arr,bool dma_burst)
{
ccmCONFIG(timer_ch,PWM2,ccr,arr,dma_burst);
}
void timer_ch_HIGH(const uint8_t timer_ch,uint16_t ccr,uint16_t arr,bool dma_burst)
{
ccmCONFIG(timer_ch,ACTIVE,ccr,arr,dma_burst);
}
void timer_ch_LOW(const uint8_t timer_ch,uint16_t ccr,uint16_t arr,bool dma_burst)
{
ccmCONFIG(timer_ch,INACTIVE,ccr,arr,dma_burst);
}
void timer_ch_TOGGLE(const uint8_t timer_ch,uint16_t ccr,uint16_t arr,bool dma_burst)
{
ccmCONFIG(timer_ch,TOGGLE,ccr,arr,dma_burst);
}
void timer_ch_FORCEHIGH(const uint8_t timer_ch,uint16_t ccr,uint16_t arr,bool dma_burst)
{
ccmCONFIG(timer_ch,FORCE_ACTIVE,ccr,arr,dma_burst);
}
void timer_ch_FORCELOW(const uint8_t timer_ch,uint16_t ccr,uint16_t arr,bool dma_burst)
{
ccmCONFIG(timer_ch,FORCE_INACTIVE,ccr,arr,dma_burst);
}
//******************Encoder************************* */
void set_encoder_mode(TIM_TypeDef* tim,uint8_t mode)
{
tim->SMCR&=~(TIM_SMCR_SMS);
tim->SMCR|=mode;
}
void set_encoder_TI(TIM_TypeDef* tim,uint8_t channel,uint8_t TI)
{
if(channel==1)
{
tim->CCMR1&=~TIM_CCMR1_CC1S;
tim->CCMR1|=TI;
}
else if (channel==2)
{
tim->CCMR1&=~TIM_CCMR1_CC2S;
tim->CCMR1|=( (TI==1)?2:1)<<8;
}
else
exit(EXIT_FAILURE);
}
void set_encoder_rising_trigger(TIM_TypeDef* tim,uint8_t channel)
{
if(channel==1)
tim->CCER&=~TIM_CCER_CC1P;
else if (channel==2)
tim->CCER&=~TIM_CCER_CC2P;
else
exit(EXIT_FAILURE);
}
void set_encoder_falling_trigger(TIM_TypeDef* tim,uint8_t channel)
{
if(channel==1)
{
tim->CCER&=~TIM_CCER_CC1P;
tim->CCER|=TIM_CCER_CC1P;
}
else if (channel==2)
{
tim->CCER&=~TIM_CCER_CC2P;
tim->CCER|=TIM_CCER_CC2P;
}
else
exit(EXIT_FAILURE);
}
void enable_capture(TIM_TypeDef* tim,uint8_t channel)
{
if(channel==1)
tim->CCER|=TIM_CCER_CC1E;
else if (channel==2)
tim->CCER|=TIM_CCER_CC2E;
else
exit(EXIT_FAILURE);
}
void set_encoder_filter(TIM_TypeDef* tim,uint8_t channel)
{
if(channel==1)
{
tim->CCMR1&=~TIM_CCMR1_IC1F;
}
else if (channel==2)
{
tim->CCMR1&=~TIM_CCMR1_IC2F;
}
else
exit(EXIT_FAILURE);
}
void encoderCONFIG(uint8_t timer, uint8_t mode, uint8_t channel1,uint8_t channel2,uint8_t trigger)
{
TIM_TypeDef* tim=getTIMER(timer);
timerCONFIG(timer,1,TIM_ARR_ARR);
set_encoder_mode(tim,mode);
set_encoder_TI(tim,channel1,1);
set_encoder_TI(tim,channel2,2);
if(trigger==CAPTURE_RISING)
{
set_encoder_rising_trigger(tim,channel1);
set_encoder_rising_trigger(tim,channel2);
}
else if(trigger==CAPTURE_FALLING)
{
set_encoder_falling_trigger(tim,channel1);
set_encoder_falling_trigger(tim,channel2);
}
set_encoder_filter(tim,channel1);
set_encoder_filter(tim,channel2);
enable_capture(tim,channel1);
enable_capture(tim,channel2);
}
void timer_encoder_init(uint8_t timer,uint8_t mode,uint8_t TI1_CH, uint8_t TI2_CH,uint8_t trigger)
{
uint8_t ch1_no=getTIMER_CH_NO(TI1_CH);
uint8_t ch2_no=getTIMER_CH_NO(TI2_CH);
encoderCONFIG(timer,mode,ch1_no,ch2_no,trigger);
inMODE(TI1_CH);
inMODE(TI2_CH);
}