aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/hal/inc/fsl_ftm_hal.h
blob: ef88fafe86a3ea685a9435661e339288757626f7 (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
/*
 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#if !defined(__FSL_FTM_HAL_H__)
#define __FSL_FTM_HAL_H__

#include "fsl_device_registers.h"
#include <stdbool.h>
#include <assert.h>

#if FSL_FEATURE_SOC_FTM_COUNT

/*!
 * @addtogroup ftm_hal
 * @{
 */

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define CHAN0_IDX (0U) /*!< Channel number for CHAN0.*/
#define CHAN1_IDX (1U) /*!< Channel number for CHAN1.*/
#define CHAN2_IDX (2U) /*!< Channel number for CHAN2.*/
#define CHAN3_IDX (3U) /*!< Channel number for CHAN3.*/
#define CHAN4_IDX (4U) /*!< Channel number for CHAN4.*/
#define CHAN5_IDX (5U) /*!< Channel number for CHAN5.*/
#define CHAN6_IDX (6U) /*!< Channel number for CHAN6.*/
#define CHAN7_IDX (7U) /*!< Channel number for CHAN7.*/

#define FTM_COMBINE_CHAN_CTRL_WIDTH  (8U)

/*! @brief FlexTimer clock source selection*/
typedef enum _ftm_clock_source
{
    kClock_source_FTM_None = 0, /*!< @internal gui name="None" */
    kClock_source_FTM_SystemClk, /*!< @internal gui name="System clock" */
    kClock_source_FTM_FixedClk, /*!< @internal gui name="Fixed clock" */
    kClock_source_FTM_ExternalClk /*!< @internal gui name="External clock" */
}ftm_clock_source_t;

/*! @brief FlexTimer counting mode selection */
typedef enum _ftm_counting_mode
{
    kCounting_FTM_UP = 0,
    kCounting_FTM_UpDown
}ftm_counting_mode_t;

/*! @brief FlexTimer pre-scaler factor selection for the clock source*/
typedef enum _ftm_clock_ps
{
    kFtmDividedBy1 = 0, /*!< @internal gui name="Divide by 1" */
    kFtmDividedBy2 , /*!< @internal gui name="Divide by 2" */
    kFtmDividedBy4 , /*!< @internal gui name="Divide by 4" */
    kFtmDividedBy8,  /*!< @internal gui name="Divide by 8" */
    kFtmDividedBy16, /*!< @internal gui name="Divide by 16" */
    kFtmDividedBy32, /*!< @internal gui name="Divide by 32" */
    kFtmDividedBy64, /*!< @internal gui name="Divide by 64" */
    kFtmDividedBy128 /*!< @internal gui name="Divide by 128" */
}ftm_clock_ps_t;

/*! @brief FlexTimer pre-scaler factor for the deadtime insertion*/
typedef enum _ftm_deadtime_ps
{
    kFtmDivided1 = 1, /*!< @internal gui name="Divide by 1" */
    kFtmDivided4, /*!< @internal gui name="Divide by 4" */
    kFtmDivided16, /*!< @internal gui name="Divide by 16" */
}ftm_deadtime_ps_t;

/*! @brief FlexTimer operation mode, capture, output, dual */
typedef enum _ftm_config_mode_t
{
    kFtmInputCapture, /*!< @internal gui name="Input capture" */
    kFtmOutputCompare, /*!< @internal gui name="Output compare" */
    kFtmEdgeAlignedPWM, /*!< @internal gui name="Edge aligned PWM" */
    kFtmCenterAlignedPWM, /*!< @internal gui name="Center aligned PWM" */
    kFtmCombinedPWM, /*!< @internal gui name="Combined PWM" */
    kFtmDualEdgeCapture /*!< @internal gui name="Dual edge capture" */
}ftm_config_mode_t;

/*! @brief FlexTimer input capture edge mode, rising edge, or falling edge */
typedef enum _ftm_input_capture_edge_mode_t
{
   kFtmRisingEdge = 1,
   kFtmFallingEdge,
   kFtmRisingAndFalling
}ftm_input_capture_edge_mode_t;

/*! @brief FlexTimer output compare edge mode. Toggle, clear or set.*/
typedef enum _ftm_output_compare_edge_mode_t
{
   kFtmToggleOnMatch = 1,
   kFtmClearOnMatch,
   kFtmSetOnMatch
}ftm_output_compare_edge_mode_t;

/*! @brief FlexTimer PWM output pulse mode, high-true or low-true on match up */
typedef enum _ftm_pwm_edge_mode_t
{
    kFtmHighTrue = 0, /*!< @internal gui name="High true" */
    kFtmLowTrue /*!< @internal gui name="Low true" */
}ftm_pwm_edge_mode_t;

/*! @brief FlexTimer dual capture edge mode, one shot or continuous */
typedef enum _ftm_dual_capture_edge_mode_t
{
    kFtmOneShot = 0,
    kFtmContinuous
}ftm_dual_capture_edge_mode_t;

/*! @brief FlexTimer quadrature decode modes, phase encode or count and direction mode */
typedef enum _ftm_quad_decode_mode_t
{
    kFtmQuadPhaseEncode = 0,
    kFtmQuadCountAndDir
}ftm_quad_decode_mode_t;

/*! @brief FlexTimer quadrature phase polarities, normal or inverted polarity */
typedef enum _ftm_quad_phase_polarity_t
{
    kFtmQuadPhaseNormal = 0, /*!< Phase A input signal is not inverted before identifying the rising and falling edges of this signal. @internal gui name="Normal polarity" */
    kFtmQuadPhaseInvert /*!< Phase A input signal is inverted before identifying the rising and falling edges of this signal. @internal gui name="Inverted polarity" */
}ftm_quad_phase_polarity_t;

/*! @brief FlexTimer sync options to update registers with buffer */
typedef enum _ftm_sync_method_t
{
    kFtmUseSoftwareTrig = (1U << FTM_SYNC_SWSYNC_SHIFT),
    kFtmUseHardwareTrig0 = (1U << FTM_SYNC_TRIG0_SHIFT),
    kFtmUseHardwareTrig1 = (1U << FTM_SYNC_TRIG1_SHIFT),
    kFtmUseHardwareTrig2 = (1U << FTM_SYNC_TRIG2_SHIFT)
}ftm_sync_method_t;

/*! @brief Options for the FlexTimer behaviour in BDM Mode */
typedef enum _ftm_bdm_mode_t
{
    kFtmBdmMode_00 = 0,
    /*!< FTM counter stopped, CH(n)F bit can be set, FTM channels in functional mode, writes to MOD,CNTIN and C(n)V registers bypass the register buffers. @internal gui name="Mode 0" */
    kFtmBdmMode_01,
    /*!< FTM counter stopped, CH(n)F bit is not set, FTM channels outputs are forced to their safe value , writes to MOD,CNTIN and C(n)V registers bypass the register buffers. @internal gui name="Mode 1" */
    kFtmBdmMode_10,
    /*!< FTM counter stopped, CH(n)F bit is not set, FTM channels outputs are frozen when chip enters in BDM mode, writes to MOD,CNTIN and C(n)V registers bypass the register buffers. @internal gui name="Mode 2" */
    kFtmBdmMode_11
    /*!< FTM counter in functional mode, CH(n)F bit can be set, FTM channels in functional mode, writes to MOD,CNTIN and C(n)V registers is in fully functional mode. @internal gui name="Mode 3" */
}ftm_bdm_mode_t;

/*! @brief FTM status */
typedef enum _ftm_status {
    kStatusFtmSuccess = 0U,                   /*!< FTM success status.*/
    kStatusFtmError = 1U,                     /*!< FTM error status.*/
} ftm_status_t;

/*! @brief FlexTimer edge mode*/
typedef union _ftm_edge_mode_t
{
    ftm_input_capture_edge_mode_t  input_capture_edge_mode;
    ftm_output_compare_edge_mode_t output_compare_edge_mode;
    ftm_pwm_edge_mode_t            ftm_pwm_edge_mode;
    ftm_dual_capture_edge_mode_t   ftm_dual_capture_edge_mode;
}ftm_edge_mode_t;

/*!
 * @brief FlexTimer driver PWM parameter
 * @internal gui name="PWM configuration" id="ftmPwmCfg"
 */
typedef struct FtmPwmParam
{
    ftm_config_mode_t mode;          /*!< FlexTimer PWM operation mode @internal gui name="Mode" id="ChannelMode" */
    ftm_pwm_edge_mode_t edgeMode;    /*!< PWM output mode @internal gui name="Edge mode" id="ChannelEdgeMode" */
    uint32_t uFrequencyHZ;           /*!< PWM period in Hz @internal gui name="Frequency" id="Frequency" */
    uint32_t uDutyCyclePercent;      /*!< PWM pulse width, value should be between 0 to 100
                                          0=inactive signal(0% duty cycle)...
                                          100=active signal (100% duty cycle). @internal gui name="Duty cycle" id="ChannelDuty" */
    uint16_t uFirstEdgeDelayPercent; /*!< Used only in combined PWM mode to generate asymmetrical PWM.
                                          Specifies the delay to the first edge in a PWM period.
                                          If unsure please leave as 0, should be specified as
                                          percentage of the PWM period @internal gui name="First edge delay" id="ChannelFirstEdge" */
}ftm_pwm_param_t;

/*! @brief FlexTimer Dual Edge Capture parameters */
typedef struct FtmDualEdgeCaptureParam
{
    ftm_dual_capture_edge_mode_t mode;              /*!< Dual Edge Capture mode: one-shot or continuous */
    ftm_input_capture_edge_mode_t currChanEdgeMode; /*!< Input Edge select for Channel n */
    ftm_input_capture_edge_mode_t nextChanEdgeMode; /*!< Input Edge select for Channel n + 1 */
}ftm_dual_edge_capture_param_t;

/*! @brief FlexTimer quadrature decode phase parameters
 * @internal gui name="Quadrature decode configuration" id="ftmQuadCfg"
 */
typedef struct FtmPhaseParam
{
    bool kFtmPhaseInputFilter;      /*!< false: disable phase filter, true: enable phase filter  @internal gui name="Phase input filter" id="QuadPhaseFilter" */
    uint32_t kFtmPhaseFilterVal;    /*!< Filter value, used only if phase input filter is enabled  @internal gui name="Phase filter value" id="QuadPhaseValue" */
    ftm_quad_phase_polarity_t kFtmPhasePolarity; /*!< kFtmQuadPhaseNormal or kFtmQuadPhaseInvert  @internal gui name="Phase polarity" id="QuadPhasePol" */
}ftm_phase_params_t;

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*FTM timer control*/
/*!
 * @brief Sets the FTM clock source.
 *
 * @param ftmBase The FTM base address pointer
 * @param clock  The FTM peripheral clock selection\n
 *        bits - 00: No clock  01: system clock  10: fixed clock   11: External clock
 */
static inline void FTM_HAL_SetClockSource(FTM_Type *ftmBase, ftm_clock_source_t clock)
{
    FTM_BWR_SC_CLKS(ftmBase, clock);
}

/*!
 * @brief Reads the FTM clock source.
 *
 * @param ftmBase The FTM base address pointer
 *
 * @return  The FTM clock source selection\n
 *          bits - 00: No clock  01: system clock  10: fixed clock   11:External clock
 */
static inline uint8_t FTM_HAL_GetClockSource(FTM_Type *ftmBase)
{
    return FTM_BRD_SC_CLKS(ftmBase);
}

/*!
 * @brief Sets the FTM clock divider.
 *
 * @param ftmBase The FTM base address pointer
 * @param ps  The FTM peripheral clock pre-scale divider
 */
static inline void FTM_HAL_SetClockPs(FTM_Type *ftmBase, ftm_clock_ps_t ps)
{
    FTM_BWR_SC_PS(ftmBase, ps);
}

/*!
 * @brief Reads the FTM clock divider.
 *
 * @param ftmBase The FTM base address pointer
 *
 * @return The FTM clock pre-scale divider
 */
static inline uint8_t FTM_HAL_GetClockPs(FTM_Type *ftmBase)
{
    return FTM_BRD_SC_PS(ftmBase);
}

/*!
 * @brief Enables the FTM peripheral timer overflow interrupt.
 *
 * @param ftmBase The FTM base address pointer
 */
static inline void FTM_HAL_EnableTimerOverflowInt(FTM_Type *ftmBase)
{
    FTM_BWR_SC_TOIE(ftmBase, 1);
}

/*!
 * @brief Disables the FTM peripheral timer overflow interrupt.
 *
 * @param ftmBase The FTM base address pointer
 */
static inline void FTM_HAL_DisableTimerOverflowInt(FTM_Type *ftmBase)
{
     FTM_BWR_SC_TOIE(ftmBase, 0);
}

/*!
 * @brief Reads the bit that controls enabling the FTM timer overflow interrupt.
 *
 * @param ftmBase The FTM base address pointer
 * @return true if overflow interrupt is enabled, false if not
 */
static inline bool FTM_HAL_IsOverflowIntEnabled(FTM_Type *ftmBase)
{
    return (bool)(FTM_BRD_SC_TOIE(ftmBase));
}

/*!
 * @brief Clears the timer overflow interrupt flag.
 *
 * @param ftmBase The FTM base address pointer
 */
static inline void FTM_HAL_ClearTimerOverflow(FTM_Type *ftmBase)
{
    FTM_BWR_SC_TOF(ftmBase, 0);
}

/*!
 * @brief Returns the FTM peripheral timer overflow interrupt flag.
 *
 * @param ftmBase The FTM base address pointer
 * @return true if overflow, false if not
 */
static inline bool FTM_HAL_HasTimerOverflowed(FTM_Type *ftmBase)
{
     return FTM_BRD_SC_TOF(ftmBase);
}

/*!
 * @brief Sets the FTM center-aligned PWM select.
 *
 * @param ftmBase The FTM base address pointer
 * @param mode 1:upcounting mode 0:up_down counting mode
 */
static inline void FTM_HAL_SetCpwms(FTM_Type *ftmBase, uint8_t mode)
{
    assert(mode < 2);
    FTM_BWR_SC_CPWMS(ftmBase, mode);
}

/*!
 * @brief Sets the FTM peripheral current counter value.
 *
 * @param ftmBase The FTM base address pointer
 * @param val  FTM timer counter value to be set
 */
static inline void  FTM_HAL_SetCounter(FTM_Type *ftmBase, uint16_t val)
{
    FTM_WR_CNT_COUNT(ftmBase, val);
}

/*!
 * @brief Returns the FTM peripheral current counter value.
 *
 * @param ftmBase The FTM base address pointer
 * @return current FTM timer counter value
 */
static inline uint16_t  FTM_HAL_GetCounter(FTM_Type *ftmBase)
{
    return FTM_RD_CNT_COUNT(ftmBase);
}

/*!
 * @brief Sets the FTM peripheral timer modulo value.
 *
 * @param ftmBase The FTM base address pointer
 * @param val The value to be set to the timer modulo
 */
static inline void FTM_HAL_SetMod(FTM_Type *ftmBase, uint16_t val)
{
    FTM_WR_MOD_MOD(ftmBase, val);
}

/*!
 * @brief Returns the FTM peripheral counter modulo value.
 *
 * @param ftmBase The FTM base address pointer
 * @return FTM timer modulo value
 */
static inline uint16_t  FTM_HAL_GetMod(FTM_Type *ftmBase)
{
    return FTM_RD_MOD_MOD(ftmBase);
}

/*!
 * @brief Sets the FTM peripheral timer counter initial value.
 *
 * @param ftmBase The FTM base address pointer
 * @param val initial value to be set
 */
static inline void FTM_HAL_SetCounterInitVal(FTM_Type *ftmBase, uint16_t val)
{
    FTM_WR_CNTIN_INIT(ftmBase, val & FTM_CNTIN_INIT_MASK);
}

/*!
 * @brief Returns the FTM peripheral counter initial value.
 *
 * @param ftmBase The FTM base address pointer
 * @return FTM timer counter initial value
 */
static inline uint16_t  FTM_HAL_GetCounterInitVal(FTM_Type *ftmBase)
{
    return FTM_RD_CNTIN_INIT(ftmBase);
}

/*FTM channel operating mode (Mode, edge and level selection) for capture, output, PWM, combine, dual */
/*!
 * @brief Sets the FTM peripheral timer channel mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param selection The mode to be set valid value MSnB:MSnA :00,01, 10, 11
 */
static inline void FTM_HAL_SetChnMSnBAMode(FTM_Type *ftmBase, uint8_t channel, uint8_t selection)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_BWR_CnSC_MSA(ftmBase, channel, selection & 1);
    FTM_BWR_CnSC_MSB(ftmBase, channel, selection & 2 ? 1 : 0);
}

