em_rmu.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * @addtogroup EM_Library
  39. * @{
  40. ******************************************************************************/
  41. /***************************************************************************//**
  42. * @addtogroup RMU
  43. * @brief Reset Management Unit (RMU) Peripheral API
  44. * @{
  45. ******************************************************************************/
  46. /*******************************************************************************
  47. ************************** GLOBAL FUNCTIONS *******************************
  48. ******************************************************************************/
  49. /***************************************************************************//**
  50. * @brief
  51. * Disable/enable reset for various peripherals and signal sources
  52. *
  53. * @param[in] enable
  54. * @li false - Disable reset signal or flag
  55. * @li true - Enable reset signal or flag
  56. ******************************************************************************/
  57. void RMU_ResetControl(RMU_Reset_TypeDef reset, bool enable)
  58. {
  59. BITBAND_Peripheral(&(RMU->CTRL), (uint32_t)reset, (uint32_t)enable);
  60. }
  61. /***************************************************************************//**
  62. * @brief
  63. * Clear the reset cause register.
  64. ******************************************************************************/
  65. void RMU_ResetCauseClear(void)
  66. {
  67. uint32_t locked;
  68. RMU->CMD = RMU_CMD_RCCLR;
  69. /* Clear some reset causes not cleared with RMU CMD register */
  70. /* (If EMU registers locked, they must be unlocked first) */
  71. locked = EMU->LOCK & EMU_LOCK_LOCKKEY_LOCKED;
  72. if (locked)
  73. {
  74. EMU_Unlock();
  75. }
  76. BITBAND_Peripheral(&(EMU->AUXCTRL), 0, 1);
  77. BITBAND_Peripheral(&(EMU->AUXCTRL), 0, 0);
  78. if (locked)
  79. {
  80. EMU_Lock();
  81. }
  82. }
  83. /***************************************************************************//**
  84. * @brief
  85. * Get the cause of the last reset.
  86. *
  87. * @details
  88. * In order to be useful, the reset cause must be cleared by SW before a new
  89. * reset occurs, otherwise reset causes may accumulate. See
  90. * RMU_ResetCauseClear(). This function call will return the main cause for
  91. * reset, which can be a bit mask (several causes), and clear away "noise".
  92. *
  93. * @return
  94. * The reset cause, a bit mask of (typically, but not always, only one) of:
  95. * @li RMU_RSTCAUSE_PORST - Power on reset
  96. * @li RMU_RSTCAUSE_BODUNREGRST - Brown out detector, unregulated power
  97. * @li RMU_RSTCAUSE_BODREGRST - Brown out detector, regulated power
  98. * @li RMU_RSTCAUSE_EXTRST - External reset
  99. * @li RMU_RSTCAUSE_WDOGRST - Watchdog reset
  100. * @li RMU_RSTCAUSE_LOCKUPRST - Cortex-M3 lockup reset
  101. * @li RMU_RSTCAUSE_SYSREQRST - Cortex-M3 system request reset
  102. * @li RMU_RSTCAUSE_EM4RST - Set if the system has been in EM4
  103. * @li RMU_RSTCAUSE_EM4WURST - Set if the system woke up on a pin from EM4
  104. * @li RMU_RSTCAUSE_BODAVDD0 - Analog power domain 0 brown out detector reset
  105. * @li RMU_RSTCAUSE_BODAVDD1 - Analog power domain 1 brown out detector reset
  106. * @li RMU_RSTCAUSE_BUBODVDDDREG - Backup BOD on VDDD_REG triggered
  107. * @li RMU_RSTCAUSE_BUBODBUVIN - Backup BOD on BU_VIN triggered
  108. * @li RMU_RSTCAUSE_BUBODUNREG - Backup BOD on unregulated power triggered
  109. * @li RMU_RSTCAUSE_BUBODREG - Backup BOD on regulated powered has triggered
  110. * @li RMU_RSTCAUSE_BUMODERST - System has been in Backup mode
  111. ******************************************************************************/
  112. uint32_t RMU_ResetCauseGet(void)
  113. {
  114. uint32_t ret = RMU->RSTCAUSE;
  115. /* Inspect and decode bits. The decoding must be done in correct order, */
  116. /* since some reset causes may trigger other reset causes due to internal */
  117. /* design. We are only interested in the main cause. */
  118. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  119. /* Clear "stray" bits if EM4 bit is set, they will always be active */
  120. if (ret & RMU_RSTCAUSE_EM4RST)
  121. {
  122. ret &= ~(RMU_RSTCAUSE_BODREGRST|
  123. RMU_RSTCAUSE_BODUNREGRST|
  124. RMU_RSTCAUSE_LOCKUPRST|
  125. RMU_RSTCAUSE_SYSREQRST);
  126. }
  127. #endif
  128. if (ret & RMU_RSTCAUSE_PORST)
  129. {
  130. ret = RMU_RSTCAUSE_PORST;
  131. }
  132. else if ((ret & 0x83) == RMU_RSTCAUSE_BODUNREGRST)
  133. {
  134. ret = RMU_RSTCAUSE_BODUNREGRST;
  135. }
  136. else if ((ret & 0x1f) == RMU_RSTCAUSE_BODREGRST)
  137. {
  138. ret = RMU_RSTCAUSE_BODREGRST;
  139. }
  140. /* Both external and watchdog reset may occur at the same time */
  141. else if ((ret & 0x1b) & (RMU_RSTCAUSE_EXTRST | RMU_RSTCAUSE_WDOGRST))
  142. {
  143. ret &= RMU_RSTCAUSE_EXTRST | RMU_RSTCAUSE_WDOGRST;
  144. }
  145. /* Both lockup and system reset may occur at the same time */
  146. else if ((ret & 0x7ff) & (RMU_RSTCAUSE_LOCKUPRST | RMU_RSTCAUSE_SYSREQRST))
  147. {
  148. ret &= RMU_RSTCAUSE_LOCKUPRST | RMU_RSTCAUSE_SYSREQRST;
  149. }
  150. /* EM4 wake up and pin retention support */
  151. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  152. else if (ret & RMU_RSTCAUSE_BODAVDD0)
  153. {
  154. ret = RMU_RSTCAUSE_BODAVDD0;
  155. }
  156. else if (ret & RMU_RSTCAUSE_BODAVDD1)
  157. {
  158. ret = RMU_RSTCAUSE_BODAVDD1;
  159. }
  160. else if (ret & (RMU_RSTCAUSE_EM4WURST|RMU_RSTCAUSE_EM4RST))
  161. {
  162. ret &= (RMU_RSTCAUSE_EM4WURST|
  163. #if defined(_EFM32_GIANT_FAMILY)
  164. RMU_RSTCAUSE_BUMODERST|
  165. #endif
  166. RMU_RSTCAUSE_EM4RST);
  167. }
  168. else if (ret & (RMU_RSTCAUSE_EM4RST|RMU_RSTCAUSE_EXTRST))
  169. {
  170. ret &= (RMU_RSTCAUSE_EM4RST|RMU_RSTCAUSE_EXTRST);
  171. }
  172. #endif
  173. /* Backup power domain support */
  174. #if defined(_EFM32_GIANT_FAMILY)
  175. else if (ret & (RMU_RSTCAUSE_BUBODVDDDREG))
  176. {
  177. /* Keep backup mode flag, will only be present in this scenario */
  178. ret &= (RMU_RSTCAUSE_BUBODVDDDREG|RMU_RSTCAUSE_BUMODERST);
  179. }
  180. else if (ret & (RMU_RSTCAUSE_BUBODBUVIN))
  181. {
  182. ret &= (RMU_RSTCAUSE_BUBODBUVIN);
  183. }
  184. else if (ret & (RMU_RSTCAUSE_BUBODUNREG))
  185. {
  186. ret &= (RMU_RSTCAUSE_BUBODUNREG);
  187. }
  188. else if (ret & (RMU_RSTCAUSE_BUBODREG))
  189. {
  190. ret &= (RMU_RSTCAUSE_BUBODREG);
  191. }
  192. #endif
  193. else
  194. {
  195. ret = 0;
  196. }
  197. return ret;
  198. }
  199. /** @} (end addtogroup RMU) */
  200. /** @} (end addtogroup EM_Library) */