Skip to content

Commit eedfdaf

Browse files
committed
usb: refactor USBD_NEXT and implement 1200bps touch
1 parent 9f80e9f commit eedfdaf

File tree

3 files changed

+37
-49
lines changed

3 files changed

+37
-49
lines changed

cores/arduino/SerialUSB.h

+13-2
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 {
@@ -22,12 +27,18 @@ class SerialUSB_ : public ZephyrSerial {
2227
protected:
2328
uint32_t dtr = 0;
2429
uint32_t baudrate;
25-
void _baudChangeHandler();
2630
static void _baudChangeDispatch(struct k_timer *timer);
31+
static void _baudChangeHandler(const struct device *dev, uint32_t rate);
2732

2833
private:
29-
struct k_timer baud_timer;
3034
bool started = false;
35+
36+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
37+
struct usbd_context *_usbd;
38+
int enable_usb_device_next();
39+
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg);
40+
static int usb_disable();
41+
#endif
3142
};
3243
} // namespace arduino
3344

cores/arduino/USB.cpp

+23-46
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,47 @@ void __attribute__((weak)) _on_1200_bps() {
2424
NVIC_SystemReset();
2525
}
2626

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)
27+
void arduino::SerialUSB_::_baudChangeHandler(const struct device *dev, uint32_t rate)
3728
{
3829
(void)dev; // unused
3930
if (rate == 1200) {
31+
k_sleep(K_MSEC(100));
4032
usb_disable();
33+
k_sleep(K_MSEC(10));
4134
_on_1200_bps();
4235
}
4336
}
4437

4538
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
4639

47-
extern "C" {
48-
#include <zephyr/usb/usbd.h>
49-
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
40+
int arduino::SerialUSB_::usb_disable() {
41+
return usbd_disable(Serial._usbd);
5042
}
5143

52-
struct usbd_context *_usbd;
53-
54-
int usb_disable() {
55-
return usbd_disable(_usbd);
56-
}
57-
58-
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg)
44+
void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg)
5945
{
60-
if (usbd_can_detect_vbus(ctx)) {
61-
if (msg->type == USBD_MSG_VBUS_READY) {
62-
usbd_enable(ctx);
63-
}
64-
65-
if (msg->type == USBD_MSG_VBUS_REMOVED) {
66-
usbd_disable(ctx);
67-
}
68-
}
46+
if (usbd_can_detect_vbus(ctx)) {
47+
if (msg->type == USBD_MSG_VBUS_READY) {
48+
usbd_enable(ctx);
49+
}
50+
51+
if (msg->type == USBD_MSG_VBUS_REMOVED) {
52+
usbd_disable(ctx);
53+
}
54+
}
6955

70-
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
56+
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
7157
uint32_t baudrate;
72-
uart_line_ctrl_get(ctx->dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
73-
_baudChangeHandler(nullptr, baudrate);
74-
}
58+
uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
59+
Serial._baudChangeHandler(nullptr, baudrate);
60+
}
7561
}
7662

77-
static int enable_usb_device_next(void)
63+
int arduino::SerialUSB_::enable_usb_device_next(void)
7864
{
7965
int err;
8066

81-
//_usbd = usbd_init_device(usbd_next_cb);
82-
_usbd = usbd_init_device(nullptr);
67+
_usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb);
8368
if (_usbd == NULL) {
8469
return -ENODEV;
8570
}
@@ -94,22 +79,14 @@ static int enable_usb_device_next(void)
9479
}
9580
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
9681

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-
}
101-
102-
10382
void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) {
10483
if (!started) {
10584
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
10685
usb_enable(NULL);
10786
#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));
87+
#warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT"
11188
#else
112-
cdc_acm_dte_rate_callback_set(usb_dev, ::_baudChangeHandler);
89+
cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::_baudChangeHandler);
11390
#endif
11491
#else
11592
enable_usb_device_next();

cores/arduino/main.cpp

+1-1
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

0 commit comments

Comments
 (0)