lt8920.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef __LT8920_H
  2. #define __LT8920_H
  3. #define REGISTER_READ 0b10000000 //bin
  4. #define REGISTER_WRITE 0b00000000 //bin
  5. #define REGISTER_MASK 0b01111111 //bin
  6. #define R_CHANNEL 7
  7. #define CHANNEL_RX_BIT 7
  8. #define CHANNEL_TX_BIT 8
  9. #define CHANNEL_MASK 0b01111111 //bin
  10. #define DEFAULT_CHANNEL 0x31
  11. #define R_CURRENT 9
  12. #define CURRENT_POWER_SHIFT 12
  13. #define CURRENT_POWER_MASK 0b1111000000000000
  14. #define CURRENT_GAIN_SHIFT 7
  15. #define CURRENT_GAIN_MASK 0b0000011110000000
  16. #define R_DATARATE 44
  17. #define DATARATE_MASK 0xFF00
  18. #define DATARATE_1MBPS 0x0101
  19. #define DATARATE_250KBPS 0x0401
  20. #define DATARATE_125KBPS 0x0801
  21. #define DATARATE_62KBPS 0x1001
  22. #define R_SYNCWORD1 36
  23. #define R_SYNCWORD2 37
  24. #define R_SYNCWORD3 38
  25. #define R_SYNCWORD4 39
  26. #define R_PACKETCONFIG 41
  27. #define PACKETCONFIG_CRC_ON 0x8000
  28. #define PACKETCONFIG_SCRAMBLE_ON 0x4000
  29. #define PACKETCONFIG_PACK_LEN_ENABLE 0x2000
  30. #define PACKETCONFIG_FW_TERM_TX 0x1000
  31. #define PACKETCONFIG_AUTO_ACK 0x0800
  32. #define PACKETCONFIG_PKT_FIFO_POLARITY 0x0400
  33. #define R_STATUS 48
  34. #define STATUS_CRC_BIT 15
  35. #define R_FIFO 50
  36. #define R_FIFO_CONTROL 52
  37. class LT8920
  38. {
  39. public:
  40. /** Data Data */
  41. enum DataRate
  42. {
  43. LT8920_1MBPS, /** default transmit rate */
  44. LT8920_250KBPS, /** 250 Kpbs, only on lt8910 */
  45. LT8920_125KBPS, /** 125 Kbps, only on lt8910 */
  46. LT8920_62KBPS /** 62 Kbps, only on lt8910 */
  47. };
  48. private:
  49. uint8_t _pin_chipselect;
  50. uint8_t _pin_pktflag;
  51. uint8_t _pin_reset;
  52. uint8_t _channel;
  53. public:
  54. /** Construct a new instance
  55. * @param cs Chip Select, this is the SLAVE SELECT pin. Please note that it is a different location than the NRF24L01+ SS pin.
  56. * @param pkt PKT_flag input signal pin. Comparable to the NRF24L01+ IRQ pin.
  57. * @param rst RESET pin. Use 0 to disable.
  58. */
  59. LT8920(void);
  60. /** Configures the module for initial use */
  61. void begin();
  62. /**
  63. * @param channel has significant 7 bits
  64. */
  65. void setChannel(uint8_t channel);
  66. /** Retrieve the current channel */
  67. uint8_t getChannel();
  68. /** Set power and gain
  69. * @param power 0-0xf
  70. * @param gain 0-0xf
  71. */
  72. void setCurrentControl(uint8_t power, uint8_t gain);
  73. /** Sets the data rate
  74. * @param rate the transmission/reception speed
  75. * @returns true when the data rate was succesfully changed.
  76. */
  77. bool setDataRate(DataRate rate);
  78. /** Returns the data rate
  79. * @returns the active data rate.
  80. */
  81. DataRate getDataRate();
  82. /** Read the value of a register
  83. * @param reg
  84. */
  85. uint16_t readRegister(uint8_t reg);
  86. /** Writes to a register
  87. * @param reg The register to write to
  88. * @param data 16bits of data, MSB first */
  89. uint8_t writeRegister(uint8_t reg, uint16_t data);
  90. /** Writes to a register, one byte at a time.
  91. * This is a convenience function, because the LT8920 uses 16 bits registers with all 16 bits written at once.
  92. * @param reg The register to write to
  93. * @param high bits 15..8
  94. * @param low bits 7..0 */
  95. uint8_t writeRegister2(uint8_t reg, uint8_t high, uint8_t low);
  96. void dump_register(uint8_t reg);
  97. /** Put the LT8920 module to sleep mode.
  98. * In contrast to POWER DOWN, this mode will keep the register values
  99. * Any SPI call will awaken the module automatically */
  100. void sleep();
  101. /** Dumps debug information on the selected port
  102. * @param stream the output stream to use as output, eg. `whatsUp(&Serial);`
  103. */
  104. //void whatsUp(Stream &stream);
  105. void whatsUp(void);
  106. /** Signals a packet ready in the FIFO queue */
  107. bool available();
  108. /** Read a packet into the buffer
  109. * @param buffer a pointer to a buffer large enough to hold the packet
  110. * @param maxBuffer the maximum size of the buffer. Any bytes left over in the buffer will be dropped.
  111. * @returns the size of the packet that was read, -1 for CRC error
  112. */
  113. int read(uint8_t *buffer, size_t maxBuffer);
  114. /** Switch the module to RX mode */
  115. void startListening();
  116. /** Sets the internal clock divider
  117. * @param clock
  118. */
  119. void setClock(uint8_t clock);
  120. /** Sends a packet
  121. * This call blocks until the packet was sent and the Tx Buffer has been completed
  122. * @param data
  123. * @param packetSize
  124. * @returns true when the packet was sent, or false when the packetSize was invalid.
  125. */
  126. bool sendPacket(uint8_t *data, size_t packetSize);
  127. /** Set Syncword
  128. * @param syncWord 64 bits of sync word settings.
  129. * @see setSyncwordLength
  130. */
  131. void setSyncWord(uint64_t syncWord);
  132. /** Sets the length of the sync word used
  133. * @param length:
  134. * 11 = 64bits, 10 = 48 bits, 01 = 32 bits, 00 = 16 bits
  135. * @see Check the datasheet for which bits are actually used.
  136. */
  137. void setSyncWordLength(uint8_t length);
  138. /** Scan the signal strength for one or more channels
  139. * @param buffer points to a buffer to store the signal strengths, at least num_channels
  140. * @param start_channel starting channel to scan, where Frequency = 2402 + channel
  141. * @param num_channels number of channels to scan in this batch, for example scanning 4 channels
  142. * with start 2480 will scan 2480, 2481, 2482, 2483
  143. */
  144. void scanRSSI(uint16_t *buffer, uint8_t start_channel, uint8_t num_channels);
  145. /** retrieve the analog signal strength for the current channel */
  146. uint8_t getRSSI();
  147. };
  148. #define _BV(bit) (1 << (bit))
  149. #endif