85 #if !defined(SX126x_SPI)
86 #define SX126x_SPI hwSPI
90 #if defined(ARDUINO_ARCH_AVR)
91 #if defined(__AVR_ATmega32U4__)
92 #define DEFAULT_SX126x_IRQ_PIN (3)
94 #define DEFAULT_SX126x_IRQ_PIN (2)
96 #elif defined(ARDUINO_ARCH_ESP8266)
97 #define DEFAULT_SX126x_IRQ_PIN (5)
98 #elif defined(ARDUINO_ARCH_ESP32)
99 #define DEFAULT_SX126x_IRQ_PIN (16)
100 #define DEFAULT_SX126x_IRQ_NUM digitalPinToInterrupt(DEFAULT_SX126x_IRQ_PIN)
101 #elif defined(ARDUINO_ARCH_SAMD)
102 #define DEFAULT_SX126x_IRQ_PIN (2)
103 #elif defined(LINUX_ARCH_RASPBERRYPI)
104 #define DEFAULT_SX126x_IRQ_PIN (22)
105 #elif defined(ARDUINO_ARCH_STM32F1)
106 #define DEFAULT_SX126x_IRQ_PIN (PA3)
107 #elif defined(TEENSYDUINO)
108 #define DEFAULT_SX126x_IRQ_PIN (8)
110 #define DEFAULT_SX126x_IRQ_PIN (2)
113 #ifndef DEFAULT_SX126x_CS_PIN
114 #define DEFAULT_SX126x_CS_PIN (SS)
118 #define SX126x_XTAL_FREQ ( double )32000000
119 #define SX126x_FREQ_DIV ( double )pow( 2.0, 25.0 )
120 #define SX126x_FREQ_STEP ( double )( SX126x_XTAL_FREQ / SX126x_FREQ_DIV )
123 #define SX126x_BW125CR45SF128 SX126x_BW_125KHZ | SX126x_CODING_RATE_4_5, SX126x_SPREADING_FACTOR_128CPS | SX126x_RX_PAYLOAD_CRC_ON, SX126x_AGC_AUTO_ON
124 #define SX126x_BW500CR45SF128 SX126x_BW_500KHZ | SX126x_CODING_RATE_4_5, SX126x_SPREADING_FACTOR_128CPS | SX126x_RX_PAYLOAD_CRC_ON, SX126x_AGC_AUTO_ON
125 #define SX126x_BW31_25CR48SF512 SX126x_BW_31_25KHZ | SX126x_CODING_RATE_4_8, SX126x_SPREADING_FACTOR_512CPS | SX126x_RX_PAYLOAD_CRC_ON, SX126x_AGC_AUTO_ON
126 #define SX126x_BW125CR48SF4096 SX126x_BW_125KHZ | SX126x_CODING_RATE_4_8, SX126x_SPREADING_FACTOR_4096CPS | SX126x_RX_PAYLOAD_CRC_ON, SX126x_AGC_AUTO_ON | SX126x_LOW_DATA_RATE_OPTIMIZE
128 #define SX126x_BROADCAST_ADDRESS (255u)
129 #define SX126x_ATC_TARGET_RANGE_DBM (2u)
130 #define SX126x_RSSI_OFFSET (137u)
131 #define SX126x_TARGET_RSSI (-70)
132 #define SX126x_PROMISCUOUS (false)
134 #if (MY_SX126x_MAX_POWER_LEVEL_DBM) <= (MY_SX126x_MIN_POWER_LEVEL_DBM)
135 #error SX126x_MAX_POWER_DBM has to be bigger than SX126x_MIN_POWER_DBM
138 #define SX126x_PACKET_HEADER_VERSION (1u)
139 #define SX126x_MIN_PACKET_HEADER_VERSION (1u)
141 #if !defined(SX126x_RETRY_TIMEOUT_MS)
147 #define SX126x_RETRY_TIMEOUT_MS (500ul)
150 #if !defined(MY_SX126x_TX_TIMEOUT_MS)
151 #define MY_SX126x_TX_TIMEOUT_MS (5*1000ul)
160 #ifndef MY_SX126x_TX_POWER_DBM
161 #define MY_SX126x_TX_POWER_DBM (13u) // 20mW
165 #define SX126x_169MHZ (169000000ul)
166 #define SX126x_315MHZ (315000000ul)
167 #define SX126x_434MHZ (433920000ul)
168 #define SX126x_868MHZ (868100000ul)
169 #define SX126x_915MHZ (915000000ul)
171 #define SX126x_RETRIES (5u)
172 #define SX126x_MAX_PACKET_LEN (0x40u)
173 #define SX126x_PREAMBLE_LENGTH (8u)
174 #define SX126x_CAD_TIMEOUT_MS (2 * 1000ul)
176 #define SX126x_POWERUP_DELAY_MS 3
177 #define SX126x_SLEEP_WARM_START 0x04
182 #define SX126x_REG_OCP 0x08E7
183 #define SX126x_REG_LORASW 0x0740
189 SX126x_RESET_STATS = 0x00,
190 SX126x_CLR_IRQSTATUS = 0x02,
191 SX126x_CLR_ERROR = 0x07,
192 SX126x_CFG_DIOIRQ = 0x08,
193 SX126x_SET_LORASYMBTIMEOUT = 0xA0,
194 SX126x_WRITE_REGISTER = 0x0D,
195 SX126x_WRITE_BUFFER = 0x0E,
196 SX126x_GET_STATS = 0x10,
197 SX126x_GET_PACKETTYPE = 0x11,
198 SX126x_GET_IRQSTATUS = 0x12,
199 SX126x_GET_RXBUFFERSTATUS = 0x13,
200 SX126x_GET_PACKETSTATUS = 0x14,
201 SX126x_GET_RSSIINST = 0x15,
202 SX126x_GET_ERROR = 0x17,
203 SX126x_READ_REGISTER = 0x1D,
204 SX126x_READ_BUFFER = 0x1E,
205 SX126x_SET_STANDBY = 0x80,
206 SX126x_SET_RX = 0x82,
207 SX126x_SET_TX = 0x83,
208 SX126x_SET_SLEEP = 0x84,
209 SX126x_SET_RFFREQUENCY = 0x86,
210 SX126x_SET_CADPARAMS = 0x88,
211 SX126x_CALIBRATE = 0x89,
212 SX126x_SET_PACKETTYPE = 0x8A,
213 SX126x_SET_MODULATIONPARAMS = 0x8B,
214 SX126x_SET_PACKETPARAMS = 0x8C,
215 SX126x_SET_TXPARAMS = 0x8E,
216 SX126x_SET_BUFFERBASEADDRESS = 0x8F,
217 SX126x_SET_TXFALLBACKMODE = 0x93,
218 SX126x_SET_RXDUTYCYCLE = 0x94,
219 SX126x_SET_PACONFIG = 0x95,
220 SX126x_SET_REGULATORMODE = 0x96,
221 SX126x_SET_TCXOMODE = 0x97,
222 SX126x_CALIBRATEIMAGE = 0x98,
223 SX126x_SET_RFSWITCHMODE = 0x9D,
224 SX126x_SET_STOPRXTIMERONPREAMBLE = 0x9F,
225 SX126x_GET_STATUS = 0xC0,
226 SX126x_SET_FS = 0xC1,
227 SX126x_SET_CAD = 0xC5,
228 SX126x_SET_TXCONTINUOUSWAVE = 0xD1,
229 SX126x_SET_TXCONTINUOUSPREAMBLE = 0xD2,
239 SX126x_STDBY_RC = 0x00,
240 SX126x_STDBY_XOSC = 0x01,
250 SX126x_USE_LDO = 0x00,
251 SX126x_USE_DCDC = 0x01,
258 SX126x_PACKET_TYPE_GFSK = 0x00,
259 SX126x_PACKET_TYPE_LORA = 0x01,
260 SX126x_PACKET_TYPE_NONE = 0x0F,
357 LORA_CAD_ON_1_SYMB = 0x00,
358 LORA_CAD_ON_2_SYMB = 0x01,
359 LORA_CAD_ON_4_SYMB = 0x02,
360 LORA_CAD_ON_8_SYMB = 0x03,
361 LORA_CAD_ON_16_SYMB = 0x04,
368 SX126x_CAD_ONLY = 0x00,
369 SX126x_CAD_RX = 0x01,
409 PACKET_TYPE_GFSK = 0x00,
410 PACKET_TYPE_LORA = 0x01,
411 PACKET_TYPE_NONE = 0x0F,
423 uint8_t paLut = 0x01;
433 RADIO_RAMP_10_US = 0x00,
434 RADIO_RAMP_20_US = 0x01,
435 RADIO_RAMP_40_US = 0x02,
436 RADIO_RAMP_80_US = 0x03,
437 RADIO_RAMP_200_US = 0x04,
438 RADIO_RAMP_800_US = 0x05,
439 RADIO_RAMP_1700_US = 0x06,
440 RADIO_RAMP_3400_US = 0x07,
505 #define SX126x_HEADER_LEN sizeof(sx126x_header_t)
506 #define SX126x_MAX_PAYLOAD_LEN (SX126x_MAX_PACKET_LEN - SX126x_HEADER_LEN)
550 SX126x_TCXO_1V6 = 0x00,
564 SX126x_IRQ_NONE = 0x0000,
565 SX126x_IRQ_TX_DONE = 0x0001,
566 SX126x_IRQ_RX_DONE = 0x0002,
567 SX126x_IRQ_PREAMBLE_DETECTED = 0x0004,
568 SX126x_IRQ_SYNCWORD_VALID = 0x0008,
569 SX126x_IRQ_HEADER_VALID = 0x0010,
570 SX126x_IRQ_HEADER_ERROR = 0x0020,
571 SX126x_IRQ_CRC_ERROR = 0x0040,
572 SX126x_IRQ_CAD_DONE = 0x0080,
573 SX126x_IRQ_CAD_ACTIVITY_DETECTED = 0x0100,
574 SX126x_IRQ_RX_TX_TIMEOUT = 0x0200,
575 SX126x_IRQ_ALL = 0xFFFF,
600 static void SX126x_readCommand(
sx126x_commands_t command, uint8_t *buffer, uint16_t size);
608 static void SX126x_sendRegisters(uint16_t address, uint8_t *buffer, uint16_t size);
615 static void SX126x_sendRegister(uint16_t address, uint8_t value);
621 static bool SX126x_initialise();
626 static void SX126x_wakeUp();
631 static void SX126x_standBy();
636 static void SX126x_busy();
641 static void SX126x_sleep();
646 static void SX126x_interruptHandler();
651 static void SX126x_handle();
663 static void SX126x_setFrequency(uint32_t frequency);
669 static void SX126x_setIrqMask(uint16_t mask);
675 static void SX126x_clearIrq(uint16_t mask);
681 static void SX126x_setPacketParameters(uint8_t length);
687 static bool SX126x_sanityCheck();
693 static void SX126x_setAddress(uint8_t address);
699 static uint8_t SX126x_getAddress(
void);
709 static bool SX126x_sendWithRetry(
const uint8_t recipient,
const void *buffer,
710 const uint8_t bufferSize,
const bool noACK);
720 static bool SX126x_send(
const uint8_t recipient, uint8_t *
data,
const uint8_t len,
727 static bool SX126x_sendPacket(sx126x_packet_t *packet);
735 static void SX126x_sendBuffer(
const uint8_t offset,
const uint8_t *buffer,
const uint8_t size);
743 static void SX126x_readBuffer(
const uint8_t offset, uint8_t *buffer,
const uint8_t size);
748 static void SX126x_tx();
754 static void SX126x_rx();
760 static bool SX126x_cad();
766 static bool SX126x_packetAvailable();
773 static uint8_t SX126x_getData(uint8_t *buffer,
const uint8_t bufferSize);
788 static void SX126x_ATC();
795 static void SX126x_setATC(
bool onOff, int8_t targetRSSI);
801 static int16_t SX126x_getSendingRSSI();
807 static int16_t SX126x_getSendingSNR();
813 static int16_t SX126x_getReceivingRSSI();
819 static int16_t SX126x_getReceivingSNR();
825 static int8_t SX126x_getTxPowerLevel();
831 static uint8_t SX126x_getTxPowerPercent();
837 static bool SX126x_setTxPowerPercent(
const uint8_t newPowerPercent);