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

Dependents:   mbed_esp8266_demo

Fork of BufferedSerial by Sam Grove

Committer:
steeven
Date:
Sun May 03 02:48:55 2015 +0000
Revision:
11:9c34df035d99
Parent:
10:9ee15ae3d1a3
Make putc blocking; Add read line function

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 10:9ee15ae3d1a3 26 BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name)
ansond 10:9ee15ae3d1a3 27 : SERIAL_BASE(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size))
sam_grove 0:a977d0a3d81e 28 {
ansond 6:8287e83943f0 29 SERIAL_BASE::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
ansond 10:9ee15ae3d1a3 30 this->_buf_size = buf_size;
ansond 10:9ee15ae3d1a3 31 this->_tx_multiple = tx_multiple;
sam_grove 0:a977d0a3d81e 32 return;
sam_grove 0:a977d0a3d81e 33 }
sam_grove 0:a977d0a3d81e 34
sam_grove 0:a977d0a3d81e 35 BufferedSerial::~BufferedSerial(void)
sam_grove 0:a977d0a3d81e 36 {
ansond 6:8287e83943f0 37 SERIAL_BASE::attach(NULL, SERIAL_BASE::RxIrq);
ansond 6:8287e83943f0 38 SERIAL_BASE::attach(NULL, SERIAL_BASE::TxIrq);
sam_grove 4:2ba4d2e1f05d 39
sam_grove 0:a977d0a3d81e 40 return;
sam_grove 0:a977d0a3d81e 41 }
sam_grove 0:a977d0a3d81e 42
sam_grove 0:a977d0a3d81e 43 int BufferedSerial::readable(void)
sam_grove 0:a977d0a3d81e 44 {
sam_grove 0:a977d0a3d81e 45 return _rxbuf.available(); // note: look if things are in the buffer
sam_grove 0:a977d0a3d81e 46 }
sam_grove 0:a977d0a3d81e 47
sam_grove 0:a977d0a3d81e 48 int BufferedSerial::writeable(void)
sam_grove 0:a977d0a3d81e 49 {
sam_grove 0:a977d0a3d81e 50 return 1; // buffer allows overwriting by design, always true
sam_grove 0:a977d0a3d81e 51 }
sam_grove 0:a977d0a3d81e 52
sam_grove 0:a977d0a3d81e 53 int BufferedSerial::getc(void)
sam_grove 0:a977d0a3d81e 54 {
steeven 11:9c34df035d99 55 while (!readable() && !SERIAL_BASE::readable())
steeven 11:9c34df035d99 56 ;
sam_grove 3:6b76fcf27545 57 return _rxbuf;
sam_grove 0:a977d0a3d81e 58 }
sam_grove 0:a977d0a3d81e 59
steeven 11:9c34df035d99 60
steeven 11:9c34df035d99 61 char* BufferedSerial::readl(char *s, int size)
steeven 11:9c34df035d99 62 {
steeven 11:9c34df035d99 63 if (s == NULL || size <= 0)
steeven 11:9c34df035d99 64 return NULL;
steeven 11:9c34df035d99 65
steeven 11:9c34df035d99 66 char* ptr = s;
steeven 11:9c34df035d99 67
steeven 11:9c34df035d99 68 while (1) {
steeven 11:9c34df035d99 69 if (readable()) {
steeven 11:9c34df035d99 70 if ((size--) > 0) {
steeven 11:9c34df035d99 71 *ptr = _rxbuf;
steeven 11:9c34df035d99 72 if (*ptr == '\n' || *ptr == 0) {
steeven 11:9c34df035d99 73 break;
steeven 11:9c34df035d99 74 }
steeven 11:9c34df035d99 75 ptr++;
steeven 11:9c34df035d99 76 } else
steeven 11:9c34df035d99 77 goto end;
steeven 11:9c34df035d99 78 }
steeven 11:9c34df035d99 79 }
steeven 11:9c34df035d99 80 if (size > 0)
steeven 11:9c34df035d99 81 *ptr = 0;
steeven 11:9c34df035d99 82 end: return s;
steeven 11:9c34df035d99 83 }
steeven 11:9c34df035d99 84
sam_grove 0:a977d0a3d81e 85 int BufferedSerial::putc(int c)
sam_grove 0:a977d0a3d81e 86 {
sam_grove 0:a977d0a3d81e 87 _txbuf = (char)c;
sam_grove 1:57a11fb5d529 88 BufferedSerial::prime();
sam_grove 4:2ba4d2e1f05d 89
sam_grove 0:a977d0a3d81e 90 return c;
sam_grove 0:a977d0a3d81e 91 }
sam_grove 0:a977d0a3d81e 92
sam_grove 0:a977d0a3d81e 93 int BufferedSerial::puts(const char *s)
sam_grove 0:a977d0a3d81e 94 {
ansond 7:6fa214b41d73 95 if (s != NULL) {
ansond 7:6fa214b41d73 96 const char* ptr = s;
ansond 7:6fa214b41d73 97
ansond 7:6fa214b41d73 98 while(*(ptr) != 0) {
ansond 7:6fa214b41d73 99 _txbuf = *(ptr++);
ansond 7:6fa214b41d73 100 }
ansond 7:6fa214b41d73 101 _txbuf = '\n'; // done per puts definition
ansond 7:6fa214b41d73 102 BufferedSerial::prime();
ansond 7:6fa214b41d73 103
ansond 7:6fa214b41d73 104 return (ptr - s) + 1;
sam_grove 0:a977d0a3d81e 105 }
ansond 7:6fa214b41d73 106 return 0;
sam_grove 0:a977d0a3d81e 107 }
sam_grove 0:a977d0a3d81e 108
sam_grove 0:a977d0a3d81e 109 int BufferedSerial::printf(const char* format, ...)
sam_grove 0:a977d0a3d81e 110 {
ansond 10:9ee15ae3d1a3 111 char buffer[this->_buf_size];
ansond 10:9ee15ae3d1a3 112 memset(buffer,0,this->_buf_size);
sam_grove 0:a977d0a3d81e 113 int r = 0;
sam_grove 4:2ba4d2e1f05d 114
sam_grove 0:a977d0a3d81e 115 va_list arg;
sam_grove 0:a977d0a3d81e 116 va_start(arg, format);
ansond 10:9ee15ae3d1a3 117 r = vsprintf(buffer, format, arg);
sam_grove 0:a977d0a3d81e 118 // this may not hit the heap but should alert the user anyways
ansond 10:9ee15ae3d1a3 119 if(r > this->_buf_size) {
ansond 10:9ee15ae3d1a3 120 error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,this->_buf_size,r);
ansond 7:6fa214b41d73 121 va_end(arg);
ansond 7:6fa214b41d73 122 return 0;
sam_grove 0:a977d0a3d81e 123 }
sam_grove 4:2ba4d2e1f05d 124 va_end(arg);
ansond 10:9ee15ae3d1a3 125 r = BufferedSerial::write(buffer, r);
sam_grove 4:2ba4d2e1f05d 126
sam_grove 0:a977d0a3d81e 127 return r;
sam_grove 0:a977d0a3d81e 128 }
sam_grove 0:a977d0a3d81e 129
sam_grove 2:7e8a450a9101 130 ssize_t BufferedSerial::write(const void *s, size_t length)
sam_grove 2:7e8a450a9101 131 {
ansond 7:6fa214b41d73 132 if (s != NULL && length > 0) {
ansond 7:6fa214b41d73 133 const char* ptr = (const char*)s;
ansond 7:6fa214b41d73 134 const char* end = ptr + length;
ansond 7:6fa214b41d73 135
ansond 7:6fa214b41d73 136 while (ptr != end) {
ansond 7:6fa214b41d73 137 _txbuf = *(ptr++);
ansond 7:6fa214b41d73 138 }
ansond 7:6fa214b41d73 139 BufferedSerial::prime();
ansond 7:6fa214b41d73 140
ansond 7:6fa214b41d73 141 return ptr - (const char*)s;
sam_grove 2:7e8a450a9101 142 }
ansond 7:6fa214b41d73 143 return 0;
sam_grove 2:7e8a450a9101 144 }
sam_grove 2:7e8a450a9101 145
sam_grove 2:7e8a450a9101 146
sam_grove 0:a977d0a3d81e 147 void BufferedSerial::rxIrq(void)
sam_grove 0:a977d0a3d81e 148 {
sam_grove 3:6b76fcf27545 149 // read from the peripheral and make sure something is available
sam_grove 4:2ba4d2e1f05d 150 if(serial_readable(&_serial)) {
sam_grove 0:a977d0a3d81e 151 _rxbuf = serial_getc(&_serial); // if so load them into a buffer
sam_grove 0:a977d0a3d81e 152 }
sam_grove 4:2ba4d2e1f05d 153
sam_grove 0:a977d0a3d81e 154 return;
sam_grove 0:a977d0a3d81e 155 }
sam_grove 0:a977d0a3d81e 156
sam_grove 0:a977d0a3d81e 157 void BufferedSerial::txIrq(void)
sam_grove 0:a977d0a3d81e 158 {
sam_grove 3:6b76fcf27545 159 // see if there is room in the hardware fifo and if something is in the software fifo
sam_grove 4:2ba4d2e1f05d 160 while(serial_writable(&_serial)) {
sam_grove 4:2ba4d2e1f05d 161 if(_txbuf.available()) {
sam_grove 4:2ba4d2e1f05d 162 serial_putc(&_serial, (int)_txbuf.get());
sam_grove 4:2ba4d2e1f05d 163 } else {
sam_grove 4:2ba4d2e1f05d 164 // disable the TX interrupt when there is nothing left to send
ansond 6:8287e83943f0 165 SERIAL_BASE::attach(NULL, SERIAL_BASE::TxIrq);
sam_grove 4:2ba4d2e1f05d 166 break;
sam_grove 4:2ba4d2e1f05d 167 }
sam_grove 0:a977d0a3d81e 168 }
sam_grove 4:2ba4d2e1f05d 169
sam_grove 0:a977d0a3d81e 170 return;
sam_grove 0:a977d0a3d81e 171 }
sam_grove 0:a977d0a3d81e 172
sam_grove 1:57a11fb5d529 173 void BufferedSerial::prime(void)
sam_grove 1:57a11fb5d529 174 {
sam_grove 2:7e8a450a9101 175 // if already busy then the irq will pick this up
sam_grove 4:2ba4d2e1f05d 176 if(serial_writable(&_serial)) {
ansond 6:8287e83943f0 177 SERIAL_BASE::attach(NULL, SERIAL_BASE::TxIrq); // make sure not to cause contention in the irq
sam_grove 3:6b76fcf27545 178 BufferedSerial::txIrq(); // only write to hardware in one place
ansond 6:8287e83943f0 179 SERIAL_BASE::attach(this, &BufferedSerial::txIrq, SERIAL_BASE::TxIrq);
sam_grove 2:7e8a450a9101 180 }
sam_grove 4:2ba4d2e1f05d 181
sam_grove 1:57a11fb5d529 182 return;
sam_grove 1:57a11fb5d529 183 }
sam_grove 0:a977d0a3d81e 184
sam_grove 0:a977d0a3d81e 185