MySensors Library & Examples  2.3.2
RF24.h
Go to the documentation of this file.
1 /*
2 * The MySensors Arduino library handles the wireless radio link and protocol
3 * between your home built sensors/actuators and HA controller of choice.
4 * The sensors forms a self healing radio network with optional repeaters. Each
5 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6 * network topology allowing messages to be routed to nodes.
7 *
8 * Created by Henrik Ekblad <[email protected]>
9 * Copyright (C) 2013-2019 Sensnology AB
10 * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11 *
12 * Documentation: http://www.mysensors.org
13 * Support Forum: http://forum.mysensors.org
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * Based on maniacbug's RF24 library, copyright (C) 2011 J. Coliz <[email protected]>
20 * RF24 driver refactored and optimized for speed and size, copyright (C) 2017 Olivier Mauti <[email protected]>
21 *
22 * Definitions for Nordic nRF24L01+ radios:
23 * https://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24L01P
24 *
25 */
26 
59 #ifndef __RF24_H__
60 #define __RF24_H__
61 
62 #include "RF24registers.h"
63 
64 #if !defined(RF24_SPI)
65 #define RF24_SPI hwSPI
66 #endif
67 
68 #if defined(ARDUINO_ARCH_AVR)
69 #define DEFAULT_RF24_CE_PIN (9)
70 #elif defined(ARDUINO_ARCH_ESP8266)
71 #define DEFAULT_RF24_CE_PIN (4)
72 #elif defined(ARDUINO_ARCH_ESP32)
73 #define DEFAULT_RF24_CE_PIN (17)
74 #elif defined(ARDUINO_ARCH_SAMD)
75 #define DEFAULT_RF24_CE_PIN (27)
76 #elif defined(LINUX_ARCH_RASPBERRYPI)
77 #define DEFAULT_RF24_CE_PIN (22)
78 //#define DEFAULT_RF24_CS_PIN (24)
79 #elif defined(ARDUINO_ARCH_STM32F1)
80 #define DEFAULT_RF24_CE_PIN (PB0)
81 #elif defined(TEENSYDUINO)
82 #define DEFAULT_RF24_CE_PIN (9)
83 #else
84 #define DEFAULT_RF24_CE_PIN (9)
85 #endif
86 
87 #define DEFAULT_RF24_CS_PIN (SS)
88 
89 
90 #define LOCAL static
91 
92 // SPI settings
93 #define RF24_SPI_DATA_ORDER MSBFIRST
94 #define RF24_SPI_DATA_MODE SPI_MODE0
95 
96 #define RF24_BROADCAST_ADDRESS (255u)
97 
98 // verify RF24 IRQ defs
99 #if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
100 #if !defined(MY_RF24_IRQ_PIN)
101 #error Message buffering feature requires MY_RF24_IRQ_PIN to be defined!
102 #endif
103 // SoftSPI does not support usingInterrupt()
104 #ifdef MY_SOFTSPI
105 #error RF24 IRQ usage cannot be used with Soft SPI
106 #endif
107 // ESP8266 does not support usingInterrupt()
108 #ifdef ARDUINO_ARCH_ESP8266
109 #error RF24 IRQ usage cannot be used with ESP8266
110 #endif
111 #ifndef SPI_HAS_TRANSACTION
112 #error RF24 IRQ usage requires transactional SPI support
113 #endif
114 #else
115 #ifdef MY_RX_MESSAGE_BUFFER_SIZE
116 #error Receive message buffering requires RF24 IRQ usage
117 #endif
118 #endif
119 
120 
121 // RF24 settings
122 #if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
123 #define RF24_CONFIGURATION (uint8_t) ((RF24_CRC_16 << 2) | (1 << RF24_MASK_TX_DS) | (1 << RF24_MASK_MAX_RT))
124 #else
125 #define RF24_CONFIGURATION (uint8_t) (RF24_CRC_16 << 2)
126 #endif
127 #define RF24_FEATURE (uint8_t)( _BV(RF24_EN_DPL))
128 #define RF24_RF_SETUP (uint8_t)(( ((MY_RF24_DATARATE & 0b10 ) << 4) | ((MY_RF24_DATARATE & 0b01 ) << 3) | (MY_RF24_PA_LEVEL << 1) ) + 1)
129 
130 // powerup delay
131 #define RF24_POWERUP_DELAY_MS (100u)
132 
133 // pipes
134 #define RF24_BROADCAST_PIPE (1u)
135 #define RF24_NODE_PIPE (0u)
136 
137 // functions
142 LOCAL void RF24_csn(const bool level);
147 LOCAL void RF24_ce(const bool level);
156 LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, const uint8_t len,
157  const bool readMode);
163 LOCAL uint8_t RF24_spiByteTransfer(const uint8_t cmd);
169 LOCAL uint8_t RF24_RAW_readByteRegister(const uint8_t cmd);
176 LOCAL uint8_t RF24_RAW_writeByteRegister(const uint8_t cmd, const uint8_t value);
177 
178 // helper macros
179 #define RF24_readByteRegister(__reg) RF24_RAW_readByteRegister(RF24_CMD_READ_REGISTER | (RF24_REGISTER_MASK & (__reg)))
180 #define RF24_writeByteRegister(__reg,__value) RF24_RAW_writeByteRegister(RF24_CMD_WRITE_REGISTER | (RF24_REGISTER_MASK & (__reg)), __value)
181 #define RF24_writeMultiByteRegister(__reg,__buf,__len) RF24_spiMultiByteTransfer(RF24_CMD_WRITE_REGISTER | (RF24_REGISTER_MASK & (__reg)),(uint8_t *)__buf, __len,false)
182 
183 
186 LOCAL void RF24_flushRX(void);
190 LOCAL void RF24_flushTX(void);
195 LOCAL uint8_t RF24_getStatus(void);
200 LOCAL uint8_t RF24_getFIFOStatus(void) __attribute__((unused));
205 LOCAL void RF24_openWritingPipe(const uint8_t recipient);
209 LOCAL void RF24_startListening(void);
213 LOCAL void RF24_stopListening(void);
217 LOCAL void RF24_sleep(void);
221 LOCAL void RF24_standBy(void);
225 LOCAL void RF24_powerDown(void);
229 LOCAL void RF24_powerUp(void);
238 LOCAL bool RF24_sendMessage(const uint8_t recipient, const void *buf, const uint8_t len,
239  const bool noACK = false);
244 LOCAL uint8_t RF24_getDynamicPayloadSize(void);
249 LOCAL bool RF24_isDataAvailable(void);
254 LOCAL uint8_t RF24_readMessage(void *buf);
259 LOCAL void RF24_setNodeAddress(const uint8_t address);
264 LOCAL uint8_t RF24_getNodeID(void);
269 LOCAL bool RF24_sanityCheck(void);
274 LOCAL bool RF24_initialize(void);
279 LOCAL void RF24_setChannel(const uint8_t channel);
285 LOCAL void RF24_setRetries(const uint8_t retransmitDelay, const uint8_t retransmitCount);
290 LOCAL void RF24_setAddressWidth(const uint8_t addressWidth);
295 LOCAL void RF24_setRFSetup(const uint8_t RFsetup);
300 LOCAL void RF24_setFeature(const uint8_t feature);
305 LOCAL uint8_t RF24_getFeature(void);
310 LOCAL void RF24_setPipe(const uint8_t pipe);
315 LOCAL void RF24_setAutoACK(const uint8_t pipe);
320 LOCAL void RF24_setDynamicPayload(const uint8_t pipe);
325 LOCAL void RF24_setRFConfiguration(const uint8_t configuration);
332 LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t *address, const uint8_t addressWidth);
338 LOCAL void RF24_setPipeLSB(const uint8_t pipe, const uint8_t LSB);
343 LOCAL uint8_t RF24_getObserveTX(void);
349 LOCAL uint8_t RF24_setStatus(const uint8_t status);
353 LOCAL void RF24_enableFeatures(void);
358 LOCAL uint8_t RF24_getTxPowerPercent(void);
363 LOCAL int16_t RF24_getTxPowerLevel(void);
369 LOCAL bool RF24_setTxPowerPercent(const uint8_t newPowerPercent);
374 LOCAL int16_t RF24_getSendingRSSI(void);
388 
389 #if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
390 
393 typedef void (*RF24_receiveCallbackType)(void);
402 LOCAL void RF24_registerReceiveCallback(RF24_receiveCallbackType cb);
403 #endif
404 
405 #endif // __RF24_H__
406 
RF24_ce
LOCAL void RF24_ce(const bool level)
RF24_ce.
RF24_setRFSetup
LOCAL void RF24_setRFSetup(const uint8_t RFsetup)
RF24_setRFSetup.
RF24_getReceivedPowerDetector
LOCAL bool RF24_getReceivedPowerDetector(void) __attribute__((unused))
Retrieve latched RPD power level, in receive mode (for testing, nRF24L01+ only).
RF24_powerDown
LOCAL void RF24_powerDown(void)
RF24_powerDown.
RF24_powerUp
LOCAL void RF24_powerUp(void)
RF24_powerUp.
RF24_getDynamicPayloadSize
LOCAL uint8_t RF24_getDynamicPayloadSize(void)
RF24_getDynamicPayloadSize.
RF24_setFeature
LOCAL void RF24_setFeature(const uint8_t feature)
RF24_setFeature.
RF24_RAW_readByteRegister
LOCAL uint8_t RF24_RAW_readByteRegister(const uint8_t cmd)
RF24_RAW_readByteRegister.
RF24_getNodeID
LOCAL uint8_t RF24_getNodeID(void)
RF24_getNodeID.
RF24_enableConstantCarrierWave
LOCAL void RF24_enableConstantCarrierWave(void) __attribute__((unused))
Generate a constant carrier wave at active channel & transmit power (for testing only).
RF24_getStatus
LOCAL uint8_t RF24_getStatus(void)
RF24_getStatus.
RF24_stopListening
LOCAL void RF24_stopListening(void)
RF24_stopListening.
RF24_sendMessage
LOCAL bool RF24_sendMessage(const uint8_t recipient, const void *buf, const uint8_t len, const bool noACK=false)
RF24_sendMessage.
RF24_getSendingRSSI
LOCAL int16_t RF24_getSendingRSSI(void)
RF24_getSendingRSSI.
RF24_setAddressWidth
LOCAL void RF24_setAddressWidth(const uint8_t addressWidth)
RF24_setAddressWidth.
RF24_getFIFOStatus
LOCAL uint8_t RF24_getFIFOStatus(void) __attribute__((unused))
RF24_getFIFOStatus.
RF24_setRFConfiguration
LOCAL void RF24_setRFConfiguration(const uint8_t configuration)
RF24_setRFConfiguration.
RF24_setRetries
LOCAL void RF24_setRetries(const uint8_t retransmitDelay, const uint8_t retransmitCount)
RF24_setRetries.
RF24_initialize
LOCAL bool RF24_initialize(void)
RF24_initialize.
RF24_getTxPowerPercent
LOCAL uint8_t RF24_getTxPowerPercent(void)
RF24_getTxPowerPercent.
RF24_enableFeatures
LOCAL void RF24_enableFeatures(void)
RF24_enableFeatures.
RF24_sleep
LOCAL void RF24_sleep(void)
RF24_sleep.
__attribute__
struct @4::@5 __attribute__
Doxygen will complain without this comment.
Definition: DigitalPin.h:65
RF24_getObserveTX
LOCAL uint8_t RF24_getObserveTX(void)
RF24_getObserveTX.
RF24_openWritingPipe
LOCAL void RF24_openWritingPipe(const uint8_t recipient)
RF24_openWritingPipe.
RF24_setDynamicPayload
LOCAL void RF24_setDynamicPayload(const uint8_t pipe)
RF24_setDynamicPayload.
RF24_flushTX
LOCAL void RF24_flushTX(void)
RF24_flushTX.
RF24_startListening
LOCAL void RF24_startListening(void)
RF24_startListening.
RF24_spiMultiByteTransfer
LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, const uint8_t len, const bool readMode)
RF24_spiMultiByteTransfer.
RF24_RAW_writeByteRegister
LOCAL uint8_t RF24_RAW_writeByteRegister(const uint8_t cmd, const uint8_t value)
RF24_RAW_writeByteRegister.
RF24_setPipeAddress
LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t *address, const uint8_t addressWidth)
RF24_setPipeAddress.
RF24_getFeature
LOCAL uint8_t RF24_getFeature(void)
RF24_getFeature.
RF24_setTxPowerPercent
LOCAL bool RF24_setTxPowerPercent(const uint8_t newPowerPercent)
RF24_setTxPowerPercent.
RF24_setPipe
LOCAL void RF24_setPipe(const uint8_t pipe)
RF24_setPipe.
RF24_isDataAvailable
LOCAL bool RF24_isDataAvailable(void)
RF24_isDataAvailable.
RF24_setChannel
LOCAL void RF24_setChannel(const uint8_t channel)
RF24_setChannel.
RF24_setPipeLSB
LOCAL void RF24_setPipeLSB(const uint8_t pipe, const uint8_t LSB)
RF24_setPipeLSB.
RF24_setNodeAddress
LOCAL void RF24_setNodeAddress(const uint8_t address)
RF24_setNodeAddress.
RF24_disableConstantCarrierWave
LOCAL void RF24_disableConstantCarrierWave(void) __attribute__((unused))
Stop generating a constant carrier wave (for testing only).
RF24_setAutoACK
LOCAL void RF24_setAutoACK(const uint8_t pipe)
RF24_setAutoACK.
LOCAL
#define LOCAL
static
Definition: RF24.h:90
RF24_standBy
LOCAL void RF24_standBy(void)
RF24_standBy.
RF24_setStatus
LOCAL uint8_t RF24_setStatus(const uint8_t status)
RF24_setStatus.
RF24_flushRX
LOCAL void RF24_flushRX(void)
RF24_flushRX.
RF24_readMessage
LOCAL uint8_t RF24_readMessage(void *buf)
RF24_readMessage.
RF24_getTxPowerLevel
LOCAL int16_t RF24_getTxPowerLevel(void)
RF24_getTxPowerLevel.
RF24_sanityCheck
LOCAL bool RF24_sanityCheck(void)
RF24_sanityCheck.
RF24_csn
LOCAL void RF24_csn(const bool level)
RF24_csn.
RF24_spiByteTransfer
LOCAL uint8_t RF24_spiByteTransfer(const uint8_t cmd)
RF24_spiByteTransfer.