S32 SDK
wdog_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016 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_driver.h"
48 
49 
50 /*******************************************************************************
51  * Variables
52  ******************************************************************************/
53 
56 
59 
60 /*******************************************************************************
61  * Private Functions
62  ******************************************************************************/
63 static status_t WDOG_DRV_Config(uint32_t instance, wdog_user_config_t wdogUserConfig);
64 
65 #ifdef DEV_ERROR_DETECT
66 /* Gets the frequency of the specified WDOG clock source. Only used at development
67  * time, when WDOG_DRV_Init checks if the needed clock sources are enabled. */
68 static uint32_t WDOG_DRV_GetClockSourceFreq(wdog_clk_source_t wdogClk)
69 {
70  uint32_t freq = 0;
71 
72  switch (wdogClk)
73  {
74  case WDOG_BUS_CLOCK:
75  (void)CLOCK_SYS_GetFreq(BUS_CLOCK, &freq);
76  break;
77  case WDOG_SIRC_CLOCK:
78  (void)CLOCK_SYS_GetFreq(SIRC_CLOCK, &freq);
79  break;
80  case WDOG_SOSC_CLOCK:
81  (void)CLOCK_SYS_GetFreq(SOSC_CLOCK, &freq);
82  break;
83  case WDOG_LPO_CLOCK:
84  (void)CLOCK_SYS_GetFreq(SIM_LPO_CLOCK, &freq);
85  break;
86  default:
87  /* Should not get here */
88  break;
89  }
90 
91  return freq;
92 }
93 #endif
94 
95 /*******************************************************************************
96  * Code
97  ******************************************************************************/
98 
99 /*FUNCTION**********************************************************************
100  *
101  * Function Name : WDOG_DRV_Init
102  * Description : initialize the WDOG driver
103  *
104  * Implements : WDOG_DRV_Init_Activity
105  *END**************************************************************************/
106 status_t WDOG_DRV_Init(uint32_t instance,
107  const wdog_user_config_t *userConfigPtr)
108 {
109  status_t status = STATUS_SUCCESS;
110 
111  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
112  DEV_ASSERT(userConfigPtr != NULL);
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_HAL_GetConfig(g_wdogBase[instance]).clkSource);
120  crtClockHz = WDOG_DRV_GetClockSourceFreq(userConfigPtr->clkSource);
121 
122  DEV_ASSERT((prevClockHz != 0U) && (crtClockHz != 0U));
123 #endif
124 
125  /* Configure module */
126  status = WDOG_DRV_Config(instance, *userConfigPtr);
127 
128  if (status == STATUS_SUCCESS)
129  {
130  /* enable WDOG timeout interrupt */
131  INT_SYS_EnableIRQ(g_wdogIrqId[instance]);
132  }
133 
134 
135  return status;
136 }
137 
138 
139 /*FUNCTION**********************************************************************
140  *
141  * Function Name : WDOG_DRV_Deinit
142  * Description : deinitialize the WDOG driver
143  *
144  * Implements : WDOG_DRV_Deinit_Activity
145  *END**************************************************************************/
146 void WDOG_DRV_Deinit(uint32_t instance)
147 {
148  WDOG_Type *baseAddr;
149 
150  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
151 
152  baseAddr = g_wdogBase[instance];
153 
155 
156  /* Disable WDOG */
157  WDOG_HAL_Disable(baseAddr);
158 
160 
161  /* Disable WDOG timeout interrupt */
162  INT_SYS_DisableIRQ(g_wdogIrqId[instance]);
163 }
164 
165 
166 /*FUNCTION**********************************************************************
167  *
168  * Function Name : WDOG_DRV_Config
169  * Description : config the WDOG driver
170  *
171  *END**************************************************************************/
172 static status_t WDOG_DRV_Config(uint32_t instance, wdog_user_config_t wdogUserConfig)
173 {
174  WDOG_Type *baseAddr;
175  status_t status = STATUS_SUCCESS;
176 
177  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
178 
179  baseAddr = g_wdogBase[instance];
180 
182 
183  if (WDOG_HAL_IsUpdateEnabled(baseAddr))
184  {
185  WDOG_HAL_Config(baseAddr, &wdogUserConfig);
186  while (WDOG_HAL_IsUnlocked(baseAddr))
187  {
188  /* Wait until the unlock window closes */
189  }
190  }
191  else
192  {
193  status = STATUS_ERROR;
194  }
195 
197 
198  return status;
199 }
200 
201 /*FUNCTION**********************************************************************
202  *
203  * Function Name : WDOG_DRV_GetConfig
204  * Description : get the current configuration of the WDOG driver
205  *
206  * Implements : WDOG_DRV_GetConfig_Activity
207  *END**************************************************************************/
208  void WDOG_DRV_GetConfig(uint32_t instance, wdog_user_config_t *config)
209 {
210  const WDOG_Type *baseAddr;
211 
212  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
213  DEV_ASSERT(config != NULL);
214 
215  baseAddr = g_wdogBase[instance];
216 
217  *config = WDOG_HAL_GetConfig(baseAddr);
218 }
219 
220 /*FUNCTION**********************************************************************
221  *
222  * Function Name : WDOG_DRV_SetInt
223  * Description : enable/disable the WDOG timeout interrupt and set handler
224  *
225  * Implements : WDOG_DRV_SetInt_Activity
226  *END**************************************************************************/
227  status_t WDOG_DRV_SetInt(uint32_t instance, bool enable, void (*handler)(void))
228 {
229  WDOG_Type *baseAddr;
230  status_t status = STATUS_SUCCESS;
231 
232  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
233  DEV_ASSERT(!enable || (handler != NULL));
234 
235  baseAddr = g_wdogBase[instance];
236 
237  if (enable && (handler != NULL))
238  {
239  INT_SYS_InstallHandler(g_wdogIrqId[instance], handler, (isr_t*) 0);
240  }
241 
242  if (WDOG_HAL_IsUpdateEnabled(baseAddr))
243  {
245 
246  WDOG_HAL_SetInt(baseAddr, enable);
247  while (WDOG_HAL_IsUnlocked(baseAddr))
248  {
249  /* Wait until the unlock window closes */
250  }
251 
253  }
254  else
255  {
256  status = STATUS_ERROR;
257  }
258 
259  return status;
260 }
261 
262 /*FUNCTION**********************************************************************
263  *
264  * Function Name : WDOG_DRV_Trigger
265  * Description : reset the WDOG counter
266  *
267  * Implements : WDOG_DRV_Trigger_Activity
268  *END**************************************************************************/
269  void WDOG_DRV_Trigger(uint32_t instance)
270 {
271  WDOG_Type *baseAddr;
272 
273  DEV_ASSERT(instance < WDOG_INSTANCE_COUNT);
274 
275  baseAddr = g_wdogBase[instance];
276 
277  WDOG_HAL_Trigger(baseAddr);
278 }
279 
280 /*******************************************************************************
281  * EOF
282  ******************************************************************************/
status_t WDOG_DRV_Init(uint32_t instance, const wdog_user_config_t *userConfigPtr)
Initializes the WDOG driver.
Definition: wdog_driver.c:106
void WDOG_HAL_Config(WDOG_Type *base, const wdog_user_config_t *config)
Configures all WDOG registers.
Definition: wdog_hal.c:64
#define WDOG_BASE_PTRS
Definition: S32K144.h:11281
wdog_user_config_t WDOG_HAL_GetConfig(const WDOG_Type *base)
Gets the current WDOG configuration.
Definition: wdog_hal.c:103
void WDOG_DRV_GetConfig(uint32_t instance, wdog_user_config_t *config)
Gets the current configuration of the WDOG.
Definition: wdog_driver.c:208
static void WDOG_HAL_Disable(WDOG_Type *base)
Disables the WDOG.
Definition: wdog_hal.h:195
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K144.h:269
static bool WDOG_HAL_IsUnlocked(const WDOG_Type *base)
Checks if the WDOG is unlocked.
Definition: wdog_hal.h:599
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:78
WDOG configuration structure Implements : wdog_user_config_t_Class.
Definition: wdog_hal.h:90
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_hal.h:91
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
WDOG_Type *const g_wdogBase[]
Table of base addresses for WDOG instances.
Definition: wdog_driver.c:55
wdog_clk_source_t
Clock sources for the WDOG. Implements : wdog_clk_source_t_Class.
Definition: wdog_hal.h:58
status_t WDOG_DRV_SetInt(uint32_t instance, bool enable, void(*handler)(void))
Enables/Disables the WDOG timeout interrupt and sets a function to be called when a timeout interrupt...
Definition: wdog_driver.c:227
void INT_SYS_EnableIRQGlobal(void)
Enables system interrupt.
void WDOG_DRV_Deinit(uint32_t instance)
De-initializes the WDOG driver.
Definition: wdog_driver.c:146
#define WDOG_INSTANCE_COUNT
Definition: S32K144.h:11270
void INT_SYS_EnableIRQ(IRQn_Type irqNumber)
Enables an interrupt for a given IRQ number.
static void WDOG_HAL_SetInt(WDOG_Type *base, bool enable)
Enables/Disables WDOG interrupt.
Definition: wdog_hal.h:347
static void WDOG_HAL_Trigger(WDOG_Type *base)
Refreshes the WDOG counter.
Definition: wdog_hal.h:208
#define WDOG_IRQS
Definition: S32K144.h:11287
void WDOG_DRV_Trigger(uint32_t instance)
Refreshes the WDOG counter.
Definition: wdog_driver.c:269
const IRQn_Type g_wdogIrqId[]
Table to save WDOG IRQ enum numbers defined in CMSIS header file.
Definition: wdog_driver.c:58
void(* isr_t)(void)
Interrupt handler type.
static status_t WDOG_DRV_Config(uint32_t instance, wdog_user_config_t wdogUserConfig)
Definition: wdog_driver.c:172
static bool WDOG_HAL_IsUpdateEnabled(const WDOG_Type *base)
Verifies if the WDOG updates are allowed.
Definition: wdog_hal.h:367
void INT_SYS_InstallHandler(IRQn_Type irqNumber, const isr_t newHandler, isr_t *const oldHandler)
Installs an interrupt handler routine for a given IRQ number.