/*!
 * @brief Sets the FTM peripheral timer channel edge level.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param level The rising or falling edge to be set, valid value ELSnB:ELSnA :00,01, 10, 11
 */
static inline void FTM_HAL_SetChnEdgeLevel(FTM_Type *ftmBase, uint8_t channel, uint8_t level)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_BWR_CnSC_ELSA(ftmBase, channel, level & 1 ? 1 : 0);
    FTM_BWR_CnSC_ELSB(ftmBase, channel, level & 2 ? 1 : 0);
}

/*!
 * @brief Gets the FTM peripheral timer channel mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @return The MSnB:MSnA mode value, will be 00,01, 10, 11
 */
static inline uint8_t FTM_HAL_GetChnMode(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    return (FTM_BRD_CnSC_MSA(ftmBase, channel)|| (FTM_BRD_CnSC_MSB(ftmBase, channel) << 1));
}

/*!
 * @brief Gets the FTM peripheral timer channel edge level.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @return The ELSnB:ELSnA mode value, will be 00,01, 10, 11
 */
static inline uint8_t FTM_HAL_GetChnEdgeLevel(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    return (FTM_BRD_CnSC_ELSA(ftmBase, channel)|| (FTM_BRD_CnSC_ELSB(ftmBase, channel) << 1));
}

