summaryrefslogtreecommitdiff
path: root/bsp/gd32103c-eval/Libraries/GD32F1xx_standard_peripheral/Source/gd32f10x_timer.c
blob: d5fd7fdf744d6ec16a1cc4bb86dba55c7adfa718 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
/**
 ******************************************************************************
  * @brief   TIMER functions of the firmware library.
 ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "gd32f10x_timer.h"
#include "gd32f10x_rcc.h"

/** @addtogroup GD32F10x_Firmware
  * @{
  */

/** @defgroup TIMER
  * @brief TIMER driver modules
  * @{
  */

/** @defgroup TIMER_Private_Defines
  * @{
  */
/* TIMER registers bit mask */
#define SMC_ETR_MASK               ((uint16_t)0x00FF)
#define CHCTLR_OFFSET              ((uint16_t)0x0018)
#define CHE_CHE_SET                ((uint16_t)0x0001)
#define CHE_CHNE_SET               ((uint16_t)0x0004)
/**
  * @}
  */

/* Private function prototypes */
static void TI1_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter);
static void TI2_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter);
static void TI3_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter);
static void TI4_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter);

/** @defgroup TIMER_Private_Functions
  * @{
  */

/**
  * @brief  Deinitialize the TIMER .
  * @param  TIMERx:  x ={ 0-13 } .
  * @retval None
  */
void TIMER_DeInit(TIMER_TypeDef *TIMERx)
{
    if (TIMERx == TIMER0) {
        RCC->APB2RCR |= RCC_APB2PERIPH_TIMER0RST;
        RCC->APB2RCR &= ~RCC_APB2PERIPH_TIMER0;
    } else if (TIMERx == TIMER1) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER1RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER1;
    } else if (TIMERx == TIMER2) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER2RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER2;
    } else if (TIMERx == TIMER3) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER3RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER3;
    } else if (TIMERx == TIMER4) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER4RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER4;
    } else if (TIMERx == TIMER5) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER5RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER5;
    } else if (TIMERx == TIMER6) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER6RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER6;
    } else if (TIMERx == TIMER7) {
        RCC->APB2RCR |= RCC_APB2PERIPH_TIMER7RST;
        RCC->APB2RCR &= ~RCC_APB2PERIPH_TIMER7;
    } else if (TIMERx == TIMER8) {
        RCC->APB2RCR |= RCC_APB2PERIPH_TIMER8RST;
        RCC->APB2RCR &= ~RCC_APB2PERIPH_TIMER8;
    } else if (TIMERx == TIMER9) {
        RCC->APB2RCR |= RCC_APB2PERIPH_TIMER9RST;
        RCC->APB2RCR &= ~RCC_APB2PERIPH_TIMER9;
    } else if (TIMERx == TIMER10) {
        RCC->APB2RCR |= RCC_APB2PERIPH_TIMER10RST;
        RCC->APB2RCR &= ~RCC_APB2PERIPH_TIMER10;
    } else if (TIMERx == TIMER11) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER11RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER11;
    } else if (TIMERx == TIMER12) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER12RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER12;
    } else if (TIMERx == TIMER13) {
        RCC->APB1RCR |= RCC_APB1PERIPH_TIMER13RST;
        RCC->APB1RCR &= ~RCC_APB1PERIPH_TIMER13;
    }
}

/**
  * @brief  Initialize the specified Timer
  * @param  TIMERx:  x ={ 0 -13 } .
  * @param  TIMER_Init: pointer to a TIMER_BaseInitPara structure.
  * @retval None
  */
void TIMER_BaseInit(TIMER_TypeDef *TIMERx, TIMER_BaseInitPara *TIMER_Init)
{
    uint16_t tmpctlr1 = 0;

    tmpctlr1 = TIMERx->CTLR1;

    if ((TIMERx == TIMER0) || (TIMERx == TIMER7) || (TIMERx == TIMER1) || (TIMERx == TIMER2) ||
            (TIMERx == TIMER3) || (TIMERx == TIMER4) || (TIMERx == TIMER8) || (TIMERx == TIMER9)
            || (TIMERx == TIMER10) || (TIMERx == TIMER11) || (TIMERx == TIMER12) || (TIMERx == TIMER13)) {
        /* Configure the Counter Mode */
        tmpctlr1 &= (uint16_t)(~((uint16_t)(TIMER_CTLR1_DIR | TIMER_CTLR1_CAM)));
        tmpctlr1 |= (uint32_t)TIMER_Init->TIMER_CounterMode;
    }

    if ((TIMERx != TIMER5) && (TIMERx != TIMER6)) {
        /* Configure the clock division */
        tmpctlr1 &= (uint16_t)(~((uint16_t)TIMER_CTLR1_CDIV));
        tmpctlr1 |= (uint32_t)TIMER_Init->TIMER_ClockDivision;
    }

    TIMERx->CTLR1 = tmpctlr1;

    /* Configure the Autoreload value */
    TIMERx->CARL = TIMER_Init->TIMER_Period ;

    /* Configure the Prescaler value */
    TIMERx->PSC = TIMER_Init->TIMER_Prescaler;

    if ((TIMERx == TIMER0)  ||
            (TIMERx == TIMER7)) {
        /* Configure the Repetition Counter value */
        TIMERx->CREP = TIMER_Init->TIMER_RepetitionCounter;
    }

    /* Generate an update event */
    TIMERx->EVG = TIMER_PSC_RELOAD_NOW;
}

/**
  * @brief  Fill each TIMER_BaseInitPara Struct member with a default value.
  * @param  TIMER_Init: pointer to a TIMER_BaseInitPara structure.
  * @retval None
  */
void TIMER_BaseStructInit(TIMER_BaseInitPara *TIMER_Init)
{
    /* Fill the default value */
    TIMER_Init->TIMER_Period                = 0xFFFFFFFF;
    TIMER_Init->TIMER_Prescaler             = 0x0000;
    TIMER_Init->TIMER_ClockDivision         = TIMER_CDIV_DIV1;
    TIMER_Init->TIMER_CounterMode           = TIMER_COUNTER_UP;
    TIMER_Init->TIMER_RepetitionCounter     = 0x0000;
}

/**
  * @brief  Configure the TIMER Prescaler.
  * @param  TIMERx:  x ={ 0-13 }
  * @param  Prescaler:  Prescaler value
  * @param  TIMER_PSCReloadMode:  Prescaler Reload mode
  *   This value will be :
  *     @arg TIMER_PSC_RELOAD_UPDATE    : The Prescaler is loaded at the next update event.
  *     @arg TIMER_PSC_RELOAD_NOW       : The Prescaler is loaded right now.
  * @retval None
  */
void TIMER_PrescalerConfig(TIMER_TypeDef *TIMERx, uint16_t Prescaler, uint16_t TIMER_PSCReloadMode)
{
    /* Set PSC */
    TIMERx->PSC = Prescaler;

    /* Choose reload mode */
    TIMERx->EVG = TIMER_PSCReloadMode;
}

/**
  * @brief  Configure the TIMER Counter Mode
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_CounterMode:  the Counter Mode
  *   This value will be :
  *     @arg TIMER_COUNTER_UP             : Up Counting Mode
  *     @arg TIMER_COUNTER_DOWN           : Down Counting Mode
  *     @arg TIMER_COUNTER_CENTER_ALIGNED1: Center Aligned Counting Mode1
  *     @arg TIMER_COUNTER_CENTER_ALIGNED2: Center Aligned Counting Mode2
  *     @arg TIMER_COUNTER_CENTER_ALIGNED3: Center Aligned Counting Mode3
  * @retval None
  */
void TIMER_CounterMode(TIMER_TypeDef *TIMERx, uint16_t TIMER_CounterMode)
{
    uint16_t tmpctlr1 = 0;

    tmpctlr1 = TIMERx->CTLR1;

    /* Reset the CAM and DIR Bits */
    tmpctlr1 &= (uint16_t)(~((uint16_t)(TIMER_CTLR1_DIR | TIMER_CTLR1_CAM)));

    /* Configures the Counter Mode */
    tmpctlr1 |= TIMER_CounterMode;

    /* Update the TIMER CTLR1 */
    TIMERx->CTLR1 = tmpctlr1;
}

/**
  * @brief  Configure the TIMER Counter Register value
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  Counter: the Counter register new value.
  * @retval None
  */
void TIMER_SetCounter(TIMER_TypeDef *TIMERx, uint32_t Counter)
{
    TIMERx->CNT = Counter;
}

/**
  * @brief  Configure the Autoreload value
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  AutoReloadValue:
  * @retval None
  */
void TIMER_SetAutoreload(TIMER_TypeDef *TIMERx, uint32_t AutoReloadValue)
{
    TIMERx->CARL = AutoReloadValue;
}

/**
  * @brief  Get the Counter value.
  * @param  TIMERx:  x ={ 0-13 } .
  * @retval Counter Register value.
  */
uint32_t TIMER_GetCounter(TIMER_TypeDef *TIMERx)
{
    return TIMERx->CNT;
}

/**
  * @brief  Get the Prescaler value.
  * @param  TIMERx:  x ={ 0-13 } .
  * @retval Prescaler Register value
  */
uint16_t TIMER_GetPrescaler(TIMER_TypeDef *TIMERx)
{
    return TIMERx->PSC;
}

/**
  * @brief  Configure the TIMERx Update event.
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  NewValue: new value of the TIMERx UPDIS bit
  *   This value will be :
  *     @arg ENABLE  : Update Enbale
  *     @arg DISABLE : Update Disable
  * @retval None
  */
void TIMER_UpdateDisableConfig(TIMER_TypeDef *TIMERx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        TIMERx->CTLR1 |= TIMER_CTLR1_UPDIS;
    } else {
        TIMERx->CTLR1 &= (uint16_t)~((uint16_t)TIMER_CTLR1_UPDIS);
    }
}

/**
  * @brief  Configure the TIMER Update Request source.
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  TIMER_UpdateSrc: Configures the Update source.
  *   This value will be :
  *     @arg TIMER_UPDATE_SRC_GLOBAL    : Update generate by setting of UPG bit or the counter
  *                                       overflow/underflow , or the slave mode controller trigger.
  *     @arg TIMER_UPDATE_SRC_REGULAR   : Update generate only by counter overflow/underflow.
  * @retval None
  */
void TIMER_UpdateRequestConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_UpdateSrc)
{
    if (TIMER_UpdateSrc != TIMER_UPDATE_SRC_GLOBAL) {
        TIMERx->CTLR1 |= TIMER_CTLR1_UPS;
    } else {
        TIMERx->CTLR1 &= (uint16_t)~((uint16_t)TIMER_CTLR1_UPS);
    }
}

/**
  * @brief  Configure the CARL Preload function
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  NewValue: the state of the Preload function on CARL.
  *   This value will be :
  *     @arg ENABLE
  *     @arg DISABLE
  * @retval None
  */
void TIMER_CARLPreloadConfig(TIMER_TypeDef *TIMERx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        /* Set the CARL Preload Bit */
        TIMERx->CTLR1 |= TIMER_CTLR1_ARSE;
    } else {
        /* Reset the CARL Preload Bit */
        TIMERx->CTLR1 &= (uint16_t)~((uint16_t)TIMER_CTLR1_ARSE);
    }
}

