USB Host Library for Sprint Dongles
Fork of USBHostWANDongleSprint by
USB3GModule/WANDongleSerialPort.cpp@0:bfed5767d0a5, 2012-09-12 (annotated)
- Committer:
- donatien
- Date:
- Wed Sep 12 08:15:02 2012 +0000
- Revision:
- 0:bfed5767d0a5
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bfed5767d0a5 | 1 | /* Copyright (c) 2010-2012 mbed.org, MIT License |
donatien | 0:bfed5767d0a5 | 2 | * |
donatien | 0:bfed5767d0a5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 0:bfed5767d0a5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
donatien | 0:bfed5767d0a5 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
donatien | 0:bfed5767d0a5 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
donatien | 0:bfed5767d0a5 | 7 | * Software is furnished to do so, subject to the following conditions: |
donatien | 0:bfed5767d0a5 | 8 | * |
donatien | 0:bfed5767d0a5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 0:bfed5767d0a5 | 10 | * substantial portions of the Software. |
donatien | 0:bfed5767d0a5 | 11 | * |
donatien | 0:bfed5767d0a5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 0:bfed5767d0a5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 0:bfed5767d0a5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 0:bfed5767d0a5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:bfed5767d0a5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 0:bfed5767d0a5 | 17 | */ |
donatien | 0:bfed5767d0a5 | 18 | |
donatien | 0:bfed5767d0a5 | 19 | #define __DEBUG__ 0 |
donatien | 0:bfed5767d0a5 | 20 | #ifndef __MODULE__ |
donatien | 0:bfed5767d0a5 | 21 | #define __MODULE__ "WANDongleSerialPort.cpp" |
donatien | 0:bfed5767d0a5 | 22 | #endif |
donatien | 0:bfed5767d0a5 | 23 | |
donatien | 0:bfed5767d0a5 | 24 | #include "core/dbg.h" |
donatien | 0:bfed5767d0a5 | 25 | #include <cstdint> |
donatien | 0:bfed5767d0a5 | 26 | #include "rtos.h" |
donatien | 0:bfed5767d0a5 | 27 | |
donatien | 0:bfed5767d0a5 | 28 | #include "WANDongleSerialPort.h" |
donatien | 0:bfed5767d0a5 | 29 | |
donatien | 0:bfed5767d0a5 | 30 | WANDongleSerialPort::WANDongleSerialPort() : cb_tx_en(false), cb_rx_en(false), listener(NULL) |
donatien | 0:bfed5767d0a5 | 31 | { |
donatien | 0:bfed5767d0a5 | 32 | reset(); |
donatien | 0:bfed5767d0a5 | 33 | } |
donatien | 0:bfed5767d0a5 | 34 | |
donatien | 0:bfed5767d0a5 | 35 | void WANDongleSerialPort::init(USBHost* pHost) |
donatien | 0:bfed5767d0a5 | 36 | { |
donatien | 0:bfed5767d0a5 | 37 | host = pHost; |
donatien | 0:bfed5767d0a5 | 38 | } |
donatien | 0:bfed5767d0a5 | 39 | |
donatien | 0:bfed5767d0a5 | 40 | void WANDongleSerialPort::reset() |
donatien | 0:bfed5767d0a5 | 41 | { |
donatien | 0:bfed5767d0a5 | 42 | tx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 43 | rx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 44 | |
donatien | 0:bfed5767d0a5 | 45 | bulk_in = NULL; |
donatien | 0:bfed5767d0a5 | 46 | bulk_out = NULL; |
donatien | 0:bfed5767d0a5 | 47 | |
donatien | 0:bfed5767d0a5 | 48 | buf_out_len = 0; |
donatien | 0:bfed5767d0a5 | 49 | max_out_size = 0; |
donatien | 0:bfed5767d0a5 | 50 | lock_tx = false; |
donatien | 0:bfed5767d0a5 | 51 | cb_tx_pending = false; |
donatien | 0:bfed5767d0a5 | 52 | |
donatien | 0:bfed5767d0a5 | 53 | buf_in_len = 0; |
donatien | 0:bfed5767d0a5 | 54 | buf_in_read_pos = 0; |
donatien | 0:bfed5767d0a5 | 55 | lock_rx = false; |
donatien | 0:bfed5767d0a5 | 56 | cb_rx_pending = false; |
donatien | 0:bfed5767d0a5 | 57 | |
donatien | 0:bfed5767d0a5 | 58 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 59 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 60 | } |
donatien | 0:bfed5767d0a5 | 61 | |
donatien | 0:bfed5767d0a5 | 62 | int WANDongleSerialPort::readPacket() |
donatien | 0:bfed5767d0a5 | 63 | { |
donatien | 0:bfed5767d0a5 | 64 | DBG("Read packet on %p", this); |
donatien | 0:bfed5767d0a5 | 65 | rx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 66 | if(lock_rx) |
donatien | 0:bfed5767d0a5 | 67 | { |
donatien | 0:bfed5767d0a5 | 68 | ERR("Fail"); |
donatien | 0:bfed5767d0a5 | 69 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 70 | return -1; |
donatien | 0:bfed5767d0a5 | 71 | } |
donatien | 0:bfed5767d0a5 | 72 | |
donatien | 0:bfed5767d0a5 | 73 | if( bulk_in == NULL ) |
donatien | 0:bfed5767d0a5 | 74 | { |
donatien | 0:bfed5767d0a5 | 75 | WARN("Port is disconnected"); |
donatien | 0:bfed5767d0a5 | 76 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 77 | return -1; |
donatien | 0:bfed5767d0a5 | 78 | } |
donatien | 0:bfed5767d0a5 | 79 | |
donatien | 0:bfed5767d0a5 | 80 | lock_rx = true; //Receiving |
donatien | 0:bfed5767d0a5 | 81 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 82 | // DBG("readPacket"); |
donatien | 0:bfed5767d0a5 | 83 | //lock_rx.lock(); |
donatien | 0:bfed5767d0a5 | 84 | host->lock(); |
donatien | 0:bfed5767d0a5 | 85 | USB_TYPE res = host->bulkRead(dev, (USBEndpoint *)bulk_in, buf_in, ((USBEndpoint *)bulk_in)->getSize(), false); //Queue transfer |
donatien | 0:bfed5767d0a5 | 86 | if(res != USB_TYPE_PROCESSING) |
donatien | 0:bfed5767d0a5 | 87 | { |
donatien | 0:bfed5767d0a5 | 88 | host->unlock(); |
donatien | 0:bfed5767d0a5 | 89 | //lock_rx.unlock(); |
donatien | 0:bfed5767d0a5 | 90 | ERR("host->bulkRead() returned %d", res); |
donatien | 0:bfed5767d0a5 | 91 | Thread::wait(100); |
donatien | 0:bfed5767d0a5 | 92 | return -1; |
donatien | 0:bfed5767d0a5 | 93 | } |
donatien | 0:bfed5767d0a5 | 94 | host->unlock(); |
donatien | 0:bfed5767d0a5 | 95 | return 0; |
donatien | 0:bfed5767d0a5 | 96 | } |
donatien | 0:bfed5767d0a5 | 97 | |
donatien | 0:bfed5767d0a5 | 98 | int WANDongleSerialPort::writePacket() |
donatien | 0:bfed5767d0a5 | 99 | { |
donatien | 0:bfed5767d0a5 | 100 | tx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 101 | if(lock_tx) |
donatien | 0:bfed5767d0a5 | 102 | { |
donatien | 0:bfed5767d0a5 | 103 | ERR("Fail"); |
donatien | 0:bfed5767d0a5 | 104 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 105 | return -1; |
donatien | 0:bfed5767d0a5 | 106 | } |
donatien | 0:bfed5767d0a5 | 107 | |
donatien | 0:bfed5767d0a5 | 108 | if( bulk_out == NULL ) |
donatien | 0:bfed5767d0a5 | 109 | { |
donatien | 0:bfed5767d0a5 | 110 | WARN("Port is disconnected"); |
donatien | 0:bfed5767d0a5 | 111 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 112 | return -1; |
donatien | 0:bfed5767d0a5 | 113 | } |
donatien | 0:bfed5767d0a5 | 114 | |
donatien | 0:bfed5767d0a5 | 115 | lock_tx = true; //Transmitting |
donatien | 0:bfed5767d0a5 | 116 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 117 | // DBG("writePacket"); |
donatien | 0:bfed5767d0a5 | 118 | |
donatien | 0:bfed5767d0a5 | 119 | //lock_tx.lock(); |
donatien | 0:bfed5767d0a5 | 120 | host->lock(); |
donatien | 0:bfed5767d0a5 | 121 | USB_TYPE res = host->bulkWrite(dev, (USBEndpoint *)bulk_out, buf_out, buf_out_len, false); //Queue transfer |
donatien | 0:bfed5767d0a5 | 122 | if(res != USB_TYPE_PROCESSING) |
donatien | 0:bfed5767d0a5 | 123 | { |
donatien | 0:bfed5767d0a5 | 124 | host->unlock(); |
donatien | 0:bfed5767d0a5 | 125 | //lock_tx.unlock(); |
donatien | 0:bfed5767d0a5 | 126 | ERR("host->bulkWrite() returned %d", res); |
donatien | 0:bfed5767d0a5 | 127 | Thread::wait(100); |
donatien | 0:bfed5767d0a5 | 128 | return -1; |
donatien | 0:bfed5767d0a5 | 129 | } |
donatien | 0:bfed5767d0a5 | 130 | host->unlock(); |
donatien | 0:bfed5767d0a5 | 131 | return 0; |
donatien | 0:bfed5767d0a5 | 132 | } |
donatien | 0:bfed5767d0a5 | 133 | |
donatien | 0:bfed5767d0a5 | 134 | int WANDongleSerialPort::putc(int c) |
donatien | 0:bfed5767d0a5 | 135 | { |
donatien | 0:bfed5767d0a5 | 136 | tx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 137 | if(!lock_tx) |
donatien | 0:bfed5767d0a5 | 138 | { |
donatien | 0:bfed5767d0a5 | 139 | if(buf_out_len < max_out_size) |
donatien | 0:bfed5767d0a5 | 140 | { |
donatien | 0:bfed5767d0a5 | 141 | buf_out[buf_out_len] = (uint8_t)c; |
donatien | 0:bfed5767d0a5 | 142 | buf_out_len++; |
donatien | 0:bfed5767d0a5 | 143 | } |
donatien | 0:bfed5767d0a5 | 144 | } |
donatien | 0:bfed5767d0a5 | 145 | else |
donatien | 0:bfed5767d0a5 | 146 | { |
donatien | 0:bfed5767d0a5 | 147 | ERR("CAN'T WRITE!"); |
donatien | 0:bfed5767d0a5 | 148 | } |
donatien | 0:bfed5767d0a5 | 149 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 150 | return c; |
donatien | 0:bfed5767d0a5 | 151 | } |
donatien | 0:bfed5767d0a5 | 152 | |
donatien | 0:bfed5767d0a5 | 153 | int WANDongleSerialPort::getc() |
donatien | 0:bfed5767d0a5 | 154 | { |
donatien | 0:bfed5767d0a5 | 155 | rx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 156 | int c = 0; |
donatien | 0:bfed5767d0a5 | 157 | if(!lock_rx) |
donatien | 0:bfed5767d0a5 | 158 | { |
donatien | 0:bfed5767d0a5 | 159 | if(buf_in_read_pos < buf_in_len) |
donatien | 0:bfed5767d0a5 | 160 | { |
donatien | 0:bfed5767d0a5 | 161 | c = (int)buf_in[buf_in_read_pos]; |
donatien | 0:bfed5767d0a5 | 162 | buf_in_read_pos++; |
donatien | 0:bfed5767d0a5 | 163 | } |
donatien | 0:bfed5767d0a5 | 164 | } |
donatien | 0:bfed5767d0a5 | 165 | else |
donatien | 0:bfed5767d0a5 | 166 | { |
donatien | 0:bfed5767d0a5 | 167 | ERR("CAN'T READ!"); |
donatien | 0:bfed5767d0a5 | 168 | } |
donatien | 0:bfed5767d0a5 | 169 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 170 | return c; |
donatien | 0:bfed5767d0a5 | 171 | } |
donatien | 0:bfed5767d0a5 | 172 | |
donatien | 0:bfed5767d0a5 | 173 | int WANDongleSerialPort::readable() |
donatien | 0:bfed5767d0a5 | 174 | { |
donatien | 0:bfed5767d0a5 | 175 | rx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 176 | if (lock_rx) |
donatien | 0:bfed5767d0a5 | 177 | { |
donatien | 0:bfed5767d0a5 | 178 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 179 | return 0; |
donatien | 0:bfed5767d0a5 | 180 | } |
donatien | 0:bfed5767d0a5 | 181 | |
donatien | 0:bfed5767d0a5 | 182 | /* if( !lock_rx.trylock() ) |
donatien | 0:bfed5767d0a5 | 183 | { |
donatien | 0:bfed5767d0a5 | 184 | return 0; |
donatien | 0:bfed5767d0a5 | 185 | }*/ |
donatien | 0:bfed5767d0a5 | 186 | int res = buf_in_len - buf_in_read_pos; |
donatien | 0:bfed5767d0a5 | 187 | //lock_rx.unlock(); |
donatien | 0:bfed5767d0a5 | 188 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 189 | return res; |
donatien | 0:bfed5767d0a5 | 190 | } |
donatien | 0:bfed5767d0a5 | 191 | |
donatien | 0:bfed5767d0a5 | 192 | int WANDongleSerialPort::writeable() |
donatien | 0:bfed5767d0a5 | 193 | { |
donatien | 0:bfed5767d0a5 | 194 | tx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 195 | if (lock_tx) |
donatien | 0:bfed5767d0a5 | 196 | { |
donatien | 0:bfed5767d0a5 | 197 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 198 | return 0; |
donatien | 0:bfed5767d0a5 | 199 | } |
donatien | 0:bfed5767d0a5 | 200 | |
donatien | 0:bfed5767d0a5 | 201 | /*if( !lock_tx.trylock() ) |
donatien | 0:bfed5767d0a5 | 202 | { |
donatien | 0:bfed5767d0a5 | 203 | return 0; |
donatien | 0:bfed5767d0a5 | 204 | }*/ |
donatien | 0:bfed5767d0a5 | 205 | int res = max_out_size - buf_out_len; |
donatien | 0:bfed5767d0a5 | 206 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 207 | //lock_tx.unlock(); |
donatien | 0:bfed5767d0a5 | 208 | return res; |
donatien | 0:bfed5767d0a5 | 209 | } |
donatien | 0:bfed5767d0a5 | 210 | |
donatien | 0:bfed5767d0a5 | 211 | void WANDongleSerialPort::attach(IUSBHostSerialListener* pListener) |
donatien | 0:bfed5767d0a5 | 212 | { |
donatien | 0:bfed5767d0a5 | 213 | if(pListener == NULL) |
donatien | 0:bfed5767d0a5 | 214 | { |
donatien | 0:bfed5767d0a5 | 215 | setupIrq(false, RxIrq); |
donatien | 0:bfed5767d0a5 | 216 | setupIrq(false, TxIrq); |
donatien | 0:bfed5767d0a5 | 217 | } |
donatien | 0:bfed5767d0a5 | 218 | listener = pListener; |
donatien | 0:bfed5767d0a5 | 219 | if(pListener != NULL) |
donatien | 0:bfed5767d0a5 | 220 | { |
donatien | 0:bfed5767d0a5 | 221 | setupIrq(true, RxIrq); |
donatien | 0:bfed5767d0a5 | 222 | setupIrq(true, TxIrq); |
donatien | 0:bfed5767d0a5 | 223 | } |
donatien | 0:bfed5767d0a5 | 224 | } |
donatien | 0:bfed5767d0a5 | 225 | |
donatien | 0:bfed5767d0a5 | 226 | void WANDongleSerialPort::setupIrq(bool en, IrqType irq /*= RxIrq*/) |
donatien | 0:bfed5767d0a5 | 227 | { |
donatien | 0:bfed5767d0a5 | 228 | switch(irq) |
donatien | 0:bfed5767d0a5 | 229 | { |
donatien | 0:bfed5767d0a5 | 230 | case RxIrq: |
donatien | 0:bfed5767d0a5 | 231 | rx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 232 | cb_rx_en = en; |
donatien | 0:bfed5767d0a5 | 233 | if(en && cb_rx_pending) |
donatien | 0:bfed5767d0a5 | 234 | { |
donatien | 0:bfed5767d0a5 | 235 | cb_rx_pending = false; |
donatien | 0:bfed5767d0a5 | 236 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 237 | listener->readable(); //Process the interrupt that was raised |
donatien | 0:bfed5767d0a5 | 238 | } |
donatien | 0:bfed5767d0a5 | 239 | else |
donatien | 0:bfed5767d0a5 | 240 | { |
donatien | 0:bfed5767d0a5 | 241 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 242 | } |
donatien | 0:bfed5767d0a5 | 243 | break; |
donatien | 0:bfed5767d0a5 | 244 | case TxIrq: |
donatien | 0:bfed5767d0a5 | 245 | tx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 246 | cb_tx_en = en; |
donatien | 0:bfed5767d0a5 | 247 | if(en && cb_tx_pending) |
donatien | 0:bfed5767d0a5 | 248 | { |
donatien | 0:bfed5767d0a5 | 249 | cb_tx_pending = false; |
donatien | 0:bfed5767d0a5 | 250 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 251 | listener->writeable(); //Process the interrupt that was raised |
donatien | 0:bfed5767d0a5 | 252 | } |
donatien | 0:bfed5767d0a5 | 253 | else |
donatien | 0:bfed5767d0a5 | 254 | { |
donatien | 0:bfed5767d0a5 | 255 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 256 | } |
donatien | 0:bfed5767d0a5 | 257 | break; |
donatien | 0:bfed5767d0a5 | 258 | } |
donatien | 0:bfed5767d0a5 | 259 | } |
donatien | 0:bfed5767d0a5 | 260 | |
donatien | 0:bfed5767d0a5 | 261 | |
donatien | 0:bfed5767d0a5 | 262 | void WANDongleSerialPort::connect( USBDeviceConnected* pDev, USBEndpoint* pInEp, USBEndpoint* pOutEp ) |
donatien | 0:bfed5767d0a5 | 263 | { |
donatien | 0:bfed5767d0a5 | 264 | dev = pDev; |
donatien | 0:bfed5767d0a5 | 265 | bulk_in = pInEp; |
donatien | 0:bfed5767d0a5 | 266 | bulk_out = pOutEp; |
donatien | 0:bfed5767d0a5 | 267 | max_out_size = bulk_out->getSize(); |
donatien | 0:bfed5767d0a5 | 268 | if( max_out_size > WANDONGLE_MAX_OUTEP_SIZE ) |
donatien | 0:bfed5767d0a5 | 269 | { |
donatien | 0:bfed5767d0a5 | 270 | max_out_size = WANDONGLE_MAX_OUTEP_SIZE; |
donatien | 0:bfed5767d0a5 | 271 | } |
donatien | 0:bfed5767d0a5 | 272 | bulk_in->attach(this, &WANDongleSerialPort::rxHandler); |
donatien | 0:bfed5767d0a5 | 273 | bulk_out->attach(this, &WANDongleSerialPort::txHandler); |
donatien | 0:bfed5767d0a5 | 274 | readPacket(); //Start receiving data |
donatien | 0:bfed5767d0a5 | 275 | } |
donatien | 0:bfed5767d0a5 | 276 | |
donatien | 0:bfed5767d0a5 | 277 | void WANDongleSerialPort::disconnect( ) |
donatien | 0:bfed5767d0a5 | 278 | { |
donatien | 0:bfed5767d0a5 | 279 | reset(); |
donatien | 0:bfed5767d0a5 | 280 | } |
donatien | 0:bfed5767d0a5 | 281 | |
donatien | 0:bfed5767d0a5 | 282 | //Private methods |
donatien | 0:bfed5767d0a5 | 283 | |
donatien | 0:bfed5767d0a5 | 284 | |
donatien | 0:bfed5767d0a5 | 285 | void WANDongleSerialPort::rxHandler() |
donatien | 0:bfed5767d0a5 | 286 | { |
donatien | 0:bfed5767d0a5 | 287 | if (((USBEndpoint *) bulk_in)->getState() == USB_TYPE_IDLE) //Success |
donatien | 0:bfed5767d0a5 | 288 | { |
donatien | 0:bfed5767d0a5 | 289 | buf_in_read_pos = 0; |
donatien | 0:bfed5767d0a5 | 290 | buf_in_len = ((USBEndpoint *) bulk_in)->getLengthTransferred(); //Update length |
donatien | 0:bfed5767d0a5 | 291 | //lock_rx.unlock(); |
donatien | 0:bfed5767d0a5 | 292 | rx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 293 | lock_rx = false; //Transmission complete |
donatien | 0:bfed5767d0a5 | 294 | if(cb_rx_en) |
donatien | 0:bfed5767d0a5 | 295 | { |
donatien | 0:bfed5767d0a5 | 296 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 297 | listener->readable(); //Call handler from the IRQ context |
donatien | 0:bfed5767d0a5 | 298 | //readPacket() should be called by the handler subsequently once the buffer has been emptied |
donatien | 0:bfed5767d0a5 | 299 | } |
donatien | 0:bfed5767d0a5 | 300 | else |
donatien | 0:bfed5767d0a5 | 301 | { |
donatien | 0:bfed5767d0a5 | 302 | cb_rx_pending = true; //Queue the callback |
donatien | 0:bfed5767d0a5 | 303 | rx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 304 | } |
donatien | 0:bfed5767d0a5 | 305 | |
donatien | 0:bfed5767d0a5 | 306 | } |
donatien | 0:bfed5767d0a5 | 307 | else //Error, try reading again |
donatien | 0:bfed5767d0a5 | 308 | { |
donatien | 0:bfed5767d0a5 | 309 | //lock_rx.unlock(); |
donatien | 0:bfed5767d0a5 | 310 | DBG("Trying again"); |
donatien | 0:bfed5767d0a5 | 311 | readPacket(); |
donatien | 0:bfed5767d0a5 | 312 | } |
donatien | 0:bfed5767d0a5 | 313 | } |
donatien | 0:bfed5767d0a5 | 314 | |
donatien | 0:bfed5767d0a5 | 315 | void WANDongleSerialPort::txHandler() |
donatien | 0:bfed5767d0a5 | 316 | { |
donatien | 0:bfed5767d0a5 | 317 | if (((USBEndpoint *) bulk_out)->getState() == USB_TYPE_IDLE) //Success |
donatien | 0:bfed5767d0a5 | 318 | { |
donatien | 0:bfed5767d0a5 | 319 | tx_mtx.lock(); |
donatien | 0:bfed5767d0a5 | 320 | buf_out_len = 0; //Reset length |
donatien | 0:bfed5767d0a5 | 321 | lock_tx = false; //Transmission complete |
donatien | 0:bfed5767d0a5 | 322 | //lock_tx.unlock(); |
donatien | 0:bfed5767d0a5 | 323 | if(cb_tx_en) |
donatien | 0:bfed5767d0a5 | 324 | { |
donatien | 0:bfed5767d0a5 | 325 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 326 | listener->writeable(); //Call handler from the IRQ context |
donatien | 0:bfed5767d0a5 | 327 | //writePacket() should be called by the handler subsequently once the buffer has been filled |
donatien | 0:bfed5767d0a5 | 328 | } |
donatien | 0:bfed5767d0a5 | 329 | else |
donatien | 0:bfed5767d0a5 | 330 | { |
donatien | 0:bfed5767d0a5 | 331 | cb_tx_pending = true; //Queue the callback |
donatien | 0:bfed5767d0a5 | 332 | tx_mtx.unlock(); |
donatien | 0:bfed5767d0a5 | 333 | } |
donatien | 0:bfed5767d0a5 | 334 | } |
donatien | 0:bfed5767d0a5 | 335 | else //Error, try reading again |
donatien | 0:bfed5767d0a5 | 336 | { |
donatien | 0:bfed5767d0a5 | 337 | //lock_tx.unlock(); |
donatien | 0:bfed5767d0a5 | 338 | writePacket(); |
donatien | 0:bfed5767d0a5 | 339 | } |
donatien | 0:bfed5767d0a5 | 340 | } |