Skip to content

Commit daeed2a

Browse files
facchinmpennam
authored andcommitted
usb: refactor USBD_NEXT and implement 1200bps touch
1 parent 0d80138 commit daeed2a

File tree

5 files changed

+79
-81
lines changed

5 files changed

+79
-81
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 {
@@ -25,12 +30,17 @@ class SerialUSB_ : public ZephyrSerial {
2530
protected:
2631
uint32_t dtr = 0;
2732
uint32_t baudrate;
28-
void _baudChangeHandler();
29-
static void _baudChangeDispatch(struct k_timer *timer);
33+
static void baudChangeHandler(const struct device *dev, uint32_t rate);
3034

3135
private:
32-
struct k_timer baud_timer;
3336
bool started = false;
37+
38+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
39+
struct usbd_context *_usbd;
40+
int enable_usb_device_next();
41+
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg);
42+
static int usb_disable();
43+
#endif
3444
};
3545
} // namespace arduino
3646

cores/arduino/USB.cpp

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,11 @@
1313
#if ((DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)) && (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
1414
const struct device *const usb_dev = DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
1515

16-
void usb_status_cb(enum usb_dc_status_code cb_status, const uint8_t *param) {
17-
(void)param; // unused
18-
if (cb_status == USB_DC_CONFIGURED) {
19-
20-
}
21-
}
22-
2316
void __attribute__((weak)) _on_1200_bps() {
2417
NVIC_SystemReset();
2518
}
2619

27-
void arduino::SerialUSB_::_baudChangeHandler()
28-
{
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-
{
20+
void arduino::SerialUSB_::baudChangeHandler(const struct device *dev, uint32_t rate) {
3821
(void)dev; // unused
3922
if (rate == 1200) {
4023
usb_disable();
@@ -43,77 +26,58 @@ static void _baudChangeHandler(const struct device *dev, uint32_t rate)
4326
}
4427

4528
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
46-
47-
extern "C" {
48-
#include <zephyr/usb/usbd.h>
49-
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
50-
}
51-
52-
struct usbd_context *_usbd;
53-
54-
int usb_disable() {
55-
return usbd_disable(_usbd);
29+
int arduino::SerialUSB_::usb_disable() {
30+
return usbd_disable(Serial._usbd);
5631
}
5732

58-
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg)
59-
{
60-
if (usbd_can_detect_vbus(ctx)) {
61-
if (msg->type == USBD_MSG_VBUS_READY) {
62-
usbd_enable(ctx);
63-
}
33+
void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) {
34+
if (usbd_can_detect_vbus(ctx)) {
35+
if (msg->type == USBD_MSG_VBUS_READY) {
36+
usbd_enable(ctx);
37+
}
6438

65-
if (msg->type == USBD_MSG_VBUS_REMOVED) {
66-
usbd_disable(ctx);
67-
}
68-
}
39+
if (msg->type == USBD_MSG_VBUS_REMOVED) {
40+
usbd_disable(ctx);
41+
}
42+
}
6943

70-
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
44+
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
7145
uint32_t baudrate;
72-
uart_line_ctrl_get(ctx->dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
73-
_baudChangeHandler(nullptr, baudrate);
74-
}
46+
uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
47+
Serial.baudChangeHandler(nullptr, baudrate);
48+
}
7549
}
7650

77-
static int enable_usb_device_next(void)
78-
{
79-
int err;
80-
81-
//_usbd = usbd_init_device(usbd_next_cb);
82-
_usbd = usbd_init_device(nullptr);
83-
if (_usbd == NULL) {
84-
return -ENODEV;
85-
}
86-
87-
if (!usbd_can_detect_vbus(_usbd)) {
88-
err = usbd_enable(_usbd);
89-
if (err) {
90-
return err;
91-
}
92-
}
93-
return 0;
94-
}
95-
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
51+
int arduino::SerialUSB_::enable_usb_device_next(void) {
52+
int err;
9653

97-
void arduino::SerialUSB_::_baudChangeDispatch(struct k_timer *timer) {
98-
arduino::SerialUSB_* dev = (arduino::SerialUSB_*)k_timer_user_data_get(timer);
99-
dev->_baudChangeHandler();
100-
}
54+
_usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb);
55+
if (_usbd == NULL) {
56+
return -ENODEV;
57+
}
10158

59+
if (!usbd_can_detect_vbus(_usbd)) {
60+
err = usbd_enable(_usbd);
61+
if (err) {
62+
return err;
63+
}
64+
}
65+
return 0;
66+
}
67+
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
10268

10369
void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) {
10470
if (!started) {
105-
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
71+
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
10672
usb_enable(NULL);
107-
#ifndef CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT
108-
k_timer_init(&baud_timer, SerialUSB_::_baudChangeDispatch, NULL);
109-
k_timer_user_data_set(&baud_timer, this);
110-
k_timer_start(&baud_timer, K_MSEC(100), K_MSEC(100));
111-
#else
112-
cdc_acm_dte_rate_callback_set(usb_dev, ::_baudChangeHandler);
113-
#endif
114-
#else
73+
#ifndef CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT
74+
#warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT"
75+
#else
76+
cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::baudChangeHandler);
77+
#endif
78+
#else
11579
enable_usb_device_next();
116-
#endif
80+
#endif
11781
ZephyrSerial::begin(baudrate, config);
11882
started = true;
11983
}
@@ -124,7 +88,6 @@ arduino::SerialUSB_::operator bool() {
12488
return dtr;
12589
}
12690

127-
12891
size_t arduino::SerialUSB_::write(const uint8_t *buffer, size_t size) {
12992
if (!Serial) return 0;
13093
return arduino::ZephyrSerial::write(buffer, size);

cores/arduino/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void __attribute__((weak))initVariant(void) {
2323

2424

2525
int main(void) {
26-
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
26+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
2727
Serial.begin(115200);
2828
#endif
2929

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,31 @@ 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 && (CONFIG_USB_DEVICE_STACK || CONFIG_USB_DEVICE_STACK_NEXT)
3737

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

41+
#if CONFIG_USB_DEVICE_STACK_NEXT
42+
#include <zephyr/usb/usbd.h>
43+
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
44+
int usb_enable(usb_dc_status_callback status_cb)
45+
{
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+
4161
static int enable_shell_usb(void)
4262
{
4363
bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0;

0 commit comments

Comments
 (0)