/**
  * @brief  Select the TIMER Single Pulse Mode.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_SPMode: specifies the SPM Mode to be used.
  *   This value will be :
  *     @arg TIMER_SP_MODE_SINGLE
  *     @arg TIMER_SP_MODE_REPETITIVE
  * @retval None
  */
void TIMER_SinglePulseMode(TIMER_TypeDef *TIMERx, uint16_t TIMER_SPMode)
{
    /* Reset the SPM Bit */
    TIMERx->CTLR1 &= (uint16_t)~((uint16_t)TIMER_CTLR1_SPM);

    /* Set the SPM Bit */
    TIMERx->CTLR1 |= TIMER_SPMode;
}

/**
  * @brief  Configure the TIMERx Clock Division value.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_CDIV: the clock division value.
  *   This value will be :
  *     @arg TIMER_CDIV_DIV1: TDTS = Tck_tim
  *     @arg TIMER_CDIV_DIV2: TDTS = 2*Tck_tim
  *     @arg TIMER_CDIV_DIV4: TDTS = 4*Tck_tim
  * @retval None
  */
void TIMER_SetClockDivision(TIMER_TypeDef *TIMERx, uint16_t TIMER_CDIV)
{
    /* Reset the CDIV value*/
    TIMERx->CTLR1 &= (uint16_t)~((uint16_t)TIMER_CTLR1_CDIV);

    /* Set the CDIV value */
    TIMERx->CTLR1 |= TIMER_CDIV;
}

/**
  * @brief  ENABLE or DISABLE the TIMER.
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  NewValue: ENABLE or DISABLE.
  * @retval None
  */
void TIMER_Enable(TIMER_TypeDef *TIMERx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        /* Enable the TIMER */
        TIMERx->CTLR1 |= TIMER_CTLR1_CNTE;
    } else {
        /* Disable the TIMER */
        TIMERx->CTLR1 &= (uint16_t)(~((uint16_t)TIMER_CTLR1_CNTE));
    }
}

/**
  * @brief  Configure the: Break feature, dead time, Lock level, ROS/IOS State and the OAE
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  TIMER_BKDTInit: pointer to a TIMER_BKDTInitPara structure that
  *         contains the BKDT Register configuration  information for the TIMER.
  * @retval None
  */
void TIMER_BKDTConfig(TIMER_TypeDef *TIMERx, TIMER_BKDTInitPara *TIMER_BKDTInit)
{
    TIMERx->BKDT = (uint32_t)TIMER_BKDTInit->TIMER_ROSState      |
                   TIMER_BKDTInit->TIMER_IOSState      |
                   TIMER_BKDTInit->TIMER_LOCKLevel     |
                   TIMER_BKDTInit->TIMER_DeadTime      |
                   TIMER_BKDTInit->TIMER_Break         |
                   TIMER_BKDTInit->TIMER_BreakPolarity |
                   TIMER_BKDTInit->TIMER_OutAuto;
}

/**
  * @brief  Fill TIMER_BKDTInit structure member with default value.
  * @param  TIMER_BKDTInit :  pointer to a TIMER_BKDTInitPara structure which will be initialized.
  * @retval None
  */
void TIMER_BKDTStructInit(TIMER_BKDTInitPara *TIMER_BKDTInit)
{
    TIMER_BKDTInit->TIMER_ROSState         = TIMER_ROS_STATE_DISABLE;
    TIMER_BKDTInit->TIMER_IOSState         = TIMER_IOS_STATE_DISABLE;
    TIMER_BKDTInit->TIMER_LOCKLevel        = TIMER_LOCK_LEVEL_OFF;
    TIMER_BKDTInit->TIMER_DeadTime         = 0x00;
    TIMER_BKDTInit->TIMER_Break            = TIMER_BREAK_DISABLE;
    TIMER_BKDTInit->TIMER_BreakPolarity    = TIMER_BREAK_POLARITY_LOW;
    TIMER_BKDTInit->TIMER_OutAuto          = TIMER_OUTAUTO_DISABLE;
}

/**
  * @brief  Enable or disable the TIMER ALL Outputs.
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  NewValue: ENABLE or DISABLE .
  * @retval None
  */
void TIMER_CtrlPWMOutputs(TIMER_TypeDef *TIMERx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        /* Enable the TIMER ALL Output */
        TIMERx->BKDT |= TIMER_BKDT_POE;
    } else {
        /* Disable the TIMER ALL Output */
        TIMERx->BKDT &= (uint16_t)(~((uint16_t)TIMER_BKDT_POE));
    }
}

/**
  * @brief  Initialize the TIMERx Channel1 with TIMER_OCInitPara .
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_OCInit : pointer to a TIMER_OCInitPara structure .
  * @retval None
  */
void TIMER_OC1_Init(TIMER_TypeDef *TIMERx, TIMER_OCInitPara *TIMER_OCInit)
{
    uint16_t tmpchctlrx = 0, tmpche = 0, tmpctlr2 = 0;

    /* Disable the Channel 1: Reset the CH1E Bit */
    TIMERx->CHE &= (uint16_t)(~(uint16_t)TIMER_CHE_CH1E);

    /* Get the TIMERx CHE register value */
    tmpche = TIMERx->CHE;

    /* Get the TIMERx CTLR2 register value */
    tmpctlr2 =    TIMERx->CTLR2;

    /* Get the TIMERx CHCTLR1 register value */
    tmpchctlrx = TIMERx->CHCTLR1;

    /* Reset the Output Compare Mode Bits */
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR1_CH1OM));
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR1_CH1M));

    /* Select the Output Compare Mode */
    tmpchctlrx |= TIMER_OCInit->TIMER_OCMode;

    /* Reset the Output Polarity */
    tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH1P));

    /* Set the Output Compare Polarity */
    tmpche |= TIMER_OCInit->TIMER_OCPolarity;

    /* Set the Output State */
    tmpche |= TIMER_OCInit->TIMER_OutputState;

    if ((TIMERx == TIMER0) || (TIMERx == TIMER7)) {
        /* Reset the Output complementary Polarity */
        tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH1NP));

        /* Set the Output complementary Polarity */
        tmpche |= TIMER_OCInit->TIMER_OCNPolarity;

        /* Reset the Output complementary State */
        tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH1NE));

        /* Set the Output complementary State */
        tmpche |= TIMER_OCInit->TIMER_OutputNState;

        /* Reset the Ouput Compare and Output Compare complementary IDLE State */
        tmpctlr2 &= (uint16_t)(~((uint16_t)TIMER_CTLR2_ISO1));
        tmpctlr2 &= (uint16_t)(~((uint16_t)TIMER_CTLR2_ISO1N));

        /* Set the Output Idle state */
        tmpctlr2 |= TIMER_OCInit->TIMER_OCIdleState;

        /* Set the Output complementary Idle state */
        tmpctlr2 |= TIMER_OCInit->TIMER_OCNIdleState;
    }

    /* Write to TIMERx CTLR2 */
    TIMERx->CTLR2 = tmpctlr2;

    /* Write to TIMERx CHCTLR1 */
    TIMERx->CHCTLR1 = tmpchctlrx;

    /* Set the Capture / Compare Register value */
    TIMERx->CHCC1 = TIMER_OCInit->TIMER_Pulse;

    /* Write to TIMERx CHE */
    TIMERx->CHE = tmpche;
}

/**
  * @brief  Initialize the TIMERx Channel2 with TIMER_OCInitPara .
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_OCInit : pointer to a TIMER_OCInitPara structure .
  * @retval None
  */
void TIMER_OC2_Init(TIMER_TypeDef *TIMERx, TIMER_OCInitPara *TIMER_OCInit)
{
    uint16_t tmpchctlrx = 0, tmpche = 0, tmpctlr2 = 0;

    /* Disable the Channel 2: Reset the CH2E Bit */
    TIMERx->CHE &= (uint16_t)(~((uint16_t)TIMER_CHE_CH2E));

    /* Get the TIMERx CHE register value */
    tmpche = TIMERx->CHE;

    /* Get the TIMERx CTLR2 register value */
    tmpctlr2 =    TIMERx->CTLR2;

    /* Get the TIMERx CHCTLR1 register value */
    tmpchctlrx = TIMERx->CHCTLR1;

    /* Reset the Output Compare Mode Bits */
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR1_CH2OM));
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR1_CH2M));

    /* Select the Output Compare Mode */
    tmpchctlrx |= (uint16_t)(TIMER_OCInit->TIMER_OCMode << 8);

    /* Reset the Output Polarity */
    tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH2P));

    /* Set the Output Compare Polarity */
    tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OCPolarity << 4);

    /* Set the Output State */
    tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OutputState << 4);

    if ((TIMERx == TIMER0) || (TIMERx == TIMER7)) {
        /* Reset the Output complementary Polarity */
        tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH2NP));

        /* Set the Output complementary Polarity */
        tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OCNPolarity << 4);

        /* Reset the Output complementary State */
        tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH2NE));

        /* Set the Output complementary State */
        tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OutputNState << 4);

        /* Reset the Ouput Compare and Output Compare complementary IDLE State */
        tmpctlr2 &= (uint16_t)(~((uint16_t)TIMER_CTLR2_ISO2));
        tmpctlr2 &= (uint16_t)(~((uint16_t)TIMER_CTLR2_ISO2N));

        /* Set the Output Idle state */
        tmpctlr2 |= (uint16_t)(TIMER_OCInit->TIMER_OCIdleState << 2);

        /* Set the Output complementary Idle state */
        tmpctlr2 |= (uint16_t)(TIMER_OCInit->TIMER_OCNIdleState << 2);
    }

    /* Write to TIMERx CTLR2 */
    TIMERx->CTLR2 = tmpctlr2;

    /* Write to TIMERx CHCTLR1 */
    TIMERx->CHCTLR1 = tmpchctlrx;

    /* Set the Capture / Compare Register value */
    TIMERx->CHCC2 = TIMER_OCInit->TIMER_Pulse;

    /* Write to TIMERx CHE */
    TIMERx->CHE = tmpche;
}

/**
  * @brief  Initialize the TIMERx Channel3 with TIMER_OCInitPara .
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8} .
  * @param  TIMER_OCInit : pointer to a TIMER_OCInitPara structure .
  * @retval None
  */
