PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

243 lines
10KB

  1. // RH_NRF51.h
  2. // Author: Mike McCauley
  3. // Copyright (C) 2015 Mike McCauley
  4. // $Id: RH_NRF51.h,v 1.3 2015/08/14 21:20:12 mikem Exp $
  5. //
  6. #ifndef RH_NRF51_h
  7. #define RH_NRF51_h
  8. #include <RHGenericDriver.h>
  9. // This is the maximum number of bytes that can be carried by the nRF51.
  10. // We use some for headers, keeping fewer for RadioHead messages
  11. #define RH_NRF51_MAX_PAYLOAD_LEN 254
  12. // The length of the headers we add.
  13. // The headers are inside the nRF51 payload
  14. #define RH_NRF51_HEADER_LEN 4
  15. // This is the maximum RadioHead user message length that can be supported by this library. Limited by
  16. // the supported message lengths in the nRF51
  17. #define RH_NRF51_MAX_MESSAGE_LEN (RH_NRF51_MAX_PAYLOAD_LEN-RH_NRF51_HEADER_LEN)
  18. /////////////////////////////////////////////////////////////////////
  19. /// \class RH_NRF51 RH_NRF51.h <RH_NRF51.h>
  20. /// \brief Send and receive addressed datagrams by nRF51 compatible transceivers.
  21. ///
  22. /// Supported transceivers include:
  23. /// - Nordic nRF51 based 2.4GHz radio modules, such as nRF51822
  24. /// and other compatible chips, such as used in RedBearLabs devices like:
  25. /// http://store.redbearlab.com/products/redbearlab-nrf51822
  26. /// http://store.redbearlab.com/products/blenano
  27. ///
  28. /// This base class provides basic functions for sending and receiving unaddressed, unreliable datagrams
  29. /// of arbitrary length to 254 octets per packet. Use one of the Manager classes to get addressing and
  30. /// acknowledgement reliability, routing, meshes etc.
  31. ///
  32. /// The nRF51822 (https://www.nordicsemi.com/eng/Products/Bluetooth-Smart-Bluetooth-low-energy/nRF51822)
  33. /// is a complete SoC (system on a chip) with ARM microprocessor and 2.4 GHz radio, which supports a range of channels
  34. /// and transmission bit rates. Chip antenna is on-board.
  35. ///
  36. /// This library provides functions for sending and receiving messages of up to 254 octets on any
  37. /// frequency supported by the nRF51822, at a selected data rate.
  38. ///
  39. /// The nRF51 transceiver is configured to use Enhanced Shockburst with no acknowledgement and no retransmits.
  40. /// TXADDRESS and RXADDRESSES:RXADDR0 (ie pipe 0) are the logical address used. The on-air network address
  41. /// is set in BASE0 and PREFIX0. SHORTS is used to automatically transition the radio between Ready, Start and Disable.
  42. /// No interrupts are used.
  43. ///
  44. /// Naturally, for any 2 radios to communicate that must be configured to use the same frequency and
  45. /// data rate, and with identical network addresses.
  46. ///
  47. /// Example programs are included to show the main modes of use.
  48. ///
  49. /// \par Packet Format
  50. ///
  51. /// All messages sent and received by this class conform to this packet format. It is NOT compatible
  52. /// with the one used by RH_NRF24 and the nRF24L01 product specification, mainly because the nRF24 only suports
  53. /// 6 bits of message length.
  54. ///
  55. /// - 1 octets PREAMBLE
  56. /// - 3 to 5 octets NETWORK ADDRESS
  57. /// - 8 bits PAYLOAD LENGTH
  58. /// - 0 to 254 octets PAYLOAD, consisting of:
  59. /// - 1 octet TO header
  60. /// - 1 octet FROM header
  61. /// - 1 octet ID header
  62. /// - 1 octet FLAGS header
  63. /// - 0 to 250 octets of user message
  64. /// - 2 octets CRC (Algorithm x^16+x^12^x^5+1 with initial value 0xFFFF).
  65. ///
  66. /// \par Example programs
  67. ///
  68. /// Several example programs are provided.
  69. ///
  70. /// The sample programs are designed to be built using Arduino 1.6.4 or later using the procedures outlined
  71. /// in http://redbearlab.com/getting-started-nrf51822/
  72. ///
  73. /// \par Radio Performance
  74. ///
  75. /// At DataRate2Mbps (2Mb/s), payload length vs airtime:
  76. /// 0 bytes takes about 70us, 128 bytes takes 520us, 254 bytes take 1020us.
  77. /// You can extrapolate linearly to slower data rates.
  78. ///
  79. /// The RF powers claimed by the chip manufacturer have not been independently verified here.
  80. ///
  81. /// \par Memory
  82. ///
  83. /// The compiled client and server sketches are about 42k bytes on Arduino.
  84. /// The reliable client and server sketches compile to about 43k bytes on Arduino. Unfortunately the
  85. /// Arduino build environmnet does not drop unused clsses and code, so the resulting programs include
  86. /// all the unused classes ad code. This needs to be revisited.
  87. /// RAM requirements are minimal.
  88. ///
  89. class RH_NRF51 : public RHGenericDriver
  90. {
  91. public:
  92. /// \brief Defines convenient values for setting data rates in setRF()
  93. typedef enum
  94. {
  95. DataRate1Mbps = 0, ///< 1 Mbps
  96. DataRate2Mbps, ///< 2 Mbps
  97. DataRate250kbps ///< 250 kbps
  98. } DataRate;
  99. /// \brief Convenient values for setting transmitter power in setRF()
  100. typedef enum
  101. {
  102. // Add 20dBm for nRF24L01p with PA and LNA modules
  103. TransmitPower4dBm = 0, ///< 4 dBm
  104. TransmitPower0dBm, ///< 0 dBm
  105. TransmitPowerm4dBm, ///< -4 dBm
  106. TransmitPowerm8dBm, ///< -8 dBm
  107. TransmitPowerm12dBm, ///< -12 dBm
  108. TransmitPowerm16dBm, ///< -16 dBm
  109. TransmitPowerm20dBm, ///< -20 dBm
  110. TransmitPowerm30dBm, ///< -30 dBm
  111. } TransmitPower;
  112. /// Constructor.
  113. /// After constructing, you must call init() to initialise the interface
  114. /// and the radio module
  115. RH_NRF51();
  116. /// Initialises this instance and the radio module connected to it.
  117. /// The following steps are taken:
  118. /// - Start the processors High Frequency clock DC/DC converter and
  119. /// - Disable and reset the radio
  120. /// - Set the logical channel to 0 for transmit and receive (only pipe 0 is used)
  121. /// - Configure the CRC (2 octets, algorithm x^16+x^12^x^5+1 with initial value 0xffff)
  122. /// - Set the default network address of 0xE7E7E7E7E7
  123. /// - Set channel to 2
  124. /// - Set data rate to DataRate2Mbps
  125. /// - Set TX power to TransmitPower0dBm
  126. /// \return true if everything was successful
  127. bool init();
  128. /// Sets the transmit and receive channel number.
  129. /// The frequency used is (2400 + channel) MHz
  130. /// \return true on success
  131. bool setChannel(uint8_t channel);
  132. /// Sets the Network address.
  133. /// Only nodes with the same network address can communicate with each other. You
  134. /// can set different network addresses in different sets of nodes to isolate them from each other.
  135. /// Internally, this sets the nRF51 BASE0 and PREFIX0 to be the given network address.
  136. /// The first octet of the address is used for PREFIX0 and the rest is used for BASE0. BALEN is
  137. /// set to the approprtae base length.
  138. /// The default network address is 0xE7E7E7E7E7.
  139. /// \param[in] address The new network address. Must match the network address of any receiving node(s).
  140. /// \param[in] len Number of bytes of address to set (3 to 5).
  141. /// \return true on success, false if len is not in the range 3-5 inclusive.
  142. bool setNetworkAddress(uint8_t* address, uint8_t len);
  143. /// Sets the data rate and transmitter power to use.
  144. /// \param [in] data_rate The data rate to use for all packets transmitted and received. One of RH_NRF51::DataRate.
  145. /// \param [in] power Transmitter power. One of RH_NRF51::TransmitPower.
  146. /// \return true on success
  147. bool setRF(DataRate data_rate, TransmitPower power);
  148. /// Sets the radio in power down mode, with the configuration set to the
  149. /// last value from setOpMode().
  150. /// Sets chip enable to LOW.
  151. void setModeIdle();
  152. /// Sets the radio in RX mode.
  153. void setModeRx();
  154. /// Sets the radio in TX mode.
  155. void setModeTx();
  156. /// Sends data to the address set by setTransmitAddress()
  157. /// Sets the radio to TX mode.
  158. /// \param [in] data Data bytes to send.
  159. /// \param [in] len Number of data bytes to send
  160. /// \return true on success (which does not necessarily mean the receiver got the message, only that the message was
  161. /// successfully transmitted).
  162. bool send(const uint8_t* data, uint8_t len);
  163. /// Blocks until the current message (if any)
  164. /// has been transmitted
  165. /// \return true on success, false if the chip is not in transmit mode or other transmit failure
  166. virtual bool waitPacketSent();
  167. /// Indicates if the chip is in transmit mode and
  168. /// there is a packet currently being transmitted
  169. /// \return true if the chip is in transmit mode and there is a transmission in progress
  170. bool isSending();
  171. /// Prints the value of all NRF_RADIO registers.
  172. /// to the Serial device if RH_HAVE_SERIAL is defined for the current platform
  173. /// For debugging purposes only.
  174. /// Caution: there are 1024 of them (many reserved and set to 0).
  175. /// \return true on success
  176. bool printRegisters();
  177. /// Checks whether a received message is available.
  178. /// This can be called multiple times in a timeout loop
  179. /// \return true if a complete, valid message has been received and is able to be retrieved by
  180. /// recv()
  181. bool available();
  182. /// Turns the receiver on if it not already on.
  183. /// Once a message with CRC correct is received, the receiver will be returned to Idle mode.
  184. /// If there is a valid message available, copy it to buf and return true
  185. /// else return false.
  186. /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted).
  187. /// You should be sure to call this function frequently enough to not miss any messages
  188. /// It is recommended that you call it in your main loop.
  189. /// \param[in] buf Location to copy the received message
  190. /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied.
  191. /// \return true if a valid message was copied to buf
  192. bool recv(uint8_t* buf, uint8_t* len);
  193. /// The maximum message length supported by this driver
  194. /// \return The maximum message length supported by this driver
  195. uint8_t maxMessageLength();
  196. protected:
  197. /// Examine the receive buffer to determine whether the message is for this node
  198. void validateRxBuf();
  199. /// Clear our local receive buffer
  200. void clearRxBuf();
  201. private:
  202. /// The receiver/transmitter buffer
  203. /// First octet is the payload length, remainder is the payload
  204. uint8_t _buf[RH_NRF51_MAX_PAYLOAD_LEN+1];
  205. /// True when there is a valid message in the buffer
  206. bool _rxBufValid;
  207. };
  208. /// @example nrf51_client.pde
  209. /// @example nrf51_server.pde
  210. /// @example nrf51_reliable_datagram_client.pde
  211. /// @example nrf51_reliable_datagram_server.pde
  212. /// @example nrf51_audio_tx.pde
  213. /// @example nrf51_audio_rx.pde
  214. #endif