Update RTC on STM32F4 when flashing the uC

Viewed 507

Is there a way to update the RTC with computer information when the program is uploaded on the STM32F4 ? For example, the information of date and hour on the computer is: 12h40 11/09/2018, and when I flash the microcontroler with IAR/AC6, then the RTC is set whith these pieces of information ?

4 Answers

My idea is to make a script which you run after flashing (I'm not familiar with IAR, but hope it is possible to run something after FLASH) and this script will set over debugging port RTC registers with current time.

If you use ST-Link/V2 hardware for flashing and you have any experience with python, you can use for that pyswd library. is easy, just in python script get actual time and set appropriate registers in connected MCU.

You can modify most port registers through SWD (or JTAG) in the same way as flash and RAM addresses.

Maybe some of the budget-class debug adapters lack a scriptable option, but at least the mainstream range of adapters allows you to write values to any address the CPU can reach through the internal buses (AHB, APBx etc.). If you read the reference manual carefully, you can at least find out the raw addresses (and bit patterns) of the registers you have to assign. If you are lucky, the software tools of your debug adapter even provide you a feature where you can do this with lots of syntactic sugar - without having to dive into the manuals.

As far as I see it, you have two options:

  1. Bake PC time into your firmware. To do this, you could build your firmware with some area of memory, or indeed the Makefile, dedicated for real-time values, which are populated by a script during the build. When the firmware first runs, it would read from these locations and update the RTC. If you went for this approach, your time would always be out by some amount, depending on how long it takes to build and download the firmware to the device.
  2. Write the PC time on first power-on. To do this, you would need an interface between the PC and the device, and an application running on the PC. The application would be commanded to get the current PC time and send it to the device over the interface (e.g., a serial cable), and the RTC would then be updated. This is a better approach, since device time will be more tightly coupled to PC time, and you could script this also. The downside here is you have to build the interface.

Also, as an aside, consider whether your application actually needs 'real-time'. Many embedded devices can make do with relative time, e.g., milliseconds elapsed since power-on.

IMO easiest way is to modify openOCD TCL script to upload (after flashing) small program which will update the RTC. 15 mins of work

Related