Page 1 of 1

esp32 freertos 任务调度问题

Posted: Tue Sep 08, 2020 1:01 pm
by simonshi
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "sdkconfig.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_system.h"
  7. #include "esp_spi_flash.h"
  8.  
  9. #define START_TASK_PRIO 1   // 任务优先级
  10. #define START_STK_SIZE 1024  // 任务堆栈大小
  11. TaskHandle_t StartTask_Handler; // 任务句柄
  12. void start_task(void *pvParameters); // 任务函数
  13.  
  14. #define TASK1_TASK_PRIO 2   // 任务优先级
  15. #define TASK1_STK_SIZE 2048  // 任务堆栈大小
  16. TaskHandle_t Task1Task_Handler; // 任务句柄
  17. void task1_task(void *pvParameters); // 任务函数
  18.  
  19. #define TASK2_TASK_PRIO 3
  20. #define TASK2_STK_SIZE 2048
  21. TaskHandle_t Task2Task_Handler;
  22. void task2_task(void *pvParameters);
  23.  
  24. static portMUX_TYPE my_mutex = portMUX_INITIALIZER_UNLOCKED;
  25.  
  26. void app_main(void)
  27. {
  28.  
  29.     xTaskCreate(
  30.         (TaskFunction_t) start_task,
  31.         (const char*) "start_task",
  32.         (uint16_t)START_STK_SIZE,
  33.         (void*) NULL,
  34.         (UBaseType_t) START_TASK_PRIO,
  35.         (TaskHandle_t*) &StartTask_Handler
  36.     );
  37.    
  38.  
  39.     vTaskStartScheduler();
  40.     while (1);
  41.    
  42. }
  43.  
  44. void start_task(void *pvParameters)
  45. {
  46.     portENTER_CRITICAL(&my_mutex); // 进入临界区
  47.     // create task1 task
  48.     xTaskCreate(
  49.         (TaskFunction_t) task1_task,
  50.         (const char*) "task1_task",
  51.         (uint16_t) TASK1_STK_SIZE,
  52.         (void*) NULL,
  53.         (UBaseType_t)TASK1_TASK_PRIO,
  54.         (TaskHandle_t*) &Task1Task_Handler
  55.     );
  56.     // create task2 task
  57.     xTaskCreate(
  58.         (TaskFunction_t) task2_task,
  59.         (const char*) "task2_task",
  60.         (uint16_t) TASK2_STK_SIZE,
  61.         (void*) NULL,
  62.         (UBaseType_t)TASK2_TASK_PRIO,
  63.         (TaskHandle_t*) &Task2Task_Handler
  64.     );
  65.  
  66.     vTaskDelete(StartTask_Handler); // 删除开始任务
  67.     portEXIT_CRITICAL(&my_mutex); // 退出临界区
  68. }
  69.  
  70. void task1_task(void *pvParameters)
  71. {
  72.     uint8_t task1_num = 0;
  73.     while (1)
  74.     {
  75.         task1_num++;
  76.         printf("任务 1 已经执行:%d 次\r\n",task1_num);
  77.  
  78.         if (task1_num == 5)
  79.         {
  80.             vTaskDelete(Task2Task_Handler); // 任务1执行5次删除任务2
  81.             printf("任务 1 删除了任务 2 !\r\n");
  82.         }
  83.        
  84.         vTaskDelay(1500); // 延时1.5s,也就是1500个时钟节拍
  85.     }
  86. }
  87.  
  88. void task2_task(void *pvParameters)
  89. {
  90.     uint8_t task2_num = 0;
  91.     while (1)
  92.     {
  93.         task2_num++;
  94.         printf("任务 2 已经执行:%d 次\r\n",task2_num);
  95.         vTaskDelay(1500); // 延时1.5s
  96.     }
  97. }
烧录到硬件后,esp32运行的时候一直reboot.请教一下,是什么问题,或者排查思路

'C:\Users\shiwe\esp-idf\tools\idf.py' '-p' 'COM37'"...
--- idf_monitor on COM37 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_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:2
load:0x3fff0030,len:4
load:0x3fff0034,len:6928
ho 0 tail 12 room 4
load:0x40078000,len:13072
load:0x40080400,len:3896
0x40080400: _init at ??:?

