This is code is part of a Technion course project in advanced IoT, implementing a device to receive and present sensors data from a Formula racing car built by students at Technion - Israel Institute of Technology.

Dependencies:   mbed Buffer

Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by ST

This is code is part of a Technion course project in advanced IoT, implementing a device to receive sensors data from another L072CZ-LRWAN1 installed on a Formula racing car (built by students at Technion - Israel Institute of Technology), and sends it to a GUI presenting the data (GUI project: github.com/ward-mattar/TechnionFormulaGUI).

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_Receiver"
  • Select a Platform like so:
  1. Click button at top-left
  2. Add Board
  3. 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
Committer:
wardm
Date:
Sat May 19 15:42:38 2018 +0000
Revision:
12:046346a16ff4
V1.0.0

Who changed what in which revision?

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