forked for unsupported target

Dependents:   stm32-disco-example DISCO-F469NI_BD_SD_Card_Control

Fork of USBHOST by ST

Committer:
frq08711@LMECWL0871.LME.ST.COM
Date:
Wed Feb 15 10:49:44 2017 +0100
Revision:
1:ab240722d7ef
update to mbed 5.3.5

Who changed what in which revision?

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