Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:f1d3878b8dd9 1 /**
vpcola 0:f1d3878b8dd9 2 * @file BufferedSerial.cpp
vpcola 0:f1d3878b8dd9 3 * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX
vpcola 0:f1d3878b8dd9 4 * @author sam grove
vpcola 0:f1d3878b8dd9 5 * @version 1.0
vpcola 0:f1d3878b8dd9 6 * @see
vpcola 0:f1d3878b8dd9 7 *
vpcola 0:f1d3878b8dd9 8 * Copyright (c) 2013
vpcola 0:f1d3878b8dd9 9 *
vpcola 0:f1d3878b8dd9 10 * Licensed under the Apache License, Version 2.0 (the "License");
vpcola 0:f1d3878b8dd9 11 * you may not use this file except in compliance with the License.
vpcola 0:f1d3878b8dd9 12 * You may obtain a copy of the License at
vpcola 0:f1d3878b8dd9 13 *
vpcola 0:f1d3878b8dd9 14 * http://www.apache.org/licenses/LICENSE-2.0
vpcola 0:f1d3878b8dd9 15 *
vpcola 0:f1d3878b8dd9 16 * Unless required by applicable law or agreed to in writing, software
vpcola 0:f1d3878b8dd9 17 * distributed under the License is distributed on an "AS IS" BASIS,
vpcola 0:f1d3878b8dd9 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vpcola 0:f1d3878b8dd9 19 * See the License for the specific language governing permissions and
vpcola 0:f1d3878b8dd9 20 * limitations under the License.
vpcola 0:f1d3878b8dd9 21 */
vpcola 0:f1d3878b8dd9 22
vpcola 0:f1d3878b8dd9 23 #include "BufferedSerial.h"
vpcola 0:f1d3878b8dd9 24 #include <stdarg.h>
vpcola 0:f1d3878b8dd9 25
vpcola 0:f1d3878b8dd9 26 extern "C" int BufferedPrintfC(void *stream, int size, const char* format, va_list arg);
vpcola 0:f1d3878b8dd9 27
vpcola 0:f1d3878b8dd9 28 BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name)
vpcola 0:f1d3878b8dd9 29 : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size))
vpcola 0:f1d3878b8dd9 30 {
vpcola 0:f1d3878b8dd9 31 RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
vpcola 0:f1d3878b8dd9 32 this->_buf_size = buf_size;
vpcola 0:f1d3878b8dd9 33 this->_tx_multiple = tx_multiple;
vpcola 0:f1d3878b8dd9 34 return;
vpcola 0:f1d3878b8dd9 35 }
vpcola 0:f1d3878b8dd9 36
vpcola 0:f1d3878b8dd9 37 BufferedSerial::~BufferedSerial(void)
vpcola 0:f1d3878b8dd9 38 {
vpcola 0:f1d3878b8dd9 39 RawSerial::attach(NULL, RawSerial::RxIrq);
vpcola 0:f1d3878b8dd9 40 RawSerial::attach(NULL, RawSerial::TxIrq);
vpcola 0:f1d3878b8dd9 41
vpcola 0:f1d3878b8dd9 42 return;
vpcola 0:f1d3878b8dd9 43 }
vpcola 0:f1d3878b8dd9 44
vpcola 0:f1d3878b8dd9 45 int BufferedSerial::readable(void)
vpcola 0:f1d3878b8dd9 46 {
vpcola 0:f1d3878b8dd9 47 return _rxbuf.available(); // note: look if things are in the buffer
vpcola 0:f1d3878b8dd9 48 }
vpcola 0:f1d3878b8dd9 49
vpcola 0:f1d3878b8dd9 50 int BufferedSerial::writeable(void)
vpcola 0:f1d3878b8dd9 51 {
vpcola 0:f1d3878b8dd9 52 return 1; // buffer allows overwriting by design, always true
vpcola 0:f1d3878b8dd9 53 }
vpcola 0:f1d3878b8dd9 54
vpcola 0:f1d3878b8dd9 55 int BufferedSerial::getc(void)
vpcola 0:f1d3878b8dd9 56 {
vpcola 0:f1d3878b8dd9 57 return _rxbuf;
vpcola 0:f1d3878b8dd9 58 }
vpcola 0:f1d3878b8dd9 59
vpcola 0:f1d3878b8dd9 60 int BufferedSerial::putc(int c)
vpcola 0:f1d3878b8dd9 61 {
vpcola 0:f1d3878b8dd9 62 _txbuf = (char)c;
vpcola 0:f1d3878b8dd9 63 BufferedSerial::prime();
vpcola 0:f1d3878b8dd9 64
vpcola 0:f1d3878b8dd9 65 return c;
vpcola 0:f1d3878b8dd9 66 }
vpcola 0:f1d3878b8dd9 67
vpcola 0:f1d3878b8dd9 68 int BufferedSerial::puts(const char *s)
vpcola 0:f1d3878b8dd9 69 {
vpcola 0:f1d3878b8dd9 70 if (s != NULL) {
vpcola 0:f1d3878b8dd9 71 const char* ptr = s;
vpcola 0:f1d3878b8dd9 72
vpcola 0:f1d3878b8dd9 73 while(*(ptr) != 0) {
vpcola 0:f1d3878b8dd9 74 _txbuf = *(ptr++);
vpcola 0:f1d3878b8dd9 75 }
vpcola 0:f1d3878b8dd9 76 _txbuf = '\n'; // done per puts definition
vpcola 0:f1d3878b8dd9 77 BufferedSerial::prime();
vpcola 0:f1d3878b8dd9 78
vpcola 0:f1d3878b8dd9 79 return (ptr - s) + 1;
vpcola 0:f1d3878b8dd9 80 }
vpcola 0:f1d3878b8dd9 81 return 0;
vpcola 0:f1d3878b8dd9 82 }
vpcola 0:f1d3878b8dd9 83
vpcola 0:f1d3878b8dd9 84 extern "C" size_t BufferedSerialThunk(void *buf_serial, const void *s, size_t length)
vpcola 0:f1d3878b8dd9 85 {
vpcola 0:f1d3878b8dd9 86 BufferedSerial *buffered_serial = (BufferedSerial *)buf_serial;
vpcola 0:f1d3878b8dd9 87 return buffered_serial->write(s, length);
vpcola 0:f1d3878b8dd9 88 }
vpcola 0:f1d3878b8dd9 89
vpcola 0:f1d3878b8dd9 90 int BufferedSerial::printf(const char* format, ...)
vpcola 0:f1d3878b8dd9 91 {
vpcola 0:f1d3878b8dd9 92 va_list arg;
vpcola 0:f1d3878b8dd9 93 va_start(arg, format);
vpcola 0:f1d3878b8dd9 94 int r = BufferedPrintfC((void*)this, this->_buf_size, format, arg);
vpcola 0:f1d3878b8dd9 95 va_end(arg);
vpcola 0:f1d3878b8dd9 96 return r;
vpcola 0:f1d3878b8dd9 97 }
vpcola 0:f1d3878b8dd9 98
vpcola 0:f1d3878b8dd9 99 ssize_t BufferedSerial::write(const void *s, size_t length)
vpcola 0:f1d3878b8dd9 100 {
vpcola 0:f1d3878b8dd9 101 if (s != NULL && length > 0) {
vpcola 0:f1d3878b8dd9 102 const char* ptr = (const char*)s;
vpcola 0:f1d3878b8dd9 103 const char* end = ptr + length;
vpcola 0:f1d3878b8dd9 104
vpcola 0:f1d3878b8dd9 105 while (ptr != end) {
vpcola 0:f1d3878b8dd9 106 _txbuf = *(ptr++);
vpcola 0:f1d3878b8dd9 107 }
vpcola 0:f1d3878b8dd9 108 BufferedSerial::prime();
vpcola 0:f1d3878b8dd9 109
vpcola 0:f1d3878b8dd9 110 return ptr - (const char*)s;
vpcola 0:f1d3878b8dd9 111 }
vpcola 0:f1d3878b8dd9 112 return 0;
vpcola 0:f1d3878b8dd9 113 }
vpcola 0:f1d3878b8dd9 114
vpcola 0:f1d3878b8dd9 115
vpcola 0:f1d3878b8dd9 116 void BufferedSerial::rxIrq(void)
vpcola 0:f1d3878b8dd9 117 {
vpcola 0:f1d3878b8dd9 118 // read from the peripheral and make sure something is available
vpcola 0:f1d3878b8dd9 119 if(serial_readable(&_serial)) {
vpcola 0:f1d3878b8dd9 120 _rxbuf = serial_getc(&_serial); // if so load them into a buffer
vpcola 0:f1d3878b8dd9 121 // trigger callback if necessary
vpcola 0:f1d3878b8dd9 122 if (_cbs[RxIrq]) {
vpcola 0:f1d3878b8dd9 123 _cbs[RxIrq]();
vpcola 0:f1d3878b8dd9 124 }
vpcola 0:f1d3878b8dd9 125 }
vpcola 0:f1d3878b8dd9 126
vpcola 0:f1d3878b8dd9 127 return;
vpcola 0:f1d3878b8dd9 128 }
vpcola 0:f1d3878b8dd9 129
vpcola 0:f1d3878b8dd9 130 void BufferedSerial::txIrq(void)
vpcola 0:f1d3878b8dd9 131 {
vpcola 0:f1d3878b8dd9 132 // see if there is room in the hardware fifo and if something is in the software fifo
vpcola 0:f1d3878b8dd9 133 while(serial_writable(&_serial)) {
vpcola 0:f1d3878b8dd9 134 if(_txbuf.available()) {
vpcola 0:f1d3878b8dd9 135 serial_putc(&_serial, (int)_txbuf.get());
vpcola 0:f1d3878b8dd9 136 } else {
vpcola 0:f1d3878b8dd9 137 // disable the TX interrupt when there is nothing left to send
vpcola 0:f1d3878b8dd9 138 RawSerial::attach(NULL, RawSerial::TxIrq);
vpcola 0:f1d3878b8dd9 139 // trigger callback if necessary
vpcola 0:f1d3878b8dd9 140 if (_cbs[TxIrq]) {
vpcola 0:f1d3878b8dd9 141 _cbs[TxIrq]();
vpcola 0:f1d3878b8dd9 142 }
vpcola 0:f1d3878b8dd9 143 break;
vpcola 0:f1d3878b8dd9 144 }
vpcola 0:f1d3878b8dd9 145 }
vpcola 0:f1d3878b8dd9 146
vpcola 0:f1d3878b8dd9 147 return;
vpcola 0:f1d3878b8dd9 148 }
vpcola 0:f1d3878b8dd9 149
vpcola 0:f1d3878b8dd9 150 void BufferedSerial::prime(void)
vpcola 0:f1d3878b8dd9 151 {
vpcola 0:f1d3878b8dd9 152 // if already busy then the irq will pick this up
vpcola 0:f1d3878b8dd9 153 if(serial_writable(&_serial)) {
vpcola 0:f1d3878b8dd9 154 RawSerial::attach(NULL, RawSerial::TxIrq); // make sure not to cause contention in the irq
vpcola 0:f1d3878b8dd9 155 BufferedSerial::txIrq(); // only write to hardware in one place
vpcola 0:f1d3878b8dd9 156 RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
vpcola 0:f1d3878b8dd9 157 }
vpcola 0:f1d3878b8dd9 158
vpcola 0:f1d3878b8dd9 159 return;
vpcola 0:f1d3878b8dd9 160 }
vpcola 0:f1d3878b8dd9 161
vpcola 0:f1d3878b8dd9 162 void BufferedSerial::attach(Callback<void()> func, IrqType type)
vpcola 0:f1d3878b8dd9 163 {
vpcola 0:f1d3878b8dd9 164 _cbs[type] = func;
vpcola 0:f1d3878b8dd9 165 }
vpcola 0:f1d3878b8dd9 166