@@ -586,6 +586,9 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
586586}
587587
588588static void rtc_init (void ) {
589+ uint32_t rtc_clksrc ;
590+ uint32_t timeout = 10000 ;
591+
589592 /* Enable the PWR clock */
590593 RCC_APB1PeriphClockCmd (RCC_APB1Periph_PWR , ENABLE );
591594
@@ -596,11 +599,29 @@ static void rtc_init(void) {
596599 RCC_LSEConfig (RCC_LSE_ON );
597600
598601 /* Wait till LSE is ready */
599- while (RCC_GetFlagStatus (RCC_FLAG_LSERDY ) == RESET ) {
602+ while ((RCC_GetFlagStatus (RCC_FLAG_LSERDY ) == RESET ) && (-- timeout > 0 )) {
603+ }
604+
605+ /* If LSE timed out, use LSI instead */
606+ if (timeout == 0 ) {
607+ /* Enable the LSI OSC */
608+ RCC_LSICmd (ENABLE );
609+
610+ /* Wait till LSI is ready */
611+ while (RCC_GetFlagStatus (RCC_FLAG_LSIRDY ) == RESET ) {
612+ }
613+
614+ /* Use LSI as the RTC Clock Source */
615+ rtc_clksrc = RCC_RTCCLKSource_LSI ;
616+ } else {
617+ /* Use LSE as the RTC Clock Source */
618+ rtc_clksrc = RCC_RTCCLKSource_LSE ;
600619 }
601620
602621 /* Select the RTC Clock Source */
603- RCC_RTCCLKConfig (RCC_RTCCLKSource_LSE );
622+ RCC_RTCCLKConfig (rtc_clksrc );
623+
624+ /* Note: LSI is around (32KHz), these dividers should work either way */
604625 /* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
605626 uint32_t uwSynchPrediv = 0xFF ;
606627 uint32_t uwAsynchPrediv = 0x7F ;
0 commit comments