a

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
hydroguy45
Date:
Fri Nov 01 18:52:45 2019 +0000
Revision:
12:ea030e3181d3
Parent:
7:ca226305d28b
Cleaned up the header

Who changed what in which revision?

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