|
|
@@ -49,14 +49,6 @@
|
|
|
|
|
|
#define DRIVER_DEFAULT_INTERRUPTS 0xE7 //Exclude data overrun (bit[3]) and brp_div (bit[4])
|
|
|
|
|
|
-//Control flags
|
|
|
-#define CTRL_FLAG_STOPPED 0x001 //TWAI peripheral in stopped state
|
|
|
-#define CTRL_FLAG_RECOVERING 0x002 //Bus is undergoing bus recovery
|
|
|
-#define CTRL_FLAG_ERR_WARN 0x004 //TEC or REC is >= error warning limit
|
|
|
-#define CTRL_FLAG_ERR_PASSIVE 0x008 //TEC or REC is >= 128
|
|
|
-#define CTRL_FLAG_BUS_OFF 0x010 //Bus-off due to TEC >= 256
|
|
|
-#define CTRL_FLAG_TX_BUFF_OCCUPIED 0x020 //Transmit buffer is occupied
|
|
|
-
|
|
|
#define ALERT_LOG_LEVEL_WARNING TWAI_ALERT_ARB_LOST //Alerts above and including this level use ESP_LOGW
|
|
|
#define ALERT_LOG_LEVEL_ERROR TWAI_ALERT_TX_FAILED //Alerts above and including this level use ESP_LOGE
|
|
|
|
|
|
@@ -65,7 +57,7 @@
|
|
|
//Control structure for TWAI driver
|
|
|
typedef struct {
|
|
|
//Control and status members
|
|
|
- uint32_t control_flags;
|
|
|
+ twai_state_t state;
|
|
|
twai_mode_t mode;
|
|
|
uint32_t rx_missed_count;
|
|
|
uint32_t tx_failed_count;
|
|
|
@@ -116,81 +108,6 @@ static void twai_alert_handler(uint32_t alert_code, int *alert_req)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static inline void twai_handle_bus_off(int *alert_req)
|
|
|
-{
|
|
|
- //Bus-Off condition. TEC should set and held at 127, REC should be 0, reset mode entered
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_BUS_OFF);
|
|
|
- /* Note: REC is still allowed to increase during bus-off. REC > err_warn
|
|
|
- can prevent "bus recovery complete" interrupt from occurring. Set to
|
|
|
- listen only mode to freeze REC. */
|
|
|
- twai_hal_handle_bus_off(&twai_context);
|
|
|
- twai_alert_handler(TWAI_ALERT_BUS_OFF, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_recovery_complete(int *alert_req)
|
|
|
-{
|
|
|
- //Bus recovery complete.
|
|
|
- bool recov_cplt = twai_hal_handle_bus_recov_cplt(&twai_context);
|
|
|
- assert(recov_cplt);
|
|
|
-
|
|
|
- //Reset and set flags to the equivalent of the stopped state
|
|
|
- TWAI_RESET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_RECOVERING | CTRL_FLAG_ERR_WARN |
|
|
|
- CTRL_FLAG_ERR_PASSIVE | CTRL_FLAG_BUS_OFF |
|
|
|
- CTRL_FLAG_TX_BUFF_OCCUPIED);
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_STOPPED);
|
|
|
- twai_alert_handler(TWAI_ALERT_BUS_RECOVERED, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_recovery_in_progress(int * alert_req)
|
|
|
-{
|
|
|
- //Bus-recovery in progress. TEC has dropped below error warning limit
|
|
|
- twai_alert_handler(TWAI_ALERT_RECOVERY_IN_PROGRESS, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_above_ewl(int *alert_req)
|
|
|
-{
|
|
|
- //TEC or REC surpassed error warning limit
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_ERR_WARN);
|
|
|
- twai_alert_handler(TWAI_ALERT_ABOVE_ERR_WARN, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_below_ewl(int *alert_req)
|
|
|
-{
|
|
|
- //TEC and REC are both below error warning
|
|
|
- TWAI_RESET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_ERR_WARN);
|
|
|
- twai_alert_handler(TWAI_ALERT_BELOW_ERR_WARN, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_error_passive(int *alert_req)
|
|
|
-{
|
|
|
- //Entered error passive
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_ERR_PASSIVE);
|
|
|
- twai_alert_handler(TWAI_ALERT_ERR_PASS, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_error_active(int *alert_req)
|
|
|
-{
|
|
|
- //Returned to error active
|
|
|
- TWAI_RESET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_ERR_PASSIVE);
|
|
|
- twai_alert_handler(TWAI_ALERT_ERR_ACTIVE, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_bus_error(int *alert_req)
|
|
|
-{
|
|
|
- // ECC register is read to re-arm bus error interrupt. ECC is not used
|
|
|
- twai_hal_handle_bus_error(&twai_context);
|
|
|
- p_twai_obj->bus_error_count++;
|
|
|
- twai_alert_handler(TWAI_ALERT_BUS_ERROR, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
-static inline void twai_handle_arb_lost(int *alert_req)
|
|
|
-{
|
|
|
- //ALC register is read to re-arm arb lost interrupt. ALC is not used
|
|
|
- twai_hal_handle_arb_lost(&twai_context);
|
|
|
- p_twai_obj->arb_lost_count++;
|
|
|
- twai_alert_handler(TWAI_ALERT_ARB_LOST, alert_req);
|
|
|
-}
|
|
|
-
|
|
|
static inline void twai_handle_rx_buffer_frames(BaseType_t *task_woken, int *alert_req)
|
|
|
{
|
|
|
uint32_t msg_count = twai_hal_get_rx_msg_count(&twai_context);
|
|
|
@@ -206,8 +123,6 @@ static inline void twai_handle_rx_buffer_frames(BaseType_t *task_woken, int *ale
|
|
|
twai_alert_handler(TWAI_ALERT_RX_QUEUE_FULL, alert_req);
|
|
|
}
|
|
|
}
|
|
|
- //Todo: Add Software Filters
|
|
|
- //Todo: Check for data overrun of RX FIFO, then trigger alert
|
|
|
}
|
|
|
|
|
|
static inline void twai_handle_tx_buffer_frame(BaseType_t *task_woken, int *alert_req)
|
|
|
@@ -235,7 +150,6 @@ static inline void twai_handle_tx_buffer_frame(BaseType_t *task_woken, int *aler
|
|
|
}
|
|
|
} else {
|
|
|
//No more frames to transmit
|
|
|
- TWAI_RESET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
|
|
|
twai_alert_handler(TWAI_ALERT_TX_IDLE, alert_req);
|
|
|
}
|
|
|
}
|
|
|
@@ -250,43 +164,55 @@ static void twai_intr_handler_main(void *arg)
|
|
|
TWAI_EXIT_CRITICAL_ISR();
|
|
|
return;
|
|
|
}
|
|
|
- event = twai_hal_decode_interrupt_events(&twai_context, p_twai_obj->control_flags & CTRL_FLAG_RECOVERING);
|
|
|
+ event = twai_hal_decode_interrupt_events(&twai_context);
|
|
|
+ if (event & TWAI_HAL_EVENT_RX_BUFF_FRAME) {
|
|
|
+ twai_handle_rx_buffer_frames(&task_woken, &alert_req);
|
|
|
+ }
|
|
|
+ //TX command should be the last command related handler to be called, so that
|
|
|
+ //other command register bits do not overwrite the TX command bit.
|
|
|
+ if (event & TWAI_HAL_EVENT_TX_BUFF_FREE) {
|
|
|
+ twai_handle_tx_buffer_frame(&task_woken, &alert_req);
|
|
|
+ }
|
|
|
|
|
|
+ //Handle events that only require alerting (i.e. no handler)
|
|
|
if (event & TWAI_HAL_EVENT_BUS_OFF) {
|
|
|
- twai_handle_bus_off(&alert_req);
|
|
|
+ p_twai_obj->state = TWAI_STATE_BUS_OFF;
|
|
|
+ twai_alert_handler(TWAI_ALERT_BUS_OFF, &alert_req);
|
|
|
}
|
|
|
if (event & TWAI_HAL_EVENT_BUS_RECOV_CPLT) {
|
|
|
- twai_handle_recovery_complete(&alert_req);
|
|
|
+ //Reset and set flags to the equivalent of the stopped state
|
|
|
+ p_twai_obj->state = TWAI_STATE_STOPPED;
|
|
|
+ twai_alert_handler(TWAI_ALERT_BUS_RECOVERED, &alert_req);
|
|
|
}
|
|
|
- if (event & TWAI_HAL_EVENT_BUS_RECOV_PROGRESS) {
|
|
|
- twai_handle_recovery_in_progress(&alert_req);
|
|
|
+ if (event & TWAI_HAL_EVENT_BUS_ERR) {
|
|
|
+ p_twai_obj->bus_error_count++;
|
|
|
+ twai_alert_handler(TWAI_ALERT_BUS_ERROR, &alert_req);
|
|
|
}
|
|
|
- if (event & TWAI_HAL_EVENT_ABOVE_EWL) {
|
|
|
- twai_handle_above_ewl(&alert_req);
|
|
|
+ if (event & TWAI_HAL_EVENT_ARB_LOST) {
|
|
|
+ p_twai_obj->arb_lost_count++;
|
|
|
+ twai_alert_handler(TWAI_ALERT_ARB_LOST, &alert_req);
|
|
|
}
|
|
|
- if (event & TWAI_HAL_EVENT_BELOW_EWL) {
|
|
|
- twai_handle_below_ewl(&alert_req);
|
|
|
+ if (event & TWAI_HAL_EVENT_BUS_RECOV_PROGRESS) {
|
|
|
+ //Bus-recovery in progress. TEC has dropped below error warning limit
|
|
|
+ twai_alert_handler(TWAI_ALERT_RECOVERY_IN_PROGRESS, &alert_req);
|
|
|
}
|
|
|
if (event & TWAI_HAL_EVENT_ERROR_PASSIVE) {
|
|
|
- twai_handle_error_passive(&alert_req);
|
|
|
+ //Entered error passive
|
|
|
+ twai_alert_handler(TWAI_ALERT_ERR_PASS, &alert_req);
|
|
|
}
|
|
|
if (event & TWAI_HAL_EVENT_ERROR_ACTIVE) {
|
|
|
- twai_handle_error_active(&alert_req);
|
|
|
- }
|
|
|
- if (event & TWAI_HAL_EVENT_BUS_ERR) {
|
|
|
- twai_handle_bus_error(&alert_req);
|
|
|
+ //Returned to error active
|
|
|
+ twai_alert_handler(TWAI_ALERT_ERR_ACTIVE, &alert_req);
|
|
|
}
|
|
|
- if (event & TWAI_HAL_EVENT_ARB_LOST) {
|
|
|
- twai_handle_arb_lost(&alert_req);
|
|
|
- }
|
|
|
- if (event & TWAI_HAL_EVENT_RX_BUFF_FRAME) {
|
|
|
- twai_handle_rx_buffer_frames(&task_woken, &alert_req);
|
|
|
+ if (event & TWAI_HAL_EVENT_ABOVE_EWL) {
|
|
|
+ //TEC or REC surpassed error warning limit
|
|
|
+ twai_alert_handler(TWAI_ALERT_ABOVE_ERR_WARN, &alert_req);
|
|
|
}
|
|
|
- //TX command related handlers should be called last, so that other commands
|
|
|
- //do not overwrite the TX command related bits in the command register.
|
|
|
- if (event & TWAI_HAL_EVENT_TX_BUFF_FREE) {
|
|
|
- twai_handle_tx_buffer_frame(&task_woken, &alert_req);
|
|
|
+ if (event & TWAI_HAL_EVENT_BELOW_EWL) {
|
|
|
+ //TEC and REC are both below error warning
|
|
|
+ twai_alert_handler(TWAI_ALERT_BELOW_ERR_WARN, &alert_req);
|
|
|
}
|
|
|
+
|
|
|
TWAI_EXIT_CRITICAL_ISR();
|
|
|
|
|
|
if (p_twai_obj->alert_semphr != NULL && alert_req) {
|
|
|
@@ -371,7 +297,7 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_
|
|
|
#endif
|
|
|
|
|
|
//Initialize flags and variables. All other members are 0 initialized by calloc()
|
|
|
- p_twai_obj_dummy->control_flags = CTRL_FLAG_STOPPED;
|
|
|
+ p_twai_obj_dummy->state = TWAI_STATE_STOPPED;
|
|
|
p_twai_obj_dummy->mode = g_config->mode;
|
|
|
p_twai_obj_dummy->alerts_enabled = g_config->alerts_enabled;
|
|
|
|
|
|
@@ -390,7 +316,6 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_
|
|
|
bool init = twai_hal_init(&twai_context);
|
|
|
assert(init);
|
|
|
twai_hal_configure(&twai_context, t_config, f_config, DRIVER_DEFAULT_INTERRUPTS, g_config->clkout_divider);
|
|
|
- //Todo: Allow interrupt to be registered to specified CPU
|
|
|
TWAI_EXIT_CRITICAL();
|
|
|
|
|
|
//Allocate GPIO and Interrupts
|
|
|
@@ -434,8 +359,7 @@ esp_err_t twai_driver_uninstall(void)
|
|
|
TWAI_ENTER_CRITICAL();
|
|
|
//Check state
|
|
|
TWAI_CHECK_FROM_CRIT(p_twai_obj != NULL, ESP_ERR_INVALID_STATE);
|
|
|
- TWAI_CHECK_FROM_CRIT(p_twai_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF), ESP_ERR_INVALID_STATE);
|
|
|
- //Todo: Add check to see if in reset mode. //Enter reset mode to stop any TWAI bus activity
|
|
|
+ TWAI_CHECK_FROM_CRIT(p_twai_obj->state == TWAI_STATE_STOPPED || p_twai_obj->state == TWAI_STATE_BUS_OFF, ESP_ERR_INVALID_STATE);
|
|
|
//Clear registers by reading
|
|
|
twai_hal_deinit(&twai_context);
|
|
|
periph_module_disable(PERIPH_TWAI_MODULE); //Disable TWAI peripheral
|
|
|
@@ -466,18 +390,18 @@ esp_err_t twai_start(void)
|
|
|
//Check state
|
|
|
TWAI_ENTER_CRITICAL();
|
|
|
TWAI_CHECK_FROM_CRIT(p_twai_obj != NULL, ESP_ERR_INVALID_STATE);
|
|
|
- TWAI_CHECK_FROM_CRIT(p_twai_obj->control_flags & CTRL_FLAG_STOPPED, ESP_ERR_INVALID_STATE);
|
|
|
+ TWAI_CHECK_FROM_CRIT(p_twai_obj->state == TWAI_STATE_STOPPED, ESP_ERR_INVALID_STATE);
|
|
|
|
|
|
- //Reset RX queue, and RX message count
|
|
|
+ //Reset RX queue, RX message count, amd TX queue
|
|
|
xQueueReset(p_twai_obj->rx_queue);
|
|
|
+ if (p_twai_obj->tx_queue != NULL) {
|
|
|
+ xQueueReset(p_twai_obj->tx_queue);
|
|
|
+ }
|
|
|
p_twai_obj->rx_msg_count = 0;
|
|
|
- //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
|
|
|
- bool started = twai_hal_start(&twai_context, p_twai_obj->mode);
|
|
|
- assert(started);
|
|
|
+ p_twai_obj->tx_msg_count = 0;
|
|
|
+ twai_hal_start(&twai_context, p_twai_obj->mode);
|
|
|
|
|
|
- TWAI_RESET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_STOPPED);
|
|
|
+ p_twai_obj->state = TWAI_STATE_RUNNING;
|
|
|
TWAI_EXIT_CRITICAL();
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
@@ -487,19 +411,16 @@ esp_err_t twai_stop(void)
|
|
|
//Check state
|
|
|
TWAI_ENTER_CRITICAL();
|
|
|
TWAI_CHECK_FROM_CRIT(p_twai_obj != NULL, ESP_ERR_INVALID_STATE);
|
|
|
- TWAI_CHECK_FROM_CRIT(!(p_twai_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)), ESP_ERR_INVALID_STATE);
|
|
|
+ TWAI_CHECK_FROM_CRIT(p_twai_obj->state == TWAI_STATE_RUNNING, ESP_ERR_INVALID_STATE);
|
|
|
|
|
|
- bool stopped = twai_hal_stop(&twai_context);
|
|
|
- assert(stopped);
|
|
|
-
|
|
|
- TWAI_RESET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_STOPPED);
|
|
|
+ twai_hal_stop(&twai_context);
|
|
|
|
|
|
//Reset TX Queue and message count
|
|
|
if (p_twai_obj->tx_queue != NULL) {
|
|
|
xQueueReset(p_twai_obj->tx_queue);
|
|
|
}
|
|
|
p_twai_obj->tx_msg_count = 0;
|
|
|
+ p_twai_obj->state = TWAI_STATE_STOPPED;
|
|
|
|
|
|
TWAI_EXIT_CRITICAL();
|
|
|
|
|
|
@@ -516,18 +437,17 @@ esp_err_t twai_transmit(const twai_message_t *message, TickType_t ticks_to_wait)
|
|
|
TWAI_ENTER_CRITICAL();
|
|
|
//Check State
|
|
|
TWAI_CHECK_FROM_CRIT(!(p_twai_obj->mode == TWAI_MODE_LISTEN_ONLY), ESP_ERR_NOT_SUPPORTED);
|
|
|
- TWAI_CHECK_FROM_CRIT(!(p_twai_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)), ESP_ERR_INVALID_STATE);
|
|
|
+ TWAI_CHECK_FROM_CRIT(p_twai_obj->state == TWAI_STATE_RUNNING, ESP_ERR_INVALID_STATE);
|
|
|
//Format frame
|
|
|
esp_err_t ret = ESP_FAIL;
|
|
|
twai_hal_frame_t tx_frame;
|
|
|
twai_hal_format_frame(message, &tx_frame);
|
|
|
|
|
|
//Check if frame can be sent immediately
|
|
|
- if ((p_twai_obj->tx_msg_count == 0) && !(p_twai_obj->control_flags & CTRL_FLAG_TX_BUFF_OCCUPIED)) {
|
|
|
+ if (p_twai_obj->tx_msg_count == 0) {
|
|
|
//No other frames waiting to transmit. Bypass queue and transmit immediately
|
|
|
twai_hal_set_tx_buffer_and_transmit(&twai_context, &tx_frame);
|
|
|
p_twai_obj->tx_msg_count++;
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
|
|
|
ret = ESP_OK;
|
|
|
}
|
|
|
TWAI_EXIT_CRITICAL();
|
|
|
@@ -539,19 +459,19 @@ esp_err_t twai_transmit(const twai_message_t *message, TickType_t ticks_to_wait)
|
|
|
} else if (xQueueSend(p_twai_obj->tx_queue, &tx_frame, ticks_to_wait) == pdTRUE) {
|
|
|
//Copied to TX Queue
|
|
|
TWAI_ENTER_CRITICAL();
|
|
|
- if (p_twai_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)) {
|
|
|
- //TX queue was reset (due to stop/bus_off), remove copied frame from queue to prevent transmission
|
|
|
- int res = xQueueReceive(p_twai_obj->tx_queue, &tx_frame, 0);
|
|
|
- assert(res == pdTRUE);
|
|
|
- ret = ESP_ERR_INVALID_STATE;
|
|
|
- } else if ((p_twai_obj->tx_msg_count == 0) && !(p_twai_obj->control_flags & CTRL_FLAG_TX_BUFF_OCCUPIED)) {
|
|
|
- //TX buffer was freed during copy, manually trigger transmission
|
|
|
- int res = xQueueReceive(p_twai_obj->tx_queue, &tx_frame, 0);
|
|
|
- assert(res == pdTRUE);
|
|
|
- twai_hal_set_tx_buffer_and_transmit(&twai_context, &tx_frame);
|
|
|
- p_twai_obj->tx_msg_count++;
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
|
|
|
- ret = ESP_OK;
|
|
|
+ if ((!twai_hal_check_state_flags(&twai_context, TWAI_HAL_STATE_FLAG_TX_BUFF_OCCUPIED)) && uxQueueMessagesWaiting(p_twai_obj->tx_queue) > 0) {
|
|
|
+ //If the TX buffer is free but the TX queue is not empty. Check if we need to manually start a transmission
|
|
|
+ if (twai_hal_check_state_flags(&twai_context, TWAI_HAL_STATE_FLAG_BUS_OFF) || !twai_hal_check_state_flags(&twai_context, TWAI_HAL_STATE_FLAG_RUNNING)) {
|
|
|
+ //TX buffer became free due to bus-off or is no longer running. No need to start a transmission
|
|
|
+ ret = ESP_ERR_INVALID_STATE;
|
|
|
+ } else {
|
|
|
+ //Manually start a transmission
|
|
|
+ int res = xQueueReceive(p_twai_obj->tx_queue, &tx_frame, 0);
|
|
|
+ assert(res == pdTRUE);
|
|
|
+ twai_hal_set_tx_buffer_and_transmit(&twai_context, &tx_frame);
|
|
|
+ p_twai_obj->tx_msg_count++;
|
|
|
+ ret = ESP_OK;
|
|
|
+ }
|
|
|
} else {
|
|
|
//Frame was copied to queue, waiting to be transmitted
|
|
|
p_twai_obj->tx_msg_count++;
|
|
|
@@ -627,20 +547,17 @@ esp_err_t twai_initiate_recovery(void)
|
|
|
TWAI_ENTER_CRITICAL();
|
|
|
//Check state
|
|
|
TWAI_CHECK_FROM_CRIT(p_twai_obj != NULL, ESP_ERR_INVALID_STATE);
|
|
|
- TWAI_CHECK_FROM_CRIT(p_twai_obj->control_flags & CTRL_FLAG_BUS_OFF, ESP_ERR_INVALID_STATE);
|
|
|
- TWAI_CHECK_FROM_CRIT(!(p_twai_obj->control_flags & CTRL_FLAG_RECOVERING), ESP_ERR_INVALID_STATE);
|
|
|
+ TWAI_CHECK_FROM_CRIT(p_twai_obj->state == TWAI_STATE_BUS_OFF, ESP_ERR_INVALID_STATE);
|
|
|
|
|
|
//Reset TX Queue/Counters
|
|
|
if (p_twai_obj->tx_queue != NULL) {
|
|
|
xQueueReset(p_twai_obj->tx_queue);
|
|
|
}
|
|
|
p_twai_obj->tx_msg_count = 0;
|
|
|
- TWAI_RESET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED);
|
|
|
- TWAI_SET_FLAG(p_twai_obj->control_flags, CTRL_FLAG_RECOVERING);
|
|
|
|
|
|
//Trigger start of recovery process
|
|
|
- bool started = twai_hal_start_bus_recovery(&twai_context);
|
|
|
- assert(started);
|
|
|
+ twai_hal_start_bus_recovery(&twai_context);
|
|
|
+ p_twai_obj->state = TWAI_STATE_RECOVERING;
|
|
|
TWAI_EXIT_CRITICAL();
|
|
|
|
|
|
return ESP_OK;
|
|
|
@@ -661,15 +578,7 @@ esp_err_t twai_get_status_info(twai_status_info_t *status_info)
|
|
|
status_info->rx_missed_count = p_twai_obj->rx_missed_count;
|
|
|
status_info->arb_lost_count = p_twai_obj->arb_lost_count;
|
|
|
status_info->bus_error_count = p_twai_obj->bus_error_count;
|
|
|
- if (p_twai_obj->control_flags & CTRL_FLAG_RECOVERING) {
|
|
|
- status_info->state = TWAI_STATE_RECOVERING;
|
|
|
- } else if (p_twai_obj->control_flags & CTRL_FLAG_BUS_OFF) {
|
|
|
- status_info->state = TWAI_STATE_BUS_OFF;
|
|
|
- } else if (p_twai_obj->control_flags & CTRL_FLAG_STOPPED) {
|
|
|
- status_info->state = TWAI_STATE_STOPPED;
|
|
|
- } else {
|
|
|
- status_info->state = TWAI_STATE_RUNNING;
|
|
|
- }
|
|
|
+ status_info->state = p_twai_obj->state;
|
|
|
TWAI_EXIT_CRITICAL();
|
|
|
|
|
|
return ESP_OK;
|
|
|
@@ -683,7 +592,7 @@ esp_err_t twai_clear_transmit_queue(void)
|
|
|
|
|
|
TWAI_ENTER_CRITICAL();
|
|
|
//If a message is currently undergoing transmission, the tx interrupt handler will decrement tx_msg_count
|
|
|
- p_twai_obj->tx_msg_count = (p_twai_obj->control_flags & CTRL_FLAG_TX_BUFF_OCCUPIED) ? 1 : 0;
|
|
|
+ p_twai_obj->tx_msg_count = twai_hal_check_state_flags(&twai_context, TWAI_HAL_STATE_FLAG_TX_BUFF_OCCUPIED) ? 1 : 0;
|
|
|
xQueueReset(p_twai_obj->tx_queue);
|
|
|
TWAI_EXIT_CRITICAL();
|
|
|
|