diff --git a/components/driver/twai.c b/components/driver/twai.c index 54c6bd4768..bb79ab959a 100644 --- a/components/driver/twai.c +++ b/components/driver/twai.c @@ -339,7 +339,12 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_ TWAI_CHECK(g_config->rx_queue_len > 0, ESP_ERR_INVALID_ARG); TWAI_CHECK(g_config->tx_io >= 0 && g_config->tx_io < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG); TWAI_CHECK(g_config->rx_io >= 0 && g_config->rx_io < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG); - TWAI_CHECK(TWAI_BRP_IS_VALID(t_config->brp), ESP_ERR_INVALID_ARG); +#if (CONFIG_ESP32_REV_MIN >= 2) + TWAI_CHECK(t_config->brp >= TWAI_BRP_MIN && t_config->brp <= TWAI_BRP_MAX_ECO, ESP_ERR_INVALID_ARG); +#else + TWAI_CHECK(t_config->brp >= TWAI_BRP_MIN && t_config->brp <= TWAI_BRP_MAX, ESP_ERR_INVALID_ARG); +#endif + esp_err_t ret; twai_obj_t *p_twai_obj_dummy; diff --git a/components/soc/include/hal/twai_types.h b/components/soc/include/hal/twai_types.h index a359ec802f..1e6fc7ed8b 100644 --- a/components/soc/include/hal/twai_types.h +++ b/components/soc/include/hal/twai_types.h @@ -20,6 +20,7 @@ extern "C" { #include #include +#include "sdkconfig.h" /** * @brief TWAI Constants @@ -59,7 +60,7 @@ extern "C" { #define TWAI_TIMING_CONFIG_5KBITS() {.brp = 800, .tseg_1 = 15, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} #define TWAI_TIMING_CONFIG_10KBITS() {.brp = 400, .tseg_1 = 15, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} #endif -#if (TWAI_BRP_MAX > 128) +#if (TWAI_BRP_MAX > 128) || (CONFIG_ESP32_REV_MIN >= 2) #define TWAI_TIMING_CONFIG_12_5KBITS() {.brp = 256, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} #define TWAI_TIMING_CONFIG_16KBITS() {.brp = 200, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} #define TWAI_TIMING_CONFIG_20KBITS() {.brp = 200, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} diff --git a/components/soc/soc/esp32/include/soc/can_caps.h b/components/soc/soc/esp32/include/soc/can_caps.h index f0efef7764..f372e9e07e 100644 --- a/components/soc/soc/esp32/include/soc/can_caps.h +++ b/components/soc/soc/esp32/include/soc/can_caps.h @@ -20,20 +20,12 @@ extern "C" { #warning soc/can_caps.h is deprecated, please use soc/twai_caps.h instead -#include "sdkconfig.h" - /* ---------------------------- Compatibility ------------------------------- */ -#if (CONFIG_ESP32_REV_MIN >= 2) -#define CAN_BRP_DIV_SUPPORTED 1 -#define CAN_BRP_DIV_THRESH 128 -#define CAN_BRP_IS_VALID(brp) (((brp) >= 2 && (brp) <= 128 && ((brp) & 0x1) == 0) || ((brp) >= 132 && (brp) <= 256 && ((brp) & 0x3) == 0)) -#define CAN_BRP_MAX 256 -#else -#define CAN_BRP_IS_VALID(brp) ((brp) >= 2 && (brp) <= 128 && ((brp) & 0x1) == 0) -#define CAN_BRP_MAX 128 -#endif - +#define CAN_BRP_MIN 2 +#define CAN_BRP_MAX 128 +#define CAN_BRP_MAX_ECO 256 +#define CAN_BRP_DIV_THRESH 128 #define CAN_SUPPORT_MULTI_ADDRESS_LAYOUT 1 #ifdef __cplusplus diff --git a/components/soc/soc/esp32/include/soc/twai_caps.h b/components/soc/soc/esp32/include/soc/twai_caps.h index 75d977ef3a..03cf155997 100644 --- a/components/soc/soc/esp32/include/soc/twai_caps.h +++ b/components/soc/soc/esp32/include/soc/twai_caps.h @@ -18,22 +18,10 @@ extern "C" { #endif -#include "sdkconfig.h" - -//Chip specific TWAI related macros -#if __DOXYGEN__ || (CONFIG_ESP32_REV_MIN >= 2) -#define TWAI_BRP_DIV_SUPPORTED 1 -#define TWAI_BRP_DIV_THRESH 128 -//Any even number from 2 to 128, or multiples of 4 from 132 to 256 -#define TWAI_BRP_IS_VALID(brp) (((brp) >= 2 && (brp) <= 128 && ((brp) & 0x1) == 0) || ((brp) >= 132 && (brp) <= 256 && ((brp) & 0x3) == 0)) -#define TWAI_BRP_MAX 256 -#else -//Any even number from 2 to 128 -#define TWAI_BRP_IS_VALID(brp) ((brp) >= 2 && (brp) <= 128 && ((brp) & 0x1) == 0) -#define TWAI_BRP_MAX 128 -#endif - -//Chip specific TWAI capabilities +#define TWAI_BRP_MIN 2 +#define TWAI_BRP_MAX 128 +#define TWAI_BRP_MAX_ECO 256 +#define TWAI_BRP_DIV_THRESH 128 #define TWAI_SUPPORT_MULTI_ADDRESS_LAYOUT 1 #ifdef __cplusplus diff --git a/components/soc/soc/esp32s2/include/soc/twai_caps.h b/components/soc/soc/esp32s2/include/soc/twai_caps.h index 88968ba90a..4ad5fbd829 100644 --- a/components/soc/soc/esp32s2/include/soc/twai_caps.h +++ b/components/soc/soc/esp32s2/include/soc/twai_caps.h @@ -18,10 +18,8 @@ extern "C" { #endif -//Chip specific TWAI related macros -//Any even number from 2 to 32768 -#define TWAI_BRP_IS_VALID(brp) ((brp) >= 2 && (brp) <= 32768 && ((brp) & 0x1) == 0) -#define TWAI_BRP_MAX 32768 +#define TWAI_BRP_MIN 2 +#define TWAI_BRP_MAX 32768 #ifdef __cplusplus } diff --git a/components/soc/src/esp32/include/hal/can_types.h b/components/soc/src/esp32/include/hal/can_types.h index 487e6e41da..2c65d2130f 100644 --- a/components/soc/src/esp32/include/hal/can_types.h +++ b/components/soc/src/esp32/include/hal/can_types.h @@ -38,7 +38,7 @@ extern "C" { #define CAN_MSG_FLAG_SELF TWAI_MSG_FLAG_SELF #define CAN_MSG_FLAG_DLC_NON_COMP TWAI_MSG_FLAG_DLC_NON_COMP -#if (TWAI_BRP_MAX > 128) +#if (TWAI_BRP_MAX > 128) || (CONFIG_ESP32_REV_MIN >= 2) #define CAN_TIMING_CONFIG_12_5KBITS() TWAI_TIMING_CONFIG_12_5KBITS() #define CAN_TIMING_CONFIG_16KBITS() TWAI_TIMING_CONFIG_16KBITS() #define CAN_TIMING_CONFIG_20KBITS() TWAI_TIMING_CONFIG_20KBITS() diff --git a/components/soc/src/esp32/include/hal/twai_ll.h b/components/soc/src/esp32/include/hal/twai_ll.h index 5a4422ab74..c82692af54 100644 --- a/components/soc/src/esp32/include/hal/twai_ll.h +++ b/components/soc/src/esp32/include/hal/twai_ll.h @@ -28,6 +28,7 @@ extern "C" { #include #include +#include "sdkconfig.h" #include "hal/twai_types.h" #include "soc/twai_periph.h" @@ -336,8 +337,8 @@ static inline uint32_t twai_ll_get_and_clear_intrs(twai_dev_t *hw) */ static inline void twai_ll_set_enabled_intrs(twai_dev_t *hw, uint32_t intr_mask) { -#ifdef TWAI_BRP_DIV_SUPPORTED - //ESP32 Rev 2 has brp div. Need to mask when setting +#if (CONFIG_ESP32_REV_MIN >= 2) + //ESP32 Rev 2 or later has brp div field. Need to mask it out hw->interrupt_enable_reg.val = (hw->interrupt_enable_reg.val & 0x10) | intr_mask; #else hw->interrupt_enable_reg.val = intr_mask; @@ -362,7 +363,7 @@ static inline void twai_ll_set_enabled_intrs(twai_dev_t *hw, uint32_t intr_mask) */ static inline void twai_ll_set_bus_timing(twai_dev_t *hw, uint32_t brp, uint32_t sjw, uint32_t tseg1, uint32_t tseg2, bool triple_sampling) { -#ifdef TWAI_BRP_DIV_SUPPORTED +#if (CONFIG_ESP32_REV_MIN >= 2) if (brp > TWAI_BRP_DIV_THRESH) { //Need to set brp_div bit hw->interrupt_enable_reg.brp_div = 1;