STM32 RTC: Add retry to initialization logic. Sometime RTC initialization takes longer. From dlsitzer.

This commit is contained in:
Gregory Nutt 2014-05-27 07:10:53 -06:00
parent 10d8ea8f59
commit 2b42e2d587

View File

@ -1,7 +1,7 @@
/************************************************************************************ /************************************************************************************
* arch/arm/src/stm32/stm32_rtcc.c * arch/arm/src/stm32/stm32_rtcc.c
* *
* Copyright (C) 2012-2013 Gregory Nutt. All rights reserved. * Copyright (C) 2012-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -575,6 +575,8 @@ int up_rtcinitialize(void)
{ {
uint32_t regval; uint32_t regval;
int ret; int ret;
int maxretry = 10;
int nretry = 0;
rtc_dumpregs("On reset"); rtc_dumpregs("On reset");
@ -590,27 +592,63 @@ int up_rtcinitialize(void)
stm32_pwr_enablebkp(); stm32_pwr_enablebkp();
/* Check if the one-time initialization of the RTC has already been performed. /* Loop, attempting to initialize/resume the RTC. This loop is necessary
* We can determine this by checking if the magic number has been writing to * because it seems that occasionally it takes longer to initialize the RTC
* to back-up date register DR0. * (the actual failure is in rtc_synchwait()).
*/ */
regval = getreg32(STM32_RTC_BK0R); do
if (regval != RTC_MAGIC)
{ {
/* Perform the one-time setup of the LSE clocking to the RTC */ /* Check if the one-time initialization of the RTC has already been
* performed. We can determine this by checking if the magic number
* has been writing to to back-up date register DR0.
*/
ret = rtc_setup(); regval = getreg32(STM32_RTC_BK0R);
if (regval != RTC_MAGIC)
{
rtclldbg("Do setup\n");
/* Remember that the RTC is initialized */ /* Perform the one-time setup of the LSE clocking to the RTC */
putreg32(RTC_MAGIC, STM32_RTC_BK0R); ret = rtc_setup();
/* Remember that the RTC is initialized */
putreg32(RTC_MAGIC, STM32_RTC_BK0R);
}
else
{
rtclldbg("Do resume\n");
/* RTC already set-up, just resume normal operation */
ret = rtc_resume();
}
/* Check that setup or resume returned successfully */
switch (ret)
{
case OK:
{
rtclldbg("setup/resume okay\n");
break;
}
default:
{
rtclldbg("setup/resume failed (%d)\n", ret);
break;
}
}
} }
else while (ret != OK && ++nretry < maxretry);
{
/* RTC already set-up, just resume normal operation */
ret = rtc_resume(); if (ret != OK && nretry > 0)
{
rtclldbg("setup/resume ran %d times and failed with %d\n", nretry, ret);
return -ETIMEDOUT;
} }
/* Configure RTC interrupt to catch alarm interrupts. All RTC interrupts are /* Configure RTC interrupt to catch alarm interrupts. All RTC interrupts are