1
votes

I'm working on a project wich integrates a STM32L051R8T6 chipset and I need the RTC functionality for some functions, like slow timers and sleep wakeup. However if I call Mbed's set_time() to set the RTC, the program hangs or doesn't execute as expected.

Before implementing anything I'm trying to run Mbed's RTC example code: https://os.mbed.com/docs/mbed-os/v5.8/reference/rtc.html , but I'm having no luck. The RTC seems to be set with set_time(), however, when I call time(NULL) the I always get the initialy set time. Looks like the RTC is not counting.

I'm compiling the code for the STM32L053R8 using Mbed's online compiler, not sure if that target is very different from mine and that is what causes the issue.

This is the code I'm trying to execute:

#include "mbed.h"

int main() {
    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37

    while (true) {
        time_t seconds = time(NULL);

        printf("Time as seconds since January 1, 1970 = %d\n", seconds);

        printf("Time as a basic string = %s", ctime(&seconds));

        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        printf("Time as a custom formatted string = %s", buffer);

        wait(1);
    }
}

When it doesn't hang the RTC time doesn't change:

Terminal output

Any help is welcomed!

1
Are you on DISCO-L072CZ-LRWAN1 board or on a custom board? It has the RTC enabled on the dev board. You could check the implementation of rtc_api.c for your target to see if anything weird is going on, maybe the clock source for RTC is not running? Have you changed anything there?Jan Jongboom
It is a custom board based on the STM32L051R8T6. But you were correct, the solution was related with the rtc_api files. In my case I had to inlcude the full path for the header file and call rtc_init() at the begining of the code. That function automatically set the clock source for the RTC. After this the code was no longer hanging and the RTC ticked as expected. Thanks for the help. I was also recommended to use an external Xtal for stability.Nedo

1 Answers

2
votes

Including full path for the rtc_api.h file and adding rtc_init() at the begining of the code solved the issue. The rtc_init() function takes care of selecting the available clock source. The working code looks as follows:

#include "mbed.h"
#include "mbed/hal/rtc_api.h"

int main() {
    rtc_init();
    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37

    while (true) {
        time_t seconds = time(NULL);

        printf("Time as seconds since January 1, 1970 = %d\n", seconds);

        printf("Time as a basic string = %s", ctime(&seconds));

        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        printf("Time as a custom formatted string = %s", buffer);

        wait(1);
    }
}