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