MTS_SPI_Slave unfinished
Fork of MTS-Serial by
MTSSerial.cpp@14:d5a86071845e, 2017-11-16 (annotated)
- Committer:
- ScottHoppeMultitech
- Date:
- Thu Nov 16 17:02:41 2017 +0000
- Revision:
- 14:d5a86071845e
- Parent:
- 4:d348d001283b
- Child:
- 12:e12b79a4ab4f
added Thread member
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) |
Mike Fiore |
1:d34b566d6f47 | 9 | , serial(TXD,RXD) |
Mike Fiore |
1:d34b566d6f47 | 10 | { |
Mike Fiore |
1:d34b566d6f47 | 11 | serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq); |
Mike Fiore |
1:d34b566d6f47 | 12 | } |
Mike Fiore |
1:d34b566d6f47 | 13 | |
Mike Fiore |
1:d34b566d6f47 | 14 | MTSSerial::~MTSSerial() |
Mike Fiore |
1:d34b566d6f47 | 15 | { |
Mike Fiore |
1:d34b566d6f47 | 16 | } |
Mike Fiore |
1:d34b566d6f47 | 17 | |
Mike Fiore |
1:d34b566d6f47 | 18 | void MTSSerial::baud(int baudrate) |
Mike Fiore |
1:d34b566d6f47 | 19 | { |
Mike Fiore |
1:d34b566d6f47 | 20 | serial.baud(baudrate); |
Mike Fiore |
1:d34b566d6f47 | 21 | } |
Mike Fiore |
1:d34b566d6f47 | 22 | |
Mike Fiore |
1:d34b566d6f47 | 23 | void MTSSerial::format(int bits, SerialBase::Parity parity, int stop_bits) |
Mike Fiore |
1:d34b566d6f47 | 24 | { |
Mike Fiore |
1:d34b566d6f47 | 25 | serial.format(bits, parity, stop_bits); |
Mike Fiore |
1:d34b566d6f47 | 26 | } |
Mike Fiore |
1:d34b566d6f47 | 27 | |
Mike Fiore |
1:d34b566d6f47 | 28 | void MTSSerial::handleRead() |
Mike Fiore |
1:d34b566d6f47 | 29 | { |
Mike Fiore |
1:d34b566d6f47 | 30 | char byte = serial.getc(); |
Mike Fiore |
1:d34b566d6f47 | 31 | if(rxBuffer.write(byte) != 1) { |
Mike Fiore |
1:d34b566d6f47 | 32 | logError("Serial Rx Byte Dropped [%c][0x%02X]", byte, byte); |
Mike Fiore |
1:d34b566d6f47 | 33 | } |
Mike Fiore |
1:d34b566d6f47 | 34 | } |
Mike Fiore |
1:d34b566d6f47 | 35 | |
Mike Fiore |
1:d34b566d6f47 | 36 | void MTSSerial::handleWrite() |
Mike Fiore |
1:d34b566d6f47 | 37 | { |
Mike Fiore |
1:d34b566d6f47 | 38 | while(txBuffer.size() != 0) { |
Mike Fiore |
1:d34b566d6f47 | 39 | if (serial.writeable()) { |
Mike Fiore |
1:d34b566d6f47 | 40 | char byte; |
Mike Fiore |
1:d34b566d6f47 | 41 | if(txBuffer.read(byte) == 1) { |
Mike Fiore |
1:d34b566d6f47 | 42 | serial.attach(NULL, Serial::RxIrq); |
Mike Fiore |
1:d34b566d6f47 | 43 | serial.putc(byte); |
Mike Fiore |
1:d34b566d6f47 | 44 | serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq); |
Mike Fiore |
1:d34b566d6f47 | 45 | } |
Mike Fiore |
1:d34b566d6f47 | 46 | } else { |
Mike Fiore |
1:d34b566d6f47 | 47 | return; |
Mike Fiore |
1:d34b566d6f47 | 48 | } |
Mike Fiore |
1:d34b566d6f47 | 49 | } |
Mike Fiore |
1:d34b566d6f47 | 50 | } |
Mike Fiore |
1:d34b566d6f47 | 51 | |
Mike Fiore |
1:d34b566d6f47 | 52 |