1 //*****************************************************************************
2 //
3 //  am_hal_wdt.h
4 //! @file
5 //!
6 //! @brief Hardware abstraction layer for the Watchdog Timer module.
7 //!
8 //! @addtogroup wdt2 Watchdog Timer (WDT)
9 //! @ingroup apollo2hal
10 //! @{
11 //
12 //*****************************************************************************
13 
14 //*****************************************************************************
15 //
16 // Copyright (c) 2017, Ambiq Micro
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are met:
21 //
22 // 1. Redistributions of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // 3. Neither the name of the copyright holder nor the names of its
30 // contributors may be used to endorse or promote products derived from this
31 // software without specific prior written permission.
32 //
33 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
37 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 // POSSIBILITY OF SUCH DAMAGE.
44 //
45 // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
46 //
47 //*****************************************************************************
48 #ifndef AM_HAL_WDT_H
49 #define AM_HAL_WDT_H
50 
51 #include <stdint.h>
52 #include <stdbool.h>
53 
54 //*****************************************************************************
55 //
56 // Macro definitions
57 //
58 //*****************************************************************************
59 
60 //*****************************************************************************
61 //
62 //! @name WDT Clock Divider Selections.
63 //! @brief Macro definitions for WDT clock frequencies.
64 //!
65 //! These macros may be used with the am_hal_wdt_config_t structure to set the
66 //! clock frequency of the watch dog timer.
67 //!
68 //! @{
69 //
70 //*****************************************************************************
71 #define AM_HAL_WDT_LFRC_CLK_DEFAULT AM_REG_WDT_CFG_CLKSEL_128HZ
72 #define AM_HAL_WDT_LFRC_CLK_128HZ   AM_REG_WDT_CFG_CLKSEL_128HZ
73 #define AM_HAL_WDT_LFRC_CLK_16HZ    AM_REG_WDT_CFG_CLKSEL_16HZ
74 #define AM_HAL_WDT_LFRC_CLK_1HZ     AM_REG_WDT_CFG_CLKSEL_1HZ
75 #define AM_HAL_WDT_LFRC_CLK_1_16HZ  AM_REG_WDT_CFG_CLKSEL_1_16HZ
76 #define AM_HAL_WDT_LFRC_CLK_OFF     AM_REG_WDT_CFG_CLKSEL_OFF
77 //! @}
78 
79 //*****************************************************************************
80 //
81 //! @name WDT Enable Reset in the WDT Configuration.
82 //! @brief Macro definitions for WDT Reset Enable.
83 //!
84 //! These macros may be used with the am_hal_wdt_config_t structure to enable
85 //! the watch dog timer to generate resets to the chip.
86 //!
87 //! @{
88 //
89 //*****************************************************************************
90 #define AM_HAL_WDT_ENABLE_RESET      AM_REG_WDT_CFG_RESEN(1)
91 #define AM_HAL_WDT_DISABLE_RESET     AM_REG_WDT_CFG_RESEN(0)
92 //! @}
93 
94 //*****************************************************************************
95 //
96 //! @name WDT Enable Interrupt Generation from the WDT Configuration.
97 //! @brief Macro definitions for WDT Interrupt Enable.
98 //!
99 //! These macros may be used with the am_hal_wdt_config_t structure to enable
100 //! the watch dog timer to generate generate WDT interrupts.
101 //!
102 //! @{
103 //
104 //*****************************************************************************
105 #define AM_HAL_WDT_ENABLE_INTERRUPT      AM_REG_WDT_CFG_INTEN(1)
106 #define AM_HAL_WDT_DISABLE_INTERRUPT     AM_REG_WDT_CFG_INTEN(0)
107 //! @}
108 
109 //*****************************************************************************
110 //
111 //! @brief Watchdog timer configuration structure.
112 //!
113 //! This structure is made to be used with the am_hal_wdt_init() function. It
114 //! describes the configuration of the watchdog timer.
115 //
116 //*****************************************************************************
117 typedef struct
118 {
119     //! Configuration Values for watchdog timer
120     //! event is generated.
121     uint32_t ui32Config;
122 
123     //! Number of watchdog timer ticks allowed before a watchdog interrupt
124     //! event is generated.
125     uint16_t ui16InterruptCount;
126 
127     //! Number of watchdog timer ticks allowed before the watchdog will issue a
128     //! system reset.
129     uint16_t ui16ResetCount;
130 
131 }
132 am_hal_wdt_config_t;
133 
134 
135 //*****************************************************************************
136 //
137 //! @brief Restarts the watchdog timer ("Pets" the dog)
138 //!
139 //! This function restarts the watchdog timer from the beginning, preventing
140 //! any interrupt or reset even from occuring until the next time the watchdog
141 //! timer expires.
142 //!
143 //! @return None.
144 //
145 //*****************************************************************************
146 #define am_hal_wdt_restart()                                                  \
147     do                                                                        \
148     {                                                                         \
149         AM_REGn(WDT, 0, RSTRT) = AM_REG_WDT_RSTRT_RSTRT_KEYVALUE;             \
150         (void)AM_REGn(WDT, 0, RSTRT);                                         \
151     }                                                                         \
152     while(0)
153 
154 #ifdef __cplusplus
155 extern "C"
156 {
157 #endif
158 //*****************************************************************************
159 //
160 // External function definitions
161 //
162 //*****************************************************************************
163 extern void am_hal_wdt_init(const am_hal_wdt_config_t *psConfig);
164 extern void am_hal_wdt_start(void);
165 extern void am_hal_wdt_halt(void);
166 extern void am_hal_wdt_lock_and_start(void);
167 extern void am_hal_wdt_int_enable(void);
168 extern uint32_t am_hal_wdt_int_enable_get(void);
169 extern void am_hal_wdt_int_disable(void);
170 extern void am_hal_wdt_int_clear(void);
171 extern void am_hal_wdt_int_set(void);
172 extern uint32_t am_hal_wdt_int_status_get(bool bEnabledOnly);
173 #ifdef __cplusplus
174 }
175 #endif
176 
177 #endif // AM_HAL_WDT_H
178 
179 //*****************************************************************************
180 //
181 // End Doxygen group.
182 //! @}
183 //
184 //*****************************************************************************
185