Skip to content

Commit 1800e0a

Browse files
pennamfacchinm
authored andcommitted
RP2040: Add basic RTC support
1 parent e15f031 commit 1800e0a

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/include/hardware/rtc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef void (*rtc_callback_t)(void);
3434
/*! \brief Initialise the RTC system
3535
* \ingroup hardware_rtc
3636
*/
37-
void rtc_init(void);
37+
void _rtc_init(void);
3838

3939
/*! \brief Set the RTC to the specified time
4040
* \ingroup hardware_rtc

targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/rtc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bool rtc_running(void) {
1919
return (rtc_hw->ctrl & RTC_CTRL_RTC_ACTIVE_BITS);
2020
}
2121

22-
void rtc_init(void) {
22+
void _rtc_init(void) {
2323
// Get clk_rtc freq and make sure it is running
2424
uint rtc_freq = clock_get_hz(clk_rtc);
2525
assert(rtc_freq != 0);
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#if DEVICE_RTC
2+
3+
#include "rtc_api.h"
4+
#include "hardware/rtc.h"
5+
#include "hardware/structs/rtc.h"
6+
#include "mbed_mktime.h"
7+
8+
void rtc_init(void)
9+
{
10+
_rtc_init();
11+
}
12+
13+
void rtc_free(void)
14+
{
15+
/* RTC clock can not be reset */
16+
}
17+
18+
int rtc_isenabled(void)
19+
{
20+
return rtc_running();
21+
}
22+
23+
time_t rtc_read(void)
24+
{
25+
struct tm timeinfo;
26+
time_t t;
27+
datetime_t date;
28+
29+
if (!rtc_get_datetime(&date)) {
30+
return 0;
31+
}
32+
33+
/* Setup a tm structure based on the RTC
34+
struct tm :
35+
tm_sec seconds after the minute 0-61
36+
tm_min minutes after the hour 0-59
37+
tm_hour hours since midnight 0-23
38+
tm_mday day of the month 1-31
39+
tm_mon months since January 0-11
40+
tm_year years since 1900
41+
tm_yday information is ignored by _rtc_maketime
42+
tm_wday information is ignored by _rtc_maketime
43+
tm_isdst information is ignored by _rtc_maketime
44+
*/
45+
timeinfo.tm_year = date.year - 1900;
46+
timeinfo.tm_mon = date.month - 1;
47+
timeinfo.tm_mday = date.day;
48+
timeinfo.tm_wday = date.dotw;
49+
timeinfo.tm_hour = date.hour;
50+
timeinfo.tm_min = date.min;
51+
timeinfo.tm_sec = date.sec;
52+
53+
if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
54+
return 0;
55+
}
56+
57+
return t;
58+
}
59+
60+
void rtc_write(time_t t)
61+
{
62+
struct tm timeinfo;
63+
datetime_t date;
64+
65+
if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
66+
return;
67+
}
68+
69+
/* Setup a datetime_t structure based on the RTC
70+
struct datetime_t
71+
year; 0..4095
72+
month; 1..12, 1 is January
73+
day; 1..28,29,30,31 depending on month
74+
dotw; 0..6, 0 is Sunday
75+
hour; 0..23
76+
min; 0..59
77+
sec; 0..59
78+
*/
79+
date.year = timeinfo.tm_year + 1900;
80+
date.month = timeinfo.tm_mon + 1;
81+
date.day = timeinfo.tm_mday;
82+
date.dotw = timeinfo.tm_wday;
83+
date.hour = timeinfo.tm_hour;
84+
date.min = timeinfo.tm_min;
85+
date.sec = timeinfo.tm_sec;
86+
87+
rtc_set_datetime(&date);
88+
return;
89+
}
90+
91+
#endif // DEVICE_RTC

targets/targets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8600,6 +8600,7 @@
86008600
"SERIAL",
86018601
"SERIAL_FC",
86028602
"SPI",
8603+
"RTC",
86038604
"USTICKER",
86048605
"WATCHDOG",
86058606
"USBDEVICE",

0 commit comments

Comments
 (0)