diff --git a/main/can.c b/main/can.c index d8621be..c380537 100644 --- a/main/can.c +++ b/main/can.c @@ -9,12 +9,13 @@ #include "freertos/ringbuf.h" #include "xvprintf.h" - - static const char* LOG_TAG = "can"; bool is_error_passive = false; +SemaphoreHandle_t can_mutex; +can_status_t curr_can_state = { 0 }; + can_state_e get_can_state() { twai_status_info_t status; esp_err_t res = twai_get_status_info(&status); @@ -89,6 +90,7 @@ void can_msg_to_str(twai_message_t *can_msg, char *out_str) { } void can_task(void* arg) { + can_mutex = xSemaphoreCreateMutex(); twai_message_t rx_msg; char data_bytes_str[50]; // ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); diff --git a/main/can.h b/main/can.h index 25b3c35..af7b279 100644 --- a/main/can.h +++ b/main/can.h @@ -3,6 +3,8 @@ #include "driver/twai.h" #include "sdkconfig.h" +#include "freertos/semphr.h" +#include typedef struct { char status[30]; @@ -21,6 +23,18 @@ typedef enum { CAN_RECOVERING = 5, } can_state_e; +typedef struct { + can_state_e state; + uint32_t msgs_to_tx; /**< Number of messages queued for transmission or awaiting transmission completion */ + uint32_t msgs_to_rx; /**< Number of messages in RX queue waiting to be read */ + uint32_t tx_error_counter; /**< Current value of Transmit Error Counter */ + uint32_t rx_error_counter; /**< Current value of Receive Error Counter */ + uint32_t tx_failed_count; /**< Number of messages that failed transmissions */ + uint32_t rx_missed_count; /**< Number of messages that were lost due to a full RX queue (or errata workaround if enabled) */ + uint32_t rx_overrun_count; /**< Number of messages that were lost due to a RX FIFO overrun */ + uint32_t arb_lost_count; /**< Number of instances arbitration was lost */ + uint32_t bus_error_count; /**< Number of instances a bus error has occurred */ +} can_status_t; static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS(); static const twai_general_config_t g_config = { @@ -36,6 +50,8 @@ static const twai_general_config_t g_config = { .intr_flags = ESP_INTR_FLAG_LEVEL1, }; +extern SemaphoreHandle_t can_mutex; +extern can_status_t curr_can_state; // functions diff --git a/main/cmd_can.c b/main/cmd_can.c index 1208fba..a857943 100644 --- a/main/cmd_can.c +++ b/main/cmd_can.c @@ -1,4 +1,5 @@ #include "cmd_can.h" +#include "freertos/portmacro.h" #include "inttypes.h" #include "driver/twai.h" #include "freertos/projdefs.h" @@ -12,7 +13,6 @@ #include #include -static void register_send_can_frame(void); static void register_send_can_frame(void); static void register_canup(void); static void register_candown(void); @@ -22,7 +22,11 @@ static void register_canrecover(void); void register_can_commands(void) { register_send_can_frame(); - + register_canup(); + register_candown(); + register_canstats(); + register_canstart(); + register_canrecover(); } static struct { @@ -114,31 +118,38 @@ static int canstats(int argc, char **argv) { } static int canup(int argc, char **argv) { + esp_err_t res; + xSemaphoreTake(can_mutex, portMAX_DELAY); // Install CAN driver // TODO: add CAN filtering static twai_filter_config_t f_config = {.acceptance_code = 0, .acceptance_mask = 0xFFFFFFFF, .single_filter = true}; - esp_err_t res = twai_driver_install(&g_config, &t_config, &f_config); + res = twai_driver_install(&g_config, &t_config, &f_config); if (res != ESP_OK) { printf("Couldn't install CAN driver! Rebooting...\n"); esp_restart(); } printf("CAN driver installed\n"); // Start CAN driver - ESP_ERROR_CHECK(twai_start()); + res = twai_start(); printf("CAN driver started\n"); + xSemaphoreGive(can_mutex); return 0; } static int canstart(int argc, char **argv) { // Start CAN driver + xSemaphoreTake(can_mutex, portMAX_DELAY); ESP_ERROR_CHECK(twai_start()); printf("CAN driver started\n"); + xSemaphoreGive(can_mutex); return 0; } static int candown(int argc, char **argv) { + xSemaphoreTake(can_mutex, portMAX_DELAY); ESP_ERROR_CHECK(twai_stop()); ESP_ERROR_CHECK(twai_driver_uninstall()); + xSemaphoreGive(can_mutex); return 0; } diff --git a/main/main.c b/main/main.c index 36afdd0..73a97aa 100644 --- a/main/main.c +++ b/main/main.c @@ -8,7 +8,6 @@ void app_main(void) { - can_init(); init_tx_ringbuf(); xTaskCreate(can_task, "can task", 4800, NULL, CONFIG_CAN_TASK_PRIORITY, NULL); initialize_filesystem();