lt8920.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. LT8920_ERROR
  48. };
  49. private:
  50. uint8_t _pin_chipselect;
  51. uint8_t _pin_pktflag;
  52. uint8_t _pin_reset;
  53. uint8_t _channel;
  54. public:
  55. /** Construct a new instance
  56. * @param cs Chip Select, this is the SLAVE SELECT pin. Please note that it is a different location than the NRF24L01+ SS pin.
  57. * @param pkt PKT_flag input signal pin. Comparable to the NRF24L01+ IRQ pin.
  58. * @param rst RESET pin. Use 0 to disable.
  59. */
  60. LT8920(void);
  61. /** Configures the module for initial use */
  62. void begin();
  63. /**
  64. * @param channel has significant 7 bits
  65. */
  66. void setChannel(uint8_t channel);
  67. /** Retrieve the current channel */
  68. uint8_t getChannel();
  69. /** Set power and gain
  70. * @param power 0-0xf
  71. * @param gain 0-0xf
  72. */
  73. void setCurrentControl(uint8_t power, uint8_t gain);
  74. /** Sets the data rate
  75. * @param rate the transmission/reception speed
  76. * @returns true when the data rate was succesfully changed.
  77. */
  78. bool setDataRate(DataRate rate);
  79. /** Returns the data rate
  80. * @returns the active data rate.
  81. */
  82. DataRate getDataRate();
  83. /** Read the value of a register
  84. * @param reg
  85. */
  86. uint16_t readRegister(uint8_t reg);
  87. /** Writes to a register
  88. * @param reg The register to write to
  89. * @param data 16bits of data, MSB first */
  90. uint8_t writeRegister(uint8_t reg, uint16_t data);
  91. /** Writes to a register, one byte at a time.
  92. * This is a convenience function, because the LT8920 uses 16 bits registers with all 16 bits written at once.
  93. * @param reg The register to write to
  94. * @param high bits 15..8
  95. * @param low bits 7..0 */
  96. uint8_t writeRegister2(uint8_t reg, uint8_t high, uint8_t low);
  97. void dump_register(uint8_t reg);
  98. /** Put the LT8920 module to sleep mode.
  99. * In contrast to POWER DOWN, this mode will keep the register values
  100. * Any SPI call will awaken the module automatically */
  101. void sleep();
  102. /** Dumps debug information on the selected port
  103. * @param stream the output stream to use as output, eg. `whatsUp(&Serial);`
  104. */
  105. //void whatsUp(Stream &stream);
  106. void whatsUp(void);
  107. /** Signals a packet ready in the FIFO queue */
  108. bool available();
  109. /** Read a packet into the buffer
  110. * @param buffer a pointer to a buffer large enough to hold the packet
  111. * @param maxBuffer the maximum size of the buffer. Any bytes left over in the buffer will be dropped.
  112. * @returns the size of the packet that was read, -1 for CRC error
  113. */
  114. int read(uint8_t *buffer, size_t maxBuffer);
  115. /** Switch the module to RX mode */
  116. void startListening();
  117. /** Sets the internal clock divider
  118. * @param clock
  119. */
  120. void setClock(uint8_t clock);
  121. /** Sends a packet
  122. * This call blocks until the packet was sent and the Tx Buffer has been completed
  123. * @param data
  124. * @param packetSize
  125. * @returns true when the packet was sent, or false when the packetSize was invalid.
  126. */
  127. bool sendPacket(uint8_t *data, size_t packetSize);
  128. /** Set Syncword
  129. * @param syncWord 64 bits of sync word settings.
  130. * @see setSyncwordLength
  131. */
  132. void setSyncWord(uint64_t syncWord);
  133. /** Sets the length of the sync word used
  134. * @param length:
  135. * 11 = 64bits, 10 = 48 bits, 01 = 32 bits, 00 = 16 bits
  136. * @see Check the datasheet for which bits are actually used.
  137. */
  138. void setSyncWordLength(uint8_t length);
  139. /** Scan the signal strength for one or more channels
  140. * @param buffer points to a buffer to store the signal strengths, at least num_channels
  141. * @param start_channel starting channel to scan, where Frequency = 2402 + channel
  142. * @param num_channels number of channels to scan in this batch, for example scanning 4 channels
  143. * with start 2480 will scan 2480, 2481, 2482, 2483
  144. */
  145. void scanRSSI(uint16_t *buffer, uint8_t start_channel, uint8_t num_channels);
  146. /** retrieve the analog signal strength for the current channel */
  147. uint8_t getRSSI();
  148. };
  149. #define _BV(bit) (1 << (bit))
  150. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  151. #endif