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.

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 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:
  1. Click button at top-left
  2. Add Board
  3. 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
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?

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