From e894d65548009a76cdf4b1909bddc7e063109b15 Mon Sep 17 00:00:00 2001 From: wanckl Date: Wed, 22 Oct 2025 17:25:25 +0800 Subject: [PATCH] refactor(driver_twai): remove errata config selection 1 --- components/driver/twai/Kconfig.twai | 12 ------------ components/hal/esp32/include/hal/twai_ll.h | 9 ++++++++- components/hal/twai_hal_v1.c | 2 +- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/components/driver/twai/Kconfig.twai b/components/driver/twai/Kconfig.twai index 11ff4187296..64e60d7c61e 100644 --- a/components/driver/twai/Kconfig.twai +++ b/components/driver/twai/Kconfig.twai @@ -21,18 +21,6 @@ menu "Legacy TWAI Driver Configurations" (driver/twai.h). If you want to continue using the legacy driver, and don't want to see related deprecation warnings, you can enable this option. - config TWAI_ERRATA_FIX_BUS_OFF_REC - bool "Add SW workaround for REC change during bus-off" - depends on IDF_TARGET_ESP32 - default y - help - When the bus-off condition is reached, the REC should be reset to 0 and frozen (via LOM) by the - driver's ISR. However on the ESP32, there is an edge case where the REC will increase before the - driver's ISR can respond in time (e.g., due to the rapid occurrence of bus errors), thus causing the - REC to be non-zero after bus-off. A non-zero REC can prevent bus-off recovery as the bus-off recovery - condition is that both TEC and REC become 0. Enabling this option will add a workaround in the driver - to forcibly reset REC to zero on reaching bus-off. - config TWAI_ERRATA_FIX_TX_INTR_LOST bool "Add SW workaround for TX interrupt lost errata" depends on IDF_TARGET_ESP32 diff --git a/components/hal/esp32/include/hal/twai_ll.h b/components/hal/esp32/include/hal/twai_ll.h index 59a0513d7e9..d38a5049ecf 100644 --- a/components/hal/esp32/include/hal/twai_ll.h +++ b/components/hal/esp32/include/hal/twai_ll.h @@ -28,7 +28,13 @@ #include "soc/dport_reg.h" #define TWAI_LL_GET_HW(controller_id) ((controller_id == 0) ? (&TWAI) : NULL) -#define TWAI_LL_BRP_DIV_THRESH 128 + +// When the bus-off condition is reached, the REC should be reset to 0 and frozen (via LOM) by the +// driver's ISR. However on the ESP32, there is an edge case where the REC will increase before the +// driver's ISR can respond in time (e.g., due to the rapid occurrence of bus errors), thus causing the +// REC to be non-zero after bus-off. A non-zero REC can prevent bus-off recovery as the bus-off recovery +// condition is that both TEC and REC become 0. +#define TWAI_LL_HAS_BUSOFF_REC_ISSUE 1 #ifdef __cplusplus extern "C" { @@ -38,6 +44,7 @@ static uint32_t twai_ll_get_brp_max(void); /* ------------------------- Defines and Typedefs --------------------------- */ #define TWAI_LL_BRP_MIN 2 #define TWAI_LL_BRP_MAX twai_ll_get_brp_max() // max brp of esp32 is depends on chip version +#define TWAI_LL_BRP_DIV_THRESH 128 #define TWAI_LL_TSEG1_MIN 1 #define TWAI_LL_TSEG2_MIN 1 #define TWAI_LL_TSEG1_MAX 16 //the max register value diff --git a/components/hal/twai_hal_v1.c b/components/hal/twai_hal_v1.c index e063bdffa93..c8f8a731636 100644 --- a/components/hal/twai_hal_v1.c +++ b/components/hal/twai_hal_v1.c @@ -270,7 +270,7 @@ uint32_t twai_hal_get_events(twai_hal_context_t *hal_ctx) //Handle low latency events if (events & TWAI_HAL_EVENT_BUS_OFF) { -#ifdef CONFIG_TWAI_ERRATA_FIX_BUS_OFF_REC +#if TWAI_LL_HAS_BUSOFF_REC_ISSUE //Errata workaround: Force REC to 0 by re-triggering bus-off (by setting TEC to 0 then 255) twai_ll_set_tec(hal_ctx->dev, 0); twai_ll_set_tec(hal_ctx->dev, 255);