78 lines
2.4 KiB
C
78 lines
2.4 KiB
C
#include <stdint.h>
|
|
#include "esp_log.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "tinyusb.h"
|
|
#include "tusb_cdc_acm.h"
|
|
#include "sdkconfig.h"
|
|
#include "include/Sys_Printer.h"
|
|
|
|
|
|
static const char *TAG = "usb_cdc";
|
|
|
|
extern QueueHandle_t User_data_queue;
|
|
user_data_t Receive_usb_data;
|
|
|
|
// static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1];
|
|
|
|
void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event)
|
|
{
|
|
/* initialization */
|
|
size_t rx_size = 0;
|
|
|
|
/* read */
|
|
esp_err_t ret = tinyusb_cdcacm_read(itf, &Receive_usb_data.data[0], CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size);
|
|
if (ret == ESP_OK) {
|
|
// ESP_LOGI(TAG, "Data from channel %d:", itf);
|
|
// ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO);
|
|
Receive_usb_data.type = USB;
|
|
Receive_usb_data.size = rx_size;
|
|
xQueueSend(User_data_queue, &Receive_usb_data, portMAX_DELAY);
|
|
|
|
} else {
|
|
ESP_LOGE(TAG, "Read error");
|
|
}
|
|
|
|
/* write back */
|
|
// tinyusb_cdcacm_write_queue(itf, buf, rx_size);
|
|
// tinyusb_cdcacm_write_flush(itf, 0);
|
|
}
|
|
|
|
void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event)
|
|
{
|
|
int dtr = event->line_state_changed_data.dtr;
|
|
int rts = event->line_state_changed_data.rts;
|
|
ESP_LOGI(TAG, "Line state changed on channel %d: DTR:%d, RTS:%d", itf, dtr, rts);
|
|
}
|
|
|
|
|
|
void Usb_Virtual_serial_port_init(void)
|
|
{
|
|
ESP_LOGI(TAG, "USB initialization");
|
|
const tinyusb_config_t tusb_cfg = {
|
|
.device_descriptor = NULL,
|
|
.string_descriptor = NULL,
|
|
.external_phy = false,
|
|
.configuration_descriptor = NULL,
|
|
};
|
|
|
|
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
|
|
|
|
tinyusb_config_cdcacm_t acm_cfg = {
|
|
.usb_dev = TINYUSB_USBDEV_0,
|
|
.cdc_port = TINYUSB_CDC_ACM_0,
|
|
.rx_unread_buf_sz = 64,
|
|
.callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback
|
|
.callback_rx_wanted_char = NULL,
|
|
.callback_line_state_changed = NULL,
|
|
.callback_line_coding_changed = NULL
|
|
};
|
|
|
|
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
|
|
/* the second way to register a callback */
|
|
ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback(
|
|
TINYUSB_CDC_ACM_0,
|
|
CDC_EVENT_LINE_STATE_CHANGED,
|
|
&tinyusb_cdc_line_state_changed_callback));
|
|
ESP_LOGI(TAG, "USB initialization DONE");
|
|
} |