1
\$\begingroup\$

I have configured an RTC module on a uC to generate interrupts every second (~32 kHz module clock with a 0x7FFF prescaler in between the counter and the clock - I hope so, seems like ~32k is the default value, and it would be a shame if the error arises from an incorrect clock being connected to the module). However, it seems like seconds counter register is incremented every time a single assembly instruction is carried out (sometimes every two assembly instructions), thus generating interrupts at an astonishingly fast rate. What could be the cause of this behaviour? And if you were to troubleshoot it without being able to ask this question on forums like I am doing, how would you go about finding the issue?

In response to comments - RTC clock indeed may only be connected to either an internal or an external oscillators, both oscillating at ~32 kHz. The uC is an Infineon XMC series with Cortex M-4 processor. I can hardly think of any section of my code that I can post, as it's simple register configuration that (a) Enables periodic RTC events, (b) Enables SCU (System Control Unit - in charge of clocks, hibernation domain and power/reset domains, etc.) RTC periodic interrupts, (c) Configure NVIC properties. As I've noted in one of the comments; the part that confuses me the most about this problem is that the 'seconds' value seems to be correct when passed to a hyperterminal, yet the refresh rate (putty terminal is refreshed every time an interrupt is handled) is very fast - yet the seconds count seems to increment every second, meaning that the timekeeping is correct.

One more small update on the issue. Seems like the RTC timer is working correctly and even manages to keep up with real life for at least an hour when I leave it running in the background. However, what happens is the interrupt sequence gets stuck in a while loop for no particular reason - if I set up the interrupts to trigger every minute instead of every second, then the program will issue an interrupt one minute after I start running it, and then execute that interrupt over and over again, flickering the terminal very fast.

\$\endgroup\$
  • 1
    \$\begingroup\$ Which microcontroller are you using? The appropriate troubleshooting methods will depend on what's available in the specific architecture... \$\endgroup\$ – bitsmack Jan 28 '16 at 6:54
  • 1
    \$\begingroup\$ Also, please post relevant sections of your code. It'll help us to figure out the cause :) \$\endgroup\$ – bitsmack Jan 28 '16 at 6:55
  • 1
    \$\begingroup\$ Also look at the interrupt vector table to verify your seconds int handler is installed on ONLY the correct vector \$\endgroup\$ – Brian Drummond Jan 28 '16 at 11:22
  • 2
    \$\begingroup\$ Good edit! That's the clue. Are you clearing the interrupt flag at the end of your ISR? If not, it will jump right back in. \$\endgroup\$ – bitsmack Jan 28 '16 at 22:15
  • 1
    \$\begingroup\$ If you have found the solution, add it as an answer rather than an edit to your question, and then mark it as accepted. \$\endgroup\$ – Tom Carpenter Jan 28 '16 at 23:24
3
\$\begingroup\$

The interrupt flag is not cleared automatically for this particular interrupt/uC. The RTC counter works fine, however, once an interrupt is issued, if the flag is not cleared at the end of the handler routine, upon returning from exception handling, the processor will see that the flag is raised and enter the same routine again, thus putting itself in one big while loop. One of my other observations is that this is not always the case - some flags are cleared automatically, whereas others you have to clear via software.

\$\endgroup\$
  • \$\begingroup\$ And don't forget to accept it so it shows up on top and other readers can find it quickly. +1 \$\endgroup\$ – Transistor Jan 28 '16 at 23:46
1
\$\begingroup\$

That sounds like you have selected the wrong clock source. Some microcontrollers allow you to use the CPU clock as input to the timer block, to allow for high-precision timers.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.