Skip to content

USB fixes #96

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

Open
wants to merge 4 commits into
base: main
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
11 changes: 7 additions & 4 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,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 @@ -273,12 +274,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 @@ -463,7 +465,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 Expand Up @@ -511,12 +513,13 @@ opta.upload.vid=0x2341
opta.upload.pid=0x0364
opta.upload.interface=0
opta.upload.use_1200bps_touch=true
opta.upload.wait_for_upload_port=true
opta.upload.wait_for_upload_port=false
opta.upload.native_usb=true
opta.upload.maximum_size=1966080
opta.upload.maximum_data_size=523624

opta.upload.address=0x080E0000
opta.upload.wait_for_device=-w

opta.upload.maximum_size=786432
opta.upload.maximum_data_size=523624
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 @@ -25,12 +30,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
70 changes: 24 additions & 46 deletions cores/arduino/USB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,48 @@ 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)
{
(void)dev; // unused
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);
printf("Baud rate changed to %d\n", 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 @@ -94,22 +80,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
5 changes: 5 additions & 0 deletions loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ project(app LANGUAGES C CXX)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/blobs)

# for USB device stack NEXT
target_sources_ifdef(CONFIG_USB_DEVICE_STACK_NEXT app PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../cores/arduino/usb_device_descriptor.c
)

FILE(GLOB app_sources *.c)
target_sources(app PRIVATE ${app_sources})

