Using STM32 RTC backup registers

The STM32F4's RTC has 20 x 32-bit backup registers RTC_BKP_DR0 to RTC_BKP_DR19, which are maintained while in a power-off state if Vbat is supplied from a coin-cell battery or other.

There is no support in mbed's libraries for reading/writing from them, but again we can use STM HAL library calls instead.

I wrote a couple of functions as wrappers around the HAL calls:

uint32_t rtc_read_backup_reg(uint32_t BackupRegister) {
    RTC_HandleTypeDef RtcHandle;
    RtcHandle.Instance = RTC;
    return HAL_RTCEx_BKUPRead(&RtcHandle, BackupRegister);
}

void rtc_write_backup_reg(uint32_t BackupRegister, uint32_t data) {
    RTC_HandleTypeDef RtcHandle;
    RtcHandle.Instance = RTC;
    HAL_PWR_EnableBkUpAccess();
    HAL_RTCEx_BKUPWrite(&RtcHandle, BackupRegister, data);
    HAL_PWR_DisableBkUpAccess();
}

Currently, if you set the time using the mbed library, the backup registers are erased.To avoid this, we must use mbed-dev & edit your STM32`s rtc_api.c. Comment out

rtc_api.c rtc_init()

/*
        __HAL_RCC_BACKUPRESET_FORCE();
        __HAL_RCC_BACKUPRESET_RELEASE();
*/

then your data will survive setting the clock. Issue raised.


1 comment on Using STM32 RTC backup registers:

25 Jan 2017

Hi Dave,

accessing the RTC backup registers works on a Nucleo F401RE but not on a Nucleo F103RB.

Obviously the HW is different. The data sheet for the F103 says: "The backup registers are ten 16-bit registers used to store 20 bytes ..." For the F401: "The backup registers are 32-bit registers used to store 80 bytes..." That may explain why the wrapper functions (above) don't work for the F103.

Can somebody explain what to do to access the backup registers in the F103 ??

Please log in to post comments.