PN532 Driver library This library provides an abstract API to drive the pn532 nfc chip, with I2C/HSU/SPI interface. Its based on the Seeed Studio's Arduino version.

Dependents:   PN532_ReadUid Nfctest2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BufferedSerial.cpp Source File

BufferedSerial.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    BufferedSerial.cpp
00003  * @brief   Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX
00004  * @author  sam grove
00005  * @version 1.0
00006  * @see
00007  *
00008  * Copyright (c) 2013
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022 
00023 #include "BufferedSerial.h"
00024 #include <stdarg.h>
00025 
00026 BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name)
00027     : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size))
00028 {
00029     RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
00030     this->_buf_size = buf_size;
00031     this->_tx_multiple = tx_multiple;   
00032     return;
00033 }
00034 
00035 BufferedSerial::~BufferedSerial(void)
00036 {
00037     RawSerial::attach(NULL, RawSerial::RxIrq);
00038     RawSerial::attach(NULL, RawSerial::TxIrq);
00039 
00040     return;
00041 }
00042 
00043 int BufferedSerial::readable(void)
00044 {
00045     return _rxbuf.available();  // note: look if things are in the buffer
00046 }
00047 
00048 int BufferedSerial::writeable(void)
00049 {
00050     return 1;   // buffer allows overwriting by design, always true
00051 }
00052 
00053 int BufferedSerial::getc(void)
00054 {
00055     if (_rxbuf.available())
00056         return _rxbuf;
00057     else
00058         return -1;      // no more data
00059 }
00060 
00061 int BufferedSerial::putc(int c)
00062 {
00063     _txbuf = (char)c;
00064     BufferedSerial::prime();
00065 
00066     return c;
00067 }
00068 
00069 int BufferedSerial::puts(const char *s)
00070 {
00071     if (s != NULL) {
00072         const char* ptr = s;
00073     
00074         while(*(ptr) != 0) {
00075             _txbuf = *(ptr++);
00076         }
00077         _txbuf = '\n';  // done per puts definition
00078         BufferedSerial::prime();
00079     
00080         return (ptr - s) + 1;
00081     }
00082     return 0;
00083 }
00084 
00085 int BufferedSerial::printf(const char* format, ...)
00086 {
00087     char buffer[this->_buf_size];
00088     memset(buffer,0,this->_buf_size);
00089     int r = 0;
00090 
00091     va_list arg;
00092     va_start(arg, format);
00093     r = vsprintf(buffer, format, arg);
00094     // this may not hit the heap but should alert the user anyways
00095     if(r > this->_buf_size) {
00096         error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,this->_buf_size,r);
00097         va_end(arg);
00098         return 0;
00099     }
00100     va_end(arg);
00101     r = BufferedSerial::write(buffer, r);
00102 
00103     return r;
00104 }
00105 
00106 ssize_t BufferedSerial::write(const void *s, size_t length)
00107 {
00108     if (s != NULL && length > 0) {
00109         const char* ptr = (const char*)s;
00110         const char* end = ptr + length;
00111     
00112         while (ptr != end) {
00113             _txbuf = *(ptr++);
00114         }
00115         BufferedSerial::prime();
00116     
00117         return ptr - (const char*)s;
00118     }
00119     return 0;
00120 }
00121 
00122 
00123 void BufferedSerial::rxIrq(void)
00124 {
00125     // read from the peripheral and make sure something is available
00126     if(serial_readable(&_serial)) {
00127         _rxbuf = serial_getc(&_serial); // if so load them into a buffer
00128     }
00129 
00130     return;
00131 }
00132 
00133 void BufferedSerial::txIrq(void)
00134 {
00135     // see if there is room in the hardware fifo and if something is in the software fifo
00136     while(serial_writable(&_serial)) {
00137         if(_txbuf.available()) {
00138             serial_putc(&_serial, (int)_txbuf.get());
00139         } else {
00140             // disable the TX interrupt when there is nothing left to send
00141             RawSerial::attach(NULL, RawSerial::TxIrq);
00142             break;
00143         }
00144     }
00145 
00146     return;
00147 }
00148 
00149 void BufferedSerial::prime(void)
00150 {
00151     // if already busy then the irq will pick this up
00152     if(serial_writable(&_serial)) {
00153         RawSerial::attach(NULL, RawSerial::TxIrq);    // make sure not to cause contention in the irq
00154         BufferedSerial::txIrq();                // only write to hardware in one place
00155         RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
00156     }
00157 
00158     return;
00159 }
00160 
00161 
00162 size_t BufferedSerial::write(uint8_t val) 
00163 {
00164     putc (val);
00165     return 1;
00166 }