![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code for Technion Formula car sensors reader
Dependencies: mbed Buffer FATFileSystem
Fork of SX1272PingPong by
This is code is part of a Technion course project in advanced IoT, implementing a device to read and transmit sensors data from a Formula racing car built by students at Technion - Israel Institute of Technology.
How to install
- Create an account on Mbed: https://os.mbed.com/account/signup/
- Import project into Compiler
- In the Program Workspace select "Formula_Nucleo_Reader"
- Select a Platform like so:
- Click button at top-left
- Add Board
- Search "NUCLEO F103RB" and then "Add to your Mbed Compiler"
- Finally click "Compile", if the build was successful, the binary would download automatically
- To install it on device simply plug it in to a PC, open device drive and drag then drop binary file in it
BufferedSerial/BufferedSerial.cpp@15:2e0d977dbb31, 2018-05-17 (annotated)
- Committer:
- wardm
- Date:
- Thu May 17 20:37:41 2018 +0000
- Revision:
- 15:2e0d977dbb31
V1.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wardm | 15:2e0d977dbb31 | 1 | /** |
wardm | 15:2e0d977dbb31 | 2 | * @file BufferedSerial.cpp |
wardm | 15:2e0d977dbb31 | 3 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
wardm | 15:2e0d977dbb31 | 4 | * @author sam grove |
wardm | 15:2e0d977dbb31 | 5 | * @version 1.0 |
wardm | 15:2e0d977dbb31 | 6 | * @see |
wardm | 15:2e0d977dbb31 | 7 | * |
wardm | 15:2e0d977dbb31 | 8 | * Copyright (c) 2013 |
wardm | 15:2e0d977dbb31 | 9 | * |
wardm | 15:2e0d977dbb31 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
wardm | 15:2e0d977dbb31 | 11 | * you may not use this file except in compliance with the License. |
wardm | 15:2e0d977dbb31 | 12 | * You may obtain a copy of the License at |
wardm | 15:2e0d977dbb31 | 13 | * |
wardm | 15:2e0d977dbb31 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
wardm | 15:2e0d977dbb31 | 15 | * |
wardm | 15:2e0d977dbb31 | 16 | * Unless required by applicable law or agreed to in writing, software |
wardm | 15:2e0d977dbb31 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
wardm | 15:2e0d977dbb31 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
wardm | 15:2e0d977dbb31 | 19 | * See the License for the specific language governing permissions and |
wardm | 15:2e0d977dbb31 | 20 | * limitations under the License. |
wardm | 15:2e0d977dbb31 | 21 | */ |
wardm | 15:2e0d977dbb31 | 22 | |
wardm | 15:2e0d977dbb31 | 23 | #include "BufferedSerial.h" |
wardm | 15:2e0d977dbb31 | 24 | #include <stdarg.h> |
wardm | 15:2e0d977dbb31 | 25 | |
wardm | 15:2e0d977dbb31 | 26 | BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name) |
wardm | 15:2e0d977dbb31 | 27 | : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size)) |
wardm | 15:2e0d977dbb31 | 28 | { |
wardm | 15:2e0d977dbb31 | 29 | RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq); |
wardm | 15:2e0d977dbb31 | 30 | this->_buf_size = buf_size; |
wardm | 15:2e0d977dbb31 | 31 | this->_tx_multiple = tx_multiple; |
wardm | 15:2e0d977dbb31 | 32 | return; |
wardm | 15:2e0d977dbb31 | 33 | } |
wardm | 15:2e0d977dbb31 | 34 | |
wardm | 15:2e0d977dbb31 | 35 | BufferedSerial::~BufferedSerial(void) |
wardm | 15:2e0d977dbb31 | 36 | { |
wardm | 15:2e0d977dbb31 | 37 | RawSerial::attach(NULL, RawSerial::RxIrq); |
wardm | 15:2e0d977dbb31 | 38 | RawSerial::attach(NULL, RawSerial::TxIrq); |
wardm | 15:2e0d977dbb31 | 39 | |
wardm | 15:2e0d977dbb31 | 40 | return; |
wardm | 15:2e0d977dbb31 | 41 | } |
wardm | 15:2e0d977dbb31 | 42 | |
wardm | 15:2e0d977dbb31 | 43 | int BufferedSerial::readable(void) |
wardm | 15:2e0d977dbb31 | 44 | { |
wardm | 15:2e0d977dbb31 | 45 | return _rxbuf.available(); // note: look if things are in the buffer |
wardm | 15:2e0d977dbb31 | 46 | } |
wardm | 15:2e0d977dbb31 | 47 | |
wardm | 15:2e0d977dbb31 | 48 | int BufferedSerial::writeable(void) |
wardm | 15:2e0d977dbb31 | 49 | { |
wardm | 15:2e0d977dbb31 | 50 | return 1; // buffer allows overwriting by design, always true |
wardm | 15:2e0d977dbb31 | 51 | } |
wardm | 15:2e0d977dbb31 | 52 | |
wardm | 15:2e0d977dbb31 | 53 | int BufferedSerial::getc(void) |
wardm | 15:2e0d977dbb31 | 54 | { |
wardm | 15:2e0d977dbb31 | 55 | return _rxbuf; |
wardm | 15:2e0d977dbb31 | 56 | } |
wardm | 15:2e0d977dbb31 | 57 | |
wardm | 15:2e0d977dbb31 | 58 | int BufferedSerial::putc(int c) |
wardm | 15:2e0d977dbb31 | 59 | { |
wardm | 15:2e0d977dbb31 | 60 | _txbuf = (char)c; |
wardm | 15:2e0d977dbb31 | 61 | BufferedSerial::prime(); |
wardm | 15:2e0d977dbb31 | 62 | |
wardm | 15:2e0d977dbb31 | 63 | return c; |
wardm | 15:2e0d977dbb31 | 64 | } |
wardm | 15:2e0d977dbb31 | 65 | |
wardm | 15:2e0d977dbb31 | 66 | int BufferedSerial::puts(const char *s) |
wardm | 15:2e0d977dbb31 | 67 | { |
wardm | 15:2e0d977dbb31 | 68 | if (s != NULL) { |
wardm | 15:2e0d977dbb31 | 69 | const char* ptr = s; |
wardm | 15:2e0d977dbb31 | 70 | |
wardm | 15:2e0d977dbb31 | 71 | while(*(ptr) != 0) { |
wardm | 15:2e0d977dbb31 | 72 | _txbuf = *(ptr++); |
wardm | 15:2e0d977dbb31 | 73 | } |
wardm | 15:2e0d977dbb31 | 74 | _txbuf = '\n'; // done per puts definition |
wardm | 15:2e0d977dbb31 | 75 | BufferedSerial::prime(); |
wardm | 15:2e0d977dbb31 | 76 | |
wardm | 15:2e0d977dbb31 | 77 | return (ptr - s) + 1; |
wardm | 15:2e0d977dbb31 | 78 | } |
wardm | 15:2e0d977dbb31 | 79 | return 0; |
wardm | 15:2e0d977dbb31 | 80 | } |
wardm | 15:2e0d977dbb31 | 81 | |
wardm | 15:2e0d977dbb31 | 82 | int BufferedSerial::printf(const char* format, ...) |
wardm | 15:2e0d977dbb31 | 83 | { |
wardm | 15:2e0d977dbb31 | 84 | char buffer[this->_buf_size]; |
wardm | 15:2e0d977dbb31 | 85 | memset(buffer,0,this->_buf_size); |
wardm | 15:2e0d977dbb31 | 86 | int r = 0; |
wardm | 15:2e0d977dbb31 | 87 | |
wardm | 15:2e0d977dbb31 | 88 | va_list arg; |
wardm | 15:2e0d977dbb31 | 89 | va_start(arg, format); |
wardm | 15:2e0d977dbb31 | 90 | r = vsprintf(buffer, format, arg); |
wardm | 15:2e0d977dbb31 | 91 | // this may not hit the heap but should alert the user anyways |
wardm | 15:2e0d977dbb31 | 92 | if(r > this->_buf_size) { |
wardm | 15:2e0d977dbb31 | 93 | error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,this->_buf_size,r); |
wardm | 15:2e0d977dbb31 | 94 | va_end(arg); |
wardm | 15:2e0d977dbb31 | 95 | return 0; |
wardm | 15:2e0d977dbb31 | 96 | } |
wardm | 15:2e0d977dbb31 | 97 | va_end(arg); |
wardm | 15:2e0d977dbb31 | 98 | r = BufferedSerial::write(buffer, r); |
wardm | 15:2e0d977dbb31 | 99 | |
wardm | 15:2e0d977dbb31 | 100 | return r; |
wardm | 15:2e0d977dbb31 | 101 | } |
wardm | 15:2e0d977dbb31 | 102 | |
wardm | 15:2e0d977dbb31 | 103 | ssize_t BufferedSerial::write(const void *s, size_t length) |
wardm | 15:2e0d977dbb31 | 104 | { |
wardm | 15:2e0d977dbb31 | 105 | if (s != NULL && length > 0) { |
wardm | 15:2e0d977dbb31 | 106 | const char* ptr = (const char*)s; |
wardm | 15:2e0d977dbb31 | 107 | const char* end = ptr + length; |
wardm | 15:2e0d977dbb31 | 108 | |
wardm | 15:2e0d977dbb31 | 109 | while (ptr != end) { |
wardm | 15:2e0d977dbb31 | 110 | _txbuf = *(ptr++); |
wardm | 15:2e0d977dbb31 | 111 | } |
wardm | 15:2e0d977dbb31 | 112 | BufferedSerial::prime(); |
wardm | 15:2e0d977dbb31 | 113 | |
wardm | 15:2e0d977dbb31 | 114 | return ptr - (const char*)s; |
wardm | 15:2e0d977dbb31 | 115 | } |
wardm | 15:2e0d977dbb31 | 116 | return 0; |
wardm | 15:2e0d977dbb31 | 117 | } |
wardm | 15:2e0d977dbb31 | 118 | |
wardm | 15:2e0d977dbb31 | 119 | |
wardm | 15:2e0d977dbb31 | 120 | void BufferedSerial::rxIrq(void) |
wardm | 15:2e0d977dbb31 | 121 | { |
wardm | 15:2e0d977dbb31 | 122 | // read from the peripheral and make sure something is available |
wardm | 15:2e0d977dbb31 | 123 | if(serial_readable(&_serial)) { |
wardm | 15:2e0d977dbb31 | 124 | _rxbuf = serial_getc(&_serial); // if so load them into a buffer |
wardm | 15:2e0d977dbb31 | 125 | } |
wardm | 15:2e0d977dbb31 | 126 | |
wardm | 15:2e0d977dbb31 | 127 | return; |
wardm | 15:2e0d977dbb31 | 128 | } |
wardm | 15:2e0d977dbb31 | 129 | |
wardm | 15:2e0d977dbb31 | 130 | void BufferedSerial::txIrq(void) |
wardm | 15:2e0d977dbb31 | 131 | { |
wardm | 15:2e0d977dbb31 | 132 | // see if there is room in the hardware fifo and if something is in the software fifo |
wardm | 15:2e0d977dbb31 | 133 | while(serial_writable(&_serial)) { |
wardm | 15:2e0d977dbb31 | 134 | if(_txbuf.available()) { |
wardm | 15:2e0d977dbb31 | 135 | serial_putc(&_serial, (int)_txbuf.get()); |
wardm | 15:2e0d977dbb31 | 136 | } else { |
wardm | 15:2e0d977dbb31 | 137 | // disable the TX interrupt when there is nothing left to send |
wardm | 15:2e0d977dbb31 | 138 | RawSerial::attach(NULL, RawSerial::TxIrq); |
wardm | 15:2e0d977dbb31 | 139 | break; |
wardm | 15:2e0d977dbb31 | 140 | } |
wardm | 15:2e0d977dbb31 | 141 | } |
wardm | 15:2e0d977dbb31 | 142 | |
wardm | 15:2e0d977dbb31 | 143 | return; |
wardm | 15:2e0d977dbb31 | 144 | } |
wardm | 15:2e0d977dbb31 | 145 | |
wardm | 15:2e0d977dbb31 | 146 | void BufferedSerial::prime(void) |
wardm | 15:2e0d977dbb31 | 147 | { |
wardm | 15:2e0d977dbb31 | 148 | // if already busy then the irq will pick this up |
wardm | 15:2e0d977dbb31 | 149 | if(serial_writable(&_serial)) { |
wardm | 15:2e0d977dbb31 | 150 | RawSerial::attach(NULL, RawSerial::TxIrq); // make sure not to cause contention in the irq |
wardm | 15:2e0d977dbb31 | 151 | BufferedSerial::txIrq(); // only write to hardware in one place |
wardm | 15:2e0d977dbb31 | 152 | RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq); |
wardm | 15:2e0d977dbb31 | 153 | } |
wardm | 15:2e0d977dbb31 | 154 | |
wardm | 15:2e0d977dbb31 | 155 | return; |
wardm | 15:2e0d977dbb31 | 156 | } |
wardm | 15:2e0d977dbb31 | 157 | |
wardm | 15:2e0d977dbb31 | 158 |