ftm_ic_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
35 #include "ftm_ic_driver.h"
36 #include "ftm_hw_access.h"
37 
38 /*******************************************************************************
39  * Code
40  ******************************************************************************/
41 static void FTM_DRV_InputCaptureHandler(uint32_t instance,
42  uint8_t channelPair);
43 
44 static void FTM_DRV_IrqHandler(uint32_t instance,
45  uint8_t channelPair);
46 
47 #if (FEATURE_FTM_HAS_NUM_IRQS_CHANS == 1U)
48 void FTM0_Ch0_7_IRQHandler(void);
49 void FTM1_Ch0_7_IRQHandler(void);
50 #endif
51 
52 #if (FEATURE_FTM_HAS_NUM_IRQS_CHANS == 4U)
53 void FTM0_Ch0_Ch1_IRQHandler(void);
54 
55 void FTM0_Ch2_Ch3_IRQHandler(void);
56 
57 void FTM0_Ch4_Ch5_IRQHandler(void);
58 
59 void FTM0_Ch6_Ch7_IRQHandler(void);
60 
61 void FTM1_Ch0_Ch1_IRQHandler(void);
62 
63 void FTM1_Ch2_Ch3_IRQHandler(void);
64 
65 void FTM1_Ch4_Ch5_IRQHandler(void);
66 
67 void FTM1_Ch6_Ch7_IRQHandler(void);
68 
69 #if (FTM_INSTANCE_COUNT > 2U)
70 void FTM2_Ch0_Ch1_IRQHandler(void);
71 
72 void FTM2_Ch2_Ch3_IRQHandler(void);
73 
74 void FTM2_Ch4_Ch5_IRQHandler(void);
75 
76 void FTM2_Ch6_Ch7_IRQHandler(void);
77 
78 void FTM3_Ch0_Ch1_IRQHandler(void);
79 
80 void FTM3_Ch2_Ch3_IRQHandler(void);
81 
82 void FTM3_Ch4_Ch5_IRQHandler(void);
83 
84 void FTM3_Ch6_Ch7_IRQHandler(void);
85 #endif
86 
87 #if (FTM_INSTANCE_COUNT > 4U)
88 void FTM4_Ch0_Ch1_IRQHandler(void);
89 
90 void FTM4_Ch2_Ch3_IRQHandler(void);
91 
92 void FTM4_Ch4_Ch5_IRQHandler(void);
93 
94 void FTM4_Ch6_Ch7_IRQHandler(void);
95 
96 void FTM5_Ch0_Ch1_IRQHandler(void);
97 
98 void FTM5_Ch2_Ch3_IRQHandler(void);
99 
100 void FTM5_Ch4_Ch5_IRQHandler(void);
101 
102 void FTM5_Ch6_Ch7_IRQHandler(void);
103 #endif
104 
105 #if (FTM_INSTANCE_COUNT > 6U)
106 void FTM6_Ch0_Ch1_IRQHandler(void);
107 
108 void FTM6_Ch2_Ch3_IRQHandler(void);
109 
110 void FTM6_Ch4_Ch5_IRQHandler(void);
111 
112 void FTM6_Ch6_Ch7_IRQHandler(void);
113 
114 void FTM7_Ch0_Ch1_IRQHandler(void);
115 
116 void FTM7_Ch2_Ch3_IRQHandler(void);
117 
118 void FTM7_Ch4_Ch5_IRQHandler(void);
119 
120 void FTM7_Ch6_Ch7_IRQHandler(void);
121 #endif
122 #endif
123 
124 /*FUNCTION**********************************************************************
125  *
126  * Function Name : FTM_DRV_InitInputCapture
127  * Description : Configures Channel Input Capture for either getting time-stamps on edge detection
128  * or on signal measurement . When the edge specified in the captureMode
129  * argument occurs on the channel the FTM counter is captured into the CnV register.
130  * The user will have to read the CnV register separately to get this value. The filter
131  * function is disabled if the filterVal argument passed in is 0. The filter function
132  * is available only on channels 0,1,2,3.
133  *
134  * Implements : FTM_DRV_InitInputCapture_Activity
135  *END**************************************************************************/
137  const ftm_input_param_t * param)
138 {
139  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
140  DEV_ASSERT(param != NULL);
141  FTM_Type * ftmBase = g_ftmBase[instance];
142  uint8_t chnlPairNum = 0U;
143  uint8_t index = 0U;
144  uint8_t hwChannel = 0U;
145  ftm_signal_measurement_mode_t measurementType;
146  ftm_state_t * state = ftmStatePtr[instance];
147  status_t retStatus = STATUS_SUCCESS;
148 
149  if ((NULL != state) && (FTM_MODE_NOT_INITIALIZED == state->ftmMode))
150  {
151  FTM_DRV_SetClockSource(ftmBase, FTM_CLOCK_SOURCE_NONE);
152  FTM_DRV_SetCounterInitVal(ftmBase, 0U);
153  FTM_DRV_SetMod(ftmBase, param->nMaxCountValue);
154  FTM_DRV_SetCpwms(ftmBase, false);
155  /* Disable the combine mode */
156  FTM_DRV_SetDualChnMofCombineCmd(ftmBase, chnlPairNum, false);
157  FTM_DRV_SetDualChnCombineCmd(ftmBase, chnlPairNum, false);
158 
159  for (index = 0U; index < param->nNumChannels; index++)
160  {
161  hwChannel = param->inputChConfig[index].hwChannelId;
162  chnlPairNum = (uint8_t)(hwChannel >> 1U);
163  /* Save in state structure user define handlers */
164  state->channelsCallbacksParams[hwChannel] = param->inputChConfig[index].channelsCallbacksParams;
165  state->channelsCallbacks[hwChannel] = param->inputChConfig[index].channelsCallbacks;
166  state->enableNotification[hwChannel] = true;
167 
168  /* Enable filtering for input channels */
169  if (hwChannel < CHAN4_IDX)
170  {
171  if (true == param->inputChConfig[index].filterEn)
172  {
173  FTM_DRV_SetChnInputCaptureFilter(ftmBase, hwChannel, (uint8_t)param->inputChConfig[index].filterValue);
174  }
175  else
176  {
177  FTM_DRV_SetChnInputCaptureFilter(ftmBase, hwChannel, 0U);
178  }
179  }
180 
181  if (FTM_EDGE_DETECT == param->inputChConfig[index].inputMode)
182  {
183  /* Disable the dual edge mode */
184  FTM_DRV_SetDualEdgeCaptureCmd(ftmBase, chnlPairNum, false);
185  /* Set input capture mode */
186  FTM_DRV_SetChnMSnBAMode(ftmBase, hwChannel, 0U);
187  /* Check if no edge is selected */
189  /* Set the event which will generate the interrupt */
190  FTM_DRV_SetChnEdgeLevel(ftmBase, hwChannel, (uint8_t)param->inputChConfig[index].edgeAlignement);
191  /* Enable interrupt request for the current channel */
192  FTM_DRV_EnableChnInt(ftmBase, hwChannel);
193  INT_SYS_EnableIRQ(g_ftmIrqId[instance][hwChannel]);
194  }
195  else if (FTM_SIGNAL_MEASUREMENT == param->inputChConfig[index].inputMode)
196  {
197  /* Check the channel is even number */
198  DEV_ASSERT((hwChannel % 2U) == 0U);
199  /* Enable the dual edge mode */
200  FTM_DRV_SetDualEdgeCaptureCmd(ftmBase, chnlPairNum, true);
201  /* Enable dual edge input capture */
202  FTM_DRV_SetDualChnDecapCmd(ftmBase, chnlPairNum, true);
203  /* If continuous mode is set*/
204  if (true == param->inputChConfig[index].continuousModeEn)
205  {
206  /* Set MSnA and MSnB bit*/
207  FTM_DRV_SetChnMSnBAMode(ftmBase, hwChannel, 3U);
208  }
209  else
210  {
211  /* Clear MSnA and Set MSnB bit*/
212  FTM_DRV_SetChnMSnBAMode(ftmBase, hwChannel, 2U);
213  }
214 
215  measurementType = param->inputChConfig[index].measurementType;
216  /* Check If want to measure a pulse width or period of the signal */
217  if ((FTM_PERIOD_ON_MEASUREMENT == measurementType) || (FTM_RISING_EDGE_PERIOD_MEASUREMENT== measurementType))
218  {
219  FTM_DRV_SetChnEdgeLevel(ftmBase, hwChannel, 1U);
220  if (FTM_PERIOD_ON_MEASUREMENT == measurementType)
221  {
222  /* Measure time between rising and falling edge - positive duty */
223  FTM_DRV_SetChnEdgeLevel(ftmBase, (uint8_t)(hwChannel + 1U), 2U);
224  }
225  else
226  {
227  /* If channel (n) is configured to capture falling edges (ELS(n)B:ELS(n)A = 0:1)
228  * then channel (n+1) also captures falling edges (ELS(n+1)B:ELS(n+1)A = 0:1) */
229  FTM_DRV_SetChnEdgeLevel(ftmBase, (uint8_t)(hwChannel + 1U), 1U);
230  }
231  }
232  else if ((FTM_PERIOD_OFF_MEASUREMENT == measurementType) || (FTM_FALLING_EDGE_PERIOD_MEASUREMENT == measurementType))
233  {
234  FTM_DRV_SetChnEdgeLevel(ftmBase, hwChannel, 2U);
235  if (FTM_PERIOD_OFF_MEASUREMENT == measurementType)
236  {
237  /* Measure time between falling and rising edge - negative duty */
238  FTM_DRV_SetChnEdgeLevel(ftmBase, (uint8_t)(hwChannel + 1U), 1U);
239  }
240  else
241  {
242  /* If channel (n) is configured to capture rising edges (ELS(n)B:ELS(n)A = 1:0) than
243  * channel (n+1) is setup to capture also raising edges (ELS(n+1)B:ELS(n+1)A = 1:0) */
244  FTM_DRV_SetChnEdgeLevel(ftmBase, (uint8_t)(hwChannel + 1U), 2U);
245  }
246  }
247  else
248  {
249  retStatus = STATUS_ERROR;
250  break;
251  }
252 
253  /* Enable the interrupt request for the channel which will indicate that the measurement is done. */
254  FTM_DRV_EnableChnInt(ftmBase, (uint8_t)(hwChannel + 1U));
255  INT_SYS_EnableIRQ(g_ftmIrqId[instance][hwChannel]);
256  }
257  else
258  {
259  /* Do nothing */
260  }
261  }
262 
263  if (STATUS_SUCCESS == retStatus)
264  {
266  /* Set clock source to start the counter */
267  FTM_DRV_SetClockSource(ftmBase, state->ftmClockSource);
268  }
269  }
270  else
271  {
272  retStatus = STATUS_ERROR;
273  }
274 
275  return retStatus;
276 }
277 
278 /*FUNCTION**********************************************************************
279  *
280  * Function Name : FTM_DRV_DeinitInputCapture
281  * Description : Disables Channel Input Capture
282  *
283  * Implements : FTM_DRV_DeinitInputCapture_Activity
284  *END**************************************************************************/
286  const ftm_input_param_t * param)
287 {
288  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
289  DEV_ASSERT(param != NULL);
290  FTM_Type * ftmBase = g_ftmBase[instance];
291  uint8_t chnlPairNum = 0U;
292  uint8_t index = 0U;
293  uint8_t hwChannel = 0U;
294  ftm_state_t * state = ftmStatePtr[instance];
295 
296  /* FTM counter is disabled */
297  FTM_DRV_SetClockSource(ftmBase, FTM_CLOCK_SOURCE_NONE);
298  FTM_DRV_SetCounterInitVal(ftmBase, 0U);
299  FTM_DRV_SetMod(ftmBase, 0xFFFFU);
300  FTM_DRV_SetCpwms(ftmBase, false);
301  for (index = 0U; index < param->nNumChannels; index++)
302  {
303  hwChannel = param->inputChConfig[index].hwChannelId;
304  chnlPairNum = (uint8_t)(hwChannel >> 1U);
305  /* Disable filtering for input channels */
306  if (hwChannel < CHAN4_IDX)
307  {
308  FTM_DRV_SetChnInputCaptureFilter(ftmBase, hwChannel, 0U);
309  }
310 
311  FTM_DRV_SetDualChnCombineCmd(ftmBase, chnlPairNum, false);
312  FTM_DRV_SetDualEdgeCaptureCmd(ftmBase, chnlPairNum, false);
313  FTM_DRV_SetChnEdgeLevel(ftmBase, hwChannel, (uint8_t)0U);
314  FTM_DRV_DisableChnInt(ftmBase, hwChannel);
315  }
316 
317  /* Clear Callbacks function from the state structure */
318  for (index = 0U; index < FEATURE_FTM_CHANNEL_COUNT; index++)
319  {
320  state->channelsCallbacksParams[index] = NULL;
321  state->channelsCallbacks[index] = NULL;
322  }
323 
325 
326  return STATUS_SUCCESS;
327 }
328 
329 /*FUNCTION**********************************************************************
330  *
331  * Function Name : FTM_DRV_GetInputCaptureMeasurement
332  * Description : This function is used to calculate the measurement and/or time stamps values
333  * which are read from the C(n, n+1)V registers and stored to the static buffers.
334  *
335  * Implements : FTM_DRV_GetInputCaptureMeasurement_Activity
336  *END**************************************************************************/
337 uint16_t FTM_DRV_GetInputCaptureMeasurement(uint32_t instance,
338  uint8_t channel)
339 {
340  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
342  const ftm_state_t * state = ftmStatePtr[instance];
343  uint16_t value = 0U;
344 
345  if (state != NULL)
346  {
347  value = state->measurementResults[channel];
348  }
349 
350  return value;
351 }
352 
353 /*FUNCTION**********************************************************************
354  *
355  * Function Name : FTM_DRV_StartNewSignalMeasurement
356  * Description : This function starts new Signal Measurements on a dual input compare channel
357  * that is configured as single-shot measurement.
358  *
359  * Implements : FTM_DRV_StartNewSignalMeasurement_Activity
360  *END**************************************************************************/
362  uint8_t channel)
363 {
364  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
366  /* Clear CH(n)F and CH(n+1)F flags and Set DECAP bit */
367  FTM_Type * ftmBase = g_ftmBase[instance];
368  uint8_t chnlPairNum = (uint8_t)(channel >> 1U);
369 
370  /* Get channel mode */
371  if (FTM_FEATURE_INPUT_CAPTURE_SINGLE_SHOT == FTM_DRV_GetChnMode(ftmBase, channel))
372  {
373  if (FTM_DRV_GetDualEdgeCaptureBit(ftmBase, chnlPairNum))
374  {
375  /* Clear event flags for channel n and n + 1 */
376  FTM_DRV_ClearChnEventFlag(ftmBase, (uint8_t)(channel + 1U));
377  FTM_DRV_ClearChnEventFlag(ftmBase, channel);
378  /* Set DECAP bit to start measurement */
379  FTM_DRV_SetDualChnDecapCmd(ftmBase, chnlPairNum, true);
380  }
381  }
382  else
383  {
384  /* Nothing to do */
385  }
386 
387  return STATUS_SUCCESS;
388 }
389 
390 /*FUNCTION**********************************************************************
391  *
392  * Function Name : FTM_IC_DRV_SetChannelMode
393  * Description : This function is used to change the channel mode at run time or
394  * when stopping channel. The channel mode is selected in the ftm_ic_op_mode_t
395  * enumeration type.
396  *
397  * Implements : FTM_IC_DRV_SetChannelMode_Activity
398  *END**************************************************************************/
400  uint8_t channel,
401  ftm_ic_op_mode_t inputMode,
402  bool enableContinuousCapture)
403 {
404  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
406 
407  FTM_Type * ftmBase = g_ftmBase[instance];
408  uint8_t level = 1U;
409  uint8_t nextChnlevel = 1U;
410  uint8_t channelPair = (uint8_t)(channel >> 1U);
411 
412  if (inputMode == FTM_DISABLE_OPERATION)
413  {
414  /* Set the edge level to disable operation on the channel input */
415  FTM_DRV_SetChnEdgeLevel(ftmBase, channel, 0x0U);
416  }
417  else if ((inputMode == FTM_TIMESTAMP_RISING_EDGE) ||
418  (inputMode == FTM_TIMESTAMP_FALLING_EDGE) ||
419  (inputMode == FTM_TIMESTAMP_BOTH_EDGES))
420  {
421  /* Disable the dual edge mode */
422  FTM_DRV_SetDualEdgeCaptureCmd(ftmBase, channelPair, false);
423  /* Set input capture mode */
424  FTM_DRV_SetChnMSnBAMode(ftmBase, channel, 0U);
425  /* Set the event which will generate the interrupt */
426  FTM_DRV_SetChnEdgeLevel(ftmBase, channel, (uint8_t)inputMode);
427  /* Enable interrupt request for the current channel */
428  FTM_DRV_EnableChnInt(ftmBase, channel);
429  }
430  else
431  {
432  /* Check the channel is even number */
433  DEV_ASSERT((channel % 2U) == 0U);
434  /* Enable the dual edge mode */
435  FTM_DRV_SetDualEdgeCaptureCmd(ftmBase, channelPair, true);
436  /* Enable dual edge input capture */
437  FTM_DRV_SetDualChnDecapCmd(ftmBase, channelPair, true);
438  /* If continuous mode is set*/
439  if (true == enableContinuousCapture)
440  {
441  /* Set MSnA and MSnB bit*/
442  FTM_DRV_SetChnMSnBAMode(ftmBase, channel, 3U);
443  }
444  else
445  {
446  /* Clear MSnA and Set MSnB bit*/
447  FTM_DRV_SetChnMSnBAMode(ftmBase, channel, 2U);
448  }
449 
450  /* Check If want to measure a pulse width or period of the signal */
451  if ((FTM_MEASURE_PULSE_HIGH == inputMode) || (FTM_MEASURE_RISING_EDGE_PERIOD == inputMode))
452  {
453  if (FTM_MEASURE_PULSE_HIGH == inputMode)
454  {
455  /* Measure time between rising and falling edge - positive duty */
456  nextChnlevel = 2U;
457  }
458  }
459  else
460  {
461  level = 2U;
462  if (FTM_MEASURE_FALLING_EDGE_PERIOD == inputMode)
463  {
464  /* If channel (n) is configured to capture rising edges (ELS(n)B:ELS(n)A = 1:0) than
465  * channel (n+1) is setup to capture also raising edges (ELS(n+1)B:ELS(n+1)A = 1:0) */
466  nextChnlevel = 2U;
467  }
468  }
469 
470  FTM_DRV_SetChnEdgeLevel(ftmBase, channel, level);
471  FTM_DRV_SetChnEdgeLevel(ftmBase, (uint8_t)(channel + 1U), nextChnlevel);
472 
473  /* Disable interrupt request for the current channel */
474  FTM_DRV_DisableChnInt(ftmBase, channel);
475  FTM_DRV_EnableChnInt(ftmBase, (uint8_t)(channel + 1U));
476  }
477 
478  return STATUS_SUCCESS;
479 }
480 
481 #if (FEATURE_FTM_HAS_NUM_IRQS_CHANS == 1U)
482 /* Implementation of FTM0_Ch0_Ch7_IRQHandler master handler named in startup code. */
483 void FTM0_Ch0_7_IRQHandler(void)
484 {
485  uint32_t statusFlag = FTM_DRV_GetStatusFlags(0U);
486  uint32_t enabledInt = FTM_DRV_GetEnabledInterrupts(0U);
487  uint32_t index = 0U;
488  uint32_t shift = 0U;
489  bool isIntOnChn = false;
490  bool isIntOnNextChn = false;
491 
492  for (index = 0U; index < (uint32_t)(FEATURE_FTM_CHANNEL_COUNT / 2U); index++)
493  {
494  shift = index << 1U;
495  isIntOnChn = (((statusFlag & enabledInt) & (0x1UL << shift)) != 0U) ? true : false;
496  shift++;
497  isIntOnNextChn = (((statusFlag & enabledInt) & (0x1UL << shift)) != 0U) ? true : false;
498 
499  if ((isIntOnChn == true) || (isIntOnNextChn == true))
500  {
501  FTM_DRV_IrqHandler(0U, (uint8_t)index);
502  }
503  }
504 }
505 
506 /* Implementation of FTM1_Ch0_Ch7_IRQHandler master handler named in startup code. */
507 void FTM1_Ch0_7_IRQHandler(void)
508 {
509  uint32_t statusFlag = FTM_DRV_GetStatusFlags(1U);
510  uint32_t enabledInt = FTM_DRV_GetEnabledInterrupts(1U);
511  uint32_t index = 0U;
512  uint32_t shift = 0U;
513  bool isIntOnChn = false;
514  bool isIntOnNextChn = false;
515 
516  for (index = 0U; index < (uint32_t)(FEATURE_FTM_CHANNEL_COUNT / 2U); index++)
517  {
518  shift = index << 1U;
519  isIntOnChn = (((statusFlag & enabledInt) & (0x1UL << shift)) != 0U) ? true : false;
520  shift++;
521  isIntOnNextChn = (((statusFlag & enabledInt) & (0x1UL << shift)) != 0U) ? true : false;
522 
523  if ((isIntOnChn == true) || (isIntOnNextChn == true))
524  {
525  FTM_DRV_IrqHandler(1U, (uint8_t)index);
526  }
527  }
528 }
529 #endif
530 
531 #if (FEATURE_FTM_HAS_NUM_IRQS_CHANS == 4U)
532 /* Implementation of FTM0_Ch0_Ch1_IRQHandler master handler named in startup code. */
533 void FTM0_Ch0_Ch1_IRQHandler(void)
534 {
535  FTM_DRV_IrqHandler(0U, 0U);
536 }
537 
538 /* Implementation of FTM0_Ch2_Ch3_IRQHandler master handler named in startup code. */
539 void FTM0_Ch2_Ch3_IRQHandler(void)
540 {
541  FTM_DRV_IrqHandler(0U, 1U);
542 }
543 
544 /* Implementation of FTM0_Ch4_Ch5_IRQHandler master handler named in startup code. */
545 void FTM0_Ch4_Ch5_IRQHandler(void)
546 {
547  FTM_DRV_IrqHandler(0U, 2U);
548 }
549 
550 /* Implementation of FTM0_Ch6_Ch7_IRQHandler master handler named in startup code. */
551 void FTM0_Ch6_Ch7_IRQHandler(void)
552 {
553  FTM_DRV_IrqHandler(0U, 3U);
554 }
555 
556 /* Implementation of FTM1_Ch0_Ch1_IRQHandler master handler named in startup code. */
557 void FTM1_Ch0_Ch1_IRQHandler(void)
558 {
559  FTM_DRV_IrqHandler(1U, 0U);
560 }
561 
562 /* Implementation of FTM1_Ch2_Ch3_IRQHandler master handler named in startup code. */
563 void FTM1_Ch2_Ch3_IRQHandler(void)
564 {
565  FTM_DRV_IrqHandler(1U, 1U);
566 }
567 
568 /* Implementation of FTM1_Ch4_Ch5_IRQHandler master handler named in startup code. */
569 void FTM1_Ch4_Ch5_IRQHandler(void)
570 {
571  FTM_DRV_IrqHandler(1U, 2U);
572 }
573 
574 /* Implementation of FTM1_Ch6_Ch7_IRQHandler master handler named in startup code. */
575 void FTM1_Ch6_Ch7_IRQHandler(void)
576 {
577  FTM_DRV_IrqHandler(1U, 3U);
578 }
579 
580 #if (FTM_INSTANCE_COUNT > 2U)
581 /* Implementation of FTM2_Ch0_Ch1_IRQHandler master handler named in startup code. */
582 void FTM2_Ch0_Ch1_IRQHandler(void)
583 {
584  FTM_DRV_IrqHandler(2U, 0U);
585 }
586 
587 /* Implementation of FTM2_Ch2_Ch3_IRQHandler master handler named in startup code. */
588 void FTM2_Ch2_Ch3_IRQHandler(void)
589 {
590  FTM_DRV_IrqHandler(2U, 1U);
591 }
592 
593 /* Implementation of FTM2_Ch4_Ch5_IRQHandler master handler named in startup code. */
594 void FTM2_Ch4_Ch5_IRQHandler(void)
595 {
596  FTM_DRV_IrqHandler(2U, 2U);
597 }
598 
599 /* Implementation of FTM2_Ch6_Ch7_IRQHandler master handler named in startup code. */
600 void FTM2_Ch6_Ch7_IRQHandler(void)
601 {
602  FTM_DRV_IrqHandler(2U, 3U);
603 }
604 
605 /* Implementation of FTM3_Ch0_Ch1_IRQHandler master handler named in startup code. */
606 void FTM3_Ch0_Ch1_IRQHandler(void)
607 {
608  FTM_DRV_IrqHandler(3U, 0U);
609 }
610 
611 /* Implementation of FTM3_Ch2_Ch3_IRQHandler master handler named in startup code. */
612 void FTM3_Ch2_Ch3_IRQHandler(void)
613 {
614  FTM_DRV_IrqHandler(3U, 1U);
615 }
616 
617 /* Implementation of FTM3_Ch4_Ch5_IRQHandler master handler named in startup code. */
618 void FTM3_Ch4_Ch5_IRQHandler(void)
619 {
620  FTM_DRV_IrqHandler(3U, 2U);
621 }
622 
623 /* Implementation of FTM3_Ch6_Ch7_IRQHandler master handler named in startup code. */
624 void FTM3_Ch6_Ch7_IRQHandler(void)
625 {
626  FTM_DRV_IrqHandler(3U, 3U);
627 }
628 #endif
629 
630 #if (FTM_INSTANCE_COUNT > 4U)
631 /* Implementation of FTM4_Ch0_Ch1_IRQHandler master handler named in startup code. */
632 void FTM4_Ch0_Ch1_IRQHandler(void)
633 {
634  FTM_DRV_IrqHandler(4U, 0U);
635 }
636 
637 /* Implementation of FTM4_Ch2_Ch3_IRQHandler master handler named in startup code. */
638 void FTM4_Ch2_Ch3_IRQHandler(void)
639 {
640  FTM_DRV_IrqHandler(4U, 1U);
641 }
642 
643 /* Implementation of FTM4_Ch4_Ch5_IRQHandler master handler named in startup code. */
644 void FTM4_Ch4_Ch5_IRQHandler(void)
645 {
646  FTM_DRV_IrqHandler(4U, 2U);
647 }
648 
649 /* Implementation of FTM4_Ch6_Ch7_IRQHandler master handler named in startup code. */
650 void FTM4_Ch6_Ch7_IRQHandler(void)
651 {
652  FTM_DRV_IrqHandler(4U, 3U);
653 }
654 
655 /* Implementation of FTM5_Ch0_Ch1_IRQHandler master handler named in startup code. */
656 void FTM5_Ch0_Ch1_IRQHandler(void)
657 {
658  FTM_DRV_IrqHandler(5U, 0U);
659 }
660 
661 /* Implementation of FTM5_Ch2_Ch3_IRQHandler master handler named in startup code. */
662 void FTM5_Ch2_Ch3_IRQHandler(void)
663 {
664  FTM_DRV_IrqHandler(5U, 1U);
665 }
666 
667 /* Implementation of FTM5_Ch4_Ch5_IRQHandler master handler named in startup code. */
668 void FTM5_Ch4_Ch5_IRQHandler(void)
669 {
670  FTM_DRV_IrqHandler(5U, 2U);
671 }
672 
673 /* Implementation of FTM5_Ch6_Ch7_IRQHandler master handler named in startup code. */
674 void FTM5_Ch6_Ch7_IRQHandler(void)
675 {
676  FTM_DRV_IrqHandler(5U, 3U);
677 }
678 #endif
679 
680 #if (FTM_INSTANCE_COUNT > 6U)
681 /* Implementation of FTM6_Ch0_Ch1_IRQHandler master handler named in startup code. */
682 void FTM6_Ch0_Ch1_IRQHandler(void)
683 {
684  FTM_DRV_IrqHandler(6U, 0U);
685 }
686 
687 /* Implementation of FTM6_Ch2_Ch3_IRQHandler master handler named in startup code. */
688 void FTM6_Ch2_Ch3_IRQHandler(void)
689 {
690  FTM_DRV_IrqHandler(6U, 1U);
691 }
692 
693 /* Implementation of FTM6_Ch4_Ch5_IRQHandler master handler named in startup code. */
694 void FTM6_Ch4_Ch5_IRQHandler(void)
695 {
696  FTM_DRV_IrqHandler(6U, 2U);
697 }
698 
699 /* Implementation of FTM6_Ch6_Ch7_IRQHandler master handler named in startup code. */
700 void FTM6_Ch6_Ch7_IRQHandler(void)
701 {
702  FTM_DRV_IrqHandler(6U, 3U);
703 }
704 
705 /* Implementation of FTM7_Ch0_Ch1_IRQHandler master handler named in startup code. */
706 void FTM7_Ch0_Ch1_IRQHandler(void)
707 {
708  FTM_DRV_IrqHandler(7U, 0U);
709 }
710 
711 /* Implementation of FTM7_Ch2_Ch3_IRQHandler master handler named in startup code. */
712 void FTM7_Ch2_Ch3_IRQHandler(void)
713 {
714  FTM_DRV_IrqHandler(7U, 1U);
715 }
716 
717 /* Implementation of FTM7_Ch4_Ch5_IRQHandler master handler named in startup code. */
718 void FTM7_Ch4_Ch5_IRQHandler(void)
719 {
720  FTM_DRV_IrqHandler(7U, 2U);
721 }
722 
723 /* Implementation of FTM7_Ch6_Ch7_IRQHandler master handler named in startup code. */
724 void FTM7_Ch6_Ch7_IRQHandler(void)
725 {
726  FTM_DRV_IrqHandler(7U, 3U);
727 }
728 #endif
729 #endif
730 
731 static void FTM_DRV_IrqHandler(uint32_t instance,
732  uint8_t channelPair)
733 {
734  const ftm_state_t * state = ftmStatePtr[instance];
735  switch (state->ftmMode)
736  {
738  FTM_DRV_InputCaptureHandler(instance, channelPair);
739  break;
740  default:
741  /* Nothing to do */
742  break;
743  }
744 }
745 
746 static void FTM_DRV_InputCaptureHandler(uint32_t instance,
747  uint8_t channelPair)
748 {
749  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
750  DEV_ASSERT(channelPair < (FEATURE_FTM_CHANNEL_COUNT >> 1U));
751 
752  ftm_state_t * state = ftmStatePtr[instance];
753  FTM_Type * ftmBase = g_ftmBase[instance];
754  uint8_t channel = (uint8_t)(channelPair << 1U);
755 
756  /* Verify the mode for current pair of channels */
757  if (FTM_DRV_GetDualEdgeCaptureBit(ftmBase, channelPair))
758  {
759  /* Dual edge input capture case */
760  uint16_t first_event_time = FTM_DRV_GetChnCountVal(ftmBase, channel);
761  uint16_t second_event_time = FTM_DRV_GetChnCountVal(ftmBase, (uint8_t)(channel + 1U));
762  if (second_event_time < first_event_time)
763  {
764  /* Measurement when overflow occurred */
765  state->measurementResults[channel] = (uint16_t)(second_event_time + (FTM_DRV_GetMod(ftmBase) - first_event_time));
766  }
767  else
768  {
769  /* Measurement when overflow doesn't occurred */
770  state->measurementResults[channel] = (uint16_t)(second_event_time - first_event_time);
771  }
772 
773  /* Clear flags for channels n and n+1 */
774  FTM_DRV_ClearChnEventFlag(ftmBase, channel);
775  FTM_DRV_ClearChnEventFlag(ftmBase, (uint8_t)(channel + 1U));
776  }
777  else
778  {
779  /* To get the channel interrupt source the both channels flag must be checked */
780  if (false == FTM_DRV_HasChnEventOccurred(ftmBase, channel))
781  {
782  channel++;
783  }
784 
785  /* Get the time stamp of the event */
786  state->measurementResults[channel] = FTM_DRV_GetChnCountVal(ftmBase, channel);
787  /* Clear the flag for C(n+1) channel */
788  FTM_DRV_ClearChnEventFlag(ftmBase, channel);
789  }
790 
791  /* If the callback is defined to use it */
792  if (((state->channelsCallbacks[channel]) != NULL) && (state->enableNotification[channel] == true))
793  {
795  }
796 }
797 
798 /*******************************************************************************
799 * EOF
800 ******************************************************************************/
ic_callback_t channelsCallbacks[(8U)]
Definition: ftm_common.h:399
static uint16_t FTM_DRV_GetChnCountVal(const FTM_Type *ftmBase, uint8_t channel)
Gets the FTM peripheral timer channel counter value.
Definition: ftm_common.h:746
FlexTimer state structure of the driver.
Definition: ftm_common.h:391
ftm_edge_alignment_mode_t edgeAlignement
ftm_state_t * ftmStatePtr[(2u)]
Pointer to runtime state structure.
Definition: ftm_common.c:84
#define FEATURE_FTM_CHANNEL_COUNT
#define CHAN4_IDX
Channel number for CHAN5.
Definition: ftm_common.h:205
FlexTimer driver input capture parameters.
status_t FTM_DRV_StartNewSignalMeasurement(uint32_t instance, uint8_t channel)
Starts new single-shot signal measurement of the given channel.
status_t FTM_IC_DRV_SetChannelMode(uint32_t instance, uint8_t channel, ftm_ic_op_mode_t inputMode, bool enableContinuousCapture)
Set mode operation for channel in the input captue mode.
ic_callback_t channelsCallbacks
static void FTM_DRV_InputCaptureHandler(uint32_t instance, uint8_t channelPair)
uint16_t measurementResults[(8U)]
Definition: ftm_common.h:397
ftm_ic_op_mode_t
The measurement type for input capture mode Implements : ftm_ic_op_mode_t_Class.
Definition: ftm_ic_driver.h:82
bool enableNotification[(8U)]
Definition: ftm_common.h:400
static void FTM_DRV_IrqHandler(uint32_t instance, uint8_t channelPair)
const IRQn_Type g_ftmIrqId[(2u)][(8U)]
Interrupt vectors for the FTM peripheral.
Definition: ftm_common.c:74
#define DEV_ASSERT(x)
Definition: devassert.h:77
#define FTM_FEATURE_INPUT_CAPTURE_SINGLE_SHOT
ftm_clock_source_t ftmClockSource
Definition: ftm_common.h:393
status_t FTM_DRV_InitInputCapture(uint32_t instance, const ftm_input_param_t *param)
Configures Channel Input Capture for either getting time-stamps on edge detection or on signal measur...
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
uint16_t nMaxCountValue
#define FTM_INSTANCE_COUNT
Definition: S32K118.h:3868
status_t FTM_DRV_DeinitInputCapture(uint32_t instance, const ftm_input_param_t *param)
Disables input capture mode and clears FTM timer configuration.
ftm_signal_measurement_mode_t measurementType
ftm_signal_measurement_mode_t
FlexTimer input capture measurement type for dual edge input capture.
Definition: ftm_ic_driver.h:56
ftm_config_mode_t ftmMode
Definition: ftm_common.h:394
void * channelsCallbacksParams[(8U)]
Definition: ftm_common.h:398
uint32_t FTM_DRV_GetEnabledInterrupts(uint32_t instance)
This function will get the enabled FTM interrupts.
Definition: ftm_common.c:585
void INT_SYS_EnableIRQ(IRQn_Type irqNumber)
Enables an interrupt for a given IRQ number.
ftm_input_ch_param_t * inputChConfig
static uint16_t FTM_DRV_GetMod(const FTM_Type *ftmBase)
Returns the FTM peripheral counter modulo value.
Definition: ftm_common.h:516
ftm_input_op_mode_t inputMode
uint32_t FTM_DRV_GetStatusFlags(uint32_t instance)
This function will get the FTM status flags.
Definition: ftm_common.c:631
uint16_t FTM_DRV_GetInputCaptureMeasurement(uint32_t instance, uint8_t channel)
This function is used to calculate the measurement and/or time stamps values which are read from the ...
FTM_Type *const g_ftmBase[(2u)]
Table of base addresses for FTM instances.
Definition: ftm_common.c:71