1 /* USBSerialStream.h */
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 #ifndef USBSERIALSTREAM_H_
21 #define USBSERIALSTREAM_H_
26 #include "USBHost3GModule/IUSBHostSerial.h"
27 #include "USBHost3GModule/IUSBHostSerialListener.h"
30 #include "core/MtxCircBuffer.h"
32 /* Input Serial Stream for USB virtual serial ports interfaces
33 This class is not thread-safe, except for the *Abort() methods that can be called by any thread/ISR
36 class USBSerialStream
: public IOStream
, IUSBHostSerialListener
39 enum { CIRCBUF_SIZE
= 127 };
40 USBSerialStream(IUSBHostSerial
& serial
);
41 /*virtual*/ ~USBSerialStream();
43 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
44 virtual int read(uint8_t* buf
, size_t* pLength
, size_t maxLength
, uint32_t timeout
=osWaitForever
);
45 virtual size_t available();
46 virtual int waitAvailable(uint32_t timeout
=osWaitForever
); //Wait for data to be available
47 virtual int abortRead(); //Abort current reading (or waiting) operation
50 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
51 virtual int write(uint8_t* buf
, size_t length
, uint32_t timeout
=osWaitForever
);
52 virtual size_t space();
53 virtual int waitSpace(uint32_t timeout
=osWaitForever
); //Wait for space to be available
54 virtual int abortWrite(); //Abort current writing (or waiting) operation
57 IUSBHostSerial
& m_serial
;
58 volatile bool m_serialTxFifoEmpty
;
60 void setupReadableISR(bool en
);
61 virtual void readable(); //Callback from m_serial when new data is available
63 Semaphore m_availableSphre
; //Used for signalling
65 void setupWriteableISR(bool en
);
66 virtual void writeable(); //Callback from m_serial when new space is available
68 Semaphore m_spaceSphre
; //Used for signalling
70 MtxCircBuffer
<uint8_t, CIRCBUF_SIZE
+ 1> m_inBuf
;
71 MtxCircBuffer
<uint8_t, CIRCBUF_SIZE
+ 1> m_outBuf
;
74 #endif /* USBSERIALSTREAM_H_ */