Skip to content

Commit fbcbbfd

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

File tree

5 files changed

+91
-89
lines changed

5 files changed

+91
-89
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: 50 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -15,116 +15,84 @@
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) {
37-
(void)dev; // unused
38-
if (rate == 1200) {
39-
usb_disable();
40-
_on_1200_bps();
41-
}
22+
void arduino::SerialUSB_::baudChangeHandler(const struct device *dev, uint32_t rate) {
23+
(void)dev; // unused
24+
if (rate == 1200) {
25+
usb_disable();
26+
_on_1200_bps();
27+
}
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);
49-
}
50-
51-
struct usbd_context *_usbd;
52-
53-
int usb_disable() {
54-
return usbd_disable(_usbd);
31+
int arduino::SerialUSB_::usb_disable() {
32+
return usbd_disable(Serial._usbd);
5533
}
5634

57-
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) {
58-
if (usbd_can_detect_vbus(ctx)) {
59-
if (msg->type == USBD_MSG_VBUS_READY) {
60-
usbd_enable(ctx);
61-
}
62-
63-
if (msg->type == USBD_MSG_VBUS_REMOVED) {
64-
usbd_disable(ctx);
65-
}
66-
}
67-
68-
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
69-
uint32_t baudrate;
70-
uart_line_ctrl_get(ctx->dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
71-
_baudChangeHandler(nullptr, baudrate);
72-
}
35+
void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) {
36+
if (usbd_can_detect_vbus(ctx)) {
37+
if (msg->type == USBD_MSG_VBUS_READY) {
38+
usbd_enable(ctx);
39+
}
40+
41+
if (msg->type == USBD_MSG_VBUS_REMOVED) {
42+
usbd_disable(ctx);
43+
}
44+
}
45+
46+
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
47+
uint32_t baudrate;
48+
uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
49+
Serial.baudChangeHandler(nullptr, baudrate);
50+
}
7351
}
7452

75-
static int enable_usb_device_next(void) {
76-
int err;
77-
78-
//_usbd = usbd_init_device(usbd_next_cb);
79-
_usbd = usbd_init_device(nullptr);
80-
if (_usbd == NULL) {
81-
return -ENODEV;
82-
}
83-
84-
if (!usbd_can_detect_vbus(_usbd)) {
85-
err = usbd_enable(_usbd);
86-
if (err) {
87-
return err;
88-
}
89-
}
90-
return 0;
53+
int arduino::SerialUSB_::enable_usb_device_next(void) {
54+
int err;
55+
56+
_usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb);
57+
if (_usbd == NULL) {
58+
return -ENODEV;
59+
}
60+
61+
if (!usbd_can_detect_vbus(_usbd)) {
62+
err = usbd_enable(_usbd);
63+
if (err) {
64+
return err;
65+
}
66+
}
67+
return 0;
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) {
100-
if (!started) {
72+
if (!started) {
10173
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
102-
usb_enable(NULL);
74+
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
111-
enable_usb_device_next();
81+
enable_usb_device_next();
11282
#endif
113-
ZephyrSerial::begin(baudrate, config);
114-
started = true;
115-
}
83+
ZephyrSerial::begin(baudrate, config);
84+
started = true;
85+
}
11686
}
11787

11888
arduino::SerialUSB_::operator bool() {
11989
uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &dtr);
12090
return dtr;
12191
}
12292

123-
size_t arduino::SerialUSB_::write(const uint8_t *buffer, size_t size) {
124-
if (!Serial) {
125-
return 0;
126-
}
127-
return arduino::ZephyrSerial::write(buffer, size);
93+
size_t arduino::SerialUSB_::write(const uint8_t *buffer, size_t size) {
94+
if (!Serial) return 0;
95+
return arduino::ZephyrSerial::write(buffer, size);
12896
}
12997

13098
void arduino::SerialUSB_::flush() {

cores/arduino/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +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)
24-
Serial.begin(115200);
23+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
24+
Serial.begin(115200);
2525
#endif
2626

2727
initVariant();

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: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,32 @@ struct sketch_header_v1 {
3232
#define SKETCH_FLAG_DEBUG 0x01
3333
#define SKETCH_FLAG_LINKED 0x02
3434

35-
#define TARGET_HAS_USB_CDC_SHELL \
36-
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL &&CONFIG_USB_DEVICE_STACK
35+
#define TARGET_HAS_USB_CDC_SHELL \
36+
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL && (CONFIG_USB_DEVICE_STACK || CONFIG_USB_DEVICE_STACK_NEXT)
3737

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

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

0 commit comments

Comments
 (0)