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 * The file is Licensed under the Apache License, Version 2.0
wardm 12:046346a16ff4 3 * (c) 2017 Helmut Tschemernjak
wardm 12:046346a16ff4 4 * 30826 Garbsen (Hannover) Germany
wardm 12:046346a16ff4 5 */
wardm 12:046346a16ff4 6
wardm 12:046346a16ff4 7
wardm 12:046346a16ff4 8
wardm 12:046346a16ff4 9 #ifdef ARDUINO
wardm 12:046346a16ff4 10 #ifndef __ARDUINO_MBED_H__
wardm 12:046346a16ff4 11 #define __ARDUINO_MBED_H__
wardm 12:046346a16ff4 12
wardm 12:046346a16ff4 13 #include <arduino.h>
wardm 12:046346a16ff4 14 #include "Callback-A.h"
wardm 12:046346a16ff4 15 #include <SPI.h>
wardm 12:046346a16ff4 16 #undef min
wardm 12:046346a16ff4 17 #undef max
wardm 12:046346a16ff4 18 #undef map
wardm 12:046346a16ff4 19
wardm 12:046346a16ff4 20 typedef int PinName;
wardm 12:046346a16ff4 21 #define NC -1
wardm 12:046346a16ff4 22 /* we need to redefine out dprintf because stdio.h uses the same name */
wardm 12:046346a16ff4 23 #define dprint dxprintf
wardm 12:046346a16ff4 24 #if ARDUINO_SAMD_VARIANT_COMPLIANCE >= 10606
wardm 12:046346a16ff4 25 #define MYdigitalPinToInterrupt(x) digitalPinToInterrupt(x)
wardm 12:046346a16ff4 26 #else
wardm 12:046346a16ff4 27 #define MYdigitalPinToInterrupt(x) (x)
wardm 12:046346a16ff4 28 #endif
wardm 12:046346a16ff4 29
wardm 12:046346a16ff4 30 void InitSerial(Stream *serial, int timeout_ms);
wardm 12:046346a16ff4 31 extern Stream *ser;
wardm 12:046346a16ff4 32 extern bool SerialUSB_active;
wardm 12:046346a16ff4 33
wardm 12:046346a16ff4 34 /*
wardm 12:046346a16ff4 35 * Arduino_d21.cpp
wardm 12:046346a16ff4 36 */
wardm 12:046346a16ff4 37 extern void startTimer(Tcc *t, uint64_t delay_ns);
wardm 12:046346a16ff4 38 extern void stopTimer(Tcc *t);
wardm 12:046346a16ff4 39 extern uint64_t ns_getTicker(void);
wardm 12:046346a16ff4 40 extern Tcc *getTimeout_tcc(void);
wardm 12:046346a16ff4 41 extern int CPUID(uint8_t *buf, int maxSize, uint32_t xorval);
wardm 12:046346a16ff4 42
wardm 12:046346a16ff4 43
wardm 12:046346a16ff4 44 extern void sleep(void);
wardm 12:046346a16ff4 45 extern void deepsleep(void);
wardm 12:046346a16ff4 46
wardm 12:046346a16ff4 47 #define MAX_TIMEOUTS 10
wardm 12:046346a16ff4 48 class Timeout;
wardm 12:046346a16ff4 49 struct TimeoutVector {
wardm 12:046346a16ff4 50 Timeout *timer;
wardm 12:046346a16ff4 51 };
wardm 12:046346a16ff4 52 extern TimeoutVector TimeOuts[];
wardm 12:046346a16ff4 53
wardm 12:046346a16ff4 54
wardm 12:046346a16ff4 55 /*
wardm 12:046346a16ff4 56 * Arduino-mbed.cpp
wardm 12:046346a16ff4 57 */
wardm 12:046346a16ff4 58 extern uint32_t s_getTicker(void);
wardm 12:046346a16ff4 59 extern uint32_t ms_getTicker(void);
wardm 12:046346a16ff4 60 extern uint32_t us_getTicker(void);
wardm 12:046346a16ff4 61 extern void wait_ms(uint32_t ms);
wardm 12:046346a16ff4 62
wardm 12:046346a16ff4 63
wardm 12:046346a16ff4 64 enum PinMode {
wardm 12:046346a16ff4 65 PullUp = 1,
wardm 12:046346a16ff4 66 PullDown = 2,
wardm 12:046346a16ff4 67 };
wardm 12:046346a16ff4 68
wardm 12:046346a16ff4 69 class DigitalInOut {
wardm 12:046346a16ff4 70 public:
wardm 12:046346a16ff4 71 DigitalInOut(PinName pin) {
wardm 12:046346a16ff4 72 _gpioPin = pin;
wardm 12:046346a16ff4 73 }
wardm 12:046346a16ff4 74 void write(int value) {
wardm 12:046346a16ff4 75 digitalWrite(_gpioPin, value == 0 ? LOW : HIGH);
wardm 12:046346a16ff4 76 };
wardm 12:046346a16ff4 77
wardm 12:046346a16ff4 78 void output() {
wardm 12:046346a16ff4 79 pinMode(_gpioPin, OUTPUT);
wardm 12:046346a16ff4 80 };
wardm 12:046346a16ff4 81
wardm 12:046346a16ff4 82 void input() {
wardm 12:046346a16ff4 83 pinMode(_gpioPin, INPUT);
wardm 12:046346a16ff4 84 };
wardm 12:046346a16ff4 85
wardm 12:046346a16ff4 86 void mode(PinMode pull) {
wardm 12:046346a16ff4 87 switch(pull) {
wardm 12:046346a16ff4 88 case PullUp:
wardm 12:046346a16ff4 89 pinMode(_gpioPin, INPUT_PULLUP);
wardm 12:046346a16ff4 90 break;
wardm 12:046346a16ff4 91 case PullDown:
wardm 12:046346a16ff4 92 pinMode(_gpioPin, INPUT_PULLDOWN);
wardm 12:046346a16ff4 93 break;
wardm 12:046346a16ff4 94 }
wardm 12:046346a16ff4 95 }
wardm 12:046346a16ff4 96
wardm 12:046346a16ff4 97 int read() {
wardm 12:046346a16ff4 98 if (digitalRead(_gpioPin) == HIGH)
wardm 12:046346a16ff4 99 return 1;
wardm 12:046346a16ff4 100 else
wardm 12:046346a16ff4 101 return 0;
wardm 12:046346a16ff4 102 };
wardm 12:046346a16ff4 103 operator int() {
wardm 12:046346a16ff4 104 return read();
wardm 12:046346a16ff4 105 };
wardm 12:046346a16ff4 106
wardm 12:046346a16ff4 107 DigitalInOut& operator= (int value) {
wardm 12:046346a16ff4 108 // Underlying write is thread safe
wardm 12:046346a16ff4 109 write(value);
wardm 12:046346a16ff4 110 return *this;
wardm 12:046346a16ff4 111 }
wardm 12:046346a16ff4 112
wardm 12:046346a16ff4 113 DigitalInOut& operator= (DigitalInOut& rhs) {
wardm 12:046346a16ff4 114 write(rhs.read());
wardm 12:046346a16ff4 115 return *this;
wardm 12:046346a16ff4 116 }
wardm 12:046346a16ff4 117
wardm 12:046346a16ff4 118 private:
wardm 12:046346a16ff4 119 int _gpioPin;
wardm 12:046346a16ff4 120 };
wardm 12:046346a16ff4 121
wardm 12:046346a16ff4 122 class DigitalOut : public DigitalInOut {
wardm 12:046346a16ff4 123 public:
wardm 12:046346a16ff4 124
wardm 12:046346a16ff4 125 DigitalOut(PinName pin) : DigitalInOut(pin) {
wardm 12:046346a16ff4 126 output();
wardm 12:046346a16ff4 127 }
wardm 12:046346a16ff4 128
wardm 12:046346a16ff4 129 DigitalOut& operator= (int value) {
wardm 12:046346a16ff4 130 write(value);
wardm 12:046346a16ff4 131 return *this;
wardm 12:046346a16ff4 132 }
wardm 12:046346a16ff4 133
wardm 12:046346a16ff4 134 };
wardm 12:046346a16ff4 135
wardm 12:046346a16ff4 136 class DigitalIn : public DigitalInOut {
wardm 12:046346a16ff4 137 public:
wardm 12:046346a16ff4 138
wardm 12:046346a16ff4 139 DigitalIn(PinName pin) : DigitalInOut(pin) {
wardm 12:046346a16ff4 140 input();
wardm 12:046346a16ff4 141 }
wardm 12:046346a16ff4 142 };
wardm 12:046346a16ff4 143
wardm 12:046346a16ff4 144 class XSPI {
wardm 12:046346a16ff4 145 public:
wardm 12:046346a16ff4 146 XSPI(PinName mosi, PinName miso, PinName sclk) {
wardm 12:046346a16ff4 147 _mosi = mosi;
wardm 12:046346a16ff4 148 _miso = miso;
wardm 12:046346a16ff4 149 _sclk = sclk;
wardm 12:046346a16ff4 150 if (mosi == PIN_SPI_MOSI && miso == PIN_SPI_MISO && sclk == PIN_SPI_SCK)
wardm 12:046346a16ff4 151 _spi = &SPI;
wardm 12:046346a16ff4 152 #if SPI_INTERFACES_COUNT > 1
wardm 12:046346a16ff4 153 else if (mosi == PIN_SPI1_MOSI && miso == PIN_SPI1_MISO && sclk == PIN_SPI1_SCK)
wardm 12:046346a16ff4 154 _spi = &SPI1;
wardm 12:046346a16ff4 155 #endif
wardm 12:046346a16ff4 156 #if SPI_INTERFACES_COUNT > 2
wardm 12:046346a16ff4 157 else if (mosi == PIN_SPI2_MOSI && miso == PIN_SPI2_MISO && sclk == PIN_SPI2_SCK)
wardm 12:046346a16ff4 158 _spi = &SPI2;
wardm 12:046346a16ff4 159 #endif
wardm 12:046346a16ff4 160 else {
wardm 12:046346a16ff4 161 _spi = NULL;
wardm 12:046346a16ff4 162 return;
wardm 12:046346a16ff4 163 }
wardm 12:046346a16ff4 164 _hz = 1000000;
wardm 12:046346a16ff4 165 _mode = SPI_MODE0;
wardm 12:046346a16ff4 166 _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode));
wardm 12:046346a16ff4 167 }
wardm 12:046346a16ff4 168 ~XSPI() {
wardm 12:046346a16ff4 169 _spi->endTransaction();
wardm 12:046346a16ff4 170 };
wardm 12:046346a16ff4 171
wardm 12:046346a16ff4 172 void format(int bits, int mode = 0) {
wardm 12:046346a16ff4 173 if (mode == 0)
wardm 12:046346a16ff4 174 _mode = SPI_MODE0;
wardm 12:046346a16ff4 175 else if (mode == 1)
wardm 12:046346a16ff4 176 _mode = SPI_MODE1;
wardm 12:046346a16ff4 177 else if (mode == 2)
wardm 12:046346a16ff4 178 _mode = SPI_MODE2;
wardm 12:046346a16ff4 179 else if (mode == 3)
wardm 12:046346a16ff4 180 _mode = SPI_MODE3;
wardm 12:046346a16ff4 181 else
wardm 12:046346a16ff4 182 _mode = SPI_MODE0;
wardm 12:046346a16ff4 183 _bits = bits;
wardm 12:046346a16ff4 184 _spi->endTransaction();
wardm 12:046346a16ff4 185 _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode));
wardm 12:046346a16ff4 186 }
wardm 12:046346a16ff4 187 void frequency(int hz) {
wardm 12:046346a16ff4 188 _hz = hz;
wardm 12:046346a16ff4 189 _spi->endTransaction();
wardm 12:046346a16ff4 190 _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode));
wardm 12:046346a16ff4 191 }
wardm 12:046346a16ff4 192
wardm 12:046346a16ff4 193 int write(int value) {
wardm 12:046346a16ff4 194 int ret = _spi->transfer(value);
wardm 12:046346a16ff4 195 return ret;
wardm 12:046346a16ff4 196 }
wardm 12:046346a16ff4 197
wardm 12:046346a16ff4 198 private:
wardm 12:046346a16ff4 199 SPIClass *_spi;
wardm 12:046346a16ff4 200 int _hz;
wardm 12:046346a16ff4 201 int _mode;
wardm 12:046346a16ff4 202 int _bits;
wardm 12:046346a16ff4 203 int _mosi, _miso, _sclk;
wardm 12:046346a16ff4 204 };
wardm 12:046346a16ff4 205
wardm 12:046346a16ff4 206 class InterruptIn {
wardm 12:046346a16ff4 207 public:
wardm 12:046346a16ff4 208 static void donothing(void) {
wardm 12:046346a16ff4 209 }
wardm 12:046346a16ff4 210
wardm 12:046346a16ff4 211 InterruptIn(PinName pin) : _func() {
wardm 12:046346a16ff4 212 _gpioPin = pin;
wardm 12:046346a16ff4 213 _func = InterruptIn::donothing;
wardm 12:046346a16ff4 214 pinMode(_gpioPin, INPUT);
wardm 12:046346a16ff4 215 }
wardm 12:046346a16ff4 216
wardm 12:046346a16ff4 217 ~InterruptIn() {
wardm 12:046346a16ff4 218 detachInterrupt(MYdigitalPinToInterrupt(_gpioPin));
wardm 12:046346a16ff4 219 };
wardm 12:046346a16ff4 220
wardm 12:046346a16ff4 221 static void _irq_handler(InterruptIn *id) {
wardm 12:046346a16ff4 222 if (id)
wardm 12:046346a16ff4 223 id->_func();
wardm 12:046346a16ff4 224 }
wardm 12:046346a16ff4 225
wardm 12:046346a16ff4 226 void rise(Callback<void()> func);
wardm 12:046346a16ff4 227
wardm 12:046346a16ff4 228 void fall(Callback<void()> func);
wardm 12:046346a16ff4 229
wardm 12:046346a16ff4 230 void mode(PinMode pull) {
wardm 12:046346a16ff4 231 switch(pull) {
wardm 12:046346a16ff4 232 case PullUp:
wardm 12:046346a16ff4 233 pinMode(_gpioPin, INPUT_PULLUP);
wardm 12:046346a16ff4 234 break;
wardm 12:046346a16ff4 235 case PullDown:
wardm 12:046346a16ff4 236 pinMode(_gpioPin, INPUT_PULLDOWN);
wardm 12:046346a16ff4 237 break;
wardm 12:046346a16ff4 238 }
wardm 12:046346a16ff4 239 }
wardm 12:046346a16ff4 240 private:
wardm 12:046346a16ff4 241 int _gpioPin;
wardm 12:046346a16ff4 242 Callback<void()> _func;
wardm 12:046346a16ff4 243 };
wardm 12:046346a16ff4 244
wardm 12:046346a16ff4 245
wardm 12:046346a16ff4 246
wardm 12:046346a16ff4 247 class Timer {
wardm 12:046346a16ff4 248 public:
wardm 12:046346a16ff4 249 void start(void) {
wardm 12:046346a16ff4 250 _time = ns_getTicker();
wardm 12:046346a16ff4 251 }
wardm 12:046346a16ff4 252 uint32_t read_sec(void) {
wardm 12:046346a16ff4 253 int64_t n = ns_getTicker() - (uint64_t)_time;
wardm 12:046346a16ff4 254 n /= (uint64_t)1000000000;
wardm 12:046346a16ff4 255 return n;
wardm 12:046346a16ff4 256 }
wardm 12:046346a16ff4 257 uint32_t read_ms(void) {
wardm 12:046346a16ff4 258 int64_t n = ns_getTicker() - (uint64_t)_time;
wardm 12:046346a16ff4 259 n /= (uint64_t)1000000;
wardm 12:046346a16ff4 260 return n;
wardm 12:046346a16ff4 261 }
wardm 12:046346a16ff4 262 uint32_t read_us(void) {
wardm 12:046346a16ff4 263 int64_t n = ns_getTicker() - (uint64_t)_time;
wardm 12:046346a16ff4 264 n /= (uint64_t)1000;
wardm 12:046346a16ff4 265 return n;
wardm 12:046346a16ff4 266 }
wardm 12:046346a16ff4 267 private:
wardm 12:046346a16ff4 268 uint64_t _time;
wardm 12:046346a16ff4 269 };
wardm 12:046346a16ff4 270
wardm 12:046346a16ff4 271
wardm 12:046346a16ff4 272 class Timeout {
wardm 12:046346a16ff4 273 public:
wardm 12:046346a16ff4 274 Timeout() : _func() {
wardm 12:046346a16ff4 275 }
wardm 12:046346a16ff4 276 ~Timeout() {
wardm 12:046346a16ff4 277 detach();
wardm 12:046346a16ff4 278 }
wardm 12:046346a16ff4 279
wardm 12:046346a16ff4 280 void attach_sec(Callback<void()> func, uint32_t secs) {
wardm 12:046346a16ff4 281 if (secs == 0)
wardm 12:046346a16ff4 282 return detach();
wardm 12:046346a16ff4 283 _func = func;
wardm 12:046346a16ff4 284 _timeout = ns_getTicker() + (uint64_t)secs * (uint64_t)1000000000;
wardm 12:046346a16ff4 285 insert();
wardm 12:046346a16ff4 286 restart();
wardm 12:046346a16ff4 287 }
wardm 12:046346a16ff4 288
wardm 12:046346a16ff4 289 void attach(Callback<void()> func, uint32_t msecs) {
wardm 12:046346a16ff4 290 if (msecs == 0)
wardm 12:046346a16ff4 291 return detach();
wardm 12:046346a16ff4 292 _func = func;
wardm 12:046346a16ff4 293 _timeout = ns_getTicker() + (uint64_t)msecs * (uint64_t)1000000;
wardm 12:046346a16ff4 294 insert();
wardm 12:046346a16ff4 295 restart();
wardm 12:046346a16ff4 296 }
wardm 12:046346a16ff4 297
wardm 12:046346a16ff4 298 void attach_us(Callback<void()> func, long usecs) {
wardm 12:046346a16ff4 299 if (usecs == 0)
wardm 12:046346a16ff4 300 return detach();
wardm 12:046346a16ff4 301 _func = func;
wardm 12:046346a16ff4 302 _timeout = ns_getTicker() + (uint64_t)usecs * (uint64_t)1000;
wardm 12:046346a16ff4 303 insert();
wardm 12:046346a16ff4 304 restart();
wardm 12:046346a16ff4 305 }
wardm 12:046346a16ff4 306
wardm 12:046346a16ff4 307 void detach(void) {
wardm 12:046346a16ff4 308 _func = NULL;
wardm 12:046346a16ff4 309 remove();
wardm 12:046346a16ff4 310 restart();
wardm 12:046346a16ff4 311 }
wardm 12:046346a16ff4 312
wardm 12:046346a16ff4 313 static void _irq_handler(Timeout *tp) {
wardm 12:046346a16ff4 314 if (tp) {
wardm 12:046346a16ff4 315 tp->_func();
wardm 12:046346a16ff4 316 }
wardm 12:046346a16ff4 317 }
wardm 12:046346a16ff4 318
wardm 12:046346a16ff4 319 static void restart(void);
wardm 12:046346a16ff4 320 uint64_t _timeout; // in ns this lasts for 539 years.
wardm 12:046346a16ff4 321 protected:
wardm 12:046346a16ff4 322 void insert(void);
wardm 12:046346a16ff4 323 void remove(void);
wardm 12:046346a16ff4 324 private:
wardm 12:046346a16ff4 325 Callback<void()> _func;
wardm 12:046346a16ff4 326 };
wardm 12:046346a16ff4 327
wardm 12:046346a16ff4 328 #endif // __ARDUINO_MBED_H__
wardm 12:046346a16ff4 329
wardm 12:046346a16ff4 330 #endif // ARDUINO