Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

io/MTSSerial.cpp

Committer:
AntonLS
Date:
2015-06-30
Revision:
15:b86c4b798aa1
Parent:
11:d3aa5fca2330
Child:
18:affef3a7db2a

File content as of revision 15:b86c4b798aa1:

/* Universal Socket Modem Interface Library
* Copyright (c) 2013 Multi-Tech Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "MTSSerial.h"

// Added new RTS CTS args.    ALS  20150413

using namespace mts;

MTSSerial::MTSSerial( PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize,
                      PinName RTS, PinName CTS )
    : MTSBufferedIO(txBufferSize, rxBufferSize)
    , serial( TXD, RXD, NULL, RTS, CTS )
{
    enableRxIrq();
//  serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq);
    enableTxIrq();
    //serial.attach(this, &MTSSerial::handleWrite, Serial::TxIrq);
}

MTSSerial::~MTSSerial()
{

}

void MTSSerial::baud(int baudrate)
{
    serial.baud(baudrate);
}

void MTSSerial::format(int bits, SerialBase::Parity parity, int stop_bits)
{
    serial.format(bits, parity, stop_bits);
}

extern DigitalOut led0;  //  ALS

void MTSSerial::handleRead()
{
    char byte = serial.getc();
    if(rxBuffer.write(byte) != 1) {
    //  printf("[ERROR] Serial Rx Byte Dropped [%c][0x%02X]\r\n", byte, byte);
        printf( " ! " );
    }
    led0 = !led0;  // Toggle LED on char in.  ALS
}

// Currently uses Non-Irq based blocking write calls   -- Now uses TXDRDY IRQ  ALS  20150419
void MTSSerial::handleWrite()
{
    /* while */ if(txBuffer.size() != 0) {
        if (serial.writeable()) {
            char byte;
            if(txBuffer.read(byte) == 1) {
                //serial.attach(NULL, Serial::RxIrq);
                disableRxIrq();
                serial.putc(byte);
                //serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq);
                enableRxIrq();
            }
        } else {
            return;
        }
    } else if( serial.writeable() )  disableTxIrq();  // Prevent needless IRQs, but keep TXDRDY set.  ALS
}

// TXDRDY IRQ enable/disable   ALS  20150419
void MTSSerial::disableTxIrq()
{
    serial.attach( NULL, Serial::TxIrq );
}

void MTSSerial::enableTxIrq()
{
    serial.attach( this, &MTSSerial::handleWrite, Serial::TxIrq );

    handleWrite();  // Start the ball rolling if necessary.
}

// RXDRDY IRQ enable/disable   ALS  20150420
void MTSSerial::disableRxIrq()
{
    serial.attach( NULL, Serial::RxIrq );
}

void MTSSerial::enableRxIrq()
{
    serial.attach( this, &MTSSerial::handleRead, Serial::RxIrq );
}