void TIMER_OC3_Init(TIMER_TypeDef *TIMERx, TIMER_OCInitPara *TIMER_OCInit)
{
    uint16_t tmpchctlrx = 0, tmpche = 0, tmpctlr2 = 0;

    /* Disable the Channel 3: Reset the CH3E Bit */
    TIMERx->CHE &= (uint16_t)(~((uint16_t)TIMER_CHE_CH3E));

    /* Get the TIMERx CHE register value */
    tmpche = TIMERx->CHE;

    /* Get the TIMERx CTLR2 register value */
    tmpctlr2 =    TIMERx->CTLR2;

    /* Get the TIMERx CHCTLR2 register value */
    tmpchctlrx = TIMERx->CHCTLR2;

    /* Reset the Output Compare Mode Bits */
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR2_CH3OM));
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR2_CH3M));

    /* Select the Output Compare Mode */
    tmpchctlrx |= TIMER_OCInit->TIMER_OCMode;

    /* Reset the Output Polarity */
    tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH3P));

    /* Set the Output Compare Polarity */
    tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OCPolarity << 8);

    /* Set the Output State */
    tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OutputState << 8);

    if ((TIMERx == TIMER0) || (TIMERx == TIMER7)) {
        /* Reset the Output complementary Polarity */
        tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH3NP));

        /* Set the Output complementary Polarity */
        tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OCNPolarity << 8);

        /* Reset the Output complementary State */
        tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH3NE));

        /* Set the Output complementary State */
        tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OutputNState << 8);

        /* Reset the Ouput Compare and Output Compare complementary IDLE State */
        tmpctlr2 &= (uint16_t)(~((uint16_t)TIMER_CTLR2_ISO3));
        tmpctlr2 &= (uint16_t)(~((uint16_t)TIMER_CTLR2_ISO3N));

        /* Set the Output Idle state */
        tmpctlr2 |= (uint16_t)(TIMER_OCInit->TIMER_OCIdleState << 4);

        /* Set the Output complementary Idle state */
        tmpctlr2 |= (uint16_t)(TIMER_OCInit->TIMER_OCNIdleState << 4);
    }
    /* Write to TIMERx CTLR2 */
    TIMERx->CTLR2 = tmpctlr2;

    /* Write to TIMERx CHCTLR2 */
    TIMERx->CHCTLR2 = tmpchctlrx;

    /* Set the Capture / Compare Register value */
    TIMERx->CHCC3 = TIMER_OCInit->TIMER_Pulse;

    /* Write to TIMERx CHE */
    TIMERx->CHE = tmpche;
}

/**
  * @brief  Initialize the TIMERx Channel4 with TIMER_OCInitPara .
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8} .
  * @param  TIMER_OCInit : pointer to a TIMER_OCInitPara structure .
  * @retval None
  */
void TIMER_OC4_Init(TIMER_TypeDef *TIMERx, TIMER_OCInitPara *TIMER_OCInit)
{
    uint16_t tmpchctlrx = 0, tmpche = 0, tmpctlr2 = 0;

    /* Disable the Channel 4: Reset the CH4E Bit */
    TIMERx->CHE &= (uint16_t)(~((uint16_t)TIMER_CHE_CH4E));

    /* Get the TIMERx CHE register value */
    tmpche = TIMERx->CHE;

    /* Get the TIMERx CTLR2 register value */
    tmpctlr2 =    TIMERx->CTLR2;

    /* Get the TIMERx CHCTLR2 register value */
    tmpchctlrx = TIMERx->CHCTLR2;

    /* Reset the Output Compare Mode Bits */
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR2_CH4OM));
    tmpchctlrx &= (uint16_t)(~((uint16_t)TIMER_CHCTLR2_CH4M));

    /* Select the Output Compare Mode */
    tmpchctlrx |= (uint16_t)(TIMER_OCInit->TIMER_OCMode << 8);

    /* Reset the Output Polarity */
    tmpche &= (uint16_t)(~((uint16_t)TIMER_CHE_CH4P));

    /* Set the Output Compare Polarity */
    tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OCPolarity << 12);

    /* Set the Output State */
    tmpche |= (uint16_t)(TIMER_OCInit->TIMER_OutputState << 12);

    if ((TIMERx == TIMER0) || (TIMERx == TIMER7)) {
        /* Reset the Ouput Compare IDLE State */
        tmpctlr2 &= (uint16_t)(~((uint16_t)TIMER_CTLR2_ISO4));

        /* Set the Output Idle state */
        tmpctlr2 |= (uint16_t)(TIMER_OCInit->TIMER_OCIdleState << 6);
    }

    /* Write to TIMERx CTLR2 */
    TIMERx->CTLR2 = tmpctlr2;

    /* Write to TIMERx CHCTLR2 */
    TIMERx->CHCTLR2 = tmpchctlrx;

    /* Set the Capture Compare Register value */
    TIMERx->CHCC4 = TIMER_OCInit->TIMER_Pulse;

    /* Write to TIMERx CHE */
    TIMERx->CHE = tmpche;
}

/**
  * @brief  Fill TIMER_OCInitPara member with default value.
  * @param  TIMER_OCInit: pointer to a TIMER_OCInitPara structure.
  * @retval None
  */
void TIMER_OCStructInit(TIMER_OCInitPara *TIMER_OCInit)
{
    TIMER_OCInit->TIMER_OCMode         = TIMER_OC_MODE_TIMING;
    TIMER_OCInit->TIMER_OutputState    = TIMER_OUTPUT_STATE_DISABLE;
    TIMER_OCInit->TIMER_OutputNState   = TIMER_OUTPUTN_STATE_DISABLE;
    TIMER_OCInit->TIMER_Pulse          = 0x0000000;
    TIMER_OCInit->TIMER_OCPolarity     = TIMER_OC_POLARITY_HIGH;
    TIMER_OCInit->TIMER_OCNPolarity    = TIMER_OC_POLARITY_HIGH;
    TIMER_OCInit->TIMER_OCIdleState    = TIMER_OC_IDLE_STATE_RESET;
    TIMER_OCInit->TIMER_OCNIdleState   = TIMER_OCN_IDLE_STATE_RESET;
}

/**
  * @brief  Configure the TIMER Output Compare Mode.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_Ch:
  *   This value will be :
  *     @arg TIMER_CH_1
  *     @arg TIMER_CH_2
  *     @arg TIMER_CH_3
  *     @arg TIMER_CH_4
  * @param  TIMER_OCMode: the TIMER Output Compare Mode.
  *   This value will be :
  *     @arg TIMER_OC_MODE_TIMING
  *     @arg TIMER_OC_MODE_ACTIVE
  *     @arg TIMER_OC_MODE_TOGGLE
  *     @arg TIMER_OC_MODE_PWM1
  *     @arg TIMER_OC_MODE_PWM2
  *     @arg TIMER_FORCED_HIGH
  *     @arg TIMER_FORCED_LOW
  * @retval None
  */
void TIMER_OCxModeConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_Ch, uint16_t TIMER_OCMode)
{
    uint32_t tmp = 0;
    uint16_t tmp1 = 0;

    tmp = (uint32_t) TIMERx;
    tmp += CHCTLR_OFFSET;

    tmp1 = CHE_CHE_SET << (uint16_t)TIMER_Ch;

    /* Disable the Channel: Reset the CHxE Bit */
    TIMERx->CHE &= (uint16_t) ~tmp1;

    if ((TIMER_Ch == TIMER_CH_1) || (TIMER_Ch == TIMER_CH_3)) {
        tmp += (TIMER_Ch >> 1);

        /* Reset the CHxOM bits in the CHCTLRx register */
        *(__IO uint32_t *) tmp &= (uint32_t)~((uint32_t)TIMER_CHCTLR1_CH1OM);

        /* Configure the CHxOM bits in the CHCTLRx register */
        *(__IO uint32_t *) tmp |= TIMER_OCMode;
    } else {
        tmp += (uint16_t)(TIMER_Ch - (uint16_t)4) >> (uint16_t)1;

        /* Reset the CHxOM bits in the CHCTLRx register */
        *(__IO uint32_t *) tmp &= (uint32_t)~((uint32_t)TIMER_CHCTLR1_CH2OM);

        /* Configure the CHxOM bits in the CHCTLRx register */
        *(__IO uint32_t *) tmp |= (uint16_t)(TIMER_OCMode << 8);
    }
}

/**
  * @brief  Configure the TIMERx Capture or Compare Register value
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  CompValue1: the Capture / Compare1 register new value
  * @retval None
  */
void TIMER_Compare1Config(TIMER_TypeDef *TIMERx, uint32_t CompValue1)
{
    TIMERx->CHCC1 = CompValue1 ;
}

/**
  * @brief  Configure the TIMERx Capture or Compare Register value
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  CompValue2: the Capture / Compare1 register new value
  * @retval None
  */
void TIMER_Compare2Config(TIMER_TypeDef *TIMERx, uint32_t CompValue2)
{
    TIMERx->CHCC2 = CompValue2;
}

/**
  * @brief  Configure the TIMERx Capture or Compare Register value
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  CompValue3: the Capture / Compare1 register new value
  * @retval None
  */
void TIMER_Compare3Config(TIMER_TypeDef *TIMERx, uint32_t CompValue3)
{
    TIMERx->CHCC3 = CompValue3;
}

/**
  * @brief  Configure the TIMERx Capture or Compare Register value
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  CompValue4: the Capture / Compare1 register new value
  * @retval None
  */
void TIMER_Compare4Config(TIMER_TypeDef *TIMERx, uint32_t CompValue4)
{
    TIMERx->CHCC4 = CompValue4;
}

/**
  * @brief  Force the TIMERx output level to high or low
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_Forced: forced the output level.
  *   This value will be :
  *     @arg TIMER_FORCED_HIGH: Force output high level
  *     @arg TIMER_FORCED_LOW : Force output low level
  * @retval None
  */
void TIMER_Forced_OC1(TIMER_TypeDef *TIMERx, uint16_t TIMER_Forced)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the CH1OM Bits */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1OM);

    /* Configure The Forced output Mode */
    tmpchctlr1 |= TIMER_Forced ;

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  Force the TIMERx output level to high or low
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_Forced: forced the output level.
  *   This value will be :
  *     @arg TIMER_FORCED_HIGH: Force output high level
  *     @arg TIMER_FORCED_LOW : Force output low level
  * @retval None
  */
void TIMER_Forced_OC2(TIMER_TypeDef *TIMERx, uint16_t TIMER_Forced)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the CH2OM Bits */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH2OM);

    /* Configure The Forced output Mode */
    tmpchctlr1 |= (uint16_t)(TIMER_Forced << 8);

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  Force the TIMERx output level to high or low
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_Forced: forced the output level.
  *   This value will be :
  *     @arg TIMER_FORCED_HIGH: Force output high level
  *     @arg TIMER_FORCED_LOW : Force output low level
  * @retval None
  */
void TIMER_Forced_OC3(TIMER_TypeDef *TIMERx, uint16_t TIMER_Forced)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the CH3OM Bits */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH3OM);

    /* Configure The Forced output Mode */
    tmpchctlr2 |= TIMER_Forced ;

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  Force the TIMERx output level to high or low
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_Forced: forced the output level.
  *   This value will be :
  *     @arg TIMER_FORCED_HIGH: Force output high level
  *     @arg TIMER_FORCED_LOW : Force output low level
  * @retval None
  */
void TIMER_Forced_OC4(TIMER_TypeDef *TIMERx, uint16_t TIMER_Forced)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the CH4OM Bits */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH4OM);

    /* Configure The Forced output Mode */
    tmpchctlr2 |= (uint16_t)(TIMER_Forced << 8);

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  Configure the TIMER Capture or Compare Preload Control bit
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  NewValue: ENABLE or DISABLE.
  * @retval None
  */
void TIMER_CC_PreloadControl(TIMER_TypeDef *TIMERx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        /* Set the CCSE Bit */
        TIMERx->CTLR2 |= TIMER_CTLR2_CCSE;
    } else {
        /* Reset the CCSE Bit */
        TIMERx->CTLR2 &= (uint16_t)~((uint16_t)TIMER_CTLR2_CCSE);
    }
}

