I am referring this repo to run have blink a led using internal HW timer interrupts.
Though I tweaked “ESP32_C3_TimerInterrupt.h” from the repo and made following changes:
- Added this line of definition.
#define TIMER_BASE_CLK (APB_CLK_FREQ) //Frequency of the clock on the input of the timer groups
- Changed order of stdconfig elements to this, as there was an error regarding order not correct.
class ESP32TimerInterrupt
{
private:
timer_config_t stdConfig =
{
.alarm_en = TIMER_ALARM_EN, //enable timer alarm
.counter_en = TIMER_START, //starts counting counter once timer_init called
.intr_type = TIMER_INTR_MAX,
.counter_dir = TIMER_COUNT_UP, //counts from 0 to counter value
.auto_reload = TIMER_AUTORELOAD_EN, //reloads counter automatically
#if SOC_TIMER_GROUP_SUPPORT_XTAL
.clk_src = TIMER_SRC_CLK_APB, //Use XTAL as source clock
#endif
.divider = TIMER_DIVIDER
};
Now that I am able to run examples, I am not sure how to blink a led on.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 4
// Include the ESP32 timer interrupt library
#include "ESP32_C3_TimerInterrupt.h"
// Define the pin number for the LED
volatile SemaphoreHandle_t timerSemaphore;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
volatile bool LED_PIN = 10;
volatile bool isrExecuted = false;
// Define the timer interval in milliseconds
#define TIMER_INTERVAL_MS 100
#define TIMER0_DURATION_MS 3000
// Create an ESP32Timer object for timer 0
ESP32Timer Timer0(0);
// Define ISR to toggle the LED blink.
bool IRAM_ATTR TimerHandler(void* timerNo) {
portENTER_CRITICAL_ISR(&timerMux);
Serial.print("ISR Callback");
digitalWrite(LED_PIN, HIGH);
isrExecuted = true;
xSemaphoreGiveFromISR(timerSemaphore, NULL);
portEXIT_CRITICAL_ISR(&timerMux);
return true;
}
void setup() {
// Initialize serial communication at 115200 baud rate
Serial.begin(115200);
// Set the LED pin as an output
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Wait for the serial port to connect
while (!Serial && millis() < 5000);
// Print startup messages to the serial
Serial.print(F("\nStarting TimerInterruptTest on "));
Serial.println(ARDUINO_BOARD);
Serial.println(ESP32_C3_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = "));
Serial.print(F_CPU / 1000000);
Serial.println(F(" MHz"));
timerSemaphore = xSemaphoreCreateBinary();
// Initialize the Timer and attach the timer interrupt function
if (Timer0.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler)) {
Serial.println(F("Timer attached with interval:"));
Serial.print(TIMER_INTERVAL_MS);
Serial.println(F(" ms"));
} else {
Serial.println(F("Can't set Timer"));
}
// Start the timer initially
Timer0.restartTimer();
}
void loop()
{
static unsigned long lastTimer0 = 0;
static bool timer0Stopped = false;
if (millis() - lastTimer0 > TIMER0_DURATION_MS)
{
lastTimer0 = millis();
if (timer0Stopped)
{
Serial.print(F("Start ITimer0, millis() = "));
Serial.println(millis());
Timer0.restartTimer();
}
else
{
Serial.print(F("Stop ITimer0, millis() = "));
Serial.println(millis());
Timer0.stopTimer();
}
timer0Stopped = !timer0Stopped;
Serial.print(F("ISR executed: "));
Serial.println(isrExecuted);
}
}
I am not seeing ISR being executed in this sketch. The output is just as,
11:51:05.497 -> Start ITimer0, millis() = 156052
11:51:05.497 -> E (156057) timer_group: timer_start(103): HW TIMER NEVER INIT ERROR
11:51:05.497 -> ISR executed: 0
11:51:08.499 -> Stop ITimer0, millis() = 159053
11:51:08.499 -> ISR executed: 0
11:51:11.477 -> Start ITimer0, millis() = 162054
11:51:11.510 -> E (162059) timer_group: timer_start(103): HW TIMER NEVER INIT ERROR
11:51:11.510 -> ISR executed: 0
11:51:14.478 -> Stop ITimer0, millis() = 165055
Not sure what to do, may I am pretty new to this.