2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 04:20:29 2018 +0000
Revision:
44:ef52682c423e
Parent:
43:1675750cca08
2018.07.26;

Who changed what in which revision?

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