Skip to content

Commit 61ad427

Browse files
Ganesh RamachandranGanesh Ramachandran
authored andcommitted
Changes for RTC peripheral status
1 parent 2fee8d7 commit 61ad427

File tree

1 file changed

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

1 file changed

+16
-8
lines changed

targets/TARGET_TOSHIBA/TARGET_TMPM3H6/rtc_api.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
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
TSB_CG_FSYSENB_IPENB03 = 1; // Enable Sys Clock for RTC
4545
external_losc_enable(); // Enable low-speed oscillator
46-
TSB_RTC->PAGER = 0x00; //disable clock and alarm
46+
TSB_RTC->PAGER = 0x00; // Disable clock and alarm
4747

4848
while ((TSB_RTC->RESTR & RTCRESTR_RSTTMR_MASK) == RTCRESTR_RSTTMR_R_RUN) {
4949
// Reset RTC sec counter
@@ -66,24 +66,27 @@ void rtc_init(void)
6666
TSB_RTC->MINR = (uint8_t)0x02; // Set minute value
6767
TSB_RTC->SECR = (uint8_t)0x22; // Set second value
6868
TSB_RTC->PAGER |= RTC_CLK_ENABLE; // Enable Clock
69-
flag = 1; // Enable internal flag
69+
rtc_inited = 1; // Enable RTC initialzed status
7070
}
7171
}
7272

7373
void rtc_free(void)
7474
{
75-
if (flag) { // Check status of RTC peripheral driver is ENABLE or DISABLE
76-
flag = 0; // Set status of RTC peripheral driver is DISABLE
77-
}
75+
rtc_inited = 0; // Set status of RTC peripheral driver as DISABLE
7876
}
7977

8078
int rtc_isenabled(void)
8179
{
82-
return flag; // Return a flag that represents status of RTC peripheral driver
80+
return rtc_inited; // Return status of RTC peripheral driver
8381
}
8482

8583
time_t rtc_read(void)
8684
{
85+
if (!rtc_inited) {
86+
// Return invalid time for now!
87+
return 0;
88+
}
89+
8790
struct tm timeinfo;
8891
uint8_t read_1 = 0U;
8992
uint8_t read_2 = 0U;
@@ -141,6 +144,11 @@ time_t rtc_read(void)
141144

142145
void rtc_write(time_t t)
143146
{
147+
if (!rtc_inited) {
148+
// Initialize the RTC as not yet initialized
149+
rtc_init();
150+
}
151+
144152
struct tm timeinfo;
145153
if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
146154
return;

0 commit comments

Comments
 (0)