cancel
Showing results for 
Search instead for 
Did you mean: 

How to read wall clock time on NUCLEO-L476RG

NRasm.1
Associate II

Hi,

I'm trying to set the RTC date and time when uploading my code to a NUCLEO-L476RG board. I've already configured the RTC in CubeMX, and it works as it should. But I've only managed to set the time manually by setting the BCD values for Hours, Minutes, Seconds and so on in CubeMX when configuring the RTC (or changing them directly in the RTC init function).

So I would like to know if there is a way to read the current wall clock time from the PC and use this to set the RTC. I've tried including time.h and using time(NULL) to read the wall clock time, but it returns -1 (which according to the docs means calendar time is not available). Do I need to configure the GCC toolchain to be able to use time.h, or is there another way to read wall clock time onto the NUCLEO board?

Any help is much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

There is no automatism built into any toolchain I know, which would set the 'L476 real time from PC.

So you have to write it yourself. You need some mechanism through which you connect 'L476 to PC - e.g. UART - a program on PC side which transmits the real time, and a program on 'L476 side which receives the real time and writes it into RTC.

Maybe the 'L476-side part could be avoided by writing a script utilizing any debug vehicle available (e.g. gdb server, gdb itself, heck maybe even STLink Utility CLI). But if you need to ask, how, then this is not a viable option for you.

[EDIT] found this related thread saying basically something similar https://stackoverflow.com/questions/52274167/update-rtc-on-stm32f4-when-flashing-the-uc

JW

View solution in original post

4 REPLIES 4

There is no automatism built into any toolchain I know, which would set the 'L476 real time from PC.

So you have to write it yourself. You need some mechanism through which you connect 'L476 to PC - e.g. UART - a program on PC side which transmits the real time, and a program on 'L476 side which receives the real time and writes it into RTC.

Maybe the 'L476-side part could be avoided by writing a script utilizing any debug vehicle available (e.g. gdb server, gdb itself, heck maybe even STLink Utility CLI). But if you need to ask, how, then this is not a viable option for you.

[EDIT] found this related thread saying basically something similar https://stackoverflow.com/questions/52274167/update-rtc-on-stm32f4-when-flashing-the-uc

JW

Where exactly are you expecting this information to come from??

On internet connected devices, or programming stations, one could presumably use SNTP

Windows PC have API calls for reading time, and pretty much every C library I've used on Windows/Linux support the time() related calls.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thought I'd looked everywhere, can't believe I didn't find that post. I was asking because I've previously used Arduino IDE; where I could read the PC time from the uc. Not entirely sure how they made it possible, but I guess the Arduino IDE has some sort of API for this. Since cubeIDE has a lot of added functionality, I thought maybe the same could be the case here. But it wasn't anywhere in the docs, so i wasn't expecting anything. Asking just in case. Guess I'll need some script for the PC to transmit the date and time through the UART. Thanks for the reply!

The device I'm working on will be connected to a smartphone via bluetooth, so maybe an app for the phone could be an option, so it syncs the time whenever it connects. Otherwise I'll need a script for the PC to sync when it's plugged in. Thanks for your advice,