/**
  * @brief  Configure the TIMER channel 1 Capture or Compare Preload register
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_OCPreload : state of the TIMERx Preload register
  *   This value will be :
  *     @arg TIMER_OC_PRELOAD_ENABLE
  *     @arg TIMER_OC_PRELOAD_DISABLE
  * @retval None
  */
void TIMER_OC1_Preload(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPreload)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the CH1OSE Bit */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1OSE);

    /* Enable or Disable  the Output Compare Preload  */
    tmpchctlr1 |= TIMER_OCPreload;

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  Configure the TIMER channel 2 Capture or Compare Preload register
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_OCPreload : state of the TIMERx Preload register
  *   This value will be :
  *     @arg TIMER_OC_PRELOAD_ENABLE
  *     @arg TIMER_OC_PRELOAD_DISABLE
  * @retval None
  */
void TIMER_OC2_Preload(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPreload)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the CH2OSE Bit */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH2OSE);

    /* Enable or Disable  the Output Compare Preload  */
    tmpchctlr1 |= (uint16_t)(TIMER_OCPreload << 8);

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  Configure the TIMER channel 3 Capture or Compare Preload register
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCPreload : state of the TIMERx Preload register
  *   This value will be :
  *     @arg TIMER_OC_PRELOAD_ENABLE
  *     @arg TIMER_OC_PRELOAD_DISABLE
  * @retval None
  */
void TIMER_OC3_Preload(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPreload)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the CH3OSE Bit */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH3OSE);

    /* Enable or Disable  the Output Compare Preload  */
    tmpchctlr2 |= TIMER_OCPreload;

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  Configure the TIMER channel 4 Capture or Compare Preload register
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCPreload : state of the TIMERx Preload register
  *   This value will be :
  *     @arg TIMER_OC_PRELOAD_ENABLE
  *     @arg TIMER_OC_PRELOAD_DISABLE
  * @retval None
  */
void TIMER_OC4_Preload(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPreload)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the CH4OSE Bit */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH4OSE);

    /* Enable or Disable the Output Compare Preload  */
    tmpchctlr2 |= (uint16_t)(TIMER_OCPreload << 8);

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  Configure the TIMER channel 1 Compare output Fast mode
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_OCFast: state of the Compare Output Fast Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_FAST_ENABLE : output fast enable
  *     @arg TIMER_OC_FAST_DISABLE: output fast disable
  * @retval None
  */
void TIMER_OC1_FastConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCFast)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the CH1OFE Bit */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1OFE);

    /* Enable or Disable the Output Compare Fast Bit  */
    tmpchctlr1 |= TIMER_OCFast;

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  Configure the TIMER channel 2 Compare output Fast mode
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_OCFast: state of the Compare Output Fast Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_FAST_ENABLE : output fast enable
  *     @arg TIMER_OC_FAST_DISABLE: output fast disable
  * @retval None
  */
void TIMER_OC2_FastConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCFast)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the CH2OFE Bit */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH2OFE);

    /* Enable or Disable the Output Compare Fast Bit  */
    tmpchctlr1 |= (uint16_t)(TIMER_OCFast << 8);

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  Configure the TIMER channel 3 Compare output Fast mode
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCFast: state of the Compare Output Fast Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_FAST_ENABLE : output fast enable
  *     @arg TIMER_OC_FAST_DISABLE: output fast disable
  * @retval None
  */
void TIMER_OC3_FastConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCFast)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the CH3OFE Bit */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH3OFE);

    /* Enable or Disable the Output Compare Fast Bit  */
    tmpchctlr2 |= TIMER_OCFast;

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  Configure the TIMER channel 4 Compare output Fast mode
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCFast: state of the Compare Output Fast Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_FAST_ENABLE : output fast enable
  *     @arg TIMER_OC_FAST_DISABLE: output fast disable
  * @retval None
  */
void TIMER_OC4_FastConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCFast)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the CH4OFE Bit */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH4OFE);

    /* Enable or Disable the Output Compare Fast Bit */
    tmpchctlr2 |= (uint16_t)(TIMER_OCFast << 8);

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  If Clear the OCREF signal on an external event
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_OCClear: new state of the Output Compare Clear Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_CLEAR_ENABLE : Output clear enable
  *     @arg TIMER_OC_CLEAR_DISABLE: Output clear disable
  * @retval None
  */
void TIMER_OC1_RefClear(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCClear)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the CH1OCE Bit */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1OCE);

    /* Enable or Disable the Output Compare Clear Bit */
    tmpchctlr1 |= TIMER_OCClear;

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  If Clear the OCREF signal on an external event
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_OCClear: new state the Output Compare Clear Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_CLEAR_ENABLE : Output clear enable
  *     @arg TIMER_OC_CLEAR_DISABLE: Output clear disable
  * @retval None
  */
void TIMER_OC2_RefClear(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCClear)
{
    uint16_t tmpchctlr1 = 0;

    tmpchctlr1 = TIMERx->CHCTLR1;

    /* Reset the OC2CE Bit */
    tmpchctlr1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH2OCE);

    /* Enable or Disable the Output Compare Clear Bit */
    tmpchctlr1 |= (uint16_t)(TIMER_OCClear << 8);

    TIMERx->CHCTLR1 = tmpchctlr1;
}

/**
  * @brief  If Clear the OCREF signal on an external event
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCClear: new state the Output Compare Clear Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_CLEAR_ENABLE : Output clear enable
  *     @arg TIMER_OC_CLEAR_DISABLE: Output clear disable
  * @retval None
  */
void TIMER_OC3_RefClear(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCClear)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the CH3OCE Bit */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH3OCE);

    /* Enable or Disable the Output Compare Clear Bit */
    tmpchctlr2 |= TIMER_OCClear;

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  If Clear the OCREF signal on an external event
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCClear: new state the Output Compare Clear Enable Bit.
  *   This value will be :
  *     @arg TIMER_OC_CLEAR_ENABLE : Output clear enable
  *     @arg TIMER_OC_CLEAR_DISABLE: Output clear disable
  * @retval None
  */
void TIMER_OC4_RefClear(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCClear)
{
    uint16_t tmpchctlr2 = 0;

    tmpchctlr2 = TIMERx->CHCTLR2;

    /* Reset the OC4CE Bit */
    tmpchctlr2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH4OCE);

    /* Enable or Disable the Output Compare Clear Bit */
    tmpchctlr2 |= (uint16_t)(TIMER_OCClear << 8);

    TIMERx->CHCTLR2 = tmpchctlr2;
}

/**
  * @brief  Configure the TIMERx channel 1 polarity.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_OCPolarity :
  *   This value will be :
  *     @arg TIMER_OC_POLARITY_HIGH:  active high
  *     @arg TIMER_OC_POLARITY_LOW :  active low
  * @retval None
  */