Expand Down
10 changes: 10 additions & 0 deletions loader/fixups.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ SYS_INIT(disable_bootloader_mpu, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAU
SYS_INIT(disable_mpu_rasr_xn, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
#endif

#if defined(CONFIG_SOC_STM32H747XX_M7)
void enable_bkp_access(void)
{
/* Enable access to the backup domain */
// HAL_PWR_EnableBkUpAccess();
SET_BIT(PWR->CR1, PWR_CR1_DBP);
}
SYS_INIT(enable_bkp_access, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
#endif

#if defined(CONFIG_BOARD_ARDUINO_GIGA_R1) && defined(CONFIG_VIDEO)
#include <zephyr/kernel.h>
#include <zephyr/device.h>
Expand Down
2 changes: 1 addition & 1 deletion loader/llext_exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,4 @@ FORCE_EXPORT_SYM(__aeabi_dcmpge);

#if defined (CONFIG_CPP)
FORCE_EXPORT_SYM(__cxa_pure_virtual);
#endif
#endif
22 changes: 21 additions & 1 deletion loader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,31 @@ struct sketch_header_v1 {
#define SKETCH_FLAG_LINKED 0x02

#define TARGET_HAS_USB_CDC_SHELL \
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL && CONFIG_USB_DEVICE_STACK
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL && (CONFIG_USB_DEVICE_STACK || CONFIG_USB_DEVICE_STACK_NEXT)

#if TARGET_HAS_USB_CDC_SHELL
const struct device *const usb_dev = DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));

#if CONFIG_USB_DEVICE_STACK_NEXT
#include <zephyr/usb/usbd.h>
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
int usb_enable(usb_dc_status_callback status_cb)
{
int err;
struct usbd_context *_usbd = usbd_init_device(NULL);
if (_usbd == NULL) {
return -ENODEV;
}
if (!usbd_can_detect_vbus(_usbd)) {
err = usbd_enable(_usbd);
if (err) {
return err;
}
}
return 0;
}
#endif

static int enable_shell_usb(void)
{
bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0;
Expand Down
3 changes: 2 additions & 1 deletion platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_STACK_NEXT=y
CONFIG_USB_DEVICE_PRODUCT="Arduino GIGA R1"
CONFIG_USB_DEVICE_MANUFACTURER="Arduino"
CONFIG_USB_DEVICE_VID=0x2341
CONFIG_USB_DEVICE_PID=0x0066

CONFIG_USB_CDC_ACM=y
CONFIG_USB_CDC_ACM_RINGBUF_SIZE=512
CONFIG_UART_LINE_CTRL=y
CONFIG_USBD_CDC_ACM_CLASS=y
CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT=y
CONFIG_USBD_CDC_ACM_LOG_LEVEL_ERR=y
CONFIG_UDC_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USBD_LOG_LEVEL_ERR=y
CONFIG_UART_LINE_CTRL=y

CONFIG_LLEXT_STORAGE_WRITABLE=n
CONFIG_SHARED_MULTI_HEAP=y
Expand Down Expand Up @@ -62,6 +64,9 @@ CONFIG_BT_CTLR_ADV_PERIODIC=y
CONFIG_BT_CTLR_DTM_HCI=y
CONFIG_CYW4343W_MURATA_1DX=y
CONFIG_BT_HCI_DRIVER_LOG_LEVEL_DBG=y

CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_BT_RX_STACK_SIZE=4096
CONFIG_BT_HCI_TX_STACK_SIZE=4096

CONFIG_RTC=y
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@
};
};

&rtc {
clocks = <&rcc STM32_CLOCK_BUS_APB4 0x00010000>,
<&rcc STM32_SRC_LSE RTC_SEL(1)>;
status = "okay";
};

&flash0 {
partitions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
# Host number of completed commands does not follow normal flow control.
CONFIG_BT_BUF_CMD_TX_COUNT=10


CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_STACK_NEXT=y
CONFIG_USB_DEVICE_PRODUCT="Arduino Nano 33 BLE"
CONFIG_USB_DEVICE_MANUFACTURER="Arduino"
CONFIG_USB_DEVICE_VID=0x2341
CONFIG_USB_DEVICE_PID=0x035A

CONFIG_USB_CDC_ACM=y
CONFIG_USB_CDC_ACM_RINGBUF_SIZE=512
CONFIG_UART_LINE_CTRL=y
CONFIG_USBD_CDC_ACM_CLASS=y
CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT=y
CONFIG_USBD_CDC_ACM_LOG_LEVEL_ERR=y
CONFIG_UDC_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USBD_LOG_LEVEL_ERR=y
CONFIG_UART_LINE_CTRL=y

CONFIG_ADC=y
CONFIG_PWM=y
Expand Down
4 changes: 1 addition & 3 deletions variants/arduino_nano_33_ble_nrf52840_sense/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ void initVariant(void) {
GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), pin_enable_gpios);
if (gpio_is_ready_dt(&enable_sensors)) {
gpio_flags_t flags = enable_sensors.dt_flags | GPIO_OUTPUT_HIGH | GPIO_ACTIVE_HIGH | NRF_GPIO_DRIVE_H0H1;
Serial.println((uint32_t)flags, HEX);


gpio_pin_configure(enable_sensors.port, enable_sensors.pin, flags);
gpio_pin_set(enable_sensors.port, enable_sensors.pin, HIGH);
//Serial.println("### Sensor pin enabled ###");

delay(500);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_STACK_NEXT=y
CONFIG_USB_DEVICE_PRODUCT="Arduino Opta"
CONFIG_USB_DEVICE_MANUFACTURER="Arduino"
CONFIG_USB_DEVICE_VID=0x2341
CONFIG_USB_DEVICE_PID=0x0064

CONFIG_USB_CDC_ACM=y
CONFIG_USB_CDC_ACM_RINGBUF_SIZE=512
CONFIG_UART_LINE_CTRL=y
# Get rid of old USB stack
CONFIG_BOARD_SERIAL_BACKEND_CDC_ACM=n

CONFIG_USBD_CDC_ACM_CLASS=y
CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT=y
CONFIG_USBD_CDC_ACM_LOG_LEVEL_ERR=y
CONFIG_UDC_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USBD_LOG_LEVEL_ERR=y
CONFIG_UART_LINE_CTRL=y

CONFIG_LLEXT_STORAGE_WRITABLE=n

Expand Down
Loading