S32 SDK
ftm_oc_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  */
28 #include "ftm_oc_driver.h"
29 
30 
31 /*******************************************************************************
32  * Code
33  ******************************************************************************/
34 
35 /*FUNCTION**********************************************************************
36  *
37  * Function Name : FTM_DRV_InitOutputCompare
38  * Description : Configures the FTM to generate timed pulses
39  * When the FTM counter matches the value of compareVal argument (this is
40  * written into CnV register), the channel output is changed based on what is specified
41  * in the compareMode argument.
42  *
43  * Implements : FTM_DRV_InitOutputCompare_Activity
44  *END**************************************************************************/
46  const ftm_output_cmp_param_t * param)
47 {
48  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
49  DEV_ASSERT(param != NULL);
50  FTM_Type * ftmBase = g_ftmBase[instance];
51  uint8_t index = 0U;
52  uint8_t hwChannel = 0U;
53  uint8_t chnlPairNum = 0U;
54  ftm_state_t * state = ftmStatePtr[instance];
55  status_t retStatus = STATUS_SUCCESS;
56 
57  if ((NULL != state) && (FTM_MODE_NOT_INITIALIZED == state->ftmMode))
58  {
59  FTM_DRV_SetClockSource(ftmBase, FTM_CLOCK_SOURCE_NONE);
60  FTM_DRV_SetCpwms(ftmBase, false);
61  /* Clear the overflow flag */
62  FTM_DRV_ClearTimerOverflow(ftmBase);
63  FTM_DRV_SetCounterInitVal(ftmBase, 0U);
64  FTM_DRV_SetMod(ftmBase, param->maxCountValue);
65  FTM_DRV_SetCounter(ftmBase, 0U);
66  FTM_DRV_SetQuadDecoderCmd(ftmBase, false);
67  /* Use FTM as counter, disable all the channels */
68  for (index = 0U; index < param->nNumOutputChannels; index++)
69  {
70  hwChannel = param->outputChannelConfig[index].hwChannelId;
71  chnlPairNum = (uint8_t)(hwChannel >> 1U);
72  FTM_DRV_SetDualChnMofCombineCmd(ftmBase, chnlPairNum, false);
73  FTM_DRV_SetDualChnCombineCmd(ftmBase, chnlPairNum, false);
74  FTM_DRV_SetDualEdgeCaptureCmd(ftmBase, chnlPairNum, false);
75  /* Set Channel Output mode */
76  FTM_DRV_SetChnEdgeLevel(ftmBase, hwChannel, (uint8_t)(param->outputChannelConfig[index].chMode));
77  /* Enter counter mode for all configured channels */
78  FTM_DRV_SetChnMSnBAMode(ftmBase, hwChannel, 1U);
79  /* Write initial count value for all channels */
80  FTM_DRV_SetChnCountVal(ftmBase, hwChannel, param->outputChannelConfig[index].comparedValue);
81  /* Enable channel output */
82  FTM_DRV_EnablePwmChannelOutputs(ftmBase, hwChannel);
83  /* Enable the generation a trigger on chip module */
84  FTM_DRV_SetChnTriggerCmd(ftmBase, hwChannel, param->outputChannelConfig[index].enableExternalTrigger);
85  }
86 
87  /* Set software trigger */
88  FTM_DRV_SetSoftwareTriggerCmd(ftmBase, true);
89  state->ftmMode = param->mode;
90  /* Set clock source to start the counter */
91  FTM_DRV_SetClockSource(ftmBase, state->ftmClockSource);
92  }
93  else
94  {
95  retStatus = STATUS_ERROR;
96  }
97 
98  return retStatus;
99 }
100 
101 /*FUNCTION**********************************************************************
102  *
103  * Function Name : FTM_DRV_DeinitOutputCompare
104  * Description : Disables compare match output control and clears FTM timer configuration
105  *
106  * Implements : FTM_DRV_DeinitOutputCompare_Activity
107  *END**************************************************************************/
109  const ftm_output_cmp_param_t * param)
110 {
111  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
112  DEV_ASSERT(param != NULL);
113  FTM_Type * ftmBase = g_ftmBase[instance];
114  uint8_t index = 0U;
115  uint8_t hwChannel = 0U;
116  ftm_state_t * state = ftmStatePtr[instance];
117 
118  /* Stop the FTM counter */
119  FTM_DRV_SetClockSource(ftmBase, FTM_CLOCK_SOURCE_NONE);
120  /* Clear the overflow flag */
121  FTM_DRV_ClearTimerOverflow(ftmBase);
122  FTM_DRV_SetCounterInitVal(ftmBase, 0U);
123  for (index = 0U; index < param->nNumOutputChannels; index++)
124  {
125  hwChannel = param->outputChannelConfig[index].hwChannelId;
126  /* Disable Channel Output mode */
127  FTM_DRV_SetChnEdgeLevel(ftmBase, hwChannel, (uint8_t)0U);
128  /* Write initial count value for all channels to 0xFFFF */
129  FTM_DRV_SetChnCountVal(ftmBase, hwChannel, 0U);
130  /* Disable channel output */
131  FTM_DRV_DisablePwmChannelOutputs(ftmBase, hwChannel);
132  }
133 
134  /* Clear out the registers */
135  FTM_DRV_SetMod(ftmBase, 0U);
136  FTM_DRV_SetCounter(ftmBase, 0U);
138 
139  return STATUS_SUCCESS;
140 }
141 
142 /*FUNCTION**********************************************************************
143  *
144  * Function Name : FTM_DRV_UpdateOutputCompareChannel
145  * Description : Sets the next compare match value on the given channel starting
146  * from the current counter value.
147  *
148  * Implements : FTM_DRV_UpdateOutputCompareChannel_Activity
149  *END**************************************************************************/
151  uint8_t channel,
152  uint16_t nextComparematchValue,
154  bool softwareTrigger)
155 {
156  DEV_ASSERT(instance < FTM_INSTANCE_COUNT);
158  uint16_t counterValue = FTM_DRV_GetCounter(g_ftmBase[instance]);
159  uint16_t compareValue = 0U;
160  uint16_t maxCounterValue;
161  FTM_Type * ftmBase = g_ftmBase[instance];
162 
163  if (update == FTM_RELATIVE_VALUE)
164  {
165  maxCounterValue = FTM_DRV_GetMod(g_ftmBase[instance]);
166  /* Configure channel compare register */
167  if ((uint16_t)(counterValue + nextComparematchValue) > maxCounterValue)
168  {
169  compareValue = (uint16_t)(nextComparematchValue - (maxCounterValue - counterValue));
170  }
171  else
172  {
173  compareValue = (uint16_t)(counterValue + nextComparematchValue);
174  }
175  }
176  else
177  {
178  compareValue = nextComparematchValue;
179  }
180 
181  /* Set CnV value and use software trigger for sync */
182  FTM_DRV_SetChnCountVal(g_ftmBase[instance], channel, compareValue);
183  if (softwareTrigger)
184  {
185  FTM_DRV_SetSoftwareTriggerCmd(ftmBase, true);
186  }
187 
188  return STATUS_SUCCESS;
189 }
190 
191 /*******************************************************************************
192 * EOF
193 ******************************************************************************/
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
ftm_config_mode_t mode
Definition: ftm_oc_driver.h:88
ftm_state_t * ftmStatePtr[FTM_INSTANCE_COUNT]
Pointer to runtime state structure.
Definition: ftm_common.c:83
ftm_output_compare_mode_t chMode
Definition: ftm_oc_driver.h:74
#define FEATURE_FTM_CHANNEL_COUNT
#define DEV_ASSERT(x)
Definition: devassert.h:77
status_t FTM_DRV_InitOutputCompare(uint32_t instance, const ftm_output_cmp_param_t *param)
Configures the FTM to generate timed pulses(Output compare mode).
Definition: ftm_oc_driver.c:45
ftm_output_compare_update_t
FlexTimer input capture type of the next output compare value.
Definition: ftm_oc_driver.h:60
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
const ftm_output_cmp_ch_param_t * outputChannelConfig
Definition: ftm_oc_driver.h:90
ftm_config_mode_t ftmMode
Definition: ftm_common.h:124
FlexTimer driver PWM parameters.
Definition: ftm_oc_driver.h:85
static uint16_t FTM_DRV_GetMod(const FTM_Type *ftmBase)
Returns the FTM peripheral counter modulo value.
Definition: ftm_common.h:372
status_t FTM_DRV_UpdateOutputCompareChannel(uint32_t instance, uint8_t channel, uint16_t nextComparematchValue, ftm_output_compare_update_t update, bool softwareTrigger)
Sets the next compare match value based on the current counter value.
static uint16_t FTM_DRV_GetCounter(const FTM_Type *ftmBase)
Returns the FTM peripheral current counter value.
Definition: ftm_common.h:358
status_t FTM_DRV_DeinitOutputCompare(uint32_t instance, const ftm_output_cmp_param_t *param)
Disables compare match output control and clears FTM timer configuration.