COSA
An Object-Oriented Platform for Arduino Programming
NRF24L01P.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_NRF24L01P_HH
22 #define COSA_NRF24L01P_HH
23 
24 #include "Cosa/SPI.hh"
25 #include "Cosa/OutputPin.hh"
27 #include "Cosa/Wireless.hh"
28 #if !defined(BOARD_ATTINYX5)
29 
56 class NRF24L01P : protected SPI::Driver, public Wireless::Driver {
57 public:
61  static const size_t DEVICE_PAYLOAD_MAX = 32;
62 
67  static const size_t PAYLOAD_MAX = DEVICE_PAYLOAD_MAX - 2;
68 
79 #if defined(BOARD_ATTINYX4)
80  NRF24L01P(uint16_t net, uint8_t dev,
84 #elif defined(BOARD_ATMEGA2560)
85  NRF24L01P(uint16_t net, uint8_t dev,
89 #else
90  NRF24L01P(uint16_t net, uint8_t dev,
94 #endif
95 
100  void powerup();
101 
105  void standby();
106 
111  virtual void powerdown();
112 
120  virtual bool begin(const void* config = NULL);
121 
128  virtual bool end()
129  {
130  standby();
131  return (true);
132  }
133 
140  virtual bool available();
141 
153  virtual int send(uint8_t dest, uint8_t port, const iovec_t* vec);
154 
168  virtual int send(uint8_t dest, uint8_t port, const void* buf, size_t len);
169 
185  virtual int recv(uint8_t& src, uint8_t& port, void* buf, size_t count,
186  uint32_t ms = 0L);
187 
193  virtual void output_power_level(int8_t dBm);
194 
199  uint16_t trans() const
200  {
201  return (m_trans);
202  }
203 
208  uint16_t retrans() const
209  {
210  return (m_retrans);
211  }
212 
217  uint16_t drops() const
218  {
219  return (m_drops);
220  }
221 
222 protected:
226  enum State {
231  } __attribute__((packed));
232 
236  enum Command {
237  R_REGISTER = 0x00,
238  W_REGISTER = 0x20,
239  REG_MASK = 0x1f,
240  R_RX_PAYLOAD = 0x61,
241  W_TX_PAYLOAD = 0xa0,
242  FLUSH_TX = 0xe1,
243  FLUSH_RX = 0xe2,
244  REUSE_TX_PL = 0xe3,
245  R_RX_PL_WID = 0x60,
246  W_ACK_PAYLOAD = 0xa8,
247  PIPE_MASK = 0x07,
249  NOP = 0xff
250  } __attribute__((packed));
251 
257  uint8_t read(Command cmd);
258 
265  void read(Command cmd, void* buf, size_t size);
266 
271  void write(Command cmd);
272 
278  void write(Command cmd, uint8_t data);
279 
286  void write(Command cmd, const void* buf, size_t size);
287 
291  enum Register {
292  CONFIG = 0x00,
293  EN_AA = 0x01,
294  EN_RXADDR = 0x02,
295  SETUP_AW = 0x03,
296  SETUP_RETR = 0x04,
297  RF_CH = 0x05,
298  RF_SETUP = 0x06,
299  STATUS = 0x07,
300  OBSERVE_TX = 0x08,
301  RPD = 0x09,
302  RX_ADDR_P0 = 0x0a,
303  RX_ADDR_P1 = 0x0b,
304  RX_ADDR_P2 = 0x0c,
305  RX_ADDR_P3 = 0x0d,
306  RX_ADDR_P4 = 0x0e,
307  RX_ADDR_P5 = 0x0f,
308  TX_ADDR = 0x10,
309  RX_PW_P0 = 0x11,
310  RX_PW_P1 = 0x12,
311  RX_PW_P2 = 0x13,
312  RX_PW_P3 = 0x14,
313  RX_PW_P4 = 0x15,
314  RX_PW_P5 = 0x16,
315  FIFO_STATUS = 0x17,
316  DYNPD = 0x1c,
317  FEATURE = 0x1d
318  } __attribute__((packed));
319 
323  enum {
327  EN_CRC = 3,
328  CRCO = 2,
329  PWR_UP = 1,
330  PRIM_RX = 0,
331  } __attribute__((packed));
332 
333  enum {
336  } __attribute__((packed));
337 
341  enum {
342  ENAA_P5 = 5,
343  ENAA_P4 = 4,
344  ENAA_P3 = 3,
345  ENAA_P2 = 2,
346  ENAA_P1 = 1,
347  ENAA_P0 = 0,
348  ENAA_PA = 0x3f
349  } __attribute__((packed));
350 
354  enum {
355  ERX_P5 = 5,
356  ERX_P4 = 4,
357  ERX_P3 = 3,
358  ERX_P2 = 2,
359  ERX_P1 = 1,
360  ERX_P0 = 0,
361  ERX_PA = 0x3f
362  } __attribute__((packed));
363 
367  enum {
368  AW = 0,
369  AW_3BYTES = 1,
370  AW_4BYTES = 2,
372  } __attribute__((packed));
373 
377  enum {
378  ARD = 4,
379  DEFAULT_ARD = 2,
381  ARC = 0,
382  DEFAULT_ARC = 15
384  } __attribute__((packed));
385 
389  enum {
390  CONT_WAVE = 7,
391  RF_DR_LOW = 5,
394  RF_PWR = 1
395  } __attribute__((packed));
396 
400  enum {
404  } __attribute__((packed));
405 
409  enum {
414  } __attribute__((packed));
415 
419  enum {
420  RX_DR = 6,
421  TX_DS = 5,
422  MAX_RT = 4,
423  RX_P_NO = 1,
424  RX_P_NO_MASK = 0x0e,
425  RX_P_NO_NONE = 0x07,
427  } __attribute__((packed));
428 
432  union status_t {
433  uint8_t as_byte;
434  struct {
435  uint8_t tx_full:1;
436  uint8_t rx_p_no:3;
437  uint8_t max_rt:1;
438  uint8_t tx_ds:1;
439  uint8_t rx_dr:1;
440  uint8_t reserved:1;
441  };
442 
447  status_t(uint8_t value)
448  {
449  as_byte = value;
450  }
451  };
452 
456  enum {
457  PLOS_CNT = 4,
458  ARC_CNT = 0
459  } __attribute__((packed));
460 
464  union observe_tx_t {
465  uint8_t as_byte;
466  struct {
467  uint8_t arc_cnt:4;
468  uint8_t plos_cnt:4;
469  };
470 
476  observe_tx_t(uint8_t value)
477  {
478  as_byte = value;
479  }
480  };
481 
485  enum {
486  TX_REUSE = 6,
487  TX_FULL = 5,
488  TX_EMPTY = 4,
489  RX_FULL = 1,
490  RX_EMPTY = 0,
491  } __attribute__((packed));
492 
497  uint8_t as_byte;
498  struct {
499  uint8_t rx_empty:1;
500  uint8_t rx_full:1;
501  uint8_t reserved1:2;
502  uint8_t tx_empty:1;
503  uint8_t tx_full:1;
504  uint8_t tx_reuse:1;
505  uint8_t reserved2:1;
506  };
507 
512  fifo_status_t(uint8_t value)
513  {
514  as_byte = value;
515  }
516  };
517 
521  enum {
522  DPL_P5 = 5,
523  DPL_P4 = 4,
524  DPL_P3 = 3,
525  DPL_P2 = 2,
526  DPL_P1 = 1,
527  DPL_P0 = 0,
528  DPL_PA = 0x3f
529  } __attribute__((packed));
530 
534  enum {
535  EN_DPL = 2,
538  } __attribute__((packed));
539 
540 
547  uint8_t read(Register reg)
548  {
549  return (read((Command) (R_REGISTER | (REG_MASK & reg))));
550  }
551 
559  void read(Register reg, void* buf, size_t size)
560  {
561  read((Command) (R_REGISTER | (REG_MASK & reg)), buf, size);
562  }
563 
571  void write(Register reg, uint8_t data)
572  {
573  write((Command) (W_REGISTER | (REG_MASK & reg)), data);
574  }
575 
584  void write(Register reg, const void* buf, size_t size)
585  {
586  write((Command) (W_REGISTER | (REG_MASK & reg)), buf, size);
587  }
588 
592  static const uint16_t Tpd2stby_ms = 3;
593  static const uint16_t Tstby2a_us = 130;
594  static const uint16_t Thce_us = 10;
595 
599  enum {
600  AW_MAX = 5,
601  PIPE_MAX = 6,
602  } __attribute__((packed));
603 
607  class IRQPin : public ExternalInterrupt {
608  public:
610  InterruptMode mode,
611  NRF24L01P* nrf) :
612  ExternalInterrupt(pin, mode),
613  m_nrf(nrf)
614  {}
615  friend class NRF24L01P;
616  private:
617  NRF24L01P* m_nrf;
618  };
619 
624 
625  uint16_t m_trans;
626  uint16_t m_retrans;
627  uint16_t m_drops;
628 
634 
640  {
641  return (read(FIFO_STATUS));
642  }
643 
650  {
651  return (read(OBSERVE_TX));;
652  }
653 
658  void transmit_mode(uint8_t dest);
659 
663  void receiver_mode();
664 
665  // Allow operators to access internals
666  friend IOStream& operator<<(IOStream& outs, status_t status);
667  friend IOStream& operator<<(IOStream& outs, fifo_status_t status);
668  friend IOStream& operator<<(IOStream& outs, observe_tx_t observe);
669 };
670 
678 
686 
694 
695 #endif
696 #endif
void write(Register reg, uint8_t data)
Definition: NRF24L01P.hh:571
virtual bool end()
Definition: NRF24L01P.hh:128
uint8_t tx_full
TX FIFO full.
Definition: NRF24L01P.hh:435
virtual void powerdown()
Definition: NRF24L01P.cpp:169
State m_state
Transceiver state.
Definition: NRF24L01P.hh:623
Setup of auto retransmission.
Definition: NRF24L01P.hh:296
Data send TX FIFO interrupt.
Definition: NRF24L01P.hh:421
Set RF data rate to 250 kbps.
Definition: NRF24L01P.hh:391
NRF24L01P(uint16_t net, uint8_t dev, Board::DigitalPin csn=Board::D10, Board::DigitalPin ce=Board::D9, Board::ExternalInterruptPin irq=Board::EXT0)
Definition: NRF24L01P.cpp:28
RF channel.
Definition: NRF24L01P.hh:297
Enable auto acknowledgement.
Definition: NRF24L01P.hh:293
TX FIFO full flag.
Definition: NRF24L01P.hh:426
static const uint16_t Thce_us
Definition: NRF24L01P.hh:594
uint16_t m_drops
Dropped messages.
Definition: NRF24L01P.hh:627
CRC encoding scheme (2/1 bytes CRC).
Definition: NRF24L01P.hh:328
FIFO status register.
Definition: NRF24L01P.hh:315
Enable all data pipes.
Definition: NRF24L01P.hh:361
Default auto retransmit delay (500 us)
Definition: NRF24L01P.hh:380
Receive address data pipe 0.
Definition: NRF24L01P.hh:302
IRQPin(Board::ExternalInterruptPin pin, InterruptMode mode, NRF24L01P *nrf)
Definition: NRF24L01P.hh:609
Power up/down.
Definition: NRF24L01P.hh:329
void receiver_mode()
Definition: NRF24L01P.cpp:129
IRQPin m_irq
Chip interrupt pin and handler.
Definition: NRF24L01P.hh:621
static const uint16_t Tpd2stby_ms
Definition: NRF24L01P.hh:592
uint8_t as_byte
Byte representation of fifo status.
Definition: NRF24L01P.hh:497
RX FIFO empty flag.
Definition: NRF24L01P.hh:490
Mask pipe address.
Definition: NRF24L01P.hh:247
#define NULL
Definition: Types.h:101
Configuration register.
Definition: NRF24L01P.hh:292
Enable payload with ACK.
Definition: NRF24L01P.hh:536
Mask interrupt caused byt MAX_RT.
Definition: NRF24L01P.hh:326
virtual bool begin(const void *config=NULL)
Definition: NRF24L01P.cpp:178
uint8_t max_rt
Max number of TX retransmit interrupt.
Definition: NRF24L01P.hh:437
Enable CRC.
Definition: NRF24L01P.hh:327
Read command and status register.
Definition: NRF24L01P.hh:237
Read RX payload width.
Definition: NRF24L01P.hh:245
uint16_t m_trans
Send count.
Definition: NRF24L01P.hh:625
status_t(uint8_t value)
Definition: NRF24L01P.hh:447
static const size_t PAYLOAD_MAX
Definition: NRF24L01P.hh:67
Read RX payload.
Definition: NRF24L01P.hh:240
friend IOStream & operator<<(IOStream &outs, status_t status)
Definition: NRF24L01P.cpp:338
Definition: Types.h:391
uint16_t retrans() const
Definition: NRF24L01P.hh:208
Maximum number of TX retransmits interrupt.
Definition: NRF24L01P.hh:422
void read(Register reg, void *buf, size_t size)
Definition: NRF24L01P.hh:559
Status register.
Definition: NRF24L01P.hh:299
observe_tx_t read_observe_tx()
Definition: NRF24L01P.hh:649
Disable AUTOACK on this specific packet.
Definition: NRF24L01P.hh:248
static const size_t DEVICE_PAYLOAD_MAX
Definition: NRF24L01P.hh:61
Max number of pipes.
Definition: NRF24L01P.hh:601
TX FIFO full flag.
Definition: NRF24L01P.hh:487
Max address width in bytes.
Definition: NRF24L01P.hh:600
uint16_t trans() const
Definition: NRF24L01P.hh:199
status_t read_status()
Definition: NRF24L01P.cpp:101
uint8_t tx_ds
Data send TX FIFO interrupt.
Definition: NRF24L01P.hh:438
Enable dynamic payload length.
Definition: NRF24L01P.hh:535
Data pipe number for available payload (3b).
Definition: NRF24L01P.hh:423
Mask pipe number.
Definition: NRF24L01P.hh:424
Enable auto acknowledgement data pipe 5.
Definition: NRF24L01P.hh:342
Force PLL lock signal.
Definition: NRF24L01P.hh:392
void write(Command cmd)
Definition: NRF24L01P.cpp:69
Enable dynamic payload length data pipe 5.
Definition: NRF24L01P.hh:522
uint8_t as_byte
Byte representation of performance statistics.
Definition: NRF24L01P.hh:465
uint16_t drops() const
Definition: NRF24L01P.hh:217
static const uint16_t Tstby2a_us
Definition: NRF24L01P.hh:593
ExternalInterruptPin
Definition: ATmega1284P.hh:190
Enable data pipe 5.
Definition: NRF24L01P.hh:355
void powerup()
Definition: NRF24L01P.cpp:112
Reuse last transmitted payload.
Definition: NRF24L01P.hh:244
Reuse last transmitted data packat.
Definition: NRF24L01P.hh:486
Setup of address width.
Definition: NRF24L01P.hh:295
Mask register address (5b).
Definition: NRF24L01P.hh:239
fifo_status_t read_fifo_status()
Definition: NRF24L01P.hh:639
uint8_t rx_p_no
Data pipe number for available payload.
Definition: NRF24L01P.hh:436
Feature register.
Definition: NRF24L01P.hh:317
RF setup register.
Definition: NRF24L01P.hh:298
status_t m_status
Latest status.
Definition: NRF24L01P.hh:622
Set RF output power in TX mode (bits 2).
Definition: NRF24L01P.hh:394
Transmit observe register.
Definition: NRF24L01P.hh:300
fifo_status_t(uint8_t value)
Definition: NRF24L01P.hh:512
virtual bool available()
Definition: NRF24L01P.cpp:280
Mask interrupt caused by TX_DS.
Definition: NRF24L01P.hh:325
virtual int send(uint8_t dest, uint8_t port, const iovec_t *vec)
Definition: NRF24L01P.cpp:211
Write command and status register.
Definition: NRF24L01P.hh:238
Write TX payload with ACK (3 bit addr).
Definition: NRF24L01P.hh:246
Enable dynamic payload length.
Definition: NRF24L01P.hh:316
Flush TX FIFO.
Definition: NRF24L01P.hh:242
uint16_t m_retrans
Retransmittion count.
Definition: NRF24L01P.hh:626
PWR_UP bit settings.
Definition: NRF24L01P.hh:334
virtual int recv(uint8_t &src, uint8_t &port, void *buf, size_t count, uint32_t ms=0L)
Definition: NRF24L01P.cpp:292
uint8_t read(Command cmd)
Definition: NRF24L01P.cpp:46
Count retransmitted packets (bits 4).
Definition: NRF24L01P.hh:458
observe_tx_t(uint8_t value)
Definition: NRF24L01P.hh:476
OutputPin m_ce
Chip enable activity RX/TX select pin.
Definition: NRF24L01P.hh:620
Transmit address.
Definition: NRF24L01P.hh:308
Enable all auto ack on all data pipes.
Definition: NRF24L01P.hh:348
Mask interrupt caused by RX_DR.
Definition: NRF24L01P.hh:324
Enable the W_TX_PAYLOAD_NOACK command.
Definition: NRF24L01P.hh:537
RX/TX control (PRX/PTX).
Definition: NRF24L01P.hh:330
void standby()
Definition: NRF24L01P.cpp:161
uint8_t read(Register reg)
Definition: NRF24L01P.hh:547
Count lost packets (bits 4).
Definition: NRF24L01P.hh:457
Default auto retransmit count (15)
Definition: NRF24L01P.hh:383
TX FIFO empty flag.
Definition: NRF24L01P.hh:488
virtual void output_power_level(int8_t dBm)
Definition: NRF24L01P.cpp:328
Enable dynamic payload length on all pipes.
Definition: NRF24L01P.hh:528
Write TX payload.
Definition: NRF24L01P.hh:241
Air data bitrate (2 Mbps).
Definition: NRF24L01P.hh:393
Continuous carrier transmit.
Definition: NRF24L01P.hh:390
void write(Register reg, const void *buf, size_t size)
Definition: NRF24L01P.hh:584
Number of bytes in RX payload in data pipe 0.
Definition: NRF24L01P.hh:309
RX/TX address field width (bits 2).
Definition: NRF24L01P.hh:368
void transmit_mode(uint8_t dest)
Definition: NRF24L01P.cpp:142
No operation, return status.
Definition: NRF24L01P.hh:249
Flush RX FIFO.
Definition: NRF24L01P.hh:243
Enable rx addresses.
Definition: NRF24L01P.hh:294
Received power detector.
Definition: NRF24L01P.hh:301
RX FIFO full flag.
Definition: NRF24L01P.hh:489
uint8_t as_byte
Byte representation of status.
Definition: NRF24L01P.hh:433
Data ready RX FIFO interrupt.
Definition: NRF24L01P.hh:420
uint8_t rx_dr
Data ready RX FIFO interrupt.
Definition: NRF24L01P.hh:439