dynamic STM32_LSE_ENABLED?

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
russian
Posts: 368
Joined: Mon Oct 29, 2012 3:17 am
Has thanked: 16 times
Been thanked: 14 times

dynamic STM32_LSE_ENABLED?

Post by russian »

Currently STM32_LSE_ENABLED is a compile-time flag, thus in order to run on both boards with and without LSE two versions of binary would be needed. It would be nice if the driver would be smart enough to detect if LSE is there or not, and act accordingly.

For now I've implemented a horrible hack which sets a timeout on the busy-waits related to LSE:

Code: Select all

Index: os/hal/platforms/STM32F4xx/hal_lld.c
===================================================================
--- os/hal/platforms/STM32F4xx/hal_lld.c   (revision 8349)
+++ os/hal/platforms/STM32F4xx/hal_lld.c   (working copy)
@@ -68,9 +68,10 @@
   /* No LSE Bypass.*/
   RCC->BDCR |= RCC_BDCR_LSEON;
 #endif
-  while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
+  int waitCounter = 0;
+  while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0 && ++waitCounter <LSE_TIMEOUT)
     ;                                       /* Waits until LSE is stable.   */
-#endif
+#endif /* STM32_LSE_ENABLED */
 
 #if HAL_USE_RTC
   /* If the backup domain hasn't been initialized yet then proceed with
Index: os/hal/platforms/STM32/RTCv2/rtc_lld.c
===================================================================
--- os/hal/platforms/STM32/RTCv2/rtc_lld.c   (revision 8349)
+++ os/hal/platforms/STM32/RTCv2/rtc_lld.c   (working copy)
@@ -58,7 +58,10 @@
  *
  * @notapi
  */
-#define rtc_lld_apb1_sync() {while ((RTCD1.id_rtc->ISR & RTC_ISR_RSF) == 0);}
+#define rtc_lld_apb1_sync() {                                                \
+    int counter = 0;                                                         \
+   while ((RTCD1.id_rtc->ISR & RTC_ISR_RSF) == 0 && ++counter <LSE_TIMEOUT);\
+   }
 
 /**
  * @brief   Beginning of configuration procedure.
@@ -67,7 +70,8 @@
  */
 #define rtc_lld_enter_init() {                                              \
   RTCD1.id_rtc->ISR |= RTC_ISR_INIT;                                        \
-  while ((RTCD1.id_rtc->ISR & RTC_ISR_INITF) == 0)                          \
+  int counter = 0;                                                          \
+  while ((RTCD1.id_rtc->ISR & RTC_ISR_INITF) == 0 && ++counter <LSE_TIMEOUT)\
     ;                                                                       \
 }
 
Index: os/hal/platforms/STM32/RTCv2/rtc_lld.h
===================================================================
--- os/hal/platforms/STM32/RTCv2/rtc_lld.h   (revision 8349)
+++ os/hal/platforms/STM32/RTCv2/rtc_lld.h   (working copy)
@@ -36,6 +36,11 @@
 /*===========================================================================*/
 
 /**
+ * this hack is about surviving lack of LSE (32kHz osc) part of RTC
+ */
+#define LSE_TIMEOUT 1000000
+
+/**
  * @brief   Two alarm comparators available on STM32F4x and STM32F2x.
  */
 #if !defined(STM32F0XX)
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: dynamic STM32_LSE_ENABLED?

Post by Giovanni »

While timeouts could be useful as error detection I am not sure that skipping initializations is a good idea.

Giovanni
User avatar
russian
Posts: 368
Joined: Mon Oct 29, 2012 3:17 am
Has thanked: 16 times
Been thanked: 14 times

Re: dynamic STM32_LSE_ENABLED?

Post by russian »

Skipping initialization is bad, but hanging up is worse. As is the firmware hangs up if STM32_LSE_ENABLED set tot TRUE but no 32kHz crystal. I want the software to survive the lack of 32kHz.
Post Reply