Skip to content

Commit b4c0ce3

Browse files
Ganesh RamachandranGanesh Ramachandran
authored andcommitted
Changes for RTC peripheral status
1 parent bcaeddc commit b4c0ce3

File tree

1 file changed

+16
-8
lines changed
  • targets/TARGET_TOSHIBA/TARGET_TMPM4G9

1 file changed

+16
-8
lines changed

targets/TARGET_TOSHIBA/TARGET_TMPM4G9/rtc_api.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
#define HEX2DEC(val) ((val >> 4U) * 10U + val % 16U) // Hex to Dec conversion macro
3535
#define DEC2HEX(val) ((val / 10U) * 16U + val % 10U) // Dec to Hex conversion macro
3636

37-
static int flag = 0;
37+
static int rtc_inited = 0;
3838
static int diff_year = 100; //our RTC register only support 2000~2099
3939
static void external_losc_enable(void);
4040

4141
void rtc_init(void)
4242
{
43-
if (!flag) {
43+
if (!rtc_inited) {
4444
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
4646
while ((TSB_RTC->RESTR & RTCRESTR_RSTTMR_MASK) == RTCRESTR_RSTTMR_R_RUN) {
4747
// Reset RTC sec counter
4848
}
@@ -64,24 +64,27 @@ void rtc_init(void)
6464
TSB_RTC->MINR = (uint8_t)0x02; // Set minute value
6565
TSB_RTC->SECR = (uint8_t)0x22; // Set second value
6666
TSB_RTC->PAGER |= RTC_CLK_ENABLE; // Enable Clock
67-
flag = 1; // Enable internal flag
67+
rtc_inited = 1; // Enable RTC initialzed status
6868
}
6969
}
7070

7171
void rtc_free(void)
7272
{
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
7674
}
7775

7876
int rtc_isenabled(void)
7977
{
80-
return flag; // Return a flag that represents status of RTC peripheral driver
78+
return rtc_inited; // Return status of RTC peripheral driver
8179
}
8280

8381
time_t rtc_read(void)
8482
{
83+
if (!rtc_inited) {
84+
// Return invalid time for now!
85+
return 0;
86+
}
87+
8588
struct tm timeinfo;
8689
uint8_t read_1 = 0U;
8790
uint8_t read_2 = 0U;
@@ -144,6 +147,11 @@ time_t rtc_read(void)
144147

145148
void rtc_write(time_t t)
146149
{
150+
if (!rtc_inited) {
151+
// Initialize the RTC as not yet initialized
152+
rtc_init();
153+
}
154+
147155
struct tm timeinfo;
148156
if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
149157
return;

0 commit comments

Comments
 (0)