@@ -23,61 +23,46 @@ void __attribute__((weak)) _on_1200_bps() {
23
23
NVIC_SystemReset ();
24
24
}
25
25
26
- void arduino::SerialUSB_::_baudChangeHandler ()
27
- {
28
- uart_line_ctrl_get (uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
29
- if (baudrate == 1200 ) {
30
- usb_disable ();
31
- _on_1200_bps ();
32
- }
33
- }
34
-
35
- static void _baudChangeHandler (const struct device *dev, uint32_t rate)
26
+ void arduino::SerialUSB_::_baudChangeHandler (const struct device *dev, uint32_t rate)
36
27
{
37
28
if (rate == 1200 ) {
29
+ k_sleep (K_MSEC (100 ));
38
30
usb_disable ();
31
+ k_sleep (K_MSEC (10 ));
39
32
_on_1200_bps ();
40
33
}
41
34
}
42
35
43
36
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
44
37
45
- extern " C" {
46
- #include < zephyr/usb/usbd.h>
47
- struct usbd_context *usbd_init_device (usbd_msg_cb_t msg_cb);
38
+ int arduino::SerialUSB_::usb_disable () {
39
+ return usbd_disable (Serial._usbd );
48
40
}
49
41
50
- struct usbd_context *_usbd;
51
-
52
- int usb_disable () {
53
- return usbd_disable (_usbd);
54
- }
55
-
56
- static void usbd_next_cb (struct usbd_context *const ctx, const struct usbd_msg *msg)
42
+ void arduino::SerialUSB_::usbd_next_cb (struct usbd_context *const ctx, const struct usbd_msg *msg)
57
43
{
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
- }
44
+ if (usbd_can_detect_vbus (ctx)) {
45
+ if (msg->type == USBD_MSG_VBUS_READY) {
46
+ usbd_enable (ctx);
47
+ }
48
+
49
+ if (msg->type == USBD_MSG_VBUS_REMOVED) {
50
+ usbd_disable (ctx);
51
+ }
52
+ }
67
53
68
- if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
54
+ if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
69
55
uint32_t baudrate;
70
- uart_line_ctrl_get (ctx-> dev , UART_LINE_CTRL_BAUD_RATE, &baudrate);
71
- _baudChangeHandler (nullptr , baudrate);
72
- }
56
+ uart_line_ctrl_get (Serial. uart , UART_LINE_CTRL_BAUD_RATE, &baudrate);
57
+ Serial. _baudChangeHandler (nullptr , baudrate);
58
+ }
73
59
}
74
60
75
- static int enable_usb_device_next (void )
61
+ int arduino::SerialUSB_:: enable_usb_device_next (void )
76
62
{
77
63
int err;
78
64
79
- // _usbd = usbd_init_device(usbd_next_cb);
80
- _usbd = usbd_init_device (nullptr );
65
+ _usbd = usbd_init_device (arduino::SerialUSB_::usbd_next_cb);
81
66
if (_usbd == NULL ) {
82
67
return -ENODEV;
83
68
}
@@ -92,22 +77,14 @@ static int enable_usb_device_next(void)
92
77
}
93
78
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
94
79
95
- void arduino::SerialUSB_::_baudChangeDispatch (struct k_timer *timer) {
96
- arduino::SerialUSB_* dev = (arduino::SerialUSB_*)k_timer_user_data_get (timer);
97
- dev->_baudChangeHandler ();
98
- }
99
-
100
-
101
80
void arduino::SerialUSB_::begin (unsigned long baudrate, uint16_t config) {
102
81
if (!started) {
103
82
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
104
83
usb_enable (NULL );
105
84
#ifndef CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT
106
- k_timer_init (&baud_timer, SerialUSB_::_baudChangeDispatch, NULL );
107
- k_timer_user_data_set (&baud_timer, this );
108
- k_timer_start (&baud_timer, K_MSEC (100 ), K_MSEC (100 ));
85
+ #warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT"
109
86
#else
110
- cdc_acm_dte_rate_callback_set (usb_dev, ::_baudChangeHandler);
87
+ cdc_acm_dte_rate_callback_set (usb_dev, SerialUSB_ ::_baudChangeHandler);
111
88
#endif
112
89
#else
113
90
enable_usb_device_next ();
0 commit comments