11#pragma once
22
33#include < RadioLib.h>
4+ #include " MeshCore.h"
45
56#define LR1110_IRQ_HAS_PREAMBLE 0b0000000100 // 4 4 valid LoRa header received
67#define LR1110_IRQ_HEADER_VALID 0b0000010000 // 4 4 valid LoRa header received
@@ -9,6 +10,74 @@ class CustomLR1110 : public LR1110 {
910 public:
1011 CustomLR1110 (Module *mod) : LR1110(mod) { }
1112
13+ uint8_t shiftCount = 0 ;
14+
15+ int16_t standby () override {
16+ // tx resets the shift, standby is called on tx completion
17+ // this might not actually be what resets it, but it seems to work
18+ // more investigation needed
19+ this ->shiftCount = 0 ;
20+ return LR1110::standby ();
21+ }
22+
23+ size_t getPacketLength (bool update) override {
24+ size_t len = LR1110::getPacketLength (update);
25+ if (len == 0 ) {
26+ uint32_t irq = getIrqStatus ();
27+ if (irq & RADIOLIB_LR11X0_IRQ_HEADER_ERR) {
28+ MESH_DEBUG_PRINTLN (" LR1110: got header err, assuming shift" );
29+ this ->shiftCount += 4 ; // uint8 will loop around to 0 at 256, perfect as rx buffer is 256 bytes
30+ } else {
31+ MESH_DEBUG_PRINTLN (" LR1110: got zero-length packet without header err irq" );
32+ }
33+ }
34+ return len;
35+ }
36+
37+ int16_t readData (uint8_t *data, size_t len) override {
38+ // check active modem
39+ uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
40+ int16_t state = getPacketType (&modem);
41+ RADIOLIB_ASSERT (state);
42+ if ((modem != RADIOLIB_LR11X0_PACKET_TYPE_LORA) &&
43+ (modem != RADIOLIB_LR11X0_PACKET_TYPE_GFSK)) {
44+ return (RADIOLIB_ERR_WRONG_MODEM);
45+ }
46+
47+ // check integrity CRC
48+ uint32_t irq = getIrqStatus ();
49+ int16_t crcState = RADIOLIB_ERR_NONE;
50+ // Report CRC mismatch when there's a payload CRC error, or a header error and no valid header (to avoid false alarm from previous packet)
51+ if ((irq & RADIOLIB_LR11X0_IRQ_CRC_ERR) || ((irq & RADIOLIB_LR11X0_IRQ_HEADER_ERR) && !(irq & RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID))) {
52+ crcState = RADIOLIB_ERR_CRC_MISMATCH;
53+ }
54+
55+ // get packet length
56+ // the offset is needed since LR11x0 seems to move the buffer base by 4 bytes on every packet
57+ uint8_t offset = 0 ;
58+ size_t length = LR1110::getPacketLength (true , &offset);
59+ if ((len != 0 ) && (len < length)) {
60+ // user requested less data than we got, only return what was requested
61+ length = len;
62+ }
63+
64+ // read packet data
65+ state = readBuffer8 (data, length, (uint8_t )(offset + this ->shiftCount )); // add shiftCount to offset - only change from radiolib
66+ RADIOLIB_ASSERT (state);
67+
68+ // clear the Rx buffer
69+ state = clearRxBuffer ();
70+ RADIOLIB_ASSERT (state);
71+
72+ // clear interrupt flags
73+ state = clearIrqState (RADIOLIB_LR11X0_IRQ_ALL);
74+
75+ // check if CRC failed - this is done after reading data to give user the option to keep them
76+ RADIOLIB_ASSERT (crcState);
77+
78+ return (state);
79+ }
80+
1281 RadioLibTime_t getTimeOnAir (size_t len) override {
1382 // calculate number of symbols
1483 float N_symbol = 0 ;
0 commit comments