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) {
22
+ void arduino::SerialUSB_::baudChangeHandler (const struct device *dev, uint32_t rate) {
37
23
(void )dev; // unused
38
24
if (rate == 1200 ) {
39
25
usb_disable ();
@@ -42,19 +28,13 @@ static void _baudChangeHandler(const struct device *dev, uint32_t rate) {
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 );
31
+ int arduino::SerialUSB_::usb_disable () {
32
+ // To avoid Cannot perform port reset: 1200-bps touch: setting DTR to OFF: protocol error
33
+ k_sleep ( K_MSEC ( 100 ));
34
+ return usbd_disable (Serial. _usbd );
49
35
}
50
36
51
- struct usbd_context *_usbd;
52
-
53
- int usb_disable () {
54
- return usbd_disable (_usbd);
55
- }
56
-
57
- static void usbd_next_cb (struct usbd_context *const ctx, const struct usbd_msg *msg) {
37
+ void arduino::SerialUSB_::usbd_next_cb (struct usbd_context *const ctx, const struct usbd_msg *msg) {
58
38
if (usbd_can_detect_vbus (ctx)) {
59
39
if (msg->type == USBD_MSG_VBUS_READY) {
60
40
usbd_enable (ctx);
@@ -67,16 +47,15 @@ static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *
67
47
68
48
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
69
49
uint32_t baudrate;
70
- uart_line_ctrl_get (ctx-> dev , UART_LINE_CTRL_BAUD_RATE, &baudrate);
71
- _baudChangeHandler (nullptr , baudrate);
50
+ uart_line_ctrl_get (Serial. uart , UART_LINE_CTRL_BAUD_RATE, &baudrate);
51
+ Serial. baudChangeHandler (nullptr , baudrate);
72
52
}
73
53
}
74
54
75
- static int enable_usb_device_next (void ) {
55
+ int arduino::SerialUSB_:: enable_usb_device_next (void ) {
76
56
int err;
77
57
78
- // _usbd = usbd_init_device(usbd_next_cb);
79
- _usbd = usbd_init_device (nullptr );
58
+ _usbd = usbd_init_device (arduino::SerialUSB_::usbd_next_cb);
80
59
if (_usbd == NULL ) {
81
60
return -ENODEV;
82
61
}
@@ -91,21 +70,14 @@ static int enable_usb_device_next(void) {
91
70
}
92
71
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
93
72
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
73
void arduino::SerialUSB_::begin (unsigned long baudrate, uint16_t config) {
100
74
if (!started) {
101
75
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
102
76
usb_enable (NULL );
103
77
#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 ));
78
+ #warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT"
107
79
#else
108
- cdc_acm_dte_rate_callback_set (usb_dev, ::_baudChangeHandler );
80
+ cdc_acm_dte_rate_callback_set (usb_dev, SerialUSB_::baudChangeHandler );
109
81
#endif
110
82
#else
111
83
enable_usb_device_next ();
0 commit comments