2023-11-24 05:43:28 +00:00
|
|
|
#include "cmd_can.h"
|
|
|
|
#include "driver/twai.h"
|
2023-11-24 09:02:17 +00:00
|
|
|
#include "freertos/projdefs.h"
|
2023-11-24 05:43:28 +00:00
|
|
|
#include "hal/twai_types.h"
|
2023-11-24 09:02:17 +00:00
|
|
|
#include "string.h"
|
|
|
|
#include "esp_console.h"
|
2023-11-24 10:13:14 +00:00
|
|
|
#include "argtable3/argtable3.h"
|
2023-11-24 09:02:17 +00:00
|
|
|
#include "xvprintf.h"
|
|
|
|
#include "can.h"
|
|
|
|
|
2023-11-24 13:22:22 +00:00
|
|
|
|
2023-11-24 09:02:17 +00:00
|
|
|
static void register_send_can_frame(void);
|
2023-11-24 05:43:28 +00:00
|
|
|
|
|
|
|
void register_can_commands(void) {
|
2023-11-24 09:02:17 +00:00
|
|
|
register_send_can_frame();
|
2023-11-24 05:43:28 +00:00
|
|
|
|
|
|
|
}
|
2023-11-24 09:02:17 +00:00
|
|
|
|
2023-11-24 10:13:14 +00:00
|
|
|
static struct {
|
|
|
|
struct arg_str *message;
|
|
|
|
struct arg_end *end;
|
|
|
|
} cansend_args;
|
|
|
|
|
2023-11-24 09:02:17 +00:00
|
|
|
static int send_can_frame(int argc, char **argv) {
|
2023-11-24 13:22:22 +00:00
|
|
|
char printf_str[50];
|
|
|
|
char can_msg_str_buf[50];
|
2023-11-24 10:13:14 +00:00
|
|
|
int nerrors = arg_parse(argc, argv, (void **) &cansend_args);
|
|
|
|
if (nerrors != 0) {
|
|
|
|
arg_print_errors(stderr, cansend_args.end, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-11-24 12:46:30 +00:00
|
|
|
|
2023-11-24 13:22:22 +00:00
|
|
|
const char *can_msg_ptr = cansend_args.message->sval[0];
|
2023-11-24 12:46:30 +00:00
|
|
|
strlcpy(can_msg_str_buf, can_msg_ptr, sizeof(can_msg_str_buf));
|
|
|
|
printf("%s\n", can_msg_str_buf);
|
2023-11-24 10:13:14 +00:00
|
|
|
|
2023-11-24 09:02:17 +00:00
|
|
|
twai_message_t msg = {.extd = 1};
|
|
|
|
msg.data_length_code = 0;
|
|
|
|
msg.identifier = 0xFF << 8;
|
|
|
|
twai_transmit(&msg, pdMS_TO_TICKS(1000));
|
2023-11-24 12:46:30 +00:00
|
|
|
can_msg_to_str(&msg, printf_str);
|
|
|
|
printf("sent %s\n", printf_str);
|
2023-11-24 09:02:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void register_send_can_frame(void) {
|
2023-11-24 10:13:14 +00:00
|
|
|
|
|
|
|
cansend_args.message = arg_str1(NULL, NULL, "ID#data", "Message to send, ID and data bytes, all in hex. # is the delimiter.");
|
|
|
|
cansend_args.end = arg_end(2);
|
|
|
|
|
2023-11-24 09:02:17 +00:00
|
|
|
const esp_console_cmd_t cmd = {
|
|
|
|
.command = "cansend",
|
2023-11-24 10:13:14 +00:00
|
|
|
.help = "Send a can message to the bus, example: cansend 00008C03#02",
|
2023-11-24 09:02:17 +00:00
|
|
|
.hint = NULL,
|
|
|
|
.func = &send_can_frame,
|
2023-11-24 10:13:14 +00:00
|
|
|
.argtable = &cansend_args,
|
2023-11-24 09:02:17 +00:00
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
|
|
|
|
}
|