|
15 | 15 | const struct device *const usb_dev =
|
16 | 16 | DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
|
17 | 17 |
|
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 |
| - |
24 | 18 | void __attribute__((weak)) _on_1200_bps() {
|
25 | 19 | NVIC_SystemReset();
|
26 | 20 | }
|
27 | 21 |
|
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 | + } |
42 | 28 | }
|
43 | 29 |
|
44 | 30 | #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); |
55 | 33 | }
|
56 | 34 |
|
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 | + } |
73 | 51 | }
|
74 | 52 |
|
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; |
91 | 68 | }
|
92 | 69 | #endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
|
93 | 70 |
|
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 |
| - |
99 | 71 | void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) {
|
100 |
| - if (!started) { |
| 72 | + if (!started) { |
101 | 73 | #ifndef CONFIG_USB_DEVICE_STACK_NEXT
|
102 |
| - usb_enable(NULL); |
| 74 | + usb_enable(NULL); |
103 | 75 | #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" |
107 | 77 | #else
|
108 |
| - cdc_acm_dte_rate_callback_set(usb_dev, ::_baudChangeHandler); |
| 78 | + cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::baudChangeHandler); |
109 | 79 | #endif
|
110 | 80 | #else
|
111 |
| - enable_usb_device_next(); |
| 81 | + enable_usb_device_next(); |
112 | 82 | #endif
|
113 |
| - ZephyrSerial::begin(baudrate, config); |
114 |
| - started = true; |
115 |
| - } |
| 83 | + ZephyrSerial::begin(baudrate, config); |
| 84 | + started = true; |
| 85 | + } |
116 | 86 | }
|
117 | 87 |
|
118 | 88 | arduino::SerialUSB_::operator bool() {
|
119 | 89 | uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &dtr);
|
120 | 90 | return dtr;
|
121 | 91 | }
|
122 | 92 |
|
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); |
128 | 96 | }
|
129 | 97 |
|
130 | 98 | void arduino::SerialUSB_::flush() {
|
|
0 commit comments