S32 SDK
wdog_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 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 
47 #include "wdog_hw_access.h"
48 #include "clock_manager.h"
49 
50 /*******************************************************************************
51  * Variables
52  ******************************************************************************/
53 
55 static WDOG_Type * const s_wdogBase[] = WDOG_BASE_PTRS;
56 
58 static const IRQn_Type s_wdogIrqId[] = WDOG_IRQS;
59 
60 /*******************************************************************************
61  * Private Functions
62  ******************************************************************************/
63 
64 #ifdef DEV_ERROR_DETECT
65 /* Gets the frequency of the specified WDOG clock source. Only used at development
66  * time, when WDOG_DRV_Init checks if the needed clock sources are enabled. */
67 static uint32_t WDOG_DRV_GetClockSourceFreq(wdog_clk_source_t wdogClk)
68 {
69  uint32_t freq = 0;
70 
71  switch (wdogClk)
72  {
73  case WDOG_BUS_CLOCK:
74  (void)CLOCK_SYS_GetFreq(BUS_CLK, &freq);
75  break;
76  case WDOG_SIRC_CLOCK:
77  (void)CLOCK_SYS_GetFreq(SIRC_CLK, &freq);
78  break;
79  case WDOG_SOSC_CLOCK:
80  (void)CLOCK_SYS_GetFreq(SOSC_CLK, &freq);
81  break;
82  case WDOG_LPO_CLOCK:
83  (void)CLOCK_SYS_GetFreq(SIM_LPO_CLK, &freq);
84  break;
85  default:
86  /* Should not get here */
87  break;
88  }
89 
90  return freq;
91 }
92 
93 #endif /* ifdef DEV_ERROR_DETECT */
94 
95 /*******************************************************************************
96  * Code
97  ******************************************************************************/
98 /*FUNCTION**********************************************************************
99  *
100  * Function Name : WDOG_DRV_Init
101  * Description : initialize the WDOG driver
102  *
103  * Implements : WDOG_DRV_Init_Activity
104  *END**************************************************************************/
105 status_t WDOG_DRV_Init(uint32_t instance,
106  const wdog_user_config_t * userConfigPtr)
107 {
108  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
109  DEV_ASSERT(userConfigPtr != NULL);
110  WDOG_Type * base = s_wdogBase[instance];
111  status_t status = STATUS_SUCCESS;
112 
113 #ifdef DEV_ERROR_DETECT
114  uint32_t prevClockHz, crtClockHz;
115 
116  /* Check if the previous clock source and the configuration clock source
117  * are enabled(if not, the counter will not be incremented) */
118  prevClockHz = WDOG_DRV_GetClockSourceFreq(WDOG_GetClockSource(s_wdogBase[instance]));
119  crtClockHz = WDOG_DRV_GetClockSourceFreq(userConfigPtr->clkSource);
120 
121  DEV_ASSERT((prevClockHz != 0U) && (crtClockHz != 0U));
122 #endif
123 
124  /* Configure the WDOG module */
125  status = WDOG_Config(base, userConfigPtr);
126 
127  if (status == STATUS_SUCCESS)
128  {
129  /* enable WDOG timeout interrupt */
130  INT_SYS_EnableIRQ(s_wdogIrqId[instance]);
131  }
132 
133  return status;
134 }
135 
136 /*FUNCTION**********************************************************************
137  *
138  * Function Name : WDOG_DRV_Deinit
139  * Description : De-initialize the WDOG driver
140  *
141  * Implements : WDOG_DRV_Deinit_Activity
142  *END**************************************************************************/
143 void WDOG_DRV_Deinit(uint32_t instance)
144 {
145  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
146  WDOG_Type * base = s_wdogBase[instance];
147 
149 
150  /* Disable WDOG */
151  WDOG_Disable(base);
152 
154 
155  /* Disable WDOG timeout interrupt */
156  INT_SYS_DisableIRQ(s_wdogIrqId[instance]);
157 }
158 
159 /*FUNCTION**********************************************************************
160  *
161  * Function Name : WDOG_DRV_GetConfig
162  * Description : get the current configuration of the WDOG driver
163  *
164  * Implements : WDOG_DRV_GetConfig_Activity
165  *END**************************************************************************/
166  void WDOG_DRV_GetConfig(uint32_t instance,
167  wdog_user_config_t * const config)
168 {
169  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
170  DEV_ASSERT(config != NULL);
171  const WDOG_Type *baseAddr = s_wdogBase[instance];
172 
173  *config = WDOG_GetConfig(baseAddr);
174 }
175 
176 /*FUNCTION**********************************************************************
177  *
178  * Function Name : WDOG_DRV_GetDefaultConfig
179  * Description : get default configuration of the WDOG driver
180  *
181  * Implements : WDOG_DRV_GetDefaultConfig_Activity
182  *END**************************************************************************/
184 {
185  DEV_ASSERT(config != NULL);
186 
187  /* Construct CS register new value */
188  config->winEnable = false;
189  config->prescalerEnable = false;
190  config->intEnable = false;
191  config->updateEnable = false;
192  config->opMode.debug = false;
193  config->opMode.wait = false;
194  config->opMode.stop = false;
197  config->clkSource = WDOG_LPO_CLOCK;
198 }
199 
200 /*FUNCTION**********************************************************************
201  *
202  * Function Name : WDOG_DRV_SetInt
203  * Description : enable/disable the WDOG timeout interrupt
204  *
205  * Implements : WDOG_DRV_SetInt_Activity
206  *END**************************************************************************/
207 status_t WDOG_DRV_SetInt(uint32_t instance,
208  bool enable)
209 {
210  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
211  WDOG_Type * base = s_wdogBase[instance];
212  status_t status = STATUS_SUCCESS;
213 
214  if (WDOG_IsUpdateEnabled(base))
215  {
217 
218  /* Enable/disable WDOG interrupt */
219  WDOG_SetInt(base, enable);
220 
221  while (WDOG_IsUnlocked(base))
222  {
223  /* Wait until the unlock window closes */
224  }
225 
227  }
228  else
229  {
230  status = STATUS_ERROR;
231  }
232 
233  return status;
234 }
235 
236 /*FUNCTION**********************************************************************
237  *
238  * Function Name : WDOG_DRV_Trigger
239  * Description : Refreshes the WDOG counter
240  *
241  * Implements : WDOG_DRV_Trigger_Activity
242  *END**************************************************************************/
243  void WDOG_DRV_Trigger(uint32_t instance)
244 {
245  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
246  WDOG_Type * base = s_wdogBase[instance];
247 
248  WDOG_Trigger(base);
249 }
250 
251 /*FUNCTION**********************************************************************
252  *
253  * Function Name : WDOG_DRV_GetCounter
254  * Description : Get the value of the WDOG counter.
255  *
256  * Implements : WDOG_DRV_GetCounter_Activity
257  *END**************************************************************************/
258 uint16_t WDOG_DRV_GetCounter(uint32_t instance)
259 {
260  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
261  const WDOG_Type * base = s_wdogBase[instance];
262 
263  return (uint16_t)base->CNT;
264 }
265 
266 /*FUNCTION**********************************************************************
267  *
268  * Function Name : WDOG_DRV_SetWindow
269  * Description : Set window mode and window value of the WDOG.
270  *
271  * Implements : WDOG_DRV_SetWindow_Activity
272  *END**************************************************************************/
273 void WDOG_DRV_SetWindow(uint32_t instance,
274  bool enable,
275  uint16_t windowvalue)
276 {
277  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
278  WDOG_Type * base = s_wdogBase[instance];
279 
280  /* Set WDOG window mode */
281  WDOG_SetWindowMode(base, enable);
282 
283  /* If enable window mode */
284  if(enable)
285  {
286  /* Set window value for the WDOG */
287  WDOG_SetWindowValue(base, windowvalue);
288  }
289 }
290 
291 /*FUNCTION**********************************************************************
292  *
293  * Function Name : WDOG_DRV_SetMode
294  * Description : Set mode operation of the WDOG.
295  *
296  * Implements : WDOG_DRV_SetMode_Activity
297  *END**************************************************************************/
298 void WDOG_DRV_SetMode(uint32_t instance,
299  bool enable,
300  wdog_set_mode_t Setmode)
301 {
302  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
303  WDOG_Type * base = s_wdogBase[instance];
304 
305  switch(Setmode)
306  {
307  case WDOG_DEBUG_MODE:
308  /* Set WDOG debug mode */
309  WDOG_SetDebug(base, enable);
310  break;
311  case WDOG_WAIT_MODE:
312  /* Set WDOG wait mode */
313  WDOG_SetWait(base, enable);
314  break;
315  case WDOG_STOP_MODE:
316  /* Set WDOG stop mode */
317  WDOG_SetStop(base, enable);
318  break;
319  default:
320  /* Do nothings */
321  break;
322  }
323 }
324 
325 /*FUNCTION**********************************************************************
326  *
327  * Function Name : WDOG_DRV_SetTimeout
328  * Description : Set time value of the WDOG timeout.
329  *
330  * Implements : WDOG_DRV_SetTimeout_Activity
331  *END**************************************************************************/
332 void WDOG_DRV_SetTimeout(uint32_t instance,
333  uint16_t timeout)
334 {
335  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
336  WDOG_Type * base = s_wdogBase[instance];
337 
338  WDOG_UNLOCK(base);
339 
340  base->TOVAL = timeout;
341 }
342 
343 /*FUNCTION**********************************************************************
344  *
345  * Function Name : WDOG_DRV_SetTestMode
346  * Description : Set test mode of the WDOG.
347  *
348  * Implements : WDOG_DRV_SetTestMode_Activity
349  *END**************************************************************************/
350 void WDOG_DRV_SetTestMode(uint32_t instance,
351  wdog_test_mode_t testMode)
352 {
353  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
354  WDOG_Type * base = s_wdogBase[instance];
355  uint32_t regValue = base->CS;
356 
357  regValue &= ~(WDOG_CS_TST_MASK);
358  regValue |= WDOG_CS_TST(testMode);
359 
360  WDOG_UNLOCK(base);
361 
362  base->CS = regValue;
363 }
364 
365 /*FUNCTION**********************************************************************
366  *
367  * Function Name : WDOG_DRV_GetTestMode
368  * Description : Get test mode of the WDOG.
369  *
370  * Implements : WDOG_DRV_GetTestMode_Activity
371  *END**************************************************************************/
373 {
374  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
375  const WDOG_Type * base = s_wdogBase[instance];
377 
378  /* Gets test mode */
379  switch (WDOG_GetTestMode(base))
380  {
381  case 0U:
382  testMode = WDOG_TST_DISABLED;
383  break;
384  case 1U:
385  testMode = WDOG_TST_USER;
386  break;
387  case 2U:
388  testMode = WDOG_TST_LOW;
389  break;
390  case 3U:
391  testMode = WDOG_TST_HIGH;
392  break;
393  default:
394  testMode = WDOG_TST_DISABLED;
395  break;
396  }
397 
398  return testMode;
399 }
400 
401 /*******************************************************************************
402  * EOF
403  ******************************************************************************/
void WDOG_DRV_GetDefaultConfig(wdog_user_config_t *const config)
Gets default configuration of the WDOG.
Definition: wdog_driver.c:183
__IO uint32_t CNT
Definition: S32K142.h:11189
#define FEATURE_WDOG_TO_RESET_VALUE
status_t WDOG_DRV_Init(uint32_t instance, const wdog_user_config_t *userConfigPtr)
Initializes the WDOG driver.
Definition: wdog_driver.c:105
#define WDOG_BASE_PTRS
Definition: S32K142.h:11206
static WDOG_Type *const s_wdogBase[]
Table of base addresses for WDOG instances.
Definition: wdog_driver.c:55
void WDOG_DRV_SetMode(uint32_t instance, bool enable, wdog_set_mode_t Setmode)
Sets the mode operation of the WDOG.
Definition: wdog_driver.c:298
__IO uint32_t CS
Definition: S32K142.h:11188
#define WDOG_CS_TST(x)
Definition: S32K142.h:11239
#define FEATURE_WDOG_WIN_RESET_VALUE
static const IRQn_Type s_wdogIrqId[]
Table to save WDOG IRQ enum numbers defined in CMSIS header file.
Definition: wdog_driver.c:58
void INT_SYS_DisableIRQ(IRQn_Type irqNumber)
Disables an interrupt for a given IRQ number.
void INT_SYS_DisableIRQGlobal(void)
Disable system interrupt.
#define DEV_ASSERT(x)
Definition: devassert.h:77
WDOG user configuration structure Implements : wdog_user_config_t_Class.
Definition: wdog_driver.h:98
void WDOG_DRV_SetWindow(uint32_t instance, bool enable, uint16_t windowvalue)
Set window mode and window value of the WDOG.
Definition: wdog_driver.c:273
uint32_t windowValue
Definition: wdog_driver.h:105
status_t CLOCK_SYS_GetFreq(clock_names_t clockName, uint32_t *frequency)
Gets the clock frequency for a specific clock name.
wdog_clk_source_t clkSource
Definition: wdog_driver.h:100
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
__IO uint32_t TOVAL
Definition: S32K142.h:11190
uint16_t WDOG_DRV_GetCounter(uint32_t instance)
Gets the value of the WDOG counter.
Definition: wdog_driver.c:258
wdog_op_mode_t opMode
Definition: wdog_driver.h:101
#define WDOG_CS_TST_MASK
Definition: S32K142.h:11236
void INT_SYS_EnableIRQGlobal(void)
Enables system interrupt.
void WDOG_DRV_Deinit(uint32_t instance)
De-initializes the WDOG driver.
Definition: wdog_driver.c:143
void WDOG_DRV_SetTestMode(uint32_t instance, wdog_test_mode_t testMode)
Changes the WDOG test mode.
Definition: wdog_driver.c:350
#define WDOG_INSTANCE_COUNT
Definition: S32K142.h:11195
wdog_test_mode_t
Test modes for the WDOG. Implements : wdog_test_mode_t_Class.
Definition: wdog_driver.h:64
wdog_set_mode_t
set modes for the WDOG. Implements : wdog_set_mode_t_Class
Definition: wdog_driver.h:76
void INT_SYS_EnableIRQ(IRQn_Type irqNumber)
Enables an interrupt for a given IRQ number.
wdog_clk_source_t
Clock sources for the WDOG. Implements : wdog_clk_source_t_Class.
Definition: wdog_driver.h:52
#define WDOG_IRQS
Definition: S32K142.h:11212
void WDOG_DRV_Trigger(uint32_t instance)
Refreshes the WDOG counter.
Definition: wdog_driver.c:243
uint32_t timeoutValue
Definition: wdog_driver.h:106
void WDOG_DRV_SetTimeout(uint32_t instance, uint16_t timeout)
Sets the value of the WDOG timeout.
Definition: wdog_driver.c:332
wdog_test_mode_t WDOG_DRV_GetTestMode(uint32_t instance)
Gets the WDOG test mode.
Definition: wdog_driver.c:372
void WDOG_DRV_GetConfig(uint32_t instance, wdog_user_config_t *const config)
Gets the current configuration of the WDOG.
Definition: wdog_driver.c:166
status_t WDOG_DRV_SetInt(uint32_t instance, bool enable)
Enables/Disables the WDOG timeout interrupt and sets a function to be called when a timeout interrupt...
Definition: wdog_driver.c:207
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K142.h:192