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:
Leon Lindenfelser
Date:
Thu Mar 23 16:15:14 2017 -0500
Revision:
12:e12b79a4ab4f
Parent:
11:4afbbafcd6b3
Child:
13:643ad09fc0e0
Update to match gitlab commit 90a9e07c8d77b31bb36e641b4361184ef2ac8558 Feb 2 2016.

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 "MTSBufferedIO.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 MTSBufferedIO::MTSBufferedIO(int txBufferSize, int rxBufferSize)
Mike Fiore 1:d34b566d6f47 8 : txBuffer(txBufferSize)
Mike Fiore 1:d34b566d6f47 9 , rxBuffer(rxBufferSize)
Mike Fiore 1:d34b566d6f47 10 {
Leon Lindenfelser 12:e12b79a4ab4f 11
Mike Fiore 1:d34b566d6f47 12 }
Mike Fiore 1:d34b566d6f47 13
Mike Fiore 1:d34b566d6f47 14 MTSBufferedIO::~MTSBufferedIO()
Mike Fiore 1:d34b566d6f47 15 {
Mike Fiore 1:d34b566d6f47 16 }
Mike Fiore 1:d34b566d6f47 17
Mike Fiore 11:4afbbafcd6b3 18 int MTSBufferedIO::writef(const char* format, ...) {
Mike Fiore 11:4afbbafcd6b3 19 char buff[256];
Mike Fiore 11:4afbbafcd6b3 20
Mike Fiore 11:4afbbafcd6b3 21 va_list ap;
Mike Fiore 11:4afbbafcd6b3 22 va_start(ap, format);
Mike Fiore 11:4afbbafcd6b3 23 int len = vsnprintf(buff, 256, format, ap);
Mike Fiore 11:4afbbafcd6b3 24 while (!writeable())
Mike Fiore 11:4afbbafcd6b3 25 ;
Mike Fiore 11:4afbbafcd6b3 26 write(buff, len);
Mike Fiore 11:4afbbafcd6b3 27 va_end(ap);
Mike Fiore 11:4afbbafcd6b3 28 return len;
Leon Lindenfelser 12:e12b79a4ab4f 29 }
Mike Fiore 11:4afbbafcd6b3 30
Mike Fiore 1:d34b566d6f47 31 int MTSBufferedIO::write(const char* data, int length, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 32 {
Mike Fiore 1:d34b566d6f47 33 //Writes until empty or timeout is reached (different implementation planned once tx isr is working)
Mike Fiore 1:d34b566d6f47 34 int bytesWritten = 0;
Mike Fiore 1:d34b566d6f47 35 Timer tmr;
Mike Fiore 1:d34b566d6f47 36 tmr.start();
Mike Fiore 10:b18a1064dfc6 37 length = mts_max(0,length);
Mike Fiore 1:d34b566d6f47 38 do {
Mike Fiore 1:d34b566d6f47 39 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
Mike Fiore 1:d34b566d6f47 40 if(bytesWrittenSwBuffer > 0) {
Mike Fiore 1:d34b566d6f47 41 handleWrite();
Mike Fiore 1:d34b566d6f47 42 int bytesRemainingSwBuffer = txBuffer.size();
Mike Fiore 1:d34b566d6f47 43 txBuffer.clear();
Mike Fiore 1:d34b566d6f47 44 bytesWritten += (bytesWrittenSwBuffer - bytesRemainingSwBuffer);
Mike Fiore 1:d34b566d6f47 45 }
Leon Lindenfelser 12:e12b79a4ab4f 46 } while((unsigned)tmr.read_ms() <= timeoutMillis && bytesWritten < length);
Mike Fiore 1:d34b566d6f47 47 return bytesWritten;
Mike Fiore 1:d34b566d6f47 48 }
Mike Fiore 1:d34b566d6f47 49
Mike Fiore 1:d34b566d6f47 50 int MTSBufferedIO::write(const char* data, int length)
Mike Fiore 1:d34b566d6f47 51 {
Mike Fiore 1:d34b566d6f47 52 //Blocks until all bytes are written (different implementation planned once tx isr is working)
Mike Fiore 1:d34b566d6f47 53 int bytesWritten = 0;
Mike Fiore 10:b18a1064dfc6 54 length = mts_max(0,length);
Mike Fiore 1:d34b566d6f47 55 do {
Mike Fiore 1:d34b566d6f47 56 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
Mike Fiore 1:d34b566d6f47 57 handleWrite();
Mike Fiore 1:d34b566d6f47 58 int bytesRemainingSwBuffer = txBuffer.size();
Mike Fiore 1:d34b566d6f47 59 txBuffer.clear();
Mike Fiore 1:d34b566d6f47 60 bytesWritten += bytesWrittenSwBuffer - bytesRemainingSwBuffer;
Mike Fiore 1:d34b566d6f47 61 } while(bytesWritten < length);
Mike Fiore 1:d34b566d6f47 62 return length;
Mike Fiore 1:d34b566d6f47 63 }
Mike Fiore 1:d34b566d6f47 64
Mike Fiore 1:d34b566d6f47 65 int MTSBufferedIO::write(char data, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 66 {
Mike Fiore 1:d34b566d6f47 67 return write(&data, 1, timeoutMillis);
Mike Fiore 1:d34b566d6f47 68 }
Mike Fiore 1:d34b566d6f47 69
Mike Fiore 1:d34b566d6f47 70 int MTSBufferedIO::write(char data)
Mike Fiore 1:d34b566d6f47 71 {
Mike Fiore 1:d34b566d6f47 72 return write(&data, 1);
Mike Fiore 1:d34b566d6f47 73 }
Mike Fiore 1:d34b566d6f47 74
Mike Fiore 1:d34b566d6f47 75 int MTSBufferedIO::writeable() {
Mike Fiore 1:d34b566d6f47 76 return txBuffer.remaining();
Mike Fiore 1:d34b566d6f47 77 }
Mike Fiore 1:d34b566d6f47 78
Mike Fiore 1:d34b566d6f47 79 int MTSBufferedIO::read(char* data, int length, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 80 {
Mike Fiore 1:d34b566d6f47 81 int bytesRead = 0;
Mike Fiore 1:d34b566d6f47 82 Timer tmr;
Mike Fiore 1:d34b566d6f47 83 tmr.start();
Mike Fiore 10:b18a1064dfc6 84 length = mts_max(0,length);
Mike Fiore 1:d34b566d6f47 85 do {
Mike Fiore 1:d34b566d6f47 86 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
Leon Lindenfelser 12:e12b79a4ab4f 87 } while((unsigned)tmr.read_ms() <= timeoutMillis && bytesRead < length);
Mike Fiore 1:d34b566d6f47 88 return bytesRead;
Mike Fiore 1:d34b566d6f47 89 }
Mike Fiore 1:d34b566d6f47 90
Mike Fiore 1:d34b566d6f47 91 int MTSBufferedIO::read(char* data, int length)
Mike Fiore 1:d34b566d6f47 92 {
Mike Fiore 1:d34b566d6f47 93 int bytesRead = 0;
Mike Fiore 10:b18a1064dfc6 94 length = mts_max(0,length);
Mike Fiore 1:d34b566d6f47 95 while(bytesRead < length) {
Mike Fiore 1:d34b566d6f47 96 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
Mike Fiore 1:d34b566d6f47 97 }
Mike Fiore 1:d34b566d6f47 98 return length;
Mike Fiore 1:d34b566d6f47 99 }
Mike Fiore 1:d34b566d6f47 100
Mike Fiore 1:d34b566d6f47 101 int MTSBufferedIO::read(char& data, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 102 {
Mike Fiore 1:d34b566d6f47 103 return read(&data, 1, timeoutMillis);
Mike Fiore 1:d34b566d6f47 104 }
Mike Fiore 1:d34b566d6f47 105
Mike Fiore 1:d34b566d6f47 106 int MTSBufferedIO::read(char& data)
Mike Fiore 1:d34b566d6f47 107 {
Mike Fiore 1:d34b566d6f47 108 return rxBuffer.read(&data, 1);
Mike Fiore 1:d34b566d6f47 109 }
Mike Fiore 1:d34b566d6f47 110
Mike Fiore 1:d34b566d6f47 111 int MTSBufferedIO::readable() {
Mike Fiore 1:d34b566d6f47 112 return rxBuffer.size();
Mike Fiore 1:d34b566d6f47 113 }
Mike Fiore 1:d34b566d6f47 114
Mike Fiore 1:d34b566d6f47 115 bool MTSBufferedIO::txEmpty()
Mike Fiore 1:d34b566d6f47 116 {
Mike Fiore 1:d34b566d6f47 117 return txBuffer.isEmpty();
Mike Fiore 1:d34b566d6f47 118 }
Mike Fiore 1:d34b566d6f47 119
Mike Fiore 1:d34b566d6f47 120 bool MTSBufferedIO::rxEmpty()
Mike Fiore 1:d34b566d6f47 121 {
Mike Fiore 1:d34b566d6f47 122 return rxBuffer.isEmpty();
Mike Fiore 1:d34b566d6f47 123 }
Mike Fiore 1:d34b566d6f47 124
Mike Fiore 1:d34b566d6f47 125 bool MTSBufferedIO::txFull()
Mike Fiore 1:d34b566d6f47 126 {
Mike Fiore 1:d34b566d6f47 127 return txBuffer.isFull();
Mike Fiore 1:d34b566d6f47 128 }
Mike Fiore 1:d34b566d6f47 129
Mike Fiore 1:d34b566d6f47 130 bool MTSBufferedIO::rxFull()
Mike Fiore 1:d34b566d6f47 131 {
Mike Fiore 1:d34b566d6f47 132 return rxBuffer.isFull();
Mike Fiore 1:d34b566d6f47 133 }
Mike Fiore 1:d34b566d6f47 134
Mike Fiore 1:d34b566d6f47 135 void MTSBufferedIO::txClear()
Mike Fiore 1:d34b566d6f47 136 {
Mike Fiore 1:d34b566d6f47 137 txBuffer.clear();
Mike Fiore 1:d34b566d6f47 138 }
Mike Fiore 1:d34b566d6f47 139
Mike Fiore 1:d34b566d6f47 140 void MTSBufferedIO::rxClear()
Mike Fiore 1:d34b566d6f47 141 {
Mike Fiore 1:d34b566d6f47 142 rxBuffer.clear();
Mike Fiore 1:d34b566d6f47 143 }