void TIMER_OC1_Polarity(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPolarity)
{
    uint16_t tmpche = 0;

    tmpche = TIMERx->CHE;

    /* Configures the CH1P Bit */
    tmpche &= (uint16_t)~((uint16_t)TIMER_CHE_CH1P);
    tmpche |= TIMER_OCPolarity;

    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the TIMERx Channel 1 complementary polarity.
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  TIMER_OCNPolarity:
  *   This value will be :
  *     @arg TIMER_OCN_POLARITY_HIGH:  active high
  *     @arg TIMER_OCN_POLARITY_LOW:  active low
  * @retval None
  */
void TIMER_OC1N_Polarity(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCNPolarity)
{
    uint16_t tmpche = 0;

    tmpche = TIMERx->CHE;

    /* Configures the CH1NP Bit */
    tmpche &= (uint16_t)~((uint16_t)TIMER_CHE_CH1NP);
    tmpche |= TIMER_OCNPolarity;

    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the TIMERx channel 2 polarity.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_OCPolarity :
  *   This value will be :
  *     @arg TIMER_OC_POLARITY_HIGH:  active high
  *     @arg TIMER_OC_POLARITY_LOW :  active low
  * @retval None
  */
void TIMER_OC2_Polarity(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPolarity)
{
    uint16_t tmpche = 0;

    tmpche = TIMERx->CHE;

    /* Configure the CH2P Bit */
    tmpche &= (uint16_t)~((uint16_t)TIMER_CHE_CH2P);
    tmpche |= (uint16_t)(TIMER_OCPolarity << 4);

    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the TIMERx Channel 2 complementary polarity.
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  TIMER_OCNPolarity:
  *   This value will be :
  *     @arg TIMER_OCN_POLARITY_HIGH:  active high
  *     @arg TIMER_OCN_POLARITY_LOW:  active low
  * @retval None
  */
void TIMER_OC2N_Polarity(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCNPolarity)
{
    uint16_t tmpche = 0;

    tmpche = TIMERx->CHE;

    /* Configure the CH2NP Bit */
    tmpche &= (uint16_t)~((uint16_t)TIMER_CHE_CH2NP);
    tmpche |= (uint16_t)(TIMER_OCNPolarity << 4);

    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the TIMERx channel 3 polarity.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCPolarity :
  *   This value will be :
  *     @arg TIMER_OC_POLARITY_HIGH:  active high
  *     @arg TIMER_OC_POLARITY_LOW :  active low
  * @retval None
  */
void TIMER_OC3_Polarity(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPolarity)
{
    uint16_t tmpche = 0;

    tmpche = TIMERx->CHE;

    /* Configure the CH3P Bit */
    tmpche &= (uint16_t)~((uint16_t)TIMER_CHE_CH3P);
    tmpche |= (uint16_t)(TIMER_OCPolarity << 8);

    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the TIMERx Channel 3 complementary polarity.
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  TIMER_OCNPolarity:
  *   This value will be :
  *     @arg TIMER_OCN_POLARITY_HIGH:  active high
  *     @arg TIMER_OCN_POLARITY_LOW :  active low
  * @retval None
  */
void TIMER_OC3N_Polarity(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCNPolarity)
{
    uint16_t tmpche = 0;

    tmpche = TIMERx->CHE;

    /* Configure the CH3NP Bit */
    tmpche &= (uint16_t)~((uint16_t)TIMER_CHE_CH3NP);
    tmpche |= (uint16_t)(TIMER_OCNPolarity << 8);

    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the TIMERx channel 4 polarity.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_OCPolarity :
  *   This value will be :
  *     @arg TIMER_OC_POLARITY_HIGH:  active high
  *     @arg TIMER_OC_POLARITY_LOW :  active low
  * @retval None
  */
void TIMER_OC4_Polarity(TIMER_TypeDef *TIMERx, uint16_t TIMER_OCPolarity)
{
    uint16_t tmpche = 0;

    tmpche = TIMERx->CHE;

    /* Configure the CH4P Bit */
    tmpche &= (uint16_t)~((uint16_t)TIMER_CHE_CH4P);
    tmpche |= (uint16_t)(TIMER_OCPolarity << 12);


    TIMERx->CHE = tmpche;
}

/**
  * @brief  Turn-on or off the Channel x Capture or Compare .
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_Ch:
  *   This value will be :
  *     @arg TIMER_CH_1:
  *     @arg TIMER_CH_2:
  *     @arg TIMER_CH_3:
  *     @arg TIMER_CH_4:
  * @param  TIMER_CCx:  the TIMER Channel CCxE bit new value.
  *   This value will be:
  *     @arg TIMER_CCX_ENABLE
  *     @arg TIMER_CCX_DISABLE
  * @retval None
  */
void TIMER_CCxCmd(TIMER_TypeDef *TIMERx, uint16_t TIMER_Ch, uint16_t TIMER_CCx)
{
    uint16_t tmp = 0;

    tmp = CHE_CHE_SET << TIMER_Ch;

    /* Reset the CCx Bit  */
    TIMERx->CHE &= (uint16_t)~ tmp;

    /* Configure the CCx Bit */
    TIMERx->CHE |= (uint16_t)(TIMER_CCx << TIMER_Ch);
}

/**
  * @brief  Turn-on or off the Channel x complementary Capture or Compare
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  TIMER_Ch:
  *   This value will be :
  *     @arg TIMER_CH_1:
  *     @arg TIMER_CH_2:
  *     @arg TIMER_CH_3:
  * @param  TIMER_CCxN: the Channel CCxN bit new value.
  *   This value will be:
  *     @arg TIMER_CCXN_ENABLE
  *     @arg TIMER_CCXN_DISABLE
  * @retval None
  */
void TIMER_CCxNCmd(TIMER_TypeDef *TIMERx, uint16_t TIMER_Ch, uint16_t TIMER_CCxN)
{
    uint16_t tmp = 0;

    tmp = CHE_CHNE_SET << TIMER_Ch;

    /* Reset the CCxN Bit  */
    TIMERx->CHE &= (uint16_t) ~tmp;

    /* Configure the CCxN Bit */
    TIMERx->CHE |= (uint16_t)(TIMER_CCxN << TIMER_Ch);
}

/**
  * @brief  Select control shadow register update control.
  * @param  TIMERx:  x ={ 1, 8 } .
  * @param  NewState: ENABLE or DISABLE.
  * @retval None
  */
void TIMER_SelectCOM(TIMER_TypeDef *TIMERx, TypeState NewValue)
{

    if (NewValue != DISABLE) {
        /* Set the CCUC Bit */
        TIMERx->CTLR2 |= TIMER_CTLR2_CCUC;
    } else {
        /* Reset the CCUC Bit */
        TIMERx->CTLR2 &= (uint16_t)~((uint16_t)TIMER_CTLR2_CCUC);
    }
}

/**
  * @brief  Initialize the Input Capture parameters of the timer
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_ICInit: pointer to a TIMER_ICInitPara structure
  * @retval None
  */
void TIMER_ICInit(TIMER_TypeDef *TIMERx, TIMER_ICInitPara *TIMER_ICInit)
{

    if (TIMER_ICInit->TIMER_CH == TIMER_CH_1) {
        TI1_Config(TIMERx, TIMER_ICInit->TIMER_ICPolarity,
                   TIMER_ICInit->TIMER_ICSelection,
                   TIMER_ICInit->TIMER_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIMER_Set_IC1_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);
    } else if (TIMER_ICInit->TIMER_CH == TIMER_CH_2) {
        TI2_Config(TIMERx, TIMER_ICInit->TIMER_ICPolarity,
                   TIMER_ICInit->TIMER_ICSelection,
                   TIMER_ICInit->TIMER_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIMER_Set_IC2_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);
    } else if (TIMER_ICInit->TIMER_CH == TIMER_CH_3) {
        TI3_Config(TIMERx, TIMER_ICInit->TIMER_ICPolarity,
                   TIMER_ICInit->TIMER_ICSelection,
                   TIMER_ICInit->TIMER_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIMER_Set_IC3_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);
    } else {
        TI4_Config(TIMERx, TIMER_ICInit->TIMER_ICPolarity,
                   TIMER_ICInit->TIMER_ICSelection,
                   TIMER_ICInit->TIMER_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIMER_Set_IC4_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);
    }
}

/**
  * @brief  Fill TIMER_ICInitPara member with default value.
  * @param  TIMER_ICInit : pointer to a TIMER_ICInitPara structure
  * @retval None
  */
void TIMER_ICStructInit(TIMER_ICInitPara *TIMER_ICInit)
{
    TIMER_ICInit->TIMER_CH               = TIMER_CH_1;
    TIMER_ICInit->TIMER_ICPolarity       = TIMER_IC_POLARITY_RISING;
    TIMER_ICInit->TIMER_ICSelection      = TIMER_IC_SELECTION_DIRECTTI;
    TIMER_ICInit->TIMER_ICPrescaler      = TIMER_IC_PSC_DIV1;
    TIMER_ICInit->TIMER_ICFilter         = 0x00;
}

/**
  * @brief  Configure the TIMER PWM input Capture mode parameters
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_ICInit: pointer to a TIMER_ICInitPara structure
  * @retval None
  */
void TIMER_PWMCaptureConfig(TIMER_TypeDef *TIMERx, TIMER_ICInitPara *TIMER_ICInit)
{
    uint16_t icoppositepolarity      = TIMER_IC_POLARITY_RISING;
    uint16_t icoppositeselection     = TIMER_IC_SELECTION_DIRECTTI;

    /* Select the Opposite Input Polarity */
    if (TIMER_ICInit->TIMER_ICPolarity == TIMER_IC_POLARITY_RISING) {
        icoppositepolarity = TIMER_IC_POLARITY_FALLING;
    } else {
        icoppositepolarity = TIMER_IC_POLARITY_RISING;
    }

    /* Select the Opposite Input */
    if (TIMER_ICInit->TIMER_ICSelection == TIMER_IC_SELECTION_DIRECTTI) {
        icoppositeselection = TIMER_IC_SELECTION_INDIRECTTI;
    } else {
        icoppositeselection = TIMER_IC_SELECTION_DIRECTTI;
    }

    if (TIMER_ICInit->TIMER_CH == TIMER_CH_1) {
        TI1_Config(TIMERx, TIMER_ICInit->TIMER_ICPolarity,
                   TIMER_ICInit->TIMER_ICSelection,
                   TIMER_ICInit->TIMER_ICFilter);

        TIMER_Set_IC1_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);

        TI2_Config(TIMERx, icoppositepolarity, icoppositeselection, TIMER_ICInit->TIMER_ICFilter);

        TIMER_Set_IC2_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);
    } else {
        TI2_Config(TIMERx, TIMER_ICInit->TIMER_ICPolarity,
                   TIMER_ICInit->TIMER_ICSelection,
                   TIMER_ICInit->TIMER_ICFilter);

        TIMER_Set_IC2_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);

        TI1_Config(TIMERx, icoppositepolarity, icoppositeselection, TIMER_ICInit->TIMER_ICFilter);

        TIMER_Set_IC1_Prescaler(TIMERx, TIMER_ICInit->TIMER_ICPrescaler);
    }
}

/**
  * @brief  Read the TIMERx Input Capture value.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @retval None
  */
uint32_t TIMER_GetCapture1(TIMER_TypeDef *TIMERx)
{
    return TIMERx->CHCC1;
}

/**
  * @brief  Read the TIMERx Input Capture value.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @retval None
  */
uint32_t TIMER_GetCapture2(TIMER_TypeDef *TIMERx)
{
    return TIMERx->CHCC2;
}

/**
  * @brief  Read the TIMERx Input Capture value.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @retval None
  */
uint32_t TIMER_GetCapture3(TIMER_TypeDef *TIMERx)
{
    return TIMERx->CHCC3;
}

/**
  * @brief  Read the TIMERx Input Capture value.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @retval None
  */
uint32_t TIMER_GetCapture4(TIMER_TypeDef *TIMERx)
{
    return TIMERx->CHCC4;
}

/**
  * @brief  Configure the TIMERx Input Capture prescaler.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14} .
  * @param  TIMER_ICPSC: the Input Capture1 prescaler value.
  *   This value will be :
  *     @arg TIMER_IC_PSC_DIV1: no prescaler
  *     @arg TIMER_IC_PSC_DIV2: divided by 2
  *     @arg TIMER_IC_PSC_DIV4: divided by 4
  *     @arg TIMER_IC_PSC_DIV8: divided by 8
  * @retval None
  */
void TIMER_Set_IC1_Prescaler(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPSC)
{
    TIMERx->CHCTLR1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1ICP);

    TIMERx->CHCTLR1 |= TIMER_ICPSC;
}

/**
  * @brief  Configure the TIMERx Input Capture prescaler.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_ICPSC: the Input Capture1 prescaler value.
  *   This value will be :
  *     @arg TIMER_IC_PSC_DIV1: no prescaler
  *     @arg TIMER_IC_PSC_DIV2: divided by 2
  *     @arg TIMER_IC_PSC_DIV4: divided by 4
  *     @arg TIMER_IC_PSC_DIV8: divided by 8
  * @retval None
  */
void TIMER_Set_IC2_Prescaler(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPSC)
{
    TIMERx->CHCTLR1 &= (uint16_t)~((uint16_t)TIMER_CHCTLR1_CH2ICP);

    TIMERx->CHCTLR1 |= (uint16_t)(TIMER_ICPSC << 8);
}

/**
  * @brief  Configure the TIMERx Input Capture prescaler.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_ICPSC: the Input Capture1 prescaler value.
  *   This value will be :
  *     @arg TIMER_IC_PSC_DIV1: no prescaler
  *     @arg TIMER_IC_PSC_DIV2: divided by 2
  *     @arg TIMER_IC_PSC_DIV4: divided by 4
  *     @arg TIMER_IC_PSC_DIV8: divided by 8
  * @retval None
  */
void TIMER_Set_IC3_Prescaler(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPSC)
{
    TIMERx->CHCTLR2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH3ICP);

    TIMERx->CHCTLR2 |= TIMER_ICPSC;
}

/**
  * @brief  Configure the TIMERx Input Capture prescaler.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_ICPSC: the Input Capture1 prescaler value.
  *   This value will be :
  *     @arg TIMER_IC_PSC_DIV1: no prescaler
  *     @arg TIMER_IC_PSC_DIV2: divided by 2
  *     @arg TIMER_IC_PSC_DIV4: divided by 4
  *     @arg TIMER_IC_PSC_DIV8: divided by 8
  * @retval None
  */
void TIMER_Set_IC4_Prescaler(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPSC)
{
    TIMERx->CHCTLR2 &= (uint16_t)~((uint16_t)TIMER_CHCTLR2_CH4ICP);

    TIMERx->CHCTLR2 |= (uint16_t)(TIMER_ICPSC << 8);
}

/**
  * @brief  Configure interrupts Enables
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  TIMER_INT:  the interrupts sources to Configure.
  *   This value will be :
  *     @arg TIMER_INT_UPDATE  : update Interrupt
  *     @arg TIMER_INT_CH1     : Channel 1 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH2     : Channel 2 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH3     : Channel 3 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH4     : Channel 4 Capture or Compare Interrupt
  *     @arg TIMER_INT_CCUG    : Commutation Interrupt
  *     @arg TIMER_INT_TRIGGER : Trigger Interrupt
  *     @arg TIMER_INT_BREAK   : Break Interrupt
  * @param  NewValue: ENABLE or DISABLE.
  * @retval None
  */
void TIMER_INTConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_INT, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        TIMERx->DIE |= TIMER_INT;
    } else {
        TIMERx->DIE &= (uint16_t)~TIMER_INT;
    }
}

