EFM32 Gecko Software Documentation  efm32g-doc-5.1.2
util_sleep.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include "thunderboard/util.h"
17 #include "rtcdriver.h"
18 #include "em_emu.h"
19 
20 /***************************************************************************/
26 /***************************************************************************/
31 /* Local variables */
32 static volatile bool rtcComplete;
37 /***************************************************************************/
42 static void rtcCb( RTCDRV_TimerID_t id, void *user );
43 void SysTick_Handler( void );
44 
45 
46 /***************************************************************************/
53 uint32_t UTIL_sleepInit( void )
54 {
55  uint32_t stat;
56 
57  /* Allocate RTC timer and start driver */
58  stat = RTCDRV_Init();
59  if( stat != ECODE_EMDRV_RTCDRV_OK ){
60  return stat;
61  }
62 
63  stat = RTCDRV_AllocateTimer(&rtcId);
64 
65  return stat;
66 }
67 
68 /***************************************************************************/
78 void UTIL_sleep( uint32_t ms )
79 {
80  rtcComplete = false;
82 
83  while( !rtcComplete )
84  EMU_EnterEM2(true);
85 
86  return;
87 }
88 
89 /***************************************************************************/
100 uint32_t UTIL_waitForEvent( uint32_t timeout )
101 {
102  uint32_t remaining;
103 
104  rtcComplete = false;
106 
107  EMU_EnterEM2( true );
108 
109  if( rtcComplete ){
110  remaining = 0;
111  }
112  else {
113  RTCDRV_TimeRemaining( rtcId, &remaining );
114  }
115 
116  return remaining;
117 }
118 
119 /***************************************************************************/
132 static void rtcCb( RTCDRV_TimerID_t id, void *user )
133 {
134  rtcComplete = true;
135 }
136 
139 /***************************************************************************/
uint32_t UTIL_sleepInit(void)
Sets up the RTC timer used for sleep functions.
Definition: util_sleep.c:53
uint32_t RTCDRV_TimerID_t
Timer ID.
Definition: rtcdriver.h:47
#define ECODE_EMDRV_RTCDRV_OK
Success return value.
Definition: rtcdriver.h:39
Oneshot timer.
Definition: rtcdriver.h:67
void EMU_EnterEM2(bool restore)
Enter energy mode 2 (EM2).
Definition: em_emu.c:480
Ecode_t RTCDRV_AllocateTimer(RTCDRV_TimerID_t *id)
Allocate timer.
Definition: rtcdriver.c:231
Ecode_t RTCDRV_TimeRemaining(RTCDRV_TimerID_t id, uint32_t *timeRemaining)
Get time left before a given timer expires.
Definition: rtcdriver.c:711
RTCDRV timer API definition.
uint32_t UTIL_waitForEvent(uint32_t timeout)
Delays number of milliseconds in sleep mode (EM2) using the RTC but but returns if an event wakes up ...
Definition: util_sleep.c:100
static volatile bool rtcComplete
Definition: util_sleep.c:32
Ecode_t RTCDRV_Init(void)
Initialize RTCDRV driver.
Definition: rtcdriver.c:324
Utility Functions for the Thunderboard Sense.
static void rtcCb(RTCDRV_TimerID_t id, void *user)
RTC callback function, called when the timer expired.
Definition: util_sleep.c:132
Energy management unit (EMU) peripheral API.
Ecode_t RTCDRV_StartTimer(RTCDRV_TimerID_t id, RTCDRV_TimerType_t type, uint32_t timeout, RTCDRV_Callback_t callback, void *user)
Start a timer.
Definition: rtcdriver.c:515
void SysTick_Handler(void)
Interrupt Service Routine for system tick counter.
Definition: util.c:78
static RTCDRV_TimerID_t rtcId
Definition: util_sleep.c:33
void UTIL_sleep(uint32_t ms)
Delays number of milliseconds in sleep mode (EM2) using the RTC.
Definition: util_sleep.c:78