continue to add filters. more templates, added commands

This commit is contained in:
Данила Горнушко 2023-11-29 09:30:15 +03:00
parent 42b7132b89
commit a841d5c44b
6 changed files with 104 additions and 7 deletions

View file

@ -39,6 +39,19 @@ menu "Can_wizard Configuration"
help help
A priority of FreeRTOS task can_task_interactive. See more about tasks priorities in FreeRTOS documentation. A priority of FreeRTOS task can_task_interactive. See more about tasks priorities in FreeRTOS documentation.
config CONSOLE_MAX_CMDLINE_LENGTH
int "Console max cmdline length"
range 100 512
default 256
config CONSOLE_MAX_CMDLINE_ARGS
int "Console max cmdline args"
range 1 32
default 8
config CAN_MAX_SMARTFILTERS_NUM
int "Max number of smartfilters"
range 2 20
default 10
endmenu endmenu

View file

@ -3,6 +3,7 @@
#include "esp_log.h" #include "esp_log.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.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>
@ -12,6 +13,7 @@
bool is_error_passive = false; bool is_error_passive = false;
bool auto_recovery = false; bool auto_recovery = false;
bool advanced_filtering = false;
SemaphoreHandle_t can_mutex; SemaphoreHandle_t can_mutex;
volatile can_status_t curr_can_state = { 0 }; volatile can_status_t curr_can_state = { 0 };
@ -66,6 +68,10 @@ void can_msg_to_str(twai_message_t *can_msg, char *start_str, char *out_str) {
} }
} }
bool matches_filters(twai_message_t *msg) {
return true;
}
// 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(200); static const TickType_t can_task_timeout = pdMS_TO_TICKS(200);
@ -106,6 +112,9 @@ void can_task(void* arg) {
sem_res = xSemaphoreTake(can_mutex, 0); sem_res = xSemaphoreTake(can_mutex, 0);
if (sem_res == pdTRUE) { if (sem_res == pdTRUE) {
while ((ret = twai_receive(&rx_msg, can_task_timeout)) == ESP_OK) { while ((ret = twai_receive(&rx_msg, can_task_timeout)) == ESP_OK) {
if (advanced_filtering) {
if (!matches_filters(&rx_msg)) continue;
}
can_msg_to_str(&rx_msg, "recv ", data_bytes_str); can_msg_to_str(&rx_msg, "recv ", data_bytes_str);
print_w_clr_time(data_bytes_str, LOG_COLOR_BLUE, false); print_w_clr_time(data_bytes_str, LOG_COLOR_BLUE, false);
} }

View file

@ -42,6 +42,7 @@ extern volatile can_status_t curr_can_state;
extern bool timestamp_enabled; extern bool timestamp_enabled;
extern bool auto_recovery; extern bool auto_recovery;
extern bool is_error_passive; extern bool is_error_passive;
extern bool advanced_filtering;
// functions // functions

View file

@ -4,6 +4,7 @@
#include "esp_log.h" #include "esp_log.h"
#include "hal/twai_types.h" #include "hal/twai_types.h"
#include "inttypes.h" #include "inttypes.h"
#include "sdkconfig.h"
#include "freertos/projdefs.h" #include "freertos/projdefs.h"
#include "string.h" #include "string.h"
#include "esp_console.h" #include "esp_console.h"
@ -21,6 +22,8 @@ static void register_candown(void);
static void register_canstats(void); static void register_canstats(void);
static void register_canstart(void); static void register_canstart(void);
static void register_canrecover(void); static void register_canrecover(void);
static void register_canfilter(void);
static void register_cansmartfilter(void);
void register_can_commands(void) { void register_can_commands(void) {
register_cansend(); register_cansend();
@ -29,6 +32,8 @@ void register_can_commands(void) {
register_canstats(); register_canstats();
register_canstart(); register_canstart();
register_canrecover(); register_canrecover();
register_canfilter();
register_cansmartfilter();
} }
static struct { static struct {
@ -144,6 +149,11 @@ static int canup(int argc, char **argv) {
twai_general_config_t gen_cfg = default_g_config; twai_general_config_t gen_cfg = default_g_config;
// TODO: add CAN filtering // TODO: add CAN filtering
twai_filter_config_t f_config; twai_filter_config_t f_config;
int nerrors = arg_parse(argc, argv, (void **) &canup_args);
if (nerrors != 0) {
arg_print_errors(stderr, canup_args.end, argv[0]);
return 1;
}
if (canup_args.filters->count) { if (canup_args.filters->count) {
f_config = my_filters; f_config = my_filters;
print_w_clr_time("Using predefined filters.", LOG_COLOR_GREEN, true); print_w_clr_time("Using predefined filters.", LOG_COLOR_GREEN, true);
@ -152,11 +162,6 @@ static int canup(int argc, char **argv) {
print_w_clr_time("Using accept all filters.", LOG_COLOR_GREEN, true); print_w_clr_time("Using accept all filters.", LOG_COLOR_GREEN, true);
} }
esp_log_level_t prev_gpio_lvl = esp_log_level_get("gpio"); esp_log_level_t prev_gpio_lvl = esp_log_level_get("gpio");
int nerrors = arg_parse(argc, argv, (void **) &canup_args);
if (nerrors != 0) {
arg_print_errors(stderr, canup_args.end, argv[0]);
return 1;
}
int mode = 0; int mode = 0;
if (canup_args.mode->count) { if (canup_args.mode->count) {
const char* mode_str = canup_args.mode->sval[0]; const char* mode_str = canup_args.mode->sval[0];
@ -353,3 +358,68 @@ static void register_canrecover(void) {
}; };
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
} }
static struct {
struct arg_lit *dual;
struct arg_str *code;
struct arg_str *mask;
struct arg_end *end;
} canfilter_args;
static int canfilter(int argc, char **argv) {
int nerrors = arg_parse(argc, argv, (void **) &canfilter_args);
if (nerrors != 0) {
arg_print_errors(stderr, canfilter_args.end, argv[0]);
return 1;
}
if (canfilter_args.dual->count) {
my_filters.single_filter = false;
} else {
my_filters.single_filter = true;
}
return 0;
}
static void register_canfilter(void) {
canfilter_args.mask = arg_str1("m", "mask", "<mask>", "Acceptance mask (as in esp-idf docs), uint32_t in hex form, 8 symbols.");
canfilter_args.code = arg_str1("c", "code", "<code>", "Acceptance code (as in esp-idf docs), uint32_t in hex form, 8 symbols.");
canfilter_args.dual = arg_lit0("d", NULL, "Use Dual Filter Mode.");
const esp_console_cmd_t cmd = {
.command = "canfilter",
.help = "Manually setup basic hardware filtering.",
.hint = NULL,
.func = &canfilter,
.argtable = &canfilter_args,
};
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}
static struct {
struct arg_str *filters;
struct arg_end *end;
} cansmart_args;
static int cansmartfilter(int argc, char **argv) {
int nerrors = arg_parse(argc, argv, (void **) &cansmart_args);
if (nerrors != 0) {
arg_print_errors(stderr, cansmart_args.end, argv[0]);
return 1;
}
return 0;
}
static void register_cansmartfilter(void) {
cansmart_args.filters = arg_strn(NULL, NULL, "<filter1> <filter2> ...", 1, CONFIG_CAN_MAX_SMARTFILTERS_NUM, "Filters, in hex format. Each one contains mask and code in format code#mask. Both mask and code are uint32_t numbers in hex format. Example: 0000FF00#0000FFFF");
const esp_console_cmd_t cmd = {
.command = "cansmartfilter",
.help = "Setup smart mixed filters (hardware + software). Num of filters can be up to the value in config.",
.hint = NULL,
.func = &cansmartfilter,
.argtable = &cansmart_args,
};
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
}

View file

@ -1,4 +1,5 @@
#include "console.h" #include "console.h"
#include "sdkconfig.h"
#include <fcntl.h> #include <fcntl.h>
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
@ -197,8 +198,8 @@ void initialize_console(void) {
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usb_serial_jtag_config)); ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usb_serial_jtag_config));
/* Tell vfs to use usb-serial-jtag driver */ /* Tell vfs to use usb-serial-jtag driver */
esp_vfs_usb_serial_jtag_use_driver(); esp_vfs_usb_serial_jtag_use_driver();
console_config.max_cmdline_args = 8; console_config.max_cmdline_args = CONFIG_CONSOLE_MAX_CMDLINE_ARGS;
console_config.max_cmdline_length = 256; console_config.max_cmdline_length = CONFIG_CONSOLE_MAX_CMDLINE_LENGTH;
if (use_colors) console_config.hint_color = atoi(LOG_COLOR_CYAN); if (use_colors) console_config.hint_color = atoi(LOG_COLOR_CYAN);
ESP_ERROR_CHECK(esp_console_init(&console_config)); ESP_ERROR_CHECK(esp_console_init(&console_config));
// linenoiseSetMultiLine(1); // linenoiseSetMultiLine(1);

View file

@ -401,6 +401,9 @@ CONFIG_CAN_TX_GPIO=8
CONFIG_CAN_TASK_PRIORITY=5 CONFIG_CAN_TASK_PRIORITY=5
CONFIG_CONSOLE_TX_PRIORITY=2 CONFIG_CONSOLE_TX_PRIORITY=2
CONFIG_CONSOLE_INT_PRIORITY=1 CONFIG_CONSOLE_INT_PRIORITY=1
CONFIG_CONSOLE_MAX_CMDLINE_LENGTH=256
CONFIG_CONSOLE_MAX_CMDLINE_ARGS=22
CONFIG_CAN_MAX_SMARTFILTERS_NUM=10
# end of Can_wizard Configuration # end of Can_wizard Configuration
# #