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 6 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
43 changes: 15 additions & 28 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ giga.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/

giga.menu.debug.false=Standard
giga.menu.debug.true=Debug

giga.menu.debug.false.postbuild_debug=
giga.menu.debug.true.postbuild_debug=-debug
giga.menu.debug.true.build.zsk_args.debug=-debug

giga.build.zephyr_target=arduino_giga_r1//m7
giga.build.zephyr_args=--shield arduino_giga_display_shield
Expand Down Expand Up @@ -40,12 +38,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 @@ -74,9 +73,7 @@ nano33ble.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/

nano33ble.menu.debug.false=Standard
nano33ble.menu.debug.true=Debug

nano33ble.menu.debug.false.postbuild_debug=
nano33ble.menu.debug.true.postbuild_debug=-debug
nano33ble.menu.debug.true.build.zsk_args.debug=-debug

nano33ble.build.zephyr_target=arduino_nano_33_ble//sense
nano33ble.build.zephyr_args=
Expand Down Expand Up @@ -136,9 +133,7 @@ ek_ra8d1.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/

ek_ra8d1.menu.debug.false=Standard
ek_ra8d1.menu.debug.true=Debug

ek_ra8d1.menu.debug.false.postbuild_debug=
ek_ra8d1.menu.debug.true.postbuild_debug=-debug
ek_ra8d1.menu.debug.true.build.zsk_args.debug=-debug

ek_ra8d1.build.zephyr_target=ek_ra8d1
ek_ra8d1.build.zephyr_args=
Expand Down Expand Up @@ -192,9 +187,7 @@ frdm_mcxn947.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin

frdm_mcxn947.menu.debug.false=Standard
frdm_mcxn947.menu.debug.true=Debug

frdm_mcxn947.menu.debug.false.postbuild_debug=
frdm_mcxn947.menu.debug.true.postbuild_debug=-debug
frdm_mcxn947.menu.debug.true.build.zsk_args.debug=-debug

frdm_mcxn947.build.zephyr_target=frdm_mcxn947//cpu0
frdm_mcxn947.build.zephyr_args=
Expand Down Expand Up @@ -243,9 +236,7 @@ portentah7.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/

portentah7.menu.debug.false=Standard
portentah7.menu.debug.true=Debug

portentah7.menu.debug.false.postbuild_debug=
portentah7.menu.debug.true.postbuild_debug=-debug
portentah7.menu.debug.true.build.zsk_args.debug=-debug

portentah7.build.zephyr_target=arduino_portenta_h7@1.0.0//m7
portentah7.build.zephyr_args=
Expand Down Expand Up @@ -273,12 +264,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 @@ -307,9 +299,7 @@ frdm_rw612.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/

frdm_rw612.menu.debug.false=Standard
frdm_rw612.menu.debug.true=Debug

frdm_rw612.menu.debug.false.postbuild_debug=
frdm_rw612.menu.debug.true.postbuild_debug=-debug
frdm_rw612.menu.debug.true.build.zsk_args.debug=-debug

frdm_rw612.build.zephyr_target=frdm_rw612
frdm_rw612.build.zephyr_args=
Expand Down Expand Up @@ -357,9 +347,7 @@ niclasense.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/

niclasense.menu.debug.false=Standard
niclasense.menu.debug.true=Debug

niclasense.menu.debug.false.postbuild_debug=
niclasense.menu.debug.true.postbuild_debug=-debug
niclasense.menu.debug.true.build.zsk_args.debug=-debug

niclasense.build.zephyr_target=arduino_nicla_sense_me
niclasense.build.zephyr_args=
Expand Down Expand Up @@ -463,7 +451,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 All @@ -481,9 +469,7 @@ opta.build.compiler_path={runtime.tools.arm-zephyr-eabi-0.16.8.path}/bin/

opta.menu.debug.false=Standard
opta.menu.debug.true=Debug

opta.menu.debug.false.postbuild_debug=
opta.menu.debug.true.postbuild_debug=-debug
opta.menu.debug.true.build.zsk_args.debug=-debug

opta.build.zephyr_target=arduino_opta//m7
opta.build.zephyr_args=
Expand Down Expand Up @@ -511,12 +497,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 @@
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();

Check failure on line 41 in cores/arduino/SerialUSB.h

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: Bad function definition - int enable_usb_device_next() should probably be int enable_usb_device_next(void)

Check failure on line 41 in cores/arduino/SerialUSB.h

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing a blank line after declarations
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg);
static int usb_disable();

Check failure on line 43 in cores/arduino/SerialUSB.h

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: Bad function definition - int usb_disable() should probably be int usb_disable(void)
#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 @@ -243,4 +243,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
Loading
Loading