1 /*
2 * Copyright (c) 2020 - 2024, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef NRFX_RESET_REASON_H
35 #define NRFX_RESET_REASON_H
36
37 #include <nrfx.h>
38 #include <hal/nrf_power.h>
39
40 #if !NRF_POWER_HAS_RESETREAS
41 #include <hal/nrf_reset.h>
42 #endif
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 /**
49 * @defgroup nrfx_reset_reason Generic Reset Reason layer
50 * @{
51 * @ingroup nrfx
52 * @ingroup nrf_power
53 * @ingroup nrf_reset
54 *
55 * @brief Helper layer that provides a uniform way of checking the reset reason.
56 */
57
58 #if NRF_POWER_HAS_RESETREAS_CTRLAP || (defined(NRF_RESET) && NRF_RESET_HAS_CTRLAP_RESET) \
59 || defined(__NRFX_DOXYGEN__)
60 /** @brief Symbol indicating whether CTRAP reset reason is present. */
61 #define NRFX_RESET_REASON_HAS_CTRLAP 1
62 #else
63 #define NRFX_RESET_REASON_HAS_CTRLAP 0
64 #endif
65
66 #if (defined(NRF_RESET) && NRF_RESET_HAS_NETWORK) || defined(__NRFX_DOXYGEN__)
67 /** @brief Symbol indicating whether network reset reasons are present. */
68 #define NRFX_RESET_REASON_HAS_NETWORK 1
69 #else
70 #define NRFX_RESET_REASON_HAS_NETWORK 0
71 #endif
72
73 #if NRF_POWER_HAS_RESETREAS_LPCOMP || defined(RESET_RESETREAS_LPCOMP_Msk) || \
74 defined(__NRFX_DOXYGEN__)
75 /** @brief Symbol indicating whether LPCOMP reset reason is present. */
76 #define NRFX_RESET_REASON_HAS_LPCOMP 1
77 #else
78 #define NRFX_RESET_REASON_HAS_LPCOMP 0
79 #endif
80
81 #if NRF_POWER_HAS_RESETREAS_NFC || defined(RESET_RESETREAS_NFC_Msk) || defined(__NRFX_DOXYGEN__)
82 /** @brief Symbol indicating whether NFC reset reason is present. */
83 #define NRFX_RESET_REASON_HAS_NFC 1
84 #else
85 #define NRFX_RESET_REASON_HAS_NFC 0
86 #endif
87
88 #if NRF_POWER_HAS_RESETREAS_VBUS || (defined(NRF_RESET) && NRF_RESET_HAS_VBUS_RESET) \
89 || defined(__NRFX_DOXYGEN__)
90 /** @brief Symbol indicating whether VBUS reset reason is present. */
91 #define NRFX_RESET_REASON_HAS_VBUS 1
92 #else
93 #define NRFX_RESET_REASON_HAS_VBUS 0
94 #endif
95
96 #if (defined(NRF_RESET) && NRF_RESET_HAS_CTRLAPSOFT_RESET) || defined(__NRFX_DOXYGEN__)
97 /** @brief Symbol indicating whether CTRL-AP reset reason is present. */
98 #define NRFX_RESET_REASON_HAS_CTRLAPSOFT 1
99 #else
100 #define NRFX_RESET_REASON_HAS_CTRLAPSOFT 0
101 #endif
102
103 #if (defined(NRF_RESET) && NRF_RESET_HAS_CTRLAPHARD_RESET) || defined(__NRFX_DOXYGEN__)
104 /** @brief Symbol indicating whether CTRL-AP hard reset reason is present. */
105 #define NRFX_RESET_REASON_HAS_CTRLAPHARD 1
106 #else
107 #define NRFX_RESET_REASON_HAS_CTRLAPHARD 0
108 #endif
109
110 #if (defined(NRF_RESET) && NRF_RESET_HAS_CTRLAPPIN_RESET) || defined(__NRFX_DOXYGEN__)
111 /** @brief Symbol indicating whether CTRL-AP pin reset reason is present. */
112 #define NRFX_RESET_REASON_HAS_CTRLAPPIN 1
113 #else
114 #define NRFX_RESET_REASON_HAS_CTRLAPPIN 0
115 #endif
116
117 #if (defined(NRF_RESET) && NRF_RESET_HAS_GRTC_RESET) || defined(__NRFX_DOXYGEN__)
118 /** @brief Symbol indicating whether GRTC reset reason is present. */
119 #define NRFX_RESET_REASON_HAS_GRTC 1
120 #else
121 #define NRFX_RESET_REASON_HAS_GRTC 0
122 #endif
123
124 #if (defined(NRF_RESET) && NRF_RESET_HAS_SECTAMPER_RESET) || defined(__NRFX_DOXYGEN__)
125 /** @brief Symbol indicating whether GRTC reset reason is present. */
126 #define NRFX_RESET_REASON_HAS_SECTAMPER 1
127 #else
128 #define NRFX_RESET_REASON_HAS_SECTAMPER 0
129 #endif
130
131 /** @brief Reset reason bit masks. */
132 typedef enum
133 {
134 #if !NRF_POWER_HAS_RESETREAS || defined(__NRFX_DOXYGEN__)
135 /**< Reset from pin-reset detected. */
136 NRFX_RESET_REASON_RESETPIN_MASK = RESET_RESETREAS_RESETPIN_Msk,
137 /**< Reset from watchdog/application watchdog timer 0 detected. */
138 NRFX_RESET_REASON_DOG0_MASK = RESET_RESETREAS_DOG0_Msk,
139 /**< Reset from watchdog/application watchdog timer 0 detected. */
140 NRFX_RESET_REASON_DOG_MASK = NRFX_RESET_REASON_DOG0_MASK,
141 #if NRFX_RESET_REASON_HAS_CTRLAP
142 /**< Reset from application CTRL-AP detected. */
143 NRFX_RESET_REASON_CTRLAP_MASK = RESET_RESETREAS_CTRLAP_Msk,
144 #endif
145 /**< Reset from soft reset/application soft reset detected. Deprecated. */
146 NRFX_RESETREAS_SREQ_MASK = RESET_RESETREAS_SREQ_Msk,
147 /**< Reset from soft reset/application soft reset detected. */
148 NRFX_RESET_REASON_SREQ_MASK = RESET_RESETREAS_SREQ_Msk,
149 /**< Reset from CPU lockup/application CPU lockup detected. */
150 NRFX_RESET_REASON_LOCKUP_MASK = RESET_RESETREAS_LOCKUP_Msk,
151 /**< Reset due to wakeup from System OFF mode when wakeup is triggered by DETECT signal from
152 * GPIO. */
153 NRFX_RESET_REASON_OFF_MASK = RESET_RESETREAS_OFF_Msk,
154 /**< Reset due to wakeup from System OFF mode when wakeup is triggered by ANADETECT signal from
155 * LPCOMP. */
156 NRFX_RESET_REASON_LPCOMP_MASK = RESET_RESETREAS_LPCOMP_Msk,
157 /**< Reset due to wakeup from System OFF mode when wakeup is triggered by entering the debug
158 * interface mode. */
159 NRFX_RESET_REASON_DIF_MASK = RESET_RESETREAS_DIF_Msk,
160 #if NRFX_RESET_REASON_HAS_NETWORK
161 /**< Reset from network soft reset detected. */
162 NRFX_RESET_REASON_LSREQ_MASK = RESET_RESETREAS_LSREQ_Msk,
163 /**< Reset from network CPU lockup detected. */
164 NRFX_RESET_REASON_LLOCKUP_MASK = RESET_RESETREAS_LLOCKUP_Msk,
165 /**< Reset from network watchdog timer detected. */
166 NRFX_RESET_REASON_LDOG_MASK = RESET_RESETREAS_LDOG_Msk,
167 /**< Force off reset from application core detected. */
168 NRFX_RESET_REASON_MFORCEOFF_MASK = RESET_RESETREAS_MFORCEOFF_Msk,
169 #endif // NRFX_RESET_REASON_HAS_NETWORK
170 /**< Reset after wakeup from System OFF mode due to NFC field being detected. */
171 NRFX_RESET_REASON_NFC_MASK = RESET_RESETREAS_NFC_Msk,
172 /**< Reset from application watchdog timer 1 detected. */
173 NRFX_RESET_REASON_DOG1_MASK = RESET_RESETREAS_DOG1_Msk,
174 #if NRFX_RESET_REASON_HAS_VBUS
175 /**< Reset after wakeup from System OFF mode due to VBUS rising into valid range. */
176 NRFX_RESET_REASON_VBUS_MASK = RESET_RESETREAS_VBUS_Msk,
177 #endif
178 #if NRFX_RESET_REASON_HAS_NETWORK
179 /**< Reset from network CTRL-AP detected. */
180 NRFX_RESET_REASON_LCTRLAP_MASK = RESET_RESETREAS_LCTRLAP_Msk,
181 #endif // NRF_RESET_HAS_NETWORK
182 #if NRF_RESET_HAS_CTRLAPSOFT_RESET
183 /**< Soft reset from CTRL-AP detected. */
184 NRFX_RESET_REASON_CTRLAPSOFT_MASK = RESET_RESETREAS_CTRLAPSOFT_Msk,
185 #endif
186 #if NRFX_RESET_REASON_HAS_CTRLAPSOFT
187 /**< Reset due to CTRL-AP hard reset. */
188 NRFX_RESET_REASON_CTRLAPHARD_MASK = RESET_RESETREAS_CTRLAPHARD_Msk,
189 #endif
190 #if NRFX_RESET_REASON_HAS_CTRLAPPIN
191 /**< Reset due to CTRL-AP pin reset. */
192 NRFX_RESET_REASON_CTRLAPPIN_MASK = RESET_RESETREAS_CTRLAPPIN_Msk,
193 #endif
194 #if NRFX_RESET_REASON_HAS_GRTC
195 /**< Reset due to wakeup from GRTC. */
196 NRFX_RESET_REASON_GRTC_MASK = RESET_RESETREAS_GRTC_Msk,
197 #endif
198 #if NRFX_RESET_REASON_HAS_SECTAMPER
199 /**< Reset due to illegal tampering of the device. */
200 NRFX_RESET_REASON_SECTAMPER_MASK = RESET_RESETREAS_SECTAMPER_Msk,
201 #endif
202 #else
203 NRFX_RESET_REASON_RESETPIN_MASK = POWER_RESETREAS_RESETPIN_Msk,
204 NRFX_RESET_REASON_DOG_MASK = POWER_RESETREAS_DOG_Msk,
205 NRFX_RESET_REASON_SREQ_MASK = POWER_RESETREAS_SREQ_Msk ,
206 NRFX_RESET_REASON_LOCKUP_MASK = POWER_RESETREAS_LOCKUP_Msk,
207 NRFX_RESET_REASON_OFF_MASK = POWER_RESETREAS_OFF_Msk,
208 #if NRFX_RESET_REASON_HAS_CTRLAP
209 NRFX_RESET_REASON_CTRLAP_MASK = POWER_RESETREAS_CTRLAP_Msk,
210 #endif
211 #if NRFX_RESET_REASON_HAS_LPCOMP
212 NRFX_RESET_REASON_LPCOMP_MASK = POWER_RESETREAS_LPCOMP_Msk,
213 #endif
214 NRFX_RESET_REASON_DIF_MASK = POWER_RESETREAS_DIF_Msk,
215 #if NRFX_RESET_REASON_HAS_NFC
216 NRFX_RESET_REASON_NFC_MASK = POWER_RESETREAS_NFC_Msk,
217 #endif
218 #if NRFX_RESET_REASON_HAS_VBUS
219 NRFX_RESET_REASON_VBUS_MASK = POWER_RESETREAS_VBUS_Msk,
220 #endif
221 #endif // !NRF_POWER_HAS_RESETREAS || defined(__NRFX_DOXYGEN__)
222 } nrfx_reset_reason_mask_t;
223
224 /**
225 * @brief Function for getting the reset reason bitmask.
226 *
227 * Unless cleared, the RESETREAS register is cumulative.
228 * If none of the reset sources is flagged, the chip was reset from the on-chip reset generator.
229 * This indicates a power-on-reset or a brown out reset.
230 *
231 * @return Mask of reset reasons constructed from @ref nrfx_reset_reason_mask_t values.
232 */
nrfx_reset_reason_get(void)233 __STATIC_INLINE uint32_t nrfx_reset_reason_get(void)
234 {
235 #if NRF_POWER_HAS_RESETREAS
236 return nrf_power_resetreas_get(NRF_POWER);
237 #else
238 return nrf_reset_resetreas_get(NRF_RESET);
239 #endif
240 }
241
242 /**
243 * @brief Function for clearing the selected reset reason fields.
244 *
245 * @param[in] mask Mask constructed from @ref nrfx_reset_reason_mask_t values.
246 */
nrfx_reset_reason_clear(uint32_t mask)247 __STATIC_INLINE void nrfx_reset_reason_clear(uint32_t mask)
248 {
249 #if NRF_POWER_HAS_RESETREAS
250 nrf_power_resetreas_clear(NRF_POWER, mask);
251 #else
252 nrf_reset_resetreas_clear(NRF_RESET, mask);
253 #endif
254 }
255
256 /** @} */
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif // NRFX_RESET_REASON_H
263