|
@@ -0,0 +1,380 @@
|
|
|
+#include "stm32l0xx_hal.h"
|
|
|
+#include "lt8920.h"
|
|
|
+#include "lt8920_trs.h"
|
|
|
+#include <stdio.h>
|
|
|
+
|
|
|
+
|
|
|
+void LT8920::dump_register(uint8_t reg)
|
|
|
+{
|
|
|
+ uint16_t r = readRegister(reg);
|
|
|
+ printf("reg: %u, value: %X\r\n", reg, r);
|
|
|
+}
|
|
|
+
|
|
|
+LT8920::LT8920(void)
|
|
|
+{
|
|
|
+ _channel = DEFAULT_CHANNEL;
|
|
|
+
|
|
|
+ LT8920_CS_HIGH;
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::begin()
|
|
|
+{
|
|
|
+ LT8920_RESET_LOW;
|
|
|
+ HAL_Delay(200);
|
|
|
+ LT8920_RESET_HIGH;
|
|
|
+ HAL_Delay(200);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ writeRegister(0, 0x6fe0);
|
|
|
+ writeRegister(1, 0x5681);
|
|
|
+ writeRegister(2, 0x6617);
|
|
|
+ writeRegister(4, 0x9cc9);
|
|
|
+ writeRegister(5, 0x6637);
|
|
|
+ writeRegister(8, 0x6c90);
|
|
|
+
|
|
|
+ setCurrentControl(4, 0);
|
|
|
+
|
|
|
+ writeRegister(10, 0x7ffd);
|
|
|
+ writeRegister(11, 0x0000);
|
|
|
+ writeRegister(12, 0x0000);
|
|
|
+ writeRegister(13, 0x48bd);
|
|
|
+
|
|
|
+ writeRegister(22, 0x00ff);
|
|
|
+ writeRegister(23, 0x8005);
|
|
|
+ writeRegister(24, 0x0067);
|
|
|
+ writeRegister(25, 0x1659);
|
|
|
+ writeRegister(26, 0x19e0);
|
|
|
+ writeRegister(27, 0x1300);
|
|
|
+ writeRegister(28, 0x1800);
|
|
|
+
|
|
|
+
|
|
|
+ writeRegister(32, 0x5000);
|
|
|
+
|
|
|
+
|
|
|
+ writeRegister(33, 0x3fc7);
|
|
|
+ writeRegister(34, 0x2000);
|
|
|
+ writeRegister(35, 0x0300);
|
|
|
+ setSyncWord(0x03805a5a03800380);
|
|
|
+
|
|
|
+ writeRegister(40, 0x4401);
|
|
|
+ writeRegister(R_PACKETCONFIG, PACKETCONFIG_CRC_ON |
|
|
|
+ PACKETCONFIG_PACK_LEN_ENABLE | PACKETCONFIG_FW_TERM_TX);
|
|
|
+
|
|
|
+ writeRegister(42, 0xfdb0);
|
|
|
+ writeRegister(43, 0x000f);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ writeRegister(R_FIFO, 0x0000);
|
|
|
+
|
|
|
+ writeRegister(R_FIFO_CONTROL, 0x8080);
|
|
|
+
|
|
|
+ HAL_Delay(200);
|
|
|
+ writeRegister(R_CHANNEL, _BV(CHANNEL_TX_BIT));
|
|
|
+ HAL_Delay(2);
|
|
|
+ writeRegister(R_CHANNEL, _channel);
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::setChannel(uint8_t channel)
|
|
|
+{
|
|
|
+ _channel = channel;
|
|
|
+ writeRegister(R_CHANNEL, (_channel & CHANNEL_MASK));
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t LT8920::getChannel()
|
|
|
+{
|
|
|
+ return _channel;
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::setCurrentControl(uint8_t power, uint8_t gain)
|
|
|
+{
|
|
|
+ writeRegister(R_CURRENT,
|
|
|
+ ((power << CURRENT_POWER_SHIFT) & CURRENT_POWER_MASK) |
|
|
|
+ ((gain << CURRENT_GAIN_SHIFT) & CURRENT_GAIN_MASK));
|
|
|
+}
|
|
|
+
|
|
|
+bool LT8920::setDataRate(DataRate rate)
|
|
|
+{
|
|
|
+ uint16_t newValue;
|
|
|
+
|
|
|
+ switch (rate)
|
|
|
+ {
|
|
|
+ case LT8920_1MBPS:
|
|
|
+ newValue = DATARATE_1MBPS;
|
|
|
+ break;
|
|
|
+ case LT8920_250KBPS:
|
|
|
+ newValue = DATARATE_250KBPS;
|
|
|
+ break;
|
|
|
+ case LT8920_125KBPS:
|
|
|
+ newValue = DATARATE_125KBPS;
|
|
|
+ break;
|
|
|
+ case LT8920_62KBPS:
|
|
|
+ newValue = DATARATE_62KBPS;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ writeRegister(R_DATARATE, newValue);
|
|
|
+ return ( (readRegister(R_DATARATE) & DATARATE_MASK) == (newValue & DATARATE_MASK));
|
|
|
+}
|
|
|
+
|
|
|
+LT8920::DataRate LT8920::getDataRate()
|
|
|
+{
|
|
|
+ uint16_t value = readRegister(R_DATARATE) & DATARATE_MASK;
|
|
|
+ switch (value)
|
|
|
+ {
|
|
|
+ case DATARATE_1MBPS:
|
|
|
+ return LT8920_1MBPS;
|
|
|
+ case DATARATE_250KBPS:
|
|
|
+ return LT8920_250KBPS;
|
|
|
+ case DATARATE_125KBPS:
|
|
|
+ return LT8920_125KBPS;
|
|
|
+ case DATARATE_62KBPS:
|
|
|
+ return LT8920_62KBPS;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+uint16_t LT8920::readRegister(uint8_t reg)
|
|
|
+{
|
|
|
+ LT8920_CS_LOW;
|
|
|
+ SPI.transfer(REGISTER_READ | (REGISTER_MASK & reg));
|
|
|
+ uint8_t high = SPI.transfer(0x00);
|
|
|
+ uint8_t low = SPI.transfer(0x00);
|
|
|
+ LT8920_CS_HIGH;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return (high << 8 | low);
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t LT8920::writeRegister(uint8_t reg, uint16_t data)
|
|
|
+{
|
|
|
+ uint8_t high = data >> 8;
|
|
|
+ uint8_t low = data & 0xFF;
|
|
|
+
|
|
|
+ return writeRegister2(reg, high, low);
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t LT8920::writeRegister2(uint8_t reg, uint8_t high, uint8_t low)
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ LT8920_CS_LOW;
|
|
|
+ uint8_t result = SPI.transfer(REGISTER_WRITE | (REGISTER_MASK & reg));
|
|
|
+ SPI.transfer(high);
|
|
|
+ SPI.transfer(low);
|
|
|
+
|
|
|
+ LT8920_CS_HIGH;
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::sleep()
|
|
|
+{
|
|
|
+
|
|
|
+ writeRegister(35, readRegister(35) | _BV(14));
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::whatsUp(void)
|
|
|
+{
|
|
|
+ uint16_t mode = readRegister(R_CHANNEL);
|
|
|
+
|
|
|
+#if 0
|
|
|
+ print("\nTx_EN=");
|
|
|
+ println((mode & _BV(CHANNEL_TX_BIT)) != false);
|
|
|
+ print("Rx_EN=");
|
|
|
+ println((mode & _BV(CHANNEL_RX_BIT)) != false);
|
|
|
+ print("Channel=");
|
|
|
+ println(mode & CHANNEL_MASK);
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+ uint16_t state = readRegister(R_STATUS);
|
|
|
+
|
|
|
+ bool crc_error = state & _BV(15);
|
|
|
+ bool fec23_error = state & _BV(14);
|
|
|
+ uint8_t framer_st = (state & 0b0011111100000000) >> 8;
|
|
|
+ bool pkt_flag = state & _BV(6);
|
|
|
+ bool fifo_flag = state & _BV(5);
|
|
|
+
|
|
|
+#if 0
|
|
|
+ stream.print("CRC=");
|
|
|
+ stream.println(crc_error);
|
|
|
+ stream.print("FEC=");
|
|
|
+ stream.println(fec23_error);
|
|
|
+ stream.print("FRAMER_ST=");
|
|
|
+ stream.println(framer_st);
|
|
|
+ stream.print("PKT=");
|
|
|
+ stream.println(pkt_flag);
|
|
|
+ stream.print("FIFO=");
|
|
|
+ stream.println(fifo_flag);
|
|
|
+#endif
|
|
|
+
|
|
|
+ uint16_t fifo = readRegister(R_FIFO_CONTROL);
|
|
|
+#if 0
|
|
|
+ stream.print("FIFO_WR_PTR=");
|
|
|
+ stream.println((fifo >> 8) & 0b111111);
|
|
|
+ stream.print("FIFO_RD_PTR=");
|
|
|
+ stream.println(fifo & 0b111111);
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+bool LT8920::available()
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+ if (LT8920_GET_PKT != 0)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+int LT8920::read(uint8_t *buffer, size_t maxBuffer)
|
|
|
+{
|
|
|
+ uint16_t value = readRegister(R_STATUS);
|
|
|
+ if (bitRead(value, STATUS_CRC_BIT) == 0)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ uint16_t data = readRegister(R_FIFO);
|
|
|
+ uint8_t packetSize = data >> 8;
|
|
|
+ if(maxBuffer < packetSize+1)
|
|
|
+ {
|
|
|
+
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+
|
|
|
+ uint8_t pos;
|
|
|
+ buffer[pos++] = (data & 0xFF);
|
|
|
+ while (pos < packetSize)
|
|
|
+ {
|
|
|
+ data = readRegister(R_FIFO);
|
|
|
+ buffer[pos++] = data >> 8;
|
|
|
+ buffer[pos++] = data & 0xFF;
|
|
|
+ }
|
|
|
+
|
|
|
+ return packetSize;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::startListening()
|
|
|
+{
|
|
|
+ writeRegister(R_CHANNEL, _channel & CHANNEL_MASK);
|
|
|
+ HAL_Delay(3);
|
|
|
+ writeRegister(R_FIFO_CONTROL, 0x0080);
|
|
|
+ writeRegister(R_CHANNEL, (_channel & CHANNEL_MASK) | _BV(CHANNEL_RX_BIT));
|
|
|
+ HAL_Delay(5);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void LT8920::setClock(uint8_t clock)
|
|
|
+{
|
|
|
+
|
|
|
+ uint16_t val = readRegister(35);
|
|
|
+ val &= 0b1111111111110001;
|
|
|
+ val |= ((clock & 0x07) << 1);;
|
|
|
+
|
|
|
+ writeRegister(35, val);
|
|
|
+}
|
|
|
+
|
|
|
+bool LT8920::sendPacket(uint8_t *data, size_t packetSize)
|
|
|
+{
|
|
|
+ if (packetSize < 1 || packetSize > 255)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ writeRegister(R_CHANNEL, 0x0000);
|
|
|
+ writeRegister(R_FIFO_CONTROL, 0x8000);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ uint8_t pos = 0;
|
|
|
+ writeRegister2(R_FIFO, packetSize, data[pos++]);
|
|
|
+ while (pos < packetSize)
|
|
|
+ {
|
|
|
+ uint8_t msb = data[pos++];
|
|
|
+ uint8_t lsb = data[pos++];
|
|
|
+
|
|
|
+ writeRegister2(R_FIFO, msb, lsb);
|
|
|
+ }
|
|
|
+
|
|
|
+ writeRegister(R_CHANNEL, (_channel & CHANNEL_MASK) | _BV(CHANNEL_TX_BIT));
|
|
|
+
|
|
|
+
|
|
|
+ while (LT8920_GET_PKT == 0)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::setSyncWord(uint64_t syncWord)
|
|
|
+{
|
|
|
+ writeRegister(R_SYNCWORD1, syncWord);
|
|
|
+ writeRegister(R_SYNCWORD2, syncWord >> 16);
|
|
|
+ writeRegister(R_SYNCWORD3, syncWord >> 32);
|
|
|
+ writeRegister(R_SYNCWORD4, syncWord >> 48);
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::setSyncWordLength(uint8_t option)
|
|
|
+{
|
|
|
+ option &= 0x03;
|
|
|
+ option <<= 11;
|
|
|
+
|
|
|
+ writeRegister(32, (readRegister(32) & 0b0001100000000000) | option);
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t LT8920::getRSSI()
|
|
|
+{
|
|
|
+
|
|
|
+ uint16_t value = readRegister(6);
|
|
|
+
|
|
|
+ return (value >> 10);
|
|
|
+}
|
|
|
+
|
|
|
+void LT8920::scanRSSI(uint16_t *buffer, uint8_t start_channel, uint8_t num_channels)
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ writeRegister(R_FIFO_CONTROL, 0x8080);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ writeRegister(42, (readRegister(42) & 0b0000001111111111) | ((num_channels-1 & 0b111111) << 10));
|
|
|
+
|
|
|
+
|
|
|
+ writeRegister(43, (readRegister(43) & 0b0000000011111111) | ((start_channel & 0b1111111) << 8));
|
|
|
+ writeRegister(43, (readRegister(43) & 0b0111111111111111) | _BV(15));
|
|
|
+
|
|
|
+ while (LT8920_GET_PKT == 0)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ uint8_t pos = 0;
|
|
|
+ while(pos < num_channels)
|
|
|
+ {
|
|
|
+ uint16_t data = readRegister(R_FIFO);
|
|
|
+ buffer[pos++] = data >> 8;
|
|
|
+ }
|
|
|
+}
|