Skip to content

Commit

Permalink
Merge pull request #356 from adafruit/remove-hid-add-cdc
Browse files Browse the repository at this point in the history
Remove hid add cdc
  • Loading branch information
hathach authored Nov 13, 2023
2 parents 820bc84 + 0342999 commit 80930ba
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 215 deletions.
141 changes: 120 additions & 21 deletions ports/espressif/boards/boards.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"

#include "hal/gpio_ll.h"
#include "hal/usb_hal.h"
Expand Down Expand Up @@ -69,8 +70,7 @@ void dotstar_write(uint8_t const rgb[]);

#ifdef LED_PIN
#include "driver/ledc.h"
ledc_channel_config_t ledc_channel =
{
ledc_channel_config_t ledc_channel = {
.channel = LEDC_CHANNEL_0,
.duty = 0,
.gpio_num = LED_PIN,
Expand All @@ -89,9 +89,7 @@ extern bool board_init_extension();
#endif

extern int main(void);

static void configure_pins(usb_hal_context_t* usb);

static void internal_timer_cb(void* arg);

//--------------------------------------------------------------------+
Expand All @@ -102,7 +100,6 @@ static void internal_timer_cb(void* arg);
void app_main(void) {
main();
}

#else

// static task for usbd
Expand Down Expand Up @@ -341,6 +338,116 @@ void board_timer_stop(void) {
esp_timer_stop(timer_hdl);
}

//--------------------------------------------------------------------+
// CDC Touch1200
//--------------------------------------------------------------------+
#ifndef TINYUF2_SELF_UPDATE

#if CONFIG_IDF_TARGET_ESP32S3
#include "hal/usb_serial_jtag_ll.h"
#include "hal/usb_phy_ll.h"

static void hw_cdc_reset_handler(void *arg) {
portBASE_TYPE xTaskWoken = 0;
uint32_t usbjtag_intr_status = usb_serial_jtag_ll_get_intsts_mask();
usb_serial_jtag_ll_clr_intsts_mask(usbjtag_intr_status);

if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_BUS_RESET) {
xSemaphoreGiveFromISR((SemaphoreHandle_t)arg, &xTaskWoken);
}

if (xTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}

static void usb_switch_to_cdc_jtag(void) {
// Disable USB-OTG
periph_module_reset(PERIPH_USB_MODULE);
periph_module_disable(PERIPH_USB_MODULE);

// Switch to hardware CDC+JTAG
CLEAR_PERI_REG_MASK(RTC_CNTL_USB_CONF_REG,
(RTC_CNTL_SW_HW_USB_PHY_SEL | RTC_CNTL_SW_USB_PHY_SEL | RTC_CNTL_USB_PAD_ENABLE));

// Do not use external PHY
CLEAR_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_PHY_SEL);

// Release GPIO pins from CDC+JTAG
CLEAR_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_USB_PAD_ENABLE);

// Force the host to re-enumerate (BUS_RESET)
// pinMode(USBPHY_DM_NUM, OUTPUT_OPEN_DRAIN);
// pinMode(USBPHY_DP_NUM, OUTPUT_OPEN_DRAIN);
// digitalWrite(USBPHY_DM_NUM, LOW);
// digitalWrite(USBPHY_DP_NUM, LOW);

// Initialize CDC+JTAG ISR to listen for BUS_RESET
usb_phy_ll_int_jtag_enable(&USB_SERIAL_JTAG);
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_BUS_RESET);
intr_handle_t intr_handle = NULL;
SemaphoreHandle_t reset_sem = xSemaphoreCreateBinary();
if (reset_sem) {
if (esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, hw_cdc_reset_handler, reset_sem, &intr_handle) != ESP_OK) {
vSemaphoreDelete(reset_sem);
reset_sem = NULL;
//log_e("HW USB CDC failed to init interrupts");
}
} else {
//log_e("reset_sem init failed");
}

// Connect GPIOs to integrated CDC+JTAG
SET_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_USB_PAD_ENABLE);

// Wait for BUS_RESET to give us back the semaphore
if (reset_sem) {
if (xSemaphoreTake(reset_sem, 1000 / portTICK_PERIOD_MS) != pdPASS) {
//log_e("reset_sem timeout");
}
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
esp_intr_free(intr_handle);
vSemaphoreDelete(reset_sem);
}
}
#endif

// Copied from Arduino's esp32-hal-tinyusb.c with usb_persist_mode = RESTART_BOOTLOADER, and usb_persist_enabled = false
static void IRAM_ATTR usb_persist_shutdown_handler(void) {
// USB CDC Download: RESTART_BOOTLOADER
#if CONFIG_IDF_TARGET_ESP32S2
periph_module_reset(PERIPH_USB_MODULE);
periph_module_enable(PERIPH_USB_MODULE);
#endif

REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
}

// Invoked when cdc when line state changed e.g connected/disconnected
// Use to reset to bootrom when disconnect with 1200 bps
void tud_cdc_line_state_cb(uint8_t instance, bool dtr, bool rts) {
(void) rts;

// DTR = false is counted as disconnected
if (!dtr) {
cdc_line_coding_t coding;
tud_cdc_get_line_coding(&coding);

if (coding.bit_rate == 1200) {
printf("Touch1200: Reset to bootrom\n");
// copied from Arduino's usb_persist_restart()
esp_register_shutdown_handler(usb_persist_shutdown_handler);
#if CONFIG_IDF_TARGET_ESP32S3
usb_switch_to_cdc_jtag();
#endif
esp_restart();
}
}
}
#endif // TINYUF2_SELF_UPDATE

