ESP32 multitasking

YHX⸮⸮PC 0x400d913b on core 0

Backtrace: 0x40086744:0x3ffc6140 0x40086843:0x3ffc6160 0x400d913b:0x3ffc6180 0x400d6a40:0x3ffc61b0 0x400d6da2:0x3ffc61d0 0x400d707d:0x3ffc6210 0x400d6868:0x3ffc6260 0x400d6765:0x3ffc62b0 0x400d67b1:0x3ffc62d0 0x400d67de:0x3ffc62f0 0x400d0794:0x3ffc6310 0x400d84b7:0x3ffc6330 0x400d4e43:0x3ffc6360

Rebooting…
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0010,len:4
load:0x3fff0014,len:588
load:0x40078000,len:0
load:0x40078000,len:9880
entry 0x400789d8
user code done

พอดีผมลองฝึกเขียนแบบ multitasking ดูครับคราวนี้เกิดปัญหาในหน้า serial board แสดงแบบนี้หมายความว่าไวครับ

วางโค้ตที่เขียนด้วยครับ

#define LED1 LED_BUILTIN
#define LED2 23

hw_timer_t *timer = NULL;

void onTimer(){
  digitalWrite(LED2, !digitalRead(LED2));
}

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  
  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 300000, true);
  timerAlarmEnable(timer);
}

void loop() {
  digitalWrite(LED1, !digitalRead(LED1));
  delay(500);
}

แต่ตอนนี้รันโปรแกรมอะไรก็ขึ้นที่ serial board ก็ขึ้นเหมือนที่ผมลงหมดเลยครับ

อธิบาย 4 บรรทันนี้หน่อยครับว่าให้มันทำอะไร

image

1.กำหนดเริ่มใช้งาน Timer โดยใช้ Timer ตัวที่ 0 กำหนดตัวหาร 80 และกำหนดให้นับเวลาขึ้น นำค่าที่กำหนดไปเก็บไว้ในตัวแปร timer
2.กำหนดให้เมื่อ Timer ในตัวแปร timer เมื่อนับเวลาครบแล้วจะไปเรียกฟังก์ชั่น onTimer ขึ้นมา
3.กำหนดให้เมื่อ Timer ในตัวแปร timer นับเวลาครบ 300000 ไมโครวินาที แล้วให้เกิดเหตุการณ์ และกำหนดให้เริ่มนับเวลาใหม่ทุกครั้งที่นับครบ
4.กำหนดให้ Timer ในตัวแปร timer เริ่มนับเวลา

ลองเพิ่ม delay เข้าไปตรงที่วงให้ดูครับ

ยังเป็นเหมือนเดิมเลยครับ

Sketch uses 124790 bytes (9%) of program storage space. Maximum is 1310720 bytes.
Global variables use 11504 bytes (3%) of dynamic memory, leaving 283408 bytes for local variables. Maximum is 294912 bytes.
esptool.py v2.1-beta1
Connecting…
Chip is ESP32D0WDQ6 (revision (unknown 0xa))
Uploading stub…
Running stub…
Stub running…
Changing baud rate to 921600
Changed.
Configuring flash size…
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47…

Writing at 0x0000e000… (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 4369.0 kbit/s)…
Hash of data verified.
Flash params set to 0x022f
Compressed 10576 bytes to 7094…

Writing at 0x00001000… (100 %)
Wrote 10576 bytes (7094 compressed) at 0x00001000 in 0.1 seconds (effective 890.6 kbit/s)…
Hash of data verified.
Compressed 130976 bytes to 69493…

Writing at 0x00010000… (20 %)
Writing at 0x00014000… (40 %)
Writing at 0x00018000… (60 %)
Writing at 0x0001c000… (80 %)
Writing at 0x00020000… (100 %)
Wrote 130976 bytes (69493 compressed) at 0x00010000 in 1.1 seconds (effective 963.9 kbit/s)…
Hash of data verified.
Compressed 3072 bytes to 122…

Writing at 0x00008000… (100 %)
Wrote 3072 bytes (122 compressed) at 0x00008000 in 0.0 seconds (effective 1536.0 kbit/s)…
Hash of data verified.

Leaving…
Hard resetting…
ส่วนของพิ้นที่แสดงการทำงานของโปรแกรมครับ

จริงๆ แล้วโค้ตแค่นี้ไม่น่ากระทบการทำงานอะไรได้นะ อืมม

งั้นลองศึกษาจาก กระทู้นี้ครับ