芯片型号:GD32L233CBT6,RTC功能测试了几天一直工作正常,今天突然出现无法初始化的问题,rtc设置不了,INITF始终不为1导致超时。使用官方的驱动,GD32L23x_Firmware_Library_V2.0.2,在此处判断INITF时候超时报错。
RTC初始化代码如下,前两天调试一直都正常的,没发现代码里面有什么问题,使用的内部32K时钟。
#define BIN_TO_BCD(x) ((((x) / 10) << 4) + ((x) % 10))
#define BCD_TO_BIN(x) (10 * ((x) >> 4) + ((x) & 0x0F))
#define RTC_IRC32K
//#define RTC_LXTAL
#ifdef RTC_IRC32K
#define PRESCALER_S 0x13F
#define PRESCALER_A 0x63
#endif
#ifdef RTC_LXTAL
#define PRESCALER_S 0xFF
#define PRESCALER_A 0x7F
#endif
struct tm calendar;
uint32_t RTCSRC_FLAG = 0;
void app_rtc_init()
{
uint32_t RTCSRC_FLAG = 0;
rcu_periph_clock_enable(RCU_PMU);
rcu_periph_clock_enable(RCU_BKP);
#ifdef RTC_IRC32K
rcu_osci_on(RCU_IRC32K);
rcu_osci_stab_wait(RCU_IRC32K);
rcu_rtc_clock_config(RCU_RTCSRC_IRC32K);
#endif
#ifdef RTC_LXTAL
rcu_osci_on(RCU_LXTAL);
rcu_osci_stab_wait(RCU_LXTAL);
rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
#endif
rcu_periph_clock_enable(RCU_RTC);
rtc_register_sync_wait();
/* detect the reset source */
// if(RESET != rcu_flag_get(RCU_FLAG_PORRST)) {
// printf("power on reset occurred....\n\r");
// } else if(RESET != rcu_flag_get(RCU_FLAG_EPRST)) {
// printf("external reset occurred....\n\r");
// }
// printf("no need to configure RTC....\n\r");
// rcu_all_reset_flag_clear();
pmu_backup_write_enable();
RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9);
if(RTCSRC_FLAG == 0x00)
{
calendar.tm_year=YEAR_BASE+25;
calendar.tm_mon=12;
calendar.tm_mday=30;
calendar.tm_hour=1;
calendar.tm_min=0;
calendar.tm_sec=40;
rtc_set_time(&calendar);
}
}
void rtc_set_time(struct tm *time_struct)
{
rtc_parameter_struct rtc_initpara = {0};
/* RTC时钟频率 = 32.768kHz / (255 + 1) / (127 + 1) = 1Hz */
rtc_initpara.factor_asyn = PRESCALER_A;
rtc_initpara.factor_syn = PRESCALER_S;
rtc_initpara.display_format = RTC_24HOUR;
rtc_initpara.year = BIN_TO_BCD(time_struct->tm_year - YEAR_BASE);
rtc_initpara.month = BIN_TO_BCD(time_struct->tm_mon);
rtc_initpara.date = BIN_TO_BCD(time_struct->tm_mday);
rtc_initpara.hour = BIN_TO_BCD(time_struct->tm_hour);
rtc_initpara.minute = BIN_TO_BCD(time_struct->tm_min);
rtc_initpara.second = BIN_TO_BCD(time_struct->tm_sec);
rtc_initpara.day_of_week = time_struct->tm_wday;
rtc_init(&rtc_initpara);
}
|