Skip to content

Commit

Permalink
Update CDC to allow buttonless flash
Browse files Browse the repository at this point in the history
  • Loading branch information
chegewara committed Jul 29, 2021
1 parent 990f663 commit ca4e924
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
10 changes: 4 additions & 6 deletions examples/cdc/cdc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class MyUSBCallbacks : public CDCCallbacks {
{
int bitrate = USBSerial.getBitrate();
Serial.printf("new bitrate: %d\n", bitrate);
Serial1.updateBaudRate(bitrate);
}

bool onConnect(bool dtr, bool rts)
Expand All @@ -28,7 +27,7 @@ class MyUSBCallbacks : public CDCCallbacks {
Serial.printf("\nnew data, len %d\n", len);
uint8_t buf[len] = {};
USBSerial.read(buf, len);
Serial1.write(buf, len);
Serial.write(buf, len);
}

void onWantedChar(char c)
Expand All @@ -41,7 +40,6 @@ class MyUSBCallbacks : public CDCCallbacks {
void setup()
{
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, 2, 3);
USBSerial.setCallbacks(new MyUSBCallbacks());
USBSerial.setWantedChar('x');

Expand All @@ -52,11 +50,11 @@ void setup()

void loop()
{
while (Serial1.available())
while (Serial.available())
{
int len = Serial1.available();
int len = Serial.available();
char buf1[len];
Serial1.read(buf1, len);
Serial.read(buf1, len);
int a = USBSerial.write((uint8_t*)buf1, len);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/classes/cdc/cdcusb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#if CFG_TUD_CDC

#define EPNUM_CDC 0x02
#define EPNUM_CDC 0x03

static CDCusb* _CDCusb[2] = {};
enum { CDC_LINE_IDLE, CDC_LINE_1, CDC_LINE_2, CDC_LINE_3 };
Expand All @@ -25,7 +25,7 @@ void CDCusb::setBaseEP(uint8_t ep)
bool CDCusb::begin(char* str)
{
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
uint8_t cdc[TUD_CDC_DESC_LEN] = {TUD_CDC_DESCRIPTOR(ifIdx, 4, (uint8_t)(0x80 | (_EPNUM_CDC - 1)), 8, (uint8_t)_EPNUM_CDC, (uint8_t)(0x80 | _EPNUM_CDC), 64)};
uint8_t cdc[TUD_CDC_DESC_LEN] = {TUD_CDC_DESCRIPTOR(ifIdx, 4, (uint8_t)(0x80 | (_EPNUM_CDC + 2)), 8, (uint8_t)_EPNUM_CDC, (uint8_t)(0x81 + _EPNUM_CDC), 64)};
memcpy(&desc_configuration[total], cdc, sizeof(cdc));
total += sizeof(cdc);
ifIdx += 2;
Expand Down Expand Up @@ -202,6 +202,7 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
if (reset)
{
_CDCusb[itf]->persistentReset(RESTART_BOOTLOADER);
// esp_restart();
}
} else {
lineState = CDC_LINE_IDLE;
Expand All @@ -211,6 +212,9 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)

void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding)
{
if(p_line_coding->bit_rate == 1200){
esp_restart();
}
memcpy(&_CDCusb[itf]->coding, p_line_coding, sizeof(cdc_line_coding_t));
if(_CDCusb[itf]->m_callbacks)
_CDCusb[itf]->m_callbacks->onCodingChange(p_line_coding);
Expand Down
52 changes: 44 additions & 8 deletions src/esptinyusb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ uint16_t EspTinyUSB::_revision;
uint16_t EspTinyUSB::_bcdUSB;
size_t EspTinyUSB::hid_report_desc_len = 0;
USBCallbacks* EspTinyUSB::m_callbacks;
static void IRAM_ATTR usb_persist_shutdown_handler(void);

static void esptinyusbtask(void *p)
{
Expand All @@ -65,8 +66,12 @@ EspTinyUSB::EspTinyUSB(bool extPhy)
REG_CLR_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_USB_RESET_DISABLE);
}

periph_module_reset((periph_module_t)PERIPH_USB_MODULE);
periph_module_enable((periph_module_t)PERIPH_USB_MODULE);
if(1){
// Reset USB module
periph_module_reset((periph_module_t)PERIPH_USB_MODULE);
periph_module_enable((periph_module_t)PERIPH_USB_MODULE);
}


// Hal init
usb_hal_context_t hal = {
Expand Down Expand Up @@ -112,6 +117,7 @@ EspTinyUSB::EspTinyUSB(bool extPhy)
strings.vendor = _vendor;

_bcdUSB = 0x200;
esp_register_shutdown_handler(usb_persist_shutdown_handler);
}
}

Expand Down Expand Up @@ -181,9 +187,9 @@ bool EspTinyUSB::begin(char* str, uint8_t n)
return true;
}

if(tusb_init()) {
ESP_LOGW("TAG", "failed to init, (return fixed in tinyusb 0.8.0)");
// return false;
if(!tusb_init()) {
ESP_LOGE("TAG", "failed to init");
return false;
}

if (usbTaskHandle != nullptr) {
Expand Down Expand Up @@ -221,18 +227,48 @@ void EspTinyUSB::useMSC(bool en)
enableMSC = en;
}

void EspTinyUSB::persistentReset(restart_type_t usb_persist_mode)
int usb_persist_mode = RESTART_BOOTLOADER;
void EspTinyUSB::persistentReset(restart_type_t _usb_persist_mode)
{
usb_persist_mode = _usb_persist_mode;
if(usb_persist_mode != RESTART_NO_PERSIST){
if (usb_persist_mode == RESTART_BOOTLOADER) {
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
esp_restart();
} else if (usb_persist_mode == RESTART_BOOTLOADER_DFU) {
//DFU Download
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
esp_restart();
} else {
//USB Persist reboot
// chip_usb_set_persist_flags(USBDC_PERSIST_ENA);
}
}
}

static void IRAM_ATTR usb_persist_shutdown_handler(void)
{
if(usb_persist_mode != RESTART_NO_PERSIST){
int usb_persist_enabled = 0;
if (usb_persist_enabled) {
usb_dc_prepare_persist();
}
if (usb_persist_mode == RESTART_BOOTLOADER) {
//USB CDC Download
if (usb_persist_enabled) {
chip_usb_set_persist_flags(USBDC_PERSIST_ENA);
} else {
periph_module_reset(PERIPH_USB_MODULE);
periph_module_enable(PERIPH_USB_MODULE);
}
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
} else if (usb_persist_mode == RESTART_BOOTLOADER_DFU) {
//DFU Download
// Reset USB Core
USB0.grstctl |= USB_CSFTRST;
while ((USB0.grstctl & USB_CSFTRST) == USB_CSFTRST){}
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
} else if (usb_persist_enabled) {
//USB Persist reboot
chip_usb_set_persist_flags(USBDC_PERSIST_ENA);
}
Expand Down

0 comments on commit ca4e924

Please sign in to comment.