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