1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef __WDOG_IMX_H__
32 #define __WDOG_IMX_H__
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "device_imx.h"
38
39 /*!
40 * @addtogroup wdog_imx_driver
41 * @{
42 */
43
44 /*******************************************************************************
45 * Definitions
46 ******************************************************************************/
47 /*! @brief The reset source of latest reset. */
48 enum _wdog_reset_source
49 {
50 wdogResetSourcePor = WDOG_WRSR_POR_MASK, /*!< Indicates the reset is the result of a power on reset.*/
51 wdogResetSourceTimeout = WDOG_WRSR_TOUT_MASK, /*!< Indicates the reset is the result of a WDOG timeout.*/
52 wdogResetSourceSwRst = WDOG_WRSR_SFTW_MASK, /*!< Indicates the reset is the result of a software reset.*/
53 };
54
55 /*! @brief Structure to configure the running mode. */
56 typedef struct _wdog_init_config
57 {
58 bool wdw; /*!< true: suspend in low power wait, false: not suspend */
59 bool wdt; /*!< true: assert WDOG_B when timeout, false: not assert WDOG_B */
60 bool wdbg; /*!< true: suspend in debug mode, false: not suspend */
61 bool wdzst; /*!< true: suspend in doze and stop mode, false: not suspend */
62 } wdog_init_config_t;
63
64 /*******************************************************************************
65 * API
66 ******************************************************************************/
67
68 #if defined(__cplusplus)
69 extern "C" {
70 #endif
71
72 /*!
73 * @name WDOG State Control
74 * @{
75 */
76
77 /*!
78 * @brief Configure WDOG functions, call once only
79 *
80 * @param base WDOG base pointer.
81 * @param initConfig WDOG mode configuration
82 */
WDOG_Init(WDOG_Type * base,const wdog_init_config_t * initConfig)83 static inline void WDOG_Init(WDOG_Type *base, const wdog_init_config_t *initConfig)
84 {
85 base->WCR |= (initConfig->wdw ? WDOG_WCR_WDW_MASK : 0) |
86 (initConfig->wdt ? WDOG_WCR_WDT_MASK : 0) |
87 (initConfig->wdbg ? WDOG_WCR_WDBG_MASK : 0) |
88 (initConfig->wdzst ? WDOG_WCR_WDZST_MASK : 0);
89 }
90
91 /*!
92 * @brief Enable WDOG with timeout, call once only
93 *
94 * @param base WDOG base pointer.
95 * @param timeout WDOG timeout ((n+1)/2 second)
96 */
97 void WDOG_Enable(WDOG_Type *base, uint8_t timeout);
98
99 /*!
100 * @brief Assert WDOG software reset signal
101 *
102 * @param base WDOG base pointer.
103 * @param wda WDOG reset.
104 * - true: Assert WDOG_B.
105 * - false: No impact on WDOG_B.
106 * @param srs System reset.
107 * - true: Assert system reset WDOG_RESET_B_DEB.
108 * - false: No impact on system reset.
109 */
110 void WDOG_Reset(WDOG_Type *base, bool wda, bool srs);
111
112 /*!
113 * @brief Get the latest reset source generated due to
114 * WatchDog Timer.
115 *
116 * @param base WDOG base pointer.
117 * @return The latest reset source (see @ref _wdog_reset_source enumeration).
118 */
WDOG_GetResetSource(WDOG_Type * base)119 static inline uint32_t WDOG_GetResetSource(WDOG_Type *base)
120 {
121 return base->WRSR;
122 }
123
124 /*!
125 * @brief Refresh the WDOG to prevent timeout
126 *
127 * @param base WDOG base pointer.
128 */
129 void WDOG_Refresh(WDOG_Type *base);
130
131 /*!
132 * @brief Disable WDOG power down counter
133 *
134 * @param base WDOG base pointer.
135 */
WDOG_DisablePowerdown(WDOG_Type * base)136 static inline void WDOG_DisablePowerdown(WDOG_Type *base)
137 {
138 base->WMCR &= ~WDOG_WMCR_PDE_MASK;
139 }
140
141 /*@}*/
142
143 /*!
144 * @name WDOG Interrupt Control
145 * @{
146 */
147
148 /*!
149 * @brief Enable WDOG interrupt
150 *
151 * @param base WDOG base pointer.
152 * @param time how long before the timeout must the interrupt occur (n/2 seconds).
153 */
WDOG_EnableInt(WDOG_Type * base,uint8_t time)154 static inline void WDOG_EnableInt(WDOG_Type *base, uint8_t time)
155 {
156 base->WICR = WDOG_WICR_WIE_MASK | WDOG_WICR_WICT(time);
157 }
158
159 /*!
160 * @brief Check whether WDOG interrupt is pending
161 *
162 * @param base WDOG base pointer.
163 * @return WDOG interrupt status.
164 * - true: Pending.
165 * - false: Not pending.
166 */
WDOG_IsIntPending(WDOG_Type * base)167 static inline bool WDOG_IsIntPending(WDOG_Type *base)
168 {
169 return (bool)(base->WICR & WDOG_WICR_WTIS_MASK);
170 }
171
172 /*!
173 * @brief Clear WDOG interrupt status
174 *
175 * @param base WDOG base pointer.
176 */
WDOG_ClearStatusFlag(WDOG_Type * base)177 static inline void WDOG_ClearStatusFlag(WDOG_Type *base)
178 {
179 base->WICR |= WDOG_WICR_WTIS_MASK;
180 }
181
182 /*@}*/
183
184 #if defined(__cplusplus)
185 }
186 #endif
187
188 /*! @}*/
189
190 #endif /* __WDOG_IMX_H__ */
191 /*******************************************************************************
192 * EOF
193 ******************************************************************************/
194