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