]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/net/cellular/CellularUSBModem/serial/usb/USBSerialStream.cpp
1 /* USBSerialStream.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.
22 #define __MODULE__ "USBSerialStream.cpp"
29 #include "USBSerialStream.h"
32 USBSerialStream::USBSerialStream(IUSBHostSerial
& serial
) : m_serial(serial
), m_serialTxFifoEmpty(true),
33 m_availableSphre(1), m_spaceSphre(1), m_inBuf()
35 m_availableSphre
.wait();
38 m_serial
.attach(this);
41 /*virtual*/ USBSerialStream::~USBSerialStream()
43 m_serial
.attach(NULL
);
46 //0 for non-blocking (returns immediately), -1 for infinite blocking
47 /*virtual*/ int USBSerialStream::read(uint8_t* buf
, size_t* pLength
, size_t maxLength
, uint32_t timeout
/*=osWaitForever*/)
49 DBG("Trying to read at most %d chars", maxLength
);
50 int ret
= waitAvailable(timeout
);
53 WARN("Error %d while waiting for incoming data", ret
);
56 int a
= available(); //Prevent macro issues
57 int readLen
= MIN( a
, maxLength
);
60 setupReadableISR(false);
66 setupReadableISR(true);
67 DBG("Read %d chars successfully", *pLength
);
71 /*virtual*/ size_t USBSerialStream::available()
73 setupReadableISR(false); //m_inBuf.available() is not reentrant
74 size_t len
= m_inBuf
.available();
75 setupReadableISR(true);
79 /*virtual*/ int USBSerialStream::waitAvailable(uint32_t timeout
/*=osWaitForever*/) //Wait for data to be available
82 if(available()) //Is data already available?
84 while( m_availableSphre
.wait(0) > 0 ); //Clear the queue as data is available
88 DBG("Waiting for data availability %d ms (-1 is infinite)", timeout
);
89 ret
= m_availableSphre
.wait(timeout
); //Wait for data to arrive or for abort
95 if(!m_inBuf
.available()) //Even if abort has been called, return that data is available
98 return NET_INTERRUPTED
;
100 DBG("Finished waiting");
101 while( m_availableSphre
.wait(0) > 0 ); //Clear the queue as data is available
105 /*virtual*/ int USBSerialStream::abortRead() //Abort current reading (or waiting) operation
107 if( /*!available()*/true ) //If there is data pending, no need to abort
109 m_availableSphre
.release(); //Force exiting the waiting state
113 DBG("Serial is readable"); ;
118 void USBSerialStream::setupReadableISR(bool en
)
120 m_serial
.setupIrq(en
, IUSBHostSerial::RxIrq
);
123 void USBSerialStream::readable() //Callback from m_serial when new data is available
125 while(m_serial
.readable())
127 m_inBuf
.queue(m_serial
.getc());
129 m_serial
.readPacket(); //Start read of next packet
130 m_availableSphre
.release(); //Force exiting the waiting state
133 //0 for non-blocking (returns immediately), -1 for infinite blocking
134 /*virtual*/ int USBSerialStream::write(uint8_t* buf
, size_t length
, uint32_t timeout
/*=-1*/)
136 DBG("Trying to write %d chars", length
);
139 int ret
= waitSpace(timeout
);
142 WARN("Error %d while waiting for space", ret
);
145 int s
= space(); //Prevent macro issues
146 int writeLen
= MIN( s
, length
);
147 DBG("Writing %d chars", writeLen
);
148 setupWriteableISR(false);
151 m_outBuf
.queue(*buf
);
156 //If m_serial tx fifo is empty we need to start the packet write
157 if( m_outBuf
.available() && m_serialTxFifoEmpty
)
161 setupWriteableISR(true);
164 DBG("Write successful");
168 /*virtual*/ size_t USBSerialStream::space()
170 setupWriteableISR(false); //m_outBuf.available() is not reentrant
171 size_t len
= CIRCBUF_SIZE
- m_outBuf
.available();
172 setupWriteableISR(true);
176 /*virtual*/ int USBSerialStream::waitSpace(uint32_t timeout
/*=-1*/) //Wait for space to be available
179 if(space()) //Is still space already left?
181 while( m_spaceSphre
.wait(0) > 0); //Clear the queue as space is available
185 DBG("Waiting for data space %d ms (-1 is infinite)", timeout
);
186 ret
= m_spaceSphre
.wait(timeout
); //Wait for space to be made or for abort
192 if(!space()) //Even if abort has been called, return that space is available
195 return NET_INTERRUPTED
;
197 while( m_spaceSphre
.wait(0) > 0); //Clear the queue as space is available
201 /*virtual*/ int USBSerialStream::abortWrite() //Abort current writing (or waiting) operation
203 if( !space() ) //If there is space left, no need to abort
205 m_spaceSphre
.release(); //Force exiting the waiting state
210 void USBSerialStream::setupWriteableISR(bool en
)
212 m_serial
.setupIrq(en
, IUSBHostSerial::TxIrq
);
215 void USBSerialStream::writeable() //Callback from m_serial when new space is available
217 if(m_outBuf
.isEmpty())
219 m_serialTxFifoEmpty
= true;
223 m_serialTxFifoEmpty
= false;
224 while(m_serial
.writeable() && !m_outBuf
.isEmpty())
227 m_outBuf
.dequeue(&c
);
228 m_serial
.putc((char)c
);
230 m_serial
.writePacket(); //Start packet write
232 if(!m_outBuf
.isFull())
234 m_spaceSphre
.release(); //Force exiting the waiting state