/**
  * @brief  Generate the software event
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  TIMER_EventSrc:
  *   This value will be :
  *     @arg TIMER_EVENT_SRC_UPDATE  : update Event
  *     @arg TIMER_EVENT_SRC_CH1     : Channel 1 Capture or Compare Event
  *     @arg TIMER_EVENT_SRC_CH2     : Channel 2 Capture or Compare Event
  *     @arg TIMER_EVENT_SRC_CH3     : Channel 3 Capture or Compare Event
  *     @arg TIMER_EVENT_SRC_CH4     : Channel 4 Capture or Compare Event
  *     @arg TIMER_EVENT_SRC_COM     : COM event
  *     @arg TIMER_EVENT_SRC_TRIGGER : Trigger Event
  *     @arg TIMER_EVENT_SRC_BREAK   : Break event
  * @retval None
  */
void TIMER_GenerateEvent(TIMER_TypeDef *TIMERx, uint16_t TIMER_EventSrc)
{
    TIMERx->EVG = TIMER_EventSrc;
}

/**
  * @brief  Get current flag status
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  TIMER_FLAG:
  *   This value will be :
  *     @arg TIMER_FLAG_UPDATE  : update Flag
  *     @arg TIMER_FLAG_CH1     : Channel 1 Capture or Compare Flag
  *     @arg TIMER_FLAG_CH2     : Channel 2 Capture or Compare Flag
  *     @arg TIMER_FLAG_CH3     : Channel 3 Capture or Compare Flag
  *     @arg TIMER_FLAG_CH4     : Channel 4 Capture or Compare Flag
  *     @arg TIMER_FLAG_COM     : Commutation Flag
  *     @arg TIMER_FLAG_TRIGGER : Trigger Flag
  *     @arg TIMER_FLAG_BREAK   : Break Flag
  *     @arg TIMER_FLAG_CH1OF   : Channel 1 Capture or Compare overcapture Flag
  *     @arg TIMER_FLAG_CH2OF   : Channel 2 Capture or Compare overcapture Flag
  *     @arg TIMER_FLAG_CH3OF   : Channel 3 Capture or Compare overcapture Flag
  *     @arg TIMER_FLAG_CH4OF   : Channel 4 Capture or Compare overcapture Flag
  * @retval The state of TIMER_FLAG ( SET or RESET ).
  */
TypeState TIMER_GetBitState(TIMER_TypeDef *TIMERx, uint16_t TIMER_FLAG)
{
    if ((TIMERx->STR & TIMER_FLAG) != (uint16_t)RESET) {
        return SET;
    } else {
        return RESET;
    }
}

/**
  * @brief  Clear the flags
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  TIMER_FLAG: the flag bit to clear.
  *   This value will be :
  *     @arg TIMER_FLAG_UPDATE  : update Flag
  *     @arg TIMER_FLAG_CH1     : Channel 1 Capture or Compare Flag
  *     @arg TIMER_FLAG_CH2     : Channel 2 Capture or Compare Flag
  *     @arg TIMER_FLAG_CH3     : Channel 3 Capture or Compare Flag
  *     @arg TIMER_FLAG_CH4     : Channel 4 Capture or Compare Flag
  *     @arg TIMER_FLAG_COM     : Commutation Flag
  *     @arg TIMER_FLAG_TRIGGER : Trigger Flag
  *     @arg TIMER_FLAG_BREAK   : Break Flag
  *     @arg TIMER_FLAG_CH1OF   : Channel 1 Capture or Compare overcapture Flag
  *     @arg TIMER_FLAG_CH2OF   : Channel 2 Capture or Compare overcapture Flag
  *     @arg TIMER_FLAG_CH3OF   : Channel 3 Capture or Compare overcapture Flag
  *     @arg TIMER_FLAG_CH4OF   : Channel 4 Capture or Compare overcapture Flag
  * @retval None
  */
void TIMER_ClearBitState(TIMER_TypeDef *TIMERx, uint16_t TIMER_FLAG)
{
    TIMERx->STR = (uint16_t)~TIMER_FLAG;
}

/**
  * @brief  Get interrupt state
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  TIMER_INT:
  *   This value will be :
  *     @arg TIMER_INT_UPDATE: update Interrupt
  *     @arg TIMER_INT_CH1     : Channel 1 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH2     : Channel 2 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH3     : Channel 3 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH4     : Channel 4 Capture or Compare Interrupt
  *     @arg TIMER_INT_CCUG    : Commutation Interrupt
  *     @arg TIMER_INT_TRIGGER : Trigger Interrupt
  *     @arg TIMER_INT_BREAK   : Break Interrupt
  * @retval The new state of the TIMER_INT(SET or RESET).
  */
TypeState TIMER_GetIntBitState(TIMER_TypeDef *TIMERx, uint16_t TIMER_INT)
{
    uint16_t TypeState = 0x0, itenable = 0x0;

    TypeState = TIMERx->STR & TIMER_INT;
    itenable = TIMERx->DIE & TIMER_INT;

    if ((TypeState != (uint16_t)RESET) && (itenable != (uint16_t)RESET)) {
        return SET;
    } else {
        return RESET;
    }
}

/**
  * @brief  Clear the interrupt pending bits
  * @param  TIMERx:  x ={ 0-13 } .
  * @param  TIMER_INT:
  *   This value will be :
  *     @arg TIMER_INT_UPDATE: update Interrupt
  *     @arg TIMER_INT_CH1     : Channel 1 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH2     : Channel 2 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH3     : Channel 3 Capture or Compare Interrupt
  *     @arg TIMER_INT_CH4     : Channel 4 Capture or Compare Interrupt
  *     @arg TIMER_INT_CCUG    : Commutation Interrupt
  *     @arg TIMER_INT_TRIGGER : Trigger Interrupt
  *     @arg TIMER_INT_BREAK   : Break Interrupt
  * @retval None
  */
void TIMER_ClearIntBitState(TIMER_TypeDef *TIMERx, uint16_t TIMER_INT)
{
    TIMERx->STR = (uint16_t)~TIMER_INT;
}

/**
  * @brief  Configure the DMA .
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_DMABase: DMA Base address.
  *   This value will be :
  *     @arg TIMER_DMA_BASE_ADDR_CTLR1
  *     @arg TIMER_DMA_BASE_ADDR_CTLR2
  *     @arg TIMER_DMA_BASE_ADDR_SMC
  *     @arg TIMER_DMA_BASE_ADDR_DIE
  *     @arg TIMER_DMA_BASE_ADDR_STR
  *     @arg TIMER_DMA_BASE_ADDR_EVG
  *     @arg TIMER_DMA_BASE_ADDR_CHCTLR1
  *     @arg TIMER_DMA_BASE_ADDR_CHCTLR2
  *     @arg TIMER_DMA_BASE_ADDR_CHE
  *     @arg TIMER_DMA_BASE_ADDR_CNT
  *     @arg TIMER_DMA_BASE_ADDR_PSC
  *     @arg TIMER_DMA_BASE_ADDR_CARL
  *     @arg TIMER_DMA_BASE_ADDR_CREP
  *     @arg TIMER_DMA_BASE_ADDR_CHCC1
  *     @arg TIMER_DMA_BASE_ADDR_CHCC2
  *     @arg TIMER_DMA_BASE_ADDR_CHCC3
  *     @arg TIMER_DMA_BASE_ADDR_CHCC4
  *     @arg TIMER_DMA_BASE_ADDR_BKDT
  *     @arg TIMER_DMA_BASE_ADDR_DCTLR
  *     @arg TIMER_DMA_BASE_ADDR_DTRSF
  * @param  TIMER_DMABurstLength: DMA Burst length.
  *     This value will be :
  *            [ TIMER_DMA_BURST_1TRANSFER , TIMER_DMA_BURST_18TRANSFERS ]
  * @retval None
  */
void TIMER_DMAConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_DMABase, uint16_t TIMER_DMABurstLength)
{
    TIMERx->DCTLR = TIMER_DMABase | TIMER_DMABurstLength;
}

/**
  * @brief  Configure the TIMERx's DMA Requests
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } .
  * @param  TIMER_DMASrc: the DMA Request sources.
  *   This value will be :
  *     @arg TIMER_DMA_UPDATE  : update start DMA
  *     @arg TIMER_DMA_CH1     : Channel 1 Capture or Compare start DMA
  *     @arg TIMER_DMA_CH2     : Channel 2 Capture or Compare start DMA
  *     @arg TIMER_DMA_CH3     : Channel 3 Capture or Compare start DMA
  *     @arg TIMER_DMA_CH4     : Channel 4 Capture or Compare start DMA
  *     @arg TIMER_DMA_COM     : Commutation DMA
  *     @arg TIMER_DMA_TRIGGER : Trigger DMA
  * @param  NewValue: ENABLE or DISABLE.
  * @retval None
  */
void TIMER_DMACmd(TIMER_TypeDef *TIMERx, uint16_t TIMER_DMASrc, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        /* Enable the DMA */
        TIMERx->DIE |= TIMER_DMASrc;
    } else {
        /* Disable the DMA */
        TIMERx->DIE &= (uint16_t)~TIMER_DMASrc;
    }
}

/**
  * @brief  Select the Capture or Compare DMA source
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  NewValue: ENABLE or DISABLE
  * @retval None
  */
void TIMER_CC_DMA(TIMER_TypeDef *TIMERx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        TIMERx->CTLR2 |= TIMER_CTLR2_DMAS;
    } else {
        TIMERx->CTLR2 &= (uint16_t)~((uint16_t)TIMER_CTLR2_DMAS);
    }
}

/**
  * @brief  Configure the internal Clock
  * @param  TIMERx: x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 }
  * @retval None
  */
void TIMER_InternalClockConfig(TIMER_TypeDef *TIMERx)
{
    TIMERx->SMC &= (uint16_t)(~((uint16_t)TIMER_SMC_SMC));
}

/**
  * @brief  Configure the Internal Trigger as External Input Clock
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_InputTriSrc:
  *   This value will be :
  *     @arg  TIMER_TS_ITR0 : Internal Trigger 0
  *     @arg  TIMER_TS_ITR1 : Internal Trigger 1
  *     @arg  TIMER_TS_ITR2 : Internal Trigger 2
  *     @arg  TIMER_TS_ITR3 : Internal Trigger 3
  * @retval None
  */
void TIMER_ITRxExtClock(TIMER_TypeDef *TIMERx, uint16_t TIMER_InputTriSrc)
{
    /* Select the Internal Trigger */
    TIMER_SelectInputTrigger(TIMERx, TIMER_InputTriSrc);

    /* Select the External clock mode1 */
    TIMERx->SMC |= TIMER_SLAVE_MODE_EXTERNAL1;
}

/**
  * @brief  Configure the External Trigger as External Input Clock
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_TIxExCLKSrc: Trigger source.
  *   This value will be :
  *     @arg TIMER_TIX_EXCLK1_SRC_TI1ED : TI1 Edge Detector
  *     @arg TIMER_TIX_EXCLK1_SRC_TI1   : Filtered Timer Input 1
  *     @arg TIMER_TIX_EXCLK1_SRC_TI2   : Filtered Timer Input 2
  * @param  TIMER_ICPolarity:
  *   This value will be :
  *     @arg TIMER_IC_POLARITY_RISING
  *     @arg TIMER_IC_POLARITY_FALLING
  * @param  ICFilter: specifies the filter value.
  *   This parameter must be a value between 0x0 and 0xF.
  * @retval None
  */
