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::setGfskModulationParams(uint32_t bitRate, uint8_t pulseShape, uint8_t rxBw, uint32_t freqDev) {
11 : uint8_t buff[] = {
12 0 : (uint8_t)((bitRate >> 24) & 0xFF), (uint8_t)((bitRate >> 16) & 0xFF),
13 0 : (uint8_t)((bitRate >> 8) & 0xFF), (uint8_t)(bitRate & 0xFF),
14 0 : pulseShape, rxBw, (uint8_t)((freqDev >> 16) & 0xFF),
15 0 : (uint8_t)((freqDev >> 8) & 0xFF), (uint8_t)(freqDev & 0xFF),
16 0 : };
17 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_SET_GFSK_MODULATION_PARAMS, true, buff, sizeof(buff));
18 0 : if (state == RADIOLIB_ERR_NONE) {
19 0 : state = this->setDCDCworkaround();
20 : }
21 0 : return(state);
22 : }
23 :
24 0 : int16_t LR2021::setGfskPacketParams(uint16_t preambleLen, uint8_t preambleDetect, bool longPreamble, bool pldLenBits, uint8_t addrComp, uint8_t packetFormat, uint16_t payloadLen, uint8_t crc, uint8_t dcFree) {
25 : uint8_t buff[] = {
26 0 : (uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF), preambleDetect,
27 0 : (uint8_t)(((uint8_t)longPreamble << 5) | ((uint8_t)pldLenBits << 4) | (addrComp << 2) | ((uint8_t)packetFormat & 0x03)),
28 0 : (uint8_t)((payloadLen >> 8) & 0xFF), (uint8_t)(payloadLen & 0xFF),
29 0 : (uint8_t)((crc << 4) | (dcFree << 0)),
30 0 : };
31 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_GFSK_PACKET_PARAMS, true, buff, sizeof(buff)));
32 : }
33 :
34 0 : int16_t LR2021::setGfskWhiteningParams(uint8_t whitenType, uint16_t init) {
35 : uint8_t buff[] = {
36 0 : (uint8_t)((whitenType << 4) | (uint8_t)((init >> 8) & 0x0F)),
37 : (uint8_t)(init & 0xFF),
38 0 : };
39 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_GFSK_WHITENING_PARAMS, true, buff, sizeof(buff)));
40 : }
41 :
42 0 : int16_t LR2021::setGfskCrcParams(uint32_t poly, uint32_t init) {
43 : uint8_t buff[] = {
44 0 : (uint8_t)((poly >> 24) & 0xFF), (uint8_t)((poly >> 16) & 0xFF),
45 0 : (uint8_t)((poly >> 8) & 0xFF), (uint8_t)(poly & 0xFF),
46 0 : (uint8_t)((init >> 24) & 0xFF), (uint8_t)((init >> 16) & 0xFF),
47 0 : (uint8_t)((init >> 8) & 0xFF), (uint8_t)(init & 0xFF),
48 0 : };
49 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_GFSK_CRC_PARAMS, true, buff, sizeof(buff)));
50 : }
51 :
52 0 : int16_t LR2021::setGfskSyncword(const uint8_t* syncWord, size_t syncWordLen, bool msbFirst) {
53 0 : uint8_t buff[9] = { 0 };
54 0 : for(int8_t i = 7; i >= (int8_t) (RADIOLIB_LR2021_GFSK_SYNC_WORD_LEN - syncWordLen); i--) {
55 0 : buff[i] = syncWord[i - (int8_t) (RADIOLIB_LR2021_GFSK_SYNC_WORD_LEN - syncWordLen)];
56 : }
57 0 : buff[8] = (uint8_t)msbFirst << 7 | ((syncWordLen*8) & 0x7F);
58 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_GFSK_SYNCWORD, true, buff, sizeof(buff)));
59 : }
60 :
61 0 : int16_t LR2021::setGfskAddress(uint8_t addrNode, uint8_t addrBroadcast) {
62 0 : uint8_t buff[] = { addrNode, addrBroadcast };
63 0 : return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_GFSK_ADDRESS, true, buff, sizeof(buff)));
64 : }
65 :
66 0 : int16_t LR2021::getGfskRxStats(uint16_t* packetRx, uint16_t* packetCrcError, uint16_t* lenError, uint16_t* preambleDet, uint16_t* syncOk, uint16_t* syncFail, uint16_t* timeout) {
67 0 : uint8_t buff[14] = { 0 };
68 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_GFSK_RX_STATS, false, buff, sizeof(buff));
69 0 : if(packetRx) { *packetRx = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1]; }
70 0 : if(packetCrcError) { *packetCrcError = ((uint16_t)(buff[2]) << 8) | (uint16_t)buff[3]; }
71 0 : if(lenError) { *lenError = ((uint16_t)(buff[4]) << 8) | (uint16_t)buff[5]; }
72 0 : if(preambleDet) { *preambleDet = ((uint16_t)(buff[6]) << 8) | (uint16_t)buff[7]; }
73 0 : if(syncOk) { *syncOk = ((uint16_t)(buff[8]) << 8) | (uint16_t)buff[9]; }
74 0 : if(syncFail) { *syncFail = ((uint16_t)(buff[10]) << 8) | (uint16_t)buff[11]; }
75 0 : if(timeout) { *timeout = ((uint16_t)(buff[12]) << 8) | (uint16_t)buff[13]; }
76 0 : return(state);
77 : }
78 :
79 0 : int16_t LR2021::getGfskPacketStatus(uint16_t* packetLen, float* rssiAvg, float* rssiSync, bool* addrMatchNode, bool* addrMatchBroadcast, float* lqi) {
80 0 : uint8_t buff[6] = { 0 };
81 0 : int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_GFSK_PACKET_STATUS, false, buff, sizeof(buff));
82 : uint16_t raw;
83 0 : if(packetLen) { *packetLen = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1]; }
84 0 : if(rssiAvg) {
85 0 : raw = (uint16_t)buff[2] << 1;
86 0 : raw |= (buff[4] & 0x04) >> 2;
87 0 : *rssiAvg = (float)raw / -2.0f;
88 : }
89 0 : if(rssiSync) {
90 0 : raw = (uint16_t)buff[3] << 1;
91 0 : raw |= (buff[4] & 0x01);
92 0 : *rssiSync = (float)raw / -2.0f;
93 : }
94 0 : if(addrMatchNode) { *addrMatchNode = (buff[4] & 0x10); }
95 0 : if(addrMatchBroadcast) { *addrMatchBroadcast = (buff[4] & 0x20); }
96 0 : if(lqi) { *lqi = buff[5] * 4.0f; }
97 0 : return(state);
98 : }
99 :
100 : #endif
|