cancel
Showing results for 
Search instead for 
Did you mean: 

RTC fine until I write to the counter

willcfj
Senior
Posted on February 01, 2016 at 05:26

I'm using an STM32F100RB.  I've started with the reference code RTC/Calendar/main.c and it all works fine until I try to use the RTC_SetCounter function.  All is OK when it gets called on startup with a value of 0. I get my regular interrupts every 1 second as expected.  However, as soon as I try to set a time later after the counter is running, I get stuck in an infinite RTC interrupt loop.

I'm still pretty new to this, but in doing some debugging, it looks like even though I call

RTC_ClearFlag(RTC_FLAG_SEC)

RTC_ClearITPendingBit(RTC_IT_SEC)

the RTC_CRL bit 0 for the seconds interrupt never gets cleared, so when I return from the interrupt, I just go back into it again.  I tried an infinite loop inside the ISR to keepon writing 0 to RTC_CRL until the bit gets cleared and it gets stuck there, I believe confirming this to be the issue. 

Weird part is that ISR works fine servicing 1-second interrupts until I do the write to the counter registers, so clearly gumming something up.  I'm using the reference code subroutines.  I figure there is something else ancillary I have messed up. 

Any suggestions appreciated.

2 REPLIES 2
willcfj
Senior
Posted on February 04, 2016 at 04:14

This may end up looking more like a blog, but chipping away.  Root issue does seem that the write is killing any further transactions to the RTC.  Besides not being able to clear the SECF flag, I noticed in the IRQ routine that RTOFF is 0 and stuck at 0.  As an experiment, I disabled interrupts before the write and then just did the 5-step write process (check RTOFF = 1, CNF=1, write register, CNF = 0, poll for RTOFF = 1.  I get stuck in the second RTOFF poll forever.  Hence interrupts is where CPU was getting stuck, but only see it as interrupts have priority over mainline code.  Now just need to see why RTOFF gets stuck.  I know the actual write never happens AND know that the RTC itself keeps on going as after resetting the CPU and reading the RTC count, it is clear it just kept on running while the CPU was locked up. No time gets lost.  RTOFF is for writes, don't know why it stops interrupts from being cleared.

willcfj
Senior
Posted on February 13, 2016 at 03:19

SOLVED!  With more poking around, I saw that the BKPEN bit in RCC_APB1ENR was 0 as well as DPB in PWR_CR.  Armed with that information I went back to the reference RTC code from ST.  The commands to set those bits were behind a #ifdef putting the RTC clock out on a pin, something I didn't want, so was #ifdef'd out.  Correct implementation is that the first two API calls after

#ifdef RTCClockOutput_Enable

to and () need to be executed regardless of using the clock output. 

will