Solution how to fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..

Dependencies:   mbed-dev

Fork of Nucleo_RTC_battery_bkup_pwr_off_okay by Kenji Arai

Experimental fork https://os.mbed.com/users/kenjiArai/code/Nucleo_RTC_battery_bkup_pwr_off_okay/ to fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..

At this moment (7/11/17) use forked mbed-dev https://os.mbed.com/users/maxxir/code/mbed-dev/.

Or require patch for ./mbed-dev/targets/TARGET_STM/rtc_api.c.

You can manual add to your project fresh mbed-dev and change file mbed-dev\targets\TARGET_STM\rtc_api.c from root project patched rtc_api.c.stm32f10x.txt.

Exploring origin errors, I saw that something wrong with HAL API on STM32F1xx with this functions:

 HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);

 HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);


 HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);

 HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);

Look here (as I understand it possible broken on STM32CUBE HAL level now):

https://community.st.com/thread/43218-stm32f103-loss-rtc-date-when-reset

So I use direct RTC register manipulation for STM32F1xx:

rtc_read(), rtc_write() (native rtc_init() - works good).

Also added stub for non-working on STM32F1xx rtc_read_subseconds().

Now the stm32F103 can survive power off, and allows you to get and set the time.

Tested OK on boards:

NUCLEO STM32F103RB, DIY STM32F100CB (forked from DISCO_F100RB)

Happy coding!

maxxir

10/11/17

Revision:
6:5ada291037aa
Parent:
5:bc464a588c2e
Child:
7:27a1cf7f8921
--- a/main.cpp	Sat May 23 22:26:37 2015 +0000
+++ b/main.cpp	Fri Jan 29 07:51:04 2016 +0000
@@ -24,7 +24,7 @@
 //  Definition ------------------------------------------------------------------------------------
 #define SHOW_KEY_PROMPT     30
 
-#define USE_LCD
+//#define USE_LCD
 
 #if defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE) \
  || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F334R8)
@@ -36,10 +36,11 @@
 DigitalIn userSW(USER_BUTTON);
 DigitalOut myled(LED1);         // Indicate the sampling period
 Serial pc(USBTX, USBRX);
-#if defined(USE_LCD)
+//#if defined(USE_LCD)
 I2C i2c(D14,D15);               // SDA, SCL
 TextLCD_I2C_N lcd(&i2c, 0x7c, TextLCD::LCD8x2);  // LCD(Akizuki AQM0802A)
-#endif
+//#endif
+DigitalOut pwr_onoff(PA_9);
 
 //  RAM -------------------------------------------------------------------------------------------
 
@@ -58,6 +59,8 @@
     uint8_t wait_counter = 0;
     uint8_t xtal = 0;
 
+    pwr_onoff = 1;
+    wait(2.0);
 #if defined(USE_LCD)
     // lcd
     lcd.locate(0, 0);    // 1st line top