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