//--------------------------------------------------------------------+
// Display
//--------------------------------------------------------------------+
Expand All @@ -351,8 +458,7 @@ void board_timer_stop(void) {

spi_device_handle_t _display_spi;

void board_display_init(void)
{
void board_display_init(void) {
spi_bus_config_t bus_cfg = {
.miso_io_num = DISPLAY_PIN_MISO,
.mosi_io_num = DISPLAY_PIN_MOSI,
Expand Down Expand Up @@ -380,8 +486,7 @@ void board_display_init(void)
ESP_ERROR_CHECK(lcd_init(_display_spi));
}

void board_display_draw_line(int y, uint16_t* pixel_color, uint32_t pixel_num)
{
void board_display_draw_line(int y, uint16_t* pixel_color, uint32_t pixel_num) {
(void) pixel_num; // same as DISPLAY_HEIGHT
lcd_draw_lines(_display_spi, y, (uint16_t*) pixel_color);
}
Expand All @@ -398,16 +503,14 @@ void board_display_draw_line(int y, uint16_t* pixel_color, uint32_t pixel_num)
spi_device_handle_t _dotstar_spi;
uint8_t _dotstar_data[ (1+DOTSTAR_NUMBER+1) * 4];

void dotstar_init(void)
{
void dotstar_init(void) {
#ifdef DOTSTAR_PIN_PWR
gpio_reset_pin(DOTSTAR_PIN_PWR);
gpio_set_direction(DOTSTAR_PIN_PWR, GPIO_MODE_OUTPUT);
gpio_set_level(DOTSTAR_PIN_PWR, DOTSTAR_POWER_STATE);
#endif

spi_bus_config_t bus_cfg =
{
spi_bus_config_t bus_cfg = {
.miso_io_num = -1,
.mosi_io_num = DOTSTAR_PIN_DATA,
.sclk_io_num = DOTSTAR_PIN_SCK,
Expand All @@ -416,8 +519,7 @@ void dotstar_init(void)
.max_transfer_sz = sizeof(_dotstar_data)
};

spi_device_interface_config_t devcfg =
{
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 10 * 1000000, // 10 Mhz
.mode = 0,
.spics_io_num = -1,
Expand All @@ -431,8 +533,7 @@ void dotstar_init(void)
ESP_ERROR_CHECK(spi_bus_add_device(DOTSTAR_SPI, &devcfg, &_dotstar_spi));
}

void dotstar_write(uint8_t const rgb[])
{
void dotstar_write(uint8_t const rgb[]) {
// convert from 0-255 (8 bit) to 0-31 (5 bit)
uint8_t const ds_brightness = (DOTSTAR_BRIGHTNESS * 32) / 256;

Expand All @@ -441,8 +542,7 @@ void dotstar_write(uint8_t const rgb[])

uint8_t* color = _dotstar_data+4;

for(uint8_t i=0; i<DOTSTAR_NUMBER; i++)
{
for(uint8_t i=0; i<DOTSTAR_NUMBER; i++) {
*color++ = 0xE0 | ds_brightness;
*color++ = rgb[2];
*color++ = rgb[1];
Expand All @@ -462,8 +562,7 @@ void dotstar_write(uint8_t const rgb[])
*color++ = 0xff;
*color++ = 0xff;

static spi_transaction_t xact =
{
static spi_transaction_t xact = {
.length = (sizeof(_dotstar_data) - 4 + (DOTSTAR_NUMBER+15)/16 )*8, // length in bits, see end frame not above
.tx_buffer = _dotstar_data
};
Expand Down
21 changes: 19 additions & 2 deletions ports/espressif/components/tinyusb_src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,35 @@
#endif

//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_CDC 1
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

//------------- CDC -------------//

// force CDC endpoint number same as bootrom cdc mode
#define BOARD_EPNUM_CDC_NOTIF 0x85
#define BOARD_EPNUM_CDC_OUT 0x03
#define BOARD_EPNUM_CDC_IN 0x84

// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE 512
#define CFG_TUD_CDC_TX_BUFSIZE 512

// CDC Endpoint transfer buffer size, more is faster
#define CFG_TUD_CDC_EP_BUFSIZE 64

//------------- MSC -------------//
// MSC Buffer size of Device Mass storage
#define CFG_TUD_MSC_BUFSIZE 4096

//------------- HID -------------//
// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_BUFSIZE 64

//------------- Vendor -------------//
// Vendor FIFO size of TX and RX
// If not configured vendor endpoints will not be buffered
#define CFG_TUD_VENDOR_RX_BUFSIZE 64
Expand Down
2 changes: 1 addition & 1 deletion ports/k32l2/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

Expand Down
2 changes: 1 addition & 1 deletion ports/lpc55/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

Expand Down
2 changes: 1 addition & 1 deletion ports/mimxrt10xx/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
//------------- CLASS -------------//
// #define CFG_TUD_CDC 0
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
// #define CFG_TUD_VENDOR 0

// MSC Buffer size of Device Mass storage
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32f4/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

Expand Down
2 changes: 1 addition & 1 deletion ports/stm32h7/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

Expand Down
2 changes: 1 addition & 1 deletion ports/stm32l4/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

Expand Down
2 changes: 1 addition & 1 deletion ports/template_port/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

Expand Down
Loading

0 comments on commit 80930ba

Please sign in to comment.