Last code used in national competition

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Thu May 05 18:50:17 2022 +0000
Revision:
2:dc50e57d0808
Saving latest code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
einsteingustavo 2:dc50e57d0808 1 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 2 // Driver definition for HopeRF RFM69W/RFM69HW/RFM69CW/RFM69HCW, Semtech SX1231/1231H
einsteingustavo 2:dc50e57d0808 3 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 4 // Copyright Felix Rusu (2014), felix@lowpowerlab.com
einsteingustavo 2:dc50e57d0808 5 // http://lowpowerlab.com/
einsteingustavo 2:dc50e57d0808 6 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 7 // License
einsteingustavo 2:dc50e57d0808 8 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 9 // This program is free software; you can redistribute it
einsteingustavo 2:dc50e57d0808 10 // and/or modify it under the terms of the GNU General
einsteingustavo 2:dc50e57d0808 11 // Public License as published by the Free Software
einsteingustavo 2:dc50e57d0808 12 // Foundation; either version 3 of the License, or
einsteingustavo 2:dc50e57d0808 13 // (at your option) any later version.
einsteingustavo 2:dc50e57d0808 14 //
einsteingustavo 2:dc50e57d0808 15 // This program is distributed in the hope that it will
einsteingustavo 2:dc50e57d0808 16 // be useful, but WITHOUT ANY WARRANTY; without even the
einsteingustavo 2:dc50e57d0808 17 // implied warranty of MERCHANTABILITY or FITNESS FOR A
einsteingustavo 2:dc50e57d0808 18 // PARTICULAR PURPOSE. See the GNU General Public
einsteingustavo 2:dc50e57d0808 19 // License for more details.
einsteingustavo 2:dc50e57d0808 20 //
einsteingustavo 2:dc50e57d0808 21 // You should have received a copy of the GNU General
einsteingustavo 2:dc50e57d0808 22 // Public License along with this program.
einsteingustavo 2:dc50e57d0808 23 // If not, see <http://www.gnu.org/licenses/>.
einsteingustavo 2:dc50e57d0808 24 //
einsteingustavo 2:dc50e57d0808 25 // Licence can be viewed at
einsteingustavo 2:dc50e57d0808 26 // http://www.gnu.org/licenses/gpl-3.0.txt
einsteingustavo 2:dc50e57d0808 27 //
einsteingustavo 2:dc50e57d0808 28 // Please maintain this license information along with authorship
einsteingustavo 2:dc50e57d0808 29 // and copyright notices in any redistribution of this code
einsteingustavo 2:dc50e57d0808 30 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 31 #ifndef RFM69_h
einsteingustavo 2:dc50e57d0808 32 #define RFM69_h
einsteingustavo 2:dc50e57d0808 33 #include <mbed.h> // assumes Arduino IDE v1.0 or greater
einsteingustavo 2:dc50e57d0808 34
einsteingustavo 2:dc50e57d0808 35 #define RF69_MAX_DATA_LEN 61 // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead - 2 bytes crc)
einsteingustavo 2:dc50e57d0808 36 //#define RF69_SPI_CS SS // SS is the SPI slave select pin, for instance D10 on ATmega328
einsteingustavo 2:dc50e57d0808 37
einsteingustavo 2:dc50e57d0808 38 #define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
einsteingustavo 2:dc50e57d0808 39 #define RF69_MODE_SLEEP 0 // XTAL OFF
einsteingustavo 2:dc50e57d0808 40 #define RF69_MODE_STANDBY 1 // XTAL ON
einsteingustavo 2:dc50e57d0808 41 #define RF69_MODE_SYNTH 2 // PLL ON
einsteingustavo 2:dc50e57d0808 42 #define RF69_MODE_RX 3 // RX MODE
einsteingustavo 2:dc50e57d0808 43 #define RF69_MODE_TX 4 // TX MODE
einsteingustavo 2:dc50e57d0808 44
einsteingustavo 2:dc50e57d0808 45 // available frequency bands
einsteingustavo 2:dc50e57d0808 46 #define RF69_315MHZ 31 // non trivial values to avoid misconfiguration
einsteingustavo 2:dc50e57d0808 47 #define RF69_433MHZ 43
einsteingustavo 2:dc50e57d0808 48 #define RF69_868MHZ 86
einsteingustavo 2:dc50e57d0808 49 #define RF69_915MHZ 91
einsteingustavo 2:dc50e57d0808 50
einsteingustavo 2:dc50e57d0808 51 #define null 0
einsteingustavo 2:dc50e57d0808 52 #define COURSE_TEMP_COEF -90 // puts the temperature reading in the ballpark, user can fine tune the returned value
einsteingustavo 2:dc50e57d0808 53 #define RF69_BROADCAST_ADDR 255
einsteingustavo 2:dc50e57d0808 54 #define RF69_CSMA_LIMIT_MS 1000
einsteingustavo 2:dc50e57d0808 55 #define RF69_TX_LIMIT_MS 1000
einsteingustavo 2:dc50e57d0808 56 #define RF69_FSTEP 61.03515625 // == FXOSC / 2^19 = 32MHz / 2^19 (p13 in datasheet)
einsteingustavo 2:dc50e57d0808 57
einsteingustavo 2:dc50e57d0808 58 class RFM69 {
einsteingustavo 2:dc50e57d0808 59 public:
einsteingustavo 2:dc50e57d0808 60 static volatile uint8_t DATA[RF69_MAX_DATA_LEN]; // recv/xmit buf, including header & crc bytes
einsteingustavo 2:dc50e57d0808 61 static volatile uint8_t DATALEN;
einsteingustavo 2:dc50e57d0808 62 static volatile uint8_t SENDERID;
einsteingustavo 2:dc50e57d0808 63 static volatile uint8_t TARGETID; // should match _address
einsteingustavo 2:dc50e57d0808 64 static volatile uint8_t PAYLOADLEN;
einsteingustavo 2:dc50e57d0808 65 static volatile uint8_t ACK_REQUESTED;
einsteingustavo 2:dc50e57d0808 66 static volatile uint8_t ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
einsteingustavo 2:dc50e57d0808 67 static volatile int16_t RSSI; // most accurate RSSI during reception (closest to the reception)
einsteingustavo 2:dc50e57d0808 68 static volatile uint8_t _mode; // should be protected?
einsteingustavo 2:dc50e57d0808 69
einsteingustavo 2:dc50e57d0808 70 RFM69(PinName mosi, PinName miso, PinName sclk, PinName slaveSelectPin, PinName interrupt);
einsteingustavo 2:dc50e57d0808 71
einsteingustavo 2:dc50e57d0808 72 bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1);
einsteingustavo 2:dc50e57d0808 73 void setAddress(uint8_t addr);
einsteingustavo 2:dc50e57d0808 74 void setNetwork(uint8_t networkID);
einsteingustavo 2:dc50e57d0808 75 bool canSend();
einsteingustavo 2:dc50e57d0808 76 void send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false);
einsteingustavo 2:dc50e57d0808 77 bool sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=40); // 40ms roundtrip req for 61byte packets
einsteingustavo 2:dc50e57d0808 78 bool receiveDone();
einsteingustavo 2:dc50e57d0808 79 bool ACKReceived(uint8_t fromNodeID);
einsteingustavo 2:dc50e57d0808 80 bool ACKRequested();
einsteingustavo 2:dc50e57d0808 81 void sendACK(const void* buffer = "", uint8_t bufferSize=0);
einsteingustavo 2:dc50e57d0808 82 uint32_t getFrequency();
einsteingustavo 2:dc50e57d0808 83 void setFrequency(uint32_t freqHz);
einsteingustavo 2:dc50e57d0808 84 void encrypt(const char* key);
einsteingustavo 2:dc50e57d0808 85 // void setCS(uint8_t newSPISlaveSelect);
einsteingustavo 2:dc50e57d0808 86 int16_t readRSSI(bool forceTrigger=false);
einsteingustavo 2:dc50e57d0808 87 void promiscuous(bool onOff=true);
einsteingustavo 2:dc50e57d0808 88 void setHighPower(bool onOFF=true); // has to be called after initialize() for RFM69HW
einsteingustavo 2:dc50e57d0808 89 void setPowerLevel(uint8_t level); // reduce/increase transmit power level
einsteingustavo 2:dc50e57d0808 90 void sleep();
einsteingustavo 2:dc50e57d0808 91 uint8_t readTemperature(int8_t calFactor=0); // get CMOS temperature (8bit)
einsteingustavo 2:dc50e57d0808 92 void rcCalibration(); // calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy]
einsteingustavo 2:dc50e57d0808 93
einsteingustavo 2:dc50e57d0808 94 // allow hacking registers by making these public
einsteingustavo 2:dc50e57d0808 95 uint8_t readReg(uint8_t addr);
einsteingustavo 2:dc50e57d0808 96 void writeReg(uint8_t addr, uint8_t val);
einsteingustavo 2:dc50e57d0808 97 void readAllRegs();
einsteingustavo 2:dc50e57d0808 98
einsteingustavo 2:dc50e57d0808 99 protected:
einsteingustavo 2:dc50e57d0808 100 void isr0();
einsteingustavo 2:dc50e57d0808 101 void virtual interruptHandler();
einsteingustavo 2:dc50e57d0808 102 void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false);
einsteingustavo 2:dc50e57d0808 103
einsteingustavo 2:dc50e57d0808 104 static RFM69* selfPointer;
einsteingustavo 2:dc50e57d0808 105 DigitalOut _slaveSelectPin;
einsteingustavo 2:dc50e57d0808 106 InterruptIn _interrupt;
einsteingustavo 2:dc50e57d0808 107 uint8_t _address;
einsteingustavo 2:dc50e57d0808 108 Timer t;
einsteingustavo 2:dc50e57d0808 109 bool _promiscuousMode;
einsteingustavo 2:dc50e57d0808 110 uint8_t _powerLevel;
einsteingustavo 2:dc50e57d0808 111 bool _isRFM69HW;
einsteingustavo 2:dc50e57d0808 112 SPI _spi;
einsteingustavo 2:dc50e57d0808 113 void receiveBegin();
einsteingustavo 2:dc50e57d0808 114 void setMode(uint8_t mode);
einsteingustavo 2:dc50e57d0808 115 void setHighPowerRegs(bool onOff);
einsteingustavo 2:dc50e57d0808 116 void select();
einsteingustavo 2:dc50e57d0808 117 void unselect();
einsteingustavo 2:dc50e57d0808 118 };
einsteingustavo 2:dc50e57d0808 119
einsteingustavo 2:dc50e57d0808 120 #endif
einsteingustavo 2:dc50e57d0808 121