/*!
 * @brief Enables or disables the FTM peripheral timer channel DMA.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param val enable or disable
 */
static inline void FTM_HAL_SetChnDmaCmd(FTM_Type *ftmBase, uint8_t channel, bool val)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_BWR_CnSC_DMA(ftmBase, channel,(val? 1 : 0));
}

/*!
 * @brief Returns whether the FTM peripheral timer channel DMA is enabled.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @return true if enabled, false if disabled
 */
static inline bool FTM_HAL_IsChnDma(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    return (FTM_BRD_CnSC_DMA(ftmBase, channel) ? true : false);
}

/*!
 * @brief Get FTM channel(n) interrupt enabled or not.
 * @param ftmBase FTM module base address.
 * @param channel  The FTM peripheral channel number
 */
static inline bool FTM_HAL_IsChnIntEnabled(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    return (bool)(FTM_BRD_CnSC_CHIE(ftmBase, channel));
}

/*!
 * @brief Enables the FTM peripheral timer channel(n) interrupt.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 */
static inline void FTM_HAL_EnableChnInt(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_BWR_CnSC_CHIE(ftmBase, channel, 1);
}
/*!
 * @brief Disables the FTM peripheral timer channel(n) interrupt.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 */
static inline void FTM_HAL_DisableChnInt(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_BWR_CnSC_CHIE(ftmBase, channel, 0);
}

