Skip to content

Timer#1 and #2 are conflict and cannot be used the same time #6905

Closed
@Globefishp

Description

@Globefishp

Board

WEMOS LOLIN32 LITE

Device Description

No other hardware, just on breadboard.

Hardware Configuration

No hardware needed.

Version

v2.0.3

IDE Name

Arduino IDE

Operating System

Windows 10 1809

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

115200

Description

When I try to use multiple timer, I meet a strange behaviour that:
If I start timer #0 #1 #2, timer #1 will never execute its callback function.(but without crash)
If I start timer #0 #2, it works fine.
If I start timer #1 #2, timer #1 don't execute its callback function.

I further demostrate this problem in the below sketch that use 4 timer. Here the callback function do simple increment. By observing Serial Output we can know which timer is enabled.

By comment "timerStart" we can get multiple timer combination, which timer#1 never "work" when timer#2 is started.
When start only timer#1, it will execute callback function of timer#2;
If I delete the initiation code of timer#2, timer#1 will execute its own callback function.

Because of this "crosstalk", only #0, #2, #3 counter increase if you open all 4 timers.

I wonder there's no hardware limit in the 4 timer. They run indenpendently, and even, I can run all 4 timer when coding in ESP32-IDF style, bypassing the Arduino function. Code is here:

//Here demostrate a ESP32-IDF style implementation for 4 timer.
#include <driver/timer.h>

uint16_t timer0_count=0;
uint16_t timer1_count=0;
uint16_t timer2_count=0;
uint16_t timer3_count=0;

bool IRAM_ATTR timer0_event(void *args) {
  timer0_count++;
  return 0;
}
bool IRAM_ATTR timer1_event(void *args) {
  timer1_count++;
  return 0;
}
bool IRAM_ATTR timer2_event(void *args) {
  timer2_count++;
  return 0;
}
bool IRAM_ATTR timer3_event(void *args) {
  timer3_count++;
  return 0;
}

void setup() {
  Serial.begin(115200);
  
  // put your setup code here, to run once:
  timer_config_t timer0 = { 
    .alarm_en = TIMER_ALARM_EN,
    .counter_en = TIMER_PAUSE,
    .intr_type = TIMER_INTR_LEVEL,
    .counter_dir = TIMER_COUNT_UP,
    .auto_reload = TIMER_AUTORELOAD_EN,
    .divider = 80,
  };
  timer_init(TIMER_GROUP_0, TIMER_0, &timer0); //TimerGroup0,Num0,Config.
  timer_init(TIMER_GROUP_0, TIMER_1, &timer0); 
  timer_init(TIMER_GROUP_1, TIMER_0, &timer0); 
  timer_init(TIMER_GROUP_1, TIMER_1, &timer0);

  timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 1000000);
  timer_set_alarm_value(TIMER_GROUP_0, TIMER_1, 1000000);
  timer_set_alarm_value(TIMER_GROUP_1, TIMER_0, 1000000);
  timer_set_alarm_value(TIMER_GROUP_1, TIMER_1, 1000000);

  timer_isr_callback_add(TIMER_GROUP_0, TIMER_0, timer0_event, NULL, ESP_INTR_FLAG_IRAM);
  timer_isr_callback_add(TIMER_GROUP_0, TIMER_1, timer1_event, NULL, ESP_INTR_FLAG_IRAM);
  timer_isr_callback_add(TIMER_GROUP_1, TIMER_0, timer2_event, NULL, ESP_INTR_FLAG_IRAM);
  timer_isr_callback_add(TIMER_GROUP_1, TIMER_1, timer3_event, NULL, ESP_INTR_FLAG_IRAM);
   
  timer_start(TIMER_GROUP_0,TIMER_0);
  timer_start(TIMER_GROUP_0,TIMER_1);
  timer_start(TIMER_GROUP_1,TIMER_0);
  timer_start(TIMER_GROUP_1,TIMER_1);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.printf("%d %d %d %d \n", timer0_count,timer1_count,timer2_count,timer3_count);
  delay(100);
}

So I guess there may be some mistake in Arduino implementation for Timer.
Could you please help?

Sketch

//ESP32-Arduino Timer cannot work as expected, Seems some conflict between timer1 and timer2 in Arduino implementation

uint16_t timer0_count=0;
uint16_t timer1_count=0;
uint16_t timer2_count=0;
uint16_t timer3_count=0;

hw_timer_t *Timer0 = NULL;
hw_timer_t *Timer1 = NULL;
hw_timer_t *Timer2 = NULL;
hw_timer_t *Timer3 = NULL;


void IRAM_ATTR timer0_event() {
  timer0_count++;
}
void IRAM_ATTR timer1_event() {
  timer1_count++;
}
void IRAM_ATTR timer2_event() {
  timer2_count++;
}
void IRAM_ATTR timer3_event() {
  timer3_count++;
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  //-------Timer Initiation---------
  Timer0 = timerBegin(0, 80, true); 
  timerAttachInterrupt(Timer0, &timer0_event, true);
  timerAlarmWrite(Timer0, 800000, true); 
  timerAlarmEnable(Timer0); 
  timerStop(Timer0); 
  timerWrite(Timer0, 0);

  Timer1 = timerBegin(1, 80, true); 
  timerAttachInterrupt(Timer1, &timer1_event, true);
  timerAlarmWrite(Timer1, 800000, true); 
  timerAlarmEnable(Timer1); 
  timerStop(Timer1); 
  timerWrite(Timer1, 0);

  Timer2 = timerBegin(2, 80, true); 
  timerAttachInterrupt(Timer2, &timer2_event, true);
  timerAlarmWrite(Timer2, 800000, true); 
  timerAlarmEnable(Timer2); 
  timerStop(Timer2); 
  timerWrite(Timer2, 0);

  Timer3 = timerBegin(3, 80, true); 
  timerAttachInterrupt(Timer3, &timer3_event, true);
  timerAlarmWrite(Timer3, 800000, true); 
  timerAlarmEnable(Timer3); 
  timerStop(Timer3); 
  timerWrite(Timer3, 0);
  
  //---------Timer Start----------
  // timerStart(Timer0);
  timerStart(Timer1);
  // timerStart(Timer2);
  // timerStart(Timer3);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.printf("%d %d %d %d \n", timer0_count,timer1_count,timer2_count,timer3_count);
  delay(100);
}

Debug Message

Run the code and see Serial Output.

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions