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.
19 #ifndef IOSERIALSTREAM_H_
20 #define IOSERIALSTREAM_H_
24 #include "RawSerial.h"
27 #include "core/MtxCircBuffer.h"
29 /** Input Serial Stream for physical serial interfaces (UART...)
30 This class is not thread-safe, except for the *Abort() methods that can be called by any thread/ISR
32 class IOSerialStream
: public IOStream
35 enum { CIRCBUF_SIZE
= 255 };
36 IOSerialStream(mbed::RawSerial
& serial
);
37 /*virtual*/ ~IOSerialStream();
39 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
40 virtual int read(uint8_t* buf
, size_t* pLength
, size_t maxLength
, uint32_t timeout
=osWaitForever
);
41 virtual size_t available();
42 virtual int waitAvailable(uint32_t timeout
=osWaitForever
); //Wait for data to be available
43 virtual int abortRead(); //Abort current reading (or waiting) operation
46 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
47 virtual int write(uint8_t* buf
, size_t length
, uint32_t timeout
=osWaitForever
);
48 virtual size_t space();
49 virtual int waitSpace(uint32_t timeout
=osWaitForever
); //Wait for space to be available
50 virtual int abortWrite(); //Abort current writing (or waiting) operation
54 mbed::RawSerial
& m_serial
;
55 volatile bool m_serialTxFifoEmpty
;
57 void setupReadableISR(bool en
);
58 void readable(); //Callback from m_serial when new data is available
60 Semaphore m_availableSphre
; //Used for signalling
62 void setupWriteableISR(bool en
);
63 void writeable(); //Callback from m_serial when new space is available
65 Semaphore m_spaceSphre
; //Used for signalling
67 MtxCircBuffer
<uint8_t, CIRCBUF_SIZE
+ 1> m_inBuf
;
68 MtxCircBuffer
<uint8_t, CIRCBUF_SIZE
+ 1> m_outBuf
;
72 #endif /* IOSERIALSTREAM_H_ */