nrf24l01.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef __FW_RNF24L01_H__
  15. #define __FW_RNF24L01_H__
  16. #include "fw_hal.h"
  17. #include "string.h"
  18. #define NRF_CSN P35
  19. #define NRF_MOSI P34
  20. #define NRF_MISO P33
  21. #define NRF_SCK P32
  22. #define NRF_IRQ P36
  23. #define NRF_CE P37
  24. /********** SPI(nRF24L01) commands ***********/
  25. //
  26. #define NRF24_CMD_R_REGISTER 0x00 // [000A AAAA] Register read
  27. #define NRF24_CMD_W_REGISTER 0x20 // [001A AAAA] Register write
  28. #define NRF24_CMD_R_RX_PAYLOAD 0x61 // Read RX payload
  29. #define NRF24_CMD_W_TX_PAYLOAD 0xA0 // Write TX payload
  30. #define NRF24_CMD_FLUSH_TX 0xE1 // Flush TX FIFO
  31. #define NRF24_CMD_FLUSH_RX 0xE2 // Flush RX FIFO
  32. #define NRF24_CMD_REUSE_TX_PL 0xE3 // Reuse TX payload
  33. #define NRF24_CMD_R_RX_PL_WID 0x60 // Read RX-payload width for the top R_RX_PAYLOAD in the RX FIFO.
  34. #define NRF24_CMD_W_ACK_PAYLOAD 0xA8 // [1010 1PPP] Write ACK Payload to be with ACK packet on PIPE PPP
  35. #define NRF24_CMD_W_TX_PAYLOAD_NOACK 0xB0 //Write TX payload and disable AUTOACK
  36. #define NRF24_CMD_NOP 0xFF // No operation (used for reading status register)
  37. #define NRF24_CMD_ACTIVATE 0x50 // (De)Activates R_RX_PL_WID, W_ACK_PAYLOAD, W_TX_PAYLOAD_NOACK features
  38. #define NRF24_CMD_LOCK_UNLOCK 0x50 // Lock/unlock exclusive features
  39. // SPI(nRF24L01) register address definitions
  40. #define NRF24_REG_CONFIG 0x00 // Configuration register
  41. #define NRF24_REG_EN_AA 0x01 // Enable "Auto acknowledgment"
  42. #define NRF24_REG_EN_RXADDR 0x02 // Enable RX addresses
  43. #define NRF24_REG_SETUP_AW 0x03 // Setup of address widths
  44. #define NRF24_REG_SETUP_RETR 0x04 // Setup of automatic re-transmit
  45. #define NRF24_REG_RF_CH 0x05 // RF channel
  46. #define NRF24_REG_RF_SETUP 0x06 // RF setup
  47. #define NRF24_REG_STATUS 0x07 // Status register
  48. #define NRF24_REG_OBSERVE_TX 0x08 // Transmit observe register
  49. #define NRF24_REG_RPD 0x09 // Received power detector
  50. #define NRF24_REG_RX_ADDR_P0 0x0A // Receive address data pipe 0
  51. #define NRF24_REG_RX_ADDR_P1 0x0B // Receive address data pipe 1
  52. #define NRF24_REG_RX_ADDR_P2 0x0C // Receive address data pipe 2
  53. #define NRF24_REG_RX_ADDR_P3 0x0D // Receive address data pipe 3
  54. #define NRF24_REG_RX_ADDR_P4 0x0E // Receive address data pipe 4
  55. #define NRF24_REG_RX_ADDR_P5 0x0F // Receive address data pipe 5
  56. #define NRF24_REG_TX_ADDR 0x10 // Transmit address
  57. #define NRF24_REG_RX_PW_P0 0x11 // Number of bytes in RX payload in data pipe 0
  58. #define NRF24_REG_RX_PW_P1 0x12 // Number of bytes in RX payload in data pipe 1
  59. #define NRF24_REG_RX_PW_P2 0x13 // Number of bytes in RX payload in data pipe 2
  60. #define NRF24_REG_RX_PW_P3 0x14 // Number of bytes in RX payload in data pipe 3
  61. #define NRF24_REG_RX_PW_P4 0x15 // Number of bytes in RX payload in data pipe 4
  62. #define NRF24_REG_RX_PW_P5 0x16 // Number of bytes in RX payload in data pipe 5
  63. #define NRF24_REG_FIFO_STATUS 0x17 // FIFO status register
  64. #define NRF24_REG_DYNPD 0x1C // Enable dynamic payload length
  65. #define NRF24_REG_FEATURE 0x1D // Feature register
  66. // Register bits definitions
  67. #define NRF24_CONFIG_PRIM_RX 0x01 // PRIM_RX bit in CONFIG register
  68. #define NRF24_CONFIG_PWR_UP 0x02 // PWR_UP bit in CONFIG register
  69. #define NRF24_FEATURE_EN_DYN_ACK 0x01 // EN_DYN_ACK bit in FEATURE register
  70. #define NRF24_FEATURE_EN_ACK_PAY 0x02 // EN_ACK_PAY bit in FEATURE register
  71. #define NRF24_FEATURE_EN_DPL 0x04 // EN_DPL bit in FEATURE register
  72. #define NRF24_FLAG_RX_DREADY 0x40 // RX_DR bit (data ready RX FIFO interrupt)
  73. #define NRF24_FLAG_TX_DSENT 0x20 // TX_DS bit (data sent TX FIFO interrupt)
  74. #define NRF24_FLAG_MAX_RT 0x10 // MAX_RT bit (maximum number of TX re-transmits interrupt)
  75. #define NRF24_FLAG_TX_FULL 0x01 // 1:TX FIFO full
  76. // Register masks definitions
  77. #define NRF24_MASK_REG_MAP 0x1F // Mask bits[4:0] for CMD_RREG and CMD_WREG commands
  78. #define NRF24_MASK_CRC 0x0C // Mask for CRC bits [3:2] in CONFIG register
  79. #define NRF24_MASK_STATUS_IRQ 0x70 // Mask for all IRQ bits in STATUS register
  80. #define NRF24_MASK_RF_PWR 0x06 // Mask RF_PWR[2:1] bits in RF_SETUP register
  81. #define NRF24_MASK_RX_P_NO 0x0E // Mask RX_P_NO[3:1] bits in STATUS register
  82. #define NRF24_MASK_DATARATE 0x28 // Mask RD_DR_[5,3] bits in RF_SETUP register
  83. #define NRF24_MASK_EN_RX 0x3F // Mask ERX_P[5:0] bits in EN_RXADDR register
  84. #define NRF24_MASK_RX_PW 0x3F // Mask [5:0] bits in RX_PW_Px register
  85. #define NRF24_MASK_RETR_ARD 0xF0 // Mask for ARD[7:4] bits in SETUP_RETR register
  86. #define NRF24_MASK_RETR_ARC 0x0F // Mask for ARC[3:0] bits in SETUP_RETR register
  87. #define NRF24_MASK_RXFIFO 0x03 // Mask for RX FIFO status bits [1:0] in FIFO_STATUS register
  88. #define NRF24_MASK_TXFIFO 0x30 // Mask for TX FIFO status bits [5:4] in FIFO_STATUS register
  89. #define NRF24_MASK_PLOS_CNT 0xF0 // Mask for PLOS_CNT[7:4] bits in OBSERVE_TX register
  90. #define NRF24_MASK_ARC_CNT 0x0F // Mask for ARC_CNT[3:0] bits in OBSERVE_TX register
  91. #define NRF24_ADDR_WIDTH 5 // RX/TX address width
  92. #define NRF24_PLOAD_WIDTH 32 // Payload width
  93. #define NRF24_TEST_ADDR "nRF24"
  94. typedef enum
  95. {
  96. NRF24_MODE_RX = 0x00,
  97. NRF24_MODE_TX = 0x01
  98. } NRF24_MODE;
  99. typedef enum
  100. {
  101. NRF24_SCEN_RX = 0x00,
  102. NRF24_SCEN_TX = 0x01,
  103. NRF24_SCEN_HALF_DUPLEX = 0x02
  104. } NRF24_SCEN;
  105. void NRF24L01_WriteReg(uint8_t reg, uint8_t value);
  106. uint8_t NRF24L01_ReadReg(uint8_t reg);
  107. void NRF24L01_ReadToBuf(uint8_t reg, uint8_t len);
  108. void NRF24L01_WriteFromBuf(uint8_t reg, const uint8_t *pBuf, uint8_t len);
  109. void NRF24L01_PrintBuf(void);
  110. /**
  111. * Flush the RX FIFO
  112. */
  113. void NRF24L01_FlushRX(void);
  114. /**
  115. * Flush the TX FIFO
  116. */
  117. void NRF24L01_FlushTX(void);
  118. void NRF24L01_CheckFlag(void);
  119. // uint8_t NRF24L01_RxAvailable(uint8_t* pipe_num);
  120. uint8_t NRF24L01_HandelIrqFlag(void);
  121. void NRF24L01_Tx(uint8_t *pBuf);
  122. void NRF24L01_StartFastWrite(const void* pBuf);
  123. uint8_t NRF24L01_WriteFast(const void* pBuf);
  124. void NRF24L01_ResetTX(void);
  125. uint8_t NRF24L01_Check(void);
  126. void NRF24L01_Init(NRF24_MODE mode);
  127. #endif