probably working canup, candown, canstop, new realization on canstats

This commit is contained in:
Данила Горнушко 2023-11-26 13:55:32 +03:00
parent ba67c741d9
commit 18514d4d96
3 changed files with 43 additions and 26 deletions

View file

@ -16,21 +16,39 @@ bool is_error_passive = false;
SemaphoreHandle_t can_mutex; SemaphoreHandle_t can_mutex;
can_status_t curr_can_state = { 0 }; can_status_t curr_can_state = { 0 };
can_state_e get_can_state() { static can_status_t get_can_state() {
twai_status_info_t status; can_status_t result;
twai_status_info_t status = { 0 };
esp_err_t res = twai_get_status_info(&status); esp_err_t res = twai_get_status_info(&status);
if (res != ESP_OK) return CAN_NOT_INSTALLED; if (res != ESP_OK) {
result.state = CAN_NOT_INSTALLED;
return result;
}
result.msgs_to_rx = status.msgs_to_rx;
result.msgs_to_tx = status.msgs_to_tx;
result.arb_lost_count = status.arb_lost_count;
result.bus_error_count = status.bus_error_count;
result.tx_error_counter = status.tx_error_counter;
result.rx_error_counter = status.rx_error_counter;
result.tx_failed_count = status.tx_failed_count;
result.rx_missed_count = status.rx_missed_count;
result.rx_overrun_count = status.rx_overrun_count;
switch (status.state) { switch (status.state) {
case TWAI_STATE_STOPPED: case TWAI_STATE_STOPPED:
return CAN_STOPPED; result.state = CAN_STOPPED;
break;
case TWAI_STATE_BUS_OFF: case TWAI_STATE_BUS_OFF:
return CAN_BUF_OFF; result.state = CAN_BUF_OFF;
break;
case TWAI_STATE_RECOVERING: case TWAI_STATE_RECOVERING:
return CAN_RECOVERING; result.state = CAN_RECOVERING;
break;
default: default:
if (is_error_passive) return CAN_ERROR_PASSIVE; if (is_error_passive) result.state = CAN_ERROR_PASSIVE;
else return CAN_ERROR_ACTIVE; else result.state = CAN_ERROR_ACTIVE;
break;
} }
return result;
} }
void can_init() { void can_init() {
@ -89,6 +107,7 @@ void can_msg_to_str(twai_message_t *can_msg, char *out_str) {
} }
} }
// TODO: add software filtering
void can_task(void* arg) { void can_task(void* arg) {
can_mutex = xSemaphoreCreateMutex(); can_mutex = xSemaphoreCreateMutex();
twai_message_t rx_msg; twai_message_t rx_msg;
@ -96,12 +115,14 @@ void can_task(void* arg) {
// ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); // ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
for (;;) { // A Task shall never return or exit. for (;;) { // A Task shall never return or exit.
// esp_task_wdt_reset(); // esp_task_wdt_reset();
can_bus_off_check(); // can_bus_off_check();
if (twai_receive(&rx_msg, pdMS_TO_TICKS(1000)) != ESP_OK) continue; curr_can_state = get_can_state();
// TODO: add software filtering xSemaphoreTake(can_mutex, pdMS_TO_TICKS(200));
// if ((((rx_msg.identifier >> 8) & 0xFF) != CONFIG_DEVICE_ID) && (((rx_msg.identifier >> 8) & 0xFF) != 0xFF)) continue; while(twai_receive(&rx_msg, 0) == ESP_OK) {
// ESP_LOGI(LOG_TAG, "received can frame: %" PRIu32, rx_msg.identifier); can_msg_to_str(&rx_msg, data_bytes_str);
can_msg_to_str(&rx_msg, data_bytes_str); xprintf(LOG_COLOR(LOG_COLOR_BLUE) "recv %s\n" LOG_RESET_COLOR, data_bytes_str);
xprintf(LOG_COLOR(LOG_COLOR_BLUE) "recv %s\n" LOG_RESET_COLOR, data_bytes_str); }
xSemaphoreGive(can_mutex);
vTaskDelay(pdMS_TO_TICKS(100));
} }
} }

View file

@ -61,6 +61,5 @@ void calculate_hw_can_filter(uint32_t device_id, twai_filter_config_t* filter, b
void can_bus_off_check(); void can_bus_off_check();
void can_task(void* arg); void can_task(void* arg);
void can_msg_to_str(twai_message_t *can_msg, char *out_str); void can_msg_to_str(twai_message_t *can_msg, char *out_str);
can_state_e get_can_state();
#endif // MAIN_CAN_H #endif // MAIN_CAN_H

View file

@ -99,20 +99,17 @@ static const char* can_states_str[] = {
}; };
static int canstats(int argc, char **argv) { static int canstats(int argc, char **argv) {
can_state_e canstate = get_can_state(); if (curr_can_state.state == CAN_NOT_INSTALLED) {
if (canstate == CAN_NOT_INSTALLED) {
printf("CAN driver is not installed!"); printf("CAN driver is not installed!");
return 0; return 0;
} else { } else {
twai_status_info_t status; const char *state_str = can_states_str[curr_can_state.state];
twai_get_status_info(&status);
const char *state_str = can_states_str[canstate];
printf("status: %s\n", state_str); printf("status: %s\n", state_str);
printf("TX Err Counter: %" PRIu32 "\n", status.tx_error_counter); printf("TX Err Counter: %" PRIu32 "\n", curr_can_state.tx_error_counter);
printf("RX Err Counter: %" PRIu32 "\n", status.rx_error_counter); printf("RX Err Counter: %" PRIu32 "\n", curr_can_state.rx_error_counter);
printf("Failed transmit: %" PRIu32 "\n", status.tx_failed_count); printf("Failed transmit: %" PRIu32 "\n", curr_can_state.tx_failed_count);
printf("Arbitration lost times: %" PRIu32 "\n", status.arb_lost_count); printf("Arbitration lost times: %" PRIu32 "\n", curr_can_state.arb_lost_count);
printf("Bus-off count: %" PRIu32 "\n", status.bus_error_count); printf("Bus-off count: %" PRIu32 "\n", curr_can_state.bus_error_count);
} }
return 0; return 0;
} }