EFM32 Gecko Software Documentation  efm32g-doc-4.2.1
rtcdrv.c
Go to the documentation of this file.
1 /***************************************************************************/
18 #include <stddef.h>
19 #include "em_device.h"
20 #include "em_cmu.h"
21 #include "em_emu.h"
22 #include "em_rtc.h"
23 #include "rtcdrv.h"
24 
25 static void (*rtcCb)(void);
26 static uint8_t rtcInitialized = 0;
27 static volatile bool rtcDelayComplete;
28 static uint32_t rtcFreq;
30 /***************************************************************************/
33 static void DelayCB(void)
34 {
35  rtcDelayComplete = true;
36 }
37 
38 /***************************************************************************/
47 void RTCDRV_Setup(CMU_Select_TypeDef lfaClockSrc, CMU_ClkDiv_TypeDef rtcPrescale)
48 {
49  RTC_Init_TypeDef init;
50 
51  rtcInitialized = 1;
52 
53  /* Ensure LE modules are accessible */
55 
56  /* Enable LFACLK in CMU (will also enable oscillator if not enabled) */
57  CMU_ClockSelectSet(cmuClock_LFA, lfaClockSrc);
58 
59  /* Use the prescaler to reduce power consumption. */
60  CMU_ClockDivSet(cmuClock_RTC, rtcPrescale);
61 
62  rtcFreq = CMU_ClockFreqGet(cmuClock_RTC);
63 
64  /* Enable clock to RTC module */
66 
67  init.enable = false;
68  init.debugRun = false;
69  init.comp0Top = false; /* Count to max before wrapping */
70  RTC_Init(&init);
71 
72  /* Disable interrupt generation from RTC0 */
74 
75  /* Enable interrupts */
76  NVIC_ClearPendingIRQ(RTC_IRQn);
77  NVIC_EnableIRQ(RTC_IRQn);
78 }
79 
80 /***************************************************************************/
85 void RTCDRV_Delay(uint32_t msec, bool useEM2)
86 {
87  rtcDelayComplete = false;
88  RTCDRV_Trigger(msec, DelayCB);
89 
90  while (!rtcDelayComplete)
91  {
92  if (useEM2)
93  {
94  EMU_EnterEM2(true);
95  }
96  }
97 }
98 
99 /***************************************************************************/
102 void RTC_IRQHandler(void)
103 {
104  /* Disable RTC */
105  RTC_Enable(false);
106 
107  /* Clear interrupt source */
109 
110  /* Disable interrupt */
112 
113  /* Trigger callback if defined */
114  if (rtcCb)
115  {
116  rtcCb();
117  }
118 }
119 
120 
121 /***************************************************************************/
126 void RTCDRV_Trigger(uint32_t msec, void (*cb)(void))
127 {
128  /* Disable RTC - this will also reset the counter. */
129  RTC_Enable(false);
130 
131  /* Auto init if not configured already */
132  if (!rtcInitialized)
133  {
134  /* Default to LFRCO as clock source and prescale by 32. */
135  RTCDRV_Setup(cmuSelect_LFRCO, cmuClkDiv_32);
136  }
137 
138  /* Register callback */
139  rtcCb = cb;
140 
141  /* Clear interrupt source */
143 
144  /* Calculate trigger value in ticks based on 32768Hz clock */
145  RTC_CompareSet(0, (rtcFreq * msec) / 1000);
146 
147  /* Enable RTC */
148  RTC_Enable(true);
149 
150  /* Enable interrupt on COMP0 */
152 }
153 
Clock management unit (CMU) API.
void CMU_ClockSelectSet(CMU_Clock_TypeDef clock, CMU_Select_TypeDef ref)
Select reference clock/oscillator used for a clock branch.
Definition: em_cmu.c:2406
#define _RTC_IF_MASK
Definition: efm32g_rtc.h:110
__STATIC_INLINE void RTC_IntDisable(uint32_t flags)
Disable one or more RTC interrupts.
Definition: em_rtc.h:124
__STATIC_INLINE void RTC_IntClear(uint32_t flags)
Clear one or more pending RTC interrupts.
Definition: em_rtc.h:109
CMU_Select_TypeDef
Definition: em_cmu.h:950
void EMU_EnterEM2(bool restore)
Enter energy mode 2 (EM2).
Definition: em_emu.c:413
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
void RTC_CompareSet(unsigned int comp, uint32_t value)
Set RTC compare register value.
Definition: em_rtc.c:158
uint32_t CMU_ClkDiv_TypeDef
Definition: em_cmu.h:166
Real Time Counter (RTC) driver prototypes and definitions.
Ecode_t RTCDRV_Delay(uint32_t ms)
Millisecond delay function.
Definition: rtcdriver.c:266
bool debugRun
Definition: em_rtc.h:63
__STATIC_INLINE void RTC_IntEnable(uint32_t flags)
Enable one or more RTC interrupts.
Definition: em_rtc.h:144
bool comp0Top
Definition: em_rtc.h:64
void RTC_Init(const RTC_Init_TypeDef *init)
Initialize RTC.
Definition: em_rtc.c:302
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
Definition: em_cmu.c:1369
Real Time Counter (RTC) peripheral API.
Energy management unit (EMU) peripheral API.
void RTC_Enable(bool enable)
Enable/disable RTC.
Definition: em_rtc.c:214
#define cmuClkDiv_32
Definition: em_cmu.h:153
#define RTC_IF_COMP0
Definition: efm32g_rtc.h:116
uint32_t CMU_ClockFreqGet(CMU_Clock_TypeDef clock)
Get clock frequency for a clock point.
Definition: em_cmu.c:1482
void CMU_ClockDivSet(CMU_Clock_TypeDef clock, CMU_ClkDiv_TypeDef div)
Set clock divisor/prescaler.
Definition: em_cmu.c:1141