Optimized High Speed NRF24L01+ Driver Class Documenation  V1.0
TMRh20 2014 - Optimized Fork of NRF24L01+ Driver
RF24.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7  */
8 
15 #ifndef __RF24_H__
16 #define __RF24_H__
17 
18 #include "RF24_config.h"
19 
20 #if defined (RF24_LINUX) || defined (LITTLEWIRE)
21  #include "utility/includes.h"
22 #elif defined SOFTSPI
23  #include <DigitalIO.h>
24 #endif
25 
32 
39 
46 
51 class RF24
52 {
53 private:
54 #ifdef SOFTSPI
55  SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> spi;
56 #elif defined (SPI_UART)
57  SPIUARTClass uspi;
58 #endif
59 
60 #if defined (RF24_LINUX) || defined (XMEGA_D3) /* XMEGA can use SPI class */
61  SPI spi;
62 #endif
63 #if defined (MRAA)
64  GPIO gpio;
65 #endif
66 
67  uint8_t ce_pin;
68  uint8_t csn_pin;
69  uint16_t spi_speed;
70 #if defined (RF24_LINUX) || defined (XMEGA_D3)
71  uint8_t spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes)
72  uint8_t spi_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
73 #endif
74  bool p_variant; /* False for RF24L01 and true for RF24L01P */
75  uint8_t payload_size;
76  bool dynamic_payloads_enabled;
77  uint8_t pipe0_reading_address[5];
78  uint8_t addr_width;
81 protected:
88  inline void beginTransaction();
89 
90  inline void endTransaction();
91 
92 public:
93 
110  RF24(uint8_t _cepin, uint8_t _cspin);
111  //#if defined (RF24_LINUX)
112 
124  RF24(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed );
125  //#endif
126 
127  #if defined (RF24_LINUX)
128  virtual ~RF24() {};
129  #endif
130 
137  bool begin(void);
138 
154  void startListening(void);
155 
165  void stopListening(void);
166 
176  bool available(void);
177 
200  void read( void* buf, uint8_t len );
201 
225  bool write( const void* buf, uint8_t len );
226 
252  void openWritingPipe(const uint8_t *address);
253 
282  void openReadingPipe(uint8_t number, const uint8_t *address);
283 
306  void printDetails(void);
307 
324  bool available(uint8_t* pipe_num);
325 
330  bool rxFifoFull();
331 
349  void powerDown(void);
350 
357  void powerUp(void) ;
358 
373  bool write( const void* buf, uint8_t len, const bool multicast );
374 
403  bool writeFast( const void* buf, uint8_t len );
404 
416  bool writeFast( const void* buf, uint8_t len, const bool multicast );
417 
444  bool writeBlocking( const void* buf, uint8_t len, uint32_t timeout );
445 
472  bool txStandBy();
473 
490  bool txStandBy(uint32_t timeout, bool startTx = 0);
491 
510  void writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
511 
520  bool isAckPayloadAvailable(void);
521 
532  void whatHappened(bool& tx_ok,bool& tx_fail,bool& rx_ready);
533 
558  void startFastWrite( const void* buf, uint8_t len, const bool multicast, bool startTx = 1 );
559 
580  void startWrite( const void* buf, uint8_t len, const bool multicast );
581 
597  void reUseTX();
598 
605  uint8_t flush_tx(void);
606 
615  bool testCarrier(void);
616 
634  bool testRPD(void) ;
635 
643  bool isValid() { return ce_pin != 0xff && csn_pin != 0xff; }
644 
650  void closeReadingPipe( uint8_t pipe ) ;
651 
672  //#if defined (FAILURE_HANDLING)
674  //#endif
675 
694  void setAddressWidth(uint8_t a_width);
695 
703  void setRetries(uint8_t delay, uint8_t count);
704 
710  void setChannel(uint8_t channel);
711 
717  uint8_t getChannel(void);
718 
731  void setPayloadSize(uint8_t size);
732 
740  uint8_t getPayloadSize(void);
741 
762  uint8_t getDynamicPayloadSize(void);
763 
773  void enableAckPayload(void);
774 
782  void enableDynamicPayloads(void);
783 
796  void enableDynamicAck();
797 
804  bool isPVariant(void) ;
805 
814  void setAutoAck(bool enable);
815 
825  void setAutoAck( uint8_t pipe, bool enable ) ;
826 
838  void setPALevel ( uint8_t level );
839 
848  uint8_t getPALevel( void );
849 
859 
867  rf24_datarate_e getDataRate( void ) ;
868 
874  void setCRCLength(rf24_crclength_e length);
875 
882 
888  void disableCRC( void ) ;
889 
906  void maskIRQ(bool tx_ok,bool tx_fail,bool rx_ready);
907 
920  uint32_t txDelay;
921 
931  uint32_t csDelay=5;
932 
960  void openReadingPipe(uint8_t number, uint64_t address);
961 
974  void openWritingPipe(uint64_t address);
975 
976 private:
977 
997  void csn(bool mode);
998 
1005  void ce(bool level);
1006 
1015  uint8_t read_register(uint8_t reg, uint8_t* buf, uint8_t len);
1016 
1023  uint8_t read_register(uint8_t reg);
1024 
1033  uint8_t write_register(uint8_t reg, const uint8_t* buf, uint8_t len);
1034 
1042  uint8_t write_register(uint8_t reg, uint8_t value);
1043 
1053  uint8_t write_payload(const void* buf, uint8_t len, const uint8_t writeType);
1054 
1064  uint8_t read_payload(void* buf, uint8_t len);
1065 
1071  uint8_t flush_rx(void);
1072 
1078  uint8_t get_status(void);
1079 
1080  #if !defined (MINIMAL)
1081 
1088  void print_status(uint8_t status);
1089 
1097  void print_observe_tx(uint8_t value);
1098 
1110  void print_byte_register(const char* name, uint8_t reg, uint8_t qty = 1);
1111 
1123  void print_address_register(const char* name, uint8_t reg, uint8_t qty = 1);
1124 #endif
1125 
1131  void toggle_features(void);
1132 
1137  uint8_t spiTrans(uint8_t cmd);
1138 
1139  #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
1140  void errNotify(void);
1141  #endif
1142 
1145 };
1146 
1147 
1983 #endif // __RF24_H__
1984 
void setAutoAck(bool enable)
Definition: RF24.cpp:1336
bool rxFifoFull()
Definition: RF24.cpp:956
bool failureDetected
Definition: RF24.h:673
void closeReadingPipe(uint8_t pipe)
Definition: RF24.cpp:1218
void printDetails(void)
Definition: RF24.cpp:512
rf24_datarate_e getDataRate(void)
Definition: RF24.cpp:1454
void setChannel(uint8_t channel)
Definition: RF24.cpp:437
void reUseTX()
Definition: RF24.cpp:871
bool writeBlocking(const void *buf, uint8_t len, uint32_t timeout)
Definition: RF24.cpp:837
bool testRPD(void)
Definition: RF24.cpp:1372
void whatHappened(bool &tx_ok, bool &tx_fail, bool &rx_ready)
Definition: RF24.cpp:1099
void setAddressWidth(uint8_t a_width)
Definition: RF24.cpp:1179
uint32_t txDelay
Definition: RF24.h:920
uint32_t speed
Definition: spi.h:74
void startWrite(const void *buf, uint8_t len, const bool multicast)
Definition: RF24.cpp:939
void setRetries(uint8_t delay, uint8_t count)
Definition: RF24.cpp:1531
void disableCRC(void)
Definition: RF24.cpp:1524
void endTransaction()
Definition: RF24.cpp:77
void powerDown(void)
Definition: RF24.cpp:755
bool write(const void *buf, uint8_t len)
Definition: RF24.cpp:831
rf24_pa_dbm_e
Definition: RF24.h:31
uint8_t flush_tx(void)
Definition: RF24.cpp:326
void beginTransaction()
Definition: RF24.cpp:68
void setPayloadSize(uint8_t size)
Definition: RF24.cpp:450
bool testCarrier(void)
Definition: RF24.cpp:1365
void powerUp(void)
Definition: RF24.cpp:764
bool isPVariant(void)
Definition: RF24.cpp:1329
void openReadingPipe(uint8_t number, const uint8_t *address)
Definition: RF24.cpp:1190
uint8_t getDynamicPayloadSize(void)
Definition: RF24.cpp:1036
void startListening(void)
Definition: RF24.cpp:696
#define delay(milisec)
bool available(void)
Definition: RF24.cpp:1060
void enableDynamicPayloads(void)
Definition: RF24.cpp:1235
bool writeFast(const void *buf, uint8_t len)
Definition: RF24.cpp:914
bool txStandBy()
Definition: RF24.cpp:961
uint8_t getPALevel(void)
Definition: RF24.cpp:1396
bool isAckPayloadAvailable(void)
Definition: RF24.cpp:1322
RF24(uint8_t _cepin, uint8_t _cspin)
Definition: RF24.cpp:418
void enableAckPayload(void)
Definition: RF24.cpp:1256
uint32_t csDelay
Definition: RF24.h:931
void setPALevel(uint8_t level)
Definition: RF24.cpp:1379
void startFastWrite(const void *buf, uint8_t len, const bool multicast, bool startTx=1)
Definition: RF24.cpp:925
void openWritingPipe(const uint8_t *address)
Definition: RF24.cpp:1128
GPIO()
void setCRCLength(rf24_crclength_e length)
Definition: RF24.cpp:1481
void stopListening(void)
Definition: RF24.cpp:727
bool setDataRate(rf24_datarate_e speed)
Definition: RF24.cpp:1404
uint8_t getPayloadSize(void)
Definition: RF24.cpp:457
void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready)
Definition: RF24.cpp:1024
bool begin(void)
Definition: RF24.cpp:571
void enableDynamicAck()
Definition: RF24.cpp:1277
rf24_crclength_e
Definition: RF24.h:45
Definition: RF24.h:51
SPI()
bool isValid()
Definition: RF24.h:643
rf24_datarate_e
Definition: RF24.h:38
void writeAckPayload(uint8_t pipe, const void *buf, uint8_t len)
Definition: RF24.cpp:1291
uint8_t getChannel(void)
Definition: RF24.cpp:443
void read(void *buf, uint8_t len)
Definition: RF24.cpp:1087
rf24_crclength_e getCRCLength(void)
Definition: RF24.cpp:1504
uint8_t mode
Definition: spi.h:70