Version of easy-connect with the u-blox cellular platforms C027 and C030 added.

Dependents:   HelloMQTT

Committer:
group-ublox
Date:
Thu Aug 10 14:33:05 2017 +0000
Revision:
0:19aa55d66228
Initial commit

Who changed what in which revision?

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