A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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