diff --git a/main/can.c b/main/can.c index 744f37a..d8621be 100644 --- a/main/can.c +++ b/main/can.c @@ -10,22 +10,28 @@ #include "xvprintf.h" -static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS(); -static const twai_general_config_t g_config = { - .mode = TWAI_MODE_NORMAL, - .tx_io = CONFIG_CAN_TX_GPIO, - .rx_io = CONFIG_CAN_RX_GPIO, - .clkout_io = TWAI_IO_UNUSED, - .bus_off_io = TWAI_IO_UNUSED, - .tx_queue_len = 5, - .rx_queue_len = 5, - .alerts_enabled = TWAI_ALERT_BUS_OFF | TWAI_ALERT_BUS_RECOVERED, - .clkout_divider = 0, - .intr_flags = ESP_INTR_FLAG_LEVEL1, -}; static const char* LOG_TAG = "can"; +bool is_error_passive = false; + +can_state_e get_can_state() { + twai_status_info_t status; + esp_err_t res = twai_get_status_info(&status); + if (res != ESP_OK) return CAN_NOT_INSTALLED; + switch (status.state) { + case TWAI_STATE_STOPPED: + return CAN_STOPPED; + case TWAI_STATE_BUS_OFF: + return CAN_BUF_OFF; + case TWAI_STATE_RECOVERING: + return CAN_RECOVERING; + default: + if (is_error_passive) return CAN_ERROR_PASSIVE; + else return CAN_ERROR_ACTIVE; + } +} + void can_init() { // Install CAN driver // calculate_hw_can_filter(CONFIG_DEVICE_ID, &f_config, false); diff --git a/main/can.h b/main/can.h index 7404b96..25b3c35 100644 --- a/main/can.h +++ b/main/can.h @@ -4,6 +4,39 @@ #include "driver/twai.h" #include "sdkconfig.h" +typedef struct { + char status[30]; + int tec; + int rec; + int color; + bool extd; +} can_prompt_t; + +typedef enum { + CAN_NOT_INSTALLED = 0, + CAN_STOPPED = 1, + CAN_ERROR_ACTIVE = 2, + CAN_ERROR_PASSIVE = 3, + CAN_BUF_OFF = 4, + CAN_RECOVERING = 5, +} can_state_e; + + +static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS(); +static const twai_general_config_t g_config = { + .mode = TWAI_MODE_NORMAL, + .tx_io = CONFIG_CAN_TX_GPIO, + .rx_io = CONFIG_CAN_RX_GPIO, + .clkout_io = TWAI_IO_UNUSED, + .bus_off_io = TWAI_IO_UNUSED, + .tx_queue_len = 5, + .rx_queue_len = 5, + .alerts_enabled = TWAI_ALERT_BUS_OFF | TWAI_ALERT_BUS_RECOVERED, + .clkout_divider = 0, + .intr_flags = ESP_INTR_FLAG_LEVEL1, +}; + + // functions void can_init(); @@ -12,5 +45,6 @@ void calculate_hw_can_filter(uint32_t device_id, twai_filter_config_t* filter, b void can_bus_off_check(); void can_task(void* arg); void can_msg_to_str(twai_message_t *can_msg, char *out_str); +can_state_e get_can_state(); #endif // MAIN_CAN_H diff --git a/main/cmd_can.c b/main/cmd_can.c index 4543531..1208fba 100644 --- a/main/cmd_can.c +++ b/main/cmd_can.c @@ -1,4 +1,5 @@ #include "cmd_can.h" +#include "inttypes.h" #include "driver/twai.h" #include "freertos/projdefs.h" #include "hal/twai_types.h"