Working version on STM32F411. Test for CLI

Dependencies:   FATFileSystem mbed-rtos mbed

Fork of USBHost by mbed official

Committer:
mbed_official
Date:
Thu Oct 17 10:15:19 2013 +0100
Revision:
18:37c948cf0dbf
Parent:
17:c7b1b8451598
Child:
24:868cbfe611a7
Synchronized with git revision 7585a23e6c29c41da4793a46a746ac3ec743b780

Who changed what in which revision?

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