Skip to content

Instantly share code, notes, and snippets.

@CelliesProjects
Last active May 15, 2021 12:56
Show Gist options
  • Save CelliesProjects/a9a0b725a2a5a68787b696fb619c8a6f to your computer and use it in GitHub Desktop.
Save CelliesProjects/a9a0b725a2a5a68787b696fb619c8a6f to your computer and use it in GitHub Desktop.
esp32 - simple poc demonstrating a timer interupt driven ISR living in a class
/* simple poc demonstrating a timer interupt driven ISR living in a class */
class foo {
public:
bool start_isr();
private:
static void _timer_isr();
hw_timer_t * exampleTimer;
};
void IRAM_ATTR foo::_timer_isr() {
ESP_LOGI(TAG, "from ISR - seconds since boot: %i", time(NULL));
}
bool foo::start_isr() {
const uint8_t TIMER_NO = 0; /* 4 timers so 0-3 */
if (exampleTimer) {
ESP_LOGE(TAG, "hw timer %i already in use", TIMER_NO);
return false;
}
exampleTimer = timerBegin(TIMER_NO, APB_CLK_FREQ / (1000 * 1000), true); /* timer no: 0, prescaler calc results in 1 mHz clock res , timers counts up */
if (!exampleTimer) {
ESP_LOGE(TAG, "could not start timer %i", TIMER_NO);
return false;
}
timerAttachInterrupt(exampleTimer, &_timer_isr, true); /* true means timer repeats */
const uint64_t ONE_SECOND = 1000000;
timerAlarmWrite(exampleTimer, ONE_SECOND * 3, true);
timerAlarmEnable(exampleTimer);
return true;
}
foo foo;
void setup() {
ESP_LOGI(TAG, "APB clock freq: %i", APB_CLK_FREQ);
//RTC_APB_FREQ_REG
foo.start_isr();
foo.start_isr(); /* should give an error */
}
void loop() {
// put your main code here, to run repeatedly:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment