Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB fixes #96

Open
wants to merge 2 commits into
base: arduino
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions cores/arduino/SerialUSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

#include <zephyrSerial.h>

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
#include <zephyr/usb/usbd.h>
extern "C" struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
#endif

namespace arduino {

class SerialUSB_ : public ZephyrSerial {
Expand All @@ -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

Expand Down
69 changes: 23 additions & 46 deletions cores/arduino/USB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <zephyr/usb/usbd.h>
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;
}
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down