Code ESP32C3
Code: Select all
#include "usb_serial.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include <fcntl.h>
#include "driver/usb_serial_jtag.h"
#include "esp_vfs_usb_serial_jtag.h"
#include "hal/usb_serial_jtag_ll.h"
#include "esp_vfs_dev.h"
#include "mi_processes.h"
#include "esp_err.h"
#include "esp_log.h"
#include <stdio.h>
#include <stdio_ext.h>
#include "driver/gpio.h"
#define PIN_VBUSMONITOR 8
#define DEBUG_USB_DATA 0
bool send_usb = false;
static void Receive_callback(void *argument);
void init_usb_serial(void)
{
// init GPIO detect USB
gpio_set_direction(PIN_VBUSMONITOR, GPIO_MODE_INPUT);
/* Disable buffering on stdin */
setvbuf(stdin, NULL, _IONBF, 0);
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
/* Enable non-blocking mode on stdin and stdout */
fcntl(fileno(stdout), F_SETFL, 0);
fcntl(fileno(stdin), F_SETFL, 0);
usb_serial_jtag_driver_config_t usb_serial_jtag_config;
usb_serial_jtag_config.rx_buffer_size = 1024;
usb_serial_jtag_config.tx_buffer_size = 1024;
esp_err_t ret = ESP_OK;
/* Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes */
ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
if (ret != ESP_OK)
{
return;
}
/* Tell vfs to use usb-serial-jtag driver */
esp_vfs_usb_serial_jtag_use_driver();
xTaskCreate(Receive_callback, "usb serial callback", 4096, NULL, 5, NULL); // receiving commands from main uart
}
bool get_connect_usb(void)
{
return gpio_get_level(PIN_VBUSMONITOR);
}
static void Receive_callback(void *argument)
{
enumMsgRecState msg_rx_state = MSG_WAIT_FOR_CMD;
// char c[10];
char c;
static uint8_t check_sum;
static uint16_t rx_len;
VBUS_MSG msg;
const int in_fd = fileno(stdin);
for (;;)
{
if(usb_serial_jtag_read_bytes(&c, 1, 0) > 0)
{
#if DEBUG_USB_DATA
printf("char received = %X \n",c);
#endif
switch (msg_rx_state) {
case MSG_WAIT_FOR_CMD:
case MSG_REC_SIGNATURE_1:
case MSG_WAIT_FOR_EXCEPTION:
if (c == MSG_SIGNATURE_1) {
check_sum = c;
msg_rx_state = MSG_REC_SIGNATURE_2;
#if DEBUG_USB_DATA
printf("MSG_WAIT_FOR_CMD");
#endif
}
else {
msg_rx_state = MSG_WAIT_FOR_EXCEPTION;
}
break;
case MSG_REC_SIGNATURE_2:
msg.MsgID = c;
check_sum += c;
msg_rx_state = MSG_REC_LOW_BYTE_LEN;
#if DEBUG_USB_DATA
printf("MSG_REC_SIGNATURE_2");
#endif
break;
case MSG_REC_LOW_BYTE_LEN:
rx_len = 0;
msg.MsgLen = c;
check_sum += c;
#if DEBUG_USB_DATA
printf("MSG_REC_LOW_BYTE_LEN");
#endif
if (msg.MsgLen > 0) {
msg_rx_state = MSG_REC_DATA;
}
else {
msg_rx_state = MSG_REC_CHECKSUM;
}
break;
case MSG_REC_DATA:
msg.MsgParamBuf[rx_len] = c;
check_sum += c;
rx_len++;
#if DEBUG_USB_DATA
printf("MSG_REC_DATA");
#endif
if (rx_len < msg.MsgLen) {
/*Continue receive until get full msg data*/
}
else {
msg_rx_state = MSG_REC_CHECKSUM;
}
break;
case MSG_REC_CHECKSUM:
#if DEBUG_USB_DATA
printf("MSG_REC_CHECKSUM");
#endif
#if DEBUG_USB_DATA
printf("check_sum = 0x%X", check_sum);
#endif
if (check_sum != c) /*Wrong check sum*/
{
msg_rx_state = MSG_WAIT_FOR_CMD;
}
else {
msg_rx_state = MSG_WAIT_PROCESSING;
set_usb_send();
}
break;
case MSG_WAIT_PROCESSING:
default:
break;
}
if(msg_rx_state == MSG_WAIT_PROCESSING)
{
#if DEBUG_USB_DATA
printf("MSG_WAIT_PROCESSING");
#endif
msg_rx_state = MSG_WAIT_FOR_CMD;
Proc_Battery(&msg);
}
}
vTaskDelay(pdMS_TO_TICKS(20));
}
}
void set_usb_send(void)
{
send_usb = true;
}
void reset_usb_send(void)
{
send_usb = false;
}
bool get_mode_usb(void)
{
return send_usb;
}
static void flushWrite(void) {
if (__fbufsize(stdout) > 0) {
fflush(stdout);
}
fsync(fileno(stdout));
}
void usb_send(uint8_t* buffer, uint32_t len)
{
const int out_fd = fileno(stdout);
write(out_fd, buffer, len);
flushWrite();
printf("\n");
// usb_serial_jtag_write_bytes(buffer[, len, 0);
// usb_serial_jtag_ll_txfifo_flush();
}
Thank you for your support.