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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*******************************************************************************
* COPYRIGHT (C) 2021 CMS Technologies Ltd. *
* *
********************************************************************************
* FileName : timm.c *
* Author : *
* Version : 1.0.2 *
* Date : 2024.03.29 *
* Description : *
* Function List : *
********************************************************************************/
#include "timm.h"
#include "cgc.h"
static TMM_REG_t TMM_reg;
volatile uint16_t tmm_overflow_count_a = 0U;
volatile uint16_t tmm_overflow_count_b = 0U;
/***********************************************************************************************************************
* Function Name: TMM_RegSetting
* @brief this function is to mapping TMM reg
* @param
* @return
***********************************************************************************************************************/
void TMM_RegSetting(TMMSelect_TypeDef TMMx)
{
switch (TMMx)
{
case TMM0:
TMM_reg.TMMR = &TMM->TMMR;
TMM_reg.TMPMR = &TMM->TMPMR;
TMM_reg.TMFCR = &TMM->TMFCR;
TMM_reg.TMOCR = &TMM->TMOCR;
TMM_reg.TMCR[0] = &TMM->TMCR0;
TMM_reg.TMIORA[0] = &TMM->TMIORA0;
TMM_reg.TMIORC[0] = &TMM->TMIORC0;
TMM_reg.TMIER[0] = &TMM->TMIER0;
TMM_reg.TMPOCR[0] = &TMM->TMPOCR0;
TMM_reg.TMSR = &TMM->TMSR0;
TMM_reg.TMGRA = &TMM->TMGRA0;
TMM_reg.TMGRB = &TMM->TMGRB0;
TMM_reg.TMGRC = &TMM->TMGRC0;
TMM_reg.TMGRD = &TMM->TMGRD0;
TMM_reg.OPTL0 = &TMM->OPCTL0;
TMM_reg.TMOER1 = &TMM->TMOER1;
TMM_reg.TMDF[0] = &TMM->TMDF0;
break;
case TMM1:
TMM_reg.TMMR = &TMM->TMMR;
TMM_reg.TMPMR = &TMM->TMPMR;
TMM_reg.TMFCR = &TMM->TMFCR;
TMM_reg.TMOCR = &TMM->TMOCR;
TMM_reg.TMCR[1] = &TMM->TMCR1;
TMM_reg.TMIORA[1] = &TMM->TMIORA1;
TMM_reg.TMIORC[1] = &TMM->TMIORC1;
TMM_reg.TMIER[1] = &TMM->TMIER1;
TMM_reg.TMPOCR[1] = &TMM->TMPOCR1;
TMM_reg.TMSR = &TMM->TMSR1;
TMM_reg.TMGRA = &TMM->TMGRA1;
TMM_reg.TMGRB = &TMM->TMGRB1;
TMM_reg.TMGRC = &TMM->TMGRC1;
TMM_reg.TMGRD = &TMM->TMGRD1;
TMM_reg.OPTL0 = &TMM->OPCTL0;
TMM_reg.TMOER1 = &TMM->TMOER1;
TMM_reg.TMDF[1] = &TMM->TMDF1;
break;
default:
break;
}
}
/**
* @brief Initializes the TIEMRM peripheral according to the specified
* parameters in the TIMM_InitStruct .
* @param TIMM_InitStruct: pointer to a TMM_InitTypeDef structure that contains
* the configuration information for the specified TIEMRM peripheral.
* @retval none
*/
void TMM_Init(TMM_InitTypeDef *TMM_InitStruct)
{
assert_param(IS_TMM_CLOCK(TMM_InitStruct->TMM_Clk));
assert_param(IS_TMM_CHANNEL(TMM_InitStruct->TMM_Channel));
CGC_PER1PeriphClockCmd(CGC_PER1Periph_TMM,ENABLE);
TMM_RegSetting(TMM_InitStruct->TMM_Select);
/*reset tmm counter and set conuter mode when compare match*/
if(TMM_InitStruct->TMM_Select == TMM0)
{
TMM->TMSTR &= (uint8_t)~(_01_TMM_TM0_COUNT_START);
TMM->TMSTR |= _04_TMM_TM0_COUNT_CONTINUES;
}
else
{
TMM->TMSTR &= (uint8_t)~(_01_TMM_TM0_COUNT_START << TMM_InitStruct->TMM_Select);
TMM->TMSTR |= _04_TMM_TM0_COUNT_CONTINUES << TMM_InitStruct->TMM_Select;
}
if((TMM_InitStruct->TMM_Clk == TMM_FCLK_TMCLK) && TMM_InitStruct->TMM_Mode != ( TMM_Mode_PWM3))
{
*TMM_reg.TMFCR |= 0x40; // input clock is valid
}
if(TMM_InitStruct->TMM_Mode == TMM_Mode_Capture)
{
*TMM_reg.TMMR |= 0x00 | (TMM_InitStruct->TMM_CHD_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 5 |\
(TMM_InitStruct->TMM_CHC_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 4;
*TMM_reg.TMDF[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_IC.TMM_Filter_Clk;
*TMM_reg.TMCR[TMM_InitStruct->TMM_Select] = TMM_InitStruct->TMM_CounterClear | TMM_InitStruct->TMM_Clk;
if((TMM_InitStruct->TMM_Channel & TMM_Channel_A) == TMM_Channel_A)
{
*TMM_reg.TMIORA[TMM_InitStruct->TMM_Select] |= 0x04 | TMM_InitStruct->TMM_IC.TMM_CHA.TMM_ICPolarity;
*TMM_reg.TMDF[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_IC.TMM_CHA.TMM_ICFilter;
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_B)== TMM_Channel_B)
{
*TMM_reg.TMIORA[TMM_InitStruct->TMM_Select] |= 0x40 | (TMM_InitStruct->TMM_IC.TMM_CHB.TMM_ICPolarity << 4); //??????
*TMM_reg.TMDF[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_IC.TMM_CHB.TMM_ICFilter << 1;
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_C)== TMM_Channel_C)
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= 0x08 |0x04 | TMM_InitStruct->TMM_IC.TMM_CHC.TMM_ICPolarity;
*TMM_reg.TMDF[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_IC.TMM_CHC.TMM_ICFilter << 2;
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_D)== TMM_Channel_D)
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= 0x80 |0x40 | (TMM_InitStruct->TMM_IC.TMM_CHD.TMM_ICPolarity << 4); //??????
*TMM_reg.TMDF[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_IC.TMM_CHD.TMM_ICFilter << 3;
}
}
if(TMM_InitStruct->TMM_Mode == TMM_Mode_Compare)
{
*TMM_reg.TMMR |= 0x00 | (TMM_InitStruct->TMM_CHD_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 5 |\
(TMM_InitStruct->TMM_CHC_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 4 ;
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] &= 0x00;
*TMM_reg.TMCR[TMM_InitStruct->TMM_Select] = TMM_InitStruct->TMM_CounterClear | TMM_InitStruct->TMM_Clk; //counter cleared condition
if((TMM_InitStruct->TMM_Channel & TMM_Channel_A) == TMM_Channel_A)
{
*TMM_reg.TMIORA[TMM_InitStruct->TMM_Select] |= (TMM_InitStruct->TMM_OC.TMM_CHA.TMM_OCPolarity);
*TMM_reg.TMGRA = TMM_InitStruct->TMM_CHA_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_A) << (TMM_InitStruct->TMM_Select << 2)); // 0x00 ???????????????
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_B)== TMM_Channel_B)
{
*TMM_reg.TMIORA[TMM_InitStruct->TMM_Select] |= ((TMM_InitStruct->TMM_OC.TMM_CHB.TMM_OCPolarity)<< 4);
*TMM_reg.TMGRB = TMM_InitStruct->TMM_CHB_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_B) << (TMM_InitStruct->TMM_Select << 2));
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_C)== TMM_Channel_C)
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= (TMM_InitStruct->TMM_OC.TMM_CHC.TMM_OCPolarity);
*TMM_reg.TMGRC = TMM_InitStruct->TMM_CHC_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_C) << (TMM_InitStruct->TMM_Select << 2));
if(TMM_InitStruct->TMM_OC.TMM_CHC_OutPin_Ctrl == TMM_OC_Output_PinCtrl_Enable)
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= 0x00;
if(TMM_InitStruct->TMM_CHC_AS_BufferRegister)
{
*TMM_reg.TMMR &= ~ (TMM_InitStruct->TMM_CHC_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 4;
}
}
else
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= 0x08; // use TMGRC to control TMIOA pin output
}
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_D)== TMM_Channel_D)
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= ((TMM_InitStruct->TMM_OC.TMM_CHD.TMM_OCPolarity)<< 4);
*TMM_reg.TMGRD = TMM_InitStruct->TMM_CHD_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_D) << (TMM_InitStruct->TMM_Select << 2));
if(TMM_InitStruct->TMM_OC.TMM_CHD_OutPin_Ctrl == TMM_OC_Output_PinCtrl_Enable)
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= 0x00;
if(TMM_InitStruct->TMM_CHD_AS_BufferRegister)
{
*TMM_reg.TMMR &= ~ (TMM_InitStruct->TMM_CHD_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 5;
}
}
else
{
*TMM_reg.TMIORC[TMM_InitStruct->TMM_Select] |= 0x80; // use TMGRD to control TMIOB pin output
}
}
}
if(TMM_InitStruct->TMM_Mode == TMM_Mode_PWM || TMM_InitStruct->TMM_Mode == TMM_Mode_PWM_RSTSYN )
{
*TMM_reg.TMMR |= 0x00 | (TMM_InitStruct->TMM_CHD_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 5 |\
(TMM_InitStruct->TMM_CHC_AS_BufferRegister << (TMM_InitStruct->TMM_Select << 1)) << 4 ;
*TMM_reg.TMCR[TMM_InitStruct->TMM_Select] = TMM_InitStruct->TMM_CounterClear | TMM_InitStruct->TMM_Clk; //counter cleared condition
*TMM_reg.TMGRA = TMM_InitStruct->TMM_CHA_Pulse - 1;
if((TMM_InitStruct->TMM_Channel & TMM_Channel_B)== TMM_Channel_B)
{
*TMM_reg.TMPMR |= (TMM_Channel_B >>1) <<(TMM_InitStruct->TMM_Select << 2);
*TMM_reg.TMGRB = TMM_InitStruct->TMM_CHB_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_B) << (TMM_InitStruct->TMM_Select << 2));
*TMM_reg.TMPOCR[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_PWM.TMM_CHB.TMM_PWMActiveLevel;
*TMM_reg.TMOCR |= ((TMM_InitStruct->TMM_PWM.TMM_CHB.TMM_PWMInitLevel << 1) << (TMM_InitStruct->TMM_Select << 2));
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_C)== TMM_Channel_C)
{
*TMM_reg.TMPMR |= (TMM_Channel_C >>1) <<(TMM_InitStruct->TMM_Select << 2);
*TMM_reg.TMGRC = TMM_InitStruct->TMM_CHC_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_C) << (TMM_InitStruct->TMM_Select << 2));
*TMM_reg.TMPOCR[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_PWM.TMM_CHC.TMM_PWMActiveLevel << 1;
*TMM_reg.TMOCR |= ((TMM_InitStruct->TMM_PWM.TMM_CHC.TMM_PWMInitLevel << 2) << (TMM_InitStruct->TMM_Select << 2));
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_D)== TMM_Channel_D)
{
*TMM_reg.TMPMR |= (TMM_Channel_D >>1) <<(TMM_InitStruct->TMM_Select << 2);
*TMM_reg.TMGRD = TMM_InitStruct->TMM_CHD_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_D) << (TMM_InitStruct->TMM_Select << 2));
*TMM_reg.TMPOCR[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_PWM.TMM_CHD.TMM_PWMActiveLevel << 2;
*TMM_reg.TMOCR |= ((TMM_InitStruct->TMM_PWM.TMM_CHD.TMM_PWMInitLevel << 3) << (TMM_InitStruct->TMM_Select << 2));
}
if(TMM_InitStruct->TMM_Mode == TMM_Mode_PWM_RSTSYN)
{
*TMM_reg.TMFCR |= 0x01;
}
}
if(TMM_InitStruct->TMM_Mode == TMM_Mode_CompPWM)
{
CGC_PER1PeriphClockCmd(CGC_PER1Periph_PWMCUTOFF,ENABLE);
*TMM_reg.TMMR |= _80_TMM_TMGRD1_BUFFER |_40_TMM_TMGRC1_BUFFER|_20_TMM_TMGRD0_BUFFER; //the TMGRD1 used as TMGRB1 buffer register??TMGRC1 used as TMGRA1 buffer register?? TMGRD0 are used TMGRB0 buffer register
*TMM_reg.TMCR[TMM_InitStruct->TMM_Select] = TMM_InitStruct->TMM_CounterClear | TMM_InitStruct->TMM_Clk; //counter cleared condition
*TMM_reg.TMFCR |= (TMM_InitStruct->TMM_BDTR.TMM_NegativeState << 3) | (TMM_InitStruct->TMM_BDTR.TMM_PostiveState << 2)| TMM_InitStruct->TMM_Combine_TransMode;
*TMM_reg.TMDF[TMM_InitStruct->TMM_Select] |= (TMM_InitStruct->TMM_BDTR.TMIOA_BreakPolarity << 6) |(TMM_InitStruct->TMM_BDTR.TMIOB_BreakPolarity << 4) \
| (TMM_InitStruct->TMM_BDTR.TMIOC_BreakPolarity << 2) |(TMM_InitStruct->TMM_BDTR.TMIOD_BreakPolarity << 0);
TMM->TMELC |= (TMM_InitStruct->TMM_BDTR.TMM_Break <<1) << (TMM_InitStruct->TMM_Select << 4);
TMM->OPCTL0 |= TMM_InitStruct->TMM_BDTR.TMM_Break_Release;
if((TMM_InitStruct->TMM_Channel & TMM_Channel_A) == TMM_Channel_A)
{
if(TMM_InitStruct->TMM_Select == TMM0)
{
*TMM_reg.TMGRA = TMM_InitStruct->TMM_CHA_Pulse - 1;
*TMM_reg.TMGRA = *TMM_reg.TMGRA + TMM_InitStruct->TMM_BDTR.TMM_DeadTime - 1;
*TMM_reg.TMOER1 |= TMM_Channel_A << (TMM_InitStruct->TMM_Select << 2);
}
else
{
*TMM_reg.TMGRA = TMM_InitStruct->TMM_CHA_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_A) << (TMM_InitStruct->TMM_Select << 2));
}
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_B)== TMM_Channel_B)
{
*TMM_reg.TMGRB = TMM_InitStruct->TMM_CHB_Pulse - 1;
*TMM_reg.TMOER1 &= ~((TMM_Channel_B) << (TMM_InitStruct->TMM_Select << 2));
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_C)== TMM_Channel_C)
{
*TMM_reg.TMGRC = TMM_InitStruct->TMM_CHC_Pulse - 1;
*TMM_reg.TMOER1 &= ~(TMM_Channel_C << (TMM_InitStruct->TMM_Select << 2));
}
if((TMM_InitStruct->TMM_Channel & TMM_Channel_D)== TMM_Channel_D)
{
*TMM_reg.TMGRD = TMM_InitStruct->TMM_CHD_Pulse - 1;
*TMM_reg.TMOER1 &= ~(TMM_Channel_D << (TMM_InitStruct->TMM_Select << 2));
}
if(TMM_InitStruct->TMM_Select == TMM0)
{
TMM->TM0 = TMM_InitStruct->TMM_BDTR.TMM_DeadTime;
}
TMM->TM1 = 0;
}
*TMM_reg.TMIER[TMM_InitStruct->TMM_Select] |= TMM_InitStruct->TMM_Intp;
}
/**
* @brief This function sets TM0 channel counter.
* @param None
* @retval None
*/
void TMM0_Set_Counter(uint8_t ch,uint16_t value)
{
if(ch == TMM_Channel_A)
{
TMM->TMGRA0 = value - 1;
}
else if(ch == TMM_Channel_B)
{
TMM->TMGRB0 = value - 1;
}
else if(ch == TMM_Channel_C)
{
TMM->TMGRC0 = value - 1;
}
else if(ch == TMM_Channel_D)
{
TMM->TMGRD0 = value - 1;
}
}
/**
* @brief This function sets TM1 channel counter.
* @param None
* @retval None
*/
void TMM1_Set_Counter(uint8_t ch,uint16_t value)
{
if(ch == TMM_Channel_A)
{
TMM->TMGRA1 = value - 1;
}
else if(ch == TMM_Channel_B)
{
TMM->TMGRB1 = value - 1;
}
else if(ch == TMM_Channel_C)
{
TMM->TMGRC1 = value - 1;
}
else if(ch == TMM_Channel_D)
{
TMM->TMGRD1 = value - 1;
}
}
/**
* @brief This function starts TM0 and TM1 counter.
* @param None
* @retval None
*/
void TMM_All_Start(void)
{
TMM->TMSTR |= _01_TMM_TM0_COUNT_START | _02_TMM_TM1_COUNT_START;
}
/**
* @brief This function stops TM0 and TM1 counter.
* @param None
* @retval None
*/
void TMM_All_Stop(void)
{
TMM->TMSTR &= (uint8_t)~_01_TMM_TM0_COUNT_START;
TMM->TMSTR &= (uint8_t)~_02_TMM_TM1_COUNT_START;
}
/**
* @brief This function starts TMM0 counter.
* @param match_flag : it indicated whether counter TM0 continues count
* after matching compare register TMGRA0
* @arg ENABLE: countine count after matching compare register TMGRA0
* @arg DISABLE: stop count after matching compare register TMGRA0
* @retval None
*/
void TMM0_Start(FunctionalState match_flag)
{
volatile uint8_t tmsr_dummy;
tmsr_dummy = TMM->TMSR0; /* read TMSR0 before write 0 */
TMM->TMSR0 = 0x00U; /* clear TM0 each interrupt request */
if(match_flag == ENABLE)
{
TMM->TMSTR |= _04_TMM_TM0_COUNT_CONTINUES;
}
else
{
TMM->TMSTR |= _00_TMM_TM0_COUNT_STOP_MATCH;
}
TMM->TMSTR |= _01_TMM_TM0_COUNT_START;
}
/**
* @brief This function stops TMM0 counter.
* @param None
* @retval None
*/
void TMM0_Stop(void)
{
volatile uint8_t tmsr_dummy;
TMM->TMSTR |= _04_TMM_TM0_COUNT_CONTINUES;
TMM->TMSTR &= (uint8_t)~_01_TMM_TM0_COUNT_START;
tmsr_dummy = TMM->TMSR0; /* read TMSR0 before write 0 */
TMM->TMSR0 = 0x00U; /* clear TM0 each interrupt request */
}
/**
* @brief This function starts TMM1 counter.
* @param match_flag : it indicated whether counter TM0 continues count
* after matching compare register TMGRA0
* @arg ENABLE: countine count after matching compare register TMGRA0
* @arg DISABLE: stop count after matching compare register TMGRA0
*/
void TMM1_Start(FunctionalState match_flag)
{
volatile uint8_t tmsr_dummy;
tmsr_dummy = TMM->TMSR1; /* read TMSR1 before write 0 */
TMM->TMSR1 = 0x00U; /* clear TM1 each interrupt request */
if(match_flag == ENABLE)
{
TMM->TMSTR |= _08_TMM_TM1_COUNT_CONTINUES;
}
else
{
TMM->TMSTR |= _00_TMM_TM1_COUNT_STOP_MATCH;
}
TMM->TMSTR |= _02_TMM_TM1_COUNT_START;
}
/**
* @brief This function stops TMM1 counter.
* @param None
* @retval None
*/
void TMM1_Stop(void)
{
volatile uint8_t tmsr_dummy;
TMM->TMSTR |= _08_TMM_TM1_COUNT_CONTINUES;
TMM->TMSTR &= (uint8_t)~_02_TMM_TM1_COUNT_START;
tmsr_dummy = TMM->TMSR1; /* read TMSR1 before write 0 */
TMM->TMSR1 = 0x00U; /* clear TM0 each interrupt request */
}
/**
* @brief Checks whether the specified TMM_FLAG flag is set or not.
* @param TMMx: TMM peripheral select.
* @param TMM_FLAG: specifies the flag to check.
* This parameter can be one of the following values,whrer x is 0 or 1:
* @arg TMM_FLAG_MATCHA: TMx count matchs with Capture Compare RegisterTMGRA0
* @arg TMM_FLAG_MATCHB: TMx count matchs with Capture Compare RegisterTMGRB0
* @arg TMM_FLAG_MATCHC: TMx count matchs with Capture Compare RegisterTMGRC0
* @arg TMM_FLAG_MATCHD: TMx count matchs with Capture Compare RegisterTMGRD0
* @arg TMM_FLAG_OVFLOW: TMx count overflow flag
* @retval The new state of UART_FLAG (SET or RESET).
*/
FlagStatus TMM_GetFlagStatus(TMMSelect_TypeDef TMMx, uint8_t TMM_FLAG)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_TMM_FLAG(TMM_FLAG));
assert_param(IS_TMM_SELECT(TMMx));
/* Check the parameters */
if(TMMx == TMM0)
{
if ((TMM->TMSR0 & TMM_FLAG) != (uint16_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
}
if(TMMx == TMM1)
{
if ((TMM->TMSR1 & TMM_FLAG) != (uint16_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
}
return bitstatus;
}
uint32_t TMM_GetPulseWidth(TMMSelect_TypeDef TMMx, uint8_t channel)
{
uint8_t tmsr_temp;
uint8_t tmier_temp;
int32_t width=0;
/* Check the parameters */
assert_param(IS_TMM_SELECT(TMMx));
TMM_RegSetting(TMMx);
tmsr_temp = *TMM_reg.TMSR;
tmier_temp = *TMM_reg.TMIER[TMMx];
if (INTC_GetPendingIRQ(TMM0_IRQn) == 1)
{
INTC_ClearPendingIRQ(TMM0_IRQn); /* clear INTTMA interrupt flag */
}
else if(INTC_GetPendingIRQ(TMM1_IRQn) == 1)
{
INTC_ClearPendingIRQ(TMM1_IRQn); /* clear INTTMA interrupt flag */
}
else
{
return 0;
}
if (TMM_GetFlagStatus(TMMx,TMM_FLAG_OVFLOW) == SET)
{
*TMM_reg.TMSR = tmsr_temp & ~TMM_FLAG_OVFLOW;
// tmm_overflow_count_a += 1UL;
// tmm_overflow_count_b += 1UL;
}
if(channel == TMM_Channel_A)
{
if (TMM_GetFlagStatus(TMMx,TMM_FLAG_MATCHA) == SET)
{
*TMM_reg.TMSR = tmsr_temp & ~TMM_FLAG_MATCHA;
width = (*TMM_reg.TMGRA - *TMM_reg.TMGRC);
tmm_overflow_count_a = 0;
if(width < 0)
{
width += 0xffff;
}
}
}
if(channel == TMM_Channel_B)
{
if (TMM_GetFlagStatus(TMMx,TMM_FLAG_MATCHB) == SET)
{
*TMM_reg.TMSR = tmsr_temp & ~TMM_FLAG_MATCHB;
width = (*TMM_reg.TMGRB - *TMM_reg.TMGRD);
tmm_overflow_count_b = 0;
if(width < 0)
{
width += 0xffff;
}
}
}
*TMM_reg.TMIER[TMMx] = tmier_temp;
return width;
}