void TIMER_TIxExtCLkConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_TIxExCLKSrc,
                           uint16_t TIMER_ICPolarity, uint16_t ICFilter)
{
    /* Select the Input Clock Source */
    if (TIMER_TIxExCLKSrc == TIMER_TIX_EXCLK1_SRC_TI2) {
        TI2_Config(TIMERx, TIMER_ICPolarity, TIMER_IC_SELECTION_DIRECTTI, ICFilter);
    } else {
        TI1_Config(TIMERx, TIMER_ICPolarity, TIMER_IC_SELECTION_DIRECTTI, ICFilter);
    }

    /* Select the Trigger source */
    TIMER_SelectInputTrigger(TIMERx, TIMER_TIxExCLKSrc);

    /* Enter the External clock mode1 */
    TIMERx->SMC |= TIMER_SLAVE_MODE_EXTERNAL1;
}

/**
  * @brief  Configure the External clock Mode1
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8  } .
  * @param  TIMER_ExtTriPrescaler: The external Trigger Prescaler.
  *   This value will be :
  *     @arg TIMER_EXT_TRI_PSC_OFF: no divided.
  *     @arg TIMER_EXT_TRI_PSC_DIV2: divided by 2.
  *     @arg TIMER_EXT_TRI_PSC_DIV4: divided by 4.
  *     @arg TIMER_EXT_TRI_PSC_DIV8: divided by 8.
  * @param  TIMER_ExtTriPolarity:  Trigger Polarity.
  *   This value will be :
  *     @arg TIMER_EXT_TRI_POLARITY_INVERTED   : active low or falling edge active.
  *     @arg TIMER_EXT_TRI_POLARITY_NONINVERTED: active high or rising edge active.
  * @param  ExtTriFilter: External Trigger Filter.
  *   This parameter must be a value between 0x00 and 0x0F
  * @retval None
  */
void TIMER_ETRClockMode1Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ExTriPrescaler, uint16_t TIMER_ExTriPolarity,
                               uint16_t ExtTriFilter)
{
    uint16_t tmpsmc = 0;

    /* Configure the external Trigger  Clock source */
    TIMER_ETRConfig(TIMERx, TIMER_ExTriPrescaler, TIMER_ExTriPolarity, ExtTriFilter);

    /* Get the TIMERx SMC register value */
    tmpsmc = TIMERx->SMC;

    tmpsmc &= (uint16_t)(~((uint16_t)TIMER_SMC_SMC));

    /* Select the External clock mode1 */
    tmpsmc |= TIMER_SLAVE_MODE_EXTERNAL1;

    /* Select the Trigger selection : ETRF */
    tmpsmc &= (uint16_t)(~((uint16_t)TIMER_SMC_TRGS));
    tmpsmc |= TIMER_TS_ETRF;

    TIMERx->SMC = tmpsmc;
}

/**
  * @brief  Configure the External clock Mode2
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_ExtTriPrescaler: The external Trigger Prescaler.
  *   This value will be :
  *     @arg TIMER_EXT_TRI_PSC_OFF: no divided.
  *     @arg TIMER_EXT_TRI_PSC_DIV2: divided by 2.
  *     @arg TIMER_EXT_TRI_PSC_DIV4: divided by 4.
  *     @arg TIMER_EXT_TRI_PSC_DIV8: divided by 8.
  * @param  TIMER_ExtTriPolarity:  Trigger Polarity.
  *   This value will be :
  *     @arg TIMER_EXT_TRI_POLARITY_INVERTED   : active low or falling edge active.
  *     @arg TIMER_EXT_TRI_POLARITY_NONINVERTED: active high or rising edge active.
  * @param  ExtTriFilter: External Trigger Filter.
  *   This parameter must be a value between 0x00 and 0x0F
  * @retval None
  */
void TIMER_ETRClockMode2Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ExTriPrescaler,
                               uint16_t TIMER_ExTriPolarity, uint16_t ExtTriFilter)
{
    /* Configure the ETR Clock source */
    TIMER_ETRConfig(TIMERx, TIMER_ExTriPrescaler, TIMER_ExTriPolarity, ExtTriFilter);

    /* Select the External clock mode2 */
    TIMERx->SMC |= TIMER_SMC_ECM2E;
}

/**
  * @brief  Select the Input Trigger source
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_InputTriSrc: The Input Trigger source.
  *   This value will be :
  *     @arg TIMER_TS_ITR0    : Internal Trigger 0
  *     @arg TIMER_TS_ITR1    : Internal Trigger 1
  *     @arg TIMER_TS_ITR2    : Internal Trigger 2
  *     @arg TIMER_TS_ITR3    : Internal Trigger 3
  *     @arg TIMER_TS_TI1F_ED : TI1 Edge Detector
  *     @arg TIMER_TS_TI1FP1  : Filtered Timer Input 1
  *     @arg TIMER_TS_TI2FP2  : Filtered Timer Input 2
  *     @arg TIMER_TS_ETRF    : External Trigger input
  * @retval None
  */
void TIMER_SelectInputTrigger(TIMER_TypeDef *TIMERx, uint16_t TIMER_InputTriSrc)
{
    uint16_t tmpsmc = 0;

    tmpsmc = TIMERx->SMC;

    /* Reset the TS Bits */
    tmpsmc &= (uint16_t)(~((uint16_t)TIMER_SMC_TRGS));

    /* Set the Input Trigger source */
    tmpsmc |= TIMER_InputTriSrc ;

    TIMERx->SMC = tmpsmc;
}

/**
  * @brief  Configure the TIMERx Trigger Output Mode.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,10 , 11 , 13 , 14} .
  * @param  TIMER_TriOutSrc:
  *   This value will be :
  *     @arg TIMER_TRI_OUT_SRC_RESET  : The UPG bit in the TIMERx_EVG register as TriO.
  *     @arg TIMER_TRI_OUT_SRC_ENABLE : The CEN bit in TIMERx_CTLR1 as TriO.
  *     @arg TIMER_TRI_OUT_SRC_UPDATE : Update event as TriO.
  *     @arg TIMER_TRI_OUT_SRC_OC1    : capture or compare match ( CC1IF bit set ) as TriO.
  *     @arg TIMER_TRI_OUT_SRC_OC1REF : OC1REF as TriO.
  *     @arg TIMER_TRI_OUT_SRC_OC2REF : OC2REF as TriO.
  *     @arg TIMER_TRI_OUT_SRC_OC3REF : OC3REF as TriO.
  *     @arg TIMER_TRI_OUT_SRC_OC4REF : OC4REF as TriO.
  * @retval None
  */
void TIMER_SelectOutputTrigger(TIMER_TypeDef *TIMERx, uint16_t TIMER_TriOutSrc)
{
    /* Reset the MMC Bits */
    TIMERx->CTLR2 &= (uint16_t)~((uint16_t)TIMER_CTLR2_MMC);

    /* Configures the TriO source */
    TIMERx->CTLR2 |=  TIMER_TriOutSrc;
}

/**
  * @brief  Configure the TIMERx Slave Mode
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_SlaveMode:
  *   This value will be :
  *     @arg TIMER_SLAVE_MODE_RESET     : The trigger signal reset the timer
  *     @arg TIMER_SLAVE_MODE_GATED     : The trigger signal enable the counter when high.
  *     @arg TIMER_SLAVE_MODE_TRIGGER   : The trigger signal starts the counter.
  *     @arg TIMER_SLAVE_MODE_EXTERNAL1 : The trigger signal as a counter clock.
  * @retval None
  */
void TIMER_SelectSlaveMode(TIMER_TypeDef *TIMERx, uint16_t TIMER_SlaveMode)
{
    /* Reset the SMC Bits */
    TIMERx->SMC &= (uint16_t)~((uint16_t)TIMER_SMC_SMC);

    /* Configure the Slave Mode */
    TIMERx->SMC |= TIMER_SlaveMode;
}

/**
  * @brief  Configure the TIMERx Master or Slave Mode
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 10 , 11 , 13 , 14} .
  * @param  TIMER_MasterSlaveMode:
  *   This value will be :
  *     @arg TIMER_MASTER_SLAVE_MODE_ENABLE  : synchronize master and slave by TriO
  *     @arg TIMER_MASTER_SLAVE_MODE_DISABLE : Don't synchronize
  * @retval None
  */
void TIMER_SelectMasterSlaveMode(TIMER_TypeDef *TIMERx, uint16_t TIMER_MasterSlaveMode)
{
    /* Reset the MSM Bit */
    TIMERx->SMC &= (uint16_t)~((uint16_t)TIMER_SMC_MSM);

    /* Configure the MSM Bit */
    TIMERx->SMC |= TIMER_MasterSlaveMode;
}

/**
  * @brief  Configure the External Trigger (ETR)
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_ExTriPrescaler: external Trigger Prescaler.
  *   This value will be :
  *     @arg TIMER_EXT_TRI_PSC_OFF : no divided.
  *     @arg TIMER_EXT_TRI_PSC_DIV2: divided by 2.
  *     @arg TIMER_EXT_TRI_PSC_DIV4: divided by 4.
  *     @arg TIMER_EXT_TRI_PSC_DIV8: divided by 8.
  * @param  TIMER_ExtTriPolarity:  Trigger Polarity.
  *   This value will be :
  *     @arg TIMER_EXT_TRI_POLARITY_INVERTED   : active low or falling edge active.
  *     @arg TIMER_EXT_TRI_POLARITY_NONINVERTED: active high or rising edge active.
  * @param  ExtTRGFilter: The External Trigger signal Filter.
  *   This parameter must be a value between 0x00 and 0x0F
  * @retval None
  */
void TIMER_ETRConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_ExTriPrescaler, uint16_t TIMER_ExTriPolarity,
                     uint16_t ExtTriFilter)
{
    uint16_t tmpsmc = 0;

    tmpsmc = TIMERx->SMC;

    /*Reset the ETR Bits */
    tmpsmc &= SMC_ETR_MASK;

    /* Configure the Prescaler, the Filter value and the Polarity */
    tmpsmc |= (uint16_t)(TIMER_ExTriPrescaler | (uint16_t)(TIMER_ExTriPolarity | (uint16_t)(ExtTriFilter << (uint16_t)8)));

    TIMERx->SMC = tmpsmc;
}

/**
  * @brief  Configure the Encoder Interface.
  * @param  TIMERx:  x ={ 1 , 8 , 9 , 12 } .
  * @param  TIMER_EncoderMode:
  *   This value will be :
  *     @arg TIMER_ENCODER_MODE_TI1  : Counter counts on TI1FP1 edge depending on TI2FP2 level.
  *     @arg TIMER_ENCODER_MODE_TI2  : Counter counts on TI2FP2 edge depending on TI1FP1 level.
  *     @arg TIMER_ENCODER_MODE_TI12 : Counter counts on both TI1FP1 and TI2FP2 edges depending
  *                                    on the level of the other input.
  * @param  TIMER_IC1Polarity: input capture 1 Polarity
  *   This value will be :
  *     @arg TIMER_IC_POLARITY_FALLING : capture Falling edge.
  *     @arg TIMER_IC_POLARITY_RISING  : capture  Rising edge.
  * @param  TIMER_IC2Polarity: input capture 2 Polarity
  *   This value will be :
  *     @arg TIMER_IC_POLARITY_FALLING : capture Falling edge.
  *     @arg TIMER_IC_POLARITY_RISING  : capture  Rising edge.
  * @retval None
  */
