From 550ed39ea3943b4746b20510e389cad782d0c5ba Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Thu, 30 Jul 2020 15:45:58 +0800 Subject: [PATCH 1/2] CAN: Fix size of RX msg count field on the esp32 This commit fixes the size of the RX message count register field on the esp32. --- components/soc/esp32/include/soc/can_struct.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/soc/esp32/include/soc/can_struct.h b/components/soc/esp32/include/soc/can_struct.h index 7325cd5a32..e4a7fa7444 100644 --- a/components/soc/esp32/include/soc/can_struct.h +++ b/components/soc/esp32/include/soc/can_struct.h @@ -183,8 +183,8 @@ typedef volatile struct can_dev_s { //Misc Registers union { struct { - uint32_t rmc: 5; /* RMC[4:0] RX Message Counter */ - uint32_t reserved27: 27; /* Internal Reserved */ + uint32_t rmc: 7; /* RMC[6:0] RX Message Counter */ + uint32_t reserved25: 25; /* Internal Reserved */ }; uint32_t val; } rx_message_counter_reg; /* Address 29 */ From f1b651af7ebe7de3e6c5761ae3fe3202a147c7b3 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Fri, 31 Jul 2020 00:19:53 +0800 Subject: [PATCH 2/2] CAN: Remove asserts used for program logic This commit fixes the bug where CAN driver program logic was being called in assert(), thus leading to the logic being omitted in release builds. --- components/driver/can.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/driver/can.c b/components/driver/can.c index 4ff728b800..d67b826388 100644 --- a/components/driver/can.c +++ b/components/driver/can.c @@ -131,7 +131,8 @@ static inline void can_handle_bus_off(int *alert_req) static inline void can_handle_recovery_complete(int *alert_req) { //Bus recovery complete. - assert(can_hal_handle_bus_recov_cplt(&can_context)); + bool recov_cplt = can_hal_handle_bus_recov_cplt(&can_context); + assert(recov_cplt); //Reset and set flags to the equivalent of the stopped state CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_RECOVERING | CTRL_FLAG_ERR_WARN | @@ -222,7 +223,7 @@ static inline void can_handle_tx_buffer_frame(BaseType_t *task_woken, int *alert //Update TX message count p_can_obj->tx_msg_count--; - assert(p_can_obj->tx_msg_count >= 0); //Sanity check + assert(p_can_obj->tx_msg_count >= 0); //Sanity check //Check if there are more frames to transmit if (p_can_obj->tx_msg_count > 0 && p_can_obj->tx_queue != NULL) { @@ -382,7 +383,8 @@ esp_err_t can_driver_install(const can_general_config_t *g_config, const can_tim } periph_module_reset(PERIPH_CAN_MODULE); periph_module_enable(PERIPH_CAN_MODULE); //Enable APB CLK to CAN peripheral - assert(can_hal_init(&can_context)); + bool init = can_hal_init(&can_context); + assert(init); can_hal_configure(&can_context, t_config, f_config, DRIVER_DEFAULT_INTERRUPTS, g_config->clkout_divider); //Todo: Allow interrupt to be registered to specified CPU CAN_EXIT_CRITICAL(); @@ -468,7 +470,8 @@ esp_err_t can_start(void) //Todo: Add assert to see if in reset mode. //Should already be in bus-off mode, set again to make sure //Currently in listen only mode, need to set to mode specified by configuration - assert(can_hal_start(&can_context, p_can_obj->mode)); + bool started = can_hal_start(&can_context, p_can_obj->mode); + assert(started); CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_STOPPED); CAN_EXIT_CRITICAL(); @@ -482,7 +485,8 @@ esp_err_t can_stop(void) CAN_CHECK_FROM_CRIT(p_can_obj != NULL, ESP_ERR_INVALID_STATE); CAN_CHECK_FROM_CRIT(!(p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)), ESP_ERR_INVALID_STATE); - assert(can_hal_stop(&can_context)); + bool stopped = can_hal_stop(&can_context); + assert(stopped); CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED); CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_STOPPED); @@ -631,7 +635,8 @@ esp_err_t can_initiate_recovery(void) CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_RECOVERING); //Trigger start of recovery process - assert(can_hal_start_bus_recovery(&can_context)); + bool started = can_hal_start_bus_recovery(&can_context); + assert(started); CAN_EXIT_CRITICAL(); return ESP_OK;