stm32f4real-time-clocknon-volatile

STM32 F4 - cannot read/write the 20 RTC backup registers


I'm having some difficulty making use of the 20x 32 bit (battery backed) backup registers in the F4 RTC. That is to say, whenever I read them, I get 0s, after writing to them, looking at the registers with the debugger they still seem to contain 0s.

The manual doesn't say anything about having to enable any clocks for this, simply that the PWR_CR_DBP bit in RTC_CR needs to be asserted to allow writes.

The code I have is basically:

backupRegisterBase = (U32 *)(&(RTC->BKP0R));    //  treat locations as sequential array of 32 bit registers

....

//  reading

regBitSet(&(RTC->CR), PWR_CR_DBP, FALSE);   //  ensure W bit cleared for backup register write protection 

for (U8 i = 0; i < RTC_BKP_NUMBER; i++) //  copy the NV registers
    backupShadowRegisters[i] = backupRegisterBase[i];

....

//  writing

regBitSet(&(RTC->CR), PWR_CR_DBP, TRUE);    //  W bit set for backup registers

for (U8 i = 0; i < RTC_BKP_NUMBER; i++)
    backupRegisterBase[i] = backupShadowRegisters[i];

regBitSet(&(RTC->CR), PWR_CR_DBP, FALSE);   //  W bit cleared for backup registers protection 

Not sure what I'm doing wrong here.


Solution

  • Not sure what I'm doing wrong here.

    You are using functions which are not "commonly known" without showing them. However,

    regBitSet(&(RTC->CR), PWR_CR_DBP, FALSE);

    sounds that you are trying to set the PWR_CR.DBP bit, but you are accessing the RTC_CR register (i.e. an entirely different register).

    Note, that you have to set PWR's clock in RCC before accessing PWR registers, and also that after enabling this clock you may need to wait .