Changes added to reflect changes in MyBuffer which was modified to use static memory allocation.

Dependencies:   BufferStatic

Fork of BufferedSerialStatic by Goran Kecman

Committer:
goranirnas
Date:
Mon May 28 14:54:22 2018 +0000
Revision:
15:0c7b54af5c73
Parent:
14:60e0d42781f9
Removed mbed lib from this library.

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