Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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