|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 Ibrahim Abdelkader <iabdalkader@openmv.io> |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/runtime.h" |
| 28 | +#include "py/mphal.h" |
| 29 | +#include "py/mpconfig.h" |
| 30 | +#include "py/stream.h" |
| 31 | +#include "extmod/modmachine.h" |
| 32 | + |
| 33 | +#if MICROPY_HW_USB_CDC && MICROPY_HW_ENABLE_USBDEV |
| 34 | +#include "tusb.h" |
| 35 | +#include "device/usbd.h" |
| 36 | + |
| 37 | +#include "mp_usbd_cdc.h" |
| 38 | +#include "mp_usbd.h" |
| 39 | + |
| 40 | +static uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll |
| 41 | + |
| 42 | +uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) { |
| 43 | + uintptr_t ret = 0; |
| 44 | + if (!cdc_itf_pending) { |
| 45 | + // Explicitly run the USB stack as the scheduler may be locked (eg we are in |
| 46 | + // an interrupt handler) while there is data pending. |
| 47 | + mp_usbd_task(); |
| 48 | + } |
| 49 | + |
| 50 | + // any CDC interfaces left to poll? |
| 51 | + if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) { |
| 52 | + for (uint8_t itf = 0; itf < 8; ++itf) { |
| 53 | + if (cdc_itf_pending & (1 << itf)) { |
| 54 | + tud_cdc_rx_cb(itf); |
| 55 | + if (!cdc_itf_pending) { |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) { |
| 62 | + ret |= MP_STREAM_POLL_RD; |
| 63 | + } |
| 64 | + if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) { |
| 65 | + // When connected operate as blocking, only allow if space is available. |
| 66 | + ret |= MP_STREAM_POLL_WR; |
| 67 | + } |
| 68 | + return ret; |
| 69 | +} |
| 70 | + |
| 71 | +void tud_cdc_rx_cb(uint8_t itf) { |
| 72 | + // consume pending USB data immediately to free usb buffer and keep the endpoint from stalling. |
| 73 | + // in case the ringbuffer is full, mark the CDC interface that need attention later on for polling |
| 74 | + cdc_itf_pending &= ~(1 << itf); |
| 75 | + for (uint32_t bytes_avail = tud_cdc_n_available(itf); bytes_avail > 0; --bytes_avail) { |
| 76 | + if (ringbuf_free(&stdin_ringbuf)) { |
| 77 | + int data_char = tud_cdc_read_char(); |
| 78 | + #if MICROPY_KBD_EXCEPTION |
| 79 | + if (data_char == mp_interrupt_char) { |
| 80 | + // Clear the ring buffer |
| 81 | + stdin_ringbuf.iget = stdin_ringbuf.iput = 0; |
| 82 | + // and stop |
| 83 | + mp_sched_keyboard_interrupt(); |
| 84 | + } else { |
| 85 | + ringbuf_put(&stdin_ringbuf, data_char); |
| 86 | + } |
| 87 | + #else |
| 88 | + ringbuf_put(&stdin_ringbuf, data_char); |
| 89 | + #endif |
| 90 | + } else { |
| 91 | + cdc_itf_pending |= (1 << itf); |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len) { |
| 98 | + size_t i = 0; |
| 99 | + if (tud_cdc_connected()) { |
| 100 | + while (i < len) { |
| 101 | + uint32_t n = len - i; |
| 102 | + if (n > CFG_TUD_CDC_EP_BUFSIZE) { |
| 103 | + n = CFG_TUD_CDC_EP_BUFSIZE; |
| 104 | + } |
| 105 | + int timeout = 0; |
| 106 | + // Wait with a max of USC_CDC_TIMEOUT ms |
| 107 | + while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) { |
| 108 | + mp_event_wait_ms(1); |
| 109 | + |
| 110 | + // Explicitly run the USB stack as the scheduler may be locked (eg we |
| 111 | + // are in an interrupt handler), while there is data pending. |
| 112 | + mp_usbd_task(); |
| 113 | + } |
| 114 | + if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) { |
| 115 | + break; |
| 116 | + } |
| 117 | + uint32_t n2 = tud_cdc_write(str + i, n); |
| 118 | + tud_cdc_write_flush(); |
| 119 | + i += n2; |
| 120 | + } |
| 121 | + } |
| 122 | + return i; |
| 123 | +} |
| 124 | + |
| 125 | +#if MICROPY_HW_USB_CDC_1200BPS_TOUCH && MICROPY_HW_ENABLE_USBDEV |
| 126 | + |
| 127 | +static mp_sched_node_t mp_bootloader_sched_node; |
| 128 | + |
| 129 | +static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) { |
| 130 | + mp_hal_delay_ms(250); |
| 131 | + machine_bootloader(0, NULL); |
| 132 | +} |
| 133 | + |
| 134 | +void |
| 135 | +#if MICROPY_HW_USB_EXTERNAL_TINYUSB |
| 136 | +mp_usbd_line_state_cb |
| 137 | +#else |
| 138 | +tud_cdc_line_state_cb |
| 139 | +#endif |
| 140 | + (uint8_t itf, bool dtr, bool rts) { |
| 141 | + if (dtr == false && rts == false) { |
| 142 | + // Device is disconnected. |
| 143 | + cdc_line_coding_t line_coding; |
| 144 | + tud_cdc_n_get_line_coding(itf, &line_coding); |
| 145 | + if (line_coding.bit_rate == 1200) { |
| 146 | + // Delay bootloader jump to allow the USB stack to service endpoints. |
| 147 | + mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#endif |
| 153 | +#endif |
0 commit comments