Nucleo series RTC control under Power On/Off and Reset condition

updated on January 16th, 2017
You do NOT need any modification for mbed source code!! Just use it and works fine.

[1st try with no hardware modification]

You DON'T need a battery back up circuit as 1st trial.
RESET -> Please run it and push reset button to see what's happened.
Standby -> Please run it and push user button to enter standby mode (deep sleep) then push reset button.

Import programNucleo_RTC_battery_bkup_pwr_off_okay

In the past, you need modify rtc_api.c in mbed-dev source code. From this revision, you can just use RTC function all of conditions (Normal, Reset, Stand-by, Power OFF).

[2nd try with additional hardware circuit]

Before making a back-up circuit, you need to identify your own Nucleo mbed board VBAT control logic.
Please refer following two different logic.
If you have Nucleo-F401RE,-F411RE type of VBAT circuit, you can just make following simple back-up circuit.
If you have Nucleo-L152 type of mbed, you need additional big effort becauseL152 does NOT have VBAT pin.
If you would like to set up those back-up circuit, please refer bottom of this page.

[Tested board]

ResetStanbyPower off and restart
Nucleo-F401REokokok (Back-up Circuit)
Nucleo-F411REokokok (Back-up Circuit)
Nucleo-F446REokokok (Back-up Circuit)
Nucleo-F334R8okokok (Back-up Circuit)
Nucleo-L476RGokokok (Back-up Circuit)
Nucleo-L152REokokno check (Need additional hardware)
Nucleo-L073RZokokno check (Need additional hardware)
Nucleo-L053R8okokno check (Need additional hardware)

Followings are old information

updated on July 2nd, 2016
Added F746xx.
updated on May 28th, 2016
Added L476RG mbed.
Changed SetRTC and rtc_api.c for F401RE & F411RE, F334R8 based on latest mbed-dev revision (141).
updated on May 24th, 2015
Changed SetRTC parameter.
Added F334R8 mbed.
Backup circuit is same as F401RE & F411RE (simple solution. Please refer [Hardware Configuration] Case F401RE or/and F411RE)
updated on Feb. 22nd, 2015
Added new back-up circuit only for L152RE.
updated on Feb. 18th, 2015
Change Deep Sleep Mode to Standby Mode to reduce backup current (Only for Nucleo L152RE).
updated on Feb. 17th, 2015
Added interrupt driven power off sequence for L512RE mbed
updated on Feb. 14th, 2015
Added L512RE mbed solution

[Current Nucleo RTC problem]

1) External Xtal (32.768kHz) / LSE clock does not start due to HW & SW current setting (Not always but many times).
2) After starting a RTC function in the user program, RTC cannot continue the count when power off or/and reset even HW has a 3V battery back-up circuit.

[Available functions]

Please try this code without any additional circuit!
You DON'T need a battery back up circuit and LCD as 1st trial. Please run it and push reset button to see what's happened.

Import programNucleo_RTC_battery_bkup_pwr_off_okay

In the past, you need modify rtc_api.c in mbed-dev source code. From this revision, you can just use RTC function all of conditions (Normal, Reset, Stand-by, Power OFF).

1) Not initialize RTC register configuration if back-up registers have some specific data (judge/battery back-up circuit works well).
2) Wait LSE (External Xtal) start up time (much longer than mbed original start up time) and use External 32.768kHz OSC if it's available.
3) Can set the present time and date from PC via USB VCOM line as first time initialization.
4) Show RTC register’s contents if you want.
5) Enter “Deep Sleep mode” if you want. In case L152RE, this is mandatory before power off. In addition, you can power off anytime if PB5 sets +5V voltage drop detection circuit.

If you run the program without any modification, you can see following screen in your PC.
If your screen shows "Internal Xtal", you may have a trouble for external Xtal oscillation!!.

[Software Configuration]

Before compile the program, you need to modify rtc_api.c file in the mbed-dev library.

Case F401RE or/and F411RE

(1) Update mbed-dev library if you want
(2) File rtc_api_F4xx.c copy to directory /mbed-dev/targets/hal/TARGET_STM/TARGET_STM32F4/
(3) Delete rtc_api.c in the same directory
(4) Edit rtc_api_F4xx.c
a)Delete 1st line
b)Delete last line

Case L152RE

(1) Update mbed-src library if you want
(2) File rtc_api_L152.c copy to directory /mbed-dev/targets/hal/TARGET_STM/TARGET_STM32L1/
(3) Delete rtc_api.c in the same directory
(4) Edit rtc_api_L152.c
a)Delete 1st line
b)Delete last line

Case F334R8 & L476

Same as F401&F411 case.
If you did it successfully the above modification, you can see following warning message after compile (Condition: Compile All).
Waring directive: "You modified mbed-dev rtc_api.c for SetRTC functions""#waring "You modified mbed-dev rtc_api.c for SetRTC functions""
Please confirm it.

[Hardware Configuration]

Case F401RE or/and F411RE (+ F334R8 & L476)

Please refer following picture (Circuit and layout)
/media/uploads/kenjiArai/rtc_nucleo001.png /media/uploads/kenjiArai/rtc_bkup0.jpg

Case L152RE

Please refer below "Additional information only for L152" part.
You DON'T need any additional modification for L152RE mbed board(don't remove SB45!!).

[Detail explanation of functions]

1) Forget everything!!

Current mbed library initialize RTC related registers every power on reset and other reset conditions.


void rtc_init(void){
    RtcHandle.Instance = RTC;
    // Enable Power clock
    // Enable access to Backup domain
    // !!!!!!!!! This is a problem !!!!!!!!!!!
    // Reset Backup domain

After running user program, it's too late to recover RTC time count and back-up data.
I use “mbed-dev” library and modify the file, "rtc_api.c" to prevent this issue.

2) How to recognize the back up is success or not?

