Skip to content

Commit 27e2963

Browse files
torntrouserssandeepmistry
authored andcommitted
Add getting the frequency error of a packet (#111)
1 parent f5cae9c commit 27e2963

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

API.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ float snr = LoRa.packetSnr();
166166

167167
Returns the estimated SNR of the received packet in dB.
168168

169+
### Packet Frequency Error
170+
171+
```arduino
172+
long freqErr = LoRa.packetFrequencyError();
173+
```
174+
175+
Returns the frequency error of the received packet in Hz. The frequency error is the frequency offset between the receiver centre frequency and that of an incoming LoRa signal.
176+
169177
### Available
170178

171179
```arduino

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ endPacket KEYWORD2
2121
parsePacket KEYWORD2
2222
packetRssi KEYWORD2
2323
packetSnr KEYWORD2
24+
packetFrequencyError KEYWORD2
2425

2526
write KEYWORD2
2627

src/LoRa.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#define REG_PREAMBLE_LSB 0x21
2626
#define REG_PAYLOAD_LENGTH 0x22
2727
#define REG_MODEM_CONFIG_3 0x26
28+
#define REG_FREQ_ERROR_MSB 0x28
29+
#define REG_FREQ_ERROR_MID 0x29
30+
#define REG_FREQ_ERROR_LSB 0x2a
2831
#define REG_RSSI_WIDEBAND 0x2c
2932
#define REG_DETECTION_OPTIMIZE 0x31
3033
#define REG_DETECTION_THRESHOLD 0x37
@@ -211,6 +214,25 @@ float LoRaClass::packetSnr()
211214
return ((int8_t)readRegister(REG_PKT_SNR_VALUE)) * 0.25;
212215
}
213216

217+
long LoRaClass::packetFrequencyError()
218+
{
219+
int32_t freqError = 0;
220+
freqError = static_cast<int32_t>(readRegister(REG_FREQ_ERROR_MSB) & B111);
221+
freqError <<= 8L;
222+
freqError += static_cast<int32_t>(readRegister(REG_FREQ_ERROR_MID));
223+
freqError <<= 8L;
224+
freqError += static_cast<int32_t>(readRegister(REG_FREQ_ERROR_LSB));
225+
226+
if (readRegister(REG_FREQ_ERROR_MSB) & B1000) { // Sign bit is on
227+
freqError -= 524288; // B1000'0000'0000'0000'0000
228+
}
229+
230+
const float fXtal = 32E6; // FXOSC: crystal oscillator (XTAL) frequency (2.5. Chip Specification, p. 14)
231+
const float fError = ((static_cast<float>(freqError) * (1L << 24)) / fXtal) * (getSignalBandwidth() / 500000.0f); // p. 37
232+
233+
return static_cast<long>(fError);
234+
}
235+
214236
size_t LoRaClass::write(uint8_t byte)
215237
{
216238
return write(&byte, sizeof(byte));
@@ -370,6 +392,23 @@ void LoRaClass::setSpreadingFactor(int sf)
370392
writeRegister(REG_MODEM_CONFIG_2, (readRegister(REG_MODEM_CONFIG_2) & 0x0f) | ((sf << 4) & 0xf0));
371393
}
372394

395+
long LoRaClass::getSignalBandwidth()
396+
{
397+
byte bw = (readRegister(REG_MODEM_CONFIG_1) >> 4);
398+
switch (bw) {
399+
case 0: return 7.8E3;
400+
case 1: return 10.4E3;
401+
case 2: return 15.6E3;
402+
case 3: return 20.8E3;
403+
case 4: return 31.25E3;
404+
case 5: return 41.7E3;
405+
case 6: return 62.5E3;
406+
case 7: return 125E3;
407+
case 8: return 250E3;
408+
case 9: return 500E3;
409+
}
410+
}
411+
373412
void LoRaClass::setSignalBandwidth(long sbw)
374413
{
375414
int bw;

src/LoRa.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LoRaClass : public Stream {
2727
int parsePacket(int size = 0);
2828
int packetRssi();
2929
float packetSnr();
30+
long packetFrequencyError();
3031

3132
// from Print
3233
virtual size_t write(uint8_t byte);
@@ -71,6 +72,8 @@ class LoRaClass : public Stream {
7172

7273
void handleDio0Rise();
7374

75+
long getSignalBandwidth();
76+
7477
uint8_t readRegister(uint8_t address);
7578
void writeRegister(uint8_t address, uint8_t value);
7679
uint8_t singleTransfer(uint8_t address, uint8_t value);

0 commit comments

Comments
 (0)