S32 SDK
ewm_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 - 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  */
18 
39 #include <stddef.h>
40 #include "ewm_hw_access.h"
41 #include "interrupt_manager.h"
42 
43 /* @brief Local table used to store EWM base pointers */
44 static EWM_Type * const s_ewmBase[] = EWM_BASE_PTRS;
45 
46 /*******************************************************************************
47  * Code
48  ******************************************************************************/
49 
55 /*FUNCTION**********************************************************************
56  *
57  * Function Name : EWM_DRV_Init
58  * Description : This function initializes the EWM instance to a specified
59  * state
60  *
61  * Implements : EWM_DRV_Init_Activity
62  *END**************************************************************************/
63 status_t EWM_DRV_Init(uint32_t instance, const ewm_init_config_t * config)
64 {
65  DEV_ASSERT(instance < EWM_INSTANCE_COUNT);
66  DEV_ASSERT(config != NULL);
67 
68  /* Return status variable */
69  status_t statusCode = STATUS_SUCCESS;
70  /* Flag to store if the module is enabled */
71  bool isModuleEnabled;
72  uint8_t tempValue = 0U;
73  /* Base pointer */
74  EWM_Type * base = s_ewmBase[instance];
75 
76  /* Get the enablement status of the module */
77  isModuleEnabled = EWM_IsEnabled(base);
78  /* Check if the EWM instance is already enabled or if the windows values are not correct */
79  if ((isModuleEnabled == true) || (config->compareHigh <= config->compareLow) ||
81  {
82  /* If conditions are met change the status code to error */
83  statusCode = STATUS_ERROR;
84  }
85  else
86  {
87  /* Set clock prescaler */
88  EWM_SetPrescaler(base, config->prescaler);
89  /* Set compare high and low values */
90  EWM_SetCompareHigh(base, config->compareHigh);
91  EWM_SetCompareLow(base, config->compareLow);
92 
93  /* Configure the Control register and enable the instance.
94  * Set the values that are not affected by the input pin
95  */
96  tempValue = (uint8_t)((uint8_t)EWM_CTRL_EWMEN(1U) | EWM_CTRL_INTEN(config->interruptEnable ? 1U : 0U));
97 
98  /* Depending how the input pin is configured set the values into the
99  * temporary variable
100  */
101  switch (config->assertLogic)
102  {
104  tempValue |= EWM_CTRL_INEN_MASK; /* Input pin enabled, Input asserted on logic 0 */
105  break;
107  tempValue |= (uint8_t)(EWM_CTRL_INEN_MASK | EWM_CTRL_ASSIN_MASK); /* Input pin enabled, Input asserted on logic 1 */
108  break;
110  default:
111  /* Input pin disabled */
112  break;
113  }
114 
115  /* Write the configuration into the Control register */
116  EWM_SetControl(base, tempValue);
117  }
118 
119  /* Return the status code */
120  return statusCode;
121 }
122 
123 /*FUNCTION**********************************************************************
124  *
125  * Function Name : EWM_DRV_GetDefaultConfig
126  * Description : This function initializes the EWM configuration structure
127  * to default values
128  *
129  * Implements : EWM_DRV_GetDefaultConfig_Activity
130  *END**************************************************************************/
132 {
133  DEV_ASSERT(config != NULL);
134 
135  /* Disable interrupts */
136  config->interruptEnable = false;
137  /* Input pin enabled and configured to assert on logic 0 */
139  /* Maximum prescaler */
140  config->prescaler = 255U;
141  /* Maximum service window */
144 }
145 
146 /*FUNCTION**********************************************************************
147  *
148  * Function Name : EWM_DRV_Refresh
149  * Description : This function refreshes the EWM instance counter
150  *
151  * Implements : EWM_DRV_Refresh_Activity
152  *END**************************************************************************/
153 void EWM_DRV_Refresh(uint32_t instance)
154 {
155  DEV_ASSERT(instance < EWM_INSTANCE_COUNT);
156 
157  /* Base pointer */
158  EWM_Type * base = s_ewmBase[instance];
159 
160  /* Disable global IRQ's to avoid disturbing the refresh process */
162  /* Write the refresh values */
163  EWM_Refresh(base);
164  /* Re-enable all IRQ's */
166 }
167 
168 /*FUNCTION**********************************************************************
169  *
170  * Function Name : EWM_DRV_GetInputPinAssertLogic
171  * Description : Get the Input pin assert logic
172  *
173  * Implements : EWM_DRV_GetInputPinAssertLogic_Activity
174  *END**************************************************************************/
176 {
177  DEV_ASSERT(instance < EWM_INSTANCE_COUNT);
178 
179  /* Base pointer */
180  const EWM_Type * const base = s_ewmBase[instance];
181  /* Variable where to save the retrieved configuration */
182  ewm_in_assert_logic_t returnValue;
183  /* Temporary variable to use for storing the configuration */
184  uint8_t tempValue;
185 
186  /* Check if input pin is enabled */
187  if ((EWM_GetControl(base) & EWM_CTRL_INEN_MASK) != 0U)
188  {
189  /* If true get the assert logic into the temp variable */
190  tempValue = (uint8_t)((EWM_GetControl(base) & EWM_CTRL_ASSIN_MASK) >> EWM_CTRL_ASSIN_SHIFT);
191 
192  /* Convert the assert logic to the corresponding ewm_in_assert_logic_t
193  * value.
194  */
195  switch (tempValue)
196  {
197  case 0U:
198  returnValue = EWM_IN_ASSERT_ON_LOGIC_ZERO;
199  break;
200  case 1U:
201  returnValue = EWM_IN_ASSERT_ON_LOGIC_ONE;
202  break;
203  default:
204  returnValue = EWM_IN_ASSERT_DISABLED;
205  break;
206  }
207  }
208  else
209  {
210  /* Pin is disabled, return the corresponding value */
211  returnValue = EWM_IN_ASSERT_DISABLED;
212  }
213 
214  return returnValue;
215 }
216 
217 /*******************************************************************************
218  * EOF
219  ******************************************************************************/
ewm_in_assert_logic_t EWM_DRV_GetInputPinAssertLogic(uint32_t instance)
Get the Input pin assert logic.
Definition: ewm_driver.c:175
void EWM_DRV_Refresh(uint32_t instance)
Refresh EWM. This method needs to be called within the window period specified by the Compare Low and...
Definition: ewm_driver.c:153
#define EWM_INSTANCE_COUNT
Definition: S32K142.h:3288
#define EWM_CTRL_ASSIN_SHIFT
Definition: S32K142.h:3322
#define EWM_BASE_PTRS
Definition: S32K142.h:3299
ewm_in_assert_logic_t
Definition: ewm_driver.h:43
ewm_in_assert_logic_t assertLogic
Definition: ewm_driver.h:59
static EWM_Type *const s_ewmBase[]
Definition: ewm_driver.c:44
#define EWM_CTRL_ASSIN_MASK
Definition: S32K142.h:3321
uint8_t prescaler
Definition: ewm_driver.h:61
#define EWM_CTRL_INEN_MASK
Definition: S32K142.h:3325
void INT_SYS_DisableIRQGlobal(void)
Disable system interrupt.
#define DEV_ASSERT(x)
Definition: devassert.h:77
#define FEATURE_EWM_CMPL_MIN_VALUE
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
void INT_SYS_EnableIRQGlobal(void)
Enables system interrupt.
#define EWM_CTRL_EWMEN(x)
Definition: S32K142.h:3320
void EWM_DRV_GetDefaultConfig(ewm_init_config_t *config)
Init configuration structure to default values.
Definition: ewm_driver.c:131
status_t EWM_DRV_Init(uint32_t instance, const ewm_init_config_t *config)
Init EWM. This method initializes EWM instance to the configuration from the passed structure...
Definition: ewm_driver.c:63
#define FEATURE_EWM_CMPH_MAX_VALUE
uint8_t compareLow
Definition: ewm_driver.h:62
uint8_t compareHigh
Definition: ewm_driver.h:63
#define EWM_CTRL_INTEN(x)
Definition: S32K142.h:3332