1 /*
2  * Copyright (c) 2019 - 2023, 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 /** @brief Reset reason bit masks. */
66 typedef enum
67 {
68     NRF_RESET_RESETREAS_RESETPIN_MASK   = RESET_RESETREAS_RESETPIN_Msk,   ///< Bit mask of RESETPIN field.
69     NRF_RESET_RESETREAS_DOG0_MASK       = RESET_RESETREAS_DOG0_Msk,       ///< Bit mask of DOG0 field.
70     NRF_RESET_RESETREAS_SREQ_MASK       = RESET_RESETREAS_SREQ_Msk,       ///< Bit mask of SREQ field.
71     NRF_RESET_RESETREAS_LOCKUP_MASK     = RESET_RESETREAS_LOCKUP_Msk,     ///< Bit mask of LOCKUP field.
72     NRF_RESET_RESETREAS_OFF_MASK        = RESET_RESETREAS_OFF_Msk,        ///< Bit mask of OFF field.
73     NRF_RESET_RESETREAS_DIF_MASK        = RESET_RESETREAS_DIF_Msk,        ///< Bit mask of DIF field.
74     NRF_RESET_RESETREAS_NFC_MASK        = RESET_RESETREAS_NFC_Msk,        ///< Bit mask of NFC field.
75     NRF_RESET_RESETREAS_DOG1_MASK       = RESET_RESETREAS_DOG1_Msk,       ///< Bit mask of DOG1 field.
76 #if defined(RESET_RESETREAS_CTRLAPSOFT_Msk) || defined(__NRFX_DOXYGEN__)
77     NRF_RESET_RESETREAS_CTRLAPSOFT_MASK = RESET_RESETREAS_CTRLAPSOFT_Msk, ///< Bit mask of CTRLAPSOFT field.
78 #endif
79 #if defined(RESET_RESETREAS_CTRLAPHARD_Msk) || defined(__NRFX_DOXYGEN__)
80     NRF_RESET_RESETREAS_CTRLAPHARD_MASK = RESET_RESETREAS_CTRLAPHARD_Msk, ///< Bit mask of CTRLAPHARD field.
81 #endif
82 #if defined(RESET_RESETREAS_CTRLAPPIN_Msk) || defined(__NRFX_DOXYGEN__)
83     NRF_RESET_RESETREAS_CTRLAPPIN_MASK  = RESET_RESETREAS_CTRLAPPIN_Msk,  ///< Bit mask of CTRLAPPIN field.
84 #endif
85 #if defined(RESET_RESETREAS_CTRLAP_Msk) || defined(__NRFX_DOXYGEN__)
86     NRF_RESET_RESETREAS_CTRLAP_MASK     = RESET_RESETREAS_CTRLAP_Msk,     ///< Bit mask of CTRLAP field.
87 #endif
88 #if defined(RESET_RESETREAS_LPCOMP_Msk) || defined(__NRFX_DOXYGEN__)
89     NRF_RESET_RESETREAS_LPCOMP_MASK     = RESET_RESETREAS_LPCOMP_Msk,     ///< Bit mask of LPCOMP field.
90 #endif
91 #if NRF_RESET_HAS_NETWORK
92     NRF_RESET_RESETREAS_LSREQ_MASK      = RESET_RESETREAS_LSREQ_Msk,      ///< Bit mask of LSREQ field.
93 #endif
94 #if defined(RESET_RESETREAS_LLOCKUP_Msk) || defined(__NRFX_DOXYGEN__)
95     NRF_RESET_RESETREAS_LLOCKUP_MASK    = RESET_RESETREAS_LLOCKUP_Msk,    ///< Bit mask of LLOCKUP field.
96 #endif
97 #if defined(RESET_RESETREAS_LDOG_Msk) || defined(__NRFX_DOXYGEN__)
98     NRF_RESET_RESETREAS_LDOG_MASK       = RESET_RESETREAS_LDOG_Msk,       ///< Bit mask of LDOG field.
99 #endif
100 #if defined(RESET_RESETREAS_MFORCEOFF_Msk) || defined(__NRFX_DOXYGEN__)
101     NRF_RESET_RESETREAS_MFORCEOFF_MASK  = RESET_RESETREAS_MFORCEOFF_Msk,  ///< Bit mask of MFORCEOFF field.
102 #endif
103 #if defined(RESET_RESETREAS_GRTC_Msk) || defined(__NRFX_DOXYGEN__)
104     NRF_RESET_RESETREAS_GRTC_MASK       = RESET_RESETREAS_GRTC_Msk,       ///< Bit mask of GRTC field.
105 #endif
106 #if defined(RESET_RESETREAS_VBUS_Msk) || defined(__NRFX_DOXYGEN__)
107     NRF_RESET_RESETREAS_VBUS_MASK       = RESET_RESETREAS_VBUS_Msk,       ///< Bit mask of VBUS field.
108 #endif
109 #if NRF_RESET_HAS_NETWORK
110     NRF_RESET_RESETREAS_LCTRLAP_MASK    = RESET_RESETREAS_LCTRLAP_Msk,    ///< Bit mask of LCTRLAP field.
111 #endif
112 #if defined(RESET_RESETREAS_VMON_Msk) || defined(__NRFX_DOXYGEN__)
113     NRF_RESET_RESETREAS_VMON_MASK       = RESET_RESETREAS_VMON_Msk,       ///< Bit mask of VMON field.
114 #endif
115 #if defined(NRF_RESET_RESETREAS_EXT)
116     NRF_RESET_RESETREAS_EXT
117 #endif
118 } nrf_reset_resetreas_mask_t;
119 
120 /**
121  * @brief Function for getting the reset reason bitmask.
122  *
123  * This function returns the reset reason bitmask.
124  * Unless cleared, the RESETREAS register is cumulative.
125  * If none of the reset sources is flagged, the chip was reset from the on-chip reset generator.
126  * This indicates a power-on-reset or a brown out reset.
127  *
128  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
129  *
130  * @return The mask of reset reasons constructed with @ref nrf_reset_resetreas_mask_t.
131  */
132 NRF_STATIC_INLINE uint32_t nrf_reset_resetreas_get(NRF_RESET_Type const * p_reg);
133 
134 /**
135  * @brief Function for clearing the selected reset reason field.
136  *
137  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
138  * @param[in] mask  The mask constructed from @ref nrf_reset_resetreas_mask_t enumerator values.
139  */
140 NRF_STATIC_INLINE void nrf_reset_resetreas_clear(NRF_RESET_Type * p_reg, uint32_t mask);
141 
142 #if NRF_RESET_HAS_APPLICATION
143 /**
144  * @brief Function for setting the force off signal for the Network core.
145  *
146  * A force off will reset the Network core and switch off its power and clocks.
147  *
148  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
149  * @param[in] hold  True if the force off signal is to be held.
150  *                  False if the force off signal is to be released.
151  */
152 NRF_STATIC_INLINE void nrf_reset_network_force_off(NRF_RESET_Type * p_reg, bool hold);
153 #endif // NRF_RESET_HAS_APPLICATION
154 
155 #ifndef NRF_DECLARE_ONLY
156 
nrf_reset_resetreas_get(NRF_RESET_Type const * p_reg)157 NRF_STATIC_INLINE uint32_t nrf_reset_resetreas_get(NRF_RESET_Type const * p_reg)
158 {
159     return p_reg->RESETREAS;
160 }
161 
nrf_reset_resetreas_clear(NRF_RESET_Type * p_reg,uint32_t mask)162 NRF_STATIC_INLINE void nrf_reset_resetreas_clear(NRF_RESET_Type * p_reg, uint32_t mask)
163 {
164     p_reg->RESETREAS = mask;
165 }
166 
167 #if NRF_RESET_HAS_APPLICATION
nrf_reset_network_force_off(NRF_RESET_Type * p_reg,bool hold)168 NRF_STATIC_INLINE void nrf_reset_network_force_off(NRF_RESET_Type * p_reg, bool hold)
169 {
170     if (hold)
171     {
172         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Hold <<
173                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
174     }
175     else if (nrf53_errata_161())
176     {
177         *(volatile uint32_t *)0x50005618UL = 1UL;
178         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release <<
179                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
180         NRFX_DELAY_US(5);
181         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Hold <<
182                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
183         NRFX_DELAY_US(1);
184         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release <<
185                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
186         *(volatile uint32_t *)0x50005618UL = 0UL;
187     }
188     else
189     {
190         p_reg->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Release <<
191                                   RESET_NETWORK_FORCEOFF_FORCEOFF_Pos;
192     }
193 }
194 #endif // NRF_RESET_HAS_APPLICATION
195 
196 #endif // NRF_DECLARE_ONLY
197 
198 /** @} */
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 #endif // NRF_RESET_H__
205