Skip to content

Commit

Permalink
add isr selector, remove setCallback function (to be refined)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianoriccardi committed Oct 14, 2023
1 parent 9416212 commit 009380c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
27 changes: 4 additions & 23 deletions src/hw_timer_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,15 @@ void timerInit(void (*callback)()) {
if (timer == nullptr) { Serial.println("Timer error"); }
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, callback, true);
}

void IRAM_ATTR setCallback(void (*callback)()) {}
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
}

void IRAM_ATTR startTimerAndTrigger(uint32_t delay) {
timerStop(timer);
// timer->dev->config.enable = 0;

timerWrite(timer, 0);
/// timer->dev->load_high = 0;
// timer->dev->load_low = 0;
// timer->dev->reload = 1;

// Set alarm to call onTimer function "delay" microsecond.
// Repeat the alarm (third parameter)
timerAlarmWrite(timer, delay, false);
// timer->dev->alarm_high = 0;
// timer->dev->alarm_low = delay;
// Reload the counter, but do not stop the counting
// timer->dev->config.autoreload = 0;

// Start an alarm
timerAlarmEnable(timer);
// timer->dev->config.alarm_en = 1;

// Start timer
timerAlarmWrite(timer, delay, true);
timerStart(timer);
// timer->dev->config.enable = 1;
}

void IRAM_ATTR stopTimer() {
Expand Down
2 changes: 0 additions & 2 deletions src/hw_timer_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

void timerInit(void (*callback)());

void setCallback(void (*callback)());

void startTimerAndTrigger(uint32_t delay);

void stopTimer();
Expand Down
26 changes: 23 additions & 3 deletions src/thyristor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ struct PinDelay {
uint16_t delay;
};

enum class INT_TYPE { ACTIVATE_THYRISTORS, TURN_OFF_GATES };

static INT_TYPE nextISR = INT_TYPE::ACTIVATE_THYRISTORS;

/**
* Temporary struct to provide the interrupt a memory concurrent-safe
* NOTE: this structure is manipulated by interrupt routine
Expand Down Expand Up @@ -221,7 +225,8 @@ void activate_thyristors() {
timer1_attachInterrupt(turn_off_gates_int);
timer1_write(US_TO_RTC_TIMER_TICKS(delay));
#elif defined(ARDUINO_ARCH_ESP32)
setCallback(turn_off_gates_int);
// setCallback(turn_off_gates_int);
nextISR = INT_TYPE::TURN_OFF_GATES;
startTimerAndTrigger(delay);
#elif defined(ARDUINO_ARCH_AVR)
timerSetCallback(turn_off_gates_int);
Expand Down Expand Up @@ -386,7 +391,8 @@ void zero_cross_int() {
timer1_attachInterrupt(activate_thyristors);
timer1_write(US_TO_RTC_TIMER_TICKS(pinDelay[thyristorManaged].delay));
#elif defined(ARDUINO_ARCH_ESP32)
setCallback(activate_thyristors);
// setCallback(activate_thyristors);
nextISR = INT_TYPE::ACTIVATE_THYRISTORS;
startTimerAndTrigger(pinDelay[thyristorManaged].delay);
#elif defined(ARDUINO_ARCH_AVR)
timerSetCallback(activate_thyristors);
Expand All @@ -410,6 +416,20 @@ void zero_cross_int() {
}
}

#if defined(ARDUINO_ARCH_ESP8266)
void HW_TIMER_IRAM_ATTR isr_selector() {
#elif defined(ARDUINO_ARCH_ESP32)
void isr_selector() {
#else
void isr_selector() {
#endif
if (nextISR == INT_TYPE::ACTIVATE_THYRISTORS) {
activate_thyristors();
} else if (nextISR == INT_TYPE::TURN_OFF_GATES) {
turn_off_gates_int();
}
}

void Thyristor::setDelay(uint16_t newDelay) {
if (verbosity > 2) {
for (int i = 0; i < Thyristor::nThyristors; i++) {
Expand Down Expand Up @@ -533,7 +553,7 @@ void Thyristor::begin() {
T1C = (1 << TCTE) | ((TIM_DIV16 & 3) << TCPD) | ((TIM_EDGE & 1) << TCIT) | ((TIM_SINGLE & 1) << TCAR);
T1I = 0;
#elif defined(ARDUINO_ARCH_ESP32)
timerInit(activate_thyristors);
timerInit(isr_selector);
#elif defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD)
timerSetCallback(activate_thyristors);
timerBegin();
Expand Down

0 comments on commit 009380c

Please sign in to comment.