STM32F411 nucleo RTC

29 Jan 2015

I'm wrestling with my new STM32F411 nucleo board and the built-in RTC. I've connected a lithium coin cell to the VBAT pin, so the RTC should keep time. After rebooting, however, the time has been reset to 1970.

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    
    time_t seconds = time(NULL);
    printf("Time as seconds since January 1, 1970 = %d\n\r", seconds);
    printf("RTC example\n\r"); 
    set_time(1420070400);
    
    printf("Date and time are set.\n\r");

    while(1) {

        time_t seconds = time(NULL);

        printf("Time as seconds since January 1, 1970 = %d\n\r", seconds);
        
        myled = !myled;      
        wait(1);
    }
}

The output:

Time as seconds since January 1, 1970 = -1265437696
RTC example
Date and time are set.
Time as seconds since January 1, 1970 = 1420070400
Time as seconds since January 1, 1970 = 1420070400
Time as seconds since January 1, 1970 = 1420070401
Time as seconds since January 1, 1970 = 1420070402
Time as seconds since January 1, 1970 = 1420070403
Time as seconds since January 1, 1970 = 1420070404
Time as seconds since January 1, 1970 = 1420070405
Time as seconds since January 1, 1970 = 1420070406

Now I hit the reset button

Time as seconds since January 1, 1970 = -1265437696
RTC example
Date and time are set.
Time as seconds since January 1, 1970 = 1420070400
Time as seconds since January 1, 1970 = 1420070400
Time as seconds since January 1, 1970 = 1420070401
Time as seconds since January 1, 1970 = 1420070402
Time as seconds since January 1, 1970 = 1420070403
Time as seconds since January 1, 1970 = 1420070404
Time as seconds since January 1, 1970 = 1420070405
Time as seconds since January 1, 1970 = 1420070406
Time as seconds since January 1, 1970 = 1420070406

How can I get the RTC to cooperate?

08 Feb 2015

set_time(1420070400); < right here you are not setting the time to a constant? >

You will have to change it to set the time to the time of your boot up.

I think that is the issue.

Rob :)

08 Feb 2015

Hi Tom,
Please try my program. Before using the program, you need to change BAT pin hardware connection.
Please refer following link.
/users/kenjiArai/notebook/nucleo-series-rtc-control-under-power-onoff-and-re/

Kenji

09 Feb 2015

Rob Townsend wrote:

set_time(1420070400); < right here you are not setting the time to a constant? >

You will have to change it to set the time to the time of your boot up.

I think that is the issue.

Rob :)

I also print the time before setting this constant, which returns -1265437696.

Kenji Arai wrote:

Hi Tom,
Please try my program. Before using the program, you need to change BAT pin hardware connection.
Please refer following link.
/users/kenjiArai/notebook/nucleo-series-rtc-control-under-power-onoff-and-re/

Kenji

I will try this tomorrow, and report my results here. Thanks!

Edit: Damn, used my private account for this post, instead of my work account. Oh well...