khoih-prog/ESP32TimerInterrupt

Changing frequency/interval without re-registering callback

Closed this issue · 1 comments

I have a use case where I needed to restart the timer using a new frequency/interval on the fly, without re-registering the callback. Re-registering also throws an Apb duplicate warning thrown by the HAL.

The following works like a charm for my use case:

      bool changeFrequency(float frequency)
    {
      // select timer frequency is 1MHz for better accuracy. We don't use 16-bit prescaler for now.
      // Will use later if very low frequency is needed.
      _frequency  = 1000000;
      _timerCount = (uint64_t) _frequency / frequency;
      // count up
      
      TISR_LOGWARN3(F("ESP32TimerInterrupt: _timerNo ="), _timerNo, F(", _fre ="), _frequency);
      TISR_LOGWARN3(F("_count ="), (uint32_t) (_timerCount >> 32) , F("-"), (uint32_t) (_timerCount));

      timerAlarmWrite(_timer, _timerCount, true);
      timerAlarmEnable(_timer);
      return true;
    }

and:

    bool changeInterval(unsigned long interval)
    {
      return changeFrequency( (float) ( 1000000.0f / interval));
    }

Hi @jjwbruijn

Thanks for sharing your experience which is definitely beneficial for other users with the similar use case.

BR,