Skip to content

Commit 96288e3

Browse files
committed
Simplify Ameba RTC driver
The Ameba RTC driver converts time_t to second/minute/hour/day/month/year in rtc_write and back to time_t in rtc_read. Replace this with an implementation which uses time_t directly.
1 parent 744b95c commit 96288e3

File tree

1 file changed

+5
-62
lines changed

1 file changed

+5
-62
lines changed

targets/TARGET_Realtek/TARGET_AMEBA/rtc_api.c

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,17 @@
1919
#if DEVICE_RTC
2020
#include <time.h>
2121
#include "timer_api.h" // software-RTC: use a g-timer for the tick of the RTC
22+
#include "mbed_mktime.h"
2223

2324
#define SW_RTC_TIMER_ID TIMER4
2425

2526
static gtimer_t sw_rtc;
26-
static struct tm rtc_timeinfo;
2727
static int sw_rtc_en=0;
28-
29-
static const u8 dim[14] = {
30-
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28 };
31-
32-
static inline bool is_leap_year(unsigned int year)
33-
{
34-
return (!(year % 4) && (year % 100)) || !(year % 400);
35-
}
36-
37-
38-
static u8 days_in_month (u8 month, u8 year)
39-
{
40-
u8 ret = dim [ month - 1 ];
41-
if (ret == 0)
42-
ret = is_leap_year (year) ? 29 : 28;
43-
return ret;
44-
}
28+
static time_t rtc_time;
4529

4630
void sw_rtc_tick_handler(uint32_t id)
4731
{
48-
if(++rtc_timeinfo.tm_sec > 59) { // Increment seconds, check for overflow
49-
rtc_timeinfo.tm_sec = 0; // Reset seconds
50-
if(++rtc_timeinfo.tm_min > 59) { // Increment minutes, check for overflow
51-
rtc_timeinfo.tm_min = 0; // Reset minutes
52-
if(++rtc_timeinfo.tm_hour > 23) { // Increment hours, check for overflow
53-
rtc_timeinfo.tm_hour = 0; // Reset hours
54-
++rtc_timeinfo.tm_yday; // Increment day of year
55-
if(++rtc_timeinfo.tm_wday > 6) // Increment day of week, check for overflow
56-
rtc_timeinfo.tm_wday = 0; // Reset day of week
57-
// Increment day of month, check for overflow
58-
if(++rtc_timeinfo.tm_mday >
59-
days_in_month(rtc_timeinfo.tm_mon, rtc_timeinfo.tm_year + 1900)) {
60-
rtc_timeinfo.tm_mday = 1; // Reset day of month
61-
if(++rtc_timeinfo.tm_mon > 11) { // Increment month, check for overflow
62-
rtc_timeinfo.tm_mon = 0; // Reset month
63-
rtc_timeinfo.tm_yday = 0; // Reset day of year
64-
++rtc_timeinfo.tm_year; // Increment year
65-
} // - year
66-
} // - month
67-
} // - day
68-
} // - hour
69-
}
32+
rtc_time++;
7033
}
7134

7235
void rtc_init(void)
@@ -92,35 +55,15 @@ int rtc_isenabled(void)
9255

9356
time_t rtc_read(void)
9457
{
95-
time_t t;
96-
97-
// Convert to timestamp
98-
t = mktime(&rtc_timeinfo);
99-
100-
return t;
58+
return rtc_time;
10159
}
10260

10361
void rtc_write(time_t t)
10462
{
105-
// Convert the time in to a tm
106-
struct tm *timeinfo = localtime(&t);
107-
108-
if (timeinfo == NULL) {
109-
// Error
110-
return;
111-
}
112-
11363
gtimer_stop(&sw_rtc);
11464

11565
// Set the RTC
116-
rtc_timeinfo.tm_sec = timeinfo->tm_sec;
117-
rtc_timeinfo.tm_min = timeinfo->tm_min;
118-
rtc_timeinfo.tm_hour = timeinfo->tm_hour;
119-
rtc_timeinfo.tm_mday = timeinfo->tm_mday;
120-
rtc_timeinfo.tm_wday = timeinfo->tm_wday;
121-
rtc_timeinfo.tm_yday = timeinfo->tm_yday;
122-
rtc_timeinfo.tm_mon = timeinfo->tm_mon;
123-
rtc_timeinfo.tm_year = timeinfo->tm_year;
66+
rtc_time = t;
12467

12568
gtimer_start(&sw_rtc);
12669
}

0 commit comments

Comments
 (0)