Firmware SDK
twr_rtc.h
1 #ifndef _TWR_RTC_H
2 #define _TWR_RTC_H
3 
4 #include <time.h>
5 #include <stm32l0xx.h>
6 #include "twr_common.h"
7 
11 
12 // Note: For performance reasons, the TWR_RTC_PREDIV_S value should be a power of
13 // two.
14 #define TWR_RTC_PREDIV_S 256
15 #define TWR_RTC_PREDIV_A 128
16 
18 
20 
21 void twr_rtc_init(void);
22 
51 void twr_rtc_get_datetime(struct tm *tm);
52 
82 void twr_rtc_get_timestamp(struct timespec *tv);
83 
96 int twr_rtc_set_datetime(struct tm *tm, int ms);
97 
101 
102 uint32_t twr_rtc_datetime_to_timestamp(struct tm *tm);
103 
105 //
106 // This function supports nested invocations. If twr_rtc_enable_write has been
107 // called repeatedly, calling this function repeatedly will only lock the RTC
108 // again after all calls to twr_rtc_enable_write have been unrolled.
109 
110 static inline void twr_rtc_disable_write()
111 {
112  if (--_twr_rtc_writable_semaphore <= 0) {
114  RTC->WPR = 0xff;
115  }
116 }
117 
119 
120 static inline void twr_rtc_enable_write()
121 {
123  RTC->WPR = 0xca;
124  RTC->WPR = 0x53;
125 }
126 
137 static inline void twr_rtc_wait()
138 {
139  while(!(RTC->ISR & RTC_ISR_RSF));
140 }
141 
142 
145 
146 void twr_rtc_set_init(bool state);
147 
149 
150 #endif // _TWR_RTC_H
uint32_t twr_rtc_datetime_to_timestamp(struct tm *tm)
Convert date and time to UNIX timestamp.
Definition: twr_rtc.c:144
int _twr_rtc_writable_semaphore
Initialize real-time clock.
Definition: twr_rtc.c:72
void twr_rtc_get_timestamp(struct timespec *tv)
Definition: twr_rtc.c:218
void twr_rtc_get_datetime(struct tm *tm)
Definition: twr_rtc.c:173
int twr_rtc_set_datetime(struct tm *tm, int ms)
Definition: twr_rtc.c:270
void twr_rtc_set_init(bool state)
Enable or disable RTC initialization mode.
Definition: twr_rtc.c:328