1 /*
2  * Copyright (c) 2019 - 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 NRF_RESET_H__
35 #define NRF_RESET_H__
36 
37 #include <nrfx.h>
38 #include <nrf_erratas.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @defgroup nrf_reset_hal RESET HAL
46  * @{
47  * @ingroup nrf_power
48  * @brief   Hardware access layer for managing the RESET peripheral.
49  */
50 
51 #if defined(NRF5340_XXAA_NETWORK) || defined(__NRFX_DOXYGEN__)
52 /** @brief Presence of Network core RESET functionality. */
53 #define NRF_RESET_HAS_NETWORK 1
54 #else
55 #define NRF_RESET_HAS_NETWORK 0
56 #endif
57 
58 #if defined(NRF5340_XXAA_APPLICATION) || defined(__NRFX_DOXYGEN__)
59 /** @brief Presence of Application core RESET functionality. */
60 #define NRF_RESET_HAS_APPLICATION 1
61 #else
62 #define NRF_RESET_HAS_APPLICATION 0
63 #endif
64 
65 #if defined(RESET_RESETREAS_VBUS_Msk) || defined(__NRFX_DOXYGEN__)
66 /** @brief Symbol indicating whether VBUS reset is present. */
67 #define NRF_RESET_HAS_VBUS_RESET 1
68 #else
69 #define NRF_RESET_HAS_VBUS_RESET 0
70 #endif
71 
72 #if defined(RESET_RESETREAS_CTRLAP_Msk) || defined(__NRFX_DOXYGEN__)
73 /** @brief Symbol indicating whether CTRAP reset is present. */
74 #define NRF_RESET_HAS_CTRLAP_RESET 1
75 #else
76 #define NRF_RESET_HAS_CTRLAP_RESET 0
77 #endif
78 
79 #if defined(RESET_RESETREAS_CTRLAPSOFT_Msk) || defined(__NRFX_DOXYGEN__)
80 /** @brief Symbol indicating whether CTRAPSOFT reset is present. */
81 #define NRF_RESET_HAS_CTRLAPSOFT_RESET 1
82 #else
83 #define NRF_RESET_HAS_CTRLAPSOFT_RESET 0
84 #endif
85 
86 #if defined(RESET_RESETREAS_CTRLAPHARD_Msk) || defined(__NRFX_DOXYGEN__)
87 /** @brief Symbol indicating whether CTRAPHARD reset is present. */
88 #define NRF_RESET_HAS_CTRLAPHARD_RESET 1
89 #else
90 #define NRF_RESET_HAS_CTRLAPHARD_RESET 0
91 #endif
92 
93 #if defined(RESET_RESETREAS_CTRLAPPIN_Msk) || defined(__NRFX_DOXYGEN__)
94 /** @brief Symbol indicating whether CTRAPPIN reset is present. */
95 #define NRF_RESET_HAS_CTRLAPPIN_RESET 1
96 #else
97 #define NRF_RESET_HAS_CTRLAPPIN_RESET 0
98 #endif
99 
100 #if defined(RESET_RESETREAS_LPCOMP_Msk) || defined(__NRFX_DOXYGEN__)
101 /** @brief Symbol indicating whether LPCOMP reset is present. */
102 #define NRF_RESET_HAS_LPCOMP_RESET 1
103 #else
104 #define NRF_RESET_HAS_LPCOMP_RESET 0
105 #endif
106 
107 #if defined(RESET_RESETREAS_LLOCKUP_Msk) || defined(__NRFX_DOXYGEN__)
108 /** @brief Symbol indicating whether LLOCKUP reset is present. */
109 #define NRF_RESET_HAS_LLOCKUP_RESET 1
110 #else
111 #define NRF_RESET_HAS_LLOCKUP_RESET 0
112 #endif
113 
114 #if defined(RESET_RESETREAS_LDOG_Msk) || defined(__NRFX_DOXYGEN__)
115 /** @brief Symbol indicating whether LDOG reset is present. */
116 #define NRF_RESET_HAS_LDOG_RESET 1
117 #else
118 #define NRF_RESET_HAS_LDOG_RESET 0
119 #endif
120 
121 #if defined(RESET_RESETREAS_MFORCEOFF_Msk) || defined(__NRFX_DOXYGEN__)
122 /** @brief Symbol indicating whether MFORCEOFF reset is present. */
123 #define NRF_RESET_HAS_MFORCEOFF_RESET 1
124 #else
125 #define NRF_RESET_HAS_MFORCEOFF_RESET 0
126 #endif
127 
128 #if defined(RESET_RESETREAS_GRTC_Msk) || defined(__NRFX_DOXYGEN__)
129 /** @brief Symbol indicating whether GRTC reset is present. */
130 #define NRF_RESET_HAS_GRTC_RESET 1
131 #else
132 #define NRF_RESET_HAS_GRTC_RESET 0
133 #endif
134 
135 #if defined(RESET_RESETREAS_SECTAMPER_Msk) || defined(__NRFX_DOXYGEN__)
136 /** @brief Symbol indicating whether SECTAMPER reset is present. */
137 #define NRF_RESET_HAS_SECTAMPER_RESET 1
138 #else
139 #define NRF_RESET_HAS_SECTAMPER_RESET 0
140 #endif
141 
142 /** @brief Reset reason bit masks. */
143 typedef enum
144 {
145     NRF_RESET_RESETREAS_RESETPIN_MASK   = RESET_RESETREAS_RESETPIN_Msk,   ///< Bit mask of RESETPIN field.
146     NRF_RESET_RESETREAS_DOG0_MASK       = RESET_RESETREAS_DOG0_Msk,       ///< Bit mask of DOG0 field.
147     NRF_RESET_RESETREAS_SREQ_MASK       = RESET_RESETREAS_SREQ_Msk,       ///< Bit mask of SREQ field.
148     NRF_RESET_RESETREAS_LOCKUP_MASK     = RESET_RESETREAS_LOCKUP_Msk,     ///< Bit mask of LOCKUP field.
149     NRF_RESET_RESETREAS_OFF_MASK        = RESET_RESETREAS_OFF_Msk,        ///< Bit mask of OFF field.
150     NRF_RESET_RESETREAS_DIF_MASK        = RESET_RESETREAS_DIF_Msk,        ///< Bit mask of DIF field.
151     NRF_RESET_RESETREAS_NFC_MASK        = RESET_RESETREAS_NFC_Msk,        ///< Bit mask of NFC field.
152     NRF_RESET_RESETREAS_DOG1_MASK       = RESET_RESETREAS_DOG1_Msk,       ///< Bit mask of DOG1 field.
153 #if NRF_RESET_HAS_CTRLAPSOFT_RESET
154     NRF_RESET_RESETREAS_CTRLAPSOFT_MASK = RESET_RESETREAS_CTRLAPSOFT_Msk, ///< Bit mask of CTRLAPSOFT field.
155 #endif
156 #if NRF_RESET_HAS_CTRLAPHARD_RESET
157     NRF_RESET_RESETREAS_CTRLAPHARD_MASK = RESET_RESETREAS_CTRLAPHARD_Msk, ///< Bit mask of CTRLAPHARD field.
158 #endif
159 #if NRF_RESET_HAS_CTRLAPPIN_RESET
160     NRF_RESET_RESETREAS_CTRLAPPIN_MASK  = RESET_RESETREAS_CTRLAPPIN_Msk,  ///< Bit mask of CTRLAPPIN field.
161 #endif
162 #if NRF_RESET_HAS_CTRLAP_RESET
163     NRF_RESET_RESETREAS_CTRLAP_MASK     = RESET_RESETREAS_CTRLAP_Msk,     ///< Bit mask of CTRLAP field.
164 #endif
165 #if NRF_RESET_HAS_LPCOMP_RESET
166     NRF_RESET_RESETREAS_LPCOMP_MASK     = RESET_RESETREAS_LPCOMP_Msk,     ///< Bit mask of LPCOMP field.
167 #endif
168 #if NRF_RESET_HAS_NETWORK
169     NRF_RESET_RESETREAS_LSREQ_MASK      = RESET_RESETREAS_LSREQ_Msk,      ///< Bit mask of LSREQ field.
170 #endif
171 #if NRF_RESET_HAS_LLOCKUP_RESET
172     NRF_RESET_RESETREAS_LLOCKUP_MASK    = RESET_RESETREAS_LLOCKUP_Msk,    ///< Bit mask of LLOCKUP field.
173 #endif
174 #if NRF_RESET_HAS_LDOG_RESET
175     NRF_RESET_RESETREAS_LDOG_MASK       = RESET_RESETREAS_LDOG_Msk,       ///< Bit mask of LDOG field.
176 #endif
177 #if NRF_RESET_HAS_MFORCEOFF_RESET
178     NRF_RESET_RESETREAS_MFORCEOFF_MASK  = RESET_RESETREAS_MFORCEOFF_Msk,  ///< Bit mask of MFORCEOFF field.
179 #endif
180 #if NRF_RESET_HAS_GRTC_RESET
181     NRF_RESET_RESETREAS_GRTC_MASK       = RESET_RESETREAS_GRTC_Msk,       ///< Bit mask of GRTC field.
182 #endif
183 #if NRF_RESET_HAS_VBUS_RESET
184     NRF_RESET_RESETREAS_VBUS_MASK       = RESET_RESETREAS_VBUS_Msk,       ///< Bit mask of VBUS field.
185 #endif
186 #if NRF_RESET_HAS_NETWORK
187     NRF_RESET_RESETREAS_LCTRLAP_MASK    = RESET_RESETREAS_LCTRLAP_Msk,    ///< Bit mask of LCTRLAP field.
188 #endif
189 #if NRF_RESET_HAS_SECTAMPER_RESET
190     NRF_RESET_RESETREAS_SECTAMPER_MASK  = RESET_RESETREAS_SECTAMPER_Msk,  ///< Bit mask of SECTAMPER field.
191 #endif
192 } nrf_reset_resetreas_mask_t;
193 
194 /**
195  * @brief Function for getting the reset reason bitmask.
196  *
197  * This function returns the reset reason bitmask.
198  * Unless cleared, the RESETREAS register is cumulative.
199  * If none of the reset sources is flagged, the chip was reset from the on-chip reset generator.
200  * This indicates a power-on-reset or a brown out reset.
201  *
202  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
203  *
204  * @return The mask of reset reasons constructed with @ref nrf_reset_resetreas_mask_t.
205  */
206 NRF_STATIC_INLINE uint32_t nrf_reset_resetreas_get(NRF_RESET_Type const * p_reg);
207 
208 /**
209  * @brief Function for clearing the selected reset reason field.
210  *
211  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
212  * @param[in] mask  The mask constructed from @ref nrf_reset_resetreas_mask_t enumerator values.
213  */
214 NRF_STATIC_INLINE void nrf_reset_resetreas_clear(NRF_RESET_Type * p_reg, uint32_t mask);
215 
216 #if NRF_RESET_HAS_APPLICATION
217 /**
218  * @brief Function for setting the force off signal for the Network core.
219  *
220  * A force off will reset the Network core and switch off its power and clocks.
221  *
222  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
223  * @param[in] hold  True if the force off signal is to be held.
224  *                  False if the force off signal is to be released.
225  */
226 NRF_STATIC_INLINE void nrf_reset_network_force_off(NRF_RESET_Type * p_reg, bool hold);
227 #endif // NRF_RESET_HAS_APPLICATION
228 
229 #ifndef NRF_DECLARE_ONLY
230 
nrf_reset_resetreas_get(NRF_RESET_Type const * p_reg)231 NRF_STATIC_INLINE uint32_t nrf_reset_resetreas_get(NRF_RESET_Type const * p_reg)
232 {
233     return p_reg->RESETREAS;
234 }
235 
nrf_reset_resetreas_clear(NRF_RESET_Type * p_reg,uint32_t mask)236 NRF_STATIC_INLINE void nrf_reset_resetreas_clear(NRF_RESET_Type * p_reg, uint32_t mask)
237 {
238     p_reg->RESETREAS = mask;
239 }
240 
241 #if NRF_RESET_HAS_APPLICATION
nrf_reset_network_force_off(NRF_RESET_Type * p_reg,bool hold)242 NRF_STATIC_INLINE void nrf_reset_network_force_off(NRF_RESET_Type * p_reg, bool hold)
243 {
244     if (hold)
245     {
246         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Hold <<
247                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
248     }
249     else if (nrf53_errata_161())
250     {
251         *(volatile uint32_t *)0x50005618UL = 1UL;
252         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release <<
253                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
254         NRFX_DELAY_US(5);
255         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Hold <<
256                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
257         NRFX_DELAY_US(1);
258         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release <<
259                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
260         *(volatile uint32_t *)0x50005618UL = 0UL;
261     }
262     else
263     {
264         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release <<
265                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
266     }
267 }
268 #endif // NRF_RESET_HAS_APPLICATION
269 
270 #endif // NRF_DECLARE_ONLY
271 
272 /** @} */
273 
274 #ifdef __cplusplus
275 }
276 #endif
277 
278 #endif // NRF_RESET_H__
279