1.16.9. RTC

1、RTC时钟,一般分为硬件RTC和虚拟RTC,本芯片只有虚拟RTC。

1.16.9.1. RTC功能介绍

SDK中的RTC采用统一接口,可配置其时钟源、读写时钟、读写闹钟等功能。

1.16.9.2. rtc_dev_init

初始化rtc模块,可配置时钟源、RTC类型、闹钟使能以及闹钟回调函数。
时钟源,可选择为LRC或外挂32K。(LRC时钟源会有千分之一的误差)
时钟类型,可选择为硬件RTC或虚拟RTC。(注:本芯片没有硬件RTC)
闹钟使能,可使能闹钟功能,只有使能闹钟功能后,闹钟才可以正常使用。
闹钟回调函数,闹钟中断起来以后调用的函数,用户可自己编写相关程序。

原型:

#define CLK_SEL_32K 1
#define CLK_SEL_12M 2 //注:本芯片不支持12M
#define CLK_SEL_24M 3 //注:本芯片不支持24M
#define CLK_SEL_LRC 4

//*********************************************************************************//
//                         RTC 模式配置                                            //
//*********************************************************************************//
#define TCFG_APP_RTC_CLK_SEL             CLK_SEL_LRC //选择RTC时钟源
#define TCFG_APP_RTC_EN                  1 //使能RTC模块

struct rtc_dev_platform_data {
    const struct sys_time *default_sys_time;//配置默认时钟
    void (*cbfun)(const struct rtc_event_t *);//RTC事件回调函数
    u8 trim_interval;  //该参数暂无意义
};


int rtc_init(const struct rtc_dev_platform_data *arg);//rtc初始化
void rtc_dev_deinit(void);                          //rtc模块关闭

参数:

arg

rtc相关参数配置

注意:

系统要想知道是否为闹钟唤醒,初始化时传入 cbfun 回调函数,当为闹钟唤醒时驱动会调用 cbfun 告知应用层

1.16.9.3. rtc_write_time()/rtc_read_time()

rtc读写时间接口

原型:

struct sys_time {
    u16 year;
    u8 month;
    u8 day;
    u8 hour;
    u8 min;
    u8 sec;
};


u32 rtc_write_time(const struct sys_time *time);
u32 rtc_read_time(struct sys_time *time);

参数:

time

读写时间的结构体

1.16.9.4. rtc_alarm_switch()/rtc_is_alarm_en()

rtc闹钟开关以及相关判断接口

原型:

void rtc_alarm_switch(u32 en);
u32 rtc_is_alarm_en(void);

参数:

rtc_alarm_switch(u32 en)

en

1为使能闹钟/0为关闭闹钟

u32 rtc_is_alarm_en(void)

返回值

1为闹钟已使能/0为未使能

1.16.9.5. rtc_write_alarm()/rtc_read_alarm()

rtc读写闹钟接口

原型:

struct sys_time {
    u16 year;
    u8 month;
    u8 day;
    u8 hour;
    u8 min;
    u8 sec;
};

u32 rtc_read_alarm(struct sys_time *time);
u32 rtc_write_alarm(const struct sys_time *time);

参数:

time

读写时间的结构体

1.16.9.6. void rtc_debug_dump(void);

rtc调试接口,用于调试打印RTC信息。(RTC时间、闹钟时间以及闹钟是否使能)

原型:

void rtc_debug_dump(void);

1.16.9.7. rtc_save_context_to_vm()

将rtc相关参数保存到vm(软关机内部默认调用)

原型:

void rtc_save_context_to_vm(void);

1.16.9.8. 时钟常用接口

系统还提供了时钟相关的应用接口,便于用户开发时使用。

原型:

bool is_leap_year(u32 year);//判断是否为闰年
u32 days_in_year(u32 year);//返回当前年份天数
u32 days_in_month(u32 year, u32 month);//返回当前年份月份对应天数
u64 datetime_to_timestamp(const struct sys_time *time);//将UTC时间转换为自1970-01-01 00:00:00起的时间戳(秒)
u64 datetime_to_timestamp_us(const struct sys_time *time);//将UTC时间转换为自1970-01-01 00:00:00起的时间戳(微秒)
void timestamp_to_datetime(u64 total_seconds, struct sys_time *time);//将时间戳(自1970-01-01 00:00:00起的秒数)转换为UTC时间
u32 get_weekday_from_time(const struct sys_time *time);//根据日期计算星期几
s32 get_seconds_delta(const struct sys_time *old_time, const struct sys_time *curr_time);//计算两个时间之间的秒差
void datetime_add_seconds(struct sys_time *time, s32 sec);//给当前时间增加指定秒数(可为负数)
void day_to_ymd(u32 day, struct sys_time *sys_time);//总天数转换为年月日
u32 ymd_to_day(const struct sys_time *time);//年月日转换为总天数

1.16.9.9. RTC demo

开发时可参考 rtc_demo.c

struct sys_time temp;
u16 dump_timer;

//rtc回调函数 - 软关机唤醒时, 正常运行有rtc事件回回调该接口告诉应用层
void alm_ring_fun(const struct rtc_event_t *ev)
{
    if (ev->event & BIT(RTC_ALARM_EVENT)) {
        printf("\nalarm_wakeup_event\n");
    }
}

void rtc_time_test_dump(void)
{
    rtc_read_time(&temp);
    printf("rtc_sys_time: %d-%d-%d %d:%d:%d\n", temp.year, temp.month, temp.day, temp.hour, temp.min, temp.sec);
    rtc_read_alarm(&temp);                  //读当前alarm时间
    printf("rtc_rtc_read_alarm: %d-%d-%d %d:%d:%d\n", temp.year, temp.month, temp.day, temp.hour, temp.min, temp.sec);
}

void rtc_test_demo_start(void)
{
    //配置默认时间
    struct sys_time def_sys_time_t = {
        .year = 2026,
        .month = 4,
        .day = 1,
        .hour = 0,
        .min = 0,
        .sec = 0,
    };

    struct rtc_dev_platform_data rtc_config_t = {
        .default_sys_time = &def_sys_time_t,
        .cbfun = alm_ring_fun,
    };

    rtc_init(&rtc_config_t);
    printf("---------------\n");
    printf("\nrtc_dev_init\n");

    rtc_read_time(&temp);
    printf("rtc_default_sys_time: %d-%d-%d %d:%d:%d\n", temp.year, temp.month, temp.day, temp.hour, temp.min, temp.sec);

    datetime_add_seconds(&temp, 55);
    rtc_alarm_switch(1);
    rtc_write_alarm(&temp);
    rtc_read_alarm(&temp);
    printf("rtc_default_alarm: %d-%d-%d %d:%d:%d\n", temp.year, temp.month, temp.day, temp.hour, temp.min, temp.sec);
    printf("---------------\n");

    dump_timer = usr_timer_add(NULL, (void *)rtc_time_test_dump, 1000, 0);
}

void rtc_test_demo_close(void)
{
    if (dump_timer) {
        usr_timer_del(dump_timer);
        dump_timer = 0;
    }
    rtc_dev_deinit();
}