RT-Thread RTOS
An open source embedded real-time operating system
|
The RTC (Real-Time Clock) provides accurate real-time clock time, which can be used to generate information such as year, month, day, hour, minute, and second. At present, most real-time clock chips use a higher precision crystal oscillator as a clock source. In order to work when the main power supply is powered down, some clock chips will be powered by a battery to keep the time information valid.
The RT-Thread RTC device provides the basic services for the operating system's time system. RTCs find many uses in IoT scenarios, and even in secure transmission processes such as SSL, RTC has become an indispensable part.
The application accesses the RTC hardware through the RTC device management interface, and the relevant interfaces are as follows:
Function | Description |
---|---|
set_date() | Set date, year, month, day |
set_time() | Set time, hour, minute, second |
time() | Obtain current time |
Set the current date value of the RTC device by the following functions:
Parameter | Description |
---|---|
year | The year to be set to take effect |
month | The month to be set to take effect |
day | The date to be set to take effect |
return | —— |
RT_EOK | Set-up succeeded |
-RT_ERROR | Set-up failed, no rtc device found |
other error code | Set-up failed |
An example of use is as follows:
Set the current time value of the RTC device by the following function:
Parameter | Description |
---|---|
hour | The hour to be set to take effect |
minute | The minute to be set to take effect |
second | The second to be set to take effect |
return | —— |
RT_EOK | Set-up succeeded |
-RT_ERROR | Set-up failed, no rtc device found |
other error code | Set-up failed |
An example of use is as follows:
Obtain time using the time API in the C standard library:
Parameter | Description |
---|---|
t | Time data pointer |
return | —— |
Current time value |
Examples of use are as follows:
>Currently only one RTC device is allowed in the system and the name is "rtc"
.
You can use the function of enabling RTC software emulation, which is ideal for products that do not require high time precision and have no hardware RTC. The configuration options of menuconfig are as follows:
If the RT-Thread is connected to the Internet, you can enable automatic NTP time synchronization to synchronize local time periodically.
First open the NTP function in menuconfig as follows:
After NTP is turned on, the RTC's automatic synchronization function will be automatically turned on, and the synchronization period and the delay time of the first synchronization can also be set:
Enter date
to view the current time.
Also use the date
command, after the command, enter year
month
date
hour
minute
second
(between spaces, 24H system), and set the current time to 2018-02-16 01:15:30. The approximate effect is as follows:
For the specific usage of the RTC device, refer to the following example code. First, set the year, month, date, hour, minute and second information, and then delay the data for 3 seconds to get the current time information.