nrf24l01.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @author Tilen MAJERLE
  3. * @email tilen@majerle.eu
  4. * @website http://stm32f4-discovery.net
  5. * @link http://stm32f4-discovery.net/2015/09/hal-library-25-nrf24l01-for-stm32fxxx/
  6. * @version v1.0
  7. * @ide Keil uVision
  8. * @license MIT
  9. * @brief Library template
  10. *
  11. \verbatim
  12. ----------------------------------------------------------------------
  13. Copyright (c) 2016 Tilen MAJERLE
  14. Permission is hereby granted, free of charge, to any person
  15. obtaining a copy of this software and associated documentation
  16. files (the "Software"), to deal in the Software without restriction,
  17. including without limitation the rights to use, copy, modify, merge,
  18. publish, distribute, sublicense, and/or sell copies of the Software,
  19. and to permit persons to whom the Software is furnished to do so,
  20. subject to the following conditions:
  21. The above copyright notice and this permission notice shall be
  22. included in all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  25. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  26. AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  27. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  28. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. OTHER DEALINGS IN THE SOFTWARE.
  31. ----------------------------------------------------------------------
  32. \endverbatim
  33. */
  34. /**
  35. * @defgroup TM_NRF24L01P
  36. * @brief nRF24L01+ library for STM32xxx devices - http://stm32f4-discovery.net/2015/09/hal-library-25-nrf24l01-for-stm32fxxx/
  37. * @{
  38. *
  39. * This library allows you to work with nRF24L01+ modules.
  40. *
  41. * You can send and receive data from nRF24L01+ modules.
  42. *
  43. * \par Default pinout
  44. *
  45. \verbatim
  46. NRF24L01+ DESCRIPTION
  47. GND Ground
  48. VCC 3.3V
  49. CE RF activated pin
  50. CSN Chip select pin for SPI
  51. SCK SCK pin for SPI
  52. MOSI MOSI pin for SPI
  53. MISO MISO pin for SPI
  54. IRQ Not used. Interrupt pin. Goes low when active. Pin functionality is active, but not used in library
  55. \endverbatim
  56. *
  57. * IRQ pin is not used in this library, but its functionality is enabled by this software.
  58. *
  59. * You can still set any pin on Fxxx to be an external interrupt and handle interrupts from nRF24L01+ module.
  60. *
  61. * The easiest way to that is to use @ref TM_EXTI library and attach interrupt functionality to this pin
  62. *
  63. * \par Custom pinout
  64. *
  65. * Add lines below in your defines.h file if you want to change default pinout:
  66. */
  67. #ifndef _NRF_24L01_H_
  68. #define _NRF_24L01_H_
  69. #if defined(STM32F401xC) || defined(STM32F401xE)
  70. #include "stm32f4xx_hal.h"
  71. #endif
  72. #if defined (STM32L432xx)
  73. #include "stm32l4xx_hal.h"
  74. #endif
  75. #include "nrf24l01_defines.h"
  76. #include "nrfManager.h"
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. class Nrf24L01 {
  81. protected:
  82. NRF24L01_Conig_t nrf_config;
  83. NrfManager *_nrfManager;
  84. GPIO_TypeDef *_cs_port;
  85. GPIO_TypeDef *_ce_port;
  86. uint16_t _ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */
  87. uint16_t _cs_pin; /**< SPI Chip select */
  88. uint32_t spi_speed; /**< SPI Bus Speed */
  89. uint8_t status; /** The status byte returned from every SPI transaction */
  90. uint8_t _payload_size; /**< Fixed size of payloads */
  91. uint8_t _channel;
  92. public:
  93. Nrf24L01(uint8_t channel, NrfManager *nrf_manager, GPIO_TypeDef *port_cs, uint16_t pin_cs, GPIO_TypeDef *port_ce, uint16_t pin_ce);
  94. void SetMyAddress(uint8_t* adr);
  95. void SetTxAddress(uint8_t* adr);
  96. uint8_t GetRetransmissionsCount(void);
  97. void PowerUpTx(void);
  98. void PowerUpRx(void);
  99. void PowerDown(void);
  100. Transmit_Status_t GetTransmissionStatus(void);
  101. void Transmit(uint8_t *data);
  102. uint8_t DataReady(void);
  103. void GetData(uint8_t *data);
  104. void SetChannel(uint8_t channel);
  105. void SetRF(NRF_DataRate_t DataRate, NRF_OutputPower_t OutPwr);
  106. uint8_t GetStatus(void);
  107. uint8_t Read_Interrupts(NRF_IRQ_t* IRQ);
  108. void Clear_Interrupts(void);
  109. private:
  110. uint8_t _buffer[8];
  111. uint8_t init();
  112. void initPins(void);
  113. void softwareReset(void);
  114. void writeRegister(uint8_t reg, uint8_t value);
  115. void writeBit(uint8_t reg, uint8_t bit, uint8_t value);
  116. uint8_t readBit(uint8_t reg, uint8_t bit);
  117. uint8_t readRegister(uint8_t reg);
  118. void readRegisterMulti(uint8_t reg, uint8_t* data, uint8_t count);
  119. void writeRegisterMulti(uint8_t reg, uint8_t *data, uint8_t count);
  120. uint8_t rxFifoEmpty(void);
  121. };
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif /* _NRF_24L01_H_ */