forked from test34/can_wizard
refactoring
This commit is contained in:
parent
c3594ab41e
commit
d037b1412c
4 changed files with 35 additions and 72 deletions
63
main/can.c
63
main/can.c
|
@ -1,7 +1,6 @@
|
||||||
#include "can.h"
|
#include "can.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "freertos/projdefs.h"
|
#include "freertos/projdefs.h"
|
||||||
#include "hal/twai_types.h"
|
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -51,47 +50,6 @@ static can_status_t get_can_state() {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void can_init() {
|
|
||||||
// Install CAN driver
|
|
||||||
// calculate_hw_can_filter(CONFIG_DEVICE_ID, &f_config, false);
|
|
||||||
static twai_filter_config_t f_config = {.acceptance_code = 0, .acceptance_mask = 0xFFFFFFFF, .single_filter = true};
|
|
||||||
ESP_ERROR_CHECK(twai_driver_install(&g_config, &t_config, &f_config));
|
|
||||||
ESP_LOGI(LOG_TAG, "CAN driver installed");
|
|
||||||
// Start CAN driver
|
|
||||||
ESP_ERROR_CHECK(twai_start());
|
|
||||||
ESP_LOGI(LOG_TAG, "CAN driver started");
|
|
||||||
}
|
|
||||||
|
|
||||||
void can_stop() {
|
|
||||||
// stop and uninstall twai driver to change hardware filters
|
|
||||||
ESP_ERROR_CHECK(twai_stop());
|
|
||||||
ESP_ERROR_CHECK(twai_driver_uninstall());
|
|
||||||
}
|
|
||||||
|
|
||||||
// filtering subset of CAN IDs with hardware filters, need more filtering in software
|
|
||||||
void calculate_hw_can_filter(uint32_t device_id, twai_filter_config_t* filter, bool ota_mode) {
|
|
||||||
filter->single_filter = true;
|
|
||||||
filter->acceptance_code = device_id << 11;
|
|
||||||
if (ota_mode) {
|
|
||||||
filter->acceptance_code += 0x100000 << 11;
|
|
||||||
filter->acceptance_mask = ~((0x1000FF) << 11);
|
|
||||||
} else {
|
|
||||||
filter->acceptance_mask = ~((device_id | 0x1F0000) << 11);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void can_bus_off_check() {
|
|
||||||
uint32_t alerts = 0;
|
|
||||||
if (twai_read_alerts(&alerts, 0) == ESP_OK) {
|
|
||||||
if (alerts & TWAI_ALERT_BUS_OFF) {
|
|
||||||
ESP_ERROR_CHECK(twai_initiate_recovery());
|
|
||||||
}
|
|
||||||
if (alerts & TWAI_ALERT_BUS_RECOVERED) {
|
|
||||||
ESP_ERROR_CHECK(twai_start());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
char byte_str[3];
|
char byte_str[3];
|
||||||
out_str[0] = '\0';
|
out_str[0] = '\0';
|
||||||
|
@ -109,20 +67,29 @@ void can_msg_to_str(twai_message_t *can_msg, char *out_str) {
|
||||||
|
|
||||||
// TODO: add software filtering
|
// TODO: add software filtering
|
||||||
void can_task(void* arg) {
|
void can_task(void* arg) {
|
||||||
|
static const TickType_t can_task_timeout = pdMS_TO_TICKS(100);
|
||||||
|
uint32_t alerts = 0;
|
||||||
can_mutex = xSemaphoreCreateMutex();
|
can_mutex = xSemaphoreCreateMutex();
|
||||||
twai_message_t rx_msg;
|
twai_message_t rx_msg;
|
||||||
char data_bytes_str[50];
|
char data_bytes_str[50];
|
||||||
// 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();
|
|
||||||
// can_bus_off_check();
|
|
||||||
curr_can_state = get_can_state();
|
curr_can_state = get_can_state();
|
||||||
xSemaphoreTake(can_mutex, pdMS_TO_TICKS(200));
|
if (twai_read_alerts(&alerts, 0) == ESP_OK) {
|
||||||
while(twai_receive(&rx_msg, 0) == ESP_OK) {
|
if (alerts & TWAI_ALERT_BUS_OFF) {
|
||||||
|
ESP_LOGE(LOG_TAG, "CAN went bus-off!");
|
||||||
|
// ESP_ERROR_CHECK(twai_initiate_recovery());
|
||||||
|
}
|
||||||
|
if (alerts & TWAI_ALERT_BUS_RECOVERED) {
|
||||||
|
ESP_LOGI(LOG_TAG, "CAN recovered!");
|
||||||
|
// ESP_ERROR_CHECK(twai_start());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (xSemaphoreTake(can_mutex, 0) == pdTRUE) {
|
||||||
|
if (twai_receive(&rx_msg, can_task_timeout) == ESP_OK) {
|
||||||
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);
|
xSemaphoreGive(can_mutex);
|
||||||
vTaskDelay(pdMS_TO_TICKS(100));
|
} else vTaskDelay(can_task_timeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
main/can.h
19
main/can.h
|
@ -2,6 +2,7 @@
|
||||||
#define MAIN_CAN_H
|
#define MAIN_CAN_H
|
||||||
|
|
||||||
#include "driver/twai.h"
|
#include "driver/twai.h"
|
||||||
|
#include "hal/twai_types.h"
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
@ -36,29 +37,11 @@ typedef struct {
|
||||||
uint32_t bus_error_count; /**< Number of instances a bus error has occurred */
|
uint32_t bus_error_count; /**< Number of instances a bus error has occurred */
|
||||||
} can_status_t;
|
} can_status_t;
|
||||||
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
extern SemaphoreHandle_t can_mutex;
|
extern SemaphoreHandle_t can_mutex;
|
||||||
extern volatile can_status_t curr_can_state;
|
extern volatile can_status_t curr_can_state;
|
||||||
|
|
||||||
// functions
|
// functions
|
||||||
|
|
||||||
void can_init();
|
|
||||||
void can_stop();
|
|
||||||
void calculate_hw_can_filter(uint32_t device_id, twai_filter_config_t* filter, bool ota_mode);
|
|
||||||
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);
|
||||||
|
|
||||||
|
|
|
@ -2,14 +2,11 @@
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "freertos/portmacro.h"
|
#include "freertos/portmacro.h"
|
||||||
#include "inttypes.h"
|
#include "inttypes.h"
|
||||||
#include "driver/twai.h"
|
|
||||||
#include "freertos/projdefs.h"
|
#include "freertos/projdefs.h"
|
||||||
#include "hal/twai_types.h"
|
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "esp_console.h"
|
#include "esp_console.h"
|
||||||
#include "argtable3/argtable3.h"
|
#include "argtable3/argtable3.h"
|
||||||
#include "xvprintf.h"
|
#include "xvprintf.h"
|
||||||
#include "can.h"
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
|
@ -1,6 +1,22 @@
|
||||||
#ifndef MAIN_CMD_CAN_H
|
#ifndef MAIN_CMD_CAN_H
|
||||||
#define MAIN_CMD_CAN_H
|
#define MAIN_CMD_CAN_H
|
||||||
|
|
||||||
|
#include "can.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_AND_LOG | TWAI_ALERT_ALL,
|
||||||
|
.clkout_divider = 0,
|
||||||
|
.intr_flags = ESP_INTR_FLAG_LEVEL1,
|
||||||
|
};
|
||||||
|
|
||||||
// functions
|
// functions
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue