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 
36 #include <stdbool.h>
37 #include "em_part.h"
38 #include "em_bitband.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /***************************************************************************//**
45  * @addtogroup EM_Library
46  * @{
47  ******************************************************************************/
48 
49 /***************************************************************************//**
50  * @addtogroup EMU
51  * @{
52  ******************************************************************************/
53 
54 /*******************************************************************************
55  ********************************   ENUMS   ************************************
56  ******************************************************************************/
57 
58 #if defined(_EFM32_GIANT_FAMILY)
59 /** EM4 duty oscillator */
60 typedef enum
61 {
62   /** Select ULFRCO as duty oscillator in EM4 */
63   emuEM4Osc_ULFRCO = EMU_EM4CONF_OSC_ULFRCO,
64   /** Select LFXO as duty oscillator in EM4 */
65   emuEM4Osc_LFXO = EMU_EM4CONF_OSC_LFXO,
66   /** Select LFRCO as duty oscillator in EM4 */
67   emuEM4Osc_LFRCO = EMU_EM4CONF_OSC_LFRCO
68 } EMU_EM4Osc_TypeDef;
69 
70 /** Backup Power Voltage Probe types */
71 typedef enum
72 {
73   /** Disable voltage probe */
74   emuProbe_Disable = EMU_BUCTRL_PROBE_DISABLE,
75   /** Connect probe to VDD_DREG */
76   emuProbe_VDDDReg = EMU_BUCTRL_PROBE_VDDDREG,
77   /** Connect probe to BU_IN */
78   emuProbe_BUIN    = EMU_BUCTRL_PROBE_BUIN,
79   /** Connect probe to BU_OUT */
80   emuProbe_BUOUT   = EMU_BUCTRL_PROBE_BUOUT
81 } EMU_Probe_TypeDef;
82 
83 /** Backup Power Domain resistor selection */
84 typedef enum
85 {
86   /** Main power and backup power connected with RES0 series resistance */
87   emuRes_Res0 = EMU_PWRCONF_PWRRES_RES0,
88   /** Main power and backup power connected with RES1 series resistance */
89   emuRes_Res1 = EMU_PWRCONF_PWRRES_RES1,
90   /** Main power and backup power connected with RES2 series resistance */
91   emuRes_Res2 = EMU_PWRCONF_PWRRES_RES2,
92   /** Main power and backup power connected with RES3 series resistance */
93   emuRes_Res3 = EMU_PWRCONF_PWRRES_RES3,
94 } EMU_Resistor_TypeDef;
95 
96 /** Backup Power Domain power connection */
97 typedef enum
98 {
99   /** No connection between main and backup power */
100   emuPower_None = EMU_BUINACT_PWRCON_NONE,
101   /** Main power and backup power connected through diode,
102       allowing current from backup to main only */
103   emuPower_BUMain = EMU_BUINACT_PWRCON_BUMAIN,
104   /** Main power and backup power connected through diode,
105       allowing current from main to backup only */
106   emuPower_MainBU = EMU_BUINACT_PWRCON_MAINBU,
107   /** Main power and backup power connected without diode */
108   emuPower_NoDiode = EMU_BUINACT_PWRCON_NODIODE,
109 } EMU_Power_TypeDef;
110 
111 /** BOD threshold setting selector, active or inactive mode */
112 typedef enum
113 {
114   /** Configure BOD threshold for active mode */
115   emuBODMode_Active,
116   /** Configure BOD threshold for inactive mode */
117   emuBODMode_Inactive,
118 } EMU_BODMode_TypeDef;
119 #endif
120 
121 /*******************************************************************************
122  *******************************   STRUCTS   ***********************************
123  ******************************************************************************/
124 
125 #if defined(_EFM32_GIANT_FAMILY)
126 /** Energy Mode 4 initialization structure  */
127 typedef struct
128 {
129   /** Lock configuration of regulator, BOD and oscillator */
130   bool               lockConfig;
131   /** EM4 duty oscillator */
132   EMU_EM4Osc_TypeDef osc;
133   /** Wake up on EM4 BURTC interrupt */
134   bool               buRtcWakeup;
135   /** Enable EM4 voltage regulator */
136   bool               vreg;
137 } EMU_EM4Init_TypeDef;
138 
139 /** Default initialization of EM4 configuration */
140 #define EMU_EM4INIT_DEFAULT    \
141   {   false,             /* Dont't lock configuration after it's been set */ \
142       emuEM4Osc_ULFRCO,  /* Use default ULFRCO oscillator  */ \
143       true,              /* Wake up on EM4 BURTC interrupt */ \
144       true,              /* Enable VREG */ \
145   }
146 
147 /** Backup Power Domain Initialization structure */
148 typedef struct
149 {
150   /* Backup Power Domain power configuration */
151 
152   /** Voltage probe select, selects ADC voltage */
153   EMU_Probe_TypeDef probe;
154   /** Enable BOD calibration mode */
155   bool              bodCal;
156   /** Enable BU_STAT status pin for active BU mode */
157   bool              statusPinEnable;
158 
159   /* Backup Power Domain connection configuration */
160   /** Power domain resistor */
161   EMU_Resistor_TypeDef resistor;
162   /** BU_VOUT strong enable */
163   bool                 voutStrong;
164   /** BU_VOUT medium enable */
165   bool                 voutMed;
166   /** BU_VOUT weak enable */
167   bool                 voutWeak;
168   /** Power connection, when not in Backup Mode */
169   EMU_Power_TypeDef  inactivePower;
170   /** Power connection, when in Backup Mode */
171   EMU_Power_TypeDef  activePower;
172   /** Enable backup power domain, and release reset, enable BU_VIN pin  */
173   bool               enable;
174 } EMU_BUPDInit_TypeDef;
175 
176 /** Default */
177 #define EMU_BUPDINIT_DEFAULT                                                \
178   { emuProbe_Disable, /* Do not enable voltage probe */                     \
179     false,            /* Disable BOD calibration mode */                    \
180     false,            /* Disable BU_STAT pin for backup mode indication */  \
181                                                                             \
182     emuRes_Res0,      /* RES0 series resistance between main and backup power */ \
183     false,            /* Don't enable strong switch */                           \
184     false,            /* Don't enable medium switch */                           \
185     false,            /* Don't enable weak switch */                             \
186                                                                                  \
187     emuPower_None,    /* No connection between main and backup power (inactive mode) */  \
188     emuPower_None,    /* No connection between main and backup power (active mode) */    \
189     true              /* Enable BUPD enter on BOD, enable BU_VIN pin, release BU reset  */  \
190   }
191 #endif
192 
193 /*******************************************************************************
194  *****************************   PROTOTYPES   **********************************
195  ******************************************************************************/
196 
197 /***************************************************************************//**
198  * @brief
199  *   Enter energy mode 1 (EM1).
200  ******************************************************************************/
EMU_EnterEM1(void)201 __STATIC_INLINE void EMU_EnterEM1(void)
202 {
203   /* Just enter Cortex-M3 sleep mode */
204   SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
205   __WFI();
206 }
207 
208 
209 void EMU_EnterEM2(bool restore);
210 void EMU_EnterEM3(bool restore);
211 void EMU_EnterEM4(void);
212 void EMU_MemPwrDown(uint32_t blocks);
213 void EMU_UpdateOscConfig(void);
214 #if defined(_EFM32_GIANT_FAMILY)
215 void EMU_EM4Init(EMU_EM4Init_TypeDef *em4init);
216 void EMU_BUPDInit(EMU_BUPDInit_TypeDef *bupdInit);
217 void EMU_BUThresholdSet(EMU_BODMode_TypeDef mode, uint32_t value);
218 void EMU_BUThresRangeSet(EMU_BODMode_TypeDef mode, uint32_t value);
219 
220 /***************************************************************************//**
221  * @brief
222  *   Enable or disable EM4 lock configuration
223  * @param[in] enable
224  *   If true, locks down EM4 configuration
225  ******************************************************************************/
EMU_EM4Lock(bool enable)226 __STATIC_INLINE void EMU_EM4Lock(bool enable)
227 {
228   BITBAND_Peripheral(&(EMU->EM4CONF), _EMU_EM4CONF_LOCKCONF_SHIFT, enable);
229 }
230 
231 /***************************************************************************//**
232  * @brief
233  *   Halts until backup power functionality is ready
234  ******************************************************************************/
EMU_BUReady(void)235 __STATIC_INLINE void EMU_BUReady(void)
236 {
237   while(!(EMU->STATUS & EMU_STATUS_BURDY));
238 }
239 
240 /***************************************************************************//**
241  * @brief
242  *   Disable BU_VIN support
243  * @param[in] enable
244  *   If true, enables BU_VIN input pin support, if false disables it
245  ******************************************************************************/
EMU_BUPinEnable(bool enable)246 __STATIC_INLINE void EMU_BUPinEnable(bool enable)
247 {
248   BITBAND_Peripheral(&(EMU->ROUTE), _EMU_ROUTE_BUVINPEN_SHIFT, enable);
249 }
250 #endif
251 
252 /***************************************************************************//**
253  * @brief
254  *   Lock the EMU in order to protect all its registers against unintended
255  *   modification.
256  *
257  * @note
258  *   If locking the EMU registers, they must be unlocked prior to using any
259  *   EMU API functions modifying EMU registers. An exception to this is the
260  *   energy mode entering API (EMU_EnterEMn()), which can be used when the
261  *   EMU registers are locked.
262  ******************************************************************************/
EMU_Lock(void)263 __STATIC_INLINE void EMU_Lock(void)
264 {
265   EMU->LOCK = EMU_LOCK_LOCKKEY_LOCK;
266 }
267 
268 
269 /***************************************************************************//**
270  * @brief
271  *   Unlock the EMU so that writing to locked registers again is possible.
272  ******************************************************************************/
EMU_Unlock(void)273 __STATIC_INLINE void EMU_Unlock(void)
274 {
275   EMU->LOCK = EMU_LOCK_LOCKKEY_UNLOCK;
276 }
277 
278 /***************************************************************************//**
279  * @brief
280  *   Block entering EM2 or higher number energy modes.
281  ******************************************************************************/
EMU_EM2Block(void)282 __STATIC_INLINE void EMU_EM2Block(void)
283 {
284   BITBAND_Peripheral(&(EMU->CTRL), _EMU_CTRL_EM2BLOCK_SHIFT, 1U);
285 }
286 
287 /***************************************************************************//**
288  * @brief
289  *   Unblock entering EM2 or higher number energy modes.
290  ******************************************************************************/
EMU_EM2UnBlock(void)291 __STATIC_INLINE void EMU_EM2UnBlock(void)
292 {
293   BITBAND_Peripheral(&(EMU->CTRL), _EMU_CTRL_EM2BLOCK_SHIFT, 0U);
294 }
295 
296 
297 /** @} (end addtogroup EMU) */
298 /** @} (end addtogroup EM_Library) */
299 
300 #ifdef __cplusplus
301 }
302 #endif
303 
304 #endif /* __EM_EMU_H */
305