1 /*
2  * Copyright (c) 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder 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"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRFX_RESET_REASON_H
33 #define NRFX_RESET_REASON_H
34 
35 #include <nrfx.h>
36 #include <hal/nrf_power.h>
37 
38 #if !NRF_POWER_HAS_RESETREAS
39 #include <hal/nrf_reset.h>
40 #endif
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /**
47  * @defgroup nrfx_reset_reason Generic Reset Reason layer
48  * @{
49  * @ingroup nrfx
50  * @ingroup nrf_power
51  * @ingroup nrf_reset
52  *
53  * @brief Helper layer that provides a uniform way of checking the reset reason.
54  */
55 
56 /** @brief Reset reason bit masks. */
57 typedef enum
58 {
59 #if !NRF_POWER_HAS_RESETREAS || defined(__NRFX_DOXYGEN__)
60     NRFX_RESET_REASON_RESETPIN_MASK  = RESET_RESETREAS_RESETPIN_Msk,
61     /**< Reset from pin-reset detected. */
62     NRFX_RESET_REASON_DOG0_MASK      = RESET_RESETREAS_DOG0_Msk,
63     /**< Reset from watchdog/application watchdong timer 0 detected. */
64     NRFX_RESET_REASON_DOG_MASK       = NRFX_RESET_REASON_DOG0_MASK,
65     /**< Reset from watchdog/application watchdong timer 0 detected. */
66     NRFX_RESET_REASON_CTRLAP_MASK    = RESET_RESETREAS_CTRLAP_Msk,
67     /**< Reset from application CTRL-AP detected. */
68     NRFX_RESETREAS_SREQ_MASK         = RESET_RESETREAS_SREQ_Msk,
69     /**< Reset from soft reset/application soft reset detected. */
70     NRFX_RESET_REASON_LOCKUP_MASK    = RESET_RESETREAS_LOCKUP_Msk,
71     /**< Reset from CPU lockup/application CPU lockup detected. */
72     NRFX_RESET_REASON_OFF_MASK       = RESET_RESETREAS_OFF_Msk,
73     /**< Reset due to wakeup from System OFF mode when wakeup is triggered by DETECT signal from
74      *   GPIO. */
75     NRFX_RESET_REASON_LPCOMP_MASK    = RESET_RESETREAS_LPCOMP_Msk,
76     /**< Reset due to wakeup from System OFF mode when wakeup is triggered by ANADETECT signal from
77      *   LPCOMP. */
78     NRFX_RESET_REASON_DIF_MASK       = RESET_RESETREAS_DIF_Msk,
79     /**< Reset due to wakeup from System OFF mode when wakeup is triggered by entering the debug
80      *   interface mode. */
81 #if NRF_RESET_HAS_NETWORK
82     NRFX_RESET_REASON_LSREQ_MASK     = RESET_RESETREAS_LSREQ_Msk,
83     /**< Reset from network soft reset detected. */
84     NRFX_RESET_REASON_LLOCKUP_MASK   = RESET_RESETREAS_LLOCKUP_Msk,
85     /**< Reset from network CPU lockup detected. */
86     NRFX_RESET_REASON_LDOG_MASK      = RESET_RESETREAS_LDOG_Msk,
87     /**< Reset from network watchdog timer detected. */
88     NRFX_RESET_REASON_MFORCEOFF_MASK = RESET_RESETREAS_MFORCEOFF_Msk,
89     /**< Force off reset from application core detected. */
90 #endif // NRF_RESET_HAS_NETWORK
91     NRFX_RESET_REASON_NFC_MASK       = RESET_RESETREAS_NFC_Msk,
92     /**< Reset after wakeup from System OFF mode due to NRF field being detected. */
93     NRFX_RESET_REASON_DOG1_MASK      = RESET_RESETREAS_DOG1_Msk,
94     /**< Reset from application watchdog timer 1 detected. */
95     NRFX_RESET_REASON_VBUS_MASK      = RESET_RESETREAS_VBUS_Msk,
96     /**< Reset after wakeup from System OFF mode due to VBUS rising into valid range. */
97 #if NRF_RESET_HAS_NETWORK
98     NRFX_RESET_REASON_LCTRLAP_MASK   = RESET_RESETREAS_LCTRLAP_Msk,
99     /**< Reset from network CTRL-AP detected. */
100 #endif // NRF_RESET_HAS_NETWORK
101 #else
102     NRFX_RESET_REASON_RESETPIN_MASK  = POWER_RESETREAS_RESETPIN_Msk,
103     NRFX_RESET_REASON_DOG_MASK       = POWER_RESETREAS_DOG_Msk,
104     NRFX_RESET_REASON_SREQ_MASK      = POWER_RESETREAS_SREQ_Msk ,
105     NRFX_RESET_REASON_LOCKUP_MASK    = POWER_RESETREAS_LOCKUP_Msk,
106     NRFX_RESET_REASON_OFF_MASK       = POWER_RESETREAS_OFF_Msk,
107 #if defined(POWER_RESETREAS_LPCOMP_Msk)
108     NRFX_RESET_REASON_LPCOMP_MASK    = POWER_RESETREAS_LPCOMP_Msk,
109 #endif
110     NRFX_RESET_REASON_DIF_MASK       = POWER_RESETREAS_DIF_Msk,
111 #if defined(POWER_RESETREAS_NFC_Msk)
112     NRFX_RESET_REASON_NFC_MASK       = POWER_RESETREAS_NFC_Msk,
113 #endif
114 #if defined(POWER_RESETREAS_VBUS_Msk)
115     NRFX_RESET_REASON_VBUS_MASK      = POWER_RESETREAS_VBUS_Msk,
116 #endif
117 #endif // !NRF_POWER_HAS_RESETREAS || defined(__NRFX_DOXYGEN__)
118 } nrfx_reset_reason_mask_t;
119 
120 /**
121  * @brief Function for getting the reset reason bitmask.
122  *
123  * Unless cleared, the RESETREAS register is cumulative.
124  * If none of the reset sources is flagged, the chip was reset from the on-chip reset generator.
125  * This indicates a power-on-reset or a brown out reset.
126  *
127  * @return Mask of reset reasons constructed from @ref nrfx_reset_reason_mask_t values.
128  */
nrfx_reset_reason_get(void)129 __STATIC_INLINE uint32_t nrfx_reset_reason_get(void)
130 {
131 #if NRF_POWER_HAS_RESETREAS
132     return nrf_power_resetreas_get(NRF_POWER);
133 #else
134     return nrf_reset_resetreas_get(NRF_RESET);
135 #endif
136 }
137 
138 /**
139  * @brief Function for clearing the selected reset reason fields.
140  *
141  * @param[in] mask Mask constructed from @ref nrfx_reset_reason_mask_t values.
142  */
nrfx_reset_reason_clear(uint32_t mask)143 __STATIC_INLINE void nrfx_reset_reason_clear(uint32_t mask)
144 {
145 #if NRF_POWER_HAS_RESETREAS
146     nrf_power_resetreas_clear(NRF_POWER, mask);
147 #else
148     nrf_reset_resetreas_clear(NRF_RESET, mask);
149 #endif
150 }
151 
152 /** @} */
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif // NRFX_RESET_REASON_H
159