]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/flextimer/fsl_ftm_hal.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / hal / flextimer / fsl_ftm_hal.h
1 /*
2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #if !defined(__FSL_FTM_HAL_H__)
31 #define __FSL_FTM_HAL_H__
32
33 #include "fsl_device_registers.h"
34 #include "fsl_ftm_features.h"
35 #include <stdbool.h>
36 #include <assert.h>
37
38 /*!
39 * @addtogroup ftm_hal
40 * @{
41 */
42
43 /*******************************************************************************
44 * Definitions
45 ******************************************************************************/
46 #define HW_CHAN0 (0U) /*!< Channel number for CHAN0.*/
47 #define HW_CHAN1 (1U) /*!< Channel number for CHAN1.*/
48 #define HW_CHAN2 (2U) /*!< Channel number for CHAN2.*/
49 #define HW_CHAN3 (3U) /*!< Channel number for CHAN3.*/
50 #define HW_CHAN4 (4U) /*!< Channel number for CHAN4.*/
51 #define HW_CHAN5 (5U) /*!< Channel number for CHAN5.*/
52 #define HW_CHAN6 (6U) /*!< Channel number for CHAN6.*/
53 #define HW_CHAN7 (7U) /*!< Channel number for CHAN7.*/
54
55 #define FTM_COMBINE_CHAN_CTRL_WIDTH (8U)
56
57 /*! @brief FlexTimer clock source selection*/
58 typedef enum _ftm_clock_source
59 {
60 kClock_source_FTM_None = 0,
61 kClock_source_FTM_SystemClk,
62 kClock_source_FTM_FixedClk,
63 kClock_source_FTM_ExternalClk
64 }ftm_clock_source_t;
65
66 /*! @brief FlexTimer counting mode selection */
67 typedef enum _ftm_counting_mode
68 {
69 kCounting_FTM_UP = 0,
70 kCounting_FTM_UpDown
71 }ftm_counting_mode_t;
72
73 /*! @brief FlexTimer pre-scaler factor selection for the clock source*/
74 typedef enum _ftm_clock_ps
75 {
76 kFtmDividedBy1 = 0,
77 kFtmDividedBy2 ,
78 kFtmDividedBy4 ,
79 kFtmDividedBy8,
80 kFtmDividedBy16,
81 kFtmDividedBy32,
82 kFtmDividedBy64,
83 kFtmDividedBy128
84 }ftm_clock_ps_t;
85
86 /*! @brief FlexTimer pre-scaler factor for the deadtime insertion*/
87 typedef enum _ftm_deadtime_ps
88 {
89 kFtmDivided1 = 1,
90 kFtmDivided4 = 2,
91 kFtmDivided16 = 3,
92 }ftm_deadtime_ps_t;
93
94 /*! @brief FlexTimer operation mode, capture, output, dual */
95 typedef enum _ftm_config_mode_t
96 {
97 kFtmInputCapture,
98 kFtmOutputCompare,
99 kFtmEdgeAlignedPWM,
100 kFtmCenterAlignedPWM,
101 kFtmCombinedPWM,
102 kFtmDualEdgeCapture
103 }ftm_config_mode_t;
104
105 /*! @brief FlexTimer input capture edge mode, rising edge, or falling edge */
106 typedef enum _ftm_input_capture_edge_mode_t
107 {
108 kFtmRisingEdge = 0,
109 kFtmFallingEdge,
110 kFtmRisingAndFalling
111 }ftm_input_capture_edge_mode_t;
112
113 /*! @brief FlexTimer output compare edge mode. Toggle, clear or set.*/
114 typedef enum _ftm_output_compare_edge_mode_t
115 {
116 kFtmToggleOnMatch = 0,
117 kFtmClearOnMatch,
118 kFtmSetOnMatch
119 }ftm_output_compare_edge_mode_t;
120
121 /*! @brief FlexTimer PWM output pulse mode, high-true or low-true on match up */
122 typedef enum _ftm_pwm_edge_mode_t
123 {
124 kFtmHighTrue = 0,
125 kFtmLowTrue
126 }ftm_pwm_edge_mode_t;
127
128 /*! @brief FlexTimer dual capture edge mode, one shot or continuous */
129 typedef enum _ftm_dual_capture_edge_mode_t
130 {
131 kFtmOneShout = 0,
132 kFtmContinuous
133 }ftm_dual_capture_edge_mode_t;
134
135 /*! @brief FlexTimer quadrature decode modes, phase encode or count and direction mode */
136 typedef enum _ftm_quad_decode_mode_t
137 {
138 kFtmQuadPhaseEncode = 0,
139 kFtmQuadCountAndDir
140 }ftm_quad_decode_mode_t;
141
142 /*! @brief FlexTimer quadrature phase polarities, normal or inverted polarity */
143 typedef enum _ftm_quad_phase_polarity_t
144 {
145 kFtmQuadPhaseNormal = 0,
146 kFtmQuadPhaseInvert
147 }ftm_quad_phase_polarity_t;
148
149 /*! @brief FlexTimer edge mode*/
150 typedef union _ftm_edge_mode_t
151 {
152 ftm_input_capture_edge_mode_t input_capture_edge_mode;
153 ftm_output_compare_edge_mode_t output_compare_edge_mode;
154 ftm_pwm_edge_mode_t ftm_pwm_edge_mode;
155 ftm_dual_capture_edge_mode_t ftm_dual_capture_edge_mode;
156 }ftm_edge_mode_t;
157
158 /*!
159 * @brief FlexTimer driver PWM parameter
160 *
161 */
162 typedef struct FtmPwmParam
163 {
164 ftm_config_mode_t mode; /*!< FlexTimer PWM operation mode */
165 ftm_pwm_edge_mode_t edgeMode; /*!< PWM output mode */
166 uint32_t uFrequencyHZ; /*!< PWM period in Hz */
167 uint32_t uDutyCyclePercent; /*!< PWM pulse width, value should be between 0 to 100
168 0=inactive signal(0% duty cycle)...
169 100=active signal (100% duty cycle). */
170 uint16_t uFirstEdgeDelayPercent; /*!< Used only in combined PWM mode to generate asymmetrical PWM.
171 Specifies the delay to the first edge in a PWM period.
172 If unsure please leave as 0, should be specified as
173 percentage of the PWM period*/
174 }ftm_pwm_param_t;
175
176 /*! @brief FlexTimer quadrature decode phase parameters */
177 typedef struct FtmPhaseParam
178 {
179 bool kFtmPhaseInputFilter; /*!< false: disable phase filter, true: enable phase filter */
180 uint32_t kFtmPhaseFilterVal; /*!< Filter value, used only if phase input filter is enabled */
181 ftm_quad_phase_polarity_t kFtmPhasePolarity; /*!< kFtmQuadPhaseNormal or kFtmQuadPhaseInvert */
182 }ftm_phase_params_t;
183
184 /*FTM timer control*/
185 /*!
186 * @brief Sets the FTM clock source.
187 *
188 * @param ftmBaseAddr The FTM base address
189 * @param clock The FTM peripheral clock selection\n
190 * bits - 00: No clock 01: system clock 10: fixed clock 11: External clock
191 */
192 static inline void FTM_HAL_SetClockSource(uint32_t ftmBaseAddr, ftm_clock_source_t clock)
193 {
194 BW_FTM_SC_CLKS(ftmBaseAddr, clock);
195 }
196
197 /*!
198 * @brief Reads the FTM clock source.
199 *
200 * @param ftmBaseAddr The FTM base address
201 *
202 * @return The FTM clock source selection\n
203 * bits - 00: No clock 01: system clock 10: fixed clock 11:External clock
204 */
205 static inline uint8_t FTM_HAL_GetClockSource(uint32_t ftmBaseAddr)
206 {
207 return BR_FTM_SC_CLKS(ftmBaseAddr);
208 }
209
210 /*!
211 * @brief Sets the FTM clock divider.
212 *
213 * @param ftmBaseAddr The FTM base address
214 * @param ps The FTM peripheral clock pre-scale divider
215 */
216 static inline void FTM_HAL_SetClockPs(uint32_t ftmBaseAddr, ftm_clock_ps_t ps)
217 {
218 BW_FTM_SC_PS(ftmBaseAddr, ps);
219 }
220
221 /*!
222 * @brief Reads the FTM clock divider.
223 *
224 * @param ftmBaseAddr The FTM base address
225 *
226 * @return The FTM clock pre-scale divider
227 */
228 static inline uint8_t FTM_HAL_GetClockPs(uint32_t ftmBaseAddr)
229 {
230 return BR_FTM_SC_PS(ftmBaseAddr);
231 }
232
233 /*!
234 * @brief Enables the FTM peripheral timer overflow interrupt.
235 *
236 * @param ftmBaseAddr The FTM base address
237 */
238 static inline void FTM_HAL_EnableTimerOverflowInt(uint32_t ftmBaseAddr)
239 {
240 HW_FTM_SC_SET(ftmBaseAddr, BM_FTM_SC_TOIE);
241 }
242
243 /*!
244 * @brief Disables the FTM peripheral timer overflow interrupt.
245 *
246 * @param ftmBaseAddr The FTM base address
247 */
248 static inline void FTM_HAL_DisableTimerOverflowInt(uint32_t ftmBaseAddr)
249 {
250 HW_FTM_SC_CLR(ftmBaseAddr, BM_FTM_SC_TOIE);
251 }
252
253 /*!
254 * @brief Reads the bit that controls enabling the FTM timer overflow interrupt.
255 *
256 * @param baseAddr FTM module base address.
257 * @retval true if overflow interrupt is enabled, false if not
258 */
259 static inline bool FTM_HAL_IsOverflowIntEnabled(uint32_t baseAddr)
260 {
261 return (bool)(BR_FTM_SC_TOIE(baseAddr));
262 }
263
264 /*!
265 * @brief Clears the timer overflow interrupt flag.
266 *
267 * @param ftmBaseAddr The FTM base address
268 */
269 static inline void FTM_HAL_ClearTimerOverflow(uint32_t ftmBaseAddr)
270 {
271 BW_FTM_SC_TOF(ftmBaseAddr, 0);
272 }
273
274 /*!
275 * @brief Returns the FTM peripheral timer overflow interrupt flag.
276 *
277 * @param ftmBaseAddr The FTM base address
278 * @retval true if overflow, false if not
279 */
280 static inline bool FTM_HAL_HasTimerOverflowed(uint32_t ftmBaseAddr)
281 {
282 return BR_FTM_SC_TOF(ftmBaseAddr);
283 }
284
285 /*!
286 * @brief Sets the FTM center-aligned PWM select.
287 *
288 * @param ftmBaseAddr The FTM base address
289 * @param mode 1:upcounting mode 0:up_down counting mode
290 */
291 static inline void FTM_HAL_SetCpwms(uint32_t ftmBaseAddr, uint8_t mode)
292 {
293 assert(mode < 2);
294 BW_FTM_SC_CPWMS(ftmBaseAddr, mode);
295 }
296
297 /*!
298 * @brief Sets the FTM peripheral current counter value.
299 *
300 * @param ftmBaseAddr The FTM base address
301 * @param val FTM timer counter value to be set
302 */
303 static inline void FTM_HAL_SetCounter(uint32_t ftmBaseAddr,uint16_t val)
304 {
305 BW_FTM_CNT_COUNT(ftmBaseAddr, val);
306 }
307
308 /*!
309 * @brief Returns the FTM peripheral current counter value.
310 *
311 * @param ftmBaseAddr The FTM base address
312 * @retval current FTM timer counter value
313 */
314 static inline uint16_t FTM_HAL_GetCounter(uint32_t ftmBaseAddr)
315 {
316 return BR_FTM_CNT_COUNT(ftmBaseAddr);
317 }
318
319 /*!
320 * @brief Sets the FTM peripheral timer modulo value.
321 *
322 * @param ftmBaseAddr The FTM base address
323 * @param val The value to be set to the timer modulo
324 */
325 static inline void FTM_HAL_SetMod(uint32_t ftmBaseAddr, uint16_t val)
326 {
327 BW_FTM_MOD_MOD(ftmBaseAddr, val);
328 }
329
330 /*!
331 * @brief Returns the FTM peripheral counter modulo value.
332 *
333 * @param ftmBaseAddr The FTM base address
334 * @retval FTM timer modulo value
335 */
336 static inline uint16_t FTM_HAL_GetMod(uint32_t ftmBaseAddr)
337 {
338 return BR_FTM_MOD_MOD(ftmBaseAddr);
339 }
340
341 /*!
342 * @brief Sets the FTM peripheral timer counter initial value.
343 *
344 * @param ftmBaseAddr The FTM base address
345 * @param val initial value to be set
346 */
347 static inline void FTM_HAL_SetCounterInitVal(uint32_t ftmBaseAddr, uint16_t val)
348 {
349 BW_FTM_CNTIN_INIT(ftmBaseAddr, val & BM_FTM_CNTIN_INIT);
350 }
351
352 /*!
353 * @brief Returns the FTM peripheral counter initial value.
354 *
355 * @param ftmBaseAddr The FTM base address
356 * @retval FTM timer counter initial value
357 */
358 static inline uint16_t FTM_HAL_GetCounterInitVal(uint32_t ftmBaseAddr)
359 {
360 return BR_FTM_CNTIN_INIT(ftmBaseAddr);
361 }
362
363 /*FTM channel operating mode (Mode, edge and level selection) for capture, output, PWM, combine, dual */
364 /*!
365 * @brief Sets the FTM peripheral timer channel mode.
366 *
367 * @param ftmBaseAddr The FTM base address
368 * @param channel The FTM peripheral channel number
369 * @param selection The mode to be set valid value MSnB:MSnA :00,01, 10, 11
370 */
371 static inline void FTM_HAL_SetChnMSnBAMode(uint32_t ftmBaseAddr, uint8_t channel, uint8_t selection)
372 {
373 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
374 BW_FTM_CnSC_MSA(ftmBaseAddr, channel, selection & 1);
375 BW_FTM_CnSC_MSB(ftmBaseAddr, channel, selection & 2 ? 1 : 0);
376 }
377
378 /*!
379 * @brief Sets the FTM peripheral timer channel edge level.
380 *
381 * @param ftmBaseAddr The FTM base address
382 * @param channel The FTM peripheral channel number
383 * @param level The rising or falling edge to be set, valid value ELSnB:ELSnA :00,01, 10, 11
384 */
385 static inline void FTM_HAL_SetChnEdgeLevel(uint32_t ftmBaseAddr, uint8_t channel, uint8_t level)
386 {
387 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
388 BW_FTM_CnSC_ELSA(ftmBaseAddr, channel, level & 1 ? 1 : 0);
389 BW_FTM_CnSC_ELSB(ftmBaseAddr, channel, level & 2 ? 1 : 0);
390 }
391
392 /*!
393 * @brief Gets the FTM peripheral timer channel mode.
394 *
395 * @param ftmBaseAddr The FTM base address
396 * @param channel The FTM peripheral channel number
397 * @retval The MSnB:MSnA mode value, will be 00,01, 10, 11
398 */
399 static inline uint8_t FTM_HAL_GetChnMode(uint32_t ftmBaseAddr, uint8_t channel)
400 {
401 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
402 return (BR_FTM_CnSC_MSA(ftmBaseAddr, channel)|| (BR_FTM_CnSC_MSB(ftmBaseAddr, channel) << 1));
403 }
404
405 /*!
406 * @brief Gets the FTM peripheral timer channel edge level.
407 *
408 * @param ftmBaseAddr The FTM base address
409 * @param channel The FTM peripheral channel number
410 * @retval The ELSnB:ELSnA mode value, will be 00,01, 10, 11
411 */
412 static inline uint8_t FTM_HAL_GetChnEdgeLevel(uint32_t ftmBaseAddr, uint8_t channel)
413 {
414 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
415 return (BR_FTM_CnSC_ELSA(ftmBaseAddr, channel)|| (BR_FTM_CnSC_ELSB(ftmBaseAddr, channel) << 1));
416 }
417
418 /*!
419 * @brief Enables or disables the FTM peripheral timer channel DMA.
420 *
421 * @param ftmBaseAddr The FTM base address
422 * @param channel The FTM peripheral channel number
423 * @param val enable or disable
424 */
425 static inline void FTM_HAL_SetChnDmaCmd(uint32_t ftmBaseAddr, uint8_t channel, bool val)
426 {
427 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
428 BW_FTM_CnSC_DMA(ftmBaseAddr, channel,(val? 1 : 0));
429 }
430
431 /*!
432 * @brief Returns whether the FTM peripheral timer channel DMA is enabled.
433 *
434 * @param ftmBaseAddr The FTM base address
435 * @param channel The FTM peripheral channel number
436 * @retval true if enabled, false if disabled
437 */
438 static inline bool FTM_HAL_IsChnDma(uint32_t ftmBaseAddr, uint8_t channel)
439 {
440 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
441 return (BR_FTM_CnSC_DMA(ftmBaseAddr, channel) ? true : false);
442 }
443
444 /*!
445 * @brief Enables the FTM peripheral timer channel(n) interrupt.
446 *
447 * @param ftmBaseAddr The FTM base address
448 * @param channel The FTM peripheral channel number
449 */
450 static inline void FTM_HAL_EnableChnInt(uint32_t ftmBaseAddr, uint8_t channel)
451 {
452 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
453 BW_FTM_CnSC_CHIE(ftmBaseAddr, channel, 1);
454 }
455 /*!
456 * @brief Disables the FTM peripheral timer channel(n) interrupt.
457 *
458 * @param ftmBaseAddr The FTM base address
459 * @param channel The FTM peripheral channel number
460 */
461 static inline void FTM_HAL_DisableChnInt(uint32_t ftmBaseAddr, uint8_t channel)
462 {
463 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
464 BW_FTM_CnSC_CHIE(ftmBaseAddr, channel, 0);
465 }
466
467 /*!
468 * @brief Returns whether any event for the FTM peripheral timer channel has occurred.
469 *
470 * @param ftmBaseAddr The FTM base address
471 * @param channel The FTM peripheral channel number
472 * @retval true if event occurred, false otherwise.
473 */
474 static inline bool FTM_HAL_HasChnEventOccurred(uint32_t ftmBaseAddr, uint8_t channel)
475 {
476 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
477 return (BR_FTM_CnSC_CHF(ftmBaseAddr, channel)) ? true : false;
478 }
479
480 /*FTM channel control*/
481 /*!
482 * @brief Sets the FTM peripheral timer channel counter value.
483 *
484 * @param ftmBaseAddr The FTM base address
485 * @param channel The FTM peripheral channel number
486 * @param val counter value to be set
487 */
488 static inline void FTM_HAL_SetChnCountVal(uint32_t ftmBaseAddr, uint8_t channel, uint16_t val)
489 {
490 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
491 HW_FTM_CnV_WR(ftmBaseAddr, channel, val);
492 }
493
494 /*!
495 * @brief Gets the FTM peripheral timer channel counter value.
496 *
497 * @param ftmBaseAddr The FTM base address
498 * @param channel The FTM peripheral channel number
499 * @retval val return current channel counter value
500 */
501 static inline uint16_t FTM_HAL_GetChnCountVal(uint32_t ftmBaseAddr, uint8_t channel, uint16_t val)
502 {
503 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
504 return BR_FTM_CnV_VAL(ftmBaseAddr, channel);
505 }
506
507 /*!
508 * @brief Gets the FTM peripheral timer channel event status.
509 *
510 * @param ftmBaseAddr The FTM base address
511 * @param channel The FTM peripheral channel number
512 * @retval val return current channel event status value
513 */
514 static inline uint32_t FTM_HAL_GetChnEventStatus(uint32_t ftmBaseAddr, uint8_t channel)
515 {
516 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
517 return (HW_FTM_STATUS_RD(ftmBaseAddr)&(1U << channel)) ? true : false;
518 /*return BR_FTM_STATUS(ftmBaseAddr, channel);*/
519 }
520
521 /*!
522 * @brief Clears the FTM peripheral timer all channel event status.
523 *
524 * @param ftmBaseAddr The FTM base address
525 * @param channel The FTM peripheral channel number
526 * @retval val return current channel counter value
527 */
528 static inline void FTM_HAL_ClearChnEventStatus(uint32_t ftmBaseAddr, uint8_t channel)
529 {
530 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
531 HW_FTM_STATUS_CLR(ftmBaseAddr, 1U << channel);
532 }
533
534 /*!
535 * @brief Sets the FTM peripheral timer channel output mask.
536 *
537 * @param ftmBaseAddr The FTM base address
538 * @param channel The FTM peripheral channel number
539 * @param mask mask to be set 0 or 1, unmasked or masked
540 */
541 static inline void FTM_HAL_SetChnOutputMask(uint32_t ftmBaseAddr, uint8_t channel, bool mask)
542 {
543 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
544 mask? HW_FTM_OUTMASK_SET(ftmBaseAddr, 1U << channel) : HW_FTM_OUTMASK_CLR(ftmBaseAddr, 1U << channel);
545 /* BW_FTM_OUTMASK_CHnOM(ftmBaseAddr, channel,mask); */
546 }
547
548 /*!
549 * @brief Sets the FTM peripheral timer channel output initial state 0 or 1.
550 *
551 * @param ftmBaseAddr The FTM base address
552 * @param channel The FTM peripheral channel number
553 * @param state counter value to be set 0 or 1
554 */
555 static inline void FTM_HAL_SetChnOutputInitState(uint32_t ftmBaseAddr, uint8_t channel, uint8_t state)
556 {
557 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
558 HW_FTM_OUTINIT_CLR(ftmBaseAddr, 1U << channel);
559 HW_FTM_OUTINIT_SET(ftmBaseAddr, (uint8_t)(state << channel));
560 }
561
562 /*!
563 * @brief Sets the FTM peripheral timer channel output polarity.
564 *
565 * @param ftmBaseAddr The FTM base address
566 * @param channel The FTM peripheral channel number
567 * @param pol polarity to be set 0 or 1
568 */
569 static inline void FTM_HAL_SetChnOutputPolarity(uint32_t ftmBaseAddr, uint8_t channel, uint8_t pol)
570 {
571 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
572 HW_FTM_POL_CLR(ftmBaseAddr, 1U << channel);
573 HW_FTM_POL_SET(ftmBaseAddr, (uint8_t)(pol << channel));
574 }
575 /*!
576 * @brief Sets the FTM peripheral timer channel input polarity.
577 *
578 * @param ftmBaseAddr The FTM base address
579 * @param channel The FTM peripheral channel number
580 * @param pol polarity to be set, 0: active high, 1:active low
581 */
582 static inline void FTM_HAL_SetChnFaultInputPolarity(uint32_t ftmBaseAddr, uint8_t channel, uint8_t pol)
583 {
584 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
585 HW_FTM_FLTPOL_CLR(ftmBaseAddr, 1U << channel);
586 HW_FTM_FLTPOL_SET(ftmBaseAddr, (uint8_t)(pol<<channel));
587 }
588
589
590 /*Feature mode selection HAL*/
591 /*FTM fault control*/
592 /*!
593 * @brief Enables the FTM peripheral timer fault interrupt.
594 *
595 * @param ftmBaseAddr The FTM base address
596 */
597 static inline void FTM_HAL_EnableFaultInt(uint32_t ftmBaseAddr)
598 {
599 BW_FTM_MODE_FAULTIE(ftmBaseAddr, 1);
600 }
601
602 /*!
603 * @brief Disables the FTM peripheral timer fault interrupt.
604 *
605 * @param ftmBaseAddr The FTM base address
606 */
607 static inline void FTM_HAL_DisableFaultInt(uint32_t ftmBaseAddr)
608 {
609 BW_FTM_MODE_FAULTIE(ftmBaseAddr, 0);
610 }
611
612 /*!
613 * @brief Defines the FTM fault control mode.
614 *
615 * @param ftmBaseAddr The FTM base address
616 * @param mode, valid options are 1, 2, 3, 4
617 */
618 static inline void FTM_HAL_SetFaultControlMode(uint32_t ftmBaseAddr, uint8_t mode)
619 {
620 BW_FTM_MODE_FAULTM(ftmBaseAddr, mode);
621 }
622
623 /*!
624 * @brief Enables or disables the FTM peripheral timer capture test mode.
625 *
626 * @param ftmBaseAddr The FTM base address
627 * @param enable true to enable capture test mode, false to disable
628 */
629 static inline void FTM_HAL_SetCaptureTestCmd(uint32_t ftmBaseAddr, bool enable)
630 {
631 BW_FTM_MODE_CAPTEST(ftmBaseAddr, enable ? 1 : 0);
632 }
633
634 /*!
635 * @brief Enables or disables the FTM write protection.
636 *
637 * @param ftmBaseAddr The FTM base address
638 * @param enable true: Write-protection is enabled, false: Write-protection is disabled
639 */
640 static inline void FTM_HAL_SetWriteProtectionCmd(uint32_t ftmBaseAddr, bool enable)
641 {
642 enable ? BW_FTM_FMS_WPEN(ftmBaseAddr, 1) : BW_FTM_MODE_WPDIS(ftmBaseAddr, 1);
643 }
644
645 /*!
646 * @brief Enables the FTM peripheral timer group.
647 *
648 * @param ftmBaseAddr The FTM base address
649 * @param enable true: all registers including FTM-specific registers are available
650 * false: only the TPM-compatible registers are available
651 */
652 static inline void FTM_HAL_Enable(uint32_t ftmBaseAddr, bool enable)
653 {
654 assert(BR_FTM_MODE_WPDIS(ftmBaseAddr));
655 BW_FTM_MODE_FTMEN(ftmBaseAddr, enable ? 1 : 0);
656 }
657
658 /*!
659 * @brief Initializes the channels output.
660 *
661 * @param ftmBaseAddr The FTM base address
662 * @param enable true: the channels output is initialized according to the state of OUTINIT reg
663 * false: has no effect
664 */
665 static inline void FTM_HAL_SetInitChnOutputCmd(uint32_t ftmBaseAddr, bool enable)
666 {
667 BW_FTM_MODE_INIT(ftmBaseAddr, enable ? 1 : 0);
668 }
669
670 /*!
671 * @brief Sets the FTM peripheral timer sync mode.
672 *
673 * @param ftmBaseAddr The FTM base address
674 * @param enable true: no restriction both software and hardware triggers can be used\n
675 * false: software trigger can only be used for MOD and CnV synch, hardware trigger
676 * only for OUTMASK and FTM counter synch.
677 */
678 static inline void FTM_HAL_SetPwmSyncMode(uint32_t ftmBaseAddr, bool enable)
679 {
680 BW_FTM_MODE_PWMSYNC(ftmBaseAddr, enable ? 1 : 0);
681 }
682
683 /*FTM synchronization control*/
684 /*!
685 * @brief Enables or disables the FTM peripheral timer software trigger.
686 *
687 * @param ftmBaseAddr The FTM base address.
688 * @param enable true: software trigger is selected, false: software trigger is not selected
689 */
690 static inline void FTM_HAL_SetSoftwareTriggerCmd(uint32_t ftmBaseAddr, bool enable)
691 {
692 BW_FTM_SYNC_SWSYNC(ftmBaseAddr, enable ? 1 : 0);
693 }
694
695 /*!
696 * @brief Sets the FTM peripheral timer hardware trigger.
697 *
698 * @param ftmBaseAddr The FTM base address
699 * @param trigger_num 0, 1, 2 for trigger0, trigger1 and trigger3
700 * @param enable true: enable hardware trigger from field trigger_num for PWM synch
701 * false: disable hardware trigger from field trigger_num for PWM synch
702 */
703 void FTM_HAL_SetHardwareTrigger(uint32_t ftmBaseAddr, uint8_t trigger_num, bool enable);
704
705 /*!
706 * @brief Determines when the OUTMASK register is updated with the value of its buffer.
707 *
708 * @param ftmBaseAddr The FTM base address
709 * @param enable true if OUTMASK register is updated only by PWM sync\n
710 * false if OUTMASK register is updated in all rising edges of the system clock
711 */
712 static inline void FTM_HAL_SetOutmaskPwmSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
713 {
714 BW_FTM_SYNC_SYNCHOM(ftmBaseAddr, enable ? 1 : 0);
715 }
716
717 /*!
718 * @brief Determines if the FTM counter is re-initialized when the selected trigger for
719 * synchronization is detected.
720 *
721 * @param ftmBaseAddr The FTM base address
722 * @param enable True to update FTM counter when triggered , false to count normally
723 */
724 static inline void FTM_HAL_SetCountReinitSyncCmd(uint32_t ftmBaseAddr, bool enable)
725 {
726 BW_FTM_SYNC_REINIT(ftmBaseAddr, enable ? 1 : 0);
727 }
728
729 /*!
730 * @brief Enables or disables the FTM peripheral timer maximum loading points.
731 *
732 * @param ftmBaseAddr The FTM base address
733 * @param enable True to enable maximum loading point, false to disable
734 */
735 static inline void FTM_HAL_SetMaxLoadingCmd(uint32_t ftmBaseAddr, bool enable)
736 {
737 BW_FTM_SYNC_CNTMAX(ftmBaseAddr, enable ? 1 : 0);
738 }
739 /*!
740 * @brief Enables or disables the FTM peripheral timer minimum loading points.
741 *
742 * @param ftmBaseAddr The FTM base address
743 * @param enable True to enable minimum loading point, false to disable
744 */
745 static inline void FTM_HAL_SetMinLoadingCmd(uint32_t ftmBaseAddr, bool enable)
746 {
747 BW_FTM_SYNC_CNTMIN(ftmBaseAddr, enable ? 1 : 0);
748 }
749
750 /*!
751 * @brief Combines the channel control.
752 *
753 * Returns an index for each channel pair.
754 *
755 * @param channel The FTM peripheral channel number.
756 * @return 0 for channel pair 0 & 1\n
757 * 1 for channel pair 2 & 3\n
758 * 2 for channel pair 4 & 5\n
759 * 3 for channel pair 6 & 7
760 */
761 uint32_t FTM_HAL_GetChnPairIndex(uint8_t channel);
762
763 /*!
764 * @brief Enables the FTM peripheral timer channel pair fault control.
765 *
766 * @param ftmBaseAddr The FTM base address
767 * @param channel The FTM peripheral channel number
768 * @param enable True to enable fault control, false to disable
769 */
770 static inline void FTM_HAL_SetDualChnFaultCmd(uint32_t ftmBaseAddr, uint8_t channel, bool enable)
771 {
772 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
773
774 enable ? HW_FTM_COMBINE_SET(ftmBaseAddr, BM_FTM_COMBINE_FAULTEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH)):
775 HW_FTM_COMBINE_CLR(ftmBaseAddr, BM_FTM_COMBINE_FAULTEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH));
776 }
777
778 /*!
779 * @brief Enables or disables the FTM peripheral timer channel pair counter PWM sync.
780 *
781 * @param ftmBaseAddr The FTM base address
782 * @param channel The FTM peripheral channel number
783 * @param enable True to enable PWM synchronization, false to disable
784 */
785 static inline void FTM_HAL_SetDualChnPwmSyncCmd(uint32_t ftmBaseAddr, uint8_t channel, bool enable)
786 {
787 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
788
789 enable ? HW_FTM_COMBINE_SET(ftmBaseAddr, BM_FTM_COMBINE_SYNCEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH)):
790 HW_FTM_COMBINE_CLR(ftmBaseAddr, BM_FTM_COMBINE_SYNCEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH));
791 }
792
793 /*!
794 * @brief Enables or disabled the FTM peripheral timer channel pair deadtime insertion.
795 *
796 * @param ftmBaseAddr The FTM base address
797 * @param channel The FTM peripheral channel number
798 * @param enable True to enable deadtime insertion, false to disable
799 */
800 static inline void FTM_HAL_SetDualChnDeadtimeCmd(uint32_t ftmBaseAddr, uint8_t channel, bool enable)
801 {
802 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
803
804 enable ? HW_FTM_COMBINE_SET(ftmBaseAddr, BM_FTM_COMBINE_DTEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH)):
805 HW_FTM_COMBINE_CLR(ftmBaseAddr, BM_FTM_COMBINE_DTEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH));
806 }
807
808 /*!
809 * @brief Enables or disables the FTM peripheral timer channel dual edge capture decap.
810 *
811 * @param ftmBaseAddr The FTM base address
812 * @param channel The FTM peripheral channel number
813 * @param enable True to enable dual edge capture mode, false to disable
814 */
815 static inline void FTM_HAL_SetDualChnDecapCmd(uint32_t ftmBaseAddr, uint8_t channel, bool enable)
816 {
817 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
818
819 enable ? HW_FTM_COMBINE_SET(ftmBaseAddr, BM_FTM_COMBINE_DECAP0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH)):
820 HW_FTM_COMBINE_CLR(ftmBaseAddr, BM_FTM_COMBINE_DECAP0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH));
821 }
822
823 /*!
824 * @brief Enables the FTM peripheral timer dual edge capture mode.
825 *
826 * @param ftmBaseAddr The FTM base address
827 * @param channel The FTM peripheral channel number
828 * @param enable True to enable dual edge capture, false to disable
829 */
830 static inline void FTM_HAL_SetDualEdgeCaptureCmd(uint32_t ftmBaseAddr, uint8_t channel, bool enable)
831 {
832 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
833
834 enable ? HW_FTM_COMBINE_SET(ftmBaseAddr, BM_FTM_COMBINE_DECAPEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH)):
835 HW_FTM_COMBINE_CLR(ftmBaseAddr, BM_FTM_COMBINE_DECAPEN0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH));
836 }
837
838 /*!
839 * @brief Enables or disables the FTM peripheral timer channel pair output complement mode.
840 *
841 * @param ftmBaseAddr The FTM base address
842 * @param channel The FTM peripheral channel number
843 * @param enable True to enable complementary mode, false to disable
844 */
845 static inline void FTM_HAL_SetDualChnCompCmd(uint32_t ftmBaseAddr, uint8_t channel, bool enable)
846 {
847 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
848
849 enable ? HW_FTM_COMBINE_SET(ftmBaseAddr, BM_FTM_COMBINE_COMP0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH)):
850 HW_FTM_COMBINE_CLR(ftmBaseAddr, BM_FTM_COMBINE_COMP0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH));
851
852 }
853
854 /*!
855 * @brief Enables or disables the FTM peripheral timer channel pair output combine mode.
856 *
857 * @param ftmBaseAddr The FTM base address
858 * @param channel The FTM peripheral channel number
859 * @param enable True to enable channel pair to combine, false to disable
860 */
861 static inline void FTM_HAL_SetDualChnCombineCmd(uint32_t ftmBaseAddr, uint8_t channel, bool enable)
862 {
863 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
864
865 enable ? HW_FTM_COMBINE_SET(ftmBaseAddr, BM_FTM_COMBINE_COMBINE0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH)):
866 HW_FTM_COMBINE_CLR(ftmBaseAddr, BM_FTM_COMBINE_COMBINE0 << (FTM_HAL_GetChnPairIndex(channel) * FTM_COMBINE_CHAN_CTRL_WIDTH));
867 }
868
869 /*FTM dead time insertion control*/
870 /*!
871 * @brief Sets the FTM deadtime divider.
872 *
873 * @param ftmBaseAddr The FTM base address
874 * @param divider The FTM peripheral prescale divider\n
875 * 0x :divided by 1, 10: divided by 4, 11:divided by 16
876 */
877 static inline void FTM_HAL_SetDeadtimePrescale(uint32_t ftmBaseAddr, ftm_deadtime_ps_t divider)
878 {
879 BW_FTM_DEADTIME_DTPS(ftmBaseAddr, divider);
880 }
881
882 /*!
883 * @brief Sets the FTM deadtime value.
884 *
885 * @param ftmBaseAddr The FTM base address
886 * @param count The FTM peripheral prescale divider\n
887 * 0: no counts inserted, 1: 1 count is inserted, 2: 2 count is inserted....
888 */
889 static inline void FTM_HAL_SetDeadtimeCount(uint32_t ftmBaseAddr, uint8_t count)
890 {
891 BW_FTM_DEADTIME_DTVAL(ftmBaseAddr, count);
892 }
893
894 /*!
895 * @brief Enables or disables the generation of the trigger when the FTM counter is equal to the CNTIN register.
896 *
897 * @param ftmBaseAddr The FTM base address
898 * @param enable True to enable, false to disable
899 */
900 static inline void FTM_HAL_SetInitTriggerCmd(uint32_t ftmBaseAddr, bool enable)
901 {
902 BW_FTM_EXTTRIG_INITTRIGEN(ftmBaseAddr, enable ? 1 : 0);
903 }
904
905 /*FTM external trigger */
906 /*!
907 * @brief Enables or disables the generation of the FTM peripheral timer channel trigger.
908 *
909 * Enables or disables the when the generation of the FTM peripheral timer channel trigger when the
910 * FTM counter is equal to its initial value. Channels 6 and 7 cannot be used as triggers.
911 *
912 * @param ftmBaseAddr The FTM base address
913 * @param channel Channel to be enabled, valid value 0, 1, 2, 3, 4, 5
914 * @param val True to enable, false to disable
915 */
916 void FTM_HAL_SetChnTriggerCmd(uint32_t ftmBaseAddr, uint8_t channel, bool val);
917
918 /*!
919 * @brief Checks whether any channel trigger event has occurred.
920 *
921 * @param ftmBaseAddr The FTM base address
922 * @retval true if there is a channel trigger event, false if not.
923 */
924 static inline bool FTM_HAL_IsChnTriggerGenerated(uint32_t ftmBaseAddr)
925 {
926 return BR_FTM_EXTTRIG_TRIGF(ftmBaseAddr);
927 }
928
929
930 /*Fault mode status*/
931 /*!
932 * @brief Gets the FTM detected fault input.
933 *
934 * This function reads the status for all fault inputs
935 *
936 * @param ftmBaseAddr The FTM base address
937 * @retval Return fault byte
938 */
939 static inline uint8_t FTM_HAL_GetDetectedFaultInput(uint32_t ftmBaseAddr)
940 {
941 return (HW_FTM_FMS(ftmBaseAddr).U & 0x0f);
942 }
943 /*!
944 * @brief Checks whether the write protection is enabled.
945 *
946 * @param ftmBaseAddr The FTM base address
947 * @retval True if enabled, false if not
948 */
949 static inline bool FTM_HAL_IsWriteProtectionEnabled(uint32_t ftmBaseAddr)
950 {
951 return BR_FTM_FMS_WPEN(ftmBaseAddr) ? true : false;
952 }
953
954 /*Quadrature decoder control*/
955
956 /*!
957 * @brief Enables the channel quadrature decoder.
958 *
959 * @param ftmBaseAddr The FTM base address
960 * @param enable True to enable, false to disable
961 */
962 static inline void FTM_HAL_SetQuadDecoderCmd(uint32_t ftmBaseAddr, bool enable)
963 {
964 BW_FTM_QDCTRL_QUADEN(ftmBaseAddr, enable ? 1 : 0);
965 }
966
967 /*!
968 * @brief Enables or disables the phase A input filter.
969 *
970 * @param ftmBaseAddr The FTM base address
971 * @param enable true enables the phase input filter, false disables the filter
972 */
973 static inline void FTM_HAL_SetQuadPhaseAFilterCmd(uint32_t ftmBaseAddr, bool enable)
974 {
975 BW_FTM_QDCTRL_PHAFLTREN(ftmBaseAddr, enable ? 1 : 0);
976 }
977
978 /*!
979 * @brief Enables or disables the phase B input filter.
980 *
981 * @param ftmBaseAddr The FTM base address
982 * @param enable true enables the phase input filter, false disables the filter
983 */
984 static inline void FTM_HAL_SetQuadPhaseBFilterCmd(uint32_t ftmBaseAddr, bool enable)
985 {
986 BW_FTM_QDCTRL_PHBFLTREN(ftmBaseAddr, enable ? 1 : 0);
987 }
988
989 /*!
990 * @brief Selects polarity for the quadrature decode phase A input.
991 *
992 * @param ftmBaseAddr The FTM base address
993 * @param mode 0: Normal polarity, 1: Inverted polarity
994 */
995 static inline void FTM_HAL_SetQuadPhaseAPolarity(uint32_t ftmBaseAddr,
996 ftm_quad_phase_polarity_t mode)
997 {
998 BW_FTM_QDCTRL_PHAPOL(ftmBaseAddr, mode);
999 }
1000
1001 /*!
1002 * @brief Selects polarity for the quadrature decode phase B input.
1003 *
1004 * @param ftmBaseAddr The FTM base address
1005 * @param mode 0: Normal polarity, 1: Inverted polarity
1006 */
1007 static inline void FTM_HAL_SetQuadPhaseBPolarity(uint32_t ftmBaseAddr,
1008 ftm_quad_phase_polarity_t mode)
1009 {
1010 BW_FTM_QDCTRL_PHBPOL(ftmBaseAddr, mode);
1011 }
1012
1013 /*!
1014 * @brief Sets the encoding mode used in quadrature decoding mode.
1015 *
1016 * @param ftmBaseAddr The FTM base address
1017 * @param quadMode 0: Phase A and Phase B encoding mode\n
1018 * 1: Count and direction encoding mode
1019 */
1020 static inline void FTM_HAL_SetQuadMode(uint32_t ftmBaseAddr, ftm_quad_decode_mode_t quadMode)
1021 {
1022 BW_FTM_QDCTRL_QUADMODE(ftmBaseAddr, quadMode);
1023 }
1024
1025 /*!
1026 * @brief Gets the FTM counter direction in quadrature mode.
1027 *
1028 * @param ftmBaseAddr The FTM base address
1029 *
1030 * @retval 1 if counting direction is increasing, 0 if counting direction is decreasing
1031 */
1032 static inline uint8_t FTM_HAL_GetQuadDir(uint32_t ftmBaseAddr)
1033 {
1034 return BR_FTM_QDCTRL_QUADMODE(ftmBaseAddr);
1035 }
1036
1037 /*!
1038 * @brief Gets the Timer overflow direction in quadrature mode.
1039 *
1040 * @param ftmBaseAddr The FTM base address
1041 *
1042 * @retval 1 if TOF bit was set on the top of counting, o if TOF bit was set on the bottom of counting
1043 */
1044 static inline uint8_t FTM_HAL_GetQuadTimerOverflowDir(uint32_t ftmBaseAddr)
1045 {
1046 return BR_FTM_QDCTRL_TOFDIR(ftmBaseAddr);
1047 }
1048
1049 /*!
1050 * @brief Sets the FTM peripheral timer channel input capture filter value.
1051 * @param ftmBaseAddr The FTM base address
1052 * @param channel The FTM peripheral channel number, only 0,1,2,3, channel 4, 5,6, 7 don't have.
1053 * @param val Filter value to be set
1054 */
1055 void FTM_HAL_SetChnInputCaptureFilter(uint32_t ftmBaseAddr, uint8_t channel, uint8_t val);
1056
1057 /*!
1058 * @brief Sets the fault input filter value.
1059 *
1060 * @param ftmBaseAddr The FTM base address
1061 * @param val fault input filter value
1062 */
1063 static inline void FTM_HAL_SetFaultInputFilterVal(uint32_t ftmBaseAddr, uint32_t val)
1064 {
1065 BW_FTM_FLTCTRL_FFVAL(ftmBaseAddr, val);
1066 }
1067
1068 /*!
1069 * @brief Enables or disables the fault input filter.
1070 *
1071 * @param ftmBaseAddr The FTM base address
1072 * @param inputNum fault input to be configured, valid value 0, 1, 2, 3
1073 * @param val true to enable fault input filter, false to disable fault input filter
1074 */
1075 static inline void FTM_HAL_SetFaultInputFilterCmd(uint32_t ftmBaseAddr, uint8_t inputNum, bool val)
1076 {
1077 assert(inputNum < HW_CHAN4);
1078 val ? HW_FTM_FLTCTRL_SET(ftmBaseAddr, (1U << (inputNum + 4))) :
1079 HW_FTM_FLTCTRL_CLR(ftmBaseAddr, (1U << (inputNum + 4)));
1080 }
1081
1082 /*!
1083 * @brief Enables or disables the fault input.
1084 *
1085 * @param ftmBaseAddr The FTM base address
1086 * @param inputNum fault input to be configured, valid value 0, 1, 2, 3
1087 * @param val true to enable fault input, false to disable fault input
1088 */
1089 static inline void FTM_HAL_SetFaultInputCmd(uint32_t ftmBaseAddr, uint8_t inputNum, bool val)
1090 {
1091 assert(inputNum < HW_CHAN4);
1092 val ? HW_FTM_FLTCTRL_SET(ftmBaseAddr, (1U << inputNum)) :
1093 HW_FTM_FLTCTRL_CLR(ftmBaseAddr, (1U << inputNum));
1094 }
1095
1096 /*!
1097 * @brief Enables or disables the channel invert for a channel pair.
1098 *
1099 * @param ftmBaseAddr The FTM base address
1100 * @param channel The FTM peripheral channel number
1101 * @param val true to enable channel inverting, false to disable channel inverting
1102 */
1103 static inline void FTM_HAL_SetDualChnInvertCmd(uint32_t ftmBaseAddr, uint8_t channel, bool val)
1104 {
1105 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
1106
1107 val ? HW_FTM_INVCTRL_SET(ftmBaseAddr, (1U << FTM_HAL_GetChnPairIndex(channel))) :
1108 HW_FTM_INVCTRL_CLR(ftmBaseAddr, (1U << FTM_HAL_GetChnPairIndex(channel)));
1109 }
1110
1111 /*FTM software output control*/
1112 /*!
1113 * @brief Enables or disables the channel software output control.
1114 * @param ftmBaseAddr The FTM base address
1115 * @param channel Channel to be enabled or disabled
1116 * @param val true to enable, channel output will be affected by software output control\n
1117 false to disable, channel output is unaffected
1118 */
1119 static inline void FTM_HAL_SetChnSoftwareCtrlCmd(uint32_t ftmBaseAddr, uint8_t channel, bool val)
1120 {
1121 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
1122 val ? HW_FTM_SWOCTRL_SET(ftmBaseAddr, (1U << channel)) :
1123 HW_FTM_SWOCTRL_CLR(ftmBaseAddr, (1U << channel));
1124 }
1125 /*!
1126 * @brief Sets the channel software output control value.
1127 *
1128 * @param ftmBaseAddr The FTM base address.
1129 * @param channel Channel to be configured
1130 * @param val True to set 1, false to set 0
1131 */
1132 static inline void FTM_HAL_SetChnSoftwareCtrlVal(uint32_t ftmBaseAddr, uint8_t channel, bool val)
1133 {
1134 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
1135 val ? HW_FTM_SWOCTRL_SET(ftmBaseAddr, (1U << (channel + 8))) :
1136 HW_FTM_SWOCTRL_CLR(ftmBaseAddr, (1U << (channel + 8)));
1137 }
1138
1139 /*FTM PWM load control*/
1140 /*!
1141 * @brief Enables or disables the loading of MOD, CNTIN and CV with values of their write buffer.
1142 *
1143 * @param ftmBaseAddr The FTM base address
1144 * @param enable true to enable, false to disable
1145 */
1146 static inline void FTM_HAL_SetPwmLoadCmd(uint32_t ftmBaseAddr, bool enable)
1147 {
1148 BW_FTM_PWMLOAD_LDOK(ftmBaseAddr, enable ? 1 : 0);
1149 }
1150
1151 /*!
1152 * @brief Includes or excludes the channel in the matching process.
1153 *
1154 * @param ftmBaseAddr The FTM base address
1155 * @param channel Channel to be configured
1156 * @param val true means include the channel in the matching process\n
1157 * false means do not include channel in the matching process
1158 */
1159 static inline void FTM_HAL_SetPwmLoadChnSelCmd(uint32_t ftmBaseAddr, uint8_t channel, bool val)
1160 {
1161 assert(channel < FSL_FEATURE_FTM_CHANNEL_COUNT);
1162 val ? HW_FTM_PWMLOAD_SET(ftmBaseAddr, 1U << channel) : HW_FTM_PWMLOAD_CLR(ftmBaseAddr, 1U << channel);
1163 }
1164
1165 /*FTM configuration*/
1166 /*!
1167 * @brief Enables or disables the FTM global time base signal generation to other FTM's.
1168 *
1169 * @param ftmBaseAddr The FTM base address
1170 * @param enable True to enable, false to disable
1171 */
1172 static inline void FTM_HAL_SetGlobalTimeBaseOutputCmd(uint32_t ftmBaseAddr, bool enable)
1173 {
1174 BW_FTM_CONF_GTBEOUT(ftmBaseAddr, enable ? 1 : 0);
1175 }
1176
1177 /*!
1178 * @brief Enables or disables the FTM timer global time base.
1179 *
1180 * @param ftmBaseAddr The FTM base address
1181 * @param enable True to enable, false to disable
1182 */
1183 static inline void FTM_HAL_SetGlobalTimeBaseCmd(uint32_t ftmBaseAddr, bool enable)
1184 {
1185 BW_FTM_CONF_GTBEEN(ftmBaseAddr, enable ? 1 : 0);
1186 }
1187
1188 /*!
1189 * @brief Sets the BDM mode..
1190 *
1191 * @param ftmBaseAddr The FTM base address
1192 * @param val FTM behaviour in BDM mode, options are 0,1,2,3
1193 */
1194 static inline void FTM_HAL_SetBdmMode(uint32_t ftmBaseAddr, uint8_t val)
1195 {
1196 BW_FTM_CONF_BDMMODE(ftmBaseAddr, val);
1197 }
1198
1199 /*!
1200 * @brief Sets the FTM timer TOF Frequency
1201 *
1202 * @param ftmBaseAddr The FTM base address
1203 * @param val Value of the TOF bit set frequency
1204 */
1205 static inline void FTM_HAL_SetTofFreq(uint32_t ftmBaseAddr, uint8_t val)
1206 {
1207 BW_FTM_CONF_NUMTOF(ftmBaseAddr, val);
1208 }
1209
1210 /*FTM sync configuration*/
1211 /*hardware sync*/
1212 /*!
1213 * @brief Sets the sync mode for the FTM SWOCTRL register when using a hardware trigger.
1214 *
1215 * @param ftmBaseAddr The FTM base address
1216 * @param enable true means the hardware trigger activates register sync\n
1217 * false means the hardware trigger does not activate register sync.
1218 */
1219 static inline void FTM_HAL_SetSwoctrlHardwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1220 {
1221 BW_FTM_SYNCONF_HWSOC(ftmBaseAddr, enable ? 1 : 0);
1222 }
1223
1224 /*!
1225 * @brief Sets sync mode for FTM INVCTRL register when using a hardware trigger.
1226 *
1227 * @param ftmBaseAddr The FTM base address
1228 * @param enable true means the hardware trigger activates register sync\n
1229 * false means the hardware trigger does not activate register sync.
1230 */
1231 static inline void FTM_HAL_SetInvctrlHardwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1232 {
1233 BW_FTM_SYNCONF_HWINVC(ftmBaseAddr, enable ? 1 : 0);
1234 }
1235
1236 /*!
1237 * @brief Sets sync mode for FTM OUTMASK register when using a hardware trigger.
1238 *
1239 * @param ftmBaseAddr The FTM base address
1240 * @param enable true means hardware trigger activates register sync\n
1241 * false means hardware trigger does not activate register sync.
1242 */
1243 static inline void FTM_HAL_SetOutmaskHardwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1244 {
1245 BW_FTM_SYNCONF_HWOM(ftmBaseAddr, enable ? 1 : 0);
1246 }
1247
1248 /*!
1249 * @brief Sets sync mode for FTM MOD, CNTIN and CV registers when using a hardware trigger.
1250 *
1251 * @param ftmBaseAddr The FTM base address
1252 * @param enable true means hardware trigger activates register sync\n
1253 * false means hardware trigger does not activate register sync.
1254 */
1255 static inline void FTM_HAL_SetModCntinCvHardwareSycnModeCmd(uint32_t ftmBaseAddr, bool enable)
1256 {
1257 BW_FTM_SYNCONF_HWWRBUF(ftmBaseAddr, enable ? 1 : 0);
1258 }
1259
1260 /*!
1261 * @brief Sets sync mode for FTM counter register when using a hardware trigger.
1262 *
1263 * @param ftmBaseAddr The FTM base address
1264 * @param enable true means hardware trigger activates register sync\n
1265 * false means hardware trigger does not activate register sync.
1266 */
1267 static inline void FTM_HAL_SetCounterHardwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1268 {
1269 BW_FTM_SYNCONF_HWRSTCNT(ftmBaseAddr, enable ? 1 : 0);
1270 }
1271
1272 /*!
1273 * @brief Sets sync mode for FTM SWOCTRL register when using a software trigger.
1274 *
1275 * @param ftmBaseAddr The FTM base address
1276 * @param enable true means software trigger activates register sync\n
1277 * false means software trigger does not activate register sync.
1278 */
1279 static inline void FTM_HAL_SetSwoctrlSoftwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1280 {
1281 BW_FTM_SYNCONF_SWSOC(ftmBaseAddr, enable ? 1 : 0);
1282 }
1283
1284 /*!
1285 * @brief Sets sync mode for FTM INVCTRL register when using a software trigger.
1286 *
1287 * @param ftmBaseAddr The FTM base address
1288 * @param enable true means software trigger activates register sync\n
1289 * false means software trigger does not activate register sync.
1290 */
1291 static inline void FTM_HAL_SetInvctrlSoftwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1292 {
1293 BW_FTM_SYNCONF_SWINVC(ftmBaseAddr, enable ? 1 : 0);
1294 }
1295
1296 /*!
1297 * @brief Sets sync mode for FTM OUTMASK register when using a software trigger.
1298 *
1299 * @param ftmBaseAddr The FTM base address
1300 * @param enable true means software trigger activates register sync\n
1301 * false means software trigger does not activate register sync.
1302 */
1303 static inline void FTM_HAL_SetOutmaskSoftwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1304 {
1305 BW_FTM_SYNCONF_SWOM(ftmBaseAddr, enable ? 1 : 0);
1306 }
1307
1308 /*!
1309 * @brief Sets synch mode for FTM MOD, CNTIN and CV registers when using a software trigger.
1310 *
1311 * @param ftmBaseAddr The FTM base address
1312 * @param enable true means software trigger activates register sync\n
1313 * false means software trigger does not activate register sync.
1314 */
1315 static inline void FTM_HAL_SetModCntinCvSoftwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1316 {
1317 BW_FTM_SYNCONF_SWWRBUF(ftmBaseAddr, enable ? 1 : 0);
1318 }
1319
1320 /*!
1321 * @brief Sets sync mode for FTM counter register when using a software trigger.
1322 *
1323 * @param ftmBaseAddr The FTM base address
1324 * @param enable true means software trigger activates register sync\n
1325 * false means software trigger does not activate register sync.
1326 */
1327 static inline void FTM_HAL_SetCounterSoftwareSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1328 {
1329 BW_FTM_SYNCONF_SWRSTCNT(ftmBaseAddr, enable ? 1 : 0);
1330 }
1331
1332 /*!
1333 * @brief Sets the PWM synchronization mode to enhanced or legacy.
1334 *
1335 * @param ftmBaseAddr The FTM base address
1336 * @param enable true means use Enhanced PWM synchronization\n
1337 * false means to use Legacy mode
1338 */
1339 static inline void FTM_HAL_SetPwmSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1340 {
1341 BW_FTM_SYNCONF_SYNCMODE(ftmBaseAddr, enable ? 1 : 0);
1342 }
1343
1344 /*!
1345 * @brief Sets the SWOCTRL register PWM synchronization mode.
1346 *
1347 * @param ftmBaseAddr The FTM base address
1348 * @param enable true means SWOCTRL register is updated by PWM synch\n
1349 * false means SWOCTRL register is updated at all rising edges of system clock
1350 */
1351 static inline void FTM_HAL_SetSwoctrlPwmSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1352 {
1353 BW_FTM_SYNCONF_SWOC(ftmBaseAddr, enable ? 1 : 0);
1354 }
1355
1356 /*!
1357 * @brief Sets the INVCTRL register PWM synchronization mode.
1358 *
1359 * @param ftmBaseAddr The FTM base address
1360 * @param enable true means INVCTRL register is updated by PWM synch\n
1361 * false means INVCTRL register is updated at all rising edges of system clock
1362 */
1363 static inline void FTM_HAL_SetInvctrlPwmSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1364 {
1365 BW_FTM_SYNCONF_INVC(ftmBaseAddr, enable ? 1 : 0);
1366 }
1367
1368 /*!
1369 * @brief Sets the CNTIN register PWM synchronization mode.
1370 *
1371 * @param ftmBaseAddr The FTM base address
1372 * @param enable true means CNTIN register is updated by PWM synch\n
1373 * false means CNTIN register is updated at all rising edges of system clock
1374 */
1375 static inline void FTM_HAL_SetCntinPwmSyncModeCmd(uint32_t ftmBaseAddr, bool enable)
1376 {
1377 BW_FTM_SYNCONF_CNTINC(ftmBaseAddr, enable ? 1 : 0);
1378 }
1379
1380
1381 /*HAL functionality*/
1382 /*!
1383 * @brief Resets the FTM registers
1384 *
1385 * @param instance The FTM instance number
1386 * @param ftmBaseAddr The FTM base address
1387 */
1388 void FTM_HAL_Reset(uint32_t ftmBaseAddr, uint32_t instance);
1389
1390 /*!
1391 * @brief Initializes the FTM.
1392 *
1393 * @param ftmBaseAddr The FTM base address.
1394 */
1395 void FTM_HAL_Init(uint32_t ftmBaseAddr);
1396
1397 /*Initializes the 5 FTM operating mode, input capture, output compare, PWM output(edge aligned, center-aligned, conbine), dual and quadrature).*/
1398
1399 /*void FTM_HAL_input_capture_mode(uint32_t ftmBaseAddr);*/
1400 /*void FTM_HAL_output_compare_mode(uint32_t ftmBaseAddr);*/
1401
1402 /*!
1403 * @brief Enables the FTM timer when it is PWM output mode.
1404 *
1405 * @param ftmBaseAddr The FTM base address
1406 * @param config PWM configuration parameter
1407 * @param channel The channel or channel pair number(combined mode).
1408 */
1409 void FTM_HAL_EnablePwmMode(uint32_t ftmBaseAddr, ftm_pwm_param_t *config, uint8_t channel);
1410
1411 /*!
1412 * @brief Disables the PWM output mode.
1413 *
1414 * @param ftmBaseAddr The FTM base address
1415 * @param config PWM configuration parameter
1416 * @param channel The channel or channel pair number(combined mode).
1417 */
1418 void FTM_HAL_DisablePwmMode(uint32_t ftmBaseAddr, ftm_pwm_param_t *config, uint8_t channel);
1419
1420 /*void FTM_HAL_dual_mode(uint32_t ftmBaseAddr);*/
1421 /*void FTM_HAL_quad_mode(uint32_t ftmBaseAddr);*/
1422
1423
1424 /*void FTM_HAL_set_counting_mode(); //up, up down or free running counting mode*/
1425 /*void FTM_HAL_set_deadtime(uint32_t ftmBaseAddr, uint_32 us);*/
1426
1427 /*! @}*/
1428
1429 #endif /* __FSL_FTM_HAL_H__*/
1430 /*******************************************************************************
1431 * EOF
1432 ******************************************************************************/
1433
Imprint / Impressum