Author Topic: minimum main loop size or timer interrupts don't work. SAMD21  (Read 1350 times)

0 Members and 1 Guest are viewing this topic.

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 441
  • Country: us
minimum main loop size or timer interrupts don't work. SAMD21
« on: August 23, 2022, 05:42:40 pm »
Got this bit of code.  TM3 is set up as an interrupt using match frequency generator.   SAMD21J16B

If the main while loop is empty the TM3 interrupt does not decrement the counter.  I assume its not firing.
Put in some fluff to consume cycles and it works.

What the hill ?

Code: [Select]
    LED_PIN_HIGH();

    unsigned int stupidcounter = 1000000;

    while (1)
    {

if(!OneMsTimerVal)
{
      OneMsTimerVal = 1000;   //  ????
      OneMsWork();

              PortInputDebounce(&PortA);
              PortInputDebounce(&PortB);

          LED_TOGGLE();

}


//  **********  if commented out TC3 interrupt does not fire.   OneMsTimerVal does not decrement. LED stays ON and does not toggle.
if(stupidcounter)
{
stupidcounter--;
}
else
{
stupidcounter = 1000000;
}
//**********************************************************



   }// while(1)

}// main














 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12516
  • Country: us
    • Personal site
Re: minimum main loop size or timer interrupts don't work. SAMD21
« Reply #1 on: August 23, 2022, 05:53:33 pm »
Are variables that used by both interrupt handler and the main loop declared volatile?

If not, look at the disassembly, I bet your whole main loop got optimized out. As far as the main loop can see OneMsTimerVal never changes. Adding more code prevents this optimization.
Alex
 
The following users thanked this post: snarkysparky

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 441
  • Country: us
Re: minimum main loop size or timer interrupts don't work. SAMD21
« Reply #2 on: August 23, 2022, 06:14:53 pm »
ataradov.

You are the man!!   It's why you get the big bucks!

making OneMsTimerVal volatile fixed the discrepancy.

I would never have though the main loop would get optimized out.

Thanks
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12516
  • Country: us
    • Personal site
Re: minimum main loop size or timer interrupts don't work. SAMD21
« Reply #3 on: August 23, 2022, 06:23:48 pm »
This happens all the time when people are not careful about interrupts.

Also, this is probably a weakness in the optimizer, since dummy code does not affect the part that still could be optimized. But it looks like bigger code prevents some optimizations from running.
Alex
 

Offline cv007

  • Super Contributor
  • ***
  • Posts: 1071
Re: minimum main loop size or timer interrupts don't work. SAMD21
« Reply #4 on: August 23, 2022, 07:12:23 pm »
The loop may remain because you have 'volatile' things inside (like LED_TOGGLE), but the value loaded for OneMsTimerVal will simply use the register cached value over and over-

https://godbolt.org/z/3r9hqq775

change to volatile and OneMsTimerVal is read 'directly' each time.

(asm lines ~15-18)


Some of these optimizations also depend on what kind of linkage the var/function has, so there may be times when external linkage is involved so does not fully optimize away like you would think, and if only internal linkage you may see 'full' optimization-
https://godbolt.org/z/osT35d8Ka
« Last Edit: August 23, 2022, 07:46:32 pm by cv007 »
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17863
  • Country: fr
Re: minimum main loop size or timer interrupts don't work. SAMD21
« Reply #5 on: August 23, 2022, 07:28:48 pm »
This is a very common issue that SHOULD be taught in schools (it usually isn't) and that should be written in bold on your desktop background until it becomes second nature!

It's unfortunate that most people - even in professional settings - usually learn this the hard way by accident, and then find out thanks to a colleague (if they are lucky) or through some web search/forums. Makes it look like black art or something.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->