S32 SDK
ftm_mc_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  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
14  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
15  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
35 #include "ftm_mc_driver.h"
36 
37 
38 /*******************************************************************************
39  * Code
40  ******************************************************************************/
41 
42 /*FUNCTION**********************************************************************
43  *
44  * Function Name : FTM_DRV_InitCounter
45  * Description : Initializes the FTM counter. This function provides access to the
46  * FTM counter settings. The counter can be run in Up counting or Up-down counting modes.
47  * To run the counter in Free running mode, choose Up counting option and provide
48  * 0x0 for the countStartVal and 0xFFFF for countFinalVal. Please call this
49  * function only when FTM is used as timer/counter.
50  *
51  * Implements : FTM_DRV_InitCounter_Activity
52  *END**************************************************************************/
53 status_t FTM_DRV_InitCounter(uint32_t instance,
54  const ftm_timer_param_t * timer)
55 {
56  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
57  DEV_ASSERT(timer != NULL);
58  DEV_ASSERT((FTM_MODE_UP_TIMER == timer->mode) || (FTM_MODE_UP_DOWN_TIMER == timer->mode));
59  FTM_Type * ftmBase = g_ftmBase[instance];
60  ftm_state_t * state = ftmStatePtr[instance];
61  status_t retStatus = STATUS_SUCCESS;
62  uint8_t channel = 0U;
63 
64  if ((NULL != state) && (FTM_MODE_NOT_INITIALIZED == state->ftmMode))
65  {
66  /* Disable counter clock */
67  FTM_DRV_SetClockSource(ftmBase, FTM_CLOCK_SOURCE_NONE);
68  /* Clear the overflow flag */
69  FTM_DRV_ClearTimerOverflow(ftmBase);
70  /* Set counter initial and maximum values */
71  FTM_DRV_SetCounterInitVal(ftmBase, timer->initialValue);
72  FTM_DRV_SetMod(ftmBase, timer->finalValue);
73  /* Disable the quadrature decoder mode */
74  FTM_DRV_SetQuadDecoderCmd(ftmBase, false);
75  /* Use FTM as counter, disable all the channels */
76  for (channel = 0U; channel < FEATURE_FTM_CHANNEL_COUNT; channel++)
77  {
78  FTM_DRV_SetChnEdgeLevel(ftmBase, channel, 0U);
79  }
80 
81  /* Check the FTM counter modes */
82  if (FTM_MODE_UP_TIMER == timer->mode)
83  {
84  FTM_DRV_SetCpwms(ftmBase, false);
85  }
86  else if (FTM_MODE_UP_DOWN_TIMER == timer->mode)
87  {
88  FTM_DRV_SetCpwms(ftmBase, true);
89  }
90  else
91  {
92  /* Do nothing */
93  }
94 
95  state->ftmMode = timer->mode;
96  }
97  else
98  {
99  retStatus = STATUS_ERROR;
100  }
101 
102  return retStatus;
103 }
104 
105 /*FUNCTION**********************************************************************
106  *
107  * Function Name : FTM_DRV_CounterStart
108  * Description : Starts the FTM counter.
109  *
110  * Implements : FTM_DRV_CounterStart_Activity
111  *END**************************************************************************/
112 status_t FTM_DRV_CounterStart(uint32_t instance)
113 {
114  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
115  FTM_Type * ftmBase = g_ftmBase[instance];
116 
117  const ftm_state_t * state = ftmStatePtr[instance];
118  /* Check the clock source is available for FTM counter */
119  DEV_ASSERT(state->ftmSourceClockFrequency > 0U);
120  /* Enable counter clock */
121  FTM_DRV_SetClockSource(ftmBase, state->ftmClockSource);
122 
123  return STATUS_SUCCESS;
124 }
125 
126 /*FUNCTION**********************************************************************
127  *
128  * Function Name : FTM_DRV_CounterStop
129  * Description : Stops the FTM counter.
130  *
131  * Implements : FTM_DRV_CounterStop_Activity
132  *END**************************************************************************/
133 status_t FTM_DRV_CounterStop(uint32_t instance)
134 {
135  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
136  FTM_Type * ftmBase = g_ftmBase[instance];
137  ftm_state_t * state = ftmStatePtr[instance];
138 
139  /* Stop the FTM counter */
140  FTM_DRV_SetClockSource(ftmBase, FTM_CLOCK_SOURCE_NONE);
142 
143  return STATUS_SUCCESS;
144 }
145 
146 /*FUNCTION**********************************************************************
147  *
148  * Function Name : FTM_DRV_CounterRead
149  * Description : Reads back the current value of the FTM counter.
150  *
151  * Implements : FTM_DRV_CounterRead_Activity
152  *END**************************************************************************/
153 uint32_t FTM_DRV_CounterRead(uint32_t instance)
154 {
155  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
156  FTM_Type const * ftmBase = g_ftmBase[instance];
157 
158  return FTM_DRV_GetCounter(ftmBase);
159 }
160 
161 /*******************************************************************************
162 * EOF
163 ******************************************************************************/
status_t FTM_DRV_CounterStop(uint32_t instance)
Stops the FTM counter.
FlexTimer state structure of the driver.
Definition: ftm_common.h:121
FTM_Type *const g_ftmBase[FTM_INSTANCE_COUNT]
Table of base addresses for FTM instances.
Definition: ftm_common.c:70
uint16_t initialValue
Definition: ftm_mc_driver.h:50
ftm_state_t * ftmStatePtr[FTM_INSTANCE_COUNT]
Pointer to runtime state structure.
Definition: ftm_common.c:83
ftm_config_mode_t mode
Definition: ftm_mc_driver.h:49
FlexTimer driver timer mode configuration structure.
Definition: ftm_mc_driver.h:47
#define FEATURE_FTM_CHANNEL_COUNT
#define DEV_ASSERT(x)
Definition: devassert.h:77
ftm_clock_source_t ftmClockSource
Definition: ftm_common.h:123
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
#define FTM_INSTANCE_COUNT
Definition: S32K142.h:3939
status_t FTM_DRV_CounterStart(uint32_t instance)
Starts the FTM counter.
ftm_config_mode_t ftmMode
Definition: ftm_common.h:124
uint32_t FTM_DRV_CounterRead(uint32_t instance)
Reads back the current value of the FTM counter.
status_t FTM_DRV_InitCounter(uint32_t instance, const ftm_timer_param_t *timer)
Initialize the FTM counter.
Definition: ftm_mc_driver.c:53
uint32_t ftmSourceClockFrequency
Definition: ftm_common.h:126
static uint16_t FTM_DRV_GetCounter(const FTM_Type *ftmBase)
Returns the FTM peripheral current counter value.
Definition: ftm_common.h:358