-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drivers: rtc: rpi_pico: Add support for the Raspberry Pi Pico RTC
This adds the minimal get_time/set_time support for the rp2040 and enables support by default on the Pico boards. This doesn't support configuring the clock source or alarm interrupts yet. Signed-off-by: Andrew Featherstone <andrew.featherstone@gmail.com>
- Loading branch information
Showing
9 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2023 Andrew Featherstone | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
config RTC_RPI_PICO | ||
bool "Raspberry Pi Pico RTC driver" | ||
default y | ||
depends on DT_HAS_RASPBERRYPI_PICO_RTC_ENABLED | ||
select PICOSDK_USE_RTC | ||
depends on RESET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
#include <zephyr/drivers/rtc.h> | ||
#include <zephyr/irq.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/logging/log.h> | ||
|
||
#include <hardware/rtc.h> | ||
|
||
#define DT_DRV_COMPAT raspberrypi_pico_rtc | ||
|
||
/* struct tm start time: 1st, Jan, 1900 */ | ||
#define TM_YEAR_REF 1900 | ||
|
||
struct rtc_rpi_pico_data { | ||
struct k_mutex lock; | ||
}; | ||
|
||
LOG_MODULE_REGISTER(rtc_rpi, CONFIG_RTC_LOG_LEVEL); | ||
|
||
static int rtc_rpi_pico_init(const struct device *dev) | ||
{ | ||
struct rtc_rpi_pico_data *data = dev->data; | ||
|
||
k_mutex_init(&data->lock); | ||
rtc_init(); | ||
return 0; | ||
} | ||
|
||
static int rtc_rpi_pico_set_time(const struct device *dev, const struct rtc_time *timeptr) | ||
{ | ||
struct rtc_rpi_pico_data *data = dev->data; | ||
|
||
int16_t real_year = (int16_t)timeptr->tm_year + TM_YEAR_REF; | ||
|
||
if (real_year > 4095) { | ||
return -EINVAL; | ||
} | ||
|
||
if (timeptr->tm_wday == -1) { | ||
/* day of the week is expected */ | ||
return -EINVAL; | ||
} | ||
|
||
LOG_INF("Setting clock"); | ||
|
||
datetime_t time = { | ||
.year = real_year, | ||
.month = timeptr->tm_mon + 1, | ||
.day = timeptr->tm_mday, | ||
.dotw = timeptr->tm_wday, | ||
.hour = timeptr->tm_hour, | ||
.min = timeptr->tm_min, | ||
.sec = timeptr->tm_sec, | ||
}; | ||
|
||
int err = k_mutex_lock(&data->lock, K_FOREVER); | ||
if (err) { | ||
return err; | ||
} | ||
if (!rtc_set_datetime(&time)) { | ||
err = -EINVAL; | ||
} | ||
k_mutex_unlock(&data->lock); | ||
|
||
return err; | ||
} | ||
|
||
static int rtc_rpi_pico_get_time(const struct device *dev, struct rtc_time *timeptr) | ||
{ | ||
datetime_t time; | ||
if (!rtc_get_datetime(&time)) { | ||
return -EINVAL; | ||
} | ||
|
||
timeptr->tm_sec = time.sec; | ||
timeptr->tm_min = time.min; | ||
timeptr->tm_hour = time.hour; | ||
timeptr->tm_mday = time.day; | ||
timeptr->tm_mon = time.month - 1; | ||
timeptr->tm_year = time.year - TM_YEAR_REF; | ||
timeptr->tm_wday = time.dotw; | ||
/* unknown values */ | ||
timeptr->tm_yday = -1; | ||
timeptr->tm_isdst = -1; | ||
timeptr->tm_nsec = 0; | ||
|
||
return 0; | ||
} | ||
|
||
static const struct rtc_driver_api rtc_rpi_pico_driver_api = { | ||
.set_time = rtc_rpi_pico_set_time, | ||
.get_time = rtc_rpi_pico_get_time, | ||
}; | ||
|
||
static struct rtc_rpi_pico_data rtc_data; | ||
|
||
DEVICE_DT_INST_DEFINE(0, &rtc_rpi_pico_init, NULL, &rtc_data, NULL, POST_KERNEL, | ||
CONFIG_RTC_INIT_PRIORITY, &rtc_rpi_pico_driver_api); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright (c) 2023 Andrew Featherstone <andrew.featherstone@gmail.com> | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
description: RaspberryPi Pico RTC | ||
|
||
compatible: "raspberrypi,pico-rtc" | ||
|
||
include: [rtc.yaml, reset-device.yaml] | ||
|
||
properties: | ||
reg: | ||
required: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters