diff --git a/boards.txt b/boards.txt index 059165e3..d0ef8b13 100644 --- a/boards.txt +++ b/boards.txt @@ -37,12 +37,13 @@ giga.upload.vid=0x2341 giga.upload.pid=0x0366 giga.upload.interface=0 giga.upload.use_1200bps_touch=true -giga.upload.wait_for_upload_port=true +giga.upload.wait_for_upload_port=false giga.upload.native_usb=true giga.upload.maximum_size=1966080 giga.upload.maximum_data_size=523624 giga.upload.address=0x080E0000 +giga.upload.wait_for_device=-w giga.upload.maximum_size=786432 giga.upload.maximum_data_size=523624 @@ -268,12 +269,13 @@ portentah7.upload.vid=0x2341 portentah7.upload.pid=0x035b portentah7.upload.interface=0 portentah7.upload.use_1200bps_touch=true -portentah7.upload.wait_for_upload_port=true +portentah7.upload.wait_for_upload_port=false portentah7.upload.native_usb=true portentah7.upload.maximum_size=1966080 portentah7.upload.maximum_data_size=523624 portentah7.upload.address=0x080E0000 +portentah7.upload.wait_for_device=-w portentah7.upload.maximum_size=786432 portentah7.upload.maximum_data_size=523624 @@ -464,7 +466,7 @@ portentac33.upload.native_usb=true portentac33.upload.maximum_size=1966080 portentac33.upload.maximum_data_size=523624 portentac33.upload.address=0x100000 -portentac33.upload.dfuse=-Q +portentac33.upload.dfuse=-Q -w portentac33.bootloader.tool=dfu-util portentac33.bootloader.tool.default=dfu-util diff --git a/cores/arduino/SerialUSB.h b/cores/arduino/SerialUSB.h index 94d5cc83..b0c1ae06 100644 --- a/cores/arduino/SerialUSB.h +++ b/cores/arduino/SerialUSB.h @@ -8,6 +8,11 @@ #include +#if defined(CONFIG_USB_DEVICE_STACK_NEXT) +#include +extern "C" struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb); +#endif + namespace arduino { class SerialUSB_ : public ZephyrSerial { @@ -22,12 +27,18 @@ class SerialUSB_ : public ZephyrSerial { protected: uint32_t dtr = 0; uint32_t baudrate; - void _baudChangeHandler(); static void _baudChangeDispatch(struct k_timer *timer); + static void _baudChangeHandler(const struct device *dev, uint32_t rate); private: - struct k_timer baud_timer; bool started = false; + +#if defined(CONFIG_USB_DEVICE_STACK_NEXT) + struct usbd_context *_usbd; + int enable_usb_device_next(); + static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg); + static int usb_disable(); +#endif }; } // namespace arduino diff --git a/cores/arduino/USB.cpp b/cores/arduino/USB.cpp index 6873c798..0afb4e76 100644 --- a/cores/arduino/USB.cpp +++ b/cores/arduino/USB.cpp @@ -23,61 +23,46 @@ void __attribute__((weak)) _on_1200_bps() { NVIC_SystemReset(); } -void arduino::SerialUSB_::_baudChangeHandler() -{ - uart_line_ctrl_get(uart, UART_LINE_CTRL_BAUD_RATE, &baudrate); - if (baudrate == 1200) { - usb_disable(); - _on_1200_bps(); - } -} - -static void _baudChangeHandler(const struct device *dev, uint32_t rate) +void arduino::SerialUSB_::_baudChangeHandler(const struct device *dev, uint32_t rate) { if (rate == 1200) { + k_sleep(K_MSEC(100)); usb_disable(); + k_sleep(K_MSEC(10)); _on_1200_bps(); } } #if defined(CONFIG_USB_DEVICE_STACK_NEXT) -extern "C" { - #include - struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb); +int arduino::SerialUSB_::usb_disable() { + return usbd_disable(Serial._usbd); } -struct usbd_context *_usbd; - -int usb_disable() { - return usbd_disable(_usbd); -} - -static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) +void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) { - if (usbd_can_detect_vbus(ctx)) { - if (msg->type == USBD_MSG_VBUS_READY) { - usbd_enable(ctx); - } - - if (msg->type == USBD_MSG_VBUS_REMOVED) { - usbd_disable(ctx); - } - } + if (usbd_can_detect_vbus(ctx)) { + if (msg->type == USBD_MSG_VBUS_READY) { + usbd_enable(ctx); + } + + if (msg->type == USBD_MSG_VBUS_REMOVED) { + usbd_disable(ctx); + } + } - if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) { + if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) { uint32_t baudrate; - uart_line_ctrl_get(ctx->dev, UART_LINE_CTRL_BAUD_RATE, &baudrate); - _baudChangeHandler(nullptr, baudrate); - } + uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate); + Serial._baudChangeHandler(nullptr, baudrate); + } } -static int enable_usb_device_next(void) +int arduino::SerialUSB_::enable_usb_device_next(void) { int err; - //_usbd = usbd_init_device(usbd_next_cb); - _usbd = usbd_init_device(nullptr); + _usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb); if (_usbd == NULL) { return -ENODEV; } @@ -92,22 +77,14 @@ static int enable_usb_device_next(void) } #endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */ -void arduino::SerialUSB_::_baudChangeDispatch(struct k_timer *timer) { - arduino::SerialUSB_* dev = (arduino::SerialUSB_*)k_timer_user_data_get(timer); - dev->_baudChangeHandler(); -} - - void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) { if (!started) { #ifndef CONFIG_USB_DEVICE_STACK_NEXT usb_enable(NULL); #ifndef CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT - k_timer_init(&baud_timer, SerialUSB_::_baudChangeDispatch, NULL); - k_timer_user_data_set(&baud_timer, this); - k_timer_start(&baud_timer, K_MSEC(100), K_MSEC(100)); + #warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT" #else - cdc_acm_dte_rate_callback_set(usb_dev, ::_baudChangeHandler); + cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::_baudChangeHandler); #endif #else enable_usb_device_next(); diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index e1af8610..12e466c9 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -23,7 +23,7 @@ void __attribute__((weak))initVariant(void) { int main(void) { -#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM) +#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS)) Serial.begin(115200); #endif diff --git a/platform.txt b/platform.txt index e6a2af0f..4a584db6 100644 --- a/platform.txt +++ b/platform.txt @@ -200,12 +200,13 @@ tools.stm32flash.erase.pattern="{path}/{cmd}" {serial.port} -e 1024 -b 2400 upload.dfuse=--dfuse-address={upload.address}:leave bootloader.dfuse=--dfuse-address={bootloader.address}:leave +upload.wait_for_device= tools.dfu-util.path={runtime.tools.dfu-util.path} tools.dfu-util.cmd=dfu-util tools.dfu-util.upload.params.verbose=-d tools.dfu-util.upload.params.quiet= -tools.dfu-util.upload.pattern="{path}/{cmd}" --device ,{upload.vid}:{upload.pid} -D "{build.path}/{build.project_name}.{upload.extension}" -a{upload.interface} {upload.dfuse} +tools.dfu-util.upload.pattern="{path}/{cmd}" --device ,{upload.vid}:{upload.pid} -D "{build.path}/{build.project_name}.{upload.extension}" -a{upload.interface} {upload.dfuse} {upload.wait_for_device} tools.dfu-util.bootloader.params.verbose=-d tools.dfu-util.bootloader.params.quiet=