-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathch32v00x_tim.c
2345 lines (2173 loc) · 80.3 KB
/
ch32v00x_tim.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v00x_tim.c
* Author : WCH
* Version : V1.0.0
* Date : 2022/08/08
* Description : This file provides all the TIM firmware functions.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include <ch32v00x_rcc.h>
#include <ch32v00x_tim.h>
/* TIM registers bit mask */
#define SMCFGR_ETR_Mask ((uint16_t)0x00FF)
#define CHCTLR_Offset ((uint16_t)0x0018)
#define CCER_CCE_Set ((uint16_t)0x0001)
#define CCER_CCNE_Set ((uint16_t)0x0004)
static void TI1_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter);
static void TI2_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter);
static void TI3_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter);
static void TI4_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter);
/*********************************************************************
* @fn TIM_DeInit
*
* @brief Deinitializes the TIMx peripheral registers to their default
* reset values.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
*
* @return none
*/
void TIM_DeInit(TIM_TypeDef *TIMx)
{
if(TIMx == TIM1)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, DISABLE);
}
else if(TIMx == TIM2)
{
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);
}
}
/*********************************************************************
* @fn TIM_TimeBaseInit
*
* @brief Initializes the TIMx Time Base Unit peripheral according to
* the specified parameters in the TIM_TimeBaseInitStruct.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_TimeBaseInitStruct - pointer to a TIM_TimeBaseInitTypeDef
* structure.
*
* @return none
*/
void TIM_TimeBaseInit(TIM_TypeDef *TIMx, TIM_TimeBaseInitTypeDef *TIM_TimeBaseInitStruct)
{
uint16_t tmpcr1 = 0;
tmpcr1 = TIMx->CTLR1;
if((TIMx == TIM1) || (TIMx == TIM2))
{
tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_DIR | TIM_CMS)));
tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_CounterMode;
}
tmpcr1 &= (uint16_t)(~((uint16_t)TIM_CTLR1_CKD));
tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_ClockDivision;
TIMx->CTLR1 = tmpcr1;
TIMx->ATRLR = TIM_TimeBaseInitStruct->TIM_Period;
TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
if(TIMx == TIM1)
{
TIMx->RPTCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter;
}
TIMx->SWEVGR = TIM_PSCReloadMode_Immediate;
}
/*********************************************************************
* @fn TIM_OC1Init
*
* @brief Initializes the TIMx Channel1 according to the specified
* parameters in the TIM_OCInitStruct.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OC1Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
{
uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
TIMx->CCER &= (uint16_t)(~(uint16_t)TIM_CC1E);
tmpccer = TIMx->CCER;
tmpcr2 = TIMx->CTLR2;
tmpccmrx = TIMx->CHCTLR1;
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC1M));
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC1S));
tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC1P));
tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
tmpccer |= TIM_OCInitStruct->TIM_OutputState;
if(TIMx == TIM1)
{
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC1NP));
tmpccer |= TIM_OCInitStruct->TIM_OCNPolarity;
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC1NE));
tmpccer |= TIM_OCInitStruct->TIM_OutputNState;
tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS1));
tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS1N));
tmpcr2 |= TIM_OCInitStruct->TIM_OCIdleState;
tmpcr2 |= TIM_OCInitStruct->TIM_OCNIdleState;
}
TIMx->CTLR2 = tmpcr2;
TIMx->CHCTLR1 = tmpccmrx;
TIMx->CH1CVR = TIM_OCInitStruct->TIM_Pulse;
TIMx->CCER = tmpccer;
}
/*********************************************************************
* @fn TIM_OC2Init
*
* @brief Initializes the TIMx Channel2 according to the specified
* parameters in the TIM_OCInitStruct.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OC2Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
{
uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CC2E));
tmpccer = TIMx->CCER;
tmpcr2 = TIMx->CTLR2;
tmpccmrx = TIMx->CHCTLR1;
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC2M));
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC2S));
tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC2P));
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 4);
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 4);
if(TIMx == TIM1)
{
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC2NP));
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 4);
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC2NE));
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 4);
tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS2));
tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS2N));
tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 2);
tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 2);
}
TIMx->CTLR2 = tmpcr2;
TIMx->CHCTLR1 = tmpccmrx;
TIMx->CH2CVR = TIM_OCInitStruct->TIM_Pulse;
TIMx->CCER = tmpccer;
}
/*********************************************************************
* @fn TIM_OC3Init
*
* @brief Initializes the TIMx Channel3 according to the specified
* parameters in the TIM_OCInitStruct.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OC3Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
{
uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CC3E));
tmpccer = TIMx->CCER;
tmpcr2 = TIMx->CTLR2;
tmpccmrx = TIMx->CHCTLR2;
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC3M));
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC3S));
tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC3P));
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 8);
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 8);
if(TIMx == TIM1)
{
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC3NP));
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 8);
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC3NE));
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 8);
tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS3));
tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS3N));
tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 4);
tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 4);
}
TIMx->CTLR2 = tmpcr2;
TIMx->CHCTLR2 = tmpccmrx;
TIMx->CH3CVR = TIM_OCInitStruct->TIM_Pulse;
TIMx->CCER = tmpccer;
}
/*********************************************************************
* @fn TIM_OC4Init
*
* @brief Initializes the TIMx Channel4 according to the specified
* parameters in the TIM_OCInitStruct.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OC4Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
{
uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CC4E));
tmpccer = TIMx->CCER;
tmpcr2 = TIMx->CTLR2;
tmpccmrx = TIMx->CHCTLR2;
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC4M));
tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC4S));
tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
tmpccer &= (uint16_t)(~((uint16_t)TIM_CC4P));
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 12);
tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 12);
if(TIMx == TIM1)
{
tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS4));
tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 6);
}
TIMx->CTLR2 = tmpcr2;
TIMx->CHCTLR2 = tmpccmrx;
TIMx->CH4CVR = TIM_OCInitStruct->TIM_Pulse;
TIMx->CCER = tmpccer;
}
/*********************************************************************
* @fn TIM_ICInit
*
* @brief IInitializes the TIM peripheral according to the specified
* parameters in the TIM_ICInitStruct.
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ICInitStruct - pointer to a TIM_ICInitTypeDef structure.
*
* @return none
*/
void TIM_ICInit(TIM_TypeDef *TIMx, TIM_ICInitTypeDef *TIM_ICInitStruct)
{
if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
{
TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
else if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)
{
TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
else if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)
{
TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
else
{
TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
}
/*********************************************************************
* @fn TIM_PWMIConfig
*
* @brief Configures the TIM peripheral according to the specified
* parameters in the TIM_ICInitStruct to measure an external
* PWM signal.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ICInitStruct - pointer to a TIM_ICInitTypeDef structure.
*
* @return none
*/
void TIM_PWMIConfig(TIM_TypeDef *TIMx, TIM_ICInitTypeDef *TIM_ICInitStruct)
{
uint16_t icoppositepolarity = TIM_ICPolarity_Rising;
uint16_t icoppositeselection = TIM_ICSelection_DirectTI;
if(TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
{
icoppositepolarity = TIM_ICPolarity_Falling;
}
else
{
icoppositepolarity = TIM_ICPolarity_Rising;
}
if(TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
{
icoppositeselection = TIM_ICSelection_IndirectTI;
}
else
{
icoppositeselection = TIM_ICSelection_DirectTI;
}
if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
{
TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
else
{
TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
}
}
/*********************************************************************
* @fn TIM_BDTRConfig
*
* @brief Configures the: Break feature, dead time, Lock level, the OSSI,
* the OSSR State and the AOE(automatic output enable).
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_BDTRInitStruct - pointer to a TIM_BDTRInitTypeDef structure.
*
* @return none
*/
void TIM_BDTRConfig(TIM_TypeDef *TIMx, TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
{
TIMx->BDTR = (uint32_t)TIM_BDTRInitStruct->TIM_OSSRState | TIM_BDTRInitStruct->TIM_OSSIState |
TIM_BDTRInitStruct->TIM_LOCKLevel | TIM_BDTRInitStruct->TIM_DeadTime |
TIM_BDTRInitStruct->TIM_Break | TIM_BDTRInitStruct->TIM_BreakPolarity |
TIM_BDTRInitStruct->TIM_AutomaticOutput;
}
/*********************************************************************
* @fn TIM_TimeBaseStructInit
*
* @brief Fills each TIM_TimeBaseInitStruct member with its default value.
*
* @param TIM_TimeBaseInitStruct - pointer to a TIM_TimeBaseInitTypeDef structure.
*
* @return none
*/
void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef *TIM_TimeBaseInitStruct)
{
TIM_TimeBaseInitStruct->TIM_Period = 0xFFFF;
TIM_TimeBaseInitStruct->TIM_Prescaler = 0x0000;
TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct->TIM_RepetitionCounter = 0x0000;
}
/*********************************************************************
* @fn TIM_OCStructInit
*
* @brief Fills each TIM_OCInitStruct member with its default value.
*
* @param TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OCStructInit(TIM_OCInitTypeDef *TIM_OCInitStruct)
{
TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct->TIM_Pulse = 0x0000;
TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
}
/*********************************************************************
* @fn TIM_ICStructInit
*
* @brief Fills each TIM_ICInitStruct member with its default value.
*
* @param TIM_ICInitStruct - pointer to a TIM_ICInitTypeDef structure.
*
* @return none
*/
void TIM_ICStructInit(TIM_ICInitTypeDef *TIM_ICInitStruct)
{
TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct->TIM_ICFilter = 0x00;
}
/*********************************************************************
* @fn TIM_BDTRStructInit
*
* @brief Fills each TIM_BDTRInitStruct member with its default value.
*
* @param TIM_BDTRInitStruct - pointer to a TIM_BDTRInitTypeDef structure.
*
* @return none
*/
void TIM_BDTRStructInit(TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
{
TIM_BDTRInitStruct->TIM_OSSRState = TIM_OSSRState_Disable;
TIM_BDTRInitStruct->TIM_OSSIState = TIM_OSSIState_Disable;
TIM_BDTRInitStruct->TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStruct->TIM_DeadTime = 0x00;
TIM_BDTRInitStruct->TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStruct->TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStruct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
}
/*********************************************************************
* @fn TIM_Cmd
*
* @brief Enables or disables the specified TIM peripheral.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* NewState - ENABLE or DISABLE.
*
* @return none
*/
void TIM_Cmd(TIM_TypeDef *TIMx, FunctionalState NewState)
{
if(NewState != DISABLE)
{
TIMx->CTLR1 |= TIM_CEN;
}
else
{
TIMx->CTLR1 &= (uint16_t)(~((uint16_t)TIM_CEN));
}
}
/*********************************************************************
* @fn TIM_CtrlPWMOutputs
*
* @brief Enables or disables the TIM peripheral Main Outputs.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* NewState - ENABLE or DISABLE.
*
* @return none
*/
void TIM_CtrlPWMOutputs(TIM_TypeDef *TIMx, FunctionalState NewState)
{
if(NewState != DISABLE)
{
TIMx->BDTR |= TIM_MOE;
}
else
{
TIMx->BDTR &= (uint16_t)(~((uint16_t)TIM_MOE));
}
}
/*********************************************************************
* @fn TIM_ITConfig
*
* @brief Enables or disables the specified TIM interrupts.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_IT - specifies the TIM interrupts sources to be enabled or disabled.
* TIM_IT_Update - TIM update Interrupt source.
* TIM_IT_CC1 - TIM Capture Compare 1 Interrupt source.
* TIM_IT_CC2 - TIM Capture Compare 2 Interrupt source
* TIM_IT_CC3 - TIM Capture Compare 3 Interrupt source.
* TIM_IT_CC4 - TIM Capture Compare 4 Interrupt source.
* TIM_IT_COM - TIM Commutation Interrupt source.
* TIM_IT_Trigger - TIM Trigger Interrupt source.
* TIM_IT_Break - TIM Break Interrupt source.
* NewState - ENABLE or DISABLE.
*
* @return none
*/
void TIM_ITConfig(TIM_TypeDef *TIMx, uint16_t TIM_IT, FunctionalState NewState)
{
if(NewState != DISABLE)
{
TIMx->DMAINTENR |= TIM_IT;
}
else
{
TIMx->DMAINTENR &= (uint16_t)~TIM_IT;
}
}
/*********************************************************************
* @fn TIM_GenerateEvent
*
* @brief Configures the TIMx event to be generate by software.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_IT - specifies the TIM interrupts sources to be enabled or disabled.
* TIM_IT_Update - TIM update Interrupt source.
* TIM_IT_CC1 - TIM Capture Compare 1 Interrupt source.
* TIM_IT_CC2 - TIM Capture Compare 2 Interrupt source
* TIM_IT_CC3 - TIM Capture Compare 3 Interrupt source.
* TIM_IT_CC4 - TIM Capture Compare 4 Interrupt source.
* TIM_IT_COM - TIM Commutation Interrupt source.
* TIM_IT_Trigger - TIM Trigger Interrupt source.
* TIM_IT_Break - TIM Break Interrupt source.
* NewState - ENABLE or DISABLE.
*
* @return none
*/
void TIM_GenerateEvent(TIM_TypeDef *TIMx, uint16_t TIM_EventSource)
{
TIMx->SWEVGR = TIM_EventSource;
}
/*********************************************************************
* @fn TIM_DMAConfig
*
* @brief Configures the TIMx's DMA interface.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_DMABase: DMA Base address.
* TIM_DMABase_CR.
* TIM_DMABase_CR2.
* TIM_DMABase_SMCR.
* TIM_DMABase_DIER.
* TIM1_DMABase_SR.
* TIM_DMABase_EGR.
* TIM_DMABase_CCMR1.
* TIM_DMABase_CCMR2.
* TIM_DMABase_CCER.
* TIM_DMABase_CNT.
* TIM_DMABase_PSC.
* TIM_DMABase_CCR1.
* TIM_DMABase_CCR2.
* TIM_DMABase_CCR3.
* TIM_DMABase_CCR4.
* TIM_DMABase_BDTR.
* TIM_DMABase_DCR.
* TIM_DMABurstLength - DMA Burst length.
* TIM_DMABurstLength_1Transfer.
* TIM_DMABurstLength_18Transfers.
*
* @return none
*/
void TIM_DMAConfig(TIM_TypeDef *TIMx, uint16_t TIM_DMABase, uint16_t TIM_DMABurstLength)
{
TIMx->DMACFGR = TIM_DMABase | TIM_DMABurstLength;
}
/*********************************************************************
* @fn TIM_DMACmd
*
* @brief Enables or disables the TIMx's DMA Requests.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_DMASource - specifies the DMA Request sources.
* TIM_DMA_Update - TIM update Interrupt source.
* TIM_DMA_CC1 - TIM Capture Compare 1 DMA source.
* TIM_DMA_CC2 - TIM Capture Compare 2 DMA source.
* TIM_DMA_CC3 - TIM Capture Compare 3 DMA source.
* TIM_DMA_CC4 - TIM Capture Compare 4 DMA source.
* TIM_DMA_COM - TIM Commutation DMA source.
* TIM_DMA_Trigger - TIM Trigger DMA source.
* NewState - ENABLE or DISABLE.
*
* @return none
*/
void TIM_DMACmd(TIM_TypeDef *TIMx, uint16_t TIM_DMASource, FunctionalState NewState)
{
if(NewState != DISABLE)
{
TIMx->DMAINTENR |= TIM_DMASource;
}
else
{
TIMx->DMAINTENR &= (uint16_t)~TIM_DMASource;
}
}
/*********************************************************************
* @fn TIM_InternalClockConfig
*
* @brief Configures the TIMx internal Clock.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
*
* @return none
*/
void TIM_InternalClockConfig(TIM_TypeDef *TIMx)
{
TIMx->SMCFGR &= (uint16_t)(~((uint16_t)TIM_SMS));
}
/*********************************************************************
* @fn TIM_ITRxExternalClockConfig
*
* @brief Configures the TIMx Internal Trigger as External Clock.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_InputTriggerSource: Trigger source.
* TIM_TS_ITR0 - Internal Trigger 0.
* TIM_TS_ITR1 - Internal Trigger 1.
* TIM_TS_ITR2 - Internal Trigger 2.
* TIM_TS_ITR3 - Internal Trigger 3.
*
* @return none
*/
void TIM_ITRxExternalClockConfig(TIM_TypeDef *TIMx, uint16_t TIM_InputTriggerSource)
{
TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);
TIMx->SMCFGR |= TIM_SlaveMode_External1;
}
/*********************************************************************
* @fn TIM_TIxExternalClockConfig
*
* @brief Configures the TIMx Trigger as External Clock.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_TIxExternalCLKSource - Trigger source.
* TIM_TIxExternalCLK1Source_TI1ED - TI1 Edge Detector.
* TIM_TIxExternalCLK1Source_TI1 - Filtered Timer Input 1.
* TIM_TIxExternalCLK1Source_TI2 - Filtered Timer Input 2.
* TIM_ICPolarity - specifies the TIx Polarity.
* TIM_ICPolarity_Rising.
* TIM_ICPolarity_Falling.
* TIM_DMA_COM - TIM Commutation DMA source.
* TIM_DMA_Trigger - TIM Trigger DMA source.
* ICFilter - specifies the filter value.
* This parameter must be a value between 0x0 and 0xF.
*
* @return none
*/
void TIM_TIxExternalClockConfig(TIM_TypeDef *TIMx, uint16_t TIM_TIxExternalCLKSource,
uint16_t TIM_ICPolarity, uint16_t ICFilter)
{
if(TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)
{
TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
}
else
{
TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
}
TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);
TIMx->SMCFGR |= TIM_SlaveMode_External1;
}
/*********************************************************************
* @fn TIM_ETRClockMode1Config
*
* @brief Configures the External clock Mode1.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ExtTRGPrescaler - The external Trigger Prescaler.
* TIM_ExtTRGPSC_OFF - ETRP Prescaler OFF.
* TIM_ExtTRGPSC_DIV2 - ETRP frequency divided by 2.
* TIM_ExtTRGPSC_DIV4 - ETRP frequency divided by 4.
* TIM_ExtTRGPSC_DIV8 - ETRP frequency divided by 8.
* TIM_ExtTRGPolarity - The external Trigger Polarity.
* TIM_ExtTRGPolarity_Inverted - active low or falling edge active.
* TIM_ExtTRGPolarity_NonInverted - active high or rising edge active.
* ExtTRGFilter - External Trigger Filter.
* This parameter must be a value between 0x0 and 0xF.
*
* @return none
*/
void TIM_ETRClockMode1Config(TIM_TypeDef *TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
uint16_t ExtTRGFilter)
{
uint16_t tmpsmcr = 0;
TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
tmpsmcr = TIMx->SMCFGR;
tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMS));
tmpsmcr |= TIM_SlaveMode_External1;
tmpsmcr &= (uint16_t)(~((uint16_t)TIM_TS));
tmpsmcr |= TIM_TS_ETRF;
TIMx->SMCFGR = tmpsmcr;
}
/*********************************************************************
* @fn TIM_ETRClockMode2Config
*
* @brief Configures the External clock Mode2.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ExtTRGPrescaler - The external Trigger Prescaler.
* TIM_ExtTRGPSC_OFF - ETRP Prescaler OFF.
* TIM_ExtTRGPSC_DIV2 - ETRP frequency divided by 2.
* TIM_ExtTRGPSC_DIV4 - ETRP frequency divided by 4.
* TIM_ExtTRGPSC_DIV8 - ETRP frequency divided by 8.
* TIM_ExtTRGPolarity - The external Trigger Polarity.
* TIM_ExtTRGPolarity_Inverted - active low or falling edge active.
* TIM_ExtTRGPolarity_NonInverted - active high or rising edge active.
* ExtTRGFilter - External Trigger Filter.
* This parameter must be a value between 0x0 and 0xF.
*
* @return none
*/
void TIM_ETRClockMode2Config(TIM_TypeDef *TIMx, uint16_t TIM_ExtTRGPrescaler,
uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
{
TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
TIMx->SMCFGR |= TIM_ECE;
}
/*********************************************************************
* @fn TIM_ETRConfig
*
* @brief Configures the TIMx External Trigger (ETR).
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ExtTRGPrescaler - The external Trigger Prescaler.
* TIM_ExtTRGPSC_OFF - ETRP Prescaler OFF.
* TIM_ExtTRGPSC_DIV2 - ETRP frequency divided by 2.
* TIM_ExtTRGPSC_DIV4 - ETRP frequency divided by 4.
* TIM_ExtTRGPSC_DIV8 - ETRP frequency divided by 8.
* TIM_ExtTRGPolarity - The external Trigger Polarity.
* TIM_ExtTRGPolarity_Inverted - active low or falling edge active.
* TIM_ExtTRGPolarity_NonInverted - active high or rising edge active.
* ExtTRGFilter - External Trigger Filter.
* This parameter must be a value between 0x0 and 0xF.
*
* @return none
*/
void TIM_ETRConfig(TIM_TypeDef *TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
uint16_t ExtTRGFilter)
{
uint16_t tmpsmcr = 0;
tmpsmcr = TIMx->SMCFGR;
tmpsmcr &= SMCFGR_ETR_Mask;
tmpsmcr |= (uint16_t)(TIM_ExtTRGPrescaler | (uint16_t)(TIM_ExtTRGPolarity | (uint16_t)(ExtTRGFilter << (uint16_t)8)));
TIMx->SMCFGR = tmpsmcr;
}
/*********************************************************************
* @fn TIM_PrescalerConfig
*
* @brief Configures the TIMx Prescaler.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* Prescaler - specifies the Prescaler Register value.
* TIM_PSCReloadMode - specifies the TIM Prescaler Reload mode.
* TIM_PSCReloadMode - specifies the TIM Prescaler Reload mode.
* TIM_PSCReloadMode_Update - The Prescaler is loaded at the update event.
* TIM_PSCReloadMode_Immediate - The Prescaler is loaded immediately.
*
* @return none
*/
void TIM_PrescalerConfig(TIM_TypeDef *TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
{
TIMx->PSC = Prescaler;
TIMx->SWEVGR = TIM_PSCReloadMode;
}
/*********************************************************************
* @fn TIM_CounterModeConfig
*
* @brief Specifies the TIMx Counter Mode to be used.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_CounterMode - specifies the Counter Mode to be used.
* TIM_CounterMode_Up - TIM Up Counting Mode.
* TIM_CounterMode_Down - TIM Down Counting Mode.
* TIM_CounterMode_CenterAligned1 - TIM Center Aligned Mode1.
* TIM_CounterMode_CenterAligned2 - TIM Center Aligned Mode2.
* TIM_CounterMode_CenterAligned3 - TIM Center Aligned Mode3.
*
* @return none
*/
void TIM_CounterModeConfig(TIM_TypeDef *TIMx, uint16_t TIM_CounterMode)
{
uint16_t tmpcr1 = 0;
tmpcr1 = TIMx->CTLR1;
tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_DIR | TIM_CMS)));
tmpcr1 |= TIM_CounterMode;
TIMx->CTLR1 = tmpcr1;
}
/*********************************************************************
* @fn TIM_SelectInputTrigger
*
* @brief Selects the Input Trigger source.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_InputTriggerSource - The Input Trigger source.
* TIM_TS_ITR0 - Internal Trigger 0.
* TIM_TS_ITR1 - Internal Trigger 1.
* TIM_TS_ITR2 - Internal Trigger 2.
* TIM_TS_ITR3 - Internal Trigger 3.
* TIM_TS_TI1F_ED - TI1 Edge Detector.
* TIM_TS_TI1FP1 - Filtered Timer Input 1.
* TIM_TS_TI2FP2 - Filtered Timer Input 2.
* TIM_TS_ETRF - External Trigger input.
*
* @return none
*/
void TIM_SelectInputTrigger(TIM_TypeDef *TIMx, uint16_t TIM_InputTriggerSource)
{
uint16_t tmpsmcr = 0;
tmpsmcr = TIMx->SMCFGR;
tmpsmcr &= (uint16_t)(~((uint16_t)TIM_TS));
tmpsmcr |= TIM_InputTriggerSource;
TIMx->SMCFGR = tmpsmcr;
}
/*********************************************************************
* @fn TIM_EncoderInterfaceConfig
*
* @brief Configures the TIMx Encoder Interface.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_EncoderMode - specifies the TIMx Encoder Mode.
* TIM_EncoderMode_TI1 - Counter counts on TI1FP1 edge depending
* on TI2FP2 level.
* TIM_EncoderMode_TI2 - Counter counts on TI2FP2 edge depending
* on TI1FP1 level.
* TIM_EncoderMode_TI12 - Counter counts on both TI1FP1 and
* TI2FP2 edges depending.
* TIM_IC1Polarity - specifies the IC1 Polarity.
* TIM_ICPolarity_Falling - IC Falling edge.
* TTIM_ICPolarity_Rising - IC Rising edge.
* TIM_IC2Polarity - specifies the IC2 Polarity.
* TIM_ICPolarity_Falling - IC Falling edge.
* TIM_ICPolarity_Rising - IC Rising edge.
*
* @return none
*/
void TIM_EncoderInterfaceConfig(TIM_TypeDef *TIMx, uint16_t TIM_EncoderMode,
uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
{
uint16_t tmpsmcr = 0;
uint16_t tmpccmr1 = 0;
uint16_t tmpccer = 0;
tmpsmcr = TIMx->SMCFGR;
tmpccmr1 = TIMx->CHCTLR1;
tmpccer = TIMx->CCER;
tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMS));
tmpsmcr |= TIM_EncoderMode;
tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CC1S)) & (uint16_t)(~((uint16_t)TIM_CC2S)));
tmpccmr1 |= TIM_CC1S_0 | TIM_CC2S_0;
tmpccer &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CC1P)) & ((uint16_t) ~((uint16_t)TIM_CC2P)));
tmpccer |= (uint16_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
TIMx->SMCFGR = tmpsmcr;
TIMx->CHCTLR1 = tmpccmr1;
TIMx->CCER = tmpccer;
}
/*********************************************************************
* @fn TIM_ForcedOC1Config
*
* @brief Forces the TIMx output 1 waveform to active or inactive level.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ForcedAction - specifies the forced Action to be set to the
* output waveform.
* TIM_ForcedAction_Active - Force active level on OC1REF.
* TIM_ForcedAction_InActive - Force inactive level on OC1REF.
*
* @return none
*/
void TIM_ForcedOC1Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
{
uint16_t tmpccmr1 = 0;
tmpccmr1 = TIMx->CHCTLR1;
tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC1M);
tmpccmr1 |= TIM_ForcedAction;
TIMx->CHCTLR1 = tmpccmr1;
}
/*********************************************************************
* @fn TIM_ForcedOC2Config
*
* @brief Forces the TIMx output 2 waveform to active or inactive level.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ForcedAction - specifies the forced Action to be set to the
* output waveform.
* TIM_ForcedAction_Active - Force active level on OC2REF.
* TIM_ForcedAction_InActive - Force inactive level on OC2REF.
*
* @return none
*/
void TIM_ForcedOC2Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
{
uint16_t tmpccmr1 = 0;
tmpccmr1 = TIMx->CHCTLR1;
tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC2M);
tmpccmr1 |= (uint16_t)(TIM_ForcedAction << 8);
TIMx->CHCTLR1 = tmpccmr1;
}
/*********************************************************************
* @fn TIM_ForcedOC3Config
*
* @brief Forces the TIMx output 3 waveform to active or inactive level.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ForcedAction - specifies the forced Action to be set to the
* output waveform.
* TIM_ForcedAction_Active - Force active level on OC3REF.
* TIM_ForcedAction_InActive - Force inactive level on OC3REF.
*
* @return none
*/
void TIM_ForcedOC3Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
{
uint16_t tmpccmr2 = 0;
tmpccmr2 = TIMx->CHCTLR2;
tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC3M);
tmpccmr2 |= TIM_ForcedAction;
TIMx->CHCTLR2 = tmpccmr2;
}
/*********************************************************************
* @fn TIM_ForcedOC4Config
*
* @brief Forces the TIMx output 4 waveform to active or inactive level.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* TIM_ForcedAction - specifies the forced Action to be set to the
* output waveform.
* TIM_ForcedAction_Active - Force active level on OC4REF.
* TIM_ForcedAction_InActive - Force inactive level on OC4REF.
*
* @return none
*/
void TIM_ForcedOC4Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
{
uint16_t tmpccmr2 = 0;
tmpccmr2 = TIMx->CHCTLR2;
tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC4M);
tmpccmr2 |= (uint16_t)(TIM_ForcedAction << 8);
TIMx->CHCTLR2 = tmpccmr2;
}
/*********************************************************************
* @fn TIM_ARRPreloadConfig
*
* @brief Enables or disables TIMx peripheral Preload register on ARR.
*
* @param TIMx - where x can be 1 to 2 to select the TIM peripheral.
* NewState - ENABLE or DISABLE.
*
* @return none
*/
void TIM_ARRPreloadConfig(TIM_TypeDef *TIMx, FunctionalState NewState)
{