em_emu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Energy management unit (EMU) peripheral API
  4. * @author Energy Micro AS
  5. * @version 3.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  22. * obligation to support this Software. Energy Micro AS is providing the
  23. * Software "AS IS", with no express or implied warranties of any kind,
  24. * including, but not limited to, any implied warranties of merchantability
  25. * or fitness for any particular purpose or warranties against infringement
  26. * of any proprietary rights of a third party.
  27. *
  28. * Energy Micro AS will not be liable for any consequential, incidental, or
  29. * special damages, or any other relief, or for any claim by any third party,
  30. * arising from your use of this Software.
  31. *
  32. ******************************************************************************/
  33. #ifndef __EM_EMU_H
  34. #define __EM_EMU_H
  35. #include <stdbool.h>
  36. #include "em_part.h"
  37. #include "em_bitband.h"
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /***************************************************************************//**
  42. * @addtogroup EM_Library
  43. * @{
  44. ******************************************************************************/
  45. /***************************************************************************//**
  46. * @addtogroup EMU
  47. * @{
  48. ******************************************************************************/
  49. /*******************************************************************************
  50. ******************************** ENUMS ************************************
  51. ******************************************************************************/
  52. #if defined(_EFM32_GIANT_FAMILY)
  53. /** EM4 duty oscillator */
  54. typedef enum
  55. {
  56. /** Select ULFRCO as duty oscillator in EM4 */
  57. emuEM4Osc_ULFRCO = EMU_EM4CONF_OSC_ULFRCO,
  58. /** Select LFXO as duty oscillator in EM4 */
  59. emuEM4Osc_LFXO = EMU_EM4CONF_OSC_LFXO,
  60. /** Select LFRCO as duty oscillator in EM4 */
  61. emuEM4Osc_LFRCO = EMU_EM4CONF_OSC_LFRCO
  62. } EMU_EM4Osc_TypeDef;
  63. /** Backup Power Voltage Probe types */
  64. typedef enum
  65. {
  66. /** Disable voltage probe */
  67. emuProbe_Disable = EMU_BUCTRL_PROBE_DISABLE,
  68. /** Connect probe to VDD_DREG */
  69. emuProbe_VDDDReg = EMU_BUCTRL_PROBE_VDDDREG,
  70. /** Connect probe to BU_IN */
  71. emuProbe_BUIN = EMU_BUCTRL_PROBE_BUIN,
  72. /** Connect probe to BU_OUT */
  73. emuProbe_BUOUT = EMU_BUCTRL_PROBE_BUOUT
  74. } EMU_Probe_TypeDef;
  75. /** Backup Power Domain resistor selection */
  76. typedef enum
  77. {
  78. /** Main power and backup power connected with RES0 series resistance */
  79. emuRes_Res0 = EMU_PWRCONF_PWRRES_RES0,
  80. /** Main power and backup power connected with RES1 series resistance */
  81. emuRes_Res1 = EMU_PWRCONF_PWRRES_RES1,
  82. /** Main power and backup power connected with RES2 series resistance */
  83. emuRes_Res2 = EMU_PWRCONF_PWRRES_RES2,
  84. /** Main power and backup power connected with RES3 series resistance */
  85. emuRes_Res3 = EMU_PWRCONF_PWRRES_RES3,
  86. } EMU_Resistor_TypeDef;
  87. /** Backup Power Domain power connection */
  88. typedef enum
  89. {
  90. /** No connection between main and backup power */
  91. emuPower_None = EMU_BUINACT_PWRCON_NONE,
  92. /** Main power and backup power connected through diode,
  93. allowing current from backup to main only */
  94. emuPower_BUMain = EMU_BUINACT_PWRCON_BUMAIN,
  95. /** Main power and backup power connected through diode,
  96. allowing current from main to backup only */
  97. emuPower_MainBU = EMU_BUINACT_PWRCON_MAINBU,
  98. /** Main power and backup power connected without diode */
  99. emuPower_NoDiode = EMU_BUINACT_PWRCON_NODIODE,
  100. } EMU_Power_TypeDef;
  101. /** BOD threshold setting selector, active or inactive mode */
  102. typedef enum
  103. {
  104. /** Configure BOD threshold for active mode */
  105. emuBODMode_Active,
  106. /** Configure BOD threshold for inactive mode */
  107. emuBODMode_Inactive,
  108. } EMU_BODMode_TypeDef;
  109. #endif
  110. /*******************************************************************************
  111. ******************************* STRUCTS ***********************************
  112. ******************************************************************************/
  113. #if defined(_EFM32_GIANT_FAMILY)
  114. /** Energy Mode 4 initialization structure */
  115. typedef struct
  116. {
  117. /** Lock configuration of regulator, BOD and oscillator */
  118. bool lockConfig;
  119. /** EM4 duty oscillator */
  120. EMU_EM4Osc_TypeDef osc;
  121. /** Wake up on EM4 BURTC interrupt */
  122. bool buRtcWakeup;
  123. /** Enable EM4 voltage regulator */
  124. bool vreg;
  125. } EMU_EM4Init_TypeDef;
  126. /** Default initialization of EM4 configuration */
  127. #define EMU_EM4INIT_DEFAULT \
  128. { false, /* Dont't lock configuration after it's been set */ \
  129. emuEM4Osc_ULFRCO, /* Use default ULFRCO oscillator */ \
  130. true, /* Wake up on EM4 BURTC interrupt */ \
  131. true, /* Enable VREG */ \
  132. }
  133. /** Backup Power Domain Initialization structure */
  134. typedef struct
  135. {
  136. /* Backup Power Domain power configuration */
  137. /** Voltage probe select, selects ADC voltage */
  138. EMU_Probe_TypeDef probe;
  139. /** Enable BOD calibration mode */
  140. bool bodCal;
  141. /** Enable BU_STAT status pin for active BU mode */
  142. bool statusPinEnable;
  143. /* Backup Power Domain connection configuration */
  144. /** Power domain resistor */
  145. EMU_Resistor_TypeDef resistor;
  146. /** BU_VOUT strong enable */
  147. bool voutStrong;
  148. /** BU_VOUT medium enable */
  149. bool voutMed;
  150. /** BU_VOUT weak enable */
  151. bool voutWeak;
  152. /** Power connection, when not in Backup Mode */
  153. EMU_Power_TypeDef inactivePower;
  154. /** Power connection, when in Backup Mode */
  155. EMU_Power_TypeDef activePower;
  156. /** Enable backup power domain, and release reset, enable BU_VIN pin */
  157. bool enable;
  158. } EMU_BUPDInit_TypeDef;
  159. /** Default */
  160. #define EMU_BUPDINIT_DEFAULT \
  161. { emuProbe_Disable, /* Do not enable voltage probe */ \
  162. false, /* Disable BOD calibration mode */ \
  163. false, /* Disable BU_STAT pin for backup mode indication */ \
  164. \
  165. emuRes_Res0, /* RES0 series resistance between main and backup power */ \
  166. false, /* Don't enable strong switch */ \
  167. false, /* Don't enable medium switch */ \
  168. false, /* Don't enable weak switch */ \
  169. \
  170. emuPower_None, /* No connection between main and backup power (inactive mode) */ \
  171. emuPower_None, /* No connection between main and backup power (active mode) */ \
  172. true /* Enable BUPD enter on BOD, enable BU_VIN pin, release BU reset */ \
  173. }
  174. #endif
  175. /*******************************************************************************
  176. ***************************** PROTOTYPES **********************************
  177. ******************************************************************************/
  178. /***************************************************************************//**
  179. * @brief
  180. * Enter energy mode 1 (EM1).
  181. ******************************************************************************/
  182. __STATIC_INLINE void EMU_EnterEM1(void)
  183. {
  184. /* Just enter Cortex-M3 sleep mode */
  185. SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
  186. __WFI();
  187. }
  188. void EMU_EnterEM2(bool restore);
  189. void EMU_EnterEM3(bool restore);
  190. void EMU_EnterEM4(void);
  191. void EMU_MemPwrDown(uint32_t blocks);
  192. void EMU_UpdateOscConfig(void);
  193. #if defined(_EFM32_GIANT_FAMILY)
  194. void EMU_EM4Init(EMU_EM4Init_TypeDef *em4init);
  195. void EMU_BUPDInit(EMU_BUPDInit_TypeDef *bupdInit);
  196. void EMU_BUThresholdSet(EMU_BODMode_TypeDef mode, uint32_t value);
  197. void EMU_BUThresRangeSet(EMU_BODMode_TypeDef mode, uint32_t value);
  198. /***************************************************************************//**
  199. * @brief
  200. * Enable or disable EM4 lock configuration
  201. * @param[in] enable
  202. * If true, locks down EM4 configuration
  203. ******************************************************************************/
  204. __STATIC_INLINE void EMU_EM4Lock(bool enable)
  205. {
  206. BITBAND_Peripheral(&(EMU->EM4CONF), _EMU_EM4CONF_LOCKCONF_SHIFT, enable);
  207. }
  208. /***************************************************************************//**
  209. * @brief
  210. * Halts until backup power functionality is ready
  211. ******************************************************************************/
  212. __STATIC_INLINE void EMU_BUReady(void)
  213. {
  214. while(!(EMU->STATUS & EMU_STATUS_BURDY));
  215. }
  216. /***************************************************************************//**
  217. * @brief
  218. * Disable BU_VIN support
  219. * @param[in] enable
  220. * If true, enables BU_VIN input pin support, if false disables it
  221. ******************************************************************************/
  222. __STATIC_INLINE void EMU_BUPinEnable(bool enable)
  223. {
  224. BITBAND_Peripheral(&(EMU->ROUTE), _EMU_ROUTE_BUVINPEN_SHIFT, enable);
  225. }
  226. #endif
  227. /***************************************************************************//**
  228. * @brief
  229. * Lock the EMU in order to protect all its registers against unintended
  230. * modification.
  231. *
  232. * @note
  233. * If locking the EMU registers, they must be unlocked prior to using any
  234. * EMU API functions modifying EMU registers. An exception to this is the
  235. * energy mode entering API (EMU_EnterEMn()), which can be used when the
  236. * EMU registers are locked.
  237. ******************************************************************************/
  238. __STATIC_INLINE void EMU_Lock(void)
  239. {
  240. EMU->LOCK = EMU_LOCK_LOCKKEY_LOCK;
  241. }
  242. /***************************************************************************//**
  243. * @brief
  244. * Unlock the EMU so that writing to locked registers again is possible.
  245. ******************************************************************************/
  246. __STATIC_INLINE void EMU_Unlock(void)
  247. {
  248. EMU->LOCK = EMU_LOCK_LOCKKEY_UNLOCK;
  249. }
  250. /***************************************************************************//**
  251. * @brief
  252. * Block entering EM2 or higher number energy modes.
  253. ******************************************************************************/
  254. __STATIC_INLINE void EMU_EM2Block(void)
  255. {
  256. BITBAND_Peripheral(&(EMU->CTRL), _EMU_CTRL_EM2BLOCK_SHIFT, 1U);
  257. }
  258. /***************************************************************************//**
  259. * @brief
  260. * Unblock entering EM2 or higher number energy modes.
  261. ******************************************************************************/
  262. __STATIC_INLINE void EMU_EM2UnBlock(void)
  263. {
  264. BITBAND_Peripheral(&(EMU->CTRL), _EMU_CTRL_EM2BLOCK_SHIFT, 0U);
  265. }
  266. /** @} (end addtogroup EMU) */
  267. /** @} (end addtogroup EM_Library) */
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* __EM_EMU_H */