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