wdog_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2018 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  status_t statusClockSource = STATUS_SUCCESS;
113 
114 #ifdef DEV_ERROR_DETECT
115  uint32_t prevClockHz, crtClockHz;
116 
117  /* Check if the previous clock source and the configuration clock source
118  * are enabled(if not, the counter will not be incremented) */
119  prevClockHz = WDOG_DRV_GetClockSourceFreq(WDOG_GetClockSource(s_wdogBase[instance]));
120  crtClockHz = WDOG_DRV_GetClockSourceFreq(userConfigPtr->clkSource);
121 
122  if ((prevClockHz == 0U) || (crtClockHz == 0U))
123  {
124  statusClockSource = STATUS_ERROR;
125  }
126 #endif
127  /* If clock source disabled */
128  if (statusClockSource == STATUS_SUCCESS)
129  {
130  /* If window mode enabled and window value greater than or equal to the timeout value. Or timeout value is 0 */
131  if (((userConfigPtr->winEnable) && (userConfigPtr->windowValue >= userConfigPtr->timeoutValue)) \
132  || (userConfigPtr->timeoutValue <= FEATURE_WDOG_MINIMUM_TIMEOUT_VALUE))
133  {
134  status = STATUS_ERROR;
135  }
136  else
137  {
138  /* Configure the WDOG module */
139  status = WDOG_Config(base, userConfigPtr);
140  }
141 
142  if (status == STATUS_SUCCESS)
143  {
144  /* enable WDOG timeout interrupt */
145  INT_SYS_EnableIRQ(s_wdogIrqId[instance]);
146  }
147  }
148  else
149  {
150  status = STATUS_ERROR;
151  }
152 
153  return status;
154 }
155 
156 /*FUNCTION**********************************************************************
157  *
158  * Function Name : WDOG_DRV_Deinit
159  * Description : De-initialize the WDOG driver
160  *
161  * Implements : WDOG_DRV_Deinit_Activity
162  *END**************************************************************************/
163 status_t WDOG_DRV_Deinit(uint32_t instance)
164 {
165  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
166  WDOG_Type * base = s_wdogBase[instance];
167  status_t status = STATUS_SUCCESS;
168 
170 
171  /* If allowed reconfigures WDOG */
172  if (WDOG_IsUpdateEnabled(base))
173  {
174  /* Disable WDOG timeout interrupt */
175  INT_SYS_DisableIRQ(s_wdogIrqId[instance]);
176 
177  /* Disable WDOG */
178  WDOG_Deinit(base);
179  }
180  else
181  {
182  status = STATUS_ERROR;
183  }
184 
185  /* Enable global interrupt */
187 
188  return status;
189 }
190 
191 /*FUNCTION**********************************************************************
192  *
193  * Function Name : WDOG_DRV_GetConfig
194  * Description : get the current configuration of the WDOG driver
195  *
196  * Implements : WDOG_DRV_GetConfig_Activity
197  *END**************************************************************************/
198  void WDOG_DRV_GetConfig(uint32_t instance,
199  wdog_user_config_t * const config)
200 {
201  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
202  DEV_ASSERT(config != NULL);
203  const WDOG_Type *baseAddr = s_wdogBase[instance];
204 
205  *config = WDOG_GetConfig(baseAddr);
206 }
207 
208 /*FUNCTION**********************************************************************
209  *
210  * Function Name : WDOG_DRV_GetDefaultConfig
211  * Description : get default configuration of the WDOG driver
212  *
213  * Implements : WDOG_DRV_GetDefaultConfig_Activity
214  *END**************************************************************************/
216 {
217  DEV_ASSERT(config != NULL);
218 
219  /* Construct CS register new value */
220  config->winEnable = false;
221  config->prescalerEnable = false;
222  config->intEnable = false;
223  config->updateEnable = true;
224  config->opMode.debug = false;
225  config->opMode.wait = false;
226  config->opMode.stop = false;
227  config->clkSource = WDOG_LPO_CLOCK;
228  /* Construct TOVAL register new value */
230  /* Construct WIN register new value */
232 }
233 
234 /*FUNCTION**********************************************************************
235  *
236  * Function Name : WDOG_DRV_SetInt
237  * Description : enable/disable the WDOG timeout interrupt
238  *
239  * Implements : WDOG_DRV_SetInt_Activity
240  *END**************************************************************************/
241 status_t WDOG_DRV_SetInt(uint32_t instance,
242  bool enable)
243 {
244  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
245  WDOG_Type * base = s_wdogBase[instance];
246  status_t status = STATUS_SUCCESS;
247 
248  /* If allowed reconfigures WDOG */
249  if (WDOG_IsUpdateEnabled(base))
250  {
251  /* Disable global interrupt */
253  /* Enable/disable WDOG timeout interrupt */
254  WDOG_SetInt(base, enable);
255  /* Enable global interrupt */
257  }
258  else
259  {
260  status = STATUS_ERROR;
261  }
262 
263  return status;
264 }
265 
266 /*FUNCTION**********************************************************************
267  *
268  * Function Name : WDOG_DRV_ClearIntFlag
269  * Description : Clear interrupt flag of the WDOG
270  *
271  * Implements : WDOG_DRV_ClearIntFlag_Activity
272  *END**************************************************************************/
273 void WDOG_DRV_ClearIntFlag(uint32_t instance)
274 {
275  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
276  WDOG_Type * base = s_wdogBase[instance];
277 
278  /* Clear interrupt flag of the WDOG */
279  WDOG_ClearIntFlag(base);
280 }
281 
282 /*FUNCTION**********************************************************************
283  *
284  * Function Name : WDOG_DRV_Trigger
285  * Description : Refreshes the WDOG counter
286  *
287  * Implements : WDOG_DRV_Trigger_Activity
288  *END**************************************************************************/
289  void WDOG_DRV_Trigger(uint32_t instance)
290 {
291  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
292  WDOG_Type * base = s_wdogBase[instance];
293 
294  WDOG_Trigger(base);
295 }
296 
297 /*FUNCTION**********************************************************************
298  *
299  * Function Name : WDOG_DRV_GetCounter
300  * Description : Get the value of the WDOG counter.
301  *
302  * Implements : WDOG_DRV_GetCounter_Activity
303  *END**************************************************************************/
304 uint16_t WDOG_DRV_GetCounter(uint32_t instance)
305 {
306  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
307  const WDOG_Type * base = s_wdogBase[instance];
308 
309  return (uint16_t)base->CNT;
310 }
311 
312 /*FUNCTION**********************************************************************
313  *
314  * Function Name : WDOG_DRV_SetWindow
315  * Description : Set window mode and window value of the WDOG.
316  *
317  * Implements : WDOG_DRV_SetWindow_Activity
318  *END**************************************************************************/
319 status_t WDOG_DRV_SetWindow(uint32_t instance,
320  bool enable,
321  uint16_t windowvalue)
322 {
323  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
324  WDOG_Type * base = s_wdogBase[instance];
325  status_t status = STATUS_SUCCESS;
326 
327  /* If allowed reconfigures WDOG */
328  if (WDOG_IsUpdateEnabled(base))
329  {
330  /* Set WDOG window mode */
331  WDOG_SetWindowMode(base, enable);
332 
333  /* If enable window mode */
334  if (enable)
335  {
336  /* Set window value for the WDOG */
337  WDOG_SetWindowValue(base, windowvalue);
338  }
339  }
340  else
341  {
342  status = STATUS_ERROR;
343  }
344 
345  return status;
346 }
347 
348 /*FUNCTION**********************************************************************
349  *
350  * Function Name : WDOG_DRV_SetMode
351  * Description : Set mode operation of the WDOG.
352  *
353  * Implements : WDOG_DRV_SetMode_Activity
354  *END**************************************************************************/
355 status_t WDOG_DRV_SetMode(uint32_t instance,
356  bool enable,
357  wdog_set_mode_t Setmode)
358 {
359  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
360  WDOG_Type * base = s_wdogBase[instance];
361  status_t status = STATUS_SUCCESS;
362 
363  /* If allowed reconfigures WDOG */
364  if (WDOG_IsUpdateEnabled(base))
365  {
366  switch (Setmode)
367  {
368  case WDOG_DEBUG_MODE:
369  /* Set WDOG debug mode */
370  WDOG_SetDebug(base, enable);
371  break;
372  case WDOG_WAIT_MODE:
373  /* Set WDOG wait mode */
374  WDOG_SetWait(base, enable);
375  break;
376  case WDOG_STOP_MODE:
377  /* Set WDOG stop mode */
378  WDOG_SetStop(base, enable);
379  break;
380  default:
381  /* Do nothings */
382  break;
383  }
384  }
385  else
386  {
387  status = STATUS_ERROR;
388  }
389 
390  return status;
391 }
392 
393 /*FUNCTION**********************************************************************
394  *
395  * Function Name : WDOG_DRV_SetTimeout
396  * Description : Set time value of the WDOG timeout.
397  *
398  * Implements : WDOG_DRV_SetTimeout_Activity
399  *END**************************************************************************/
400 status_t WDOG_DRV_SetTimeout(uint32_t instance,
401  uint16_t timeout)
402 {
403  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
404  WDOG_Type * base = s_wdogBase[instance];
405  status_t status = STATUS_SUCCESS;
406 
407  /* If allowed reconfigures WDOG */
408  if (WDOG_IsUpdateEnabled(base))
409  {
410  WDOG_UNLOCK(base);
411 
412  base->TOVAL = timeout;
413  }
414  else
415  {
416  status = STATUS_ERROR;
417  }
418 
419  return status;
420 }
421 
422 /*FUNCTION**********************************************************************
423  *
424  * Function Name : WDOG_DRV_SetTestMode
425  * Description : Set test mode of the WDOG.
426  *
427  * Implements : WDOG_DRV_SetTestMode_Activity
428  *END**************************************************************************/
429 status_t WDOG_DRV_SetTestMode(uint32_t instance,
430  wdog_test_mode_t testMode)
431 {
432  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
433  WDOG_Type * base = s_wdogBase[instance];
434  uint32_t regValue = base->CS;
435  status_t status = STATUS_SUCCESS;
436 
437  /* If allowed reconfigures WDOG */
438  if (WDOG_IsUpdateEnabled(base))
439  {
440  regValue &= ~(WDOG_CS_TST_MASK);
441  regValue |= WDOG_CS_TST(testMode);
442 
443  WDOG_UNLOCK(base);
444 
445  base->CS = regValue;
446  }
447  else
448  {
449  status = STATUS_ERROR;
450  }
451 
452  return status;
453 }
454 
455 /*FUNCTION**********************************************************************
456  *
457  * Function Name : WDOG_DRV_GetTestMode
458  * Description : Get test mode of the WDOG.
459  *
460  * Implements : WDOG_DRV_GetTestMode_Activity
461  *END**************************************************************************/
463 {
464  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
465  const WDOG_Type * base = s_wdogBase[instance];
467 
468  /* Gets test mode */
469  switch (WDOG_GetTestMode(base))
470  {
471  case 0U:
472  testMode = WDOG_TST_DISABLED;
473  break;
474  case 1U:
475  testMode = WDOG_TST_USER;
476  break;
477  case 2U:
478  testMode = WDOG_TST_LOW;
479  break;
480  case 3U:
481  testMode = WDOG_TST_HIGH;
482  break;
483  default:
484  testMode = WDOG_TST_DISABLED;
485  break;
486  }
487 
488  return testMode;
489 }
490 
491 /*******************************************************************************
492  * EOF
493  ******************************************************************************/
void WDOG_DRV_GetDefaultConfig(wdog_user_config_t *const config)
Gets default configuration of the WDOG.
Definition: wdog_driver.c:215
status_t WDOG_DRV_Deinit(uint32_t instance)
De-initializes the WDOG driver.
Definition: wdog_driver.c:163
status_t WDOG_DRV_Init(uint32_t instance, const wdog_user_config_t *userConfigPtr)
Initializes the WDOG driver.
Definition: wdog_driver.c:105
void WDOG_DRV_ClearIntFlag(uint32_t instance)
Clear interrupt flag of the WDOG.
Definition: wdog_driver.c:273
#define WDOG_BASE_PTRS
Definition: S32K118.h:10163
static WDOG_Type *const s_wdogBase[]
Table of base addresses for WDOG instances.
Definition: wdog_driver.c:55
volatile uint32_t CNT
Definition: S32K118.h:10146
uint16_t timeoutValue
Definition: wdog_driver.h:106
#define WDOG_CS_TST(x)
Definition: S32K118.h:10196
#define FEATURE_WDOG_TO_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.
uint16_t windowValue
Definition: wdog_driver.h:105
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
volatile uint32_t TOVAL
Definition: S32K118.h:10147
status_t CLOCK_SYS_GetFreq(clock_names_t clockName, uint32_t *frequency)
Gets the clock frequency for a specific clock name.
status_t WDOG_DRV_SetTestMode(uint32_t instance, wdog_test_mode_t testMode)
Changes the WDOG test mode.
Definition: wdog_driver.c:429
status_t WDOG_DRV_SetMode(uint32_t instance, bool enable, wdog_set_mode_t Setmode)
Sets the mode operation of the WDOG.
Definition: wdog_driver.c:355
#define FEATURE_WDOG_WIN_RESET_VALUE
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
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K118.h:188
uint16_t WDOG_DRV_GetCounter(uint32_t instance)
Gets the value of the WDOG counter.
Definition: wdog_driver.c:304
wdog_op_mode_t opMode
Definition: wdog_driver.h:101
#define WDOG_CS_TST_MASK
Definition: S32K118.h:10193
void INT_SYS_EnableIRQGlobal(void)
Enables system interrupt.
volatile uint32_t CS
Definition: S32K118.h:10145
#define WDOG_INSTANCE_COUNT
Definition: S32K118.h:10152
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: S32K118.h:10169
status_t WDOG_DRV_SetWindow(uint32_t instance, bool enable, uint16_t windowvalue)
Set window mode and window value of the WDOG.
Definition: wdog_driver.c:319
void WDOG_DRV_Trigger(uint32_t instance)
Refreshes the WDOG counter.
Definition: wdog_driver.c:289
#define FEATURE_WDOG_MINIMUM_TIMEOUT_VALUE
wdog_test_mode_t WDOG_DRV_GetTestMode(uint32_t instance)
Gets the WDOG test mode.
Definition: wdog_driver.c:462
void WDOG_DRV_GetConfig(uint32_t instance, wdog_user_config_t *const config)
Gets the current configuration of the WDOG.
Definition: wdog_driver.c:198
status_t WDOG_DRV_SetTimeout(uint32_t instance, uint16_t timeout)
Sets the value of the WDOG timeout.
Definition: wdog_driver.c:400
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:241