Skip to content

Fix Sketch upload on Portenta C33 #183

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,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 @@ -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
69 changes: 23 additions & 46 deletions cores/arduino/USB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,47 @@ 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);
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 +79,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
11 changes: 11 additions & 0 deletions loader/fixups.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ 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)
int enable_bkp_access(void)
{
/* Enable access to the backup domain */
// HAL_PWR_EnableBkUpAccess();
SET_BIT(PWR->CR1, PWR_CR1_DBP);
return 0;
}
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
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 @@
#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)

Check failure on line 36 in loader/main.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 132 exceeds 100 columns

#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) {

Check failure on line 48 in loader/main.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: braces {} are not necessary for single statement blocks

Check failure on line 48 in loader/main.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing a blank line after declarations
return -ENODEV;
}
if (!usbd_can_detect_vbus(_usbd)) {
err = usbd_enable(_usbd);
if (err) {

Check failure on line 53 in loader/main.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: braces {} are not necessary for single statement blocks
return err;
}
}
return 0;
}
#endif

static int enable_shell_usb(void)
{
bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ CONFIG_CYW4343W_MURATA_1DX=y
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_BT_RX_STACK_SIZE=4096
CONFIG_BT_HCI_TX_STACK_SIZE=4096

CONFIG_STM32H7_BOOT_M4_AT_INIT=n
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=7168
CONFIG_MBEDTLS_HASH_ALL_ENABLED=y
CONFIG_MBEDTLS_CMAC=y

CONFIG_STM32H7_BOOT_M4_AT_INIT=n
Loading