Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

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