entry 0x40080688
I (31) boot: ESP-IDF v4.1-dirty 2nd stage bootloader
I (31) boot: compile time 18:15:32
I (31) boot: chip revision: 3
I (34) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (41) boot.esp32: SPI Speed : 40MHz
I (46) boot.esp32: SPI Mode : DIO
I (51) boot.esp32: SPI Flash Size : 4MB
I (55) boot: Enabling RNG early entropy source...
I (60) boot: Partition Table:
I (64) boot: ## Label Usage Type ST Offset Length
I (71) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (79) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (86) boot: 2 factory factory app 00 00 00010000 00100000
I (94) boot: End of partition table
I (98) boot_comm: chip revision: 3, min. application chip revision: 0
I (105) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x05250 ( 21072) map
I (122) esp_image: segment 1: paddr=0x00015278 vaddr=0x3ffb0000 size=0x02154 ( 8532) load
I (126) esp_image: segment 2: paddr=0x000173d4 vaddr=0x40080000 size=0x00404 ( 1028) load
0x40080000: _WindowOverflow4 at C:/Users/shiwe/esp-idf/components/freertos/xtensa_vectors.S:1778

I (132) esp_image: segment 3: paddr=0x000177e0 vaddr=0x40080404 size=0x08838 ( 34872) load
I (156) esp_image: segment 4: paddr=0x00020020 vaddr=0x400d0020 size=0x128d4 ( 75988) map
0x400d0020: _stext at ??:?

I (185) esp_image: segment 5: paddr=0x000328fc vaddr=0x40088c3c size=0x00e7c ( 3708) load
0x40088c3c: rtc_wdt_set_length_of_reset_signal at C:/Users/shiwe/esp-idf/components/soc/esp32/rtc_wdt.c:136

I (192) boot: Loaded app from partition at offset 0x10000
I (193) boot: Disabling RNG early entropy source...
I (195) cpu_start: Pro cpu up.
I (198) cpu_start: Application information:
I (203) cpu_start: Project name: hello-world
I (208) cpu_start: App version: 1
I (213) cpu_start: Compile time: Sep 8 2020 18:15:06
I (219) cpu_start: ELF file SHA256: 7fa99573b4848c3c...
I (225) cpu_start: ESP-IDF: v4.1-dirty
I (230) cpu_start: Starting app cpu, entry point is 0x40081028
0x40081028: call_start_cpu1 at C:/Users/shiwe/esp-idf/components/esp32/cpu_start.c:271

I (0) cpu_start: App cpu up.
I (241) heap_init: Initializing. RAM available for dynamic allocation:
I (247) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (253) heap_init: At 3FFB2960 len 0002D6A0 (181 KiB): DRAM
I (260) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (266) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (272) heap_init: At 40089AB8 len 00016548 (89 KiB): IRAM
I (279) cpu_start: Pro cpu start user code
I (297) spi_flash: detected chip: generic
I (297) spi_flash: flash io: dio
I (298) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
任务 1 已经执行:1 次
任务 2 已经执行:1 次
Guru Meditation Error: Core 0 panic'ed (InstrFetchProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x800d0add PS : 0x00050033 A0 : 0x800d0add A1 : 0x3ffb4770
A2 : 0x00000000 A3 : 0x7fffffff A4 : 0x00000000 A5 : 0x454c4449
A6 : 0x3ffb0031 A7 : 0x00000000 A8 : 0x00000000 A9 : 0x00000400
A10 : 0x00000000 A11 : 0x00000001 A12 : 0x00000000 A13 : 0x800846b0
A14 : 0x3ffb47f0 A15 : 0x00000000 SAR : 0x00000000 EXCCAUSE: 0x00000014
EXCVADDR: 0x800d0adc LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000001

ELF file SHA256: 7fa99573b4848c3c

Backtrace: 0x400d0ada:0x3ffb4770 0x400d0ada:0x3ffb32ec |<-CORRUPTED
0x400d0ada: main_task at C:/Users/shiwe/esp-idf/components/esp32/cpu_start.c:565

0x400d0ada: main_task at C:/Users/shiwe/esp-idf/components/esp32/cpu_start.c:565


Rebooting...
ets Jul 29 2019 12:21:46

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:2
load:0x3fff0030,len:4
load:0x3fff0034,len:6928
ho 0 tail 12 room 4
load:0x40078000,len:13072
load:0x40080400,len:3896
0x40080400: _init at ??:?

entry 0x40080688
I (31) boot: ESP-IDF v4.1-dirty 2nd stage bootloader
I (31) boot: compile time 18:15:32

Re: esp32 freertos 任务调度问题

Posted: Wed Sep 09, 2020 8:19 am
by ESP_morris
vTaskStartScheduler不需要你在应用程序中调用了,在你的程序运行app_main之前,调度器已经起来了。

Re: esp32 freertos 任务调度问题

Posted: Wed Sep 09, 2020 8:22 am
by simonshi
ESP_morris wrote:
Wed Sep 09, 2020 8:19 am
vTaskStartScheduler不需要你在应用程序中调用了,在你的程序运行app_main之前,调度器已经起来了。
多谢回复。我测试时,把vTaskStartScheduler注释掉后确实可以了。