RTC driver uses BKP registers!

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

RTC driver uses BKP registers!

Post by alexblack »

Before ChibiOS V.2.5.1 I used BKP registers freely in STM32F103 board, but now some of it are used in RTC_LLD:

Code: Select all

136 void rtc_lld_init(void){
137
138  /* Required because access to PRL.*/
139  rtc_lld_apb1_sync();
140
141  /*
142   * Writes preload register only if its value is not equal to desired value.
143   *
144   * Ref CD00171190: RM0008 Reference manual Cls 18.4.3 The RTC->PRL registers
145   * are write only. We must store the value for the pre-scaler in BKP->DR1
146   * and BKP->DR1 so we know it has been set.
147   * The pre-scaler must not be set on every reset as RTC clock counts are
148   * lost when it is set.
149   */
150  if ((STM32_RTCCLK - 1) != ((((uint32_t)BKP->DR1) << 16) | BKP->DR2)){
151    rtc_lld_acquire();
152    RTC->PRLH = (uint16_t)((STM32_RTCCLK - 1) >> 16) & 0x000F;
153    BKP->DR1  = (uint16_t)((STM32_RTCCLK - 1) >> 16) & 0x000F;
154    RTC->PRLL = (uint16_t)(((STM32_RTCCLK - 1)) & 0xFFFF);
155    BKP->DR2  = (uint16_t)(((STM32_RTCCLK - 1)) & 0xFFFF);
156    rtc_lld_release();
157  }
...


Is it necessary to do so and may need to add the settings in 'halconf.h' file what registers to use for this or save this values elsewhere?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: RTC driver uses BKP registers!

Post by Giovanni »

Hi,

The reason is in the comment, and we must use a non volatile register for this. I am not sure if there is an alternate way to verify if the RTC has already been initialized.

Giovanni
alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

Re: RTC driver uses BKP registers!

Post by alexblack »

I'm sure you can not just use the resources without defining them anywhere - it's like reserve a memory with const address like this:

Code: Select all

  static int *hidden = (int *) 0x40001000;
  *hidden = 10;
   ...

Because no compiler or the programmer will not know that the cell is used and may try to use it again! For example I use BKP-DR1 in BOOT_LOADER and now have problems.
We must do something about it.
Since the value for the prescaler is a constant, then only one bit is need to determine the prescaler is reset and it will reset when BKP power off. Somewhere in BKP hardware must be a status bit for this, but I can not find it so quickly.
alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

Re: RTC driver uses BKP registers!

Post by alexblack »

I studied the STM32 RTC V.1 DRIVER and found that the macro function probably has a mistake:

Code: Select all

55 /**
56  * @brief   Wait for synchronization of RTC registers with APB1 bus.
57  * @details This function must be invoked before trying to read RTC registers
58  *          in the backup domain: DIV, CNT, ALR. CR registers can always
59  *          be read.
60  *
61  * @notapi
62  */
63 #define rtc_lld_apb1_sync() {while ((RTC->CRL & RTC_CRL_RSF) == 0);}


Because datasheet (Ref CD00171190: RM0008 Reference manual Cls 18.4.2) says:
Bit 3 RSF:Registers synchronized flag
This bit is set by hardware at each time the RTC_CNT and RTC_DIV registers are updated
and cleared by software. Before any read operation after an APB1 reset or an APB1 clock
stop, this bit must be cleared by software, and the user application must wait until it is set to
be sure that the RTC_CNT, RTC_ALR orRTC_PRL registers are synchronized.
0: Registers not yet synchronized.
1: Registers synchronized.


I think code must be as:

Code: Select all

63 #define rtc_lld_apb1_sync() {RTC->CRL |= RTC_CRL_RSF; while ((RTC->CRL & RTC_CRL_RSF) == 0);}
alexblack
Posts: 277
Joined: Mon Sep 24, 2012 3:52 pm
Been thanked: 33 times

Re: RTC driver uses BKP registers!

Post by alexblack »

Giovanni
The reason is in the comment, and we must use a non volatile register for this. I am not sure if there is an alternate way to verify if the RTC has already been initialized.


I think RCC_BDCR_RTCEN bit in RCC->BDCR register must be used to detect that RTC hasn't been initialized yet but it is analyzed in hal_lld_backup_domain_init():
file: hal_lld.c

Code: Select all

...
69  /* If the backup domain hasn't been initialized yet then proceed with
70     initialization.*/
71  if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
72    /* Selects clock source.*/
73    RCC->BDCR |= STM32_RTCSEL;
74
75    /* RTC clock enabled.*/
76    RCC->BDCR |= RCC_BDCR_RTCEN;
...

It is necessary to either move the prescaler setting here or somehow say this to function 'rtc_lld_init'.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: RTC driver uses BKP registers!

Post by Giovanni »

I will look into this, there is also a bug reported on the RTCv1.

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: RTC driver uses BKP registers!

Post by barthess »

If I remeber correct, the reasons to store whole value of preload register is to avoid initializing of this register every system startup.
alexblack wrote:Because no compiler or the programmer will not know that the cell is used and may try to use it again! For example I use BKP-DR1 in BOOT_LOADER and now have problems.
We must do something about it.

Agree with you and propose following:

Code: Select all

#if !defined(RTCV1_BKP_PRLH) || !defined(RTCV1_BKP_PRLL)
#error "You must specify BKP register to store RTC preloader values yourself"
#endif

in user code (in board.h???) must be defined following symbols:

Code: Select all

#define RTCV1_BKP_PRLH ((uint32_t)BKP->DR1)
#define RTCV1_BKP_PRLL ((uint32_t)BKP->DR2)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: RTC driver uses BKP registers!

Post by Giovanni »

It can be a normal configuration option.

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: RTC driver uses BKP registers!

Post by barthess »

Giovanni wrote:It can be a normal configuration option.

Good idea. Let it be in mcuconf.h near the RTC

alexblack wrote:I think code must be as:
Code: Select all
63 #define rtc_lld_apb1_sync() {RTC->CRL |= RTC_CRL_RSF; while ((RTC->CRL & RTC_CRL_RSF) == 0);}

Syncing with RTC is slow process (some LSE clocks). This bit must be cleared only once after APB1 start/reset
so it must be done in begining of rtc_lld_init().

Giovanni, what do you think about?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: RTC driver uses BKP registers!

Post by Giovanni »

Just at beginning would be ideal IMO. Barthess please look at the tracker there is an RTC related bug I have yet to examine.

Giovanni
Post Reply