Skip to content

Commit 5df3d47

Browse files
committed
Use LSI OSC for RTC clock when LSE is not detected
* Fix rtc_init to use LSI for RTC clock source when LSE is not detected.
1 parent 6d6bc9e commit 5df3d47

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
@@ -586,6 +586,9 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
586586
}
587587

588588
static 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

Comments
 (0)