Skip to content

由 爱笑 贡献,Ai-Thinker 进行整理

【Ai-WB2中级篇】RTC实时时钟

RTC(real-time clock)为操作系统中的实时时钟设备,为操作系统提供精准的实时时间和定时报警功能。当设备下电后,通过外置电池供电,RTC继续记录操作系统时间;设备上电后,RTC提供实时时钟给操作系统,确保断电后系统时间的连续性。但是BL602芯片没有内置RTC模块,也没有提供外置电池供电,但是提供的软件RTC功能可以在带电情况下使用。 本文将详细介绍如何使用Ai-WB2的bl_iot_sdk中的RTC模块。 一:RTC介绍 BL602芯片没有内置RTC模块,但是 b1_iot_sdk 提供了软件RTC功能。RTC支持两种计时模式:DEC模式和BCDQ模式。 RTC模块的HOSAL层APl在 components/platform/hosal/include/hosal_rtc.h 中定义。常用的API如下: · int hosal_rtc_init(hosal_rtc_dev_t *rtc):初始化RTC设备。参数说明如下: · rtc:RTC设备实例。其定义如下:

📜 点击展开完整代码
c
typedef struct {
uint8_t       port;   /**< rtc port */
hosal_rtc_config_t  config; /**< rtc config */
void         *priv;   /**< priv data */
} hosal_rtc_dev_t;

其中,hosal_rtc_config_t定义如下:

📜 点击展开完整代码
c
#define HOSAL_RTC_FORMAT_DEC 1 /**< RTC DEC format */
#define HOSAL_RTC_FORMAT_BCD 2 /**< RTC BCD format */
typedef struct {
uint8_t format; /**< time formart DEC or BCD */
} hosal_rtc_config_t;
📜 点击展开完整代码
c
**·** 返回值:当调用成功时,返回0;否则返回非零值。
**·** int hosal_rtc_set_time(hosal_rtc_dev_t *rtc, const hosal_rtc_time_t *time):设置RTC日期时间。参数说明如下:
**·** rtc:RTC设备
**·** time:需要设置的时间。其定义如下:
📜 点击展开完整代码
c
/**
* @brief RTC time struct
*/
typedef struct {
uint8_t sec;     /**< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 */
uint8_t min;     /**< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 */
uint8_t hr;      /**< DEC format:value range from 0 to 23, BCD format:value range from 0x00 to 0x23 */
uint8_t date;    /**< DEC format:value range from 1 to 31, BCD format:value range from 0x01 to 0x31 */
uint8_t month;   /**< DEC format:value range from 1 to 12, BCD format:value range from 0x01 to 0x12 */
uint16_t year;   /**< DEC format:value range from 0 to 9999, BCD format:value range from 0x0000 to 0x9999 */
} hosal_rtc_time_t;
📜 点击展开完整代码
c
**·** 返回值:当调用成功时,返回0;否则返回非零值。
**·** int hosal_rtc_get_time(hosal_rtc_dev_t *rtc, hosal_rtc_time_t *time):查询当前时间。参数说明如下:
**·** rtc:RTC设备
**·** time:需要设置的时间
**·** 返回值:当调用成功时,返回0;否则返回非零值。
**·** int hosal_rtc_set_count(hosal_rtc_dev_t *rtc,uint64_t *time_stamp):设置RTC新的计数值。参数说明如下:
**·** rtc:RTC设备
**·** time stamp:需要设置的时间
**·** 返回值:当调用成功时,返回0;否则返回非零值。
**·** int hosal_rtc_get_count(hosal_rtc_dev_t *rtc,uint64_t*time_stamp):获取当前RTC的时间计数值。参数说明如下:
**·** rtc:RTC设备
**·** time stamp:返回当前的时间计数值
**·** 返回值:当调用成功时,返回0;否则返回非零值。
**·** int hosal_rtc_finalize(hosal_rtc_dev_t *rtc):销毁当前RTC设备实例,释放相关资源。参数说明如下:
**·** rtc:RTC设备
**·** 返回值:当调用成功时,返回0;否则返回非零值。
**二:RTC使用实例**
📜 点击展开完整代码
c
#include <stdio.h>
#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
#include <stdbool.h>
#include <hosal_rtc.h>
#include <blog.h>
#define TAG "rtc_demo"
static hosal_rtc_dev_t rtc;
bool rtc_demo_init(void) {
int ret1 = -1;
/* init rtc DEC format*/
hosal_rtc_dev_t rtc;
rtc.port = 0;
rtc.config.format = HOSAL_RTC_FORMAT_DEC;
hosal_rtc_init(&rtc);
/* set rtc time */
hosal_rtc_time_t time_buf;
time_buf.sec = 10;
time_buf.min = 56;
time_buf.hr = 10;
time_buf.date = 26;
time_buf.month = 4;
time_buf.year = 23;
ret1 = hosal_rtc_set_time(&rtc, &time_buf);
if (ret1 == 0) {
printf("set time sec     = %d\r\n", time_buf.sec);
printf("set time min     = %d\r\n", time_buf.min);
printf("set time hr      = %d\r\n", time_buf.hr);
printf("set time date    = %d\r\n", time_buf.date);
printf("set time month   = %d\r\n", time_buf.month);
printf("set time year    = %d\r\n", time_buf.year + 2000);
}
return (ret1 == 0);
}
void rtc_task(void* params) {
printf("rtc task started\r\n");
bool ret = rtc_demo_init();
hosal_rtc_time_t time_buf;
if (!ret) {
printf("rtc init failed\r\n");
while (true) {
vTaskDelay(1);
}
}
printf("rtc task started\r\n");
while (true) {
memset(&time_buf, 0, sizeof(hosal_rtc_time_t));
ret = hosal_rtc_get_time(&rtc, &time_buf);
if(ret == 0){
printf("set time sec     = %d\r\n", time_buf.sec);
printf("set time min     = %d\r\n", time_buf.min);
printf("set time hr      = %d\r\n", time_buf.hr);
printf("set time date    = %d\r\n", time_buf.date);
printf("set time month   = %d\r\n", time_buf.month);
printf("set time year    = %d\r\n", time_buf.year + 2000);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void main(void) {
printf("rtc demo inited\r\n");
xTaskCreate(rtc_task, "rtc_task", 1024, NULL, 15, NULL);
}

Released under the MIT License. Build Time 2026-09-11 14:52:23