Skip to content

Commit ea9e441

Browse files
committed
Merge pull request adafruit#112 from iabdalkader/master
Use LSI OSC for RTC clock when LSE is not detected
2 parents 9193f89 + 5df3d47 commit ea9e441

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

stm/main.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
585585
}
586586

587587
static void rtc_init(void) {
588+
uint32_t rtc_clksrc;
589+
uint32_t timeout =10000;
590+
588591
/* Enable the PWR clock */
589592
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
590593

@@ -595,11 +598,29 @@ static void rtc_init(void) {
595598
RCC_LSEConfig(RCC_LSE_ON);
596599

597600
/* Wait till LSE is ready */
598-
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {
601+
while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--timeout > 0)) {
602+
}
603+
604+
/* If LSE timed out, use LSI instead */
605+
if (timeout == 0) {
606+
/* Enable the LSI OSC */
607+
RCC_LSICmd(ENABLE);
608+
609+
/* Wait till LSI is ready */
610+
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {
611+
}
612+
613+
/* Use LSI as the RTC Clock Source */
614+
rtc_clksrc = RCC_RTCCLKSource_LSI;
615+
} else {
616+
/* Use LSE as the RTC Clock Source */
617+
rtc_clksrc = RCC_RTCCLKSource_LSE;
599618
}
600619

601620
/* Select the RTC Clock Source */
602-
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
621+
RCC_RTCCLKConfig(rtc_clksrc);
622+
623+
/* Note: LSI is around (32KHz), these dividers should work either way */
603624
/* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
604625
uint32_t uwSynchPrediv = 0xFF;
605626
uint32_t uwAsynchPrediv = 0x7F;

0 commit comments

Comments
 (0)