Inherit from Serial and use software buffers for TX and RX. This allows the UART peripherals to operate in a IRQ driven mode. Overrides most (but not all) stdio functions as Serial did

Dependencies:   Buffer

Fork of BufferedSerial by Sam Grove

Committer:
igbt6
Date:
Sun Nov 01 21:28:36 2015 +0000
Revision:
13:00182678a254
Parent:
12:779304f9c5d2
buffered Serial library imported

Who changed what in which revision?

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