Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 6 months ago.
Why is RTC not counting on STM32L0?
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.
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.
Any help is welcomed.
1 Answer
5 years, 6 months ago.
This is a baseline target, I would suggest to use Mbed (os2) and try this example first:
https://os.mbed.com/teams/ST/code/Nucleo_display_time/file/361a24c01ed7/main.cpp/
Import this program and it should include Mbed (os2).
If it works then try dropping the code onto the L051 to check.
If it does not work cross check the RTC registers in the RM's, there can be subtle clock differences.
I assume you have the 32Khz RTC Xtal fitted and checked on a scope that it is actually working?
Hi, thanks for the reply! I've tried the code you shared but still no luck. I don't have an external RTC xtal, I was planning to use the internal one. Is there a workaround?
posted by 23 Apr 2019I'm not sure tbh, I have always use the 32kHz for time and low power wake-up solutions. Generally the RTC needs more accuracy than IRC. Some ST's have a LPTIM clock that can be used to clock the RTC section, check the RM or better, have a look at the targets.json file and search L053, you should find the target overrides, I did see this, try setting '0' to use RTC wakeup.
You will need MBED-dev library to get this file.
"NUCLEO_L053R8": { "inherits": ["FAMILY_STM32"], "supported_form_factors": ["ARDUINO", "MORPHO"], "core": "Cortex-M0+", "extra_labels_add": ["STM32L0", "STM32L053x8", "STM32L053R8"], "config": { "clock_source": { "help": "Mask value : USE_PLL_HSE_EXTC | USE_PLL_HSE_XTAL (need HW patch) | USE_PLL_HSI", "value": "USE_PLL_HSE_EXTC|USE_PLL_HSI", "macro_name": "CLOCK_SOURCE" }, "lpticker_lptim": { "help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer", "value": 1 }, "lpticker_delay_ticks": { "help": "In case of lpticker_lptim=1, set lpticker_delay_ticks=3", "value": 3, "macro_name": "LPTICKER_DELAY_TICKS" } }, "detect_code": ["0715"], "device_has_add": ["ANALOGOUT", "CRC", "SERIAL_FC", "SERIAL_ASYNCH", "FLASH"], "default_lib": "small", "release_versions": ["2"], "device_name": "STM32L053R8" },
Thanks for the advice! I've tried to import the mbed-dev lib to an existing project but the request sadly timed out every time. However, I have the project exported and I'm using arm-gcc to compile the code offline. I have access to targets.json file, it looks as follows:
"NUCLEO_L053R8": { "inherits": ["FAMILY_STM32"], "supported_form_factors": ["ARDUINO", "MORPHO"], "core": "Cortex-M0+", "extra_labels_add": ["STM32L0", "STM32L053x8", "STM32L053R8"], "config": { "clock_source": { "help": "Mask value : USE_PLL_HSE_EXTC | USE_PLL_HSE_XTAL (need HW patch) | USE_PLL_HSI", "value": "USE_PLL_HSE_EXTC|USE_PLL_HSI", "macro_name": "CLOCK_SOURCE" }, "lpticker_lptim": { "help": "This target supports LPTIM. Set value 1 to use LPTIM for LPTICKER, or 0 to use RTC wakeup timer", "value": 1 } }, "overrides": {"lpticker_delay_ticks": 4}, "detect_code": ["0715"], "device_has_add": ["ANALOGOUT", "CRC", "SERIAL_FC", "SERIAL_ASYNCH", "FLASH"], "default_lib": "small", "release_versions": ["2"], "device_name": "STM32L053R8" },
I've tried to set the "value" field to 0 and add
"target.clock_source": "USE_PLL_HSI"
to overrides but it refused to work. Does it make sense to edit the registers by hand to select the clock input, or is there a a function than can be called in within the code to do so?
And talking about the targets.json, is it enough to recompile the whole code again or something else has to be tweaked so that the changes take effect?
posted by 23 Apr 2019Okay, there may be a problem with the library revision files you are using, I had something similar with the F103 blue pill board, try revision 187 that's what I use. All the ST's RTC's seem to work with this, but as I say, never tried without 32kHz Xtal.
A 'compile all' or 'Build all' will action any changes on all files.
Something else,
Check page 183/1034 of the RM:
LSE or LSI is the only clocking options for low power timer/wake modes. Another consideration on your design is if you need RTC accuracy on a system reset then it can only be LSE clocked which means external 32 kHz Xtal. I would recommend to fit the Xtal.
posted by 24 Apr 2019Hi, thanks for you time again. In my local project I'm using a precompiled mbed OS. I wanted to import the mbed-dev rev you said to the online compiler and then export it to a local folder, however I'm not able to do so, page starts loading and the a timeout ocurrs. I've tried to export the mbed-dev lib and compile it and although the compilers starts fine, it encounters a problem with the startup_stm32L053xx.o file and fails. Not really sure on how to implement the mbed-dev lib in my local project. I've tried to swap the target.json file, and wrote this in the main.c:
#include "mbed.h" Serial pc(PA_9, PA_10); int main() { pc.printf("RTC example\n"); set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC pc.printf("Date and time are set.\n"); }
The program hangs during the execution of the set_time() function. I'm taking into account your recomendation of the external Xtal, for the next version of my project, but now I need to figure out how to do it without it.
posted by 24 Apr 2019Good news, I made it work! I had to use rtc_init() at the begining of the code so that the source clock could been selected automatically. I had to include the full path to the HAL header file. The working code looks like this:
#include "mbed.h" #include "mbed/hal/rtc_api.h" Serial pc(PA_9, PA_10); int main(){ rtc_init(); pc.printf("RTC example\n"); set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC pc.printf("Date and time are set.\n"); while(1){ time_t seconds = time(NULL); //printf("Time as seconds since January 1, 1970 = %d\n", seconds); pc.printf("Time as a basic string = %s\r\n", ctime(&seconds)); wait(1); } }
Thanks for the help though! Made me investigate the mbed files and find the solution.
posted by 24 Apr 2019