/*!
 * @brief Returns whether any event for the FTM peripheral timer channel has occurred.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @return true if event occurred, false otherwise.
 */
static inline bool FTM_HAL_HasChnEventOccurred(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    return (FTM_BRD_CnSC_CHF(ftmBase, channel)) ? true : false;
}

/*!
 * @brief Clear the channel flag by writing a 0 to the CHF bit.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 */
static inline void FTM_HAL_ClearChnEventFlag(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_BWR_CnSC_CHF(ftmBase, channel, 0);
}

/*FTM channel control*/
/*!
 * @brief Sets the FTM peripheral timer channel counter value.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param val counter value to be set
 */
static inline void FTM_HAL_SetChnCountVal(FTM_Type *ftmBase, uint8_t channel, uint16_t val)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_WR_CnV_VAL(ftmBase, channel, val);
}

/*!
 * @brief Gets the FTM peripheral timer channel counter value.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @return return channel counter value
 */
static inline uint16_t FTM_HAL_GetChnCountVal(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    return FTM_RD_CnV_VAL(ftmBase, channel);
}

/*!
 * @brief Gets the FTM peripheral timer  channel event status.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @return return channel event status value
 */
static inline uint32_t FTM_HAL_GetChnEventStatus(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    return (FTM_RD_STATUS(ftmBase) & (1U << channel)) ? true : false;
    /*return BR_FTM_STATUS(ftmBase, channel);*/
}

/*!
 * @brief Clears the FTM peripheral timer all channel event status.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 */
static inline void FTM_HAL_ClearChnEventStatus(FTM_Type *ftmBase, uint8_t channel)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_CLR_STATUS(ftmBase, 1U << channel);
}

/*!
 * @brief Writes the provided value to the OUTMASK register.
 *
 * This function will mask/uumask multiple channels.
 *
 * @param ftmBase The FTM base address pointer
 * @param regVal  value to be written to the register
 */
static inline void FTM_HAL_SetOutmaskReg(FTM_Type *ftmBase, uint32_t regVal)
{
    FTM_WR_OUTMASK(ftmBase, regVal);
}

/*!
 * @brief Sets the FTM peripheral timer channel output mask.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param mask mask to be set 0 or 1, unmasked or masked
 */
static inline void FTM_HAL_SetChnOutputMask(FTM_Type *ftmBase, uint8_t channel, bool  mask)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    mask ? FTM_SET_OUTMASK(ftmBase, 1U << channel) : FTM_CLR_OUTMASK(ftmBase, 1U << channel);
    /* BW_FTM_OUTMASK_CHnOM(ftmBase, channel,mask); */
}

/*!
 * @brief Sets the FTM peripheral timer channel output initial state 0 or 1.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param state counter value to be set 0 or 1
 */
static inline void FTM_HAL_SetChnOutputInitStateCmd(FTM_Type *ftmBase, uint8_t channel, uint8_t state)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_CLR_OUTINIT(ftmBase, 1U << channel);
    FTM_SET_OUTINIT(ftmBase, (uint8_t)(state << channel));
}

/*!
 * @brief Sets the FTM peripheral timer channel output polarity.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param pol polarity to be set 0 or 1
 */
static inline void FTM_HAL_SetChnOutputPolarityCmd(FTM_Type *ftmBase, uint8_t channel, uint8_t pol)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_CLR_POL(ftmBase, 1U << channel);
    FTM_SET_POL(ftmBase, (uint8_t)(pol << channel));
}
/*!
 * @brief Sets the FTM peripheral timer channel input polarity.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number
 * @param pol polarity  to be set, 0: active high, 1:active low
 */
static inline void FTM_HAL_SetChnFaultInputPolarityCmd(FTM_Type *ftmBase, uint8_t channel, uint8_t pol)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    FTM_CLR_FLTPOL(ftmBase, 1U << channel);
    FTM_SET_FLTPOL(ftmBase, (uint8_t)(pol << channel));
}


/*Feature mode selection HAL*/
    /*FTM fault control*/
/*!
 * @brief Enables the FTM peripheral timer fault interrupt.
 *
 * @param ftmBase The FTM base address pointer
 */
static inline void FTM_HAL_EnableFaultInt(FTM_Type *ftmBase)
{
    FTM_BWR_MODE_FAULTIE(ftmBase, 1);
}

/*!
 * @brief Disables the FTM peripheral timer fault interrupt.
 *
 * @param ftmBase The FTM base address pointer
 */
static inline void FTM_HAL_DisableFaultInt(FTM_Type *ftmBase)
{
    FTM_BWR_MODE_FAULTIE(ftmBase, 0);
}

/*!
 * @brief Defines the FTM fault control mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param mode, valid options are 1, 2, 3, 4
 */
static inline void FTM_HAL_SetFaultControlMode(FTM_Type *ftmBase, uint8_t mode)
{
    FTM_BWR_MODE_FAULTM(ftmBase, mode);
}

/*!
 * @brief Enables or disables the FTM peripheral timer capture test mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true to enable capture test mode, false to disable
 */
