1 /***************************************************************************//**
2  * @file
3  * @brief Reset Management Unit (RMU) peripheral module peripheral API
4  *
5  * @author Energy Micro AS
6  * @version 3.0.0
7  *******************************************************************************
8  * @section License
9  * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
10  *******************************************************************************
11  *
12  * Permission is granted to anyone to use this software for any purpose,
13  * including commercial applications, and to alter it and redistribute it
14  * freely, subject to the following restrictions:
15  *
16  * 1. The origin of this software must not be misrepresented; you must not
17  *    claim that you wrote the original software.
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 3. This notice may not be removed or altered from any source distribution.
21  *
22  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
23  * obligation to support this Software. Energy Micro AS is providing the
24  * Software "AS IS", with no express or implied warranties of any kind,
25  * including, but not limited to, any implied warranties of merchantability
26  * or fitness for any particular purpose or warranties against infringement
27  * of any proprietary rights of a third party.
28  *
29  * Energy Micro AS will not be liable for any consequential, incidental, or
30  * special damages, or any other relief, or for any claim by any third party,
31  * arising from your use of this Software.
32  *
33  ******************************************************************************/
34 #include "em_rmu.h"
35 #include "em_emu.h"
36 #include "em_bitband.h"
37 
38 /***************************************************************************//**
39  * @addtogroup EM_Library
40  * @{
41  ******************************************************************************/
42 
43 /***************************************************************************//**
44  * @addtogroup RMU
45  * @brief Reset Management Unit (RMU) Peripheral API
46  * @{
47  ******************************************************************************/
48 
49 /*******************************************************************************
50  **************************   GLOBAL FUNCTIONS   *******************************
51  ******************************************************************************/
52 
53 /***************************************************************************//**
54  * @brief
55  *   Disable/enable reset for various peripherals and signal sources
56  *
57  * @param[in] enable
58  *   @li false - Disable reset signal or flag
59  *   @li true - Enable reset signal or flag
60  ******************************************************************************/
RMU_ResetControl(RMU_Reset_TypeDef reset,bool enable)61 void RMU_ResetControl(RMU_Reset_TypeDef reset, bool enable)
62 {
63   BITBAND_Peripheral(&(RMU->CTRL), (uint32_t)reset, (uint32_t)enable);
64 }
65 
66 
67 /***************************************************************************//**
68  * @brief
69  *   Clear the reset cause register.
70  ******************************************************************************/
RMU_ResetCauseClear(void)71 void RMU_ResetCauseClear(void)
72 {
73   uint32_t locked;
74 
75   RMU->CMD = RMU_CMD_RCCLR;
76 
77   /* Clear some reset causes not cleared with RMU CMD register */
78   /* (If EMU registers locked, they must be unlocked first) */
79   locked = EMU->LOCK & EMU_LOCK_LOCKKEY_LOCKED;
80   if (locked)
81   {
82     EMU_Unlock();
83   }
84 
85   BITBAND_Peripheral(&(EMU->AUXCTRL), 0, 1);
86   BITBAND_Peripheral(&(EMU->AUXCTRL), 0, 0);
87 
88   if (locked)
89   {
90     EMU_Lock();
91   }
92 }
93 
94 
95 /***************************************************************************//**
96  * @brief
97  *   Get the cause of the last reset.
98  *
99  * @details
100  *   In order to be useful, the reset cause must be cleared by SW before a new
101  *   reset occurs, otherwise reset causes may accumulate. See
102  *   RMU_ResetCauseClear(). This function call will return the main cause for
103  *   reset, which can be a bit mask (several causes), and clear away "noise".
104  *
105  * @return
106  *   The reset cause, a bit mask of (typically, but not always, only one) of:
107  *   @li RMU_RSTCAUSE_PORST - Power on reset
108  *   @li RMU_RSTCAUSE_BODUNREGRST - Brown out detector, unregulated power
109  *   @li RMU_RSTCAUSE_BODREGRST - Brown out detector, regulated power
110  *   @li RMU_RSTCAUSE_EXTRST - External reset
111  *   @li RMU_RSTCAUSE_WDOGRST - Watchdog reset
112  *   @li RMU_RSTCAUSE_LOCKUPRST - Cortex-M3 lockup reset
113  *   @li RMU_RSTCAUSE_SYSREQRST - Cortex-M3 system request reset
114  *   @li RMU_RSTCAUSE_EM4RST - Set if the system has been in EM4
115  *   @li RMU_RSTCAUSE_EM4WURST - Set if the system woke up on a pin from EM4
116  *   @li RMU_RSTCAUSE_BODAVDD0 - Analog power domain 0 brown out detector reset
117  *   @li RMU_RSTCAUSE_BODAVDD1 - Analog power domain 1 brown out detector reset
118  *   @li RMU_RSTCAUSE_BUBODVDDDREG - Backup BOD on VDDD_REG triggered
119  *   @li RMU_RSTCAUSE_BUBODBUVIN - Backup BOD on BU_VIN triggered
120  *   @li RMU_RSTCAUSE_BUBODUNREG - Backup BOD on unregulated power triggered
121  *   @li RMU_RSTCAUSE_BUBODREG - Backup BOD on regulated powered has triggered
122  *   @li RMU_RSTCAUSE_BUMODERST - System has been in Backup mode
123  ******************************************************************************/
RMU_ResetCauseGet(void)124 uint32_t RMU_ResetCauseGet(void)
125 {
126   uint32_t ret = RMU->RSTCAUSE;
127 
128   /* Inspect and decode bits. The decoding must be done in correct order, */
129   /* since some reset causes may trigger other reset causes due to internal */
130   /* design. We are only interested in the main cause. */
131 #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
132   /* Clear "stray" bits if EM4 bit is set, they will always be active */
133   if (ret & RMU_RSTCAUSE_EM4RST)
134   {
135     ret &= ~(RMU_RSTCAUSE_BODREGRST|
136              RMU_RSTCAUSE_BODUNREGRST|
137              RMU_RSTCAUSE_LOCKUPRST|
138              RMU_RSTCAUSE_SYSREQRST);
139   }
140 #endif
141   if (ret & RMU_RSTCAUSE_PORST)
142   {
143     ret = RMU_RSTCAUSE_PORST;
144   }
145   else if ((ret & 0x83) == RMU_RSTCAUSE_BODUNREGRST)
146   {
147     ret = RMU_RSTCAUSE_BODUNREGRST;
148   }
149   else if ((ret & 0x1f) == RMU_RSTCAUSE_BODREGRST)
150   {
151     ret = RMU_RSTCAUSE_BODREGRST;
152   }
153   /* Both external and watchdog reset may occur at the same time */
154   else if ((ret & 0x1b) & (RMU_RSTCAUSE_EXTRST | RMU_RSTCAUSE_WDOGRST))
155   {
156     ret &= RMU_RSTCAUSE_EXTRST | RMU_RSTCAUSE_WDOGRST;
157   }
158   /* Both lockup and system reset may occur at the same time */
159   else if ((ret & 0x7ff) & (RMU_RSTCAUSE_LOCKUPRST | RMU_RSTCAUSE_SYSREQRST))
160   {
161     ret &= RMU_RSTCAUSE_LOCKUPRST | RMU_RSTCAUSE_SYSREQRST;
162   }
163   /* EM4 wake up and pin retention support */
164 #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
165   else if (ret & RMU_RSTCAUSE_BODAVDD0)
166   {
167     ret = RMU_RSTCAUSE_BODAVDD0;
168   }
169   else if (ret & RMU_RSTCAUSE_BODAVDD1)
170   {
171     ret = RMU_RSTCAUSE_BODAVDD1;
172   }
173   else if (ret & (RMU_RSTCAUSE_EM4WURST|RMU_RSTCAUSE_EM4RST))
174   {
175     ret &= (RMU_RSTCAUSE_EM4WURST|
176 #if defined(_EFM32_GIANT_FAMILY)
177             RMU_RSTCAUSE_BUMODERST|
178 #endif
179             RMU_RSTCAUSE_EM4RST);
180   }
181   else if (ret & (RMU_RSTCAUSE_EM4RST|RMU_RSTCAUSE_EXTRST))
182   {
183     ret &= (RMU_RSTCAUSE_EM4RST|RMU_RSTCAUSE_EXTRST);
184   }
185 #endif
186   /* Backup power domain support */
187 #if defined(_EFM32_GIANT_FAMILY)
188   else if (ret & (RMU_RSTCAUSE_BUBODVDDDREG))
189   {
190     /* Keep backup mode flag, will only be present in this scenario */
191     ret &= (RMU_RSTCAUSE_BUBODVDDDREG|RMU_RSTCAUSE_BUMODERST);
192   }
193   else if (ret & (RMU_RSTCAUSE_BUBODBUVIN))
194   {
195     ret &= (RMU_RSTCAUSE_BUBODBUVIN);
196   }
197   else if (ret & (RMU_RSTCAUSE_BUBODUNREG))
198   {
199     ret &= (RMU_RSTCAUSE_BUBODUNREG);
200   }
201   else if (ret & (RMU_RSTCAUSE_BUBODREG))
202   {
203     ret &= (RMU_RSTCAUSE_BUBODREG);
204   }
205 #endif
206   else
207   {
208     ret = 0;
209   }
210   return ret;
211 }
212 
213 
214 /** @} (end addtogroup RMU) */
215 /** @} (end addtogroup EM_Library) */
216