MTS_SPI_Slave unfinished
Fork of MTS-Serial by
MTSSerial.cpp@12:e12b79a4ab4f, 2017-03-23 (annotated)
- Committer:
- Leon Lindenfelser
- Date:
- Thu Mar 23 16:15:14 2017 -0500
- Revision:
- 12:e12b79a4ab4f
- Parent:
- 4:d348d001283b
Update to match gitlab commit 90a9e07c8d77b31bb36e641b4361184ef2ac8558 Feb 2 2016.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
4:d348d001283b | 1 | #include "mbed.h" |
Mike Fiore |
1:d34b566d6f47 | 2 | #include "MTSSerial.h" |
Mike Fiore |
1:d34b566d6f47 | 3 | #include "MTSLog.h" |
Mike Fiore |
1:d34b566d6f47 | 4 | |
Mike Fiore |
1:d34b566d6f47 | 5 | using namespace mts; |
Mike Fiore |
1:d34b566d6f47 | 6 | |
Mike Fiore |
1:d34b566d6f47 | 7 | MTSSerial::MTSSerial(PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize) |
Mike Fiore |
1:d34b566d6f47 | 8 | : MTSBufferedIO(txBufferSize, rxBufferSize) |
Leon Lindenfelser |
12:e12b79a4ab4f | 9 | , _serial(new RawSerial(TXD,RXD)), |
Leon Lindenfelser |
12:e12b79a4ab4f | 10 | _baudrate(9600), |
Leon Lindenfelser |
12:e12b79a4ab4f | 11 | _bits(8), |
Leon Lindenfelser |
12:e12b79a4ab4f | 12 | _parity(mbed::SerialBase::None), |
Leon Lindenfelser |
12:e12b79a4ab4f | 13 | _stop_bits(1) |
Mike Fiore |
1:d34b566d6f47 | 14 | { |
Leon Lindenfelser |
12:e12b79a4ab4f | 15 | _serial->attach(this, &MTSSerial::handleRead, Serial::RxIrq); |
Mike Fiore |
1:d34b566d6f47 | 16 | } |
Mike Fiore |
1:d34b566d6f47 | 17 | |
Mike Fiore |
1:d34b566d6f47 | 18 | MTSSerial::~MTSSerial() |
Mike Fiore |
1:d34b566d6f47 | 19 | { |
Mike Fiore |
1:d34b566d6f47 | 20 | } |
Mike Fiore |
1:d34b566d6f47 | 21 | |
Leon Lindenfelser |
12:e12b79a4ab4f | 22 | |
Leon Lindenfelser |
12:e12b79a4ab4f | 23 | |
Mike Fiore |
1:d34b566d6f47 | 24 | void MTSSerial::baud(int baudrate) |
Mike Fiore |
1:d34b566d6f47 | 25 | { |
Leon Lindenfelser |
12:e12b79a4ab4f | 26 | _baudrate = baudrate; |
Leon Lindenfelser |
12:e12b79a4ab4f | 27 | _serial->baud(_baudrate); |
Mike Fiore |
1:d34b566d6f47 | 28 | } |
Mike Fiore |
1:d34b566d6f47 | 29 | |
Mike Fiore |
1:d34b566d6f47 | 30 | void MTSSerial::format(int bits, SerialBase::Parity parity, int stop_bits) |
Mike Fiore |
1:d34b566d6f47 | 31 | { |
Leon Lindenfelser |
12:e12b79a4ab4f | 32 | _bits = bits; |
Leon Lindenfelser |
12:e12b79a4ab4f | 33 | _parity = parity; |
Leon Lindenfelser |
12:e12b79a4ab4f | 34 | _stop_bits = stop_bits; |
Leon Lindenfelser |
12:e12b79a4ab4f | 35 | _serial->format(_bits, _parity, _stop_bits); |
Mike Fiore |
1:d34b566d6f47 | 36 | } |
Mike Fiore |
1:d34b566d6f47 | 37 | |
Mike Fiore |
1:d34b566d6f47 | 38 | void MTSSerial::handleRead() |
Mike Fiore |
1:d34b566d6f47 | 39 | { |
Leon Lindenfelser |
12:e12b79a4ab4f | 40 | rxBuffer.write(_serial->getc()); |
Mike Fiore |
1:d34b566d6f47 | 41 | } |
Mike Fiore |
1:d34b566d6f47 | 42 | |
Mike Fiore |
1:d34b566d6f47 | 43 | void MTSSerial::handleWrite() |
Mike Fiore |
1:d34b566d6f47 | 44 | { |
Mike Fiore |
1:d34b566d6f47 | 45 | while(txBuffer.size() != 0) { |
Leon Lindenfelser |
12:e12b79a4ab4f | 46 | if (_serial->writeable()) { |
Mike Fiore |
1:d34b566d6f47 | 47 | char byte; |
Mike Fiore |
1:d34b566d6f47 | 48 | if(txBuffer.read(byte) == 1) { |
Leon Lindenfelser |
12:e12b79a4ab4f | 49 | _serial->attach(NULL, Serial::RxIrq); |
Leon Lindenfelser |
12:e12b79a4ab4f | 50 | _serial->putc(byte); |
Leon Lindenfelser |
12:e12b79a4ab4f | 51 | _serial->attach(this, &MTSSerial::handleRead, Serial::RxIrq); |
Mike Fiore |
1:d34b566d6f47 | 52 | } |
Mike Fiore |
1:d34b566d6f47 | 53 | } else { |
Mike Fiore |
1:d34b566d6f47 | 54 | return; |
Mike Fiore |
1:d34b566d6f47 | 55 | } |
Mike Fiore |
1:d34b566d6f47 | 56 | } |
Mike Fiore |
1:d34b566d6f47 | 57 | } |
Mike Fiore |
1:d34b566d6f47 | 58 | |
Leon Lindenfelser |
12:e12b79a4ab4f | 59 | void mts::MTSSerial::reattach(PinName TXD, PinName RXD) { |
Leon Lindenfelser |
12:e12b79a4ab4f | 60 | delete _serial; |
Leon Lindenfelser |
12:e12b79a4ab4f | 61 | _serial = new RawSerial(TXD, RXD); |
Leon Lindenfelser |
12:e12b79a4ab4f | 62 | _serial->attach(this, &MTSSerial::handleRead, Serial::RxIrq); |
Leon Lindenfelser |
12:e12b79a4ab4f | 63 | _serial->baud(_baudrate); |
Leon Lindenfelser |
12:e12b79a4ab4f | 64 | _serial->format(_bits, _parity, _stop_bits); |
Leon Lindenfelser |
12:e12b79a4ab4f | 65 | rxBuffer.clear(); |
Leon Lindenfelser |
12:e12b79a4ab4f | 66 | txBuffer.clear(); |
Leon Lindenfelser |
12:e12b79a4ab4f | 67 | } |
Mike Fiore |
1:d34b566d6f47 | 68 | |
Leon Lindenfelser |
12:e12b79a4ab4f | 69 | void mts::MTSSerial::sendBreak() { |
Leon Lindenfelser |
12:e12b79a4ab4f | 70 | _serial->send_break(); |
Leon Lindenfelser |
12:e12b79a4ab4f | 71 | } |