samples: rtc: Generic RTC sample
This sample app set and read date/time from the Real-Time Clock. Signed-off-by: Muhammad Waleed Badar <walid.badar@gmail.com>
This commit is contained in:
parent
77ebf82b3e
commit
9a804572a3
8 changed files with 169 additions and 0 deletions
8
samples/drivers/rtc/CMakeLists.txt
Normal file
8
samples/drivers/rtc/CMakeLists.txt
Normal file
|
@ -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
samples/drivers/rtc/README.rst
Normal file
34
samples/drivers/rtc/README.rst
Normal file
|
@ -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
samples/drivers/rtc/boards/qemu_x86.overlay
Normal file
18
samples/drivers/rtc/boards/qemu_x86.overlay
Normal file
|
@ -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
samples/drivers/rtc/boards/qemu_x86_64.overlay
Normal file
18
samples/drivers/rtc/boards/qemu_x86_64.overlay
Normal file
|
@ -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
samples/drivers/rtc/boards/stm32f3_disco.overlay
Normal file
11
samples/drivers/rtc/boards/stm32f3_disco.overlay
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 STMicroelectronics
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/ {
|
||||||
|
aliases {
|
||||||
|
rtc = &rtc;
|
||||||
|
};
|
||||||
|
};
|
2
samples/drivers/rtc/prj.conf
Normal file
2
samples/drivers/rtc/prj.conf
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# Enable Peripheral
|
||||||
|
CONFIG_RTC=y
|
12
samples/drivers/rtc/sample.yaml
Normal file
12
samples/drivers/rtc/sample.yaml
Normal file
|
@ -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
samples/drivers/rtc/src/main.c
Normal file
66
samples/drivers/rtc/src/main.c
Normal file
|
@ -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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue