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  * 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_mc_driver.h"
36 #include "ftm_hw_access.h"
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:391
ftm_state_t * ftmStatePtr[(2u)]
Pointer to runtime state structure.
Definition: ftm_common.c:84
#define FEATURE_FTM_CHANNEL_COUNT
uint16_t initialValue
Definition: ftm_mc_driver.h:47
ftm_config_mode_t mode
Definition: ftm_mc_driver.h:46
FlexTimer driver timer mode configuration structure.
Definition: ftm_mc_driver.h:44
#define DEV_ASSERT(x)
Definition: devassert.h:77
ftm_clock_source_t ftmClockSource
Definition: ftm_common.h:393
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: S32K118.h:3868
status_t FTM_DRV_CounterStart(uint32_t instance)
Starts the FTM counter.
ftm_config_mode_t ftmMode
Definition: ftm_common.h:394
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:396
static uint16_t FTM_DRV_GetCounter(const FTM_Type *ftmBase)
Returns the FTM peripheral current counter value.
Definition: ftm_common.h:502
FTM_Type *const g_ftmBase[(2u)]
Table of base addresses for FTM instances.
Definition: ftm_common.c:71