2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:19:16 2018 +0000
Revision:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

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