Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z revert by Axeda Corp

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     while (serial.readable()) {
00067         char byte = serial.getc();
00068         if(rxBuffer.write(byte) != 1) {
00069             rts.write(1);
00070             rxReadyFlag = false;
00071             printf("[ERROR] Serial Rx Byte Dropped [%c][0x%02X]\r\n", byte, byte);
00072             if(byte == 0xFF) {
00073                 // hack so we dont hang - fix later
00074                 puts("[ERR] Comm errors, must reboot");
00075                 fflush(stdout);
00076                 NVIC_SystemReset();
00077             }
00078             return;
00079         }
00080         if (rxBuffer.size() > highThreshold) {
00081             notifyStopSending();
00082         }
00083     }
00084 }
00085 
00086 void MTSSerialFlowControl::handleWrite()
00087 {
00088     while(txBuffer.size() != 0) {
00089         if (serial.writeable() && cts.read() == 0) {
00090             char byte;
00091             if(txBuffer.read(byte) == 1) {
00092                 serial.putc(byte);
00093             }
00094         } else {
00095             return;
00096         }
00097     }
00098 }
00099