Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 79ca674

Browse files
facchinmpennam
authored andcommitted
usb: refactor USBD_NEXT and implement 1200bps touch
1 parent fa7f1ec commit 79ca674

File tree

5 files changed

+52
-45
lines changed

5 files changed

+52
-45
lines changed

‎cores/arduino/SerialUSB.h‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#include <zephyrSerial.h>
1010

11+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
12+
#include <zephyr/usb/usbd.h>
13+
extern "C" struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
14+
#endif
15+
1116
namespace arduino {
1217

1318
class SerialUSB_ : public ZephyrSerial {
@@ -34,12 +39,17 @@ class SerialUSB_ : public ZephyrSerial {
3439
protected:
3540
uint32_t dtr = 0;
3641
uint32_t baudrate;
37-
void _baudChangeHandler();
38-
static void _baudChangeDispatch(struct k_timer *timer);
42+
static void baudChangeHandler(const struct device *dev, uint32_t rate);
3943

4044
private:
41-
struct k_timer baud_timer;
4245
bool started = false;
46+
47+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
48+
struct usbd_context *_usbd;
49+
int enable_usb_device_next();
50+
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg);
51+
static int usb_disable();
52+
#endif
4353
};
4454
} // namespace arduino
4555

‎cores/arduino/USB.cpp‎

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,11 @@
1515
const struct device *const usb_dev =
1616
DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
1717

18-
void usb_status_cb(enum usb_dc_status_code cb_status, const uint8_t *param) {
19-
(void)param; // unused
20-
if (cb_status == USB_DC_CONFIGURED) {
21-
}
22-
}
23-
2418
void __attribute__((weak)) _on_1200_bps() {
2519
NVIC_SystemReset();
2620
}
2721

28-
void arduino::SerialUSB_::_baudChangeHandler() {
29-
uart_line_ctrl_get(uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
30-
if (baudrate == 1200) {
31-
usb_disable();
32-
_on_1200_bps();
33-
}
34-
}
35-
36-
static void _baudChangeHandler(const struct device *dev, uint32_t rate) {
22+
void arduino::SerialUSB_::baudChangeHandler(const struct device *dev, uint32_t rate) {
3723
(void)dev; // unused
3824
if (rate == 1200) {
3925
usb_disable();
@@ -42,19 +28,11 @@ static void _baudChangeHandler(const struct device *dev, uint32_t rate) {
4228
}
4329

4430
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
45-
46-
extern "C" {
47-
#include <zephyr/usb/usbd.h>
48-
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
31+
int arduino::SerialUSB_::usb_disable() {
32+
return usbd_disable(Serial._usbd);
4933
}
5034

51-
struct usbd_context *_usbd;
52-
53-
int usb_disable() {
54-
return usbd_disable(_usbd);
55-
}
56-
57-
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) {
35+
void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) {
5836
if (usbd_can_detect_vbus(ctx)) {
5937
if (msg->type == USBD_MSG_VBUS_READY) {
6038
usbd_enable(ctx);
@@ -67,16 +45,15 @@ static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *
6745

6846
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
6947
uint32_t baudrate;
70-
uart_line_ctrl_get(ctx->dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
71-
_baudChangeHandler(nullptr, baudrate);
48+
uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
49+
Serial.baudChangeHandler(nullptr, baudrate);
7250
}
7351
}
7452

75-
staticint enable_usb_device_next(void) {
53+
int arduino::SerialUSB_::enable_usb_device_next(void) {
7654
int err;
7755

78-
//_usbd = usbd_init_device(usbd_next_cb);
79-
_usbd = usbd_init_device(nullptr);
56+
_usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb);
8057
if (_usbd == NULL) {
8158
return -ENODEV;
8259
}
@@ -91,21 +68,14 @@ static int enable_usb_device_next(void) {
9168
}
9269
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
9370

94-
void arduino::SerialUSB_::_baudChangeDispatch(struct k_timer *timer) {
95-
arduino::SerialUSB_ *dev = (arduino::SerialUSB_ *)k_timer_user_data_get(timer);
96-
dev->_baudChangeHandler();
97-
}
98-
9971
void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) {
10072
if (!started) {
10173
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
10274
usb_enable(NULL);
10375
#ifndef CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT
104-
k_timer_init(&baud_timer, SerialUSB_::_baudChangeDispatch, NULL);
105-
k_timer_user_data_set(&baud_timer, this);
106-
k_timer_start(&baud_timer, K_MSEC(100), K_MSEC(100));
76+
#warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT"
10777
#else
108-
cdc_acm_dte_rate_callback_set(usb_dev, ::_baudChangeHandler);
78+
cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::baudChangeHandler);
10979
#endif
11080
#else
11181
enable_usb_device_next();

‎cores/arduino/main.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ void __attribute__((weak)) initVariant(void) {
2020
}
2121

2222
int main(void) {
23-
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
23+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && \
24+
(CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
2425
Serial.begin(115200);
2526
#endif
2627

‎loader/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ project(app LANGUAGES C CXX)
1616

1717
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/blobs)
1818

19+
# for USB device stack NEXT
20+
target_sources_ifdef(CONFIG_USB_DEVICE_STACK_NEXT app PRIVATE
21+
${CMAKE_CURRENT_LIST_DIR}/../cores/arduino/usb_device_descriptor.c
22+
)
23+
1924
FILE(GLOB app_sources *.c)
2025
target_sources(app PRIVATE ${app_sources})
2126

‎loader/main.c‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,33 @@ struct sketch_header_v1 {
3333
#define SKETCH_FLAG_LINKED 0x02
3434

3535
#define TARGET_HAS_USB_CDC_SHELL \
36-
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL &&CONFIG_USB_DEVICE_STACK
36+
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL && \
37+
(CONFIG_USB_DEVICE_STACK || CONFIG_USB_DEVICE_STACK_NEXT)
3738

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

43+
#if CONFIG_USB_DEVICE_STACK_NEXT
44+
#include <zephyr/usb/usbd.h>
45+
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
46+
47+
int usb_enable(usb_dc_status_callback status_cb) {
48+
int err;
49+
struct usbd_context *_usbd = usbd_init_device(NULL);
50+
if (_usbd == NULL) {
51+
return -ENODEV;
52+
}
53+
if (!usbd_can_detect_vbus(_usbd)) {
54+
err = usbd_enable(_usbd);
55+
if (err) {
56+
return err;
57+
}
58+
}
59+
return 0;
60+
}
61+
#endif
62+
4263
static int enable_shell_usb(void) {
4364
bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0;
4465
uint32_t level = (CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > LOG_LEVEL_DBG) ?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /