|
| 1 | +From 20801d7063780c551a38fb3be637c76d97bb0444 Mon Sep 17 00:00:00 2001 |
| 2 | + |
| 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 | + |
0 commit comments