-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This sample app set and read date/time from the Real-Time Clock. Signed-off-by: Muhammad Waleed Badar <[email protected]>
- Loading branch information
1 parent
77ebf82
commit 9a80457
Showing
8 changed files
with
169 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(rtc) | ||
|
||
FILE(GLOB app_sources src/*.c) | ||
target_sources(app PRIVATE ${app_sources}) |
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,34 @@ | ||
.. zephyr:code-sample:: rtc | ||
:name: Real-Time Clock (RTC) | ||
:relevant-api: rtc_interface | ||
|
||
Set and read the date/time from a Real-Time Clock. | ||
|
||
Overview | ||
******** | ||
|
||
This sample shows how to use the :ref:`rtc driver API <rtc_api>` | ||
to set and read the date/time from RTC and display on the console | ||
and can be built and executed on boards supporting RTC. | ||
|
||
Building and Running | ||
******************** | ||
|
||
Build and flash as follows, replacing ``stm32f3_disco`` with your board: | ||
|
||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/drivers/rtc | ||
:board: stm32f3_disco | ||
:goals: build flash | ||
:compact: | ||
|
||
Sample Output | ||
============= | ||
|
||
.. code-block:: console | ||
RTC date and time: 2024-11-17 04:21:47 | ||
RTC date and time: 2024-11-17 04:21:48 | ||
RTC date and time: 2024-11-17 04:21:49 | ||
<repeats endlessly> |
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,18 @@ | ||
/* | ||
* Copyright (c) 2023 Bjarki Arge Andreasen | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
* The RTC IRQ is not routed to the IOAPIC if the legacy | ||
* IRQ bit is set. The IRQ is required for alarm | ||
* operation and the update callback. | ||
*/ | ||
&hpet { | ||
no-legacy-irq; | ||
}; | ||
|
||
&rtc { | ||
status = "okay"; | ||
}; |
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,18 @@ | ||
/* | ||
* Copyright (c) 2023 Bjarki Arge Andreasen | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
* The RTC IRQ is not routed to the IOAPIC if the legacy | ||
* IRQ bit is set. The IRQ is required for alarm | ||
* operation and the update callback. | ||
*/ | ||
&hpet { | ||
no-legacy-irq; | ||
}; | ||
|
||
&rtc { | ||
status = "okay"; | ||
}; |
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,11 @@ | ||
/* | ||
* Copyright (c) 2024 STMicroelectronics | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/ { | ||
aliases { | ||
rtc = &rtc; | ||
}; | ||
}; |
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,2 @@ | ||
# Enable Peripheral | ||
CONFIG_RTC=y |
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 @@ | ||
sample: | ||
name: Real-Time Clock Sample | ||
tests: | ||
sample.drivers.rtc: | ||
platform_allow: | ||
- stm32f3_disco | ||
tags: | ||
- samples | ||
- rtc | ||
- api | ||
depends_on: | ||
- rtc |
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,66 @@ | ||
/* | ||
* Copyright (c) 2024, Muhammad Waleed Badar | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/kernel.h> | ||
#include <zephyr/device.h> | ||
#include <zephyr/drivers/rtc.h> | ||
#include <zephyr/sys/util.h> | ||
|
||
const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc)); | ||
|
||
static int set_date_time(const struct device *rtc) | ||
{ | ||
int ret = 0; | ||
struct rtc_time tm = { | ||
.tm_year = 2024 - 1900, | ||
.tm_mon = 11 - 1, | ||
.tm_mday = 17, | ||
.tm_hour = 4, | ||
.tm_min = 19, | ||
.tm_sec = 0, | ||
}; | ||
|
||
ret = rtc_set_time(rtc, &tm); | ||
if (ret < 0) { | ||
printk("Cannot write date time: %d\n", ret); | ||
return ret; | ||
} | ||
return ret; | ||
} | ||
|
||
static int get_date_time(const struct device *rtc) | ||
{ | ||
int ret = 0; | ||
struct rtc_time tm; | ||
|
||
ret = rtc_get_time(rtc, &tm); | ||
if (ret < 0) { | ||
printk("Cannot read date time: %d\n", ret); | ||
return ret; | ||
} | ||
|
||
printk("RTC date and time: %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, | ||
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); | ||
|
||
return ret; | ||
} | ||
|
||
int main(void) | ||
{ | ||
/* Check if the RTC is ready */ | ||
if (!device_is_ready(rtc)) { | ||
printk("Device is not ready\n"); | ||
return 0; | ||
} | ||
|
||
set_date_time(rtc); | ||
|
||
/* Continuously read the current date and time from the RTC */ | ||
while (get_date_time(rtc) == 0) { | ||
k_sleep(K_MSEC(1000)); | ||
}; | ||
return 0; | ||
} |