mcu_ab568x/app/platform/bsp/bsp_rtc.c
2025-05-30 18:03:10 +08:00

96 lines
2.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "include.h"
void rtc_clock_init(void)
{
// printf("rtc_clock_init\n");
tm_t tm;
tm.year = 2025;
tm.mon = 1;
tm.day = 1;
tm.hour = 9;
tm.min = 0;
tm.sec = 0;
//tm.weekday = get_weekday(tm.year, tm.mon, tm.day);
RTCCNT = tm_to_time(tm);
}
//多少秒后闹钟响
AT(.text.rtc)
void rtc_set_alarm_relative_time(u32 nsec)
{
tm_t rtc_tm;
rtc_tm = time_to_tm(RTCCNT); //更新时间结构体
RTCALM = tm_to_time(rtc_tm) + nsec; //设置闹钟相对于当前时间n秒后
}
//设置多少秒后闹钟唤醒
AT(.text.rtc)
void rtc_set_alarm_wakeup(u32 nsec)
{
uint rtccon3 = RTCCON3;
RTCCON0 |= BIT(18);
RTCCON0 = (RTCCON0 & ~(0X3<<8)) | (0X2<<8);
RTCCPND = BIT(17); //clear RTC alarm pending
RTCCON9 = BIT(0); //clear alarm pending
rtc_set_alarm_relative_time(nsec);
rtccon3 |= BIT(8); //RTC alarm wakeup enable
RTCCON3 = rtccon3;
}
//关闭闹钟
AT(.text.rtc)
void rtc_alarm_disable(void)
{
// printf("%s\n", __func__);
RTCCPND = BIT(17); //clear RTC alarm pending
RTCCON9 = BIT(8); //clear alarm pending
rtc_set_alarm_relative_time(0xffff);
RTCCON3 &= ~BIT(8);
}
/*
* 设置RTC时间
* 输入tm结构体指针
*/
void rtc_clock_set(tm_t rtc_tm)
{
RTCCNT = tm_to_time(rtc_tm);
}
/*
* 设置RTC时间
* 输入timestamp格林威治时间(UTC/GMT)
* time_zone时区(范围0~24, 表示西十二区(UTC_W12) ~ 东十二区(UTC_E12))
*/
void rtc_clock_timestamp_set(u32 timestamp, u8 time_zone)
{
int zone_offset = (time_zone - UTC_0);
u32 day_offset = (2020 - 1970)*365 + (2020 - 1970)/4; //RTCCNT时间从2020年开始, 标准的是从1970年开始算,需计算偏移
u32 offset = day_offset*86400;
RTCCNT = timestamp + zone_offset*3600 - offset;
}
/*
* 获取RTC时间
* 输入: 空
输出: rtc_tm结构体
*/
tm_t rtc_clock_get(void)
{
return time_to_tm(RTCCNT);
}
//休眠后后RTC校准时回调函数
AT(.sleep_text.rtc)
void rtc_clock_calc_notify(void)
{
sys_cb.rtc_cal_cnt++;
}