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