Skip to content

Commit

Permalink
samples: rtc: Generic RTC sample
Browse files Browse the repository at this point in the history
This sample app set and read date/time from the Real-Time Clock.

Signed-off-by: Muhammad Waleed Badar <[email protected]>
  • Loading branch information
walidbadar authored and henrikbrixandersen committed Nov 21, 2024
1 parent 77ebf82 commit 9a80457
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 0 deletions.
8 changes: 8 additions & 0 deletions samples/drivers/rtc/CMakeLists.txt
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})
34 changes: 34 additions & 0 deletions samples/drivers/rtc/README.rst
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>
18 changes: 18 additions & 0 deletions samples/drivers/rtc/boards/qemu_x86.overlay
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";
};
18 changes: 18 additions & 0 deletions samples/drivers/rtc/boards/qemu_x86_64.overlay
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";
};
11 changes: 11 additions & 0 deletions samples/drivers/rtc/boards/stm32f3_disco.overlay
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;
};
};
2 changes: 2 additions & 0 deletions samples/drivers/rtc/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Enable Peripheral
CONFIG_RTC=y
12 changes: 12 additions & 0 deletions samples/drivers/rtc/sample.yaml
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
66 changes: 66 additions & 0 deletions samples/drivers/rtc/src/main.c
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;
}

0 comments on commit 9a80457

Please sign in to comment.