Reputation: 121
I'm working on a project which 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 initially 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:
Upvotes: 1
Views: 653
Reputation: 121
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);
}
}
Upvotes: 2