|
| 1 | +/* |
| 2 | + * Copyright (c) Arduino s.r.l. and/or its affiliated companies |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/devicetree.h> |
| 8 | +#include <zephyr/drivers/uart.h> |
| 9 | +#include <zephyr/drivers/uart/cdc_acm.h> |
| 10 | +#include <zephyr/usb/usb_device.h> |
| 11 | +#include <SerialUSB.h> |
| 12 | + |
| 13 | +#if ZARD_FIRST_SERIAL_IS_SERIALUSB |
| 14 | +const struct device *const usb_dev = |
| 15 | + DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), cdc_acm_serial, 0)); |
| 16 | + |
| 17 | +void __attribute__((weak)) _on_1200_bps() { |
| 18 | + NVIC_SystemReset(); |
| 19 | +} |
| 20 | + |
| 21 | +void arduino::SerialUSB_::baudChangeHandler(const struct device *dev, uint32_t rate) { |
| 22 | + (void)dev; // unused |
| 23 | + if (rate == 1200) { |
| 24 | + usb_disable(); |
| 25 | + _on_1200_bps(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#if defined(CONFIG_USB_DEVICE_STACK_NEXT) |
| 30 | +int arduino::SerialUSB_::usb_disable() { |
| 31 | + // To avoid Cannot perform port reset: 1200-bps touch: setting DTR to OFF: protocol error |
| 32 | + k_sleep(K_MSEC(100)); |
| 33 | + return usbd_disable(Serial._usbd); |
| 34 | +} |
| 35 | + |
| 36 | +void arduino::SerialUSB_::usbd_next_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) { |
| 37 | + if (usbd_can_detect_vbus(ctx)) { |
| 38 | + if (msg->type == USBD_MSG_VBUS_READY) { |
| 39 | + usbd_enable(ctx); |
| 40 | + } |
| 41 | + |
| 42 | + if (msg->type == USBD_MSG_VBUS_REMOVED) { |
| 43 | + usbd_disable(ctx); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING) { |
| 48 | + uint32_t baudrate; |
| 49 | + uart_line_ctrl_get(Serial.uart, UART_LINE_CTRL_BAUD_RATE, &baudrate); |
| 50 | + Serial.baudChangeHandler(nullptr, baudrate); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +int arduino::SerialUSB_::enable_usb_device_next(void) { |
| 55 | + int err; |
| 56 | + |
| 57 | + _usbd = usbd_init_device(arduino::SerialUSB_::usbd_next_cb); |
| 58 | + if (_usbd == NULL) { |
| 59 | + return -ENODEV; |
| 60 | + } |
| 61 | + |
| 62 | + if (!usbd_can_detect_vbus(_usbd)) { |
| 63 | + err = usbd_enable(_usbd); |
| 64 | + if (err) { |
| 65 | + return err; |
| 66 | + } |
| 67 | + } |
| 68 | + return 0; |
| 69 | +} |
| 70 | +#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */ |
| 71 | + |
| 72 | +void arduino::SerialUSB_::begin(unsigned long baudrate, uint16_t config) { |
| 73 | + if (!started) { |
| 74 | +#ifndef CONFIG_USB_DEVICE_STACK_NEXT |
| 75 | + usb_enable(NULL); |
| 76 | +#ifndef CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT |
| 77 | +#warning "Can't read CDC baud change, please enable CONFIG_CDC_ACM_DTE_RATE_CALLBACK_SUPPORT" |
| 78 | +#else |
| 79 | + cdc_acm_dte_rate_callback_set(usb_dev, SerialUSB_::baudChangeHandler); |
| 80 | +#endif |
| 81 | +#else |
| 82 | + enable_usb_device_next(); |
| 83 | +#endif |
| 84 | + ZephyrSerial::begin(baudrate, config); |
| 85 | + started = true; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +arduino::SerialUSB_::operator bool() { |
| 90 | + uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &dtr); |
| 91 | + return dtr; |
| 92 | +} |
| 93 | + |
| 94 | +size_t arduino::SerialUSB_::write(const uint8_t *buffer, size_t size) { |
| 95 | + if (!Serial) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + return arduino::ZephyrSerial::write(buffer, size); |
| 99 | +} |
| 100 | + |
| 101 | +void arduino::SerialUSB_::flush() { |
| 102 | + if (!Serial) { |
| 103 | + return; |
| 104 | + } |
| 105 | + arduino::ZephyrSerial::flush(); |
| 106 | +} |
| 107 | + |
| 108 | +arduino::SerialUSB_ Serial(usb_dev); |
| 109 | +#endif |
0 commit comments