]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/net/cellular/CellularUSBModem/serial/io/IOSerialStream.cpp
1 /* IOSerialStream.cpp */
2 /* Copyright (C) 2012 mbed.org, MIT License
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 #define __DEBUG__ 0 //Maximum verbosity
22 #define __MODULE__ "IOSerialStream.cpp"
29 #include "IOSerialStream.h"
31 #define UART_X ((LPC_UART_TypeDef *)(UART_1))
33 IOSerialStream::IOSerialStream(mbed::RawSerial
& serial
) : m_serial(serial
), m_serialTxFifoEmpty(true),
34 m_availableSphre(1), m_spaceSphre(1), m_inBuf(), m_outBuf()
36 m_availableSphre
.wait();
39 m_serial
.attach(this, &IOSerialStream::readable
, mbed::SerialBase::RxIrq
);
40 m_serial
.attach(this, &IOSerialStream::writeable
, mbed::SerialBase::TxIrq
);
43 /*virtual*/ IOSerialStream::~IOSerialStream()
45 m_serial
.attach(NULL
, mbed::SerialBase::RxIrq
);
46 m_serial
.attach(NULL
, mbed::SerialBase::TxIrq
);
49 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
50 /*virtual*/ int IOSerialStream::read(uint8_t* buf
, size_t* pLength
, size_t maxLength
, uint32_t timeout
/*=osWaitForever*/)
52 DBG("Trying to read at most %d chars", maxLength
);
53 int ret
= waitAvailable(timeout
);
56 WARN("Error %d while waiting for incoming data", ret
);
59 int readLen
= MIN( available(), maxLength
);
61 setupReadableISR(false);
67 setupReadableISR(true);
68 DBG("Read %d chars successfully", *pLength
);
72 /*virtual*/ size_t IOSerialStream::available()
74 setupReadableISR(false); //m_inBuf.available() is not reentrant
75 size_t len
= m_inBuf
.available();
76 setupReadableISR(true);
80 /*virtual*/ int IOSerialStream::waitAvailable(uint32_t timeout
/*=osWaitForever*/) //Wait for data to be available
83 if(available()) //Is data already available?
85 m_availableSphre
.wait(0); //Clear the queue as data is available
89 DBG("Waiting for data availability %d ms (-1 is infinite)", timeout
);
90 ret
= m_availableSphre
.wait(timeout
); //Wait for data to arrive or for abort
96 if(!available()) //Even if abort has been called, return that data is available
99 return NET_INTERRUPTED
;
101 DBG("Finished waiting");
102 m_availableSphre
.wait(0); //Clear the queue as data is available
106 /*virtual*/ int IOSerialStream::abortRead() //Abort current reading (or waiting) operation
108 if( !available() ) //If there is data pending, no need to abort
110 m_availableSphre
.release(); //Force exiting the waiting state; kludge to pass a RC directly
114 DBG("Serial is readable"); ;
119 void IOSerialStream::setupReadableISR(bool en
)
123 UART_X
->IER
|= 1 << 0;
127 UART_X
->IER
&= ~(1 << 0);
131 void IOSerialStream::readable() //Callback from m_serial when new data is available
135 m_inBuf
.queue(m_serial
.getc());
136 } while(m_serial
.readable());
137 m_availableSphre
.release(); //Force exiting the waiting state
140 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
141 /*virtual*/ int IOSerialStream::write(uint8_t* buf
, size_t length
, uint32_t timeout
/*=osWaitForever*/)
143 DBG("Trying to write %d chars", length
);
144 int ret
= waitSpace(timeout
);
147 WARN("Error %d while waiting for space", ret
);
150 DBG("Writing %d chars", length
);
151 setupWriteableISR(false);
154 m_outBuf
.queue(*buf
);
157 if(length
&& !space())
159 DBG("Waiting to write remaining %d chars", length
);
160 setupWriteableISR(true);
161 ret
= waitSpace(timeout
);
164 WARN("Error %d while waiting for space", ret
);
167 setupWriteableISR(false);
170 //If m_serial tx fifo is empty we need to manually tx a byte in order to trigger the interrupt
171 if( m_outBuf
.available() && m_serialTxFifoEmpty
)
173 m_serialTxFifoEmpty
= false;
175 m_outBuf
.dequeue(&c
);
176 m_serial
.putc((char)c
);
178 setupWriteableISR(true);
179 DBG("Write successful");
183 /*virtual*/ size_t IOSerialStream::space()
185 setupWriteableISR(false); //m_outBuf.available() is not reentrant
186 size_t len
= CIRCBUF_SIZE
- m_outBuf
.available();
187 setupWriteableISR(true);
191 /*virtual*/ int IOSerialStream::waitSpace(uint32_t timeout
/*=osWaitForever*/) //Wait for space to be available
194 if(space()) //Is still space already left?
196 m_spaceSphre
.wait(0); //Clear the queue as space is available
200 DBG("Waiting for data space %d ms (-1 is infinite)", timeout
);
201 ret
= m_spaceSphre
.wait(timeout
); //Wait for space to be made or for abort
207 if(!space()) //Even if abort has been called, return that space is available
210 return NET_INTERRUPTED
;
212 m_spaceSphre
.wait(0); //Clear the queue as space is available
216 /*virtual*/ int IOSerialStream::abortWrite() //Abort current writing (or waiting) operation
218 if( !space() ) //If there is space left, no need to abort
220 m_spaceSphre
.release(); //Force exiting the waiting state
225 void IOSerialStream::setupWriteableISR(bool en
)
229 UART_X
->IER
|= 1 << 1;
233 UART_X
->IER
&= ~(1 << 1);
237 void IOSerialStream::writeable() //Callback from m_serial when new space is available
239 if(m_outBuf
.isEmpty())
241 m_serialTxFifoEmpty
= true;
245 while(m_serial
.writeable() && !m_outBuf
.isEmpty())
248 m_outBuf
.dequeue(&c
);
249 m_serial
.putc((char)c
);
252 m_spaceSphre
.release(); //Force exiting the waiting state