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