Electronic – Using the ST-LINK Utility to configure the RTC to current time

rtcstm32

What I was doing

I am using a STM32 microcontroller and for my project I need to be able to send data at specific times of the day (say, at 11am and 2pm). The microcontroller needs to know what time it is before I can achieve this. Unfortunately, I only have an one-way communication, and I therefore cannot request the current time from the network.

Therefore I want to set the current time, directly after I am done programming. I knew I am able to write data to the flash using the ST-LINK Utility Command Line Interface (ST-LINK_CLI) using the following command:

ST-LINK_CLI.exe -w32 <Address> <data> -Rst

I wrote a simple test script that writes the Unix time stamp to some address not currently in use by the code.

I was about to write a function to read the time-stamp and use it to set the RTC time. Until I read the following in the ST-LINK Utility User Manual:

-w32 supports writing to Flash memory, OTP, SRAM and R/W registers.

When the manual says it supports writing to the R/W registers, does this mean I can directly access the RTC registers and set them? I have tried, but I can't seem to write to these registers.

I would prefer to use this method if it does, as I would not need to write a specific function to handle it on the microcontroller anymore. This means that I can set the RTC time of any microcontroller we currently have in use, instead of needing to update their code.


What I intended to do

To set the RTC registers, I tried to do the following steps, as described in the STM Reference Manual:

  • set DPB bit in PWR_CR register
  • write 0xCA to RTC_WPR register
  • write 0x53 to RTC_WPR register
  • stop the RTC by setting the INIT bit in RTC_ISR register
  • select the 1Hz clock by writing to the RTC_PRER register
  • load the current time by writing to the RTC_TR register
  • load the current date by writing to the RTC_DR register
  • start the RTC by resetting the INIT bit in RTC_ISR register

To access the registers, I have used the following addresses:

  • PWR_CR: 0x4000 7000
  • RTC_WPR: 0x4000 2824
  • RTC_ISR: 0x4000 280C
  • RTC_PRER: 0x4000 2810
  • RTC_TR: 0x4000 2800
  • RTC_DR: 0x4000 2804

What went wrong

I am not able to write to either of these registers. With the ST-LINK Utility, I get the following message:

Error occured during memory writing!

Using the ST-LINK_CLI:

Writing 0x00000100 at 0x40007000…Error!

Reading these registers is no problem, but I cannot write to them using either the ST-LINK Utility or its Command Line Interface.


The question

How can I write to the R/W registers using the ST-LINK Utility?

Is there some kind of write protect to allow writing to the RTC registers which I overlooked?

Best Answer

Some registers are only legal for a specific access width (ie, -w32 may not be correct), or may not read back the written values which could cause a problem with verification.

There might also be sequence or state restrictions on accessing things.

An option that should work around most conceivable issues would be to craft a tiny program to do the job which would linked to run in RAM. You could substitute the data into its binary after figuring out the offset, upload the modified version, and run it. Or you could have the program get values from a region of RAM outside of the file's extents, which you would set before running. With finer grained control of the stlink you could also pass values in CPU registers, though you might(?) need the alternate open source command line program rather than ST's to do that (this small routine in RAM method is incidentally how that program accomplishes writing to flash)