adafruit / Adafruit_ZeroTimer

happy wrappers for TC modules 3,4,5 on SAMD21
MIT License
32 stars 25 forks source link

Disabling then re-enabling timer fails on Metro M4 Express #15

Open jstanle1 opened 3 years ago

jstanle1 commented 3 years ago

First, thanks for this timer wrapper -- I understand it's for internal use, but it's saved me a lot of trouble using and understanding SAMD21/51 timer hardware.

I wanted to be able to turn a timer on and off, so I've modified the timer_callback example to call zerotimer.enable() in the loop to periodically enable and disable the timer. This running on a Metro M4 Express board, Arduino IDE 1.8.12,

The example loop now looks like:

int c = 0;
bool ed = true;

void loop() {
  Serial.println("tick");
  delay(1000);
  c++;
  if (c >= 10) {
    c = 0;
    if (ed) {
      Serial.println("disable");
      zerotimer.enable(false);
      ed = false;
    } else {
      Serial.println("enable");
      zerotimer.enable(true);
      ed = true;
    }
  }
}

I found that this would disable the timer properly, but then the call to zerotimer.enable(true) would not enable the timer again. This appears to be due disabling the timer interrupt in the code for zerotimer.enable(false). The interrupt was not enabled in the timer enable code.

Taking out the line to disable the interrupt allows me to disable and successfully re-enable the timer. I haven't seen any ill effects from leaving the interrupt enabled (I may have missed a test case where this is important).

This is just a suggestion for a change to the timer library, unless I'm missing something in the library design or intended use.

Thanks.