]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/net/cellular/CellularUSBModem/serial/io/IOSerialStream.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / net / cellular / CellularUSBModem / serial / io / IOSerialStream.cpp
1 /* IOSerialStream.cpp */
2 /* Copyright (C) 2012 mbed.org, MIT License
3 *
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:
9 *
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
12 *
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.
18 */
19
20 #define __DEBUG__ 0 //Maximum verbosity
21 #ifndef __MODULE__
22 #define __MODULE__ "IOSerialStream.cpp"
23 #endif
24
25 #include "core/fwk.h"
26
27 #include <cstring>
28
29 #include "IOSerialStream.h"
30
31 #define UART_X ((LPC_UART_TypeDef *)(UART_1))
32
33 IOSerialStream::IOSerialStream(mbed::RawSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
34 m_availableSphre(1), m_spaceSphre(1), m_inBuf(), m_outBuf()
35 {
36 m_availableSphre.wait();
37 m_spaceSphre.wait();
38 //Attach interrupts
39 m_serial.attach(this, &IOSerialStream::readable, mbed::SerialBase::RxIrq);
40 m_serial.attach(this, &IOSerialStream::writeable, mbed::SerialBase::TxIrq);
41 }
42
43 /*virtual*/ IOSerialStream::~IOSerialStream()
44 {
45 m_serial.attach(NULL, mbed::SerialBase::RxIrq);
46 m_serial.attach(NULL, mbed::SerialBase::TxIrq);
47 }
48
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*/)
51 {
52 DBG("Trying to read at most %d chars", maxLength);
53 int ret = waitAvailable(timeout);
54 if(ret)
55 {
56 WARN("Error %d while waiting for incoming data", ret);
57 return ret;
58 }
59 int readLen = MIN( available(), maxLength );
60 *pLength = readLen;
61 setupReadableISR(false);
62 while(readLen--)
63 {
64 m_inBuf.dequeue(buf);
65 buf++;
66 }
67 setupReadableISR(true);
68 DBG("Read %d chars successfully", *pLength);
69 return OK;
70 }
71
72 /*virtual*/ size_t IOSerialStream::available()
73 {
74 setupReadableISR(false); //m_inBuf.available() is not reentrant
75 size_t len = m_inBuf.available();
76 setupReadableISR(true);
77 return len;
78 }
79
80 /*virtual*/ int IOSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
81 {
82 int ret;
83 if(available()) //Is data already available?
84 {
85 m_availableSphre.wait(0); //Clear the queue as data is available
86 return OK;
87 }
88
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
91 if(ret <= 0)
92 {
93 DBG("Timeout");
94 return NET_TIMEOUT;
95 }
96 if(!available()) //Even if abort has been called, return that data is available
97 {
98 DBG("Aborted");
99 return NET_INTERRUPTED;
100 }
101 DBG("Finished waiting");
102 m_availableSphre.wait(0); //Clear the queue as data is available
103 return OK;
104 }
105
106 /*virtual*/ int IOSerialStream::abortRead() //Abort current reading (or waiting) operation
107 {
108 if( !available() ) //If there is data pending, no need to abort
109 {
110 m_availableSphre.release(); //Force exiting the waiting state; kludge to pass a RC directly
111 }
112 else
113 {
114 DBG("Serial is readable"); ;
115 }
116 return OK;
117 }
118
119 void IOSerialStream::setupReadableISR(bool en)
120 {
121 if(en)
122 {
123 UART_X->IER |= 1 << 0;
124 }
125 else
126 {
127 UART_X->IER &= ~(1 << 0);
128 }
129 }
130
131 void IOSerialStream::readable() //Callback from m_serial when new data is available
132 {
133 do
134 {
135 m_inBuf.queue(m_serial.getc());
136 } while(m_serial.readable());
137 m_availableSphre.release(); //Force exiting the waiting state
138 }
139
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*/)
142 {
143 DBG("Trying to write %d chars", length);
144 int ret = waitSpace(timeout);
145 if(ret)
146 {
147 WARN("Error %d while waiting for space", ret);
148 return ret;
149 }
150 DBG("Writing %d chars", length);
151 setupWriteableISR(false);
152 while(length)
153 {
154 m_outBuf.queue(*buf);
155 buf++;
156 length--;
157 if(length && !space())
158 {
159 DBG("Waiting to write remaining %d chars", length);
160 setupWriteableISR(true);
161 ret = waitSpace(timeout);
162 if(ret)
163 {
164 WARN("Error %d while waiting for space", ret);
165 return ret;
166 }
167 setupWriteableISR(false);
168 }
169 }
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 )
172 {
173 m_serialTxFifoEmpty = false;
174 uint8_t c;
175 m_outBuf.dequeue(&c);
176 m_serial.putc((char)c);
177 }
178 setupWriteableISR(true);
179 DBG("Write successful");
180 return OK;
181 }
182
183 /*virtual*/ size_t IOSerialStream::space()
184 {
185 setupWriteableISR(false); //m_outBuf.available() is not reentrant
186 size_t len = CIRCBUF_SIZE - m_outBuf.available();
187 setupWriteableISR(true);
188 return len;
189 }
190
191 /*virtual*/ int IOSerialStream::waitSpace(uint32_t timeout/*=osWaitForever*/) //Wait for space to be available
192 {
193 int ret;
194 if(space()) //Is still space already left?
195 {
196 m_spaceSphre.wait(0); //Clear the queue as space is available
197 return OK;
198 }
199
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
202 if(ret <= 0)
203 {
204 DBG("Timeout");
205 return NET_TIMEOUT;
206 }
207 if(!space()) //Even if abort has been called, return that space is available
208 {
209 DBG("Aborted");
210 return NET_INTERRUPTED;
211 }
212 m_spaceSphre.wait(0); //Clear the queue as space is available
213 return OK;
214 }
215
216 /*virtual*/ int IOSerialStream::abortWrite() //Abort current writing (or waiting) operation
217 {
218 if( !space() ) //If there is space left, no need to abort
219 {
220 m_spaceSphre.release(); //Force exiting the waiting state
221 }
222 return OK;
223 }
224
225 void IOSerialStream::setupWriteableISR(bool en)
226 {
227 if(en)
228 {
229 UART_X->IER |= 1 << 1;
230 }
231 else
232 {
233 UART_X->IER &= ~(1 << 1);
234 }
235 }
236
237 void IOSerialStream::writeable() //Callback from m_serial when new space is available
238 {
239 if(m_outBuf.isEmpty())
240 {
241 m_serialTxFifoEmpty = true;
242 }
243 else
244 {
245 while(m_serial.writeable() && !m_outBuf.isEmpty())
246 {
247 uint8_t c;
248 m_outBuf.dequeue(&c);
249 m_serial.putc((char)c);
250 }
251 }
252 m_spaceSphre.release(); //Force exiting the waiting state
253 }
Imprint / Impressum