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:
Any help is welcomed!
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