static inline void FTM_HAL_SetCaptureTestCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_MODE_CAPTEST(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Enables or disables the FTM write protection.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true: Write-protection is enabled, false: Write-protection is disabled
 */
static inline void FTM_HAL_SetWriteProtectionCmd(FTM_Type *ftmBase, bool enable)
{
     enable ? FTM_BWR_FMS_WPEN(ftmBase, 1) : FTM_BWR_MODE_WPDIS(ftmBase, 1);
}

/*!
 * @brief Enables the FTM peripheral timer group.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true: all registers including FTM-specific registers are available
 *                false: only the TPM-compatible registers are available
 */
static inline void FTM_HAL_Enable(FTM_Type *ftmBase, bool enable)
{
     assert(FTM_BRD_MODE_WPDIS(ftmBase));
     FTM_BWR_MODE_FTMEN(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Initializes the channels output.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true: the channels output is initialized according to the state of OUTINIT reg
 *                false: has no effect
 */
static inline void FTM_HAL_SetInitChnOutputCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_MODE_INIT(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets the FTM peripheral timer sync mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true: no restriction both software and hardware triggers can be used\n
 *                false: software trigger can only be used for MOD and CnV synch, hardware trigger
 *                       only for OUTMASK and FTM counter synch.
 */
static inline void FTM_HAL_SetPwmSyncMode(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_MODE_PWMSYNC(ftmBase, enable ? 1 : 0);
}

/*FTM synchronization control*/
/*!
 * @brief Enables or disables the FTM peripheral timer software trigger.
 *
 * @param ftmBase The FTM base address pointer.
 * @param enable  true: software trigger is selected, false: software trigger is not selected
 */
static inline void FTM_HAL_SetSoftwareTriggerCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNC_SWSYNC(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets the FTM peripheral timer hardware trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param trigger_num  0, 1, 2 for trigger0, trigger1 and trigger3
 * @param enable true: enable hardware trigger from field trigger_num for PWM synch
 *               false: disable hardware trigger from field trigger_num for PWM synch
 */
void FTM_HAL_SetHardwareSyncTriggerSrc(FTM_Type *ftmBase, uint32_t trigger_num, bool enable);

/*!
 * @brief Determines when the OUTMASK register is updated with the value of its buffer.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true if OUTMASK register is updated only by PWM sync\n
 *                false if OUTMASK register is updated in all rising edges of the system clock
 */
static inline void FTM_HAL_SetOutmaskPwmSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNC_SYNCHOM(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Determines if the FTM counter is re-initialized when the selected trigger for
 * synchronization is detected.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  True to update FTM counter when triggered , false to count normally
 */
static inline void FTM_HAL_SetCountReinitSyncCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNC_REINIT(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Enables or disables the FTM peripheral timer maximum loading points.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  True to enable maximum loading point, false to disable
 */
static inline void FTM_HAL_SetMaxLoadingCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNC_CNTMAX(ftmBase, enable ? 1 : 0);
}
/*!
 * @brief Enables or disables the FTM peripheral timer minimum loading points.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  True to enable minimum loading point, false to disable
 */
static inline void FTM_HAL_SetMinLoadingCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNC_CNTMIN(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Combines the channel control.
 *
 * Returns an index for each channel pair.
 *
 * @param channel  The FTM peripheral channel number.
 * @return 0 for channel pair 0 & 1\n
 *         1 for channel pair 2 & 3\n
 *         2 for channel pair 4 & 5\n
 *         3 for channel pair 6 & 7
 */
uint32_t FTM_HAL_GetChnPairIndex(uint8_t channel);

/*!
 * @brief Enables the FTM peripheral timer channel pair fault control.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param enable  True to enable fault control, false to disable
 */
static inline  void FTM_HAL_SetDualChnFaultCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool enable)
{
    assert(chnlPairNum < (FSL_FEATURE_FTM_CHANNEL_COUNT / 2));

    enable ? FTM_SET_COMBINE(ftmBase, FTM_COMBINE_FAULTEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH)):
             FTM_CLR_COMBINE(ftmBase, FTM_COMBINE_FAULTEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH));
}

/*!
 * @brief Enables or disables the FTM peripheral timer channel pair counter PWM sync.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param enable  True to enable PWM synchronization, false to disable
 */
static inline void FTM_HAL_SetDualChnPwmSyncCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool enable)
{
    assert(chnlPairNum < (FSL_FEATURE_FTM_CHANNEL_COUNT / 2));

    enable ? FTM_SET_COMBINE(ftmBase, FTM_COMBINE_SYNCEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH)):
             FTM_CLR_COMBINE(ftmBase, FTM_COMBINE_SYNCEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH));
}

/*!
 * @brief Enables or disabled the FTM peripheral timer channel pair deadtime insertion.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param enable  True to enable deadtime insertion, false to disable
 */
static inline void FTM_HAL_SetDualChnDeadtimeCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool enable)
{
    assert(chnlPairNum < FSL_FEATURE_FTM_CHANNEL_COUNT);

    enable ? FTM_SET_COMBINE(ftmBase, FTM_COMBINE_DTEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH)):
             FTM_CLR_COMBINE(ftmBase, FTM_COMBINE_DTEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH));
}

/*!
 * @brief Enables or disables the FTM peripheral timer channel dual edge capture decap.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param enable  True to enable dual edge capture mode, false to disable
 */
static inline void FTM_HAL_SetDualChnDecapCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool enable)
{
    assert(chnlPairNum < (FSL_FEATURE_FTM_CHANNEL_COUNT / 2));

    enable ? FTM_SET_COMBINE(ftmBase, FTM_COMBINE_DECAP0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH)):
             FTM_CLR_COMBINE(ftmBase, FTM_COMBINE_DECAP0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH));
}

/*!
 * @brief Enables the FTM peripheral timer dual edge capture mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param enable  True to enable dual edge capture, false to disable
 */
static inline void FTM_HAL_SetDualEdgeCaptureCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool enable)
{
    assert(chnlPairNum < (FSL_FEATURE_FTM_CHANNEL_COUNT / 2));

    enable ? FTM_SET_COMBINE(ftmBase, FTM_COMBINE_DECAPEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH)):
             FTM_CLR_COMBINE(ftmBase, FTM_COMBINE_DECAPEN0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH));
}

