Skip to content

Commit bbfc0af

Browse files
facchinmpennam
authored andcommitted
usb: refactor USBD_NEXT and implement 1200bps touch
1 parent 47df7e5 commit bbfc0af

File tree

5 files changed

+63
-50
lines changed

5 files changed

+63
-50
lines changed

cores/arduino/SerialUSB.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#include <zephyrSerial.h>
1010

11+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
12+
#include <zephyr/usb/usbd.h>
13+
extern "C" struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
14+
#endif
15+
1116
namespace arduino {
1217

1318
class SerialUSB_ : public ZephyrSerial {
@@ -25,12 +30,18 @@ class SerialUSB_ : public ZephyrSerial {
2530
protected:
2631
uint32_t dtr = 0;
2732
uint32_t baudrate;
28-
void _baudChangeHandler();
2933
static void _baudChangeDispatch(struct k_timer *timer);
34+
static void _baudChangeHandler(const struct device *dev, uint32_t rate);
3035

3136
private:
32-
struct k_timer baud_timer;
3337
bool started = false;
38+
39+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
40+
struct usbd_context *_usbd;
41+
int enable_usb_device_next();
42+
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg);
43+
static int usb_disable();
44+
#endif
3445
};
3546
} // namespace arduino
3647

cores/arduino/USB.cpp

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,47 @@ void __attribute__((weak)) _on_1200_bps() {
2424
NVIC_SystemReset();
2525
}
2626

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)
27+
void arduino::SerialUSB_::_baudChangeHandler(const struct device *dev, uint32_t rate)
3728
{
3829
(void)dev; // unused
3930
if (rate == 1200) {
31+
k_sleep(K_MSEC(100));
4032
usb_disable();
33+
k_sleep(K_MSEC(10));
4134
_on_1200_bps();
4235
}
4336
}
4437

4538
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
4639

47-
extern "C" {
48-
#include <zephyr/usb/usbd.h>
49-
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
40+
int arduino::SerialUSB_::usb_disable() {
41+
return usbd_disable(Serial._usbd);
5042
}
5143

52-
struct usbd_context *_usbd;
53-
54-
int usb_disable() {
55-
return usbd_disable(_usbd);
56-
}
57-
58-
static void usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg)
44+
void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg)
5945
{
60-
if (usbd_can_detect_vbus(ctx)) {
61-
if (msg->type == USBD_MSG_VBUS_READY) {
62-
usbd_enable(ctx);
63-
}
64-
65-
if (msg->type == USBD_MSG_VBUS_REMOVED) {
66-
usbd_disable(ctx);
67-
}
68-
}
46+
if (usbd_can_detect_vbus(ctx)) {
47+
if (msg->type == USBD_MSG_VBUS_READY) {
48+
usbd_enable(ctx);
49+
}
50+
51+
if (msg->type == USBD_MSG_VBUS_REMOVED) {
52+
usbd_disable(ctx);
53+
}
54+
}
6955

70-
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
56+
if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) {
7157
uint32_t baudrate;
72-
uart_line_ctrl_get(ctx->dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
73-
_baudChangeHandler(nullptr, baudrate);
74-
}
58+
uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate);
59+
Serial._baudChangeHandler(nullptr, baudrate);
60+
}
7561
}
7662

77-
static int enable_usb_device_next(void)
63+
int arduino::SerialUSB_::enable_usb_device_next(void)
7864
{
7965
int err;
8066

81-
//_usbd = usbd_init_device(usbd_next_cb);
82-
_usbd = usbd_init_device(nullptr);
67+
_usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb);
8368
if (_usbd == NULL) {
8469
return -ENODEV;
8570
}
@@ -94,22 +79,14 @@ static int enable_usb_device_next(void)
9479
}
9580
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
9681

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-
}
101-
102-
10382
void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) {
10483
if (!started) {
10584
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
10685
usb_enable(NULL);
10786
#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));
87+
#warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT"
11188
#else
112-
cdc_acm_dte_rate_callback_set(usb_dev, ::_baudChangeHandler);
89+
cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::_baudChangeHandler);
11390
#endif
11491
#else
11592
enable_usb_device_next();

cores/arduino/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void __attribute__((weak))initVariant(void) {
2323

2424

2525
int main(void) {
26-
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_USB_CDC_ACM)
26+
#if (DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && (CONFIG_USB_CDC_ACM || CONFIG_USBD_CDC_ACM_CLASS))
2727
Serial.begin(115200);
2828
#endif
2929

loader/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ project(app LANGUAGES C CXX)
1616

1717
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/blobs)
1818

19+
# for USB device stack NEXT
20+
target_sources_ifdef(CONFIG_USB_DEVICE_STACK_NEXT app PRIVATE
21+
${CMAKE_CURRENT_LIST_DIR}/../cores/arduino/usb_device_descriptor.c
22+
)
23+
1924
FILE(GLOB app_sources *.c)
2025
target_sources(app PRIVATE ${app_sources})
2126

loader/main.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,31 @@ struct sketch_header_v1 {
3333
#define SKETCH_FLAG_LINKED 0x02
3434

3535
#define TARGET_HAS_USB_CDC_SHELL \
36-
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL && CONFIG_USB_DEVICE_STACK
36+
DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm) && CONFIG_SHELL && (CONFIG_USB_DEVICE_STACK || CONFIG_USB_DEVICE_STACK_NEXT)
3737

3838
#if TARGET_HAS_USB_CDC_SHELL
3939
const struct device *const usb_dev = DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm, 0));
4040

41+
#if CONFIG_USB_DEVICE_STACK_NEXT
42+
#include <zephyr/usb/usbd.h>
43+
struct usbd_context *usbd_init_device(usbd_msg_cb_t msg_cb);
44+
int usb_enable(usb_dc_status_callback status_cb)
45+
{
46+
int err;
47+
struct usbd_context *_usbd = usbd_init_device(NULL);
48+
if (_usbd == NULL) {
49+
return -ENODEV;
50+
}
51+
if (!usbd_can_detect_vbus(_usbd)) {
52+
err = usbd_enable(_usbd);
53+
if (err) {
54+
return err;
55+
}
56+
}
57+
return 0;
58+
}
59+
#endif
60+
4161
static int enable_shell_usb(void)
4262
{
4363
bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0;

0 commit comments

Comments
 (0)