diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 7efd4f8..850dfa5 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -39,6 +39,19 @@ menu "Can_wizard Configuration" help 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 diff --git a/main/can.c b/main/can.c index e0e1cee..7f01d89 100644 --- a/main/can.c +++ b/main/can.c @@ -3,6 +3,7 @@ #include "esp_log.h" #include "freertos/portmacro.h" #include "freertos/projdefs.h" +#include "hal/twai_types.h" #include "sdkconfig.h" #include #include @@ -12,6 +13,7 @@ bool is_error_passive = false; bool auto_recovery = false; +bool advanced_filtering = false; SemaphoreHandle_t can_mutex; 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 void can_task(void* arg) { 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); if (sem_res == pdTRUE) { 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); print_w_clr_time(data_bytes_str, LOG_COLOR_BLUE, false); } diff --git a/main/can.h b/main/can.h index acf088e..9d558da 100644 --- a/main/can.h +++ b/main/can.h @@ -42,6 +42,7 @@ extern volatile can_status_t curr_can_state; extern bool timestamp_enabled; extern bool auto_recovery; extern bool is_error_passive; +extern bool advanced_filtering; // functions diff --git a/main/cmd_can.c b/main/cmd_can.c index e2a9099..ea5b61a 100644 --- a/main/cmd_can.c +++ b/main/cmd_can.c @@ -4,6 +4,7 @@ #include "esp_log.h" #include "hal/twai_types.h" #include "inttypes.h" +#include "sdkconfig.h" #include "freertos/projdefs.h" #include "string.h" #include "esp_console.h" @@ -21,6 +22,8 @@ static void register_candown(void); static void register_canstats(void); static void register_canstart(void); static void register_canrecover(void); +static void register_canfilter(void); +static void register_cansmartfilter(void); void register_can_commands(void) { register_cansend(); @@ -29,6 +32,8 @@ void register_can_commands(void) { register_canstats(); register_canstart(); register_canrecover(); + register_canfilter(); + register_cansmartfilter(); } static struct { @@ -144,6 +149,11 @@ static int canup(int argc, char **argv) { twai_general_config_t gen_cfg = default_g_config; // TODO: add CAN filtering 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) { f_config = my_filters; 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); } 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; if (canup_args.mode->count) { 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)); } + +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", "", "Acceptance mask (as in esp-idf docs), uint32_t in hex form, 8 symbols."); + canfilter_args.code = arg_str1("c", "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, " ...", 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)); +} diff --git a/main/console.c b/main/console.c index 4558200..7afe547 100644 --- a/main/console.c +++ b/main/console.c @@ -1,4 +1,5 @@ #include "console.h" +#include "sdkconfig.h" #include #include "esp_err.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)); /* Tell vfs to use usb-serial-jtag driver */ esp_vfs_usb_serial_jtag_use_driver(); - console_config.max_cmdline_args = 8; - console_config.max_cmdline_length = 256; + console_config.max_cmdline_args = CONFIG_CONSOLE_MAX_CMDLINE_ARGS; + console_config.max_cmdline_length = CONFIG_CONSOLE_MAX_CMDLINE_LENGTH; if (use_colors) console_config.hint_color = atoi(LOG_COLOR_CYAN); ESP_ERROR_CHECK(esp_console_init(&console_config)); // linenoiseSetMultiLine(1); diff --git a/sdkconfig b/sdkconfig index 02872c4..f036464 100644 --- a/sdkconfig +++ b/sdkconfig @@ -401,6 +401,9 @@ CONFIG_CAN_TX_GPIO=8 CONFIG_CAN_TASK_PRIORITY=5 CONFIG_CONSOLE_TX_PRIORITY=2 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 #