MTS_SPI_Slave unfinished
Fork of MTS-Serial by
Diff: MTSSerial.cpp
- Revision:
- 12:e12b79a4ab4f
- Parent:
- 4:d348d001283b
--- a/MTSSerial.cpp Mon Mar 23 16:41:30 2015 -0500 +++ b/MTSSerial.cpp Thu Mar 23 16:15:14 2017 -0500 @@ -6,42 +6,49 @@ MTSSerial::MTSSerial(PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize) : MTSBufferedIO(txBufferSize, rxBufferSize) - , serial(TXD,RXD) + , _serial(new RawSerial(TXD,RXD)), + _baudrate(9600), + _bits(8), + _parity(mbed::SerialBase::None), + _stop_bits(1) { - serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq); + _serial->attach(this, &MTSSerial::handleRead, Serial::RxIrq); } MTSSerial::~MTSSerial() { } + + void MTSSerial::baud(int baudrate) { - serial.baud(baudrate); + _baudrate = baudrate; + _serial->baud(_baudrate); } void MTSSerial::format(int bits, SerialBase::Parity parity, int stop_bits) { - serial.format(bits, parity, stop_bits); + _bits = bits; + _parity = parity; + _stop_bits = stop_bits; + _serial->format(_bits, _parity, _stop_bits); } void MTSSerial::handleRead() { - char byte = serial.getc(); - if(rxBuffer.write(byte) != 1) { - logError("Serial Rx Byte Dropped [%c][0x%02X]", byte, byte); - } + rxBuffer.write(_serial->getc()); } void MTSSerial::handleWrite() { while(txBuffer.size() != 0) { - if (serial.writeable()) { + if (_serial->writeable()) { char byte; if(txBuffer.read(byte) == 1) { - serial.attach(NULL, Serial::RxIrq); - serial.putc(byte); - serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq); + _serial->attach(NULL, Serial::RxIrq); + _serial->putc(byte); + _serial->attach(this, &MTSSerial::handleRead, Serial::RxIrq); } } else { return; @@ -49,4 +56,16 @@ } } +void mts::MTSSerial::reattach(PinName TXD, PinName RXD) { + delete _serial; + _serial = new RawSerial(TXD, RXD); + _serial->attach(this, &MTSSerial::handleRead, Serial::RxIrq); + _serial->baud(_baudrate); + _serial->format(_bits, _parity, _stop_bits); + rxBuffer.clear(); + txBuffer.clear(); +} +void mts::MTSSerial::sendBreak() { + _serial->send_break(); +}