void TIMER_EncoderInterfaceConfig(TIMER_TypeDef *TIMERx, uint16_t TIMER_EncoderMode,
                                  uint16_t TIMER_IC1Polarity, uint16_t TIMER_IC2Polarity)
{
    uint16_t tmpsmc     = 0;
    uint16_t tmpchctlr1 = 0;
    uint16_t tmpche     = 0;

    tmpsmc = TIMERx->SMC;

    tmpchctlr1 = TIMERx->CHCTLR1;

    tmpche = TIMERx->CHE;

    /* select the encoder Mode */
    tmpsmc &= (uint16_t)(~((uint16_t)TIMER_SMC_SMC));
    tmpsmc |= TIMER_EncoderMode;

    tmpchctlr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1M)) & (uint16_t)(~((uint16_t)TIMER_CHCTLR1_CH2M)));
    tmpchctlr1 |= TIMER_CHCTLR1_CH1M_0 | TIMER_CHCTLR1_CH2M_0;

    /* select the TI1 and the TI2 Polarities*/
    tmpche &= (uint16_t)~((uint16_t)(TIMER_CHE_CH1P | TIMER_CHE_CH1NP)) & (uint16_t)~((uint16_t)(TIMER_CHE_CH2P | TIMER_CHE_CH2NP));
    tmpche |= (uint16_t)(TIMER_IC1Polarity | (uint16_t)(TIMER_IC2Polarity << (uint16_t)4));

    TIMERx->SMC = tmpsmc;

    TIMERx->CHCTLR1 = tmpchctlr1;

    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the Hall sensor interface
  * @param  TIMERx:  x ={ 1 , 8 } .
  * @param  NewValue: ENABLE or DISABLE.
  * @retval None
  */
void TIMER_SelectHallSensor(TIMER_TypeDef *TIMERx, TypeState NewValue)
{
    if (NewValue != DISABLE) {
        TIMERx->CTLR2 |= TIMER_CTLR2_TI1S;
    } else {
        TIMERx->CTLR2 &= (uint16_t)~((uint16_t)TIMER_CTLR2_TI1S);
    }
}

/* Private functions */
/**
  * @brief  Configure the TI1 as Capture Input.
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 , 12 , 13 , 14}  .
  * @param  TIMER_ICPolarity: Input Capture Polarity.
  *   This value will be :
  *     @arg TIMER_IC_POLARITY_RISING  : Capture rising edge
  *     @arg TIMER_IC_POLARITY_FALLING : Capture falling edge
  * @param  TIMER_ICSelection: Input Capture source.
  *   This value will be :
  *     @arg TIMER_IC_SELECTION_DIRECTTI   : connected to IC1.
  *     @arg TIMER_IC_SELECTION_INDIRECTTI : connected to IC2.
  *     @arg TIMER_IC_SELECTION_TRC        : connected to TRC.
  * @param  TIMER_ICFilter:  Input Capture Filter.
  *   This parameter must be a value between 0x00 and 0x0F.
  * @retval None
  */
static void TI1_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter)
{
    uint16_t tmpchctlr1 = 0, tmpche = 0;

    /* Disable the Channel 1 */
    TIMERx->CHE &= (uint16_t)~((uint16_t)TIMER_CHE_CH1E);
    tmpchctlr1 = TIMERx->CHCTLR1;
    tmpche = TIMERx->CHE;

    /* Select the Input and Configure the filter */
    tmpchctlr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1M)) & ((uint16_t)~((uint16_t)TIMER_CHCTLR1_CH1ICF)));
    tmpchctlr1 |= (uint16_t)(TIMER_ICSelection | (uint16_t)(TIMER_ICFilter << (uint16_t)4));

    /* Configure the Polarity and channel enable Bit */
    tmpche &= (uint16_t)~((uint16_t)(TIMER_CHE_CH1P | TIMER_CHE_CH1NP));
    tmpche |= (uint16_t)(TIMER_ICPolarity | (uint16_t)TIMER_CHE_CH1E);

    TIMERx->CHCTLR1 = tmpchctlr1;
    TIMERx->CHE = tmpche;
}

/**
  * @brief  Configure the TI2 as Capture Input.
  * @note   None
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 , 9 , 12 } .
  * @param  TIMER_ICPolarity:  Input Capture Polarity.
  *   This value will be :
  *     @arg TIMER_IC_POLARITY_RISING  : Capture rising edge
  *     @arg TIMER_IC_POLARITY_FALLING : Capture falling edge
  * @param  TIMER_ICSelection:  Input Capture source.
  *   This value will be :
  *     @arg TIMER_IC_SELECTION_DIRECTTI   : connected to IC2.
  *     @arg TIMER_IC_SELECTION_INDIRECTTI : connected to IC1.
  *     @arg TIMER_IC_SELECTION_TRC        : connected to TRC.
  * @param  TIMER_ICFilter:  Input Capture Filter.
  *   This parameter must be a value between 0x00 and 0x0F.
  * @retval None
  */
static void TI2_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter)
{
    uint16_t tmpchctlr1 = 0, tmpche = 0, tmp = 0;

    /* Disable the Channel 2 */
    TIMERx->CHE &= (uint16_t)~((uint16_t)TIMER_CHE_CH2E);
    tmpchctlr1 = TIMERx->CHCTLR1;
    tmpche     = TIMERx->CHE;
    tmp        = (uint16_t)(TIMER_ICPolarity << 4);

    /* Select the Input and Configure the filter */
    tmpchctlr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIMER_CHCTLR1_CH2M)) & ((uint16_t)~((uint16_t)TIMER_CHCTLR1_CH2ICF)));
    tmpchctlr1 |= (uint16_t)(TIMER_ICFilter << 12);
    tmpchctlr1 |= (uint16_t)(TIMER_ICSelection << 8);

    /* Configure the Polarity and channel enable Bit */
    tmpche &= (uint16_t)~((uint16_t)(TIMER_CHE_CH2P | TIMER_CHE_CH2NP));
    tmpche |= (uint16_t)(tmp | (uint16_t)TIMER_CHE_CH2E);

    TIMERx->CHCTLR1 = tmpchctlr1 ;
    TIMERx->CHE     = tmpche;
}

/**
  * @brief  Configure the TI3 as Capture Input
  * @note   None
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_ICPolarity:  Input Capture Polarity.
  *   This value will be :
  *     @arg TIMER_IC_POLARITY_RISING  : Capture rising edge
  *     @arg TIMER_IC_POLARITY_FALLING : Capture falling edge
  * @param  TIMER_ICSelection:  Input Capture source.
  *   This value will be :
  *     @arg TIMER_IC_SELECTION_DIRECTTI   : connected to IC3.
  *     @arg TIMER_IC_SELECTION_INDIRECTTI : connected to IC4.
  *     @arg TIMER_IC_SELECTION_TRC        : connected to TRC.
  * @param  TIMER_ICFilter:  Input Capture Filter.
  *   This parameter must be a value between 0x00 and 0x0F.
  * @retval None
  */
static void TI3_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter)
{
    uint16_t tmpchctlr2 = 0, tmpche = 0, tmp = 0;

    /* Disable the Channel 3 */
    TIMERx->CHE &= (uint16_t)~((uint16_t)TIMER_CHE_CH3E);
    tmpchctlr2 = TIMERx->CHCTLR2;
    tmpche = TIMERx->CHE;
    tmp = (uint16_t)(TIMER_ICPolarity << 8);

    /* Select the Input and Configure the filter */
    tmpchctlr2 &= (uint16_t)(((uint16_t)~((uint16_t)TIMER_CHCTLR2_CH3M)) & ((uint16_t)~((uint16_t)TIMER_CHCTLR2_CH3ICF)));
    tmpchctlr2 |= (uint16_t)(TIMER_ICSelection | (uint16_t)(TIMER_ICFilter << (uint16_t)4));

    /* Configure the Polarity and channel enable Bit */
    tmpche &= (uint16_t)~((uint16_t)(TIMER_CHE_CH3P | TIMER_CHE_CH3NP));
    tmpche |= (uint16_t)(tmp | (uint16_t)TIMER_CHE_CH3E);

    TIMERx->CHCTLR2 = tmpchctlr2;
    TIMERx->CHE     = tmpche;
}

/**
  * @brief  Configure the TI4 as Capture Input
  * @param  TIMERx:  x ={ 1 , 2 , 3 , 4 , 5 , 8 } .
  * @param  TIMER_ICPolarity:  Input Capture Polarity.
  *   This value will be :
  *     @arg TIMER_IC_POLARITY_RISING  : Capture rising edge
  *     @arg TIMER_IC_POLARITY_FALLING : Capture falling edge
  * @param  TIMER_ICSelection:  Input Capture source.
  *   This value will be :
  *     @arg TIMER_IC_SELECTION_DIRECTTI   : connected to IC4.
  *     @arg TIMER_IC_SELECTION_INDIRECTTI : connected to IC3.
  *     @arg TIMER_IC_SELECTION_TRC        : connected to TRC.
  * @param  TIMER_ICFilter:  Input Capture Filter.
  *   This parameter must be a value between 0x00 and 0x0F.
  * @retval None
  */
static void TI4_Config(TIMER_TypeDef *TIMERx, uint16_t TIMER_ICPolarity, uint16_t TIMER_ICSelection,
                       uint16_t TIMER_ICFilter)
{
    uint16_t tmpchctlr2 = 0, tmpche = 0, tmp = 0;

    /* Disable the Channel 4 */
    TIMERx->CHE &= (uint16_t)~((uint16_t)TIMER_CHE_CH4E);
    tmpchctlr2 = TIMERx->CHCTLR2;
    tmpche = TIMERx->CHE;
    tmp = (uint16_t)(TIMER_ICPolarity << 12);

    /* Select the Input and Configure the filter */
    tmpchctlr2 &= (uint16_t)((uint16_t)(~(uint16_t)TIMER_CHCTLR2_CH4M) & ((uint16_t)~((uint16_t)TIMER_CHCTLR2_CH4ICF)));
    tmpchctlr2 |= (uint16_t)(TIMER_ICSelection << 8);
    tmpchctlr2 |= (uint16_t)(TIMER_ICFilter << 12);

    /* Configure the Polarity and channel enable Bit */
    tmpche &= (uint16_t)~((uint16_t)(TIMER_CHE_CH4P | TIMER_CHE_CH4NP));
    tmpche &= (uint16_t)~((uint16_t)(TIMER_CHE_CH4P));
    tmpche |= (uint16_t)(tmp | (uint16_t)TIMER_CHE_CH4E);

    TIMERx->CHCTLR2 = tmpchctlr2;
    TIMERx->CHE = tmpche;
}
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */