Skip to content

Commit 9a80457

Browse files
walidbadarhenrikbrixandersen
authored andcommitted
samples: rtc: Generic RTC sample
This sample app set and read date/time from the Real-Time Clock. Signed-off-by: Muhammad Waleed Badar <[email protected]>
1 parent 77ebf82 commit 9a80457

File tree

8 files changed

+169
-0
lines changed

8 files changed

+169
-0
lines changed

samples/drivers/rtc/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(rtc)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/drivers/rtc/README.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.. zephyr:code-sample:: rtc
2+
:name: Real-Time Clock (RTC)
3+
:relevant-api: rtc_interface
4+
5+
Set and read the date/time from a Real-Time Clock.
6+
7+
Overview
8+
********
9+
10+
This sample shows how to use the :ref:`rtc driver API <rtc_api>`
11+
to set and read the date/time from RTC and display on the console
12+
and can be built and executed on boards supporting RTC.
13+
14+
Building and Running
15+
********************
16+
17+
Build and flash as follows, replacing ``stm32f3_disco`` with your board:
18+
19+
.. zephyr-app-commands::
20+
:zephyr-app: samples/drivers/rtc
21+
:board: stm32f3_disco
22+
:goals: build flash
23+
:compact:
24+
25+
Sample Output
26+
=============
27+
28+
.. code-block:: console
29+
30+
RTC date and time: 2024-11-17 04:21:47
31+
RTC date and time: 2024-11-17 04:21:48
32+
RTC date and time: 2024-11-17 04:21:49
33+
34+
<repeats endlessly>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2023 Bjarki Arge Andreasen
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* The RTC IRQ is not routed to the IOAPIC if the legacy
9+
* IRQ bit is set. The IRQ is required for alarm
10+
* operation and the update callback.
11+
*/
12+
&hpet {
13+
no-legacy-irq;
14+
};
15+
16+
&rtc {
17+
status = "okay";
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2023 Bjarki Arge Andreasen
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* The RTC IRQ is not routed to the IOAPIC if the legacy
9+
* IRQ bit is set. The IRQ is required for alarm
10+
* operation and the update callback.
11+
*/
12+
&hpet {
13+
no-legacy-irq;
14+
};
15+
16+
&rtc {
17+
status = "okay";
18+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2024 STMicroelectronics
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
rtc = &rtc;
10+
};
11+
};

samples/drivers/rtc/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Enable Peripheral
2+
CONFIG_RTC=y

samples/drivers/rtc/sample.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sample:
2+
name: Real-Time Clock Sample
3+
tests:
4+
sample.drivers.rtc:
5+
platform_allow:
6+
- stm32f3_disco
7+
tags:
8+
- samples
9+
- rtc
10+
- api
11+
depends_on:
12+
- rtc

samples/drivers/rtc/src/main.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2024, Muhammad Waleed Badar
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/rtc.h>
10+
#include <zephyr/sys/util.h>
11+
12+
const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc));
13+
14+
static int set_date_time(const struct device *rtc)
15+
{
16+
int ret = 0;
17+
struct rtc_time tm = {
18+
.tm_year = 2024 - 1900,
19+
.tm_mon = 11 - 1,
20+
.tm_mday = 17,
21+
.tm_hour = 4,
22+
.tm_min = 19,
23+
.tm_sec = 0,
24+
};
25+
26+
ret = rtc_set_time(rtc, &tm);
27+
if (ret < 0) {
28+
printk("Cannot write date time: %d\n", ret);
29+
return ret;
30+
}
31+
return ret;
32+
}
33+
34+
static int get_date_time(const struct device *rtc)
35+
{
36+
int ret = 0;
37+
struct rtc_time tm;
38+
39+
ret = rtc_get_time(rtc, &tm);
40+
if (ret < 0) {
41+
printk("Cannot read date time: %d\n", ret);
42+
return ret;
43+
}
44+
45+
printk("RTC date and time: %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900,
46+
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
47+
48+
return ret;
49+
}
50+
51+
int main(void)
52+
{
53+
/* Check if the RTC is ready */
54+
if (!device_is_ready(rtc)) {
55+
printk("Device is not ready\n");
56+
return 0;
57+
}
58+
59+
set_date_time(rtc);
60+
61+
/* Continuously read the current date and time from the RTC */
62+
while (get_date_time(rtc) == 0) {
63+
k_sleep(K_MSEC(1000));
64+
};
65+
return 0;
66+
}

0 commit comments

Comments
 (0)