S32 SDK
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  * 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_qd_driver.h"
36 
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 == FTM1_IDX) || (instance == FTM2_IDX));
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) */
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 */
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 == FTM1_IDX) || (instance == FTM2_IDX));
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 == FTM1_IDX) || (instance == FTM2_IDX));
133  FTM_Type const * ftmBase = g_ftmBase[instance];
135 
136  state.counterDirection = FTM_DRV_GetQuadDir(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 ******************************************************************************/
static bool FTM_DRV_GetQuadDir(const FTM_Type *ftmBase)
Gets the FTM counter direction in quadrature mode.
Definition: ftm_common.h:1123
FlexTimer state structure of the driver.
Definition: ftm_common.h:121
static void FTM_DRV_SetQuadPhaseBFilterCmd(FTM_Type *const ftmBase, bool enable)
Enables or disables the phase B input filter.
Definition: ftm_common.h:1051
FTM_Type *const g_ftmBase[FTM_INSTANCE_COUNT]
Table of base addresses for FTM instances.
Definition: ftm_common.c:70
ftm_state_t * ftmStatePtr[FTM_INSTANCE_COUNT]
Pointer to runtime state structure.
Definition: ftm_common.c:83
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
static bool FTM_DRV_HasTimerOverflowed(const FTM_Type *ftmBase)
Returns the FTM peripheral timer overflow interrupt flag.
Definition: ftm_common.h:281
#define DEV_ASSERT(x)
Definition: devassert.h:77
ftm_phase_params_t phaseAConfig
Definition: ftm_qd_driver.h:65
ftm_phase_params_t phaseBConfig
Definition: ftm_qd_driver.h:66
static void FTM_DRV_SetQuadPhaseAPolarity(FTM_Type *const ftmBase, ftm_quad_phase_polarity_t mode)
Selects polarity for the quadrature decode phase A input.
Definition: ftm_common.h:1074
static bool FTM_DRV_GetQuadTimerOverflowDir(const FTM_Type *ftmBase)
Gets the Timer overflow direction in quadrature mode.
Definition: ftm_common.h:1139
ftm_quad_decode_mode_t mode
Definition: ftm_qd_driver.h:62
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
FTM quadrature configure structure.
Definition: ftm_qd_driver.h:60
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.
static void FTM_DRV_SetQuadPhaseBPolarity(FTM_Type *const ftmBase, ftm_quad_phase_polarity_t mode)
Selects polarity for the quadrature decode phase B input.
Definition: ftm_common.h:1090
ftm_config_mode_t ftmMode
Definition: ftm_common.h:124
static void FTM_DRV_SetQuadMode(FTM_Type *const ftmBase, ftm_quad_decode_mode_t quadMode)
Sets the encoding mode used in quadrature decoding mode.
Definition: ftm_common.h:1106
ftm_quad_phase_polarity_t phasePolarity
Definition: ftm_qd_driver.h:52
FTM quadrature state(counter value and flags)
Definition: ftm_qd_driver.h:74
static uint16_t FTM_DRV_GetCounter(const FTM_Type *ftmBase)
Returns the FTM peripheral current counter value.
Definition: ftm_common.h:358