34
34
#define HEX2DEC (val ) ((val >> 4U) * 10U + val % 16U) // Hex to Dec conversion macro
35
35
#define DEC2HEX (val ) ((val / 10U) * 16U + val % 10U) // Dec to Hex conversion macro
36
36
37
- static int flag = 0 ;
37
+ static int rtc_inited = 0 ;
38
38
static int diff_year = 100 ; //our RTC register only support 2000~2099
39
39
static void external_losc_enable (void );
40
40
41
41
void rtc_init (void )
42
42
{
43
- if (!flag ) {
43
+ if (!rtc_inited ) {
44
44
external_losc_enable (); // Enable low-speed oscillator
45
- TSB_RTC -> PAGER = 0x00 ; //disable clock and alarm
45
+ TSB_RTC -> PAGER = 0x00 ; // Disable clock and alarm
46
46
while ((TSB_RTC -> RESTR & RTCRESTR_RSTTMR_MASK ) == RTCRESTR_RSTTMR_R_RUN ) {
47
47
// Reset RTC sec counter
48
48
}
@@ -64,24 +64,27 @@ void rtc_init(void)
64
64
TSB_RTC -> MINR = (uint8_t )0x02 ; // Set minute value
65
65
TSB_RTC -> SECR = (uint8_t )0x22 ; // Set second value
66
66
TSB_RTC -> PAGER |= RTC_CLK_ENABLE ; // Enable Clock
67
- flag = 1 ; // Enable internal flag
67
+ rtc_inited = 1 ; // Enable RTC initialzed status
68
68
}
69
69
}
70
70
71
71
void rtc_free (void )
72
72
{
73
- if (flag ) { // Check status of RTC peripheral driver is ENABLE or DISABLE
74
- flag = 0 ; // Set status of RTC peripheral driver is DISABLE
75
- }
73
+ rtc_inited = 0 ; // Set status of RTC peripheral driver as DISABLE
76
74
}
77
75
78
76
int rtc_isenabled (void )
79
77
{
80
- return flag ; // Return a flag that represents status of RTC peripheral driver
78
+ return rtc_inited ; // Return status of RTC peripheral driver
81
79
}
82
80
83
81
time_t rtc_read (void )
84
82
{
83
+ if (!rtc_inited ) {
84
+ // Return invalid time for now!
85
+ return 0 ;
86
+ }
87
+
85
88
struct tm timeinfo ;
86
89
uint8_t read_1 = 0U ;
87
90
uint8_t read_2 = 0U ;
@@ -144,6 +147,11 @@ time_t rtc_read(void)
144
147
145
148
void rtc_write (time_t t )
146
149
{
150
+ if (!rtc_inited ) {
151
+ // Initialize the RTC as not yet initialized
152
+ rtc_init ();
153
+ }
154
+
147
155
struct tm timeinfo ;
148
156
if (_rtc_localtime (t , & timeinfo , RTC_4_YEAR_LEAP_YEAR_SUPPORT ) == false) {
149
157
return ;
0 commit comments