/*!
 * @brief Enables or disables the FTM peripheral timer channel pair output complement mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param enable  True to enable complementary mode, false to disable
 */
static inline void FTM_HAL_SetDualChnCompCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool enable)
{
    assert(chnlPairNum < (FSL_FEATURE_FTM_CHANNEL_COUNT / 2));

    enable ? FTM_SET_COMBINE(ftmBase, FTM_COMBINE_COMP0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH)):
             FTM_CLR_COMBINE(ftmBase, FTM_COMBINE_COMP0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH));

}

/*!
 * @brief Enables or disables the FTM peripheral timer channel pair output combine mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param enable  True to enable channel pair to combine, false to disable
 */
static inline void FTM_HAL_SetDualChnCombineCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool enable)
{
    assert(chnlPairNum < (FSL_FEATURE_FTM_CHANNEL_COUNT / 2));

    enable ? FTM_SET_COMBINE(ftmBase, FTM_COMBINE_COMBINE0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH)):
             FTM_CLR_COMBINE(ftmBase, FTM_COMBINE_COMBINE0_MASK << (chnlPairNum * FTM_COMBINE_CHAN_CTRL_WIDTH));
}

/*FTM dead time insertion control*/
/*!
 * @brief Sets the FTM deadtime divider.
 *
 * @param ftmBase The FTM base address pointer
 * @param divider  The FTM peripheral prescale divider\n
 *                 0x :divided by 1, 10: divided by 4, 11:divided by 16
 */
static inline void FTM_HAL_SetDeadtimePrescale(FTM_Type *ftmBase, ftm_deadtime_ps_t divider)
{
    FTM_WR_DEADTIME_DTPS(ftmBase, divider);
}

/*!
 * @brief Sets the FTM deadtime value.
 *
 * @param ftmBase The FTM base address pointer
 * @param count  The FTM peripheral  prescale divider\n
 *               0: no counts inserted, 1: 1 count is inserted, 2: 2 count is inserted....
 */
static inline void FTM_HAL_SetDeadtimeCount(FTM_Type *ftmBase, uint8_t count)
{
    FTM_WR_DEADTIME_DTVAL(ftmBase, count);
}

/*!
* @brief Enables or disables the generation of the trigger when the FTM counter is equal to the CNTIN register.
*
* @param ftmBase The FTM base address pointer
* @param enable  True to enable, false to disable
*/
static inline void FTM_HAL_SetInitTriggerCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_EXTTRIG_INITTRIGEN(ftmBase, enable ? 1 : 0);
}

/*FTM external trigger */
/*!
 * @brief Enables or disables the generation of the FTM peripheral timer channel trigger.
 *
 * Enables or disables the generation of the FTM peripheral timer channel trigger when the
 * FTM counter is equal to its initial value. Channels 6 and 7 cannot be used as triggers.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel Channel to be enabled,  valid value 0, 1, 2, 3, 4, 5
 * @param val  True to enable, false to disable
 */
void FTM_HAL_SetChnTriggerCmd(FTM_Type *ftmBase, uint8_t channel, bool val);

/*!
 * @brief Checks whether any channel trigger event has occurred.
 *
 * @param ftmBase The FTM base address pointer
 * @return true if there is a channel trigger event, false if not.
 */
static inline bool FTM_HAL_IsChnTriggerGenerated(FTM_Type *ftmBase)
{
    return FTM_BRD_EXTTRIG_TRIGF(ftmBase);
}

/*Fault mode status*/
/*!
 * @brief Gets the FTM detected fault input.
 *
 * This function reads the status for all fault inputs
 *
 * @param ftmBase The FTM base address pointer
 * @return Return fault byte
 */
static inline uint8_t FTM_HAL_GetDetectedFaultInput(FTM_Type *ftmBase)
{
    return (FTM_RD_FMS(ftmBase) & 0x0f);
}

/*!
 * @brief Checks whether the write protection is enabled.
 *
 * @param ftmBase The FTM base address pointer
 * @return True if enabled, false if not
 */
static inline bool FTM_HAL_IsWriteProtectionEnabled(FTM_Type *ftmBase)
{
    return FTM_BRD_FMS_WPEN(ftmBase) ? true : false;
}

/*Quadrature decoder control*/

/*!
 * @brief Enables the channel quadrature decoder.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  True to enable, false to disable
 */
