ftm_qd_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_qd_driver.h"
36 #include "ftm_hw_access.h"
37 
38 /*******************************************************************************
39  * Code
40  ******************************************************************************/
41 
42 /*FUNCTION**********************************************************************
43  *
44  * Function Name : FTM_DRV_QuadDecodeStart
45  * Description : Configures the parameters needed and activates quadrature
46  * decode mode.
47  *
48  * Implements : FTM_DRV_QuadDecodeStart_Activity
49  *END**************************************************************************/
51  const ftm_quad_decode_config_t * config)
52 {
53  DEV_ASSERT((instance == 1U) || (instance == 2U));
54  DEV_ASSERT(config != NULL);
55  FTM_Type * ftmBase = g_ftmBase[instance];
56  ftm_state_t * state = ftmStatePtr[instance];
57  status_t retStatus = STATUS_SUCCESS;
58 
59  if ((NULL != state) && (FTM_MODE_NOT_INITIALIZED == state->ftmMode))
60  {
61  /* Disable Quadrature Decoder */
62  FTM_DRV_SetQuadDecoderCmd(ftmBase, false);
63  FTM_DRV_SetClockSource(ftmBase, FTM_CLOCK_SOURCE_NONE);
64  /* Configure Quadrature Decoder */
65  /* Set decoder mode Speed and direction or Phase A and Phase B encoding */
66  FTM_DRV_SetQuadMode(ftmBase, config->mode);
67  /* Set filter state for Phase A (enable/disable) */
68  FTM_DRV_SetQuadPhaseAFilterCmd(ftmBase, config->phaseAConfig.phaseInputFilter);
69  /* Set Phase A filter value if phase filter is enabled */
70  if (config->phaseAConfig.phaseInputFilter)
71  {
72  FTM_DRV_SetChnInputCaptureFilter(ftmBase, CHAN0_IDX, config->phaseAConfig.phaseFilterVal);
73  }
74 
75  /* Set filter state for Phase B (enable/disable) */
76  FTM_DRV_SetQuadPhaseBFilterCmd(ftmBase, config->phaseBConfig.phaseInputFilter);
77  /* Set Phase B filter value if phase filter is enabled */
78  if (config->phaseBConfig.phaseInputFilter)
79  {
80  FTM_DRV_SetChnInputCaptureFilter(ftmBase, CHAN1_IDX, config->phaseBConfig.phaseFilterVal);
81  }
82 
83  /* Set polarity for Phase A and Phase B */
84  FTM_DRV_SetQuadPhaseAPolarity(ftmBase, config->phaseAConfig.phasePolarity);
85  FTM_DRV_SetQuadPhaseBPolarity(ftmBase, config->phaseBConfig.phasePolarity);
86  /* Configure counter (initial value and maximum value) */
87  FTM_DRV_SetCounterInitVal(ftmBase, config->initialVal);
88  FTM_DRV_SetMod(ftmBase, config->maxVal);
89  FTM_DRV_SetCounter(ftmBase, config->initialVal);
90  /* Enable Quadrature Decoder */
91  FTM_DRV_SetQuadDecoderCmd(ftmBase, true);
93  }
94  else
95  {
96  retStatus = STATUS_ERROR;
97  }
98 
99  return retStatus;
100 }
101 
102 /*FUNCTION**********************************************************************
103  *
104  * Function Name : FTM_DRV_QuadDecodeStop
105  * Description : De-activates quadrature decoder mode.
106  *
107  * Implements : FTM_DRV_QuadDecodeStop_Activity
108  *END**************************************************************************/
110 {
111  DEV_ASSERT((instance == 1U) || (instance == 2U));
112  FTM_Type * ftmBase = g_ftmBase[instance];
113  ftm_state_t * state = ftmStatePtr[instance];
114 
115  /* Disable Quadrature decoder */
116  FTM_DRV_SetQuadDecoderCmd(ftmBase, false);
118 
119  return STATUS_SUCCESS;
120 }
121 
122 /*FUNCTION**********************************************************************
123  *
124  * Function Name : FTM_DRV_QuadGetState
125  * Description : Return the current quadrature decoder state
126  * (counter value, overflow flag and overflow direction)
127  *
128  * Implements : FTM_DRV_QuadGetState_Activity
129  *END**************************************************************************/
131 {
132  DEV_ASSERT((instance == 1U) || (instance == 2U));
133  FTM_Type const * ftmBase = g_ftmBase[instance];
135 
136  state.counterDirection = FTM_DRV_GetQuadDir(ftmBase);
137  state.overflowDirection = FTM_DRV_GetQuadTimerOverflowDir(ftmBase);
138  state.overflowFlag = FTM_DRV_HasTimerOverflowed(ftmBase);
139  state.counter = FTM_DRV_GetCounter(ftmBase);
140 
141  return state;
142 }
143 
144 /*******************************************************************************
145 * EOF
146 ******************************************************************************/
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
status_t FTM_DRV_QuadDecodeStart(uint32_t instance, const ftm_quad_decode_config_t *config)
Configures the quadrature mode and starts measurement.
Definition: ftm_qd_driver.c:50
#define DEV_ASSERT(x)
Definition: devassert.h:77
ftm_phase_params_t phaseAConfig
Definition: ftm_qd_driver.h:62
ftm_phase_params_t phaseBConfig
Definition: ftm_qd_driver.h:63
ftm_quad_decode_mode_t mode
Definition: ftm_qd_driver.h:59
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
#define CHAN0_IDX
Channel number for CHAN1.
Definition: ftm_common.h:197
FTM quadrature configure structure.
Definition: ftm_qd_driver.h:57
ftm_quad_decoder_state_t FTM_DRV_QuadGetState(uint32_t instance)
Return the current quadrature decoder state (counter value, overflow flag and overflow direction) ...
status_t FTM_DRV_QuadDecodeStop(uint32_t instance)
De-activates the quadrature decode mode.
ftm_config_mode_t ftmMode
Definition: ftm_common.h:394
#define CHAN1_IDX
Channel number for CHAN2.
Definition: ftm_common.h:199
ftm_quad_phase_polarity_t phasePolarity
Definition: ftm_qd_driver.h:49
FTM quadrature state(counter value and flags)
Definition: ftm_qd_driver.h:71
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