我在PlatformIO中使用ESP32-IDF将此代码放到我的ESP32上:
#include <driver/gpio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#define RELAY 2
void hello_task(void *pvParameter)
{
printf("Hello world!\n");
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(pdMS_TO_TICKS(1000));
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}
void led_blink(void *pvParams)
{
printf("Resetting pin\n");
gpio_reset_pin(RELAY);
printf("Setting pin direction\n");
gpio_set_direction(RELAY,GPIO_MODE_OUTPUT);
while (1) {
printf("Setting GPIO level low\n");
esp_err_t error_num = gpio_set_level(RELAY,0);
printf("error_num: %d\n", error_num);
vTaskDelay(pdMS_TO_TICKS(500)); // Here is where the panic happens
printf("Setting GPIO level high\n");
error_num = gpio_set_level(RELAY,1);
printf("error_num: %d\n", error_num);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void app_main()
{
xTaskCreate(&hello_task, "hello_task", 2048, NULL, 5, NULL);
xTaskCreate(&led_blink,"LED_BLINK",1024,NULL,5,NULL);
}
当到达LED_BLINK中的第一个vTaskDelay时,它会死机.这就是日志(log)的最后一部分显示的内容(如果有人说有必要,我可以把整个内容放进go ):
␛[0;32mI (306) app_start: Starting scheduler on CPU0␛[0m
␛[0;32mI (311) app_start: Starting scheduler on CPU1␛[0m
␛[0;32mI (311) main_task: Started on CPU0␛[0m
␛[0;32mI (321) main_task: Calling app_main()␛[0m
Hello world!
Restarting in 10 seconds...
Resetting pin
␛[0;32mI (321) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 ␛[0m
Setting pin direction
Setting GPIO level low
error_num: 0
␛[0;32mI (341) main_task: Returned from app_main()␛[0m
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x400869c8 PS : 0x00060033 A0 : 0x800883ea A1 : 0x3ffb0e50
A2 : 0x00000001 A3 : 0x0000000a A4 : 0x00000035 A5 : 0x00000005
A6 : 0xb33fffff A7 : 0xb33fffff A8 : 0x00000001 A9 : 0x00000000
A10 : 0x00000000 A11 : 0x80000001 A12 : 0x00000000 A13 : 0x00060023
A14 : 0xb33fffff A15 : 0xb33fffff SAR : 0x0000001e EXCCAUSE: 0x0000001c
EXCVADDR: 0x0000000e LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Backtrace: 0x400869c5:0x3ffb0e50 0x400883e7:0x3ffb0e70 0x40088231:0x3ffb0e90 0x40082732:0x3ffb0ea0 0x4008467f:0x3ffb5290 0x400d385f:0x3ffb52b0 0x40086726:0x3ffb52d0 0x40087c79:0x3ffb52f0
我有一个可以支持2a的电源,这远远超过了需求,所以这不是一个电源问题.我注意到日志(log)显示为OutputEn: 0
,这让我认为可能是PIN设置有问题,但PIN设置的返回代码是0……
为什么会发生这样的撞车事故?基本上,我只是复制了一些教程代码并添加了一些printf语句.