13
13
#if ((DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)) && (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
14
14
const struct device *const usb_dev = DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0 ));
15
15
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
-
23
16
void __attribute__ ((weak)) _on_1200_bps() {
24
17
NVIC_SystemReset ();
25
18
}
26
19
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) {
38
21
(void )dev; // unused
39
22
if (rate == 1200 ) {
40
23
usb_disable ();
@@ -43,77 +26,58 @@ static void _baudChangeHandler(const struct device *dev, uint32_t rate)
43
26
}
44
27
45
28
#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 );
56
31
}
57
32
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
+ }
64
38
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
+ }
69
43
70
- if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
44
+ if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
71
45
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
+ }
75
49
}
76
50
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;
96
53
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
+ }
101
58
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) */
102
68
103
69
void arduino::SerialUSB_::begin (unsigned long baudrate, uint16_t config) {
104
70
if (!started) {
105
- #ifndef CONFIG_USB_DEVICE_STACK_NEXT
71
+ #ifndef CONFIG_USB_DEVICE_STACK_NEXT
106
72
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
115
79
enable_usb_device_next ();
116
- #endif
80
+ #endif
117
81
ZephyrSerial::begin (baudrate, config);
118
82
started = true ;
119
83
}
@@ -124,7 +88,6 @@ arduino::SerialUSB_::operator bool() {
124
88
return dtr;
125
89
}
126
90
127
-
128
91
size_t arduino::SerialUSB_::write (const uint8_t *buffer, size_t size) {
129
92
if (!Serial) return 0 ;
130
93
return arduino::ZephyrSerial::write (buffer, size);
0 commit comments