1313#if ((DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)) && (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
1414const 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-
2316void __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
10369void 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 }
0 commit comments