Squashed 'tmk_core/' changes from 7967731..b9e0ea0
b9e0ea0 Merge commit '7fa9d8bdea3773d1195b04d98fcf27cf48ddd81d' as 'tool/mbed/mbed-sdk' 7fa9d8b Squashed 'tool/mbed/mbed-sdk/' content from commit 7c21ce5 git-subtree-dir: tmk_core git-subtree-split: b9e0ea08cb940de20b3610ecdda18e9d8cd7c552
This commit is contained in:
parent
a20ef7052c
commit
1fe4406f37
4198 changed files with 2016457 additions and 0 deletions
|
@ -0,0 +1,253 @@
|
|||
/* IOSerialStream.cpp */
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define __DEBUG__ 0 //Maximum verbosity
|
||||
#ifndef __MODULE__
|
||||
#define __MODULE__ "IOSerialStream.cpp"
|
||||
#endif
|
||||
|
||||
#include "core/fwk.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "IOSerialStream.h"
|
||||
|
||||
#define UART_X ((LPC_UART_TypeDef *)(UART_1))
|
||||
|
||||
IOSerialStream::IOSerialStream(mbed::RawSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
|
||||
m_availableSphre(1), m_spaceSphre(1), m_inBuf(), m_outBuf()
|
||||
{
|
||||
m_availableSphre.wait();
|
||||
m_spaceSphre.wait();
|
||||
//Attach interrupts
|
||||
m_serial.attach(this, &IOSerialStream::readable, mbed::SerialBase::RxIrq);
|
||||
m_serial.attach(this, &IOSerialStream::writeable, mbed::SerialBase::TxIrq);
|
||||
}
|
||||
|
||||
/*virtual*/ IOSerialStream::~IOSerialStream()
|
||||
{
|
||||
m_serial.attach(NULL, mbed::SerialBase::RxIrq);
|
||||
m_serial.attach(NULL, mbed::SerialBase::TxIrq);
|
||||
}
|
||||
|
||||
//0 for non-blocking (returns immediately), osWaitForever for infinite blocking
|
||||
/*virtual*/ int IOSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
|
||||
{
|
||||
DBG("Trying to read at most %d chars", maxLength);
|
||||
int ret = waitAvailable(timeout);
|
||||
if(ret)
|
||||
{
|
||||
WARN("Error %d while waiting for incoming data", ret);
|
||||
return ret;
|
||||
}
|
||||
int readLen = MIN( available(), maxLength );
|
||||
*pLength = readLen;
|
||||
setupReadableISR(false);
|
||||
while(readLen--)
|
||||
{
|
||||
m_inBuf.dequeue(buf);
|
||||
buf++;
|
||||
}
|
||||
setupReadableISR(true);
|
||||
DBG("Read %d chars successfully", *pLength);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ size_t IOSerialStream::available()
|
||||
{
|
||||
setupReadableISR(false); //m_inBuf.available() is not reentrant
|
||||
size_t len = m_inBuf.available();
|
||||
setupReadableISR(true);
|
||||
return len;
|
||||
}
|
||||
|
||||
/*virtual*/ int IOSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
|
||||
{
|
||||
int ret;
|
||||
if(available()) //Is data already available?
|
||||
{
|
||||
m_availableSphre.wait(0); //Clear the queue as data is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
DBG("Waiting for data availability %d ms (-1 is infinite)", timeout);
|
||||
ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort
|
||||
if(ret <= 0)
|
||||
{
|
||||
DBG("Timeout");
|
||||
return NET_TIMEOUT;
|
||||
}
|
||||
if(!available()) //Even if abort has been called, return that data is available
|
||||
{
|
||||
DBG("Aborted");
|
||||
return NET_INTERRUPTED;
|
||||
}
|
||||
DBG("Finished waiting");
|
||||
m_availableSphre.wait(0); //Clear the queue as data is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ int IOSerialStream::abortRead() //Abort current reading (or waiting) operation
|
||||
{
|
||||
if( !available() ) //If there is data pending, no need to abort
|
||||
{
|
||||
m_availableSphre.release(); //Force exiting the waiting state; kludge to pass a RC directly
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG("Serial is readable"); ;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
void IOSerialStream::setupReadableISR(bool en)
|
||||
{
|
||||
if(en)
|
||||
{
|
||||
UART_X->IER |= 1 << 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
UART_X->IER &= ~(1 << 0);
|
||||
}
|
||||
}
|
||||
|
||||
void IOSerialStream::readable() //Callback from m_serial when new data is available
|
||||
{
|
||||
do
|
||||
{
|
||||
m_inBuf.queue(m_serial.getc());
|
||||
} while(m_serial.readable());
|
||||
m_availableSphre.release(); //Force exiting the waiting state
|
||||
}
|
||||
|
||||
//0 for non-blocking (returns immediately), osWaitForever for infinite blocking
|
||||
/*virtual*/ int IOSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=osWaitForever*/)
|
||||
{
|
||||
DBG("Trying to write %d chars", length);
|
||||
int ret = waitSpace(timeout);
|
||||
if(ret)
|
||||
{
|
||||
WARN("Error %d while waiting for space", ret);
|
||||
return ret;
|
||||
}
|
||||
DBG("Writing %d chars", length);
|
||||
setupWriteableISR(false);
|
||||
while(length)
|
||||
{
|
||||
m_outBuf.queue(*buf);
|
||||
buf++;
|
||||
length--;
|
||||
if(length && !space())
|
||||
{
|
||||
DBG("Waiting to write remaining %d chars", length);
|
||||
setupWriteableISR(true);
|
||||
ret = waitSpace(timeout);
|
||||
if(ret)
|
||||
{
|
||||
WARN("Error %d while waiting for space", ret);
|
||||
return ret;
|
||||
}
|
||||
setupWriteableISR(false);
|
||||
}
|
||||
}
|
||||
//If m_serial tx fifo is empty we need to manually tx a byte in order to trigger the interrupt
|
||||
if( m_outBuf.available() && m_serialTxFifoEmpty )
|
||||
{
|
||||
m_serialTxFifoEmpty = false;
|
||||
uint8_t c;
|
||||
m_outBuf.dequeue(&c);
|
||||
m_serial.putc((char)c);
|
||||
}
|
||||
setupWriteableISR(true);
|
||||
DBG("Write successful");
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ size_t IOSerialStream::space()
|
||||
{
|
||||
setupWriteableISR(false); //m_outBuf.available() is not reentrant
|
||||
size_t len = CIRCBUF_SIZE - m_outBuf.available();
|
||||
setupWriteableISR(true);
|
||||
return len;
|
||||
}
|
||||
|
||||
/*virtual*/ int IOSerialStream::waitSpace(uint32_t timeout/*=osWaitForever*/) //Wait for space to be available
|
||||
{
|
||||
int ret;
|
||||
if(space()) //Is still space already left?
|
||||
{
|
||||
m_spaceSphre.wait(0); //Clear the queue as space is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
DBG("Waiting for data space %d ms (-1 is infinite)", timeout);
|
||||
ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort
|
||||
if(ret <= 0)
|
||||
{
|
||||
DBG("Timeout");
|
||||
return NET_TIMEOUT;
|
||||
}
|
||||
if(!space()) //Even if abort has been called, return that space is available
|
||||
{
|
||||
DBG("Aborted");
|
||||
return NET_INTERRUPTED;
|
||||
}
|
||||
m_spaceSphre.wait(0); //Clear the queue as space is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ int IOSerialStream::abortWrite() //Abort current writing (or waiting) operation
|
||||
{
|
||||
if( !space() ) //If there is space left, no need to abort
|
||||
{
|
||||
m_spaceSphre.release(); //Force exiting the waiting state
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
void IOSerialStream::setupWriteableISR(bool en)
|
||||
{
|
||||
if(en)
|
||||
{
|
||||
UART_X->IER |= 1 << 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
UART_X->IER &= ~(1 << 1);
|
||||
}
|
||||
}
|
||||
|
||||
void IOSerialStream::writeable() //Callback from m_serial when new space is available
|
||||
{
|
||||
if(m_outBuf.isEmpty())
|
||||
{
|
||||
m_serialTxFifoEmpty = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
while(m_serial.writeable() && !m_outBuf.isEmpty())
|
||||
{
|
||||
uint8_t c;
|
||||
m_outBuf.dequeue(&c);
|
||||
m_serial.putc((char)c);
|
||||
}
|
||||
}
|
||||
m_spaceSphre.release(); //Force exiting the waiting state
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/* IOSerialStream.h */
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef IOSERIALSTREAM_H_
|
||||
#define IOSERIALSTREAM_H_
|
||||
|
||||
#include "core/fwk.h"
|
||||
|
||||
#include "RawSerial.h"
|
||||
|
||||
#include "rtos.h"
|
||||
#include "core/MtxCircBuffer.h"
|
||||
|
||||
/** Input Serial Stream for physical serial interfaces (UART...)
|
||||
This class is not thread-safe, except for the *Abort() methods that can be called by any thread/ISR
|
||||
*/
|
||||
class IOSerialStream : public IOStream
|
||||
{
|
||||
public:
|
||||
enum { CIRCBUF_SIZE = 255 };
|
||||
IOSerialStream(mbed::RawSerial& serial);
|
||||
/*virtual*/ ~IOSerialStream();
|
||||
|
||||
//0 for non-blocking (returns immediately), osWaitForever for infinite blocking
|
||||
virtual int read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout=osWaitForever);
|
||||
virtual size_t available();
|
||||
virtual int waitAvailable(uint32_t timeout=osWaitForever); //Wait for data to be available
|
||||
virtual int abortRead(); //Abort current reading (or waiting) operation
|
||||
|
||||
|
||||
//0 for non-blocking (returns immediately), osWaitForever for infinite blocking
|
||||
virtual int write(uint8_t* buf, size_t length, uint32_t timeout=osWaitForever);
|
||||
virtual size_t space();
|
||||
virtual int waitSpace(uint32_t timeout=osWaitForever); //Wait for space to be available
|
||||
virtual int abortWrite(); //Abort current writing (or waiting) operation
|
||||
|
||||
private:
|
||||
|
||||
mbed::RawSerial& m_serial;
|
||||
volatile bool m_serialTxFifoEmpty;
|
||||
|
||||
void setupReadableISR(bool en);
|
||||
void readable(); //Callback from m_serial when new data is available
|
||||
|
||||
Semaphore m_availableSphre; //Used for signalling
|
||||
|
||||
void setupWriteableISR(bool en);
|
||||
void writeable(); //Callback from m_serial when new space is available
|
||||
|
||||
Semaphore m_spaceSphre; //Used for signalling
|
||||
|
||||
MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_inBuf;
|
||||
MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_outBuf;
|
||||
|
||||
};
|
||||
|
||||
#endif /* IOSERIALSTREAM_H_ */
|
|
@ -0,0 +1,236 @@
|
|||
/* USBSerialStream.cpp */
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define __DEBUG__ 0
|
||||
#ifndef __MODULE__
|
||||
#define __MODULE__ "USBSerialStream.cpp"
|
||||
#endif
|
||||
|
||||
#include "core/fwk.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "USBSerialStream.h"
|
||||
|
||||
|
||||
USBSerialStream::USBSerialStream(IUSBHostSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
|
||||
m_availableSphre(1), m_spaceSphre(1), m_inBuf()
|
||||
{
|
||||
m_availableSphre.wait();
|
||||
m_spaceSphre.wait();
|
||||
//Attach interrupts
|
||||
m_serial.attach(this);
|
||||
}
|
||||
|
||||
/*virtual*/ USBSerialStream::~USBSerialStream()
|
||||
{
|
||||
m_serial.attach(NULL);
|
||||
}
|
||||
|
||||
//0 for non-blocking (returns immediately), -1 for infinite blocking
|
||||
/*virtual*/ int USBSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
|
||||
{
|
||||
DBG("Trying to read at most %d chars", maxLength);
|
||||
int ret = waitAvailable(timeout);
|
||||
if(ret)
|
||||
{
|
||||
WARN("Error %d while waiting for incoming data", ret);
|
||||
return ret;
|
||||
}
|
||||
int a = available(); //Prevent macro issues
|
||||
int readLen = MIN( a, maxLength );
|
||||
*pLength = readLen;
|
||||
|
||||
setupReadableISR(false);
|
||||
while(readLen--)
|
||||
{
|
||||
m_inBuf.dequeue(buf);
|
||||
buf++;
|
||||
}
|
||||
setupReadableISR(true);
|
||||
DBG("Read %d chars successfully", *pLength);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ size_t USBSerialStream::available()
|
||||
{
|
||||
setupReadableISR(false); //m_inBuf.available() is not reentrant
|
||||
size_t len = m_inBuf.available();
|
||||
setupReadableISR(true);
|
||||
return len;
|
||||
}
|
||||
|
||||
/*virtual*/ int USBSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
|
||||
{
|
||||
int ret;
|
||||
if(available()) //Is data already available?
|
||||
{
|
||||
while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
DBG("Waiting for data availability %d ms (-1 is infinite)", timeout);
|
||||
ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort
|
||||
if(ret <= 0)
|
||||
{
|
||||
DBG("Timeout");
|
||||
return NET_TIMEOUT;
|
||||
}
|
||||
if(!m_inBuf.available()) //Even if abort has been called, return that data is available
|
||||
{
|
||||
DBG("Aborted");
|
||||
return NET_INTERRUPTED;
|
||||
}
|
||||
DBG("Finished waiting");
|
||||
while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ int USBSerialStream::abortRead() //Abort current reading (or waiting) operation
|
||||
{
|
||||
if( /*!available()*/true ) //If there is data pending, no need to abort
|
||||
{
|
||||
m_availableSphre.release(); //Force exiting the waiting state
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG("Serial is readable"); ;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
void USBSerialStream::setupReadableISR(bool en)
|
||||
{
|
||||
m_serial.setupIrq(en, IUSBHostSerial::RxIrq);
|
||||
}
|
||||
|
||||
void USBSerialStream::readable() //Callback from m_serial when new data is available
|
||||
{
|
||||
while(m_serial.readable())
|
||||
{
|
||||
m_inBuf.queue(m_serial.getc());
|
||||
}
|
||||
m_serial.readPacket(); //Start read of next packet
|
||||
m_availableSphre.release(); //Force exiting the waiting state
|
||||
}
|
||||
|
||||
//0 for non-blocking (returns immediately), -1 for infinite blocking
|
||||
/*virtual*/ int USBSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=-1*/)
|
||||
{
|
||||
DBG("Trying to write %d chars", length);
|
||||
do
|
||||
{
|
||||
int ret = waitSpace(timeout);
|
||||
if(ret)
|
||||
{
|
||||
WARN("Error %d while waiting for space", ret);
|
||||
return ret;
|
||||
}
|
||||
int s = space(); //Prevent macro issues
|
||||
int writeLen = MIN( s, length );
|
||||
DBG("Writing %d chars", writeLen);
|
||||
setupWriteableISR(false);
|
||||
while(writeLen)
|
||||
{
|
||||
m_outBuf.queue(*buf);
|
||||
buf++;
|
||||
length--;
|
||||
writeLen--;
|
||||
}
|
||||
//If m_serial tx fifo is empty we need to start the packet write
|
||||
if( m_outBuf.available() && m_serialTxFifoEmpty )
|
||||
{
|
||||
writeable();
|
||||
}
|
||||
setupWriteableISR(true);
|
||||
} while(length);
|
||||
|
||||
DBG("Write successful");
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ size_t USBSerialStream::space()
|
||||
{
|
||||
setupWriteableISR(false); //m_outBuf.available() is not reentrant
|
||||
size_t len = CIRCBUF_SIZE - m_outBuf.available();
|
||||
setupWriteableISR(true);
|
||||
return len;
|
||||
}
|
||||
|
||||
/*virtual*/ int USBSerialStream::waitSpace(uint32_t timeout/*=-1*/) //Wait for space to be available
|
||||
{
|
||||
int ret;
|
||||
if(space()) //Is still space already left?
|
||||
{
|
||||
while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
DBG("Waiting for data space %d ms (-1 is infinite)", timeout);
|
||||
ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort
|
||||
if(ret <= 0)
|
||||
{
|
||||
DBG("Timeout");
|
||||
return NET_TIMEOUT;
|
||||
}
|
||||
if(!space()) //Even if abort has been called, return that space is available
|
||||
{
|
||||
DBG("Aborted");
|
||||
return NET_INTERRUPTED;
|
||||
}
|
||||
while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*virtual*/ int USBSerialStream::abortWrite() //Abort current writing (or waiting) operation
|
||||
{
|
||||
if( !space() ) //If there is space left, no need to abort
|
||||
{
|
||||
m_spaceSphre.release(); //Force exiting the waiting state
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
void USBSerialStream::setupWriteableISR(bool en)
|
||||
{
|
||||
m_serial.setupIrq(en, IUSBHostSerial::TxIrq);
|
||||
}
|
||||
|
||||
void USBSerialStream::writeable() //Callback from m_serial when new space is available
|
||||
{
|
||||
if(m_outBuf.isEmpty())
|
||||
{
|
||||
m_serialTxFifoEmpty = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_serialTxFifoEmpty = false;
|
||||
while(m_serial.writeable() && !m_outBuf.isEmpty())
|
||||
{
|
||||
uint8_t c;
|
||||
m_outBuf.dequeue(&c);
|
||||
m_serial.putc((char)c);
|
||||
}
|
||||
m_serial.writePacket(); //Start packet write
|
||||
}
|
||||
if(!m_outBuf.isFull())
|
||||
{
|
||||
m_spaceSphre.release(); //Force exiting the waiting state
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/* USBSerialStream.h */
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef USBSERIALSTREAM_H_
|
||||
#define USBSERIALSTREAM_H_
|
||||
|
||||
|
||||
#include "core/fwk.h"
|
||||
|
||||
#include "USBHost3GModule/IUSBHostSerial.h"
|
||||
#include "USBHost3GModule/IUSBHostSerialListener.h"
|
||||
|
||||
#include "rtos.h"
|
||||
#include "core/MtxCircBuffer.h"
|
||||
|
||||
/* Input Serial Stream for USB virtual serial ports interfaces
|
||||
This class is not thread-safe, except for the *Abort() methods that can be called by any thread/ISR
|
||||
*/
|
||||
|
||||
class USBSerialStream : public IOStream, IUSBHostSerialListener
|
||||
{
|
||||
public:
|
||||
enum { CIRCBUF_SIZE = 127 };
|
||||
USBSerialStream(IUSBHostSerial& serial);
|
||||
/*virtual*/ ~USBSerialStream();
|
||||
|
||||
//0 for non-blocking (returns immediately), osWaitForever for infinite blocking
|
||||
virtual int read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout=osWaitForever);
|
||||
virtual size_t available();
|
||||
virtual int waitAvailable(uint32_t timeout=osWaitForever); //Wait for data to be available
|
||||
virtual int abortRead(); //Abort current reading (or waiting) operation
|
||||
|
||||
|
||||
//0 for non-blocking (returns immediately), osWaitForever for infinite blocking
|
||||
virtual int write(uint8_t* buf, size_t length, uint32_t timeout=osWaitForever);
|
||||
virtual size_t space();
|
||||
virtual int waitSpace(uint32_t timeout=osWaitForever); //Wait for space to be available
|
||||
virtual int abortWrite(); //Abort current writing (or waiting) operation
|
||||
|
||||
private:
|
||||
IUSBHostSerial& m_serial;
|
||||
volatile bool m_serialTxFifoEmpty;
|
||||
|
||||
void setupReadableISR(bool en);
|
||||
virtual void readable(); //Callback from m_serial when new data is available
|
||||
|
||||
Semaphore m_availableSphre; //Used for signalling
|
||||
|
||||
void setupWriteableISR(bool en);
|
||||
virtual void writeable(); //Callback from m_serial when new space is available
|
||||
|
||||
Semaphore m_spaceSphre; //Used for signalling
|
||||
|
||||
MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_inBuf;
|
||||
MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_outBuf;
|
||||
};
|
||||
|
||||
#endif /* USBSERIALSTREAM_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue