A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MTSSerialFlowControl.cpp Source File

MTSSerialFlowControl.cpp

00001 /* Universal Socket Modem Interface Library
00002 * Copyright (c) 2013 Multi-Tech Systems
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 */
00016 
00017 #include "MTSSerialFlowControl.h"
00018 
00019 using namespace mts;
00020 
00021 MTSSerialFlowControl::MTSSerialFlowControl(PinName TXD, PinName RXD, PinName RTS, PinName CTS, int txBufSize, int rxBufSize)
00022     : MTSSerial(TXD, RXD, txBufSize, rxBufSize)
00023     , rxReadyFlag(false)
00024     , rts(RTS)
00025     , cts(CTS)
00026 {
00027     notifyStartSending();
00028 
00029     highThreshold = MAX(rxBufSize - 10, rxBufSize * 0.85);
00030     lowThreshold = rxBufSize * 0.3;
00031 
00032     rxBuffer.attach(this, &MTSSerialFlowControl::notifyStartSending, lowThreshold, Vars::LESS);
00033 }
00034 
00035 MTSSerialFlowControl::~MTSSerialFlowControl()
00036 {
00037 
00038 }
00039 
00040 void MTSSerialFlowControl::rxClear()
00041 {
00042     MTSBufferedIO::rxClear();
00043     notifyStartSending();
00044 }
00045 
00046 void MTSSerialFlowControl::notifyStartSending()
00047 {
00048     if(!rxReadyFlag) {
00049         rts.write(0);
00050         rxReadyFlag = true;
00051         //printf("RTS LOW: READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity());
00052     }
00053 }
00054 
00055 void MTSSerialFlowControl::notifyStopSending()
00056 {
00057     if(rxReadyFlag) {
00058         rts.write(1);
00059         rxReadyFlag = false;
00060         //printf("RTS HIGH: NOT-READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity());
00061     }
00062 }
00063 
00064 void MTSSerialFlowControl::handleRead()
00065 {
00066     char byte = serial.getc();
00067     if(rxBuffer.write(byte) != 1) {
00068         printf("[ERROR] Serial Rx Byte Dropped [%c][0x%02X]\r\n", byte, byte);
00069     }
00070     if (rxBuffer.size() > highThreshold) {
00071         notifyStopSending();
00072     }
00073 }
00074 
00075 void MTSSerialFlowControl::handleWrite()
00076 {
00077     while(txBuffer.size() != 0) {
00078         if (serial.writeable() && cts.read() == 0) {
00079             char byte;
00080             if(txBuffer.read(byte) == 1) {
00081                 serial.attach(NULL, Serial::RxIrq);
00082                 serial.putc(byte);
00083                 serial.attach(this, &MTSSerialFlowControl::handleRead, Serial::RxIrq);
00084             }
00085         } else {
00086             return;
00087         }
00088     }
00089 }
00090