|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + * |
| 24 | + * This file is part of the TinyUSB stack. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "tusb_option.h" |
| 28 | + |
| 29 | +#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_VENDOR) |
| 30 | + |
| 31 | +#include "vendor_device.h" |
| 32 | +#include "device/usbd_pvt.h" |
| 33 | + |
| 34 | +//--------------------------------------------------------------------+ |
| 35 | +// MACRO CONSTANT TYPEDEF |
| 36 | +//--------------------------------------------------------------------+ |
| 37 | +typedef struct |
| 38 | +{ |
| 39 | + uint8_t itf_num; |
| 40 | + uint8_t ep_in; |
| 41 | + uint8_t ep_out; |
| 42 | + |
| 43 | + /*------------- From this point, data is not cleared by bus reset -------------*/ |
| 44 | + tu_fifo_t rx_ff; |
| 45 | + tu_fifo_t tx_ff; |
| 46 | + |
| 47 | + uint8_t rx_ff_buf[CFG_TUD_VENDOR_RX_BUFSIZE]; |
| 48 | + uint8_t tx_ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE]; |
| 49 | + |
| 50 | +#if CFG_FIFO_MUTEX |
| 51 | + osal_mutex_def_t rx_ff_mutex; |
| 52 | + osal_mutex_def_t tx_ff_mutex; |
| 53 | +#endif |
| 54 | + |
| 55 | + // Endpoint Transfer buffer |
| 56 | + CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE]; |
| 57 | + CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE]; |
| 58 | +} vendord_interface_t; |
| 59 | + |
| 60 | +CFG_TUSB_MEM_SECTION static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; |
| 61 | + |
| 62 | +#define ITF_MEM_RESET_SIZE offsetof(vendord_interface_t, rx_ff) |
| 63 | + |
| 64 | + |
| 65 | +bool tud_vendor_n_mounted (uint8_t itf) |
| 66 | +{ |
| 67 | + return _vendord_itf[itf].ep_in && _vendord_itf[itf].ep_out; |
| 68 | +} |
| 69 | + |
| 70 | +uint32_t tud_vendor_n_available (uint8_t itf) |
| 71 | +{ |
| 72 | + return tu_fifo_count(&_vendord_itf[itf].rx_ff); |
| 73 | +} |
| 74 | + |
| 75 | +bool tud_vendor_n_peek(uint8_t itf, int pos, uint8_t* u8) |
| 76 | +{ |
| 77 | + return tu_fifo_peek_at(&_vendord_itf[itf].rx_ff, pos, u8); |
| 78 | +} |
| 79 | + |
| 80 | +//--------------------------------------------------------------------+ |
| 81 | +// Read API |
| 82 | +//--------------------------------------------------------------------+ |
| 83 | +static void _prep_out_transaction (vendord_interface_t* p_itf) |
| 84 | +{ |
| 85 | + // skip if previous transfer not complete |
| 86 | + if ( usbd_edpt_busy(TUD_OPT_RHPORT, p_itf->ep_out) ) return; |
| 87 | + |
| 88 | + // Prepare for incoming data but only allow what we can store in the ring buffer. |
| 89 | + uint16_t max_read = tu_fifo_remaining(&p_itf->rx_ff); |
| 90 | + if ( max_read >= CFG_TUD_VENDOR_EPSIZE ) |
| 91 | + { |
| 92 | + usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_out, p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) |
| 97 | +{ |
| 98 | + vendord_interface_t* p_itf = &_vendord_itf[itf]; |
| 99 | + uint32_t num_read = tu_fifo_read_n(&p_itf->rx_ff, buffer, bufsize); |
| 100 | + _prep_out_transaction(p_itf); |
| 101 | + return num_read; |
| 102 | +} |
| 103 | + |
| 104 | +//--------------------------------------------------------------------+ |
| 105 | +// Write API |
| 106 | +//--------------------------------------------------------------------+ |
| 107 | +static bool maybe_transmit(vendord_interface_t* p_itf) |
| 108 | +{ |
| 109 | + // skip if previous transfer not complete |
| 110 | + TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_itf->ep_in) ); |
| 111 | + |
| 112 | + uint16_t count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, CFG_TUD_VENDOR_EPSIZE); |
| 113 | + if (count > 0) |
| 114 | + { |
| 115 | + TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_in, p_itf->epin_buf, count) ); |
| 116 | + } |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize) |
| 121 | +{ |
| 122 | + vendord_interface_t* p_itf = &_vendord_itf[itf]; |
| 123 | + uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, bufsize); |
| 124 | + maybe_transmit(p_itf); |
| 125 | + return ret; |
| 126 | +} |
| 127 | + |
| 128 | +//--------------------------------------------------------------------+ |
| 129 | +// USBD Driver API |
| 130 | +//--------------------------------------------------------------------+ |
| 131 | +void vendord_init(void) |
| 132 | +{ |
| 133 | + tu_memclr(_vendord_itf, sizeof(_vendord_itf)); |
| 134 | + |
| 135 | + for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) |
| 136 | + { |
| 137 | + vendord_interface_t* p_itf = &_vendord_itf[i]; |
| 138 | + |
| 139 | + // config fifo |
| 140 | + tu_fifo_config(&p_itf->rx_ff, p_itf->rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, 1, false); |
| 141 | + tu_fifo_config(&p_itf->tx_ff, p_itf->tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, 1, false); |
| 142 | + |
| 143 | +#if CFG_FIFO_MUTEX |
| 144 | + tu_fifo_config_mutex(&p_itf->rx_ff, osal_mutex_create(&p_itf->rx_ff_mutex)); |
| 145 | + tu_fifo_config_mutex(&p_itf->tx_ff, osal_mutex_create(&p_itf->tx_ff_mutex)); |
| 146 | +#endif |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void vendord_reset(uint8_t rhport) |
| 151 | +{ |
| 152 | + (void) rhport; |
| 153 | + |
| 154 | + for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) |
| 155 | + { |
| 156 | + vendord_interface_t* p_itf = &_vendord_itf[i]; |
| 157 | + |
| 158 | + tu_memclr(p_itf, ITF_MEM_RESET_SIZE); |
| 159 | + tu_fifo_clear(&p_itf->rx_ff); |
| 160 | + tu_fifo_clear(&p_itf->tx_ff); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +bool vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_len) |
| 165 | +{ |
| 166 | + // Find available interface |
| 167 | + vendord_interface_t* p_vendor = NULL; |
| 168 | + for(uint8_t i=0; i<CFG_TUD_VENDOR; i++) |
| 169 | + { |
| 170 | + if ( _vendord_itf[i].ep_in == 0 && _vendord_itf[i].ep_out == 0 ) |
| 171 | + { |
| 172 | + p_vendor = &_vendord_itf[i]; |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + TU_VERIFY(p_vendor); |
| 177 | + |
| 178 | + // Open endpoint pair with usbd helper |
| 179 | + TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in)); |
| 180 | + |
| 181 | + p_vendor->itf_num = itf_desc->bInterfaceNumber; |
| 182 | + (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); |
| 183 | + |
| 184 | + // Prepare for incoming data |
| 185 | + TU_ASSERT(usbd_edpt_xfer(rhport, p_vendor->ep_out, p_vendor->epout_buf, sizeof(p_vendor->epout_buf))); |
| 186 | + |
| 187 | + return true; |
| 188 | +} |
| 189 | + |
| 190 | +bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) |
| 191 | +{ |
| 192 | + (void) rhport; |
| 193 | + (void) result; |
| 194 | + |
| 195 | + // TODO Support multiple interfaces |
| 196 | + uint8_t const itf = 0; |
| 197 | + vendord_interface_t* p_itf = &_vendord_itf[itf]; |
| 198 | + |
| 199 | + if ( ep_addr == p_itf->ep_out ) |
| 200 | + { |
| 201 | + // Receive new data |
| 202 | + tu_fifo_write_n(&p_itf->rx_ff, p_itf->epout_buf, xferred_bytes); |
| 203 | + |
| 204 | + // Invoked callback if any |
| 205 | + if (tud_vendor_rx_cb) tud_vendor_rx_cb(itf); |
| 206 | + |
| 207 | + _prep_out_transaction(p_itf); |
| 208 | + } |
| 209 | + else if ( ep_addr == p_itf->ep_in ) |
| 210 | + { |
| 211 | + // Send complete, try to send more if possible |
| 212 | + maybe_transmit(p_itf); |
| 213 | + } |
| 214 | + |
| 215 | + return true; |
| 216 | +} |
| 217 | + |
| 218 | +#endif |
0 commit comments