stm32 RTC_WaitForSynchro()死循环

1.RTC_WaitForSynchro()死循环,发现是没有执行RTC_Configuration(),增加函数,但不知道对之后的时钟准确性有什么影响


/*******************************************************************************
* Function Name  : RTC_Configuration
* Description    : Configures the RTC.
* Input          : None
* Output         : None
* Return         : 0 reday,-1 error.
*******************************************************************************/
int RTC_Configuration(void)
{
    u32 countmax = 0x20000;
    u32 count = countmax;
    /* Enable PWR and BKP clocks */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
    /* Allow access to BKP Domain */
    PWR_BackupAccessCmd(ENABLE);
    /* Reset Backup Domain */
    BKP_DeInit();
    /* Enable LSE */
    RCC_LSEConfig(RCC_LSE_ON);
 
    /* Wait till LSE is ready */
    while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--count));
 
    if(count == 0)
    {
        count = countmax;
        //关闭外部低速晶振(LSE)
        RCC_LSEConfig(RCC_LSE_OFF);
        //如果失败,使用内部晶振
        //使能或者失能内部低速晶振(LSI)
        RCC_LSICmd(ENABLE);
        //设置RTC时钟(RTCCLK)
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
 
        /* Wait till LSI is ready */
        while((RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) && (--count));
 
        if(count == 0)
            return 1;
    }
    else
    {
        /* Select LSE as RTC Clock Source */
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);//设置RTC时钟(RTCCLK)
    }
 
    /* Enable RTC Clock */
    RCC_RTCCLKCmd(ENABLE);
    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    /* Set RTC prescaler: set RTC period to 1sec */
    RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    return 0;
}


void rt_hw_rtc_init(void)
{
    rtc.type    = RT_Device_Class_RTC;
 
    if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
    {
        rt_kprintf("rtc is not configured\n");
        rt_kprintf("please configure with set_date and set_time\n");
 
        if(RTC_Configuration() != 0)
        {
            rt_kprintf("rtc configure fail...\r\n");
            return ;
        }
    }
    else
    {
        if(RTC_Configuration() != 0)
        {
            rt_kprintf("rtc configure fail...\r\n");
            return ;
        }
 
        /* Wait for RTC registers synchronization */
        RTC_WaitForSynchro();
    }
 
    /* register rtc device */
    rtc.init    = RT_NULL;
    rtc.open    = rt_rtc_open;
    rtc.close   = RT_NULL;
    rtc.read    = rt_rtc_read;
    rtc.write   = RT_NULL;
    rtc.control = rt_rtc_control;
    /* no private */
    rtc.user_data = RT_NULL;
    rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
    return;
}









  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值