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 /* IOSerialStream.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__ "IOSerialStream.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 "IOSerialStream.h"
sam_grove 5:3f93dd1d4cb3 30
sam_grove 5:3f93dd1d4cb3 31 IOSerialStream::IOSerialStream(mbed::Serial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
sam_grove 5:3f93dd1d4cb3 32 m_availableSphre(1), m_spaceSphre(1), m_inBuf(), m_outBuf()
sam_grove 5:3f93dd1d4cb3 33 {
sam_grove 5:3f93dd1d4cb3 34 m_availableSphre.wait();
sam_grove 5:3f93dd1d4cb3 35 m_spaceSphre.wait();
sam_grove 5:3f93dd1d4cb3 36 //Attach interrupts
sam_grove 5:3f93dd1d4cb3 37 m_serial.attach(this, &IOSerialStream::readable, mbed::Serial::RxIrq);
sam_grove 5:3f93dd1d4cb3 38 m_serial.attach(this, &IOSerialStream::writeable, mbed::Serial::TxIrq);
sam_grove 5:3f93dd1d4cb3 39 }
sam_grove 5:3f93dd1d4cb3 40
sam_grove 5:3f93dd1d4cb3 41 /*virtual*/ IOSerialStream::~IOSerialStream()
sam_grove 5:3f93dd1d4cb3 42 {
sam_grove 5:3f93dd1d4cb3 43 m_serial.attach(NULL, mbed::Serial::RxIrq);
sam_grove 5:3f93dd1d4cb3 44 m_serial.attach(NULL, mbed::Serial::TxIrq);
sam_grove 5:3f93dd1d4cb3 45 }
sam_grove 5:3f93dd1d4cb3 46
sam_grove 5:3f93dd1d4cb3 47 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
sam_grove 5:3f93dd1d4cb3 48 /*virtual*/ int IOSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
sam_grove 5:3f93dd1d4cb3 49 {
sam_grove 5:3f93dd1d4cb3 50 DBG("Trying to read at most %d chars", maxLength);
sam_grove 5:3f93dd1d4cb3 51 int ret = waitAvailable(timeout);
sam_grove 5:3f93dd1d4cb3 52 if(ret)
sam_grove 5:3f93dd1d4cb3 53 {
sam_grove 5:3f93dd1d4cb3 54 WARN("Error %d while waiting for incoming data", ret);
sam_grove 5:3f93dd1d4cb3 55 return ret;
sam_grove 5:3f93dd1d4cb3 56 }
sam_grove 5:3f93dd1d4cb3 57 int readLen = MIN( available(), maxLength );
sam_grove 5:3f93dd1d4cb3 58 *pLength = readLen;
sam_grove 5:3f93dd1d4cb3 59 setupReadableISR(false);
sam_grove 5:3f93dd1d4cb3 60 while(readLen--)
sam_grove 5:3f93dd1d4cb3 61 {
sam_grove 5:3f93dd1d4cb3 62 m_inBuf.dequeue(buf);
sam_grove 5:3f93dd1d4cb3 63 buf++;
sam_grove 5:3f93dd1d4cb3 64 }
sam_grove 5:3f93dd1d4cb3 65 setupReadableISR(true);
sam_grove 5:3f93dd1d4cb3 66 DBG("Read %d chars successfully", *pLength);
sam_grove 5:3f93dd1d4cb3 67 return OK;
sam_grove 5:3f93dd1d4cb3 68 }
sam_grove 5:3f93dd1d4cb3 69
sam_grove 5:3f93dd1d4cb3 70 /*virtual*/ size_t IOSerialStream::available()
sam_grove 5:3f93dd1d4cb3 71 {
sam_grove 5:3f93dd1d4cb3 72 setupReadableISR(false); //m_inBuf.available() is not reentrant
sam_grove 5:3f93dd1d4cb3 73 size_t len = m_inBuf.available();
sam_grove 5:3f93dd1d4cb3 74 setupReadableISR(true);
sam_grove 5:3f93dd1d4cb3 75 return len;
sam_grove 5:3f93dd1d4cb3 76 }
sam_grove 5:3f93dd1d4cb3 77
sam_grove 5:3f93dd1d4cb3 78 /*virtual*/ int IOSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
sam_grove 5:3f93dd1d4cb3 79 {
sam_grove 5:3f93dd1d4cb3 80 int ret;
sam_grove 5:3f93dd1d4cb3 81 if(available()) //Is data already available?
sam_grove 5:3f93dd1d4cb3 82 {
sam_grove 5:3f93dd1d4cb3 83 m_availableSphre.wait(0); //Clear the queue as data is available
sam_grove 5:3f93dd1d4cb3 84 return OK;
sam_grove 5:3f93dd1d4cb3 85 }
sam_grove 5:3f93dd1d4cb3 86
sam_grove 5:3f93dd1d4cb3 87 DBG("Waiting for data availability %d ms (-1 is infinite)", timeout);
sam_grove 5:3f93dd1d4cb3 88 ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort
sam_grove 5:3f93dd1d4cb3 89 if(ret <= 0)
sam_grove 5:3f93dd1d4cb3 90 {
sam_grove 5:3f93dd1d4cb3 91 DBG("Timeout");
sam_grove 5:3f93dd1d4cb3 92 return NET_TIMEOUT;
sam_grove 5:3f93dd1d4cb3 93 }
sam_grove 5:3f93dd1d4cb3 94 if(!available()) //Even if abort has been called, return that data is available
sam_grove 5:3f93dd1d4cb3 95 {
sam_grove 5:3f93dd1d4cb3 96 DBG("Aborted");
sam_grove 5:3f93dd1d4cb3 97 return NET_INTERRUPTED;
sam_grove 5:3f93dd1d4cb3 98 }
sam_grove 5:3f93dd1d4cb3 99 DBG("Finished waiting");
sam_grove 5:3f93dd1d4cb3 100 m_availableSphre.wait(0); //Clear the queue as data is available
sam_grove 5:3f93dd1d4cb3 101 return OK;
sam_grove 5:3f93dd1d4cb3 102 }
sam_grove 5:3f93dd1d4cb3 103
sam_grove 5:3f93dd1d4cb3 104 /*virtual*/ int IOSerialStream::abortRead() //Abort current reading (or waiting) operation
sam_grove 5:3f93dd1d4cb3 105 {
sam_grove 5:3f93dd1d4cb3 106 if( !available() ) //If there is data pending, no need to abort
sam_grove 5:3f93dd1d4cb3 107 {
sam_grove 5:3f93dd1d4cb3 108 m_availableSphre.release(); //Force exiting the waiting state; kludge to pass a RC directly
sam_grove 5:3f93dd1d4cb3 109 }
sam_grove 5:3f93dd1d4cb3 110 else
sam_grove 5:3f93dd1d4cb3 111 {
sam_grove 5:3f93dd1d4cb3 112 DBG("Serial is readable"); ;
sam_grove 5:3f93dd1d4cb3 113 }
sam_grove 5:3f93dd1d4cb3 114 return OK;
sam_grove 5:3f93dd1d4cb3 115 }
sam_grove 5:3f93dd1d4cb3 116
sam_grove 5:3f93dd1d4cb3 117 void IOSerialStream::setupReadableISR(bool en)
sam_grove 5:3f93dd1d4cb3 118 {
sam_grove 5:3f93dd1d4cb3 119 if(en)
sam_grove 5:3f93dd1d4cb3 120 {
sam_grove 5:3f93dd1d4cb3 121 ((LPC_UART_TypeDef *)(UART_3))->IER |= 1 << 0;
sam_grove 5:3f93dd1d4cb3 122 }
sam_grove 5:3f93dd1d4cb3 123 else
sam_grove 5:3f93dd1d4cb3 124 {
sam_grove 5:3f93dd1d4cb3 125 ((LPC_UART_TypeDef *)(UART_3))->IER &= ~(1 << 0);
sam_grove 5:3f93dd1d4cb3 126 }
sam_grove 5:3f93dd1d4cb3 127 }
sam_grove 5:3f93dd1d4cb3 128
sam_grove 5:3f93dd1d4cb3 129 void IOSerialStream::readable() //Callback from m_serial when new data is available
sam_grove 5:3f93dd1d4cb3 130 {
sam_grove 5:3f93dd1d4cb3 131 do
sam_grove 5:3f93dd1d4cb3 132 {
sam_grove 5:3f93dd1d4cb3 133 m_inBuf.queue(((LPC_UART_TypeDef *)UART_3)->RBR); //FIXME mbed libraries this is an awful kludge
sam_grove 5:3f93dd1d4cb3 134 } while(m_serial.readable());
sam_grove 5:3f93dd1d4cb3 135 m_availableSphre.release(); //Force exiting the waiting state
sam_grove 5:3f93dd1d4cb3 136 }
sam_grove 5:3f93dd1d4cb3 137
sam_grove 5:3f93dd1d4cb3 138 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
sam_grove 5:3f93dd1d4cb3 139 /*virtual*/ int IOSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=osWaitForever*/)
sam_grove 5:3f93dd1d4cb3 140 {
sam_grove 5:3f93dd1d4cb3 141 DBG("Trying to write %d chars", length);
sam_grove 5:3f93dd1d4cb3 142 int ret = waitSpace(timeout);
sam_grove 5:3f93dd1d4cb3 143 if(ret)
sam_grove 5:3f93dd1d4cb3 144 {
sam_grove 5:3f93dd1d4cb3 145 WARN("Error %d while waiting for space", ret);
sam_grove 5:3f93dd1d4cb3 146 return ret;
sam_grove 5:3f93dd1d4cb3 147 }
sam_grove 5:3f93dd1d4cb3 148 DBG("Writing %d chars", length);
sam_grove 5:3f93dd1d4cb3 149 setupWriteableISR(false);
sam_grove 5:3f93dd1d4cb3 150 while(length)
sam_grove 5:3f93dd1d4cb3 151 {
sam_grove 5:3f93dd1d4cb3 152 m_outBuf.queue(*buf);
sam_grove 5:3f93dd1d4cb3 153 buf++;
sam_grove 5:3f93dd1d4cb3 154 length--;
sam_grove 5:3f93dd1d4cb3 155 if(length && !space())
sam_grove 5:3f93dd1d4cb3 156 {
sam_grove 5:3f93dd1d4cb3 157 DBG("Waiting to write remaining %d chars", length);
sam_grove 5:3f93dd1d4cb3 158 setupWriteableISR(true);
sam_grove 5:3f93dd1d4cb3 159 ret = waitSpace(timeout);
sam_grove 5:3f93dd1d4cb3 160 if(ret)
sam_grove 5:3f93dd1d4cb3 161 {
sam_grove 5:3f93dd1d4cb3 162 WARN("Error %d while waiting for space", ret);
sam_grove 5:3f93dd1d4cb3 163 return ret;
sam_grove 5:3f93dd1d4cb3 164 }
sam_grove 5:3f93dd1d4cb3 165 setupWriteableISR(false);
sam_grove 5:3f93dd1d4cb3 166 }
sam_grove 5:3f93dd1d4cb3 167 }
sam_grove 5:3f93dd1d4cb3 168 //If m_serial tx fifo is empty we need to manually tx a byte in order to trigger the interrupt
sam_grove 5:3f93dd1d4cb3 169 if( m_outBuf.available() && m_serialTxFifoEmpty )
sam_grove 5:3f93dd1d4cb3 170 {
sam_grove 5:3f93dd1d4cb3 171 m_serialTxFifoEmpty = false;
sam_grove 5:3f93dd1d4cb3 172 uint8_t c;
sam_grove 5:3f93dd1d4cb3 173 m_outBuf.dequeue(&c);
sam_grove 5:3f93dd1d4cb3 174 //m_serial.putc((char)c);
sam_grove 5:3f93dd1d4cb3 175 ((LPC_UART_TypeDef *)UART_3)->THR = c; //FIXME awful kludge
sam_grove 5:3f93dd1d4cb3 176 }
sam_grove 5:3f93dd1d4cb3 177 setupWriteableISR(true);
sam_grove 5:3f93dd1d4cb3 178 DBG("Write successful");
sam_grove 5:3f93dd1d4cb3 179 return OK;
sam_grove 5:3f93dd1d4cb3 180 }
sam_grove 5:3f93dd1d4cb3 181
sam_grove 5:3f93dd1d4cb3 182 /*virtual*/ size_t IOSerialStream::space()
sam_grove 5:3f93dd1d4cb3 183 {
sam_grove 5:3f93dd1d4cb3 184 setupWriteableISR(false); //m_outBuf.available() is not reentrant
sam_grove 5:3f93dd1d4cb3 185 size_t len = CIRCBUF_SIZE - m_outBuf.available();
sam_grove 5:3f93dd1d4cb3 186 setupWriteableISR(true);
sam_grove 5:3f93dd1d4cb3 187 return len;
sam_grove 5:3f93dd1d4cb3 188 }
sam_grove 5:3f93dd1d4cb3 189
sam_grove 5:3f93dd1d4cb3 190 /*virtual*/ int IOSerialStream::waitSpace(uint32_t timeout/*=osWaitForever*/) //Wait for space to be available
sam_grove 5:3f93dd1d4cb3 191 {
sam_grove 5:3f93dd1d4cb3 192 int ret;
sam_grove 5:3f93dd1d4cb3 193 if(space()) //Is still space already left?
sam_grove 5:3f93dd1d4cb3 194 {
sam_grove 5:3f93dd1d4cb3 195 m_spaceSphre.wait(0); //Clear the queue as space is available
sam_grove 5:3f93dd1d4cb3 196 return OK;
sam_grove 5:3f93dd1d4cb3 197 }
sam_grove 5:3f93dd1d4cb3 198
sam_grove 5:3f93dd1d4cb3 199 DBG("Waiting for data space %d ms (-1 is infinite)", timeout);
sam_grove 5:3f93dd1d4cb3 200 ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort
sam_grove 5:3f93dd1d4cb3 201 if(ret <= 0)
sam_grove 5:3f93dd1d4cb3 202 {
sam_grove 5:3f93dd1d4cb3 203 DBG("Timeout");
sam_grove 5:3f93dd1d4cb3 204 return NET_TIMEOUT;
sam_grove 5:3f93dd1d4cb3 205 }
sam_grove 5:3f93dd1d4cb3 206 if(!space()) //Even if abort has been called, return that space is available
sam_grove 5:3f93dd1d4cb3 207 {
sam_grove 5:3f93dd1d4cb3 208 DBG("Aborted");
sam_grove 5:3f93dd1d4cb3 209 return NET_INTERRUPTED;
sam_grove 5:3f93dd1d4cb3 210 }
sam_grove 5:3f93dd1d4cb3 211 m_spaceSphre.wait(0); //Clear the queue as space is available
sam_grove 5:3f93dd1d4cb3 212 return OK;
sam_grove 5:3f93dd1d4cb3 213 }
sam_grove 5:3f93dd1d4cb3 214
sam_grove 5:3f93dd1d4cb3 215 /*virtual*/ int IOSerialStream::abortWrite() //Abort current writing (or waiting) operation
sam_grove 5:3f93dd1d4cb3 216 {
sam_grove 5:3f93dd1d4cb3 217 if( !space() ) //If there is space left, no need to abort
sam_grove 5:3f93dd1d4cb3 218 {
sam_grove 5:3f93dd1d4cb3 219 m_spaceSphre.release(); //Force exiting the waiting state
sam_grove 5:3f93dd1d4cb3 220 }
sam_grove 5:3f93dd1d4cb3 221 return OK;
sam_grove 5:3f93dd1d4cb3 222 }
sam_grove 5:3f93dd1d4cb3 223
sam_grove 5:3f93dd1d4cb3 224 void IOSerialStream::setupWriteableISR(bool en)
sam_grove 5:3f93dd1d4cb3 225 {
sam_grove 5:3f93dd1d4cb3 226 if(en)
sam_grove 5:3f93dd1d4cb3 227 {
sam_grove 5:3f93dd1d4cb3 228 ((LPC_UART_TypeDef *)(UART_3))->IER |= 1 << 1;
sam_grove 5:3f93dd1d4cb3 229 }
sam_grove 5:3f93dd1d4cb3 230 else
sam_grove 5:3f93dd1d4cb3 231 {
sam_grove 5:3f93dd1d4cb3 232 ((LPC_UART_TypeDef *)(UART_3))->IER &= ~(1 << 1);
sam_grove 5:3f93dd1d4cb3 233 }
sam_grove 5:3f93dd1d4cb3 234 }
sam_grove 5:3f93dd1d4cb3 235
sam_grove 5:3f93dd1d4cb3 236 void IOSerialStream::writeable() //Callback from m_serial when new space is available
sam_grove 5:3f93dd1d4cb3 237 {
sam_grove 5:3f93dd1d4cb3 238 if(m_outBuf.isEmpty())
sam_grove 5:3f93dd1d4cb3 239 {
sam_grove 5:3f93dd1d4cb3 240 m_serialTxFifoEmpty = true;
sam_grove 5:3f93dd1d4cb3 241 }
sam_grove 5:3f93dd1d4cb3 242 else
sam_grove 5:3f93dd1d4cb3 243 {
sam_grove 5:3f93dd1d4cb3 244 while(m_serial.writeable() && !m_outBuf.isEmpty())
sam_grove 5:3f93dd1d4cb3 245 {
sam_grove 5:3f93dd1d4cb3 246 uint8_t c;
sam_grove 5:3f93dd1d4cb3 247 m_outBuf.dequeue(&c);
sam_grove 5:3f93dd1d4cb3 248 //m_serial.putc((char)c);
sam_grove 5:3f93dd1d4cb3 249 ((LPC_UART_TypeDef *)UART_3)->THR = c; //FIXME awful kludge
sam_grove 5:3f93dd1d4cb3 250 }
sam_grove 5:3f93dd1d4cb3 251 }
sam_grove 5:3f93dd1d4cb3 252 m_spaceSphre.release(); //Force exiting the waiting state
sam_grove 5:3f93dd1d4cb3 253 }