MTS_SPI_Slave unfinished

Fork of MTS-Serial by MultiTech

Committer:
Mike Fiore
Date:
Mon May 19 14:38:24 2014 -0500
Revision:
3:8e3cb3371b09
Parent:
1:d34b566d6f47
Child:
4:d348d001283b
replace inclusion of Vars.h with Utils.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 1:d34b566d6f47 1 #include "MTSSerialFlowControl.h"
Mike Fiore 1:d34b566d6f47 2 #include "MTSLog.h"
Mike Fiore 3:8e3cb3371b09 3 #include "Utils.h"
Mike Fiore 1:d34b566d6f47 4
Mike Fiore 1:d34b566d6f47 5 using namespace mts;
Mike Fiore 1:d34b566d6f47 6
Mike Fiore 1:d34b566d6f47 7 MTSSerialFlowControl::MTSSerialFlowControl(PinName TXD, PinName RXD, PinName RTS, PinName CTS, int txBufSize, int rxBufSize)
Mike Fiore 1:d34b566d6f47 8 : MTSSerial(TXD, RXD, txBufSize, rxBufSize)
Mike Fiore 1:d34b566d6f47 9 , rxReadyFlag(false)
Mike Fiore 1:d34b566d6f47 10 , rts(RTS)
Mike Fiore 1:d34b566d6f47 11 , cts(CTS)
Mike Fiore 1:d34b566d6f47 12 {
Mike Fiore 1:d34b566d6f47 13 notifyStartSending();
Mike Fiore 1:d34b566d6f47 14
Mike Fiore 1:d34b566d6f47 15 // Calculate the high and low watermark values
Mike Fiore 1:d34b566d6f47 16 highThreshold = MAX(rxBufSize - 10, rxBufSize * 0.85);
Mike Fiore 1:d34b566d6f47 17 lowThreshold = rxBufSize * 0.3;
Mike Fiore 1:d34b566d6f47 18
Mike Fiore 1:d34b566d6f47 19 // Setup the low watermark callback on the internal receive buffer
Mike Fiore 1:d34b566d6f47 20 rxBuffer.attach(this, &MTSSerialFlowControl::notifyStartSending, lowThreshold, LESS);
Mike Fiore 1:d34b566d6f47 21 }
Mike Fiore 1:d34b566d6f47 22
Mike Fiore 1:d34b566d6f47 23 MTSSerialFlowControl::~MTSSerialFlowControl()
Mike Fiore 1:d34b566d6f47 24 {
Mike Fiore 1:d34b566d6f47 25 }
Mike Fiore 1:d34b566d6f47 26
Mike Fiore 1:d34b566d6f47 27 //Override the rxClear function to make sure that flow control lines are set correctly.
Mike Fiore 1:d34b566d6f47 28 void MTSSerialFlowControl::rxClear()
Mike Fiore 1:d34b566d6f47 29 {
Mike Fiore 1:d34b566d6f47 30 MTSBufferedIO::rxClear();
Mike Fiore 1:d34b566d6f47 31 notifyStartSending();
Mike Fiore 1:d34b566d6f47 32 }
Mike Fiore 1:d34b566d6f47 33
Mike Fiore 1:d34b566d6f47 34 void MTSSerialFlowControl::notifyStartSending()
Mike Fiore 1:d34b566d6f47 35 {
Mike Fiore 1:d34b566d6f47 36 if(!rxReadyFlag) {
Mike Fiore 1:d34b566d6f47 37 rts.write(0);
Mike Fiore 1:d34b566d6f47 38 rxReadyFlag = true;
Mike Fiore 1:d34b566d6f47 39 //printf("RTS LOW: READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity());
Mike Fiore 1:d34b566d6f47 40 }
Mike Fiore 1:d34b566d6f47 41 }
Mike Fiore 1:d34b566d6f47 42
Mike Fiore 1:d34b566d6f47 43 void MTSSerialFlowControl::notifyStopSending()
Mike Fiore 1:d34b566d6f47 44 {
Mike Fiore 1:d34b566d6f47 45 if(rxReadyFlag) {
Mike Fiore 1:d34b566d6f47 46 rts.write(1);
Mike Fiore 1:d34b566d6f47 47 rxReadyFlag = false;
Mike Fiore 1:d34b566d6f47 48 //printf("RTS HIGH: NOT-READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity());
Mike Fiore 1:d34b566d6f47 49 }
Mike Fiore 1:d34b566d6f47 50 }
Mike Fiore 1:d34b566d6f47 51
Mike Fiore 1:d34b566d6f47 52 void MTSSerialFlowControl::handleRead()
Mike Fiore 1:d34b566d6f47 53 {
Mike Fiore 1:d34b566d6f47 54 char byte = serial.getc();
Mike Fiore 1:d34b566d6f47 55 if(rxBuffer.write(byte) != 1) {
Mike Fiore 1:d34b566d6f47 56 logError("Serial Rx Byte Dropped [%c][0x%02X]", byte, byte);
Mike Fiore 1:d34b566d6f47 57 }
Mike Fiore 1:d34b566d6f47 58 if (rxBuffer.size() >= highThreshold) {
Mike Fiore 1:d34b566d6f47 59 notifyStopSending();
Mike Fiore 1:d34b566d6f47 60 }
Mike Fiore 1:d34b566d6f47 61 }
Mike Fiore 1:d34b566d6f47 62
Mike Fiore 1:d34b566d6f47 63 void MTSSerialFlowControl::handleWrite()
Mike Fiore 1:d34b566d6f47 64 {
Mike Fiore 1:d34b566d6f47 65 while(txBuffer.size() != 0) {
Mike Fiore 1:d34b566d6f47 66 if (serial.writeable() && cts.read() == 0) {
Mike Fiore 1:d34b566d6f47 67 char byte;
Mike Fiore 1:d34b566d6f47 68 if(txBuffer.read(byte) == 1) {
Mike Fiore 1:d34b566d6f47 69 serial.attach(NULL, Serial::RxIrq);
Mike Fiore 1:d34b566d6f47 70 serial.putc(byte);
Mike Fiore 1:d34b566d6f47 71 serial.attach(this, &MTSSerialFlowControl::handleRead, Serial::RxIrq);
Mike Fiore 1:d34b566d6f47 72 }
Mike Fiore 1:d34b566d6f47 73 } else {
Mike Fiore 1:d34b566d6f47 74 return;
Mike Fiore 1:d34b566d6f47 75 }
Mike Fiore 1:d34b566d6f47 76 }
Mike Fiore 1:d34b566d6f47 77 }
Mike Fiore 1:d34b566d6f47 78