Skip to content

Commit

Permalink
Arduino 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jun 6, 2024
1 parent 44cbab0 commit 2117237
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
board_build.core = earlephilhower

[env:esp32dev]
board = esp32dev
framework = arduino
platform = espressif32@6.7.0
platform_packages=
platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.1
platformio/framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.1/esp32-arduino-libs-3.0.1.zip
15 changes: 15 additions & 0 deletions src/hw_timer_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const static int TIMER_ID = 0;
static hw_timer_t* timer = nullptr;

void timerInit(void (*callback)()) {
#if ESP_IDF_VERSION_MAJOR >= 5
timer = timerBegin(1000000);
timerWrite(timer, 0);
timerAttachInterrupt(timer, callback);
#else
// Use 1st timer of 4 (counted from zero).
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more
// info), count up. The counter starts to increase its value.
Expand All @@ -34,21 +39,31 @@ void timerInit(void (*callback)()) {
timerWrite(timer, 0);

timerAttachInterrupt(timer, callback, false);
#endif
}

void ARDUINO_ISR_ATTR startTimerAndTrigger(uint32_t delay) {
#if ESP_IDF_VERSION_MAJOR >= 5
timerRestart(timer);
timerAlarm(timer, delay, false, 0);
#else
timerWrite(timer, 0);
timerAlarmWrite(timer, delay, false);
timerAlarmEnable(timer);
timerStart(timer);
#endif
}

void ARDUINO_ISR_ATTR setAlarm(uint32_t delay) {
#if ESP_IDF_VERSION_MAJOR >= 5
timerAlarm(timer, delay, false, 0);
#else
timerAlarmWrite(timer, delay, false);

// On core v2.0.0-2.0.1, the timer alarm is automatically disabled after triggering,
// so re-enable the alarm
timerAlarmEnable(timer);
#endif
}

void ARDUINO_ISR_ATTR stopTimer() {
Expand Down

0 comments on commit 2117237

Please sign in to comment.