blackmagic-esp32-s2/main/usb-cdc.c

184 lines
4.9 KiB
C
Raw Normal View History

2021-10-30 07:01:10 +01:00
/**
* @file usb-cdc.c
* Do not forget to take pid's when implement own USB device class
*
* https://github.com/espressif/usb-pids
*
*/
2021-10-14 09:04:00 +01:00
#include <stdint.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
2021-10-17 15:06:53 +01:00
#include <freertos/stream_buffer.h>
2021-10-14 09:04:00 +01:00
#include <tinyusb.h>
#include <tusb_cdc_acm.h>
#include <sdkconfig.h>
2021-11-30 04:38:57 +00:00
#include <driver/gpio.h>
2021-10-17 21:30:20 +01:00
#include "usb-cdc.h"
2021-11-19 19:14:52 +00:00
#include "led.h"
#include "delay.h"
2021-11-30 04:38:57 +00:00
#include <gdb-glue.h>
2021-11-19 19:14:52 +00:00
#define USB_DN_PIN (19)
#define USB_DP_PIN (20)
2021-11-30 04:38:57 +00:00
#define CDC_USB_DEV (TINYUSB_USBDEV_0)
2021-10-14 09:04:00 +01:00
static const char* TAG = "usb-cdc";
2021-11-30 04:38:57 +00:00
static uint8_t buffer_rx[CONFIG_USB_CDC_RX_BUFSIZE];
2021-10-14 09:04:00 +01:00
2021-10-17 15:06:53 +01:00
typedef struct {
volatile bool connected;
volatile bool dtr;
volatile bool rts;
2021-11-30 04:38:57 +00:00
} USBCDC;
2021-10-17 15:06:53 +01:00
2021-11-30 04:38:57 +00:00
static USBCDC usb_cdc;
2021-10-17 15:06:53 +01:00
2021-10-17 21:30:20 +01:00
void usb_cdc_tx_char(uint8_t c, bool flush) {
2021-11-30 04:38:57 +00:00
tinyusb_cdcacm_write_queue(CDC_USB_DEV, &c, 1);
2021-10-17 15:06:53 +01:00
if(flush) {
2021-10-17 21:30:20 +01:00
// SOME GDB MAGIC
// We need to send an empty packet for some hosts to accept this as a complete transfer.
uint8_t zero_byte = 0;
2021-11-30 04:38:57 +00:00
tinyusb_cdcacm_write_queue(CDC_USB_DEV, &zero_byte, 1);
2021-10-17 21:30:20 +01:00
2021-10-17 15:06:53 +01:00
// TODO: timeout size
2021-11-30 04:38:57 +00:00
ESP_ERROR_CHECK_WITHOUT_ABORT(tinyusb_cdcacm_write_flush(CDC_USB_DEV, 1000));
2021-10-17 15:06:53 +01:00
}
}
void usb_cdc_rx_callback(int itf, cdcacm_event_t* event) {
2021-11-30 04:38:57 +00:00
if(gdb_glue_can_receive()) {
size_t max_len = gdb_glue_get_free_size();
2021-10-17 15:06:53 +01:00
if(max_len > CONFIG_USB_CDC_RX_BUFSIZE) max_len = CONFIG_USB_CDC_RX_BUFSIZE;
size_t rx_size = 0;
esp_err_t err = tinyusb_cdcacm_read(itf, buffer_rx, max_len, &rx_size);
if(err == ESP_OK) {
if(rx_size > 0) {
2021-11-30 04:38:57 +00:00
gdb_glue_receive(buffer_rx, rx_size);
2021-10-30 07:01:10 +01:00
}
2021-10-17 15:06:53 +01:00
} else {
ESP_LOGE(TAG, "Read error");
}
2021-11-30 04:38:57 +00:00
}
2021-10-14 09:04:00 +01:00
}
void usb_cdc_line_state_changed_callback(int itf, cdcacm_event_t* event) {
2021-11-30 04:38:57 +00:00
usb_cdc.dtr = event->line_state_changed_data.dtr;
usb_cdc.rts = event->line_state_changed_data.rts;
2021-10-14 09:04:00 +01:00
2021-11-30 04:38:57 +00:00
ESP_LOGI(TAG, "Line state changed! dtr:%d, rst:%d", usb_cdc.dtr, usb_cdc.rts);
2021-10-14 09:04:00 +01:00
}
void usb_cdc_line_coding_changed_callback(int itf, cdcacm_event_t* event) {
uint32_t bit_rate = event->line_coding_changed_data.p_line_coding->bit_rate;
uint8_t stop_bits = event->line_coding_changed_data.p_line_coding->stop_bits;
uint8_t parity = event->line_coding_changed_data.p_line_coding->parity;
uint8_t data_bits = event->line_coding_changed_data.p_line_coding->data_bits;
ESP_LOGI(
TAG,
"Line coding changed! bit_rate:%d, stop_bits:%d, parity:%d, data_bits:%d",
bit_rate,
stop_bits,
parity,
data_bits);
}
2021-10-17 15:06:53 +01:00
//--------------------------------------------------------------------+
// Device callbacks
//--------------------------------------------------------------------+
2021-11-19 19:14:52 +00:00
// It seems like a reliable way is to rely on tud_mount_cb on connect and tud_suspend_cb on disconnect
2021-10-17 15:06:53 +01:00
void tud_mount_cb(void) {
ESP_LOGI(TAG, "Mount");
2021-11-19 19:14:52 +00:00
2021-11-30 04:38:57 +00:00
if(!usb_cdc.connected) {
2021-11-19 19:14:52 +00:00
led_set_blue(255);
delay(10);
led_set_blue(0);
}
2021-11-30 04:38:57 +00:00
usb_cdc.connected = true;
2021-10-17 15:06:53 +01:00
}
void tud_umount_cb(void) {
ESP_LOGI(TAG, "Unmount");
2021-11-19 19:14:52 +00:00
2021-11-30 04:38:57 +00:00
if(usb_cdc.connected) {
2021-11-19 19:14:52 +00:00
led_set_blue(255);
delay(10);
led_set_blue(0);
}
2021-11-30 04:38:57 +00:00
usb_cdc.connected = false;
2021-11-19 19:14:52 +00:00
}
void tud_resume_cb(void) {
ESP_LOGI(TAG, "Resume");
2021-11-30 04:38:57 +00:00
if(usb_cdc.connected) {
2021-11-19 19:14:52 +00:00
led_set_blue(255);
delay(10);
led_set_blue(0);
}
2021-11-30 04:38:57 +00:00
usb_cdc.connected = true;
2021-11-19 19:14:52 +00:00
}
void tud_suspend_cb(bool remote_wakeup_en) {
ESP_LOGI(TAG, "Suspend");
2021-11-30 04:38:57 +00:00
if(usb_cdc.connected) {
2021-11-19 19:14:52 +00:00
led_set_blue(255);
delay(10);
led_set_blue(0);
}
2021-11-30 04:38:57 +00:00
usb_cdc.connected = false;
2021-10-17 15:06:53 +01:00
}
2021-10-14 09:04:00 +01:00
void usb_cdc_init(void) {
2021-11-30 04:38:57 +00:00
usb_cdc.connected = false;
2021-10-17 15:06:53 +01:00
2021-11-22 11:23:22 +00:00
ESP_LOGI(TAG, "init");
2021-11-19 19:14:52 +00:00
gpio_config_t io_conf;
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT_OD;
io_conf.pin_bit_mask = ((1 << USB_DN_PIN) | (1 << USB_DP_PIN));
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
2021-11-22 11:57:40 +00:00
gpio_set_level(USB_DN_PIN, 0);
2021-11-19 19:14:52 +00:00
gpio_set_level(USB_DP_PIN, 0);
2021-11-29 10:06:42 +00:00
delay(100);
2021-11-19 19:14:52 +00:00
gpio_set_level(USB_DN_PIN, 1);
2021-11-22 11:57:40 +00:00
gpio_set_level(USB_DP_PIN, 1);
2021-11-19 19:14:52 +00:00
tinyusb_config_t tusb_cfg = {
.descriptor = NULL, //Uses default descriptor specified in Menuconfig
.string_descriptor = NULL, //Uses default string specified in Menuconfig
.external_phy = false,
};
2021-10-14 09:04:00 +01:00
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
tinyusb_config_cdcacm_t amc_cfg = {
2021-11-30 04:38:57 +00:00
.usb_dev = CDC_USB_DEV,
2021-10-14 09:04:00 +01:00
.cdc_port = TINYUSB_CDC_ACM_0,
.rx_unread_buf_sz = 64,
.callback_rx = &usb_cdc_rx_callback,
.callback_rx_wanted_char = NULL,
.callback_line_state_changed = &usb_cdc_line_state_changed_callback,
.callback_line_coding_changed = &usb_cdc_line_coding_changed_callback};
ESP_ERROR_CHECK(tusb_cdc_acm_init(&amc_cfg));
2021-11-22 11:23:22 +00:00
ESP_LOGI(TAG, "init done");
2021-10-14 09:04:00 +01:00
}