2015-07-27 01:20 AM
Hello
I am trying to configure the RTC of a cheap ARM board with STM32F103C8T6: . I am using ST LINK V2-1 and it works, the clock is set for 72 MHz with HSE. The board also has a 32 KHz LSE but when I try to configure it it behaves somewhat strange. My setup routine is:RCC->APB1ENR |= (RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN);
PWR->CR |= PWR_CR_DBP; // access to backup register
RCC->BDCR |= (RCC_BDCR_LSEON); // Enable LSE
while((RCC->BDCR & RCC_BDCR_LSERDY) == 0); // wait until LSE is ready
RCC->BDCR |= RCC_BDCR_RTCSEL_LSE; // Select LSE
RCC->BDCR |= RCC_BDCR_RTCEN; // Enable RTC
RTC->CRL &= ~RTC_FLAG_RSF; // Clear RSF
while((RTC->CRL & RTC_FLAG_RSF) == 0); // Wait
while(!(RTC->CRL & RTC_CRL_RSF)); // Wait for the RSF bit in RTC_CRL to be set by hardware
while((RTC->CRL & RTC_CRL_RTOFF) == 0); // Wait for RTOFF
RTC->CRL |= RTC_CRL_CNF; // Configuration mode
RTC->PRLH = 0x0000; // Preload register
RTC->PRLL = 0x7FFF; // Per datasheet for 1 sec signal
RTC->CNTH = 0x0000;
RTC->CNTL = 0x0000;
RTC->CRL &= ~RTC_CRL_CNF;
while((RTC->CRL & RTC_CRL_RTOFF) == 0);
I can confirm all registers get the value I intend to put into them. In my main loop I print the value of the RTC counter every second:
while (1)
{
HAL_Delay(1000);
tm = 0;
tm = RTC->CNTH <<
16
;
tm |= RTC->CNTL & 0xffff;
trace_printf(''%02u:%02u:%02u --- CNT: %u\n'', (tm/3600), ((tm%3600)/60), ((tm%3600)%60), tm);
}
tm here is an uint32_t. And what happens is:
Clock frequency: 72000000 Hz 00:00:00 --- CNT: 0 00:00:01 --- CNT: 1 00:00:02 --- CNT: 2 00:00:02 --- CNT: 2 00:00:03 --- CNT: 3 00:00:03 --- CNT: 3 00:00:04 --- CNT: 4 00:00:05 --- CNT: 5 00:00:05 --- CNT: 5
This is my output. It appears the counter is not incrementing every second, but is also not incrementing every two seconds, and is sort of unpredictable. The HAL delay is working fine and gives 1 second, I checked it with a logic analyzer. When I try to configure the RTC with HAL functions and read the clock with the HAL driver I get the same result. The RTC prescaler is set to 0x7fff - I see the value when debugging.
Thanks
#stm32-rtc
2015-07-27 04:24 AM
And have you checked the 32 KHz with a scope? Does it output via MCO, I don't recall.
What if you use the LSI, assuming a 40 KHz clock (prescaler), does that appear more timely?2015-07-27 07:21 AM
I don't have a scope right now. I tried using the LSI as you suggested and it is working fine. I tried outputting the frequency on the Tamper pin PC13, but I get some kind of garbage on the logic analyzer. However, now my RTC is working with the LSE. The only thing I did was to remove the board and examine the connection, which appeared to be all ok.
Edit: Nope, it worked until the next reset. Now it is not working again.2015-07-27 11:07 AM
Problem solved by disabling PC13 pin - reference manual states it should not be used for current sourcing, and I was using it for board LED (via resistor). Now the RTC works fine with LSE.
I have connected a battery to the Vbat pin (plus resistor and diode) and my ultimate goal is to be able to keep track of time when the device is not powered from the main supply. However, after a power cycle the RCC->BDCR register does not keep its values and the RTC is switched off. This means I need to initialize it once again every power cycle. I am able to write to this register when initializing the RTC. What do I need to do in order to make the RTC work when powered from Vbat? The datasheet states that LSEON, LSEBYP, RTCSEL and RTCEN bits in the BDCR register are in the Backup domain and should retain their value on power cycle when Vbat is present, but this does not happen.2015-07-27 11:24 AM
What do I need to do in order to make the RTC work when powered from Vbat?
Place the device in STANDBY before the primary supply collapses.2015-07-27 12:58 PM
Thank you for the answer. Do I really need to enter Standby mode before I switch off the supply? I plan on using an on/off switch for the device and I don't really know how to do the ''housekeeping'' on the STM32 when the power goes down.
I am looking at reference manual RM0008 and it shows that the Backup domain is connected via an analog switch to Vbat. This switch is controlled by a low voltage detector. The manual does not say anything about entering standby mode so I thought I would not need it. Just before I started with this particular board I was playing around with an STM32F411RE board and it did not need entering Standby mode before switching off the supply; the RTC kept its value with Vbat connected. I am thinking of using the Programmable voltage detector and monitor the supply voltage: if it goes under a threshold, it would generate interrupt possibly enter Standby mode. When the input voltage rises, another interrupt will be generated and Standby mode will be exited. Do you think this would be a good idea?2015-07-27 01:14 PM
There may be other ways, I haven't used an F1 in a product for 5 years or so. I know for a fact I can get the part into STANDBY and have the chip and the RTC function for days/weeks, and clock properly, exit via alarms etc. I can also recognize when the chip resets out of STANDBY and not trample on the RTC/LSE, etc.
The LSI is definitely non-functional in the low power domain.One can bury man-weeks of time into the Low Power modes and behaviour of the STM32 parts.Don't really have much insight into the board you're using.2015-07-28 09:39 AM
I have used the RTC (with LSE) powered by a large capacitor @ Vbat when the main power supply is absent, without using Standby on an F103 micro.
I think the key is to ''recognize when the chip resetsout of STANDBY
and not trample on the RTC/LSE, etc.''.2015-07-29 01:02 PM
Got it working finally. The problem was a loose connection from the battery's negative to the chip's GND, and this caused reset of the Backup domain when main supply was off. Now RTC works as expected with the above configuration (and no IO on PC13).