Case F401RE or/and F411RE (F334 & L476)

VBAT power line connected to not only RTC but also Backup registers.
I use two(or three) 32bit registers to keep special value and check the value in the initialize routine.
If specific value remains, initialize routine skips most of the part and continue RTC function.


void rtc_init(void){
    // Check backup condition
    if (Check_Rtc_backup_reg()){// If back up data are remain, we can skip RTC initialize
    /* Write backup data into the backup RAM */
    RTC_WriteBackupRegister( RTC_BKP_DR0, RTC_DAT0 );
    RTC_WriteBackupRegister( RTC_BKP_DR1, RTC_DAT1 );

Case L152RE

L152 does NOT have VBAT pin.
It's easy to set RTC function for F401 & F411 CPU but not so easy for ST Nucleo L152RE mbed board due to back up HW configuration is different.
You can see following diagram in the manual.


We need to set CPU sleep condition and keep alive CPU VDD line.
This means we should check a condition and execute sleep command if you want to go to power off the board.

3) LSE(External 32.768kHz Xtal) oscillation is too slow to start.

The mbed library only wait 600msec (Changed more longer) then give it up to use LSE then use LSI even the external Xtal is on the Nucleo mbed board. Please refer this page to understand this issue.

[Extend other Nucleo series]

I briefly check STM32 series documentations and summarize as follows.

No.Board nameAvailable VBAT PinComment
2)L152RENoworks my environment
8)F334R8YesWorks fineMay 24th,'15
9)F401REYesWorks fine
10)F411REYesWorks fine
11)L476RGYesWorks fineMay 28th,'16

[Additional information only for L152]


An interrupt driven power off sequence needs additional hardware circuit because the function use PB5(or PB6) COMP input to check +5V live voltage drop detection.
If you don't have those circuit, please comment out following part.


//#define USE_IRQ_FOR_RTC_BKUP    // Use interrupt for enter RTC backup mode

backup current = 100uA (Standby mode(latest))
/media/uploads/kenjiArai/ss_stop_l152.jpg backup current = 280uA (Old revision Deep Sleep mode)
Use Renesas PS7200K-1A, FET Optocouple.

7 comments on Nucleo series RTC control under Power On/Off and Reset condition:

11 Feb 2015

Thanks for all the info. I'm currently working on a project using the l152re that requires the rtc. I'll try your program once the l152 is supported.

14 Feb 2015

I temporary made a L152 solution as sample program.
Need to set a Deep-sleep condition manually.
I have been trying low voltage detection automatically but not succeed yet.

27 Mar 2016

thank you, but i am still a bit confused as I am new to programming. can I just supply 5 v to e5v pin to back up my RTC in case of power out straight or I have to add a program for it

09 Oct 2016

Hi Kenji,

i have created an own PCB board with same mcu as NUCLEOF401RE and every code that i test on nucleo works also on my board. On my board i do not have a battery and LSE yet (that is planned for later) but when i test this project of yours i get only this output

Test Nucleo RTC Function
Internal Xtal for RTC

Show RTC registers
 Reg0  =0x00000000, Reg1  =0x00000000
 TR    =0x..000000, DR    =0x..002101, CR    =0x..000000
 ISR   =0x...00007, PRER  =0x..7f00ff, WUTR  =0x....ffff
 CALIBR=0x....0000, ALRMAR=0x00000000, ALRMBR=0x00000000
 WPR   =0x......00, SSR   =0x....0000, SHIFTR=0x....0000
 TSTR  =0x..000000, TSDR  =0x....0000, TSSSR =0x....0000
Show RCC registers (only RTC Related reg.)

and no time lines at all. What i did for testing is import the project in the online compiler, updated mbed-dev to latest version and clicked compile to generate the bin file (i have not updated rtc_api files). I think that "time(NULL);" at line 101 does not work anymore and hence the project hangs. Is it something that is code related? Can you give me a hint as to what might be the problem?


19 Oct 2016

Hi Anteo,
I cannot understand your enter sistuaion right now but I give you some hint.
If you are tring my program,"Nucleo_RTC_battery_bkup_pwr_off_okay", please modify main.cpp as follows.

int main()
    // keep original until here
    pc.printf("\r\n\r\nTest Nucleo RTC Function\r\n");
    myled = !myled;
    if (SetRTC(0) == 1) {
        xtal = 1;
    } else {
        xtal = 0;
    pc.printf(" Xtal for RTC\r\n");
    show_RTC_reg(); // only for debug purpose
    //-> change to below

    pc.printf(" Xtal for RTC\r\n");
    while(1) {
    	show_RTC_reg(); // only for debug purpose	
        myled = !myled;

You can check "DR", data register.
If DR data is increment every time, your LSI clock works well.
Very important issue
RTC function works with accurate LSE clock, 32.768KHz.
If you set LSI for RTC, RTC value is increment but you cannot use as "CLOCK(Watch)" due to deviation from 32.768KHz .

07 Jun 2017

Hello, Is there any tip on the vbatt on the f746ng disco board?

02 Sep 2017

Hi Fabrizio,
I'm sorry not to answer until today.
Are you talking DISCO-F746NG or Nucleo-F746ZG?
For Nucle-F746ZG, you can see follows.
For DISCO-F746NG, VBAT connected VDD directly. We cannot separate VBAT pin from VDD. /media/uploads/kenjiArai/746ng.png
DISC-F746NG = Not easy(NG!!)
Nucleo-F746ZG = You can do it (Remove SB156)

Please log in to post comments.