|
29 | 29 | #include "usb.h"
|
30 | 30 |
|
31 | 31 | #if MICROPY_HW_USB_CDC
|
| 32 | +#include "esp_rom_gpio.h" |
| 33 | +#include "esp_mac.h" |
| 34 | +#include "esp_private/usb_phy.h" |
32 | 35 |
|
33 |
| -#include "esp_timer.h" |
34 |
| -#ifndef NO_QSTR |
35 |
| -#include "tinyusb.h" |
36 |
| -#include "tusb_cdc_acm.h" |
37 |
| -#endif |
| 36 | +#include "shared/tinyusb/mp_usbd.h" |
38 | 37 |
|
39 |
| -#define CDC_ITF TINYUSB_CDC_ACM_0 |
| 38 | +static usb_phy_handle_t phy_hdl; |
40 | 39 |
|
41 |
| -static uint8_t usb_rx_buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE]; |
42 |
| - |
43 |
| -// This is called from FreeRTOS task "tusb_tsk" in espressif__esp_tinyusb (not an ISR). |
44 |
| -static void usb_callback_rx(int itf, cdcacm_event_t *event) { |
45 |
| - // espressif__esp_tinyusb places tinyusb rx data onto freertos ringbuffer which |
46 |
| - // this function forwards onto our stdin_ringbuf. |
47 |
| - for (;;) { |
48 |
| - size_t len = 0; |
49 |
| - esp_err_t ret = tinyusb_cdcacm_read(itf, usb_rx_buf, sizeof(usb_rx_buf), &len); |
50 |
| - if (ret != ESP_OK) { |
51 |
| - break; |
52 |
| - } |
53 |
| - if (len == 0) { |
54 |
| - break; |
55 |
| - } |
56 |
| - for (size_t i = 0; i < len; ++i) { |
57 |
| - if (usb_rx_buf[i] == mp_interrupt_char) { |
58 |
| - mp_sched_keyboard_interrupt(); |
59 |
| - } else { |
60 |
| - ringbuf_put(&stdin_ringbuf, usb_rx_buf[i]); |
61 |
| - } |
62 |
| - } |
63 |
| - mp_hal_wake_main_task(); |
64 |
| - } |
65 |
| -} |
66 | 40 |
|
67 | 41 | void usb_init(void) {
|
68 |
| - // Initialise the USB with defaults. |
69 |
| - tinyusb_config_t tusb_cfg = {0}; |
70 |
| - ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); |
| 42 | + // ref: https://github.com/espressif/esp-usb/blob/4b6a798d0bed444fff48147c8dcdbbd038e92892/device/esp_tinyusb/tinyusb.c |
71 | 43 |
|
72 |
| - // Initialise the USB serial interface. |
73 |
| - tinyusb_config_cdcacm_t acm_cfg = { |
74 |
| - .usb_dev = TINYUSB_USBDEV_0, |
75 |
| - .cdc_port = CDC_ITF, |
76 |
| - .rx_unread_buf_sz = 256, |
77 |
| - .callback_rx = &usb_callback_rx, |
78 |
| - #ifdef MICROPY_HW_USB_CUSTOM_RX_WANTED_CHAR_CB |
79 |
| - .callback_rx_wanted_char = &MICROPY_HW_USB_CUSTOM_RX_WANTED_CHAR_CB, |
80 |
| - #endif |
81 |
| - #ifdef MICROPY_HW_USB_CUSTOM_LINE_STATE_CB |
82 |
| - .callback_line_state_changed = (tusb_cdcacm_callback_t)&MICROPY_HW_USB_CUSTOM_LINE_STATE_CB, |
83 |
| - #endif |
84 |
| - #ifdef MICROPY_HW_USB_CUSTOM_LINE_CODING_CB |
85 |
| - .callback_line_coding_changed = &MICROPY_HW_USB_CUSTOM_LINE_CODING_CB, |
86 |
| - #endif |
| 44 | + // Configure USB PHY |
| 45 | + usb_phy_config_t phy_conf = { |
| 46 | + .controller = USB_PHY_CTRL_OTG, |
| 47 | + .otg_mode = USB_OTG_MODE_DEVICE, |
87 | 48 | };
|
88 |
| - ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); |
| 49 | + // Internal USB PHY |
| 50 | + phy_conf.target = USB_PHY_TARGET_INT; |
| 51 | + |
| 52 | + // Init ESP USB Phy |
| 53 | + usb_new_phy(&phy_conf, &phy_hdl); |
| 54 | + |
| 55 | + // Init MicroPython / TinyUSB |
| 56 | + mp_usbd_init(); |
89 | 57 |
|
90 | 58 | }
|
91 | 59 |
|
92 |
| -void usb_tx_strn(const char *str, size_t len) { |
93 |
| - // Write out the data to the CDC interface, but only while the USB host is connected. |
94 |
| - uint64_t timeout = esp_timer_get_time() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT_MS * 1000); |
95 |
| - while (tud_cdc_n_connected(CDC_ITF) && len && esp_timer_get_time() < timeout) { |
96 |
| - size_t l = tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, len); |
97 |
| - str += l; |
98 |
| - len -= l; |
99 |
| - tud_cdc_n_write_flush(CDC_ITF); |
100 |
| - } |
| 60 | +void mp_usbd_port_get_serial_number(char *serial_buf) { |
| 61 | + // use factory default MAC as serial ID |
| 62 | + uint8_t mac[8]; |
| 63 | + esp_efuse_mac_get_default(mac); |
| 64 | + MP_STATIC_ASSERT(sizeof(mac) * 2 <= MICROPY_HW_USB_DESC_STR_MAX); |
| 65 | + mp_usbd_hex_str(serial_buf, mac, sizeof(mac)); |
101 | 66 | }
|
102 | 67 |
|
103 | 68 | #endif // MICROPY_HW_USB_CDC
|
0 commit comments