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 MTSSerial.cpp Source File

MTSSerial.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 "MTSSerial.h"
00018 
00019 using namespace mts;
00020 
00021 MTSSerial::MTSSerial(PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize)
00022     : MTSBufferedIO(txBufferSize, rxBufferSize)
00023     , serial(TXD,RXD)
00024 {
00025     serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq);
00026     //serial.attach(this, &MTSSerial::handleWrite, Serial::TxIrq);
00027 }
00028 
00029 MTSSerial::~MTSSerial()
00030 {
00031 
00032 }
00033 
00034 void MTSSerial::baud(int baudrate)
00035 {
00036     serial.baud(baudrate);
00037 }
00038 
00039 void MTSSerial::format(int bits, SerialBase::Parity parity, int stop_bits)
00040 {
00041     serial.format(bits, parity, stop_bits);
00042 }
00043 
00044 void MTSSerial::handleRead()
00045 {
00046     while (serial.readable()) {
00047         char byte = serial.getc();
00048         if(rxBuffer.write(byte) != 1) {
00049             printf("[ERROR] Serial Rx Byte Dropped [%c][0x%02X]\r\n", byte, byte);
00050             if(byte == 0xFF) {
00051                 // hack so we dont hang - fix later
00052                 puts("[ERR] Comm errors, must reboot");
00053                 fflush(stdout);
00054                 NVIC_SystemReset();
00055             }
00056             return;
00057         }
00058     }
00059 }
00060 
00061 // Currently uses Non-Irq based blocking write calls
00062 void MTSSerial::handleWrite()
00063 {
00064     while(txBuffer.size() != 0) {
00065         if (serial.writeable()) {
00066             char byte;
00067             if(txBuffer.read(byte) == 1) {
00068                 serial.putc(byte);
00069             }
00070         } else {
00071             return;
00072         }
00073     }
00074 }
00075 
00076