Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 5:3f93dd1d4cb3 1 /* USBSerialStream.cpp */
sam_grove 5:3f93dd1d4cb3 2 /* Copyright (C) 2012 mbed.org, MIT License
sam_grove 5:3f93dd1d4cb3 3 *
sam_grove 5:3f93dd1d4cb3 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
sam_grove 5:3f93dd1d4cb3 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
sam_grove 5:3f93dd1d4cb3 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
sam_grove 5:3f93dd1d4cb3 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
sam_grove 5:3f93dd1d4cb3 8 * furnished to do so, subject to the following conditions:
sam_grove 5:3f93dd1d4cb3 9 *
sam_grove 5:3f93dd1d4cb3 10 * The above copyright notice and this permission notice shall be included in all copies or
sam_grove 5:3f93dd1d4cb3 11 * substantial portions of the Software.
sam_grove 5:3f93dd1d4cb3 12 *
sam_grove 5:3f93dd1d4cb3 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
sam_grove 5:3f93dd1d4cb3 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
sam_grove 5:3f93dd1d4cb3 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
sam_grove 5:3f93dd1d4cb3 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sam_grove 5:3f93dd1d4cb3 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sam_grove 5:3f93dd1d4cb3 18 */
sam_grove 5:3f93dd1d4cb3 19
sam_grove 5:3f93dd1d4cb3 20 #define __DEBUG__ 0 //Maximum verbosity
sam_grove 5:3f93dd1d4cb3 21 #ifndef __MODULE__
sam_grove 5:3f93dd1d4cb3 22 #define __MODULE__ "USBSerialStream.cpp"
sam_grove 5:3f93dd1d4cb3 23 #endif
sam_grove 5:3f93dd1d4cb3 24
sam_grove 5:3f93dd1d4cb3 25 #include "core/fwk.h"
sam_grove 5:3f93dd1d4cb3 26
sam_grove 5:3f93dd1d4cb3 27 #include <stdio.h>
sam_grove 5:3f93dd1d4cb3 28
sam_grove 5:3f93dd1d4cb3 29 #include "USBSerialStream.h"
sam_grove 5:3f93dd1d4cb3 30
sam_grove 5:3f93dd1d4cb3 31
sam_grove 5:3f93dd1d4cb3 32 USBSerialStream::USBSerialStream(IUSBHostSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
sam_grove 5:3f93dd1d4cb3 33 m_availableSphre(1), m_spaceSphre(1), m_inBuf()
sam_grove 5:3f93dd1d4cb3 34 {
sam_grove 5:3f93dd1d4cb3 35 m_availableSphre.wait();
sam_grove 5:3f93dd1d4cb3 36 m_spaceSphre.wait();
sam_grove 5:3f93dd1d4cb3 37 //Attach interrupts
sam_grove 5:3f93dd1d4cb3 38 m_serial.attach(this);
sam_grove 5:3f93dd1d4cb3 39 }
sam_grove 5:3f93dd1d4cb3 40
sam_grove 5:3f93dd1d4cb3 41 /*virtual*/ USBSerialStream::~USBSerialStream()
sam_grove 5:3f93dd1d4cb3 42 {
sam_grove 5:3f93dd1d4cb3 43 m_serial.attach(NULL);
sam_grove 5:3f93dd1d4cb3 44 }
sam_grove 5:3f93dd1d4cb3 45
sam_grove 5:3f93dd1d4cb3 46 //0 for non-blocking (returns immediately), -1 for infinite blocking
sam_grove 5:3f93dd1d4cb3 47 /*virtual*/ int USBSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
sam_grove 5:3f93dd1d4cb3 48 {
sam_grove 5:3f93dd1d4cb3 49 DBG("Trying to read at most %d chars", maxLength);
sam_grove 5:3f93dd1d4cb3 50 int ret = waitAvailable(timeout);
sam_grove 5:3f93dd1d4cb3 51 if(ret)
sam_grove 5:3f93dd1d4cb3 52 {
sam_grove 5:3f93dd1d4cb3 53 WARN("Error %d while waiting for incoming data", ret);
sam_grove 5:3f93dd1d4cb3 54 return ret;
sam_grove 5:3f93dd1d4cb3 55 }
sam_grove 5:3f93dd1d4cb3 56 int a = available(); //Prevent macro issues
sam_grove 5:3f93dd1d4cb3 57 int readLen = MIN( a, maxLength );
sam_grove 5:3f93dd1d4cb3 58 *pLength = readLen;
sam_grove 5:3f93dd1d4cb3 59
sam_grove 5:3f93dd1d4cb3 60 setupReadableISR(false);
sam_grove 5:3f93dd1d4cb3 61 while(readLen--)
sam_grove 5:3f93dd1d4cb3 62 {
sam_grove 5:3f93dd1d4cb3 63 m_inBuf.dequeue(buf);
sam_grove 5:3f93dd1d4cb3 64 buf++;
sam_grove 5:3f93dd1d4cb3 65 }
sam_grove 5:3f93dd1d4cb3 66 setupReadableISR(true);
sam_grove 5:3f93dd1d4cb3 67 DBG("Read %d chars successfully", *pLength);
sam_grove 5:3f93dd1d4cb3 68 return OK;
sam_grove 5:3f93dd1d4cb3 69 }
sam_grove 5:3f93dd1d4cb3 70
sam_grove 5:3f93dd1d4cb3 71 /*virtual*/ size_t USBSerialStream::available()
sam_grove 5:3f93dd1d4cb3 72 {
sam_grove 5:3f93dd1d4cb3 73 setupReadableISR(false); //m_inBuf.available() is not reentrant
sam_grove 5:3f93dd1d4cb3 74 size_t len = m_inBuf.available();
sam_grove 5:3f93dd1d4cb3 75 setupReadableISR(true);
sam_grove 5:3f93dd1d4cb3 76 return len;
sam_grove 5:3f93dd1d4cb3 77 }
sam_grove 5:3f93dd1d4cb3 78
sam_grove 5:3f93dd1d4cb3 79 /*virtual*/ int USBSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
sam_grove 5:3f93dd1d4cb3 80 {
sam_grove 5:3f93dd1d4cb3 81 int ret;
sam_grove 5:3f93dd1d4cb3 82 if(available()) //Is data already available?
sam_grove 5:3f93dd1d4cb3 83 {
sam_grove 5:3f93dd1d4cb3 84 while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
sam_grove 5:3f93dd1d4cb3 85 return OK;
sam_grove 5:3f93dd1d4cb3 86 }
sam_grove 5:3f93dd1d4cb3 87
sam_grove 5:3f93dd1d4cb3 88 DBG("Waiting for data availability %d ms (-1 is infinite)", timeout);
sam_grove 5:3f93dd1d4cb3 89 ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort
sam_grove 5:3f93dd1d4cb3 90 if(ret <= 0)
sam_grove 5:3f93dd1d4cb3 91 {
sam_grove 5:3f93dd1d4cb3 92 DBG("Timeout");
sam_grove 5:3f93dd1d4cb3 93 return NET_TIMEOUT;
sam_grove 5:3f93dd1d4cb3 94 }
sam_grove 5:3f93dd1d4cb3 95 if(!m_inBuf.available()) //Even if abort has been called, return that data is available
sam_grove 5:3f93dd1d4cb3 96 {
sam_grove 5:3f93dd1d4cb3 97 DBG("Aborted");
sam_grove 5:3f93dd1d4cb3 98 return NET_INTERRUPTED;
sam_grove 5:3f93dd1d4cb3 99 }
sam_grove 5:3f93dd1d4cb3 100 DBG("Finished waiting");
sam_grove 5:3f93dd1d4cb3 101 while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
sam_grove 5:3f93dd1d4cb3 102 return OK;
sam_grove 5:3f93dd1d4cb3 103 }
sam_grove 5:3f93dd1d4cb3 104
sam_grove 5:3f93dd1d4cb3 105 /*virtual*/ int USBSerialStream::abortRead() //Abort current reading (or waiting) operation
sam_grove 5:3f93dd1d4cb3 106 {
sam_grove 5:3f93dd1d4cb3 107 if( /*!available()*/true ) //If there is data pending, no need to abort
sam_grove 5:3f93dd1d4cb3 108 {
sam_grove 5:3f93dd1d4cb3 109 m_availableSphre.release(); //Force exiting the waiting state
sam_grove 5:3f93dd1d4cb3 110 }
sam_grove 5:3f93dd1d4cb3 111 else
sam_grove 5:3f93dd1d4cb3 112 {
sam_grove 5:3f93dd1d4cb3 113 DBG("Serial is readable"); ;
sam_grove 5:3f93dd1d4cb3 114 }
sam_grove 5:3f93dd1d4cb3 115 return OK;
sam_grove 5:3f93dd1d4cb3 116 }
sam_grove 5:3f93dd1d4cb3 117
sam_grove 5:3f93dd1d4cb3 118 void USBSerialStream::setupReadableISR(bool en)
sam_grove 5:3f93dd1d4cb3 119 {
sam_grove 5:3f93dd1d4cb3 120 m_serial.setupIrq(en, IUSBHostSerial::RxIrq);
sam_grove 5:3f93dd1d4cb3 121 }
sam_grove 5:3f93dd1d4cb3 122
sam_grove 5:3f93dd1d4cb3 123 void USBSerialStream::readable() //Callback from m_serial when new data is available
sam_grove 5:3f93dd1d4cb3 124 {
sam_grove 5:3f93dd1d4cb3 125 while(m_serial.readable())
sam_grove 5:3f93dd1d4cb3 126 {
sam_grove 5:3f93dd1d4cb3 127 m_inBuf.queue(m_serial.getc());
sam_grove 5:3f93dd1d4cb3 128 }
sam_grove 5:3f93dd1d4cb3 129 m_serial.readPacket(); //Start read of next packet
sam_grove 5:3f93dd1d4cb3 130 m_availableSphre.release(); //Force exiting the waiting state
sam_grove 5:3f93dd1d4cb3 131 }
sam_grove 5:3f93dd1d4cb3 132
sam_grove 5:3f93dd1d4cb3 133 //0 for non-blocking (returns immediately), -1 for infinite blocking
sam_grove 5:3f93dd1d4cb3 134 /*virtual*/ int USBSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=-1*/)
sam_grove 5:3f93dd1d4cb3 135 {
sam_grove 5:3f93dd1d4cb3 136 DBG("Trying to write %d chars", length);
sam_grove 5:3f93dd1d4cb3 137 do
sam_grove 5:3f93dd1d4cb3 138 {
sam_grove 5:3f93dd1d4cb3 139 int ret = waitSpace(timeout);
sam_grove 5:3f93dd1d4cb3 140 if(ret)
sam_grove 5:3f93dd1d4cb3 141 {
sam_grove 5:3f93dd1d4cb3 142 WARN("Error %d while waiting for space", ret);
sam_grove 5:3f93dd1d4cb3 143 return ret;
sam_grove 5:3f93dd1d4cb3 144 }
sam_grove 5:3f93dd1d4cb3 145 int s = space(); //Prevent macro issues
sam_grove 5:3f93dd1d4cb3 146 int writeLen = MIN( s, length );
sam_grove 5:3f93dd1d4cb3 147 DBG("Writing %d chars", writeLen);
sam_grove 5:3f93dd1d4cb3 148 setupWriteableISR(false);
sam_grove 5:3f93dd1d4cb3 149 while(writeLen)
sam_grove 5:3f93dd1d4cb3 150 {
sam_grove 5:3f93dd1d4cb3 151 m_outBuf.queue(*buf);
sam_grove 5:3f93dd1d4cb3 152 buf++;
sam_grove 5:3f93dd1d4cb3 153 length--;
sam_grove 5:3f93dd1d4cb3 154 writeLen--;
sam_grove 5:3f93dd1d4cb3 155 }
sam_grove 5:3f93dd1d4cb3 156 //If m_serial tx fifo is empty we need to start the packet write
sam_grove 5:3f93dd1d4cb3 157 if( m_outBuf.available() && m_serialTxFifoEmpty )
sam_grove 5:3f93dd1d4cb3 158 {
sam_grove 5:3f93dd1d4cb3 159 writeable();
sam_grove 5:3f93dd1d4cb3 160 }
sam_grove 5:3f93dd1d4cb3 161 setupWriteableISR(true);
sam_grove 5:3f93dd1d4cb3 162 } while(length);
sam_grove 5:3f93dd1d4cb3 163
sam_grove 5:3f93dd1d4cb3 164 DBG("Write successful");
sam_grove 5:3f93dd1d4cb3 165 return OK;
sam_grove 5:3f93dd1d4cb3 166 }
sam_grove 5:3f93dd1d4cb3 167
sam_grove 5:3f93dd1d4cb3 168 /*virtual*/ size_t USBSerialStream::space()
sam_grove 5:3f93dd1d4cb3 169 {
sam_grove 5:3f93dd1d4cb3 170 setupWriteableISR(false); //m_outBuf.available() is not reentrant
sam_grove 5:3f93dd1d4cb3 171 size_t len = CIRCBUF_SIZE - m_outBuf.available();
sam_grove 5:3f93dd1d4cb3 172 setupWriteableISR(true);
sam_grove 5:3f93dd1d4cb3 173 return len;
sam_grove 5:3f93dd1d4cb3 174 }
sam_grove 5:3f93dd1d4cb3 175
sam_grove 5:3f93dd1d4cb3 176 /*virtual*/ int USBSerialStream::waitSpace(uint32_t timeout/*=-1*/) //Wait for space to be available
sam_grove 5:3f93dd1d4cb3 177 {
sam_grove 5:3f93dd1d4cb3 178 int ret;
sam_grove 5:3f93dd1d4cb3 179 if(space()) //Is still space already left?
sam_grove 5:3f93dd1d4cb3 180 {
sam_grove 5:3f93dd1d4cb3 181 while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
sam_grove 5:3f93dd1d4cb3 182 return OK;
sam_grove 5:3f93dd1d4cb3 183 }
sam_grove 5:3f93dd1d4cb3 184
sam_grove 5:3f93dd1d4cb3 185 DBG("Waiting for data space %d ms (-1 is infinite)", timeout);
sam_grove 5:3f93dd1d4cb3 186 ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort
sam_grove 5:3f93dd1d4cb3 187 if(ret <= 0)
sam_grove 5:3f93dd1d4cb3 188 {
sam_grove 5:3f93dd1d4cb3 189 DBG("Timeout");
sam_grove 5:3f93dd1d4cb3 190 return NET_TIMEOUT;
sam_grove 5:3f93dd1d4cb3 191 }
sam_grove 5:3f93dd1d4cb3 192 if(!space()) //Even if abort has been called, return that space is available
sam_grove 5:3f93dd1d4cb3 193 {
sam_grove 5:3f93dd1d4cb3 194 DBG("Aborted");
sam_grove 5:3f93dd1d4cb3 195 return NET_INTERRUPTED;
sam_grove 5:3f93dd1d4cb3 196 }
sam_grove 5:3f93dd1d4cb3 197 while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
sam_grove 5:3f93dd1d4cb3 198 return OK;
sam_grove 5:3f93dd1d4cb3 199 }
sam_grove 5:3f93dd1d4cb3 200
sam_grove 5:3f93dd1d4cb3 201 /*virtual*/ int USBSerialStream::abortWrite() //Abort current writing (or waiting) operation
sam_grove 5:3f93dd1d4cb3 202 {
sam_grove 5:3f93dd1d4cb3 203 if( !space() ) //If there is space left, no need to abort
sam_grove 5:3f93dd1d4cb3 204 {
sam_grove 5:3f93dd1d4cb3 205 m_spaceSphre.release(); //Force exiting the waiting state
sam_grove 5:3f93dd1d4cb3 206 }
sam_grove 5:3f93dd1d4cb3 207 return OK;
sam_grove 5:3f93dd1d4cb3 208 }
sam_grove 5:3f93dd1d4cb3 209
sam_grove 5:3f93dd1d4cb3 210 void USBSerialStream::setupWriteableISR(bool en)
sam_grove 5:3f93dd1d4cb3 211 {
sam_grove 5:3f93dd1d4cb3 212 m_serial.setupIrq(en, IUSBHostSerial::TxIrq);
sam_grove 5:3f93dd1d4cb3 213 }
sam_grove 5:3f93dd1d4cb3 214
sam_grove 5:3f93dd1d4cb3 215 void USBSerialStream::writeable() //Callback from m_serial when new space is available
sam_grove 5:3f93dd1d4cb3 216 {
sam_grove 5:3f93dd1d4cb3 217 if(m_outBuf.isEmpty())
sam_grove 5:3f93dd1d4cb3 218 {
sam_grove 5:3f93dd1d4cb3 219 m_serialTxFifoEmpty = true;
sam_grove 5:3f93dd1d4cb3 220 }
sam_grove 5:3f93dd1d4cb3 221 else
sam_grove 5:3f93dd1d4cb3 222 {
sam_grove 5:3f93dd1d4cb3 223 m_serialTxFifoEmpty = false;
sam_grove 5:3f93dd1d4cb3 224 while(m_serial.writeable() && !m_outBuf.isEmpty())
sam_grove 5:3f93dd1d4cb3 225 {
sam_grove 5:3f93dd1d4cb3 226 uint8_t c;
sam_grove 5:3f93dd1d4cb3 227 m_outBuf.dequeue(&c);
sam_grove 5:3f93dd1d4cb3 228 m_serial.putc((char)c);
sam_grove 5:3f93dd1d4cb3 229 }
sam_grove 5:3f93dd1d4cb3 230 m_serial.writePacket(); //Start packet write
sam_grove 5:3f93dd1d4cb3 231 }
sam_grove 5:3f93dd1d4cb3 232 if(!m_outBuf.isFull())
sam_grove 5:3f93dd1d4cb3 233 {
sam_grove 5:3f93dd1d4cb3 234 m_spaceSphre.release(); //Force exiting the waiting state
sam_grove 5:3f93dd1d4cb3 235 }
sam_grove 5:3f93dd1d4cb3 236 }