my customized lib

Committer:
DuyLionTran
Date:
Sun Nov 26 15:08:14 2017 +0000
Revision:
0:8094b249013c
Initial commit

Who changed what in which revision?

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