mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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