Serial library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mDot_AT_firmware mtsas mtsas MTDOT-EVB-LinkCheck-AL ... more

Committer:
Mike Fiore
Date:
Tue May 20 16:13:12 2014 -0500
Revision:
4:d348d001283b
Parent:
3:8e3cb3371b09
Child:
10:b18a1064dfc6
include mbed.h in cpp files

Who changed what in which revision?

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