Here's a full example of what I'm experiencing:
Code: Select all
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "esp_err.h"
#include "esp_check.h"
#include "esp_log.h"
#include "esp_twai.h"
#include "esp_twai_onchip.h"
static const char TAG[] = "main";
struct nmea_frame {
uint32_t identifier;
uint8_t data[8];
size_t length;
};
static QueueHandle_t rx_queue = NULL;
static bool IRAM_ATTR twai_listener_on_error_callback(twai_node_handle_t handle, const twai_error_event_data_t *edata, void *user_ctx)
{
ESP_EARLY_LOGW(TAG, "bus error: 0x%x", edata->err_flags.val);
return false;
}
// Node state
static bool IRAM_ATTR twai_listener_on_state_change_callback(twai_node_handle_t handle, const twai_state_change_event_data_t *edata, void *user_ctx)
{
const char *twai_state_name[] = {"error_active", "error_warning", "error_passive", "bus_off"};
ESP_EARLY_LOGI(TAG, "state changed: %s -> %s", twai_state_name[edata->old_sta], twai_state_name[edata->new_sta]);
return false;
}
static bool IRAM_ATTR nmea_on_received(
twai_node_handle_t handle,
const twai_rx_done_event_data_t *edata,
void *user_ctx
)
{
struct nmea_frame n2k_frame = {0};
BaseType_t woken = pdFALSE;
twai_frame_t rx_frame = {
.buffer = n2k_frame.data,
.buffer_len = sizeof(n2k_frame.data) / sizeof(uint8_t),
};
if (twai_node_receive_from_isr(handle, &rx_frame) == ESP_OK) {
n2k_frame.identifier = rx_frame.header.id;
n2k_frame.length = rx_frame.header.dlc;
xQueueSendFromISR(rx_queue, &n2k_frame, &woken);
}
return woken == pdTRUE;
}
static esp_err_t nmea_init(twai_node_handle_t *handle) {
static twai_onchip_node_config_t node_config = {
.io_cfg.tx = GPIO_NUM_17,
.io_cfg.rx = GPIO_NUM_18,
.bit_timing.bitrate = 250000,
.tx_queue_depth = 5,
};
ESP_RETURN_ON_ERROR(
twai_new_node_onchip(&node_config, handle),
TAG,
"failed to create new node"
);
static twai_event_callbacks_t callbacks = {
.on_rx_done = nmea_on_received,
.on_error = twai_listener_on_error_callback,
.on_state_change = twai_listener_on_state_change_callback,
};
ESP_RETURN_ON_ERROR(
twai_node_register_event_callbacks(
*handle,
&callbacks,
NULL
),
TAG,
"failed to register cbs"
);
ESP_RETURN_ON_ERROR(
twai_node_enable(*handle),
TAG,
"failed to enable"
);
return ESP_OK;
}
static esp_err_t nmea_queues_init() {
rx_queue = xQueueCreate(100, sizeof(struct nmea_frame));
if (rx_queue == NULL) {
return ESP_ERR_NO_MEM;
}
return ESP_OK;
}
void app_main() {
twai_node_handle_t handle = NULL;
ESP_LOGI(TAG, "init");
ESP_ERROR_CHECK(nmea_queues_init());
ESP_ERROR_CHECK(nmea_init(&handle));
ESP_LOGI(TAG, "init done");
struct nmea_frame received_frame = {0};
for(;;) {
BaseType_t ret = xQueueReceive(rx_queue, &received_frame, portMAX_DELAY);
if (ret == pdTRUE) {
ESP_LOGI(TAG, "received frame with id %" PRIx32, received_frame.identifier);
} else {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
}
If I have two other devices on the bus, the 167052034 IDs are fine, but when I start transmitting from the second device (a NMEA2000-compliant device that works with the old polling driver), I start to see a bunch of bus errors.
Code: Select all
I (3394) main: received frame with id 167052034
I (4394) main: received frame with id 167052034
I (5394) main: received frame with id 167052034
I (6394) main: received frame with id 167052034
I (7394) main: received frame with id 167052034
I (8394) main: received frame with id 167052034
I (9394) main: received frame with id 167052034
I (10394) main: received frame with id 167052034
I (11394) main: received frame with id 167052034
I (12394) main: received frame with id 167052034
I (13394) main: received frame with id 167052034
I (14394) main: received frame with id 167052034
I (15394) main: received frame with id 167052034
W (15454) main: bus error: 0x0
W (15454) main: bus error: 0x0
W (15454) main: bus error: 0x0
W (15454) main: bus error: 0x8
W (15454) main: bus error: 0x8
W (15454) main: bus error: 0x0
W (15454) main: bus error: 0x0
I (15454) main: state changed: error_active -> error_warning
W (15454) main: bus error: 0x0
I (15454) main: state changed: error_warning -> error_passive
I (15474) main: received frame with id 418053952
I (15484) main: state changed: error_passive -> error_active
I (15494) main: received frame with id 418316034
I (16394) main: received frame with id 167052034
I (17394) main: received frame with id 167052034
W (17554) main: bus error: 0x0
W (17554) main: bus error: 0x0
W (17554) main: bus error: 0x0
I (17554) main: state changed: error_active -> error_passive
W (17554) main: bus error: 0x8
W (17554) main: bus error: 0x4
W (17554) main: bus error: 0x4
W (17554) main: bus error: 0x4
W (17554) main: bus error: 0x4
W (17604) main: bus error: 0x0
W (17604) main: bus error: 0x0
W (17604) main: bus error: 0x0
W (17654) main: bus error: 0x0
W (17654) main: bus error: 0x0
W (17654) main: bus error: 0x0
W (17704) main: bus error: 0x0
W (17704) main: bus error: 0x0
W (17704) main: bus error: 0x0
I (17734) main: state changed: error_passive -> error_active
I (17744) main: received frame with id 435164162
I (17744) main: received frame with id 435164162
I (17744) main: received frame with id 435164162
I (17744) main: received frame with id 435164162
I (17754) main: received frame with id 435164162
I (17754) main: received frame with id 435164162
I (17764) main: received frame with id 435164162
I (17764) main: received frame with id 435164162
I (17764) main: received frame with id 435164162
I (17774) main: received frame with id 435164162
I (17774) main: received frame with id 435164162
I (17784) main: received frame with id 435164162
I (17784) main: received frame with id 435164162
I (17794) main: received frame with id 435164162
I (17794) main: received frame with id 435164162
I (17794) main: received frame with id 435164162
I (17804) main: received frame with id 435164162
I (17804) main: received frame with id 435164162
I (17814) main: received frame with id 435164162
I (17814) main: received frame with id 435164162
I (17824) main: received frame with id 417873922
W (18254) main: bus error: 0x0
W (18254) main: bus error: 0x0
W (18254) main: bus error: 0x0
I (18254) main: received frame with id 418005056
I (18304) main: received frame with id 418005056
W (18354) main: bus error: 0x0
W (18354) main: bus error: 0x0
W (18354) main: bus error: 0x0
W (18354) main: bus error: 0x8
I (18354) main: state changed: error_active -> error_passive
W (18354) main: bus error: 0x0
I (18384) main: state changed: error_passive -> error_active
I (18394) main: received frame with id 167052034
W (18404) main: bus error: 0x0
W (18404) main: bus error: 0x0
W (18404) main: bus error: 0x0
I (18404) main: state changed: error_active -> error_passive
W (18404) main: bus error: 0x0
I (18434) main: state changed: error_passive -> error_active
I (18444) main: received frame with id 417873922
I (18494) main: received frame with id 417873922
W (18954) main: bus error: 0x0
W (18954) main: bus error: 0x0
W (18954) main: bus error: 0x0
I (18954) main: state changed: error_active -> error_passive
W (18954) main: bus error: 0x0
W (19004) main: bus error: 0x0
W (19004) main: bus error: 0x0
W (19004) main: bus error: 0x0
W (19054) main: bus error: 0x0
W (19054) main: bus error: 0x0
W (19054) main: bus error: 0x0
W (19104) main: bus error: 0x0
W (19104) main: bus error: 0x0
W (19104) main: bus error: 0x0
I (19134) main: state changed: error_passive -> error_active
I (19144) main: received frame with id 417873922
I (19194) main: received frame with id 417873922
I (19394) main: received frame with id 167052034
I (20394) main: received frame with id 167052034
If I just use the second device, the code crashes because we get stuck in the on_error callback loop and can't reset the WDT:
Code: Select all
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x9 (SPI_FAST_FLASH_BOOT)
Saved PC:0x403762f0
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce2820,len:0x159c
load:0x403c8700,len:0xd24
load:0x403cb700,len:0x2f48
entry 0x403c891c
I (29) boot: ESP-IDF 5.5.0 2nd stage bootloader
I (29) boot: compile time Aug 27 2025 13:40:29
I (29) boot: Multicore bootloader
I (29) boot: chip revision: v0.1
I (32) boot: efuse block revision: v1.2
I (36) boot.esp32s3: Boot SPI Speed : 80MHz
I (39) boot.esp32s3: SPI Mode : DIO
I (43) boot.esp32s3: SPI Flash Size : 8MB
I (47) boot: Enabling RNG early entropy source...
I (51) boot: Partition Table:
I (54) boot: ## Label Usage Type ST Offset Length
I (60) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (67) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (73) boot: 2 factory factory app 00 00 00010000 00100000
I (80) boot: End of partition table
I (83) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=0b50ch ( 46348) map
I (99) esp_image: segment 1: paddr=0001b534 vaddr=3fc92b00 size=02b18h ( 11032) load
I (101) esp_image: segment 2: paddr=0001e054 vaddr=40374000 size=01fc4h ( 8132) load
I (107) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=1a50ch (107788) map
I (132) esp_image: segment 4: paddr=0003a534 vaddr=40375fc4 size=0ca94h ( 51860) load
I (144) esp_image: segment 5: paddr=00046fd0 vaddr=600fe000 size=00020h ( 32) load
I (150) boot: Loaded app from partition at offset 0x10000
I (150) boot: Disabling RNG early entropy source...
I (161) cpu_start: Multicore app
I (170) cpu_start: Pro cpu start user code
I (170) cpu_start: cpu freq: 160000000 Hz
I (171) app_init: Application information:
I (171) app_init: Project name: n2k-test
I (175) app_init: App version: 1
I (178) app_init: Compile time: Aug 27 2025 13:40:17
I (183) app_init: ELF file SHA256: 114d1dfdd...
I (187) app_init: ESP-IDF: 5.5.0
I (191) efuse_init: Min chip rev: v0.0
I (195) efuse_init: Max chip rev: v0.99
I (199) efuse_init: Chip rev: v0.1
I (203) heap_init: Initializing. RAM available for dynamic allocation:
I (209) heap_init: At 3FC95EF8 len 00053818 (334 KiB): RAM
I (214) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
I (219) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (224) heap_init: At 600FE020 len 00001FC8 (7 KiB): RTCRAM
I (231) spi_flash: detected chip: gd
I (233) spi_flash: flash io: dio
W (236) spi_flash: Detected size(16384k) larger than the size in the binary image header(8192k). Using the size in the binary image header.
I (248) sleep_gpio: Configure to isolate all GPIO pins in sleep state
I (255) sleep_gpio: Enable automatic switching of GPIO sleep configuration
I (262) main_task: Started on CPU0
I (282) main_task: Calling app_main()
I (282) main: init
I (282) main: init done
W (282) main: bus error: 0x8
W (282) main: bus error: 0x8
W (282) main: bus error: 0x0
W (282) main: bus error: 0x0
W (282) main: bus error: 0x0
W (282) main: bus error: 0x8
W (282) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
I (302) main: state changed: error_active -> error_warning
W (302) main: bus error: 0x0
I (302) main: state changed: error_warning -> error_passive
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x4
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x4
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x4
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x4
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x0
W (302) main: bus error: 0x4
W (302) main: bus error: 0x4
W (302) main: bus error: 0x8
W (302) main: bus error: 0x8
Guru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0).
Core 0 register dump:
PC : 0x40048841 PS : 0x00060d34 A0 : 0x80048c6c A1 : 0x3fc93b00
A2 : 0x00000080 A3 : 0x00000057 A4 : 0x42002600 A5 : 0x3fc93ca0
A6 : 0x3fcea114 A7 : 0x3fc93108 A8 : 0x60000000 A9 : 0x00000380
A10 : 0x00000057 A11 : 0x6000001c A12 : 0x3fcef130 A13 : 0x4037a9be
A14 : 0x00000080 A15 : 0x3fc98974 SAR : 0x00000005 EXCCAUSE: 0x00000005
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Core 0 was running in ISR context:
EPC1 : 0x42018693 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x40048841
Backtrace: 0x4004883e:0x3fc93b00 0x40048c69:0x3fc93b20 0x40043d03:0x3fc93b40 0x40043cd5:0x3fc93b60 0x40044183:0x3fc93b80 0x40044281:0x3fc93c00 0x4037526e:0x3fc93c50 0x42002482:0x3fc93c70 0x40377585:0x3fc93ca0 0x420025fd:0x3fc98f00 0x42005faa:0x3fc98f20 0x4037b515:0x3fc98f40 0x4037a5d5:0x3fc98f60
Core 1 register dump:
PC : 0x40377efe PS : 0x00060734 A0 : 0x82005fad A1 : 0x3fc99660
A2 : 0x00000000 A3 : 0x00000000 A4 : 0x3fc976a0 A5 : 0x3fc97680
A6 : 0x4037585c A7 : 0x00000001 A8 : 0x82006776 A9 : 0x3fc99620
A10 : 0x00000000 A11 : 0x00000000 A12 : 0x3fc97680 A13 : 0x3fc97660
A14 : 0x00000001 A15 : 0x3fc99834 SAR : 0x00000000 EXCCAUSE: 0x00000005
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Backtrace: 0x40377efb:0x3fc99660 0x42005faa:0x3fc99680 0x4037b515:0x3fc996a0 0x4037a5d5:0x3fc996c0
ELF file SHA256: 114d1dfdd
Rebooting...