Skip to content

Commit 944e988

Browse files
committed
Add RP2040 RTC and Watchdog patches
1 parent 50991c1 commit 944e988

2 files changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
From 20801d7063780c551a38fb3be637c76d97bb0444 Mon Sep 17 00:00:00 2001
2+
From: pennam <[email protected]>
3+
Date: Fri, 30 Jul 2021 17:22:36 +0200
4+
Subject: [PATCH] RP2040: Add basic RTC support
5+
6+
---
7+
.../hardware_rtc/include/hardware/rtc.h | 2 +-
8+
.../pico-sdk/rp2_common/hardware_rtc/rtc.c | 2 +-
9+
.../TARGET_RP2040/rtc_api.c | 91 +++++++++++++++++++
10+
targets/targets.json | 1 +
11+
4 files changed, 94 insertions(+), 2 deletions(-)
12+
create mode 100644 targets/TARGET_RASPBERRYPI/TARGET_RP2040/rtc_api.c
13+
14+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/include/hardware/rtc.h b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/include/hardware/rtc.h
15+
index 83d5bdf288..dcdcd2285f 100644
16+
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/include/hardware/rtc.h
17+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/include/hardware/rtc.h
18+
@@ -34,7 +34,7 @@ typedef void (*rtc_callback_t)(void);
19+
/*! \brief Initialise the RTC system
20+
* \ingroup hardware_rtc
21+
*/
22+
-void rtc_init(void);
23+
+void _rtc_init(void);
24+
25+
/*! \brief Set the RTC to the specified time
26+
* \ingroup hardware_rtc
27+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/rtc.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/rtc.c
28+
index 91bd1994c1..ebd6783ba0 100644
29+
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/rtc.c
30+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_rtc/rtc.c
31+
@@ -19,7 +19,7 @@ bool rtc_running(void) {
32+
return (rtc_hw->ctrl & RTC_CTRL_RTC_ACTIVE_BITS);
33+
}
34+
35+
-void rtc_init(void) {
36+
+void _rtc_init(void) {
37+
// Get clk_rtc freq and make sure it is running
38+
uint rtc_freq = clock_get_hz(clk_rtc);
39+
assert(rtc_freq != 0);
40+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/rtc_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/rtc_api.c
41+
new file mode 100644
42+
index 0000000000..999f556355
43+
--- /dev/null
44+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/rtc_api.c
45+
@@ -0,0 +1,91 @@
46+
+#if DEVICE_RTC
47+
+
48+
+#include "rtc_api.h"
49+
+#include "hardware/rtc.h"
50+
+#include "hardware/structs/rtc.h"
51+
+#include "mbed_mktime.h"
52+
+
53+
+void rtc_init(void)
54+
+{
55+
+ _rtc_init();
56+
+}
57+
+
58+
+void rtc_free(void)
59+
+{
60+
+ /* RTC clock can not be reset */
61+
+}
62+
+
63+
+int rtc_isenabled(void)
64+
+{
65+
+ return rtc_running();
66+
+}
67+
+
68+
+time_t rtc_read(void)
69+
+{
70+
+ struct tm timeinfo;
71+
+ time_t t;
72+
+ datetime_t date;
73+
+
74+
+ if (!rtc_get_datetime(&date)) {
75+
+ return 0;
76+
+ }
77+
+
78+
+ /* Setup a tm structure based on the RTC
79+
+ struct tm :
80+
+ tm_sec seconds after the minute 0-61
81+
+ tm_min minutes after the hour 0-59
82+
+ tm_hour hours since midnight 0-23
83+
+ tm_mday day of the month 1-31
84+
+ tm_mon months since January 0-11
85+
+ tm_year years since 1900
86+
+ tm_yday information is ignored by _rtc_maketime
87+
+ tm_wday information is ignored by _rtc_maketime
88+
+ tm_isdst information is ignored by _rtc_maketime
89+
+ */
90+
+ timeinfo.tm_year = date.year - 1900;
91+
+ timeinfo.tm_mon = date.month - 1;
92+
+ timeinfo.tm_mday = date.day;
93+
+ timeinfo.tm_wday = date.dotw;
94+
+ timeinfo.tm_hour = date.hour;
95+
+ timeinfo.tm_min = date.min;
96+
+ timeinfo.tm_sec = date.sec;
97+
+
98+
+ if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
99+
+ return 0;
100+
+ }
101+
+
102+
+ return t;
103+
+}
104+
+
105+
+void rtc_write(time_t t)
106+
+{
107+
+ struct tm timeinfo;
108+
+ datetime_t date;
109+
+
110+
+ if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
111+
+ return;
112+
+ }
113+
+
114+
+ /* Setup a datetime_t structure based on the RTC
115+
+ struct datetime_t
116+
+ year; 0..4095
117+
+ month; 1..12, 1 is January
118+
+ day; 1..28,29,30,31 depending on month
119+
+ dotw; 0..6, 0 is Sunday
120+
+ hour; 0..23
121+
+ min; 0..59
122+
+ sec; 0..59
123+
+ */
124+
+ date.year = timeinfo.tm_year + 1900;
125+
+ date.month = timeinfo.tm_mon + 1;
126+
+ date.day = timeinfo.tm_mday;
127+
+ date.dotw = timeinfo.tm_wday;
128+
+ date.hour = timeinfo.tm_hour;
129+
+ date.min = timeinfo.tm_min;
130+
+ date.sec = timeinfo.tm_sec;
131+
+
132+
+ rtc_set_datetime(&date);
133+
+ return;
134+
+}
135+
+
136+
+#endif // DEVICE_RTC
137+
diff --git a/targets/targets.json b/targets/targets.json
138+
index 3150c838a1..b28205d060 100644
139+
--- a/targets/targets.json
140+
+++ b/targets/targets.json
141+
@@ -8600,6 +8600,7 @@
142+
"SERIAL",
143+
"SERIAL_FC",
144+
"SPI",
145+
+ "RTC",
146+
"USTICKER",
147+
"WATCHDOG",
148+
"USBDEVICE",
149+
--
150+
2.33.1
151+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
From 1f9ce44099f3ffa1500afa8cff735691bdaee96a Mon Sep 17 00:00:00 2001
2+
From: pennam <[email protected]>
3+
Date: Tue, 9 Nov 2021 09:20:36 +0100
4+
Subject: [PATCH] RP2040: Watchdog: get_reload_value returns 0 if wd is not
5+
properly configured
6+
7+
---
8+
.../TARGET_RP2040/watchdog_api.c | 15 ++++++++++-----
9+
1 file changed, 10 insertions(+), 5 deletions(-)
10+
11+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/watchdog_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/watchdog_api.c
12+
index 9263eb48d5..7526f6b608 100644
13+
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/watchdog_api.c
14+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/watchdog_api.c
15+
@@ -4,16 +4,18 @@
16+
17+
#if DEVICE_WATCHDOG
18+
19+
-static watchdog_config_t watchdogConfig;
20+
+static watchdog_config_t watchdogConfig = {
21+
+ 0 // timeout_ms
22+
+};
23+
24+
watchdog_status_t hal_watchdog_init(const watchdog_config_t *config)
25+
{
26+
- watchdogConfig = *config;
27+
// The pico watchdogs accept a maximum value of 0x7fffff
28+
if ( config->timeout_ms < 0x1 && config->timeout_ms > 0x7FFFFF ) {
29+
return WATCHDOG_STATUS_INVALID_ARGUMENT;
30+
}
31+
32+
+ watchdogConfig = *config;
33+
watchdog_enable(config->timeout_ms, true);
34+
35+
return WATCHDOG_STATUS_OK;
36+
@@ -32,9 +34,12 @@ watchdog_status_t hal_watchdog_stop(void)
37+
38+
uint32_t hal_watchdog_get_reload_value(void)
39+
{
40+
- uint32_t load_value = watchdogConfig.timeout_ms * 1000 * 2;
41+
- if (load_value > 0xffffffu) {
42+
- load_value = 0xffffffu;
43+
+ uint32_t load_value = 0;
44+
+ if ( watchdogConfig.timeout_ms > 0 ) {
45+
+ load_value = watchdogConfig.timeout_ms * 1000 * 2;
46+
+ if (load_value > 0xffffffu) {
47+
+ load_value = 0xffffffu;
48+
+ }
49+
}
50+
return load_value;
51+
}
52+
--
53+
2.33.1
54+

0 commit comments

Comments
 (0)