wdg_pal.c
Go to the documentation of this file.
1 /*
2  * Copyright 2017-2018 NXP
3  * All rights reserved.
4  *
5  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
6  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
8  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
9  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
10  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
11  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
12  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
13  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
14  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
15  * THE POSSIBILITY OF SUCH DAMAGE.
16  */
17 
51 #include "wdg_pal.h"
52 #include "stddef.h"
53 
54 /* Include PD files */
55 #if defined(WDG_OVER_WDOG)
56  #include "wdog_driver.h"
57 #endif
58 
59 #if defined(WDG_OVER_EWM)
60  #include "ewm_driver.h"
61 #endif
62 
63 #if defined(WDG_OVER_SWT)
64  #include "swt_driver.h"
65 #endif
66 
67 /*******************************************************************************
68  * Code
69  ******************************************************************************/
70 
76 /*FUNCTION**********************************************************************
77  *
78  * Function Name : WDG_Init
79  * Description : Initialize the WDG PAL
80  *
81  * Implements : WDG_Init_Activity
82  *END**************************************************************************/
83 status_t WDG_Init(const wdg_instance_t * const instance,
84  const wdg_config_t * configPtr)
85 {
86  DEV_ASSERT(instance != NULL);
87  DEV_ASSERT(configPtr != NULL);
88 
89  uint32_t windowValue = 0U;
91 
92  /* Calculate window value */
93  if (true == configPtr->winEnable)
94  {
95  DEV_ASSERT(configPtr->percentWindow <= 100U);
96 
97  windowValue = (uint32_t)(((uint64_t)configPtr->percentWindow * configPtr->timeoutValue) / 100U);
98  }
99 
100  /* Define WDG PAL over WDOG */
101 #if defined (WDG_OVER_WDOG)
102  if (WDG_INST_TYPE_WDOG == instance->instType)
103  {
104  /* Define structures for WDG PAL over WDOG */
105  wdog_user_config_t wdogConfig =
106  {
107  .clkSource = (wdog_clk_source_t)configPtr->clkSource,
108  .opMode =
109  {
110  .wait = configPtr->opMode.wait,
111  .stop = configPtr->opMode.stop,
112  .debug = configPtr->opMode.debug
113  },
114  .updateEnable = true,
115  .intEnable = configPtr->intEnable,
116  .winEnable = configPtr->winEnable,
117  .windowValue = (uint16_t)((configPtr->timeoutValue - windowValue) & 0xFFFFU),
118  .timeoutValue = (uint16_t)(configPtr->timeoutValue & 0xFFFFU),
119  .prescalerEnable = configPtr->prescalerEnable
120  };
121 
122  /* Initialize WDOG instance */
123  status = WDOG_DRV_Init(instance->instIdx, &wdogConfig);
124  }
125 #endif /* defined (WDG_OVER_WDOG) */
126 
127  /* Define WDG PAL over EWM */
128 #if defined (WDG_OVER_EWM)
129  if (WDG_INST_TYPE_EWM == instance->instType)
130  {
131  /* Define structures for WDG PAL over EWM */
132  ewm_init_config_t ewmConfig;
133 
134  /* Set value for EWM structure */
135  ewmConfig.interruptEnable = configPtr->intEnable;
136 
137  /* Set value for input pin asserts and clock prescaler */
138  if (NULL == configPtr->extension)
139  {
140  ewmConfig.assertLogic = EWM_IN_ASSERT_ON_LOGIC_ZERO;
141  ewmConfig.prescaler = 0xFFU;
142  }
143  else
144  {
145  ewmConfig.assertLogic = (ewm_in_assert_logic_t)((extension_ewm_for_wdg_t*)(configPtr->extension))->assertLogic;
146  ewmConfig.prescaler = ((extension_ewm_for_wdg_t*)(configPtr->extension))->prescalerValue;
147  }
148 
149  /* Set value for compare low register */
150  if (true == configPtr->winEnable)
151  {
152  ewmConfig.compareLow = (uint8_t)((configPtr->timeoutValue - windowValue) & 0xFFU);
153  }
154  else
155  {
156  ewmConfig.compareLow = FEATURE_EWM_CMPL_MIN_VALUE;
157  }
158 
159  /* Set value for compare high register */
160  if (configPtr->timeoutValue > FEATURE_EWM_CMPH_MAX_VALUE)
161  {
162  ewmConfig.compareHigh = FEATURE_EWM_CMPH_MAX_VALUE;
163  }
164  else
165  {
166  ewmConfig.compareHigh = (uint8_t)(configPtr->timeoutValue & 0xFFU);
167  }
168 
169  /* Initialize EWM instance */
170  status = EWM_DRV_Init(instance->instIdx, &ewmConfig);
171  }
172 #endif /* defined (WDG_OVER_EWM) */
173 
174  /* Define WDG PAL over SWT */
175 #if defined (WDG_OVER_SWT)
176  if (WDG_INST_TYPE_SWT == instance->instType)
177  {
178  /* Define structures for WDG PAL over SWT */
179  swt_user_config_t swtConfig =
180  {
181  .mapConfig = 0xFFU,
182  .invalidReset = false,
183  .winEnable = configPtr->winEnable,
184  .intEnable = configPtr->intEnable,
185  .stop = configPtr->opMode.stop,
186  .debug = configPtr->opMode.debug,
187  .serviceMode = SWT_FS_SEQ_MODE,
188  .lockConfig = SWT_UNLOCK,
189  .timeoutValue = configPtr->timeoutValue,
190  .windowValue = windowValue,
191  .initKey = 0U
192  };
193 
194  /* Initialize SWT instance */
195  status = SWT_DRV_Init(instance->instIdx, &swtConfig);
196  }
197 #endif /* defined (WDG_OVER_SWT) */
198 
199  return status;
200 }
201 
202 /*FUNCTION**********************************************************************
203  *
204  * Function Name : WDG_GetDefaultConfig
205  * Description : Gets default configuration of the WDG PAL
206  *
207  * Implements : WDG_GetDefaultConfig_Activity
208  *END**************************************************************************/
209 void WDG_GetDefaultConfig(wdg_config_t * const config)
210 {
211  DEV_ASSERT(config != NULL);
212 
213  config->opMode.debug = false;
214  config->opMode.wait = false;
215  config->opMode.stop = false;
216  config->percentWindow = 0U;
217  config->intEnable = false;
218  config->winEnable = false;
219  config->prescalerEnable = false;
220 
221  /* Define WDG PAL over WDOG */
222 #if (defined(WDG_OVER_WDOG) || defined(WDG_OVER_EWM))
223  config->clkSource = WDG_PAL_LPO_CLOCK;
225 #endif /* (defined (WDG_OVER_WDOG) || defined(WDG_OVER_EWM)) */
226 
227  /* Define WDG PAL over EWM */
228 #if (defined(WDG_OVER_EWM))
229  config->extension = NULL;
230 #endif /* defined (WDG_OVER_EWM) */
231 
232  /* Define WDG PAL over SWT */
233 #if defined(WDG_OVER_SWT)
234  config->clkSource = WDG_PAL_SIRC_CLOCK;
235  config->timeoutValue = FEATURE_SWT_TO_RESET_VALUE;
236 #endif /* defined (WDG_OVER_SWT) */
237 }
238 
239 /*FUNCTION**********************************************************************
240  *
241  * Function Name : WDG_Refresh
242  * Description : Refreshes the WDG PAL counter.
243  *
244  * Implements : WDG_Refresh_Activity
245  *END**************************************************************************/
246 void WDG_Refresh(const wdg_instance_t * const instance)
247 {
248  DEV_ASSERT(instance != NULL);
249 
250  /* Define WDG PAL over WDOG */
251 #if defined(WDG_OVER_WDOG)
252  if (WDG_INST_TYPE_WDOG == instance->instType)
253  {
254  /* Refresh counter over WDOG module */
255  WDOG_DRV_Trigger(instance->instIdx);
256  }
257 #endif /* defined (WDG_OVER_WDOG) */
258 
259  /* Define WDG PAL over EWM */
260 #if defined(WDG_OVER_EWM)
261  if (WDG_INST_TYPE_EWM == instance->instType)
262  {
263  /* Refresh counter over EWM module */
264  EWM_DRV_Refresh(instance->instIdx);
265  }
266 #endif /* defined (WDG_OVER_EWM) */
267 
268  /* Define WDG PAL over SWT */
269 #if defined(WDG_OVER_SWT)
270  if (WDG_INST_TYPE_SWT == instance->instType)
271  {
272  /* Refresh counter over SWT module */
273  SWT_DRV_Service(instance->instIdx);
274  }
275 #endif /* defined (WDG_OVER_SWT) */
276 }
277 
278 /*FUNCTION**********************************************************************
279  *
280  * Function Name : WDG_Deinit
281  * Description : De-initializes the WDG PAL
282  *
283  * Implements : WDG_Deinit_Activity
284  *END**************************************************************************/
285 status_t WDG_Deinit(const wdg_instance_t * const instance)
286 {
287  DEV_ASSERT(instance != NULL);
288 
289  status_t status = STATUS_UNSUPPORTED;
290 
291  /* Define WDG PAL over WDOG */
292 #if defined(WDG_OVER_WDOG)
293  if (WDG_INST_TYPE_WDOG == instance->instType)
294  {
295  /* De-initiation over WDOG module */
296  status = WDOG_DRV_Deinit(instance->instIdx);
297  }
298 #endif /* defined (WDG_OVER_WDOG) */
299 
300  /* Define WDG PAL over SWT */
301 #if defined(WDG_OVER_SWT)
302  if (WDG_INST_TYPE_SWT == instance->instType)
303  {
304  /* De-initiation over SWT module */
305  status = SWT_DRV_Deinit(instance->instIdx);
306  }
307 #endif /* defined (WDG_OVER_SWT) */
308 
309  return status;
310 }
311 
312 /*FUNCTION**********************************************************************
313  *
314  * Function Name : WDG_SetInt
315  * Description : Set interrupt for WDG PAL module
316  *
317  * Implements : WDG_SetInt_Activity
318  *END**************************************************************************/
319 status_t WDG_SetInt(const wdg_instance_t * const instance,
320  bool enable)
321 {
322  DEV_ASSERT(instance != NULL);
323 
324  status_t status = STATUS_UNSUPPORTED;
325 
326  /* Define WDG PAL over WDOG */
327 #if defined(WDG_OVER_WDOG)
328  if (WDG_INST_TYPE_WDOG == instance->instType)
329  {
330  /* Set interrupt over WDOG module */
331  status = WDOG_DRV_SetInt(instance->instIdx, enable);
332  }
333 #endif /* defined (WDG_OVER_WDOG) */
334 
335  /* Define PAL PAL over SWT */
336 #if defined(WDG_OVER_SWT)
337  if (WDG_INST_TYPE_SWT == instance->instType)
338  {
339  /* Set interrupt over SWT module */
340  status = SWT_DRV_SetIntConfig(instance->instIdx, enable);
341  }
342 #endif /* defined (WDG_OVER_SWT) */
343 
344  return status;
345 }
346 
347 /*FUNCTION**********************************************************************
348  *
349  * Function Name : WDG_SetTimeout
350  * Description : Sets the value of the WDG PAL timeout.
351  *
352  * Implements : WDG_SetTimeout_Activity
353  *END**************************************************************************/
354 status_t WDG_SetTimeout(const wdg_instance_t * const instance,
355  uint32_t value)
356 {
357  DEV_ASSERT(instance != NULL);
358 
359  status_t status = STATUS_UNSUPPORTED;
360 
361  /* Define WDG PAL over WDOG */
362 #if defined(WDG_OVER_WDOG)
363  if (WDG_INST_TYPE_WDOG == instance->instType)
364  {
365  /* Set timeout value over WDOG module */
366  status = WDOG_DRV_SetTimeout(instance->instIdx, (uint16_t)value);
367  }
368 #endif /* defined (WDG_OVER_WDOG) */
369 
370  /* Define WDG PAL over SWT */
371 #if defined(WDG_OVER_SWT)
372  if (WDG_INST_TYPE_SWT == instance->instType)
373  {
374  /* Set timeout value over SWT module */
375  status = SWT_DRV_SetTimeoutValue(instance->instIdx, value);
376  }
377 #endif /* defined (WDG_OVER_SWT) */
378 
379  return status;
380 }
381 
382 /*FUNCTION**********************************************************************
383  *
384  * Function Name : WDG_SetWindow
385  * Description : Set window mode and window value of the WDG PAL.
386  *
387  * Implements : WDG_SetWindow_Activity
388  *END**************************************************************************/
389 status_t WDG_SetWindow(const wdg_instance_t * const instance,
390  bool enable,
391  uint32_t value)
392 {
393  DEV_ASSERT(instance != NULL);
394 
395  status_t status = STATUS_UNSUPPORTED;
396 
397  /* Define WDG PAL over WDOG */
398 #if defined(WDG_OVER_WDOG)
399  if (WDG_INST_TYPE_WDOG == instance->instType)
400  {
401  /* Set window value over WDOG module */
402  status = WDOG_DRV_SetWindow(instance->instIdx, enable, (uint16_t)value);
403  }
404 #endif /* defined (WDG_OVER_WDOG) */
405 
406  /* Define WDG PAL over SWT */
407 #if defined(WDG_OVER_SWT)
408  if (WDG_INST_TYPE_SWT == instance->instType)
409  {
410  /* Set window value over SWT module */
411  status = SWT_DRV_SetWindowConfig(instance->instIdx, enable, value);
412  }
413 #endif /* defined (WDG_OVER_SWT) */
414 
415  return status;
416 }
417 
418 /*FUNCTION**********************************************************************
419  *
420  * Function Name : WDG_GetCounter
421  * Description : Gets the value of the WDG PAL counter.
422  * Note that: Counter will be reset to timeout value if WDG PAL uses SWT.
423  * The counter will continue to run if WDG PAL uses WDOG.
424  *
425  * Implements : WDG_GetCounter_Activity
426  *END**************************************************************************/
427 status_t WDG_GetCounter(const wdg_instance_t * const instance,
428  uint32_t * value)
429 {
430  DEV_ASSERT(instance != NULL);
431 
432  status_t status = STATUS_UNSUPPORTED;
433 
434  /* Define WDG PAL over WDOG */
435 #if defined(WDG_OVER_WDOG)
436  if (WDG_INST_TYPE_WDOG == instance->instType)
437  {
438  /* Get counter value over WDOG module */
439  *value = WDOG_DRV_GetCounter(instance->instIdx);
440  status = STATUS_SUCCESS;
441  }
442 #endif /* defined (WDG_OVER_WDOG) */
443 
444  /* Define WDG PAL over SWT */
445 #if defined(WDG_OVER_SWT)
446  if (WDG_INST_TYPE_SWT == instance->instType)
447  {
448  /* Disable SWT */
449  status = SWT_DRV_StopTimer(instance->instIdx);
450  /* Get counter value of SWT */
451  if (STATUS_SUCCESS == status)
452  {
453  /* Get counter value over SWT module */
454  (void)SWT_DRV_GetCounterValue(instance->instIdx, value);
455  /* Enable SWT */
456  (void)SWT_DRV_StartTimer(instance->instIdx);
457  }
458  }
459 #endif /* defined (WDG_OVER_SWT) */
460 
461  return status;
462 }
463 
464 /*FUNCTION**********************************************************************
465  *
466  * Function Name : WDG_ClearIntFlag
467  * Description : Clears the Timeout Interrupt Flag.
468  *
469  * Implements : WDG_ClearIntFlag_Activity
470  *END**************************************************************************/
471 void WDG_ClearIntFlag(const wdg_instance_t * const instance)
472 {
473  DEV_ASSERT(instance != NULL);
474 
475  /* Define WDG PAL over WDOG */
476 #if defined(WDG_OVER_WDOG)
477  if (WDG_INST_TYPE_WDOG == instance->instType)
478  {
479  /* Clears interrupt flag over WDOG module */
480  WDOG_DRV_ClearIntFlag(instance->instIdx);
481  }
482 #endif /* defined (WDG_OVER_WDOG) */
483 
484  /* Define WDG PAL over SWT */
485 #if defined(WDG_OVER_SWT)
486  if (WDG_INST_TYPE_SWT == instance->instType)
487  {
488  /* Clears interrupt flag over SWT module */
489  SWT_DRV_ClearIntFlag(instance->instIdx);
490  }
491 #endif /* defined (WDG_OVER_SWT) */
492 }
493 
494 /*******************************************************************************
495  * EOF
496  ******************************************************************************/
void WDG_Refresh(const wdg_instance_t *const instance)
Refreshes the WDG PAL counter.
Definition: wdg_pal.c:246
WDG PAL configuration structure Implements : wdg_config_t_Class.
Definition: wdg_pal.h:102
wdg_clock_source_t clkSource
Definition: wdg_pal.h:104
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
Structure storing PAL instance information.
bool winEnable
Definition: wdg_pal.h:109
status_t WDG_SetInt(const wdg_instance_t *const instance, bool enable)
Set interrupt for WDG PAL.
Definition: wdg_pal.c:319
uint32_t timeoutValue
Definition: wdg_pal.h:106
#define FEATURE_WDOG_TO_RESET_VALUE
status_t WDG_Deinit(const wdg_instance_t *const instance)
De-initializes the WDG PAL.
Definition: wdg_pal.c:285
status_t WDG_SetWindow(const wdg_instance_t *const instance, bool enable, uint32_t value)
Set window mode and window value of the WDG PAL.
Definition: wdg_pal.c:389
#define DEV_ASSERT(x)
Definition: devassert.h:77
WDOG user configuration structure Implements : wdog_user_config_t_Class.
Definition: wdog_driver.h:98
wdg_option_mode_t opMode
Definition: wdg_pal.h:105
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
uint16_t WDOG_DRV_GetCounter(uint32_t instance)
Gets the value of the WDOG counter.
Definition: wdog_driver.c:304
bool intEnable
Definition: wdg_pal.h:108
wdg_inst_type_t instType
status_t WDG_GetCounter(const wdg_instance_t *const instance, uint32_t *value)
Gets the value of the WDG PAL counter.
Definition: wdg_pal.c:427
status_t WDG_Init(const wdg_instance_t *const instance, const wdg_config_t *configPtr)
Initializes the WDG PAL.
Definition: wdg_pal.c:83
bool prescalerEnable
Definition: wdg_pal.h:110
status_t WDG_SetTimeout(const wdg_instance_t *const instance, uint32_t value)
Sets the value of the WDG PAL timeout.
Definition: wdg_pal.c:354
void WDG_ClearIntFlag(const wdg_instance_t *const instance)
Clears the Timeout Interrupt Flag.
Definition: wdg_pal.c:471
wdog_clk_source_t
Clock sources for the WDOG. Implements : wdog_clk_source_t_Class.
Definition: wdog_driver.h:52
void WDG_GetDefaultConfig(wdg_config_t *const config)
Gets default configuration of the WDG PAL.
Definition: wdg_pal.c:209
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
uint8_t percentWindow
Definition: wdg_pal.h:107
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