static inline void FTM_HAL_SetQuadDecoderCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_QDCTRL_QUADEN(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Enables or disables the phase A input filter.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable true enables the phase input filter, false disables the filter
 */
static inline void FTM_HAL_SetQuadPhaseAFilterCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_QDCTRL_PHAFLTREN(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Enables or disables the phase B input filter.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable true enables the phase input filter, false disables the filter
 */
static inline void FTM_HAL_SetQuadPhaseBFilterCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_QDCTRL_PHBFLTREN(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Selects polarity for the quadrature decode phase A input.
 *
 * @param ftmBase The FTM base address pointer
 * @param mode 0: Normal polarity, 1: Inverted polarity
 */
static inline void FTM_HAL_SetQuadPhaseAPolarity(FTM_Type *ftmBase,
                                                           ftm_quad_phase_polarity_t mode)
{
    FTM_BWR_QDCTRL_PHAPOL(ftmBase, mode);
}

/*!
 * @brief Selects polarity for the quadrature decode phase B input.
 *
 * @param ftmBase The FTM base address pointer
 * @param mode 0: Normal polarity, 1: Inverted polarity
 */
static inline void FTM_HAL_SetQuadPhaseBPolarity(FTM_Type *ftmBase,
                                                           ftm_quad_phase_polarity_t mode)
{
    FTM_BWR_QDCTRL_PHBPOL(ftmBase, mode);
}

/*!
 * @brief Sets the encoding mode used in quadrature decoding mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param quadMode 0: Phase A and Phase B encoding mode\n
 *                 1: Count and direction encoding mode
 */
static inline void FTM_HAL_SetQuadMode(FTM_Type *ftmBase, ftm_quad_decode_mode_t quadMode)
{
    FTM_BWR_QDCTRL_QUADMODE(ftmBase, quadMode);
}

/*!
 * @brief Gets the FTM counter direction in quadrature mode.
 *
 * @param ftmBase The FTM base address pointer
 *
 * @return 1 if counting direction is increasing, 0 if counting direction is decreasing
 */
static inline uint8_t FTM_HAL_GetQuadDir(FTM_Type *ftmBase)
{
    return FTM_BRD_QDCTRL_QUADMODE(ftmBase);
}

/*!
 * @brief Gets the Timer overflow direction in quadrature mode.
 *
 * @param ftmBase The FTM base address pointer
 *
 * @return 1 if TOF bit was set on the top of counting, o if TOF bit was set on the bottom of counting
 */
static inline uint8_t FTM_HAL_GetQuadTimerOverflowDir(FTM_Type *ftmBase)
{
    return FTM_BRD_QDCTRL_TOFDIR(ftmBase);
}

/*!
 * @brief Sets the FTM peripheral timer channel input capture filter value.
 * @param ftmBase The FTM base address pointer
 * @param channel  The FTM peripheral channel number, only 0,1,2,3, channel 4, 5,6, 7 don't have.
 * @param val  Filter value to be set
 */
void FTM_HAL_SetChnInputCaptureFilter(FTM_Type *ftmBase, uint8_t channel, uint8_t val);

/*!
 * @brief Sets the fault input filter value.
 *
 * @param ftmBase The FTM base address pointer
 * @param val fault input filter value
 */
static inline void FTM_HAL_SetFaultInputFilterVal(FTM_Type *ftmBase, uint32_t val)
{
    FTM_BWR_FLTCTRL_FFVAL(ftmBase, val);
}

/*!
 * @brief Enables or disables the fault input filter.
 *
 * @param ftmBase The FTM base address pointer
 * @param inputNum fault input to be configured, valid value 0, 1, 2, 3
 * @param val  true to enable fault input filter, false to disable fault input filter
 */
static inline void FTM_HAL_SetFaultInputFilterCmd(FTM_Type *ftmBase, uint8_t inputNum, bool val)
{
    assert(inputNum < CHAN4_IDX);
    val ? FTM_SET_FLTCTRL(ftmBase, (1U << (inputNum + 4))) :
          FTM_CLR_FLTCTRL(ftmBase, (1U << (inputNum + 4)));
}

/*!
 * @brief Enables or disables the fault input.
 *
 * @param ftmBase The FTM base address pointer
 * @param inputNum fault input to be configured, valid value 0, 1, 2, 3
 * @param val  true to enable fault input, false to disable fault input
 */
static inline void FTM_HAL_SetFaultInputCmd(FTM_Type *ftmBase, uint8_t inputNum, bool val)
{
    assert(inputNum < CHAN4_IDX);
    val ? FTM_SET_FLTCTRL(ftmBase, (1U << inputNum)) :
          FTM_CLR_FLTCTRL(ftmBase, (1U << inputNum));
}

/*!
 * @brief Enables or disables the channel invert for a channel pair.
 *
 * @param ftmBase The FTM base address pointer
 * @param chnlPairNum The FTM peripheral channel pair number
 * @param val  true to enable channel inverting, false to disable channel inver
 */
static inline void FTM_HAL_SetDualChnInvertCmd(FTM_Type *ftmBase, uint8_t chnlPairNum, bool val)
{
    assert(chnlPairNum < (FSL_FEATURE_FTM_CHANNEL_COUNT / 2));

    val ? FTM_SET_INVCTRL(ftmBase, (1U << chnlPairNum)) :
          FTM_CLR_INVCTRL(ftmBase, (1U << chnlPairNum));
}

/*!
 * @brief Writes the provided value to the Inverting control register.
 *
 * This function is enable/disable inverting control on multiple channel pairs.
 *
 * @param ftmBase The FTM base address pointer
 * @param regVal  value to be written to the register
 */
static inline void FTM_HAL_SetInvctrlReg(FTM_Type *ftmBase, uint32_t regVal)
{
    FTM_WR_INVCTRL(ftmBase, regVal);
}

/*FTM software output control*/
/*!
 * @brief Enables or disables the channel software output control.
 * @param ftmBase The FTM base address pointer
 * @param channel Channel to be enabled or disabled
 * @param val  true to enable, channel output will be affected by software output control\n
                  false to disable, channel output is unaffected
 */
static inline void FTM_HAL_SetChnSoftwareCtrlCmd(FTM_Type *ftmBase, uint8_t channel, bool val)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    val ? FTM_SET_SWOCTRL(ftmBase, (1U << channel)) :
          FTM_CLR_SWOCTRL(ftmBase, (1U << channel));
}
/*!
 * @brief Sets the channel software output control value.
 *
 * @param ftmBase The FTM base address pointer.
 * @param channel Channel to be configured
 * @param val  True to set 1, false to set 0
 */
static inline void FTM_HAL_SetChnSoftwareCtrlVal(FTM_Type *ftmBase, uint8_t channel, bool val)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    val ? FTM_SET_SWOCTRL(ftmBase, (1U << (channel + 8))) :
          FTM_CLR_SWOCTRL(ftmBase, (1U << (channel + 8)));
}

/*FTM PWM load control*/
/*!
 * @brief Enables or disables the loading of MOD, CNTIN and CV with values of their write buffer.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true to enable, false to disable
 */
static inline void FTM_HAL_SetPwmLoadCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_PWMLOAD_LDOK(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Includes or excludes the channel in the matching process.
 *
 * @param ftmBase The FTM base address pointer
 * @param channel Channel to be configured
 * @param val  true means include the channel in the matching process\n
 *                false means do not include channel in the matching process
 */
static inline void FTM_HAL_SetPwmLoadChnSelCmd(FTM_Type *ftmBase, uint8_t channel, bool val)
{
    assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
    val ? FTM_SET_PWMLOAD(ftmBase, 1U << channel) : FTM_CLR_PWMLOAD(ftmBase, 1U << channel);
}

/*FTM configuration*/
/*!
 * @brief Enables or disables the FTM global time base signal generation to other FTM's.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  True to enable, false to disable
 */
static inline void FTM_HAL_SetGlobalTimeBaseOutputCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_CONF_GTBEOUT(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Enables or disables the FTM timer global time base.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  True to enable, false to disable
 */
static inline void FTM_HAL_SetGlobalTimeBaseCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_CONF_GTBEEN(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets the BDM mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param val FTM behaviour in BDM mode, options are defined in the enum ftm_bdm_mode_t
 */
static inline void FTM_HAL_SetBdmMode(FTM_Type *ftmBase, ftm_bdm_mode_t val)
{
    FTM_WR_CONF_BDMMODE(ftmBase, val);
}

/*!
 * @brief Sets the FTM timer TOF Frequency
 *
 * @param ftmBase The FTM base address pointer
 * @param val  Value of the TOF bit set frequency
 */
static inline void FTM_HAL_SetTofFreq(FTM_Type *ftmBase, uint8_t val)
{
    FTM_WR_CONF_NUMTOF(ftmBase, val);
}

/*FTM sync configuration*/

/*!
 * @brief Sets the FTM register synchronization method.
 *
 * This function will set the necessary bits for the synchronization mode that user wishes to use.
 *
 * @param ftmBase The FTM base address pointer
 * @param syncMethod  Synchronization method defined by ftm_sync_method_t enum. User can choose
 *                    multiple synch methods by OR'ing options
 */
void FTM_HAL_SetSyncMode(FTM_Type *ftmBase, uint32_t syncMethod);

/*!
 * @brief Sets the sync mode for the FTM SWOCTRL register when using a hardware trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means the hardware trigger activates register sync\n
 *                false means the hardware trigger does not activate register sync.
 */
static inline void FTM_HAL_SetSwoctrlHardwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_HWSOC(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM INVCTRL register when using a hardware trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means the hardware trigger activates register sync\n
 *                false means the hardware trigger does not activate register sync.
 */
static inline void FTM_HAL_SetInvctrlHardwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_HWINVC(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM OUTMASK register when using a hardware trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means hardware trigger activates register sync\n
 *                false means hardware trigger does not activate register sync.
 */
static inline void FTM_HAL_SetOutmaskHardwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_HWOM(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM MOD, CNTIN and CV registers when using a hardware trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means hardware trigger activates register sync\n
 *                false means hardware trigger does not activate register sync.
 */
static inline void FTM_HAL_SetModCntinCvHardwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_HWWRBUF(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM counter register when using a hardware trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means hardware trigger activates register sync\n
 *                false means hardware trigger does not activate register sync.
 */
static inline void FTM_HAL_SetCounterHardwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_HWRSTCNT(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM SWOCTRL register when using a software trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means software trigger activates register sync\n
 *                false means software trigger does not activate register sync.
 */
static inline void FTM_HAL_SetSwoctrlSoftwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_SWSOC(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM INVCTRL register when using a software trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means software trigger activates register sync\n
 *                false means software trigger does not activate register sync.
 */
static inline void FTM_HAL_SetInvctrlSoftwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_SWINVC(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM OUTMASK register when using a software trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means software trigger activates register sync\n
 *                false means software trigger does not activate register sync.
 */
static inline void FTM_HAL_SetOutmaskSoftwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_SWOM(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets synch mode for FTM MOD, CNTIN and CV registers when using a software trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means software trigger activates register sync\n
 *                false means software trigger does not activate register sync.
 */
static inline void FTM_HAL_SetModCntinCvSoftwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_SWWRBUF(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets sync mode for FTM counter register when using a software trigger.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means software trigger activates register sync\n
 *                false means software trigger does not activate register sync.
 */
static inline void FTM_HAL_SetCounterSoftwareSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_SWRSTCNT(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets the PWM synchronization mode to enhanced or legacy.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means use Enhanced PWM synchronization\n
 *                false means to use Legacy mode
 */
static inline void FTM_HAL_SetPwmSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_SYNCMODE(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets the SWOCTRL register PWM synchronization mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means SWOCTRL register is updated by PWM synch\n
 *                false means SWOCTRL register is updated at all rising edges of system clock
 */
static inline void FTM_HAL_SetSwoctrlPwmSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_SWOC(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets the INVCTRL register PWM synchronization mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means INVCTRL register is updated by PWM synch\n
 *                false means INVCTRL register is updated at all rising edges of system clock
 */
static inline void FTM_HAL_SetInvctrlPwmSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_INVC(ftmBase, enable ? 1 : 0);
}

/*!
 * @brief Sets the CNTIN register PWM synchronization mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param enable  true means CNTIN register is updated by PWM synch\n
 *                false means CNTIN register is updated at all rising edges of system clock
 */
static inline void FTM_HAL_SetCntinPwmSyncModeCmd(FTM_Type *ftmBase, bool enable)
{
    FTM_BWR_SYNCONF_CNTINC(ftmBase, enable ? 1 : 0);
}


/*HAL functionality*/
/*!
 * @brief Resets the FTM registers
 *
 * @param ftmBase The FTM base address pointer
 */
void FTM_HAL_Reset(FTM_Type *ftmBase);

/*!
 * @brief Initializes the FTM.
 *
 * @param ftmBase The FTM base address pointer.
 */
void FTM_HAL_Init(FTM_Type *ftmBase);

/*!
 * @brief Enables the FTM timer when it is PWM output mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param config PWM configuration parameter
 * @param channel The channel or channel pair number(combined mode).
 */
void FTM_HAL_EnablePwmMode(FTM_Type *ftmBase, ftm_pwm_param_t *config, uint8_t channel);

/*!
 * @brief Disables the PWM output mode.
 *
 * @param ftmBase The FTM base address pointer
 * @param config PWM configuration parameter
 * @param channel The channel or channel pair number(combined mode).
 */
void FTM_HAL_DisablePwmMode(FTM_Type *ftmBase, ftm_pwm_param_t *config, uint8_t channel);

#if defined(__cplusplus)
}
#endif

/*! @}*/

#endif /* FSL_FEATURE_SOC_FTM_COUNT */

#endif /* __FSL_FTM_HAL_H__*/
/*******************************************************************************
 * EOF
 ******************************************************************************/