Line data Source code
1 : #include "LR2021.h"
2 :
3 : #include "../LR11x0/LR_common.h"
4 :
5 : #include <string.h>
6 : #include <math.h>
7 :
8 : #if !RADIOLIB_EXCLUDE_LR2021
9 :
10 0 : int16_t LR2021::setRfFrequency(uint32_t rfFreq) {
11 0 : return(this->setU32(RADIOLIB_LR2021_CMD_SET_RF_FREQUENCY, rfFreq));
12 : }
13 :
14 0 : int16_t LR2021::setRxPath(uint8_t rxPath, uint8_t rxBoost) {
15 0 : uint8_t buff[] = { (uint8_t)(rxPath & 0x01), (uint8_t)(rxBoost & 0x07) };
16 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_SET_RX_PATH, true, buff, sizeof(buff));
17 0 : if (state == RADIOLIB_ERR_NONE) {
18 0 : state = this->setDCDCworkaround();
19 : }
20 0 : return(state);
21 : }
22 :
23 0 : int16_t LR2021::getRssiInst(float* rssi) {
24 0 : uint8_t buff[2] = { 0 };
25 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_RSSI_INST, false, buff, sizeof(buff));
26 0 : if(rssi) {
27 0 : uint16_t raw = ((uint16_t)(buff[0]) << 1) | (uint16_t)((buff[1] >> 7) & 0x01);
28 0 : *rssi = (float)raw/-2.0f;
29 : }
30 0 : return(state);
31 : }
32 :
33 0 : int16_t LR2021::setRssiCalibration(uint8_t rxPath, const uint16_t gain[RADIOLIB_LR2021_GAIN_TABLE_LENGTH], const uint8_t noiseFloor[RADIOLIB_LR2021_GAIN_TABLE_LENGTH]) {
34 0 : uint8_t buff[1 + 3*RADIOLIB_LR2021_GAIN_TABLE_LENGTH] = { 0 };
35 0 : buff[0] = rxPath;
36 0 : for(uint8_t i = 0; i < RADIOLIB_LR2021_GAIN_TABLE_LENGTH; i++) {
37 0 : buff[1 + 3*i] = (uint8_t)((gain[i] & 0x300) >> 8);
38 0 : buff[2 + 3*i] = (uint8_t)(gain[i] & 0xFF);
39 0 : buff[3 + 3*i] = noiseFloor[i];
40 : }
41 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_RSSI_CALIBRATION, true, buff, sizeof(buff)));
42 : }
43 :
44 0 : int16_t LR2021::setTimestampSource(uint8_t index, uint8_t source) {
45 0 : uint8_t buff[] = { (uint8_t)(((index & 0x03) << 4) | (source & 0x0F)) };
46 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_TIMESTAMP_SOURCE, true, buff, sizeof(buff)));
47 : }
48 :
49 0 : int16_t LR2021::getTimestampValue(uint8_t index, uint32_t* timestamp) {
50 0 : uint8_t reqBuff[] = { (uint8_t)(index & 0x03) };
51 0 : uint8_t rplBuff[4] = { 0 };
52 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_TIMESTAMP_VALUE, false, rplBuff, sizeof(rplBuff), reqBuff, sizeof(reqBuff));
53 0 : if(timestamp) { *timestamp = ((uint32_t)(rplBuff[0]) << 24) | ((uint32_t)(rplBuff[1]) << 16) | ((uint32_t)(rplBuff[2]) << 8) | (uint32_t)rplBuff[3]; }
54 0 : return(state);
55 : }
56 :
57 0 : int16_t LR2021::setCca(uint32_t duration, uint8_t gain) {
58 : uint8_t buff[] = {
59 0 : (uint8_t)((duration >> 16) & 0xFF), (uint8_t)((duration >> 8) & 0xFF), (uint8_t)(duration & 0xFF), gain,
60 0 : };
61 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_CCA, true, buff, sizeof(buff)));
62 : }
63 :
64 0 : int16_t LR2021::getCcaResult(float* rssiMin, float* rssiMax, float* rssiAvg) {
65 0 : uint8_t buff[4] = { 0 };
66 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_CCA_RESULT, false, buff, sizeof(buff));
67 : uint16_t raw;
68 0 : if(rssiMin) {
69 0 : raw = ((uint16_t)(buff[0]) << 1) | (uint16_t)((buff[3] >> 2) & 0x01);
70 0 : *rssiMin = (float)raw/-2.0f;
71 : }
72 0 : if(rssiMax) {
73 0 : raw = ((uint16_t)(buff[1]) << 1) | (uint16_t)((buff[3] >> 1) & 0x01);
74 0 : *rssiMax = (float)raw/-2.0f;
75 : }
76 0 : if(rssiAvg) {
77 0 : raw = ((uint16_t)(buff[2]) << 1) | (uint16_t)((buff[3] >> 0) & 0x01);
78 0 : *rssiAvg = (float)raw/-2.0f;
79 : }
80 0 : return(state);
81 : }
82 :
83 0 : int16_t LR2021::setCadParams(uint32_t cadTimeout, uint8_t threshold, uint8_t exitMode, uint32_t trxTimeout) {
84 : uint8_t buff[] = {
85 0 : (uint8_t)((cadTimeout >> 16) & 0xFF), (uint8_t)((cadTimeout >> 8) & 0xFF), (uint8_t)(cadTimeout & 0xFF),
86 0 : threshold, (uint8_t)(exitMode & 0x03),
87 0 : (uint8_t)((trxTimeout >> 16) & 0xFF), (uint8_t)((trxTimeout >> 8) & 0xFF), (uint8_t)(trxTimeout & 0xFF),
88 0 : };
89 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_CAD_PARAMS, true, buff, sizeof(buff)));
90 : }
91 :
92 0 : int16_t LR2021::setCad(void) {
93 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_CAD, true, NULL, 0));
94 : }
95 :
96 0 : int16_t LR2021::selPa(uint8_t pa) {
97 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SEL_PA, true, &pa, sizeof(pa)));
98 : }
99 :
100 1 : int16_t LR2021::setPaConfig(uint8_t pa, uint8_t paLfMode, uint8_t paLfDutyCycle, uint8_t paLfSlices, uint8_t paHfDutyCycle) {
101 : uint8_t buff[] = {
102 1 : (uint8_t)((pa << 7) | (paLfMode & 0x03)),
103 1 : (uint8_t)(((paLfDutyCycle & 0x0F) << 4) | (paLfSlices & 0x0F)),
104 1 : (uint8_t)((paHfDutyCycle & 0x1F)),
105 1 : };
106 2 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_PA_CONFIG, true, buff, sizeof(buff)));
107 : }
108 :
109 0 : int16_t LR2021::setTxParams(int8_t txPower, uint8_t rampTime) {
110 0 : uint8_t buff[] = { (uint8_t)txPower, rampTime };
111 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_TX_PARAMS, true, buff, sizeof(buff)));
112 : }
113 :
114 0 : int16_t LR2021::setPacketType(uint8_t packetType) {
115 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_SET_PACKET_TYPE, true, &packetType, sizeof(packetType));
116 0 : if (state == RADIOLIB_ERR_NONE) {
117 0 : state = this->resetDCDCworkaround();
118 : }
119 0 : return(state);
120 : }
121 :
122 24 : int16_t LR2021::getPacketType(uint8_t* packetType) {
123 24 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_GET_PACKET_TYPE, false, packetType, sizeof(uint8_t)));
124 : }
125 :
126 : #endif
|