first commit

This commit is contained in:
lewishe
2023-04-11 11:27:36 +08:00
commit daa54b6448
2146 changed files with 915567 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+994
View File
@@ -0,0 +1,994 @@
#if !defined(_RADIOLIB_CC1101_H) && !defined(RADIOLIB_EXCLUDE_CC1101)
#define _RADIOLIB_CC1101_H
#include "../../TypeDef.h"
#include "../../Module.h"
#include "../../protocols/PhysicalLayer/PhysicalLayer.h"
// CC1101 physical layer properties
#define RADIOLIB_CC1101_FREQUENCY_STEP_SIZE 396.7285156
#define RADIOLIB_CC1101_MAX_PACKET_LENGTH 255
#define RADIOLIB_CC1101_CRYSTAL_FREQ 26.0
#define RADIOLIB_CC1101_DIV_EXPONENT 16
#define RADIOLIB_CC1101_FIFO_SIZE 64
// CC1101 SPI commands
#define RADIOLIB_CC1101_CMD_READ 0b10000000
#define RADIOLIB_CC1101_CMD_WRITE 0b00000000
#define RADIOLIB_CC1101_CMD_BURST 0b01000000
#define RADIOLIB_CC1101_CMD_ACCESS_STATUS_REG 0b01000000
#define RADIOLIB_CC1101_CMD_FIFO_RX 0b10000000
#define RADIOLIB_CC1101_CMD_FIFO_TX 0b00000000
#define RADIOLIB_CC1101_CMD_RESET 0x30
#define RADIOLIB_CC1101_CMD_FSTXON 0x31
#define RADIOLIB_CC1101_CMD_XOFF 0x32
#define RADIOLIB_CC1101_CMD_CAL 0x33
#define RADIOLIB_CC1101_CMD_RX 0x34
#define RADIOLIB_CC1101_CMD_TX 0x35
#define RADIOLIB_CC1101_CMD_IDLE 0x36
#define RADIOLIB_CC1101_CMD_WOR 0x38
#define RADIOLIB_CC1101_CMD_POWER_DOWN 0x39
#define RADIOLIB_CC1101_CMD_FLUSH_RX 0x3A
#define RADIOLIB_CC1101_CMD_FLUSH_TX 0x3B
#define RADIOLIB_CC1101_CMD_WOR_RESET 0x3C
#define RADIOLIB_CC1101_CMD_NOP 0x3D
// CC1101 register map
#define RADIOLIB_CC1101_REG_IOCFG2 0x00
#define RADIOLIB_CC1101_REG_IOCFG1 0x01
#define RADIOLIB_CC1101_REG_IOCFG0 0x02
#define RADIOLIB_CC1101_REG_FIFOTHR 0x03
#define RADIOLIB_CC1101_REG_SYNC1 0x04
#define RADIOLIB_CC1101_REG_SYNC0 0x05
#define RADIOLIB_CC1101_REG_PKTLEN 0x06
#define RADIOLIB_CC1101_REG_PKTCTRL1 0x07
#define RADIOLIB_CC1101_REG_PKTCTRL0 0x08
#define RADIOLIB_CC1101_REG_ADDR 0x09
#define RADIOLIB_CC1101_REG_CHANNR 0x0A
#define RADIOLIB_CC1101_REG_FSCTRL1 0x0B
#define RADIOLIB_CC1101_REG_FSCTRL0 0x0C
#define RADIOLIB_CC1101_REG_FREQ2 0x0D
#define RADIOLIB_CC1101_REG_FREQ1 0x0E
#define RADIOLIB_CC1101_REG_FREQ0 0x0F
#define RADIOLIB_CC1101_REG_MDMCFG4 0x10
#define RADIOLIB_CC1101_REG_MDMCFG3 0x11
#define RADIOLIB_CC1101_REG_MDMCFG2 0x12
#define RADIOLIB_CC1101_REG_MDMCFG1 0x13
#define RADIOLIB_CC1101_REG_MDMCFG0 0x14
#define RADIOLIB_CC1101_REG_DEVIATN 0x15
#define RADIOLIB_CC1101_REG_MCSM2 0x16
#define RADIOLIB_CC1101_REG_MCSM1 0x17
#define RADIOLIB_CC1101_REG_MCSM0 0x18
#define RADIOLIB_CC1101_REG_FOCCFG 0x19
#define RADIOLIB_CC1101_REG_BSCFG 0x1A
#define RADIOLIB_CC1101_REG_AGCCTRL2 0x1B
#define RADIOLIB_CC1101_REG_AGCCTRL1 0x1C
#define RADIOLIB_CC1101_REG_AGCCTRL0 0x1D
#define RADIOLIB_CC1101_REG_WOREVT1 0x1E
#define RADIOLIB_CC1101_REG_WOREVT0 0x1F
#define RADIOLIB_CC1101_REG_WORCTRL 0x20
#define RADIOLIB_CC1101_REG_FREND1 0x21
#define RADIOLIB_CC1101_REG_FREND0 0x22
#define RADIOLIB_CC1101_REG_FSCAL3 0x23
#define RADIOLIB_CC1101_REG_FSCAL2 0x24
#define RADIOLIB_CC1101_REG_FSCAL1 0x25
#define RADIOLIB_CC1101_REG_FSCAL0 0x26
#define RADIOLIB_CC1101_REG_RCCTRL1 0x27
#define RADIOLIB_CC1101_REG_RCCTRL0 0x28
#define RADIOLIB_CC1101_REG_FSTEST 0x29
#define RADIOLIB_CC1101_REG_PTEST 0x2A
#define RADIOLIB_CC1101_REG_AGCTEST 0x2B
#define RADIOLIB_CC1101_REG_TEST2 0x2C
#define RADIOLIB_CC1101_REG_TEST1 0x2D
#define RADIOLIB_CC1101_REG_TEST0 0x2E
#define RADIOLIB_CC1101_REG_PARTNUM 0x30
#define RADIOLIB_CC1101_REG_VERSION 0x31
#define RADIOLIB_CC1101_REG_FREQEST 0x32
#define RADIOLIB_CC1101_REG_LQI 0x33
#define RADIOLIB_CC1101_REG_RSSI 0x34
#define RADIOLIB_CC1101_REG_MARCSTATE 0x35
#define RADIOLIB_CC1101_REG_WORTIME1 0x36
#define RADIOLIB_CC1101_REG_WORTIME0 0x37
#define RADIOLIB_CC1101_REG_PKTSTATUS 0x38
#define RADIOLIB_CC1101_REG_VCO_VC_DAC 0x39
#define RADIOLIB_CC1101_REG_TXBYTES 0x3A
#define RADIOLIB_CC1101_REG_RXBYTES 0x3B
#define RADIOLIB_CC1101_REG_RCCTRL1_STATUS 0x3C
#define RADIOLIB_CC1101_REG_RCCTRL0_STATUS 0x3D
#define RADIOLIB_CC1101_REG_PATABLE 0x3E
#define RADIOLIB_CC1101_REG_FIFO 0x3F
// CC1101_REG_IOCFG2 MSB LSB DESCRIPTION
#define RADIOLIB_CC1101_GDO2_NORM 0b00000000 // 6 6 GDO2 output: active high (default)
#define RADIOLIB_CC1101_GDO2_INV 0b01000000 // 6 6 active low
// CC1101_REG_IOCFG1
#define RADIOLIB_CC1101_GDO1_DS_LOW 0b00000000 // 7 7 GDO1 output drive strength: low (default)
#define RADIOLIB_CC1101_GDO1_DS_HIGH 0b10000000 // 7 7 high
#define RADIOLIB_CC1101_GDO1_NORM 0b00000000 // 6 6 GDO1 output: active high (default)
#define RADIOLIB_CC1101_GDO1_INV 0b01000000 // 6 6 active low
// CC1101_REG_IOCFG0
#define RADIOLIB_CC1101_GDO0_TEMP_SENSOR_OFF 0b00000000 // 7 7 analog temperature sensor output: disabled (default)
#define RADIOLIB_CC1101_GDO0_TEMP_SENSOR_ON 0b10000000 // 7 0 enabled
#define RADIOLIB_CC1101_GDO0_NORM 0b00000000 // 6 6 GDO0 output: active high (default)
#define RADIOLIB_CC1101_GDO0_INV 0b01000000 // 6 6 active low
// CC1101_REG_IOCFG2 + REG_IOCFG1 + REG_IOCFG0
#define RADIOLIB_CC1101_GDOX_RX_FIFO_FULL 0x00 // 5 0 Rx FIFO full or above threshold
#define RADIOLIB_CC1101_GDOX_RX_FIFO_FULL_OR_PKT_END 0x01 // 5 0 Rx FIFO full or above threshold or reached packet end
#define RADIOLIB_CC1101_GDOX_TX_FIFO_ABOVE_THR 0x02 // 5 0 Tx FIFO above threshold
#define RADIOLIB_CC1101_GDOX_TX_FIFO_FULL 0x03 // 5 0 Tx FIFO full
#define RADIOLIB_CC1101_GDOX_RX_FIFO_OVERFLOW 0x04 // 5 0 Rx FIFO overflowed
#define RADIOLIB_CC1101_GDOX_TX_FIFO_UNDERFLOW 0x05 // 5 0 Tx FIFO underflowed
#define RADIOLIB_CC1101_GDOX_SYNC_WORD_SENT_OR_RECEIVED 0x06 // 5 0 sync word was sent or received
#define RADIOLIB_CC1101_GDOX_PKT_RECEIVED_CRC_OK 0x07 // 5 0 packet received and CRC check passed
#define RADIOLIB_CC1101_GDOX_PREAMBLE_QUALITY_REACHED 0x08 // 5 0 received preamble quality is above threshold
#define RADIOLIB_CC1101_GDOX_CHANNEL_CLEAR 0x09 // 5 0 RSSI level below threshold (channel is clear)
#define RADIOLIB_CC1101_GDOX_PLL_LOCKED 0x0A // 5 0 PLL is locked
#define RADIOLIB_CC1101_GDOX_SERIAL_CLOCK 0x0B // 5 0 serial data clock
#define RADIOLIB_CC1101_GDOX_SERIAL_DATA_SYNC 0x0C // 5 0 serial data output in: synchronous mode
#define RADIOLIB_CC1101_GDOX_SERIAL_DATA_ASYNC 0x0D // 5 0 asynchronous mode
#define RADIOLIB_CC1101_GDOX_CARRIER_SENSE 0x0E // 5 0 RSSI above threshold
#define RADIOLIB_CC1101_GDOX_CRC_OK 0x0F // 5 0 CRC check passed
#define RADIOLIB_CC1101_GDOX_RX_HARD_DATA1 0x16 // 5 0 direct access to demodulated data
#define RADIOLIB_CC1101_GDOX_RX_HARD_DATA0 0x17 // 5 0 direct access to demodulated data
#define RADIOLIB_CC1101_GDOX_PA_PD 0x1B // 5 0 power amplifier circuit is powered down
#define RADIOLIB_CC1101_GDOX_LNA_PD 0x1C // 5 0 low-noise amplifier circuit is powered down
#define RADIOLIB_CC1101_GDOX_RX_SYMBOL_TICK 0x1D // 5 0 direct access to symbol tick of received data
#define RADIOLIB_CC1101_GDOX_WOR_EVNT0 0x24 // 5 0 wake-on-radio event 0
#define RADIOLIB_CC1101_GDOX_WOR_EVNT1 0x25 // 5 0 wake-on-radio event 1
#define RADIOLIB_CC1101_GDOX_CLK_256 0x26 // 5 0 256 Hz clock
#define RADIOLIB_CC1101_GDOX_CLK_32K 0x27 // 5 0 32 kHz clock
#define RADIOLIB_CC1101_GDOX_CHIP_RDYN 0x29 // 5 0 (default for GDO2)
#define RADIOLIB_CC1101_GDOX_XOSC_STABLE 0x2B // 5 0
#define RADIOLIB_CC1101_GDOX_HIGH_Z 0x2E // 5 0 high impedance state (default for GDO1)
#define RADIOLIB_CC1101_GDOX_HW_TO_0 0x2F // 5 0
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_1 0x30 // 5 0 crystal oscillator clock: f = f(XOSC)/1
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_1_5 0x31 // 5 0 f = f(XOSC)/1.5
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_2 0x32 // 5 0 f = f(XOSC)/2
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_3 0x33 // 5 0 f = f(XOSC)/3
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_4 0x34 // 5 0 f = f(XOSC)/4
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_6 0x35 // 5 0 f = f(XOSC)/6
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_8 0x36 // 5 0 f = f(XOSC)/8
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_12 0x37 // 5 0 f = f(XOSC)/12
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_16 0x38 // 5 0 f = f(XOSC)/16
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_24 0x39 // 5 0 f = f(XOSC)/24
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_32 0x3A // 5 0 f = f(XOSC)/32
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_48 0x3B // 5 0 f = f(XOSC)/48
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_64 0x3C // 5 0 f = f(XOSC)/64
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_96 0x3D // 5 0 f = f(XOSC)/96
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_128 0x3E // 5 0 f = f(XOSC)/128
#define RADIOLIB_CC1101_GDOX_CLOCK_XOSC_192 0x3F // 5 0 f = f(XOSC)/192 (default for GDO0)
// CC1101_REG_FIFOTHR
#define RADIOLIB_CC1101_ADC_RETENTION_OFF 0b00000000 // 6 6 do not retain ADC settings in sleep mode (default)
#define RADIOLIB_CC1101_ADC_RETENTION_ON 0b01000000 // 6 6 retain ADC settings in sleep mode
#define RADIOLIB_CC1101_RX_ATTEN_0_DB 0b00000000 // 5 4 Rx attenuation: 0 dB (default)
#define RADIOLIB_CC1101_RX_ATTEN_6_DB 0b00010000 // 5 4 6 dB
#define RADIOLIB_CC1101_RX_ATTEN_12_DB 0b00100000 // 5 4 12 dB
#define RADIOLIB_CC1101_RX_ATTEN_18_DB 0b00110000 // 5 4 18 dB
#define RADIOLIB_CC1101_FIFO_THR_TX_61_RX_4 0b00000000 // 3 0 TX fifo threshold: 61, RX fifo threshold: 4
// CC1101_REG_SYNC1
#define RADIOLIB_CC1101_SYNC_WORD_MSB 0xD3 // 7 0 sync word MSB
// CC1101_REG_SYNC0
#define RADIOLIB_CC1101_SYNC_WORD_LSB 0x91 // 7 0 sync word LSB
// CC1101_REG_PKTLEN
#define RADIOLIB_CC1101_PACKET_LENGTH 0xFF // 7 0 packet length in bytes
// CC1101_REG_PKTCTRL1
#define RADIOLIB_CC1101_PQT 0x00 // 7 5 preamble quality threshold
#define RADIOLIB_CC1101_CRC_AUTOFLUSH_OFF 0b00000000 // 3 3 automatic Rx FIFO flush on CRC check fail: disabled (default)
#define RADIOLIB_CC1101_CRC_AUTOFLUSH_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_CC1101_APPEND_STATUS_OFF 0b00000000 // 2 2 append 2 status bytes to packet: disabled
#define RADIOLIB_CC1101_APPEND_STATUS_ON 0b00000100 // 2 2 enabled (default)
#define RADIOLIB_CC1101_ADR_CHK_NONE 0b00000000 // 1 0 address check: none (default)
#define RADIOLIB_CC1101_ADR_CHK_NO_BROADCAST 0b00000001 // 1 0 without broadcast
#define RADIOLIB_CC1101_ADR_CHK_SINGLE_BROADCAST 0b00000010 // 1 0 broadcast address 0x00
#define RADIOLIB_CC1101_ADR_CHK_DOUBLE_BROADCAST 0b00000011 // 1 0 broadcast addresses 0x00 and 0xFF
// CC1101_REG_PKTCTRL0
#define RADIOLIB_CC1101_WHITE_DATA_OFF 0b00000000 // 6 6 data whitening: disabled
#define RADIOLIB_CC1101_WHITE_DATA_ON 0b01000000 // 6 6 enabled (default)
#define RADIOLIB_CC1101_PKT_FORMAT_NORMAL 0b00000000 // 5 4 packet format: normal (FIFOs)
#define RADIOLIB_CC1101_PKT_FORMAT_SYNCHRONOUS 0b00010000 // 5 4 synchronous serial
#define RADIOLIB_CC1101_PKT_FORMAT_RANDOM 0b00100000 // 5 4 random transmissions
#define RADIOLIB_CC1101_PKT_FORMAT_ASYNCHRONOUS 0b00110000 // 5 4 asynchronous serial
#define RADIOLIB_CC1101_CRC_OFF 0b00000000 // 2 2 CRC disabled
#define RADIOLIB_CC1101_CRC_ON 0b00000100 // 2 2 CRC enabled (default)
#define RADIOLIB_CC1101_LENGTH_CONFIG_FIXED 0b00000000 // 1 0 packet length: fixed
#define RADIOLIB_CC1101_LENGTH_CONFIG_VARIABLE 0b00000001 // 1 0 variable (default)
#define RADIOLIB_CC1101_LENGTH_CONFIG_INFINITE 0b00000010 // 1 0 infinite
// CC1101_REG_ADDR
#define RADIOLIB_CC1101_DEVICE_ADDR 0x00 // 7 0 device address
// CC1101_REG_CHANNR
#define RADIOLIB_CC1101_CHAN 0x00 // 7 0 channel number
// CC1101_REG_FSCTRL1
#define RADIOLIB_CC1101_FREQ_IF 0x0F // 4 0 IF frequency setting; f_IF = (f(XOSC) / 2^10) * CC1101_FREQ_IF
// CC1101_REG_FSCTRL0
#define RADIOLIB_CC1101_FREQOFF 0x00 // 7 0 base frequency offset (2s-compliment)
// CC1101_REG_FREQ2 + REG_FREQ1 + REG_FREQ0
#define RADIOLIB_CC1101_FREQ_MSB 0x1E // 5 0 base frequency setting: f_carrier = (f(XOSC) / 2^16) * FREQ
#define RADIOLIB_CC1101_FREQ_MID 0xC4 // 7 0 where f(XOSC) = 26 MHz
#define RADIOLIB_CC1101_FREQ_LSB 0xEC // 7 0 FREQ = 3-byte value of FREQ registers
// CC1101_REG_MDMCFG4
#define RADIOLIB_CC1101_CHANBW_E 0b10000000 // 7 6 channel bandwidth: BW_channel = f(XOSC) / (8 * (4 + CHANBW_M)*2^CHANBW_E) [Hz]
#define RADIOLIB_CC1101_CHANBW_M 0b00000000 // 5 4 default value for 26 MHz crystal: 203 125 Hz
#define RADIOLIB_CC1101_DRATE_E 0x0C // 3 0 symbol rate: R_data = (((256 + DRATE_M) * 2^DRATE_E) / 2^28) * f(XOSC) [Baud]
// CC1101_REG_MDMCFG3
#define RADIOLIB_CC1101_DRATE_M 0x22 // 7 0 default value for 26 MHz crystal: 115 051 Baud
// CC1101_REG_MDMCFG2
#define RADIOLIB_CC1101_DEM_DCFILT_OFF 0b10000000 // 7 7 digital DC filter: disabled
#define RADIOLIB_CC1101_DEM_DCFILT_ON 0b00000000 // 7 7 enabled - only for data rates above 250 kBaud (default)
#define RADIOLIB_CC1101_MOD_FORMAT_2_FSK 0b00000000 // 6 4 modulation format: 2-FSK (default)
#define RADIOLIB_CC1101_MOD_FORMAT_GFSK 0b00010000 // 6 4 GFSK
#define RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK 0b00110000 // 6 4 ASK/OOK
#define RADIOLIB_CC1101_MOD_FORMAT_4_FSK 0b01000000 // 6 4 4-FSK
#define RADIOLIB_CC1101_MOD_FORMAT_MFSK 0b01110000 // 6 4 MFSK - only for data rates above 26 kBaud
#define RADIOLIB_CC1101_MANCHESTER_EN_OFF 0b00000000 // 3 3 Manchester encoding: disabled (default)
#define RADIOLIB_CC1101_MANCHESTER_EN_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_CC1101_SYNC_MODE_NONE 0b00000000 // 2 0 synchronization: no preamble/sync
#define RADIOLIB_CC1101_SYNC_MODE_15_16 0b00000001 // 2 0 15/16 sync word bits
#define RADIOLIB_CC1101_SYNC_MODE_16_16 0b00000010 // 2 0 16/16 sync word bits (default)
#define RADIOLIB_CC1101_SYNC_MODE_30_32 0b00000011 // 2 0 30/32 sync word bits
#define RADIOLIB_CC1101_SYNC_MODE_NONE_THR 0b00000100 // 2 0 no preamble sync, carrier sense above threshold
#define RADIOLIB_CC1101_SYNC_MODE_15_16_THR 0b00000101 // 2 0 15/16 sync word bits, carrier sense above threshold
#define RADIOLIB_CC1101_SYNC_MODE_16_16_THR 0b00000110 // 2 0 16/16 sync word bits, carrier sense above threshold
#define RADIOLIB_CC1101_SYNC_MODE_30_32_THR 0b00000111 // 2 0 30/32 sync word bits, carrier sense above threshold
// CC1101_REG_MDMCFG1
#define RADIOLIB_CC1101_FEC_OFF 0b00000000 // 7 7 forward error correction: disabled (default)
#define RADIOLIB_CC1101_FEC_ON 0b10000000 // 7 7 enabled - only for fixed packet length
#define RADIOLIB_CC1101_NUM_PREAMBLE_2 0b00000000 // 6 4 number of preamble bytes: 2
#define RADIOLIB_CC1101_NUM_PREAMBLE_3 0b00010000 // 6 4 3
#define RADIOLIB_CC1101_NUM_PREAMBLE_4 0b00100000 // 6 4 4 (default)
#define RADIOLIB_CC1101_NUM_PREAMBLE_6 0b00110000 // 6 4 6
#define RADIOLIB_CC1101_NUM_PREAMBLE_8 0b01000000 // 6 4 8
#define RADIOLIB_CC1101_NUM_PREAMBLE_12 0b01010000 // 6 4 12
#define RADIOLIB_CC1101_NUM_PREAMBLE_16 0b01100000 // 6 4 16
#define RADIOLIB_CC1101_NUM_PREAMBLE_24 0b01110000 // 6 4 24
#define RADIOLIB_CC1101_CHANSPC_E 0x02 // 1 0 channel spacing: df_channel = (f(XOSC) / 2^18) * (256 + CHANSPC_M) * 2^CHANSPC_E [Hz]
// CC1101_REG_MDMCFG0
#define RADIOLIB_CC1101_CHANSPC_M 0xF8 // 7 0 default value for 26 MHz crystal: 199 951 kHz
// CC1101_REG_DEVIATN
#define RADIOLIB_CC1101_DEVIATION_E 0b01000000 // 6 4 frequency deviation: f_dev = (f(XOSC) / 2^17) * (8 + DEVIATION_M) * 2^DEVIATION_E [Hz]
#define RADIOLIB_CC1101_DEVIATION_M 0b00000111 // 2 0 default value for 26 MHz crystal: +- 47 607 Hz
#define RADIOLIB_CC1101_MSK_PHASE_CHANGE_PERIOD 0x07 // 2 0 phase change symbol period fraction: 1 / (MSK_PHASE_CHANGE_PERIOD + 1)
// CC1101_REG_MCSM2
#define RADIOLIB_CC1101_RX_TIMEOUT_RSSI_OFF 0b00000000 // 4 4 Rx timeout based on RSSI value: disabled (default)
#define RADIOLIB_CC1101_RX_TIMEOUT_RSSI_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_CC1101_RX_TIMEOUT_QUAL_OFF 0b00000000 // 3 3 check for sync word on Rx timeout
#define RADIOLIB_CC1101_RX_TIMEOUT_QUAL_ON 0b00001000 // 3 3 check for PQI set on Rx timeout
#define RADIOLIB_CC1101_RX_TIMEOUT_OFF 0b00000111 // 2 0 Rx timeout: disabled (default)
#define RADIOLIB_CC1101_RX_TIMEOUT_MAX 0b00000000 // 2 0 max value (actual value depends on WOR_RES, EVENT0 and f(XOSC))
// CC1101_REG_MCSM1
#define RADIOLIB_CC1101_CCA_MODE_ALWAYS 0b00000000 // 5 4 clear channel indication: always
#define RADIOLIB_CC1101_CCA_MODE_RSSI_THR 0b00010000 // 5 4 RSSI below threshold
#define RADIOLIB_CC1101_CCA_MODE_RX_PKT 0b00100000 // 5 4 unless receiving packet
#define RADIOLIB_CC1101_CCA_MODE_RSSI_THR_RX_PKT 0b00110000 // 5 4 RSSI below threshold unless receiving packet (default)
#define RADIOLIB_CC1101_RXOFF_IDLE 0b00000000 // 3 2 next mode after packet reception: idle (default)
#define RADIOLIB_CC1101_RXOFF_FSTXON 0b00000100 // 3 2 FSTxOn
#define RADIOLIB_CC1101_RXOFF_TX 0b00001000 // 3 2 Tx
#define RADIOLIB_CC1101_RXOFF_RX 0b00001100 // 3 2 Rx
#define RADIOLIB_CC1101_TXOFF_IDLE 0b00000000 // 1 0 next mode after packet transmission: idle (default)
#define RADIOLIB_CC1101_TXOFF_FSTXON 0b00000001 // 1 0 FSTxOn
#define RADIOLIB_CC1101_TXOFF_TX 0b00000010 // 1 0 Tx
#define RADIOLIB_CC1101_TXOFF_RX 0b00000011 // 1 0 Rx
// CC1101_REG_MCSM0
#define RADIOLIB_CC1101_FS_AUTOCAL_NEVER 0b00000000 // 5 4 automatic calibration: never (default)
#define RADIOLIB_CC1101_FS_AUTOCAL_IDLE_TO_RXTX 0b00010000 // 5 4 every transition from idle to Rx/Tx
#define RADIOLIB_CC1101_FS_AUTOCAL_RXTX_TO_IDLE 0b00100000 // 5 4 every transition from Rx/Tx to idle
#define RADIOLIB_CC1101_FS_AUTOCAL_RXTX_TO_IDLE_4TH 0b00110000 // 5 4 every 4th transition from Rx/Tx to idle
#define RADIOLIB_CC1101_PO_TIMEOUT_COUNT_1 0b00000000 // 3 2 number of counter expirations before CHP_RDYN goes low: 1 (default)
#define RADIOLIB_CC1101_PO_TIMEOUT_COUNT_16 0b00000100 // 3 2 16
#define RADIOLIB_CC1101_PO_TIMEOUT_COUNT_64 0b00001000 // 3 2 64
#define RADIOLIB_CC1101_PO_TIMEOUT_COUNT_256 0b00001100 // 3 2 256
#define RADIOLIB_CC1101_PIN_CTRL_OFF 0b00000000 // 1 1 pin radio control: disabled (default)
#define RADIOLIB_CC1101_PIN_CTRL_ON 0b00000010 // 1 1 enabled
#define RADIOLIB_CC1101_XOSC_FORCE_OFF 0b00000000 // 0 0 do not force XOSC to remain on in sleep (default)
#define RADIOLIB_CC1101_XOSC_FORCE_ON 0b00000001 // 0 0 force XOSC to remain on in sleep
// CC1101_REG_FOCCFG
#define RADIOLIB_CC1101_FOC_BS_CS_GATE_OFF 0b00000000 // 5 5 do not freeze frequency compensation until CS goes high
#define RADIOLIB_CC1101_FOC_BS_CS_GATE_ON 0b00100000 // 5 5 freeze frequency compensation until CS goes high (default)
#define RADIOLIB_CC1101_FOC_PRE_K 0b00000000 // 4 3 frequency compensation loop gain before sync word: K
#define RADIOLIB_CC1101_FOC_PRE_2K 0b00001000 // 4 3 2K
#define RADIOLIB_CC1101_FOC_PRE_3K 0b00010000 // 4 3 3K (default)
#define RADIOLIB_CC1101_FOC_PRE_4K 0b00011000 // 4 3 4K
#define RADIOLIB_CC1101_FOC_POST_K 0b00000000 // 2 2 frequency compensation loop gain after sync word: same as FOC_PRE
#define RADIOLIB_CC1101_FOC_POST_K_2 0b00000100 // 2 2 K/2 (default)
#define RADIOLIB_CC1101_FOC_LIMIT_NO_COMPENSATION 0b00000000 // 1 0 frequency compensation saturation point: no compensation - required for ASK/OOK
#define RADIOLIB_CC1101_FOC_LIMIT_BW_CHAN_8 0b00000001 // 1 0 +- BW_chan/8
#define RADIOLIB_CC1101_FOC_LIMIT_BW_CHAN_4 0b00000010 // 1 0 +- BW_chan/4 (default)
#define RADIOLIB_CC1101_FOC_LIMIT_BW_CHAN_2 0b00000011 // 1 0 +- BW_chan/2
// CC1101_REG_BSCFG
#define RADIOLIB_CC1101_BS_PRE_KI 0b00000000 // 7 6 clock recovery integral gain before sync word: Ki
#define RADIOLIB_CC1101_BS_PRE_2KI 0b01000000 // 7 6 2Ki (default)
#define RADIOLIB_CC1101_BS_PRE_3KI 0b10000000 // 7 6 3Ki
#define RADIOLIB_CC1101_BS_PRE_4KI 0b11000000 // 7 6 4Ki
#define RADIOLIB_CC1101_BS_PRE_KP 0b00000000 // 5 4 clock recovery proportional gain before sync word: Kp
#define RADIOLIB_CC1101_BS_PRE_2KP 0b00010000 // 5 4 2Kp
#define RADIOLIB_CC1101_BS_PRE_3KP 0b00100000 // 5 4 3Kp (default)
#define RADIOLIB_CC1101_BS_PRE_4KP 0b00110000 // 5 4 4Kp
#define RADIOLIB_CC1101_BS_POST_KI 0b00000000 // 3 3 clock recovery integral gain after sync word: same as BS_PRE
#define RADIOLIB_CC1101_BS_POST_KI_2 0b00001000 // 3 3 Ki/2 (default)
#define RADIOLIB_CC1101_BS_POST_KP 0b00000000 // 2 2 clock recovery proportional gain after sync word: same as BS_PRE
#define RADIOLIB_CC1101_BS_POST_KP_1 0b00000100 // 2 2 Kp (default)
#define RADIOLIB_CC1101_BS_LIMIT_NO_COMPENSATION 0b00000000 // 1 0 data rate compensation saturation point: no compensation
#define RADIOLIB_CC1101_BS_LIMIT_3_125 0b00000001 // 1 0 +- 3.125 %
#define RADIOLIB_CC1101_BS_LIMIT_6_25 0b00000010 // 1 0 +- 6.25 %
#define RADIOLIB_CC1101_BS_LIMIT_12_5 0b00000011 // 1 0 +- 12.5 %
// CC1101_REG_AGCCTRL2
#define RADIOLIB_CC1101_MAX_DVGA_GAIN_0 0b00000000 // 7 6 reduce maximum available DVGA gain: no reduction (default)
#define RADIOLIB_CC1101_MAX_DVGA_GAIN_1 0b01000000 // 7 6 disable top gain setting
#define RADIOLIB_CC1101_MAX_DVGA_GAIN_2 0b10000000 // 7 6 disable top two gain setting
#define RADIOLIB_CC1101_MAX_DVGA_GAIN_3 0b11000000 // 7 6 disable top three gain setting
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_0_DB 0b00000000 // 5 3 reduce maximum LNA gain by: 0 dB (default)
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_2_6_DB 0b00001000 // 5 3 2.6 dB
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_6_1_DB 0b00010000 // 5 3 6.1 dB
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_7_4_DB 0b00011000 // 5 3 7.4 dB
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_9_2_DB 0b00100000 // 5 3 9.2 dB
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_11_5_DB 0b00101000 // 5 3 11.5 dB
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_14_6_DB 0b00110000 // 5 3 14.6 dB
#define RADIOLIB_CC1101_LNA_GAIN_REDUCE_17_1_DB 0b00111000 // 5 3 17.1 dB
#define RADIOLIB_CC1101_MAGN_TARGET_24_DB 0b00000000 // 2 0 average amplitude target for filter: 24 dB
#define RADIOLIB_CC1101_MAGN_TARGET_27_DB 0b00000001 // 2 0 27 dB
#define RADIOLIB_CC1101_MAGN_TARGET_30_DB 0b00000010 // 2 0 30 dB
#define RADIOLIB_CC1101_MAGN_TARGET_33_DB 0b00000011 // 2 0 33 dB (default)
#define RADIOLIB_CC1101_MAGN_TARGET_36_DB 0b00000100 // 2 0 36 dB
#define RADIOLIB_CC1101_MAGN_TARGET_38_DB 0b00000101 // 2 0 38 dB
#define RADIOLIB_CC1101_MAGN_TARGET_40_DB 0b00000110 // 2 0 40 dB
#define RADIOLIB_CC1101_MAGN_TARGET_42_DB 0b00000111 // 2 0 42 dB
// CC1101_REG_AGCCTRL1
#define RADIOLIB_CC1101_AGC_LNA_PRIORITY_LNA2 0b00000000 // 6 6 LNA priority setting: LNA2 first
#define RADIOLIB_CC1101_AGC_LNA_PRIORITY_LNA 0b01000000 // 6 6 LNA first (default)
#define RADIOLIB_CC1101_CARRIER_SENSE_REL_THR_OFF 0b00000000 // 5 4 RSSI relative change to assert carrier sense: disabled (default)
#define RADIOLIB_CC1101_CARRIER_SENSE_REL_THR_6_DB 0b00010000 // 5 4 6 dB
#define RADIOLIB_CC1101_CARRIER_SENSE_REL_THR_10_DB 0b00100000 // 5 4 10 dB
#define RADIOLIB_CC1101_CARRIER_SENSE_REL_THR_14_DB 0b00110000 // 5 4 14 dB
#define RADIOLIB_CC1101_CARRIER_SENSE_ABS_THR 0x00 // 3 0 RSSI threshold to assert carrier sense in 2s compliment, Thr = MAGN_TARGET + CARRIER_SENSE_ABS_TH [dB]
// CC1101_REG_AGCCTRL0
#define RADIOLIB_CC1101_HYST_LEVEL_NONE 0b00000000 // 7 6 AGC hysteresis level: none
#define RADIOLIB_CC1101_HYST_LEVEL_LOW 0b01000000 // 7 6 low
#define RADIOLIB_CC1101_HYST_LEVEL_MEDIUM 0b10000000 // 7 6 medium (default)
#define RADIOLIB_CC1101_HYST_LEVEL_HIGH 0b11000000 // 7 6 high
#define RADIOLIB_CC1101_WAIT_TIME_8_SAMPLES 0b00000000 // 5 4 AGC wait time: 8 samples
#define RADIOLIB_CC1101_WAIT_TIME_16_SAMPLES 0b00010000 // 5 4 16 samples (default)
#define RADIOLIB_CC1101_WAIT_TIME_24_SAMPLES 0b00100000 // 5 4 24 samples
#define RADIOLIB_CC1101_WAIT_TIME_32_SAMPLES 0b00110000 // 5 4 32 samples
#define RADIOLIB_CC1101_AGC_FREEZE_NEVER 0b00000000 // 3 2 freeze AGC gain: never (default)
#define RADIOLIB_CC1101_AGC_FREEZE_SYNC_WORD 0b00000100 // 3 2 when sync word is found
#define RADIOLIB_CC1101_AGC_FREEZE_MANUAL_A 0b00001000 // 3 2 manually freeze analog control
#define RADIOLIB_CC1101_AGC_FREEZE_MANUAL_AD 0b00001100 // 3 2 manually freeze analog and digital control
#define RADIOLIB_CC1101_FILTER_LENGTH_8 0b00000000 // 1 0 averaging length for channel filter: 8 samples
#define RADIOLIB_CC1101_FILTER_LENGTH_16 0b00000001 // 1 0 16 samples (default)
#define RADIOLIB_CC1101_FILTER_LENGTH_32 0b00000010 // 1 0 32 samples
#define RADIOLIB_CC1101_FILTER_LENGTH_64 0b00000011 // 1 0 64 samples
#define RADIOLIB_CC1101_ASK_OOK_BOUNDARY_4_DB 0b00000000 // 1 0 ASK/OOK decision boundary: 4 dB
#define RADIOLIB_CC1101_ASK_OOK_BOUNDARY_8_DB 0b00000001 // 1 0 8 dB (default)
#define RADIOLIB_CC1101_ASK_OOK_BOUNDARY_12_DB 0b00000010 // 1 0 12 dB
#define RADIOLIB_CC1101_ASK_OOK_BOUNDARY_16_DB 0b00000011 // 1 0 16 dB
// CC1101_REG_WOREVT1 + REG_WOREVT0
#define RADIOLIB_CC1101_EVENT0_TIMEOUT_MSB 0x87 // 7 0 EVENT0 timeout: t_event0 = (750 / f(XOSC)) * EVENT0_TIMEOUT * 2^(5 * WOR_RES) [s]
#define RADIOLIB_CC1101_EVENT0_TIMEOUT_LSB 0x6B // 7 0 default value for 26 MHz crystal: 1.0 s
// CC1101_REG_WORCTRL
#define RADIOLIB_CC1101_RC_POWER_UP 0b00000000 // 7 7 power up RC oscillator
#define RADIOLIB_CC1101_RC_POWER_DOWN 0b10000000 // 7 7 power down RC oscillator
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_4 0b00000000 // 6 4 EVENT1 timeout: 4 RC periods
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_6 0b00010000 // 6 4 6 RC periods
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_8 0b00100000 // 6 4 8 RC periods
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_12 0b00110000 // 6 4 12 RC periods
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_16 0b01000000 // 6 4 16 RC periods
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_24 0b01010000 // 6 4 24 RC periods
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_32 0b01100000 // 6 4 32 RC periods
#define RADIOLIB_CC1101_EVENT1_TIMEOUT_48 0b01110000 // 6 4 48 RC periods (default)
#define RADIOLIB_CC1101_RC_CAL_OFF 0b00000000 // 3 3 disable RC oscillator calibration
#define RADIOLIB_CC1101_RC_CAL_ON 0b00001000 // 3 3 enable RC oscillator calibration (default)
#define RADIOLIB_CC1101_WOR_RES_1 0b00000000 // 1 0 EVENT0 resolution: 1 period (default)
#define RADIOLIB_CC1101_WOR_RES_2_5 0b00000001 // 1 0 2^5 periods
#define RADIOLIB_CC1101_WOR_RES_2_10 0b00000010 // 1 0 2^10 periods
#define RADIOLIB_CC1101_WOR_RES_2_15 0b00000011 // 1 0 2^15 periods
// CC1101_REG_FREND1
#define RADIOLIB_CC1101_LNA_CURRENT 0x01 // 7 6 front-end LNA PTAT current output adjustment
#define RADIOLIB_CC1101_LNA2MIX_CURRENT 0x01 // 5 4 front-end PTAT output adjustment
#define RADIOLIB_CC1101_LODIV_BUF_CURRENT_RX 0x01 // 3 2 Rx LO buffer current adjustment
#define RADIOLIB_CC1101_MIX_CURRENT 0x02 // 1 0 mixer current adjustment
// CC1101_REG_FREND0
#define RADIOLIB_CC1101_LODIV_BUF_CURRENT_TX 0x01 // 5 4 Tx LO buffer current adjustment
#define RADIOLIB_CC1101_PA_POWER 0x00 // 2 0 set power amplifier power according to PATABLE
// CC1101_REG_FSCAL3
#define RADIOLIB_CC1101_CHP_CURR_CAL_OFF 0b00000000 // 5 4 disable charge pump calibration
#define RADIOLIB_CC1101_CHP_CURR_CAL_ON 0b00100000 // 5 4 enable charge pump calibration (default)
#define RADIOLIB_CC1101_FSCAL3 0x09 // 3 0 charge pump output current: I_out = I_0 * 2^(FSCAL3/4) [A]
// CC1101_REG_FSCAL2
#define RADIOLIB_CC1101_VCO_CORE_LOW 0b00000000 // 5 5 VCO: low (default)
#define RADIOLIB_CC1101_VCO_CORE_HIGH 0b00100000 // 5 5 high
#define RADIOLIB_CC1101_FSCAL2 0x0A // 4 0 VCO current result/override
// CC1101_REG_FSCAL1
#define RADIOLIB_CC1101_FSCAL1 0x20 // 5 0 capacitor array setting for coarse VCO tuning
// CC1101_REG_FSCAL0
#define RADIOLIB_CC1101_FSCAL0 0x0D // 6 0 frequency synthesizer calibration setting
// CC1101_REG_RCCTRL1
#define RADIOLIB_CC1101_RCCTRL1 0x41 // 6 0 RC oscillator configuration
// CC1101_REG_RCCTRL0
#define RADIOLIB_CC1101_RCCTRL0 0x00 // 6 0 RC oscillator configuration
// CC1101_REG_PTEST
#define RADIOLIB_CC1101_TEMP_SENS_IDLE_OFF 0x7F // 7 0 temperature sensor will not be available in idle mode (default)
#define RADIOLIB_CC1101_TEMP_SENS_IDLE_ON 0xBF // 7 0 temperature sensor will be available in idle mode
// CC1101_REG_TEST0
#define RADIOLIB_CC1101_VCO_SEL_CAL_OFF 0b00000000 // 1 1 disable VCO selection calibration stage
#define RADIOLIB_CC1101_VCO_SEL_CAL_ON 0b00000010 // 1 1 enable VCO selection calibration stage
// CC1101_REG_PARTNUM
#define RADIOLIB_CC1101_PARTNUM 0x00
// CC1101_REG_VERSION
#define RADIOLIB_CC1101_VERSION_CURRENT 0x14
#define RADIOLIB_CC1101_VERSION_LEGACY 0x04
#define RADIOLIB_CC1101_VERSION_CLONE 0x17
// CC1101_REG_MARCSTATE
#define RADIOLIB_CC1101_MARC_STATE_SLEEP 0x00 // 4 0 main radio control state: sleep
#define RADIOLIB_CC1101_MARC_STATE_IDLE 0x01 // 4 0 idle
#define RADIOLIB_CC1101_MARC_STATE_XOFF 0x02 // 4 0 XOFF
#define RADIOLIB_CC1101_MARC_STATE_VCOON_MC 0x03 // 4 0 VCOON_MC
#define RADIOLIB_CC1101_MARC_STATE_REGON_MC 0x04 // 4 0 REGON_MC
#define RADIOLIB_CC1101_MARC_STATE_MANCAL 0x05 // 4 0 MANCAL
#define RADIOLIB_CC1101_MARC_STATE_VCOON 0x06 // 4 0 VCOON
#define RADIOLIB_CC1101_MARC_STATE_REGON 0x07 // 4 0 REGON
#define RADIOLIB_CC1101_MARC_STATE_STARTCAL 0x08 // 4 0 STARTCAL
#define RADIOLIB_CC1101_MARC_STATE_BWBOOST 0x09 // 4 0 BWBOOST
#define RADIOLIB_CC1101_MARC_STATE_FS_LOCK 0x0A // 4 0 FS_LOCK
#define RADIOLIB_CC1101_MARC_STATE_IFADCON 0x0B // 4 0 IFADCON
#define RADIOLIB_CC1101_MARC_STATE_ENDCAL 0x0C // 4 0 ENDCAL
#define RADIOLIB_CC1101_MARC_STATE_RX 0x0D // 4 0 RX
#define RADIOLIB_CC1101_MARC_STATE_RX_END 0x0E // 4 0 RX_END
#define RADIOLIB_CC1101_MARC_STATE_RX_RST 0x0F // 4 0 RX_RST
#define RADIOLIB_CC1101_MARC_STATE_TXRX_SWITCH 0x10 // 4 0 TXRX_SWITCH
#define RADIOLIB_CC1101_MARC_STATE_RXFIFO_OVERFLOW 0x11 // 4 0 RXFIFO_OVERFLOW
#define RADIOLIB_CC1101_MARC_STATE_FSTXON 0x12 // 4 0 FSTXON
#define RADIOLIB_CC1101_MARC_STATE_TX 0x13 // 4 0 TX
#define RADIOLIB_CC1101_MARC_STATE_TX_END 0x14 // 4 0 TX_END
#define RADIOLIB_CC1101_MARC_STATE_RXTX_SWITCH 0x15 // 4 0 RXTX_SWITCH
#define RADIOLIB_CC1101_MARC_STATE_TXFIFO_UNDERFLOW 0x16 // 4 0 TXFIFO_UNDERFLOW
// CC1101_REG_WORTIME1 + REG_WORTIME0
#define RADIOLIB_CC1101_WORTIME_MSB 0x00 // 7 0 WOR timer value
#define RADIOLIB_CC1101_WORTIME_LSB 0x00 // 7 0
// CC1101_REG_PKTSTATUS
#define RADIOLIB_CC1101_CRC_OK 0b10000000 // 7 7 CRC check passed
#define RADIOLIB_CC1101_CRC_ERROR 0b00000000 // 7 7 CRC check failed
#define RADIOLIB_CC1101_CS 0b01000000 // 6 6 carrier sense
#define RADIOLIB_CC1101_PQT_REACHED 0b00100000 // 5 5 preamble quality reached
#define RADIOLIB_CC1101_CCA 0b00010000 // 4 4 channel clear
#define RADIOLIB_CC1101_SFD 0b00001000 // 3 3 start of frame delimiter - sync word received
#define RADIOLIB_CC1101_GDO2_ACTIVE 0b00000100 // 2 2 GDO2 is active/asserted
#define RADIOLIB_CC1101_GDO0_ACTIVE 0b00000001 // 0 0 GDO0 is active/asserted
/*!
\class CC1101
\brief Control class for %CC1101 module.
*/
class CC1101: public PhysicalLayer {
public:
// introduce PhysicalLayer overloads
using PhysicalLayer::transmit;
using PhysicalLayer::receive;
using PhysicalLayer::startTransmit;
using PhysicalLayer::readData;
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
CC1101(Module* module);
Module* getMod();
// basic methods
/*!
\brief Initialization method.
\param freq Carrier frequency in MHz. Defaults to 434.0 MHz.
\param br Bit rate to be used in kbps. Defaults to 4.8 kbps.
\param freqDev Frequency deviation from carrier frequency in kHz Defaults to 5.0 kHz.
\param rxBw Receiver bandwidth in kHz. Defaults to 135.0 kHz.
\param power Output power in dBm. Defaults to 10 dBm.
\param preambleLength Preamble Length in bits. Defaults to 16 bits.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 135.0, int8_t power = 10, uint8_t preambleLength = 16);
/*!
\brief Blocking binary transmit method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\param addr Address to send the data to. Will only be added if address filtering was enabled.
\returns \ref status_codes
*/
int16_t transmit(uint8_t* data, size_t len, uint8_t addr = 0) override;
/*!
\brief Blocking binary receive method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\returns \ref status_codes
*/
int16_t receive(uint8_t* data, size_t len) override;
/*!
\brief Sets the module to standby mode.
\returns \ref status_codes
*/
int16_t standby() override;
/*!
\brief Starts direct mode transmission.
\param frf Raw RF frequency value. Defaults to 0, required for quick frequency shifts in RTTY.
\returns \ref status_codes
*/
int16_t transmitDirect(uint32_t frf = 0) override;
/*!
\brief Starts direct mode reception.
\returns \ref status_codes
*/
int16_t receiveDirect() override;
/*!
\brief Starts asynchronous direct mode transmission.
\param frf Raw RF frequency value. Defaults to 0, required for quick frequency shifts in RTTY.
\returns \ref status_codes
*/
int16_t transmitDirectAsync(uint32_t frf = 0);
/*!
\brief Starts asynchronous direct mode reception.
\returns \ref status_codes
*/
int16_t receiveDirectAsync();
/*!
\brief Stops direct mode. It is required to call this method to switch from direct transmissions to packet-based transmissions.
*/
int16_t packetMode();
// interrupt methods
/*!
\brief Sets interrupt service routine to call when GDO0 activates.
\param func ISR to call.
\param dir Signal change direction. Defaults to RISING.
*/
void setGdo0Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir = RISING);
/*!
\brief Clears interrupt service routine to call when GDO0 activates.
*/
void clearGdo0Action();
/*!
\brief Sets interrupt service routine to call when GDO2 activates.
\param func ISR to call.
\param dir Signal change direction. Defaults to FALLING.
*/
void setGdo2Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir = FALLING);
/*!
\brief Clears interrupt service routine to call when GDO0 activates.
*/
void clearGdo2Action();
/*!
\brief Interrupt-driven binary transmit method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\param addr Address to send the data to. Will only be added if address filtering was enabled.
\returns \ref status_codes
*/
int16_t startTransmit(uint8_t* data, size_t len, uint8_t addr = 0) override;
/*!
\brief Interrupt-driven receive method. GDO0 will be activated when full packet is received.
\returns \ref status_codes
*/
int16_t startReceive();
/*!
\brief Reads data received after calling startReceive method.
\param data Pointer to array to save the received binary data.
\param len Number of bytes that will be read. When set to 0, the packet length will be retreived automatically.
When more bytes than received are requested, only the number of bytes requested will be returned.
\returns \ref status_codes
*/
int16_t readData(uint8_t* data, size_t len) override;
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values are in bands 300.0 to 348.0 MHz, 387.0 to 464.0 MHz and 779.0 to 928.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
/*!
\brief Sets bit rate. Allowed values range from 0.025 to 600.0 kbps.
\param br Bit rate to be set in kbps.
\returns \ref status_codes
*/
int16_t setBitRate(float br);
/*!
\brief Sets receiver bandwidth. Allowed values range from 58.0 to 812.0 kHz.
\param rxBw Receiver bandwidth to be set in kHz.
\returns \ref status_codes
*/
int16_t setRxBandwidth(float rxBw);
/*!
\brief Sets frequency deviation. Allowed values range from 1.587 to 380.8 kHz.
\param freqDev Frequency deviation to be set in kHz.
\returns \ref status_codes
*/
int16_t setFrequencyDeviation(float freqDev) override;
/*!
\brief Sets output power. Allowed values are -30, -20, -15, -10, 0, 5, 7 or 10 dBm.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
/*!
\brief Sets 16-bit sync word as a two byte value.
\param syncH MSB of the sync word.
\param syncL LSB of the sync word.
\param maxErrBits Maximum allowed number of bit errors in received sync word. Defaults to 0.
\param requireCarrierSense Require carrier sense above threshold in addition to sync word.
\returns \ref status_codes
*/
int16_t setSyncWord(uint8_t syncH, uint8_t syncL, uint8_t maxErrBits = 0, bool requireCarrierSense = false);
/*!
\brief Sets 1 or 2 bytes of sync word.
\param syncWord Pointer to the array of sync word bytes.
\param len Sync word length in bytes.
\param maxErrBits Maximum allowed number of bit errors in received sync word. Defaults to 0.
\param requireCarrierSense Require carrier sense above threshold in addition to sync word.
\returns \ref status_codes
*/
int16_t setSyncWord(uint8_t* syncWord, uint8_t len, uint8_t maxErrBits = 0, bool requireCarrierSense = false);
/*!
\brief Sets preamble length.
\param preambleLength Preamble length to be set (in bits), allowed values: 16, 24, 32, 48, 64, 96, 128 and 192.
\returns \ref status_codes
*/
int16_t setPreambleLength(uint8_t preambleLength);
/*!
\brief Sets node and broadcast addresses. Calling this method will also enable address filtering.
\param nodeAddr Node address to be set.
\param numBroadcastAddrs Number of broadcast addresses to be used. Can be set to 0 (no broadcast), 1 (broadcast at 0x00) or 2 (broadcast at 0x00 and 0xFF).
\returns \ref status_codes
*/
int16_t setNodeAddress(uint8_t nodeAddr, uint8_t numBroadcastAddrs = 0);
/*!
\brief Disables address filtering. Calling this method will also erase previously set addresses.
\returns \ref status_codes
*/
int16_t disableAddressFiltering();
/*!
\brief Enables/disables OOK modulation instead of FSK.
\param enableOOK Enable (true) or disable (false) OOK.
\returns \ref status_codes
*/
int16_t setOOK(bool enableOOK);
/*!
\brief Gets RSSI (Recorded Signal Strength Indicator) of the last received packet.
or in asynchronous direct mode the current RSSI level
\returns RSSI in dBm.
*/
float getRSSI();
/*!
\brief Gets LQI (Link Quality Indicator) of the last received packet.
\returns Last packet LQI (lower is better).
*/
uint8_t getLQI() const;
/*!
\brief Query modem for the packet length of received payload.
\param update Update received packet length. Will return cached value when set to false.
\returns Length of last received packet in bytes.
*/
size_t getPacketLength(bool update = true) override;
/*!
\brief Set modem in fixed packet length mode.
\param len Packet length.
\returns \ref status_codes
*/
int16_t fixedPacketLengthMode(uint8_t len = RADIOLIB_CC1101_MAX_PACKET_LENGTH);
/*!
\brief Set modem in variable packet length mode.
\param len Maximum packet length.
\returns \ref status_codes
*/
int16_t variablePacketLengthMode(uint8_t maxLen = RADIOLIB_CC1101_MAX_PACKET_LENGTH);
/*!
\brief Enable sync word filtering and generation.
\param numBits Sync word length in bits.
\param requireCarrierSense Require carrier sense above threshold in addition to sync word.
\returns \ref status_codes
*/
int16_t enableSyncWordFiltering(uint8_t maxErrBits = 0, bool requireCarrierSense = false);
/*!
\brief Disable preamble and sync word filtering and generation.
\param requireCarrierSense Require carrier sense above threshold.
\returns \ref status_codes
*/
int16_t disableSyncWordFiltering(bool requireCarrierSense = false);
/*!
\brief Enable CRC filtering and generation.
\param crcOn Set or unset CRC generation and filtering.
\returns \ref status_codes
*/
int16_t setCrcFiltering(bool crcOn = true);
/*!
\brief Set modem in "sniff" mode: no packet filtering (e.g., no preamble, sync word, address, CRC).
\param promiscuous Set or unset promiscuous mode.
\returns \ref status_codes
*/
int16_t setPromiscuousMode(bool promiscuous = true);
/*!
\brief Get whether the modem is in promiscuous mode: no packet filtering (e.g., no preamble, sync word, address, CRC).
\returns Whether the modem is in promiscuous mode
*/
bool getPromiscuousMode();
/*!
\brief Sets Gaussian filter bandwidth-time product that will be used for data shaping.
Allowed value is RADIOLIB_SHAPING_0_5. Set to RADIOLIB_SHAPING_NONE to disable data shaping.
\param sh Gaussian shaping bandwidth-time product that will be used for data shaping.
\returns \ref status_codes
*/
int16_t setDataShaping(uint8_t sh) override;
/*!
\brief Sets transmission encoding. Allowed values are RADIOLIB_ENCODING_NRZ, RADIOLIB_ENCODING_MANCHESTER, and RADIOLIB_ENCODING_WHITENING.
Note that encoding on CC1101 is applied to the entire stream including preamble, sync word, and CRC.
\param encoding Encoding to be used.
\returns \ref status_codes
*/
int16_t setEncoding(uint8_t encoding) override;
/*!
\brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state.
When using automatic RF switch control, DO NOT change the pin mode of rxEn or txEn from Arduino sketch!
\param rxEn RX enable pin.
\param txEn TX enable pin.
*/
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
/*!
\brief Get one truly random byte from RSSI noise.
\returns TRNG byte.
*/
uint8_t randomByte();
/*!
\brief Read version SPI register. Should return CC1101_VERSION_LEGACY (0x04) or CC1101_VERSION_CURRENT (0x14) if CC1101 is connected and working.
\returns Version register contents or \ref status_codes
*/
int16_t getChipVersion();
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
/*!
\brief Set interrupt service routine function to call when data bit is receveid in direct mode.
\param func Pointer to interrupt service routine.
*/
void setDirectAction(void (*func)(void));
/*!
\brief Function to read and process data bit in direct reception mode.
\param pin Pin on which to read.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
#endif
/*!
\brief Configure DIO pin mapping to get a given signal on a DIO pin (if available).
\param pin Pin number onto which a signal is to be placed.
\param value The value that indicates which function to place on that pin. See chip datasheet for details.
\returns \ref status_codes
*/
int16_t setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value);
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)
protected:
#endif
Module* _mod;
// SPI read overrides to set bit for burst write and status registers access
int16_t SPIgetRegValue(uint8_t reg, uint8_t msb = 7, uint8_t lsb = 0);
int16_t SPIsetRegValue(uint8_t reg, uint8_t value, uint8_t msb = 7, uint8_t lsb = 0, uint8_t checkInterval = 2);
void SPIreadRegisterBurst(uint8_t reg, uint8_t numBytes, uint8_t* inBytes);
uint8_t SPIreadRegister(uint8_t reg);
void SPIwriteRegisterBurst(uint8_t reg, uint8_t* data, size_t len);
void SPIwriteRegister(uint8_t reg, uint8_t data);
void SPIsendCommand(uint8_t cmd);
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
float _freq = 0;
float _br = 0;
uint8_t _rawRSSI = 0;
uint8_t _rawLQI = 0;
uint8_t _modulation = RADIOLIB_CC1101_MOD_FORMAT_2_FSK;
size_t _packetLength = 0;
bool _packetLengthQueried = false;
uint8_t _packetLengthConfig = RADIOLIB_CC1101_LENGTH_CONFIG_VARIABLE;
bool _promiscuous = false;
bool _crcOn = true;
bool _directMode = true;
uint8_t _syncWordLength = 2;
int8_t _power = 0;
int16_t config();
int16_t transmitDirect(bool sync, uint32_t frf);
int16_t receiveDirect(bool sync);
int16_t directMode(bool sync);
static void getExpMant(float target, uint16_t mantOffset, uint8_t divExp, uint8_t expMax, uint8_t& exp, uint8_t& mant);
int16_t setPacketMode(uint8_t mode, uint16_t len);
};
#endif
@@ -0,0 +1,55 @@
#include "LLCC68.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
LLCC68::LLCC68(Module* mod) : SX1262(mod) {
}
int16_t LLCC68::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
// execute common part
int16_t state = SX126x::begin(cr, syncWord, preambleLength, tcxoVoltage, useRegulatorLDO);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = SX126x::fixPaClamping();
RADIOLIB_ASSERT(state);
return(state);
}
int16_t LLCC68::setBandwidth(float bw) {
RADIOLIB_CHECK_RANGE(bw, 100.0, 510.0, RADIOLIB_ERR_INVALID_BANDWIDTH);
return(SX1262::setBandwidth(bw));
}
int16_t LLCC68::setSpreadingFactor(uint8_t sf) {
switch(SX126x::_bw) {
case RADIOLIB_SX126X_LORA_BW_125_0:
RADIOLIB_CHECK_RANGE(sf, 5, 9, RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
break;
case RADIOLIB_SX126X_LORA_BW_250_0:
RADIOLIB_CHECK_RANGE(sf, 5, 10, RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
break;
case RADIOLIB_SX126X_LORA_BW_500_0:
RADIOLIB_CHECK_RANGE(sf, 5, 11, RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
break;
default:
return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
}
return(SX1262::setSpreadingFactor(sf));
}
#endif
+76
View File
@@ -0,0 +1,76 @@
#if !defined(_RADIOLIB_LLCC68_H)
#define _RADIOLIB_LLCC68_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
#include "../../Module.h"
#include "../SX126x/SX1262.h"
/*!
\class LLCC68
\brief Derived class for %LLCC68 modules.
*/
class LLCC68: public SX1262 {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
LLCC68(Module* mod);
/*!
\brief Initialization method for LoRa modem.
\param freq Carrier frequency in MHz. Defaults to 434.0 MHz.
\param bw LoRa bandwidth in kHz. Defaults to 125.0 kHz.
\param sf LoRa spreading factor. Defaults to 9.
\param cr LoRa coding rate denominator. Defaults to 7 (coding rate 4/7).
\param syncWord 2-byte LoRa sync word. Defaults to RADIOLIB_SX126X_SYNC_WORD_PRIVATE (0x12).
\param power Output power in dBm. Defaults to 10 dBm.
\param preambleLength LoRa preamble length in symbols. Defaults to 8 symbols.
\param tcxoVoltage TCXO reference voltage to be set on DIO3. Defaults to 1.6 V, set to 0 to skip.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX126X_SYNC_WORD_PRIVATE, int8_t power = 10, uint16_t preambleLength = 8, float tcxoVoltage = 1.6, bool useRegulatorLDO = false);
// configuration methods
/*!
\brief Sets LoRa bandwidth. Allowed values are 125.0, 250.0 and 500.0 kHz.
\param bw LoRa bandwidth to be set in kHz.
\returns \ref status_codes
*/
int16_t setBandwidth(float bw);
/*!
\brief Sets LoRa spreading factor. Allowed values range from 5 to 11, depending on currently set spreading factor.
\param sf LoRa spreading factor to be set.
\returns \ref status_codes
*/
int16_t setSpreadingFactor(uint8_t sf);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+21
View File
@@ -0,0 +1,21 @@
#if !defined(_RADIOLIB_RFM22_H)
#define _RADIOLIB_RFM22_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_RFM2X)
#include "../../Module.h"
#include "../Si443x/Si443x.h"
#include "../Si443x/Si4432.h"
/*!
\class RFM22
\brief Only exists as alias for Si4432, since there seems to be no difference between %RFM22 and %Si4432 modules.
*/
RADIOLIB_TYPE_ALIAS(Si4432, RFM22);
#endif
#endif
+21
View File
@@ -0,0 +1,21 @@
#if !defined(_RADIOLIB_RFM23_H)
#define _RADIOLIB_RFM23_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_RFM2X)
#include "../../Module.h"
#include "../Si443x/Si443x.h"
#include "../Si443x/Si4431.h"
/*!
\class RFM23
\brief Only exists as alias for Si4431, since there seems to be no difference between %RFM23 and %Si4431 modules.
*/
RADIOLIB_TYPE_ALIAS(Si4431, RFM23);
#endif
#endif
+90
View File
@@ -0,0 +1,90 @@
#include "RFM95.h"
#if !defined(RADIOLIB_EXCLUDE_RFM9X)
RFM95::RFM95(Module* mod) : SX1278(mod) {
}
int16_t RFM95::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, syncWord, preambleLength);
if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) {
// SX127X_REG_VERSION might be set 0x12
state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
} else if(state != RADIOLIB_ERR_NONE) {
// some other error
return(state);
}
RADIOLIB_DEBUG_PRINTLN(F("M\tSX1278"));
RADIOLIB_DEBUG_PRINTLN(F("M\tRFM95"));
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
return(state);
}
int16_t RFM95::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
// execute common part
int16_t state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, br, freqDev, rxBw, preambleLength, enableOOK);
if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) {
// SX127X_REG_VERSION might be set 0x12
state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, br, freqDev, rxBw, preambleLength, enableOOK);
RADIOLIB_ASSERT(state);
} else if(state != RADIOLIB_ERR_NONE) {
// some other error
return(state);
}
RADIOLIB_DEBUG_PRINTLN(F("M\tSX1278"));
RADIOLIB_DEBUG_PRINTLN(F("M\tRFM95"));
// configure settings not accessible by API
state = configFSK();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
if(enableOOK) {
state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setDataShaping(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
}
return(state);
}
int16_t RFM95::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 862.0, 1020.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_freq = freq;
}
return(state);
}
#endif
+101
View File
@@ -0,0 +1,101 @@
#if !defined(_RADIOLIB_RFM95_H)
#define _RADIOLIB_RFM95_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_RFM9X)
#include "../../Module.h"
#include "../SX127x/SX127x.h"
#include "../SX127x/SX1278.h"
// SX127X_REG_VERSION
#define RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL 0x11
#define RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL 0x12 // according to datasheet, only 0x11 should be possible, but some modules seem to have 0x12
/*!
\class RFM95
\brief Derived class for %RFM95 modules. Overrides some methods from SX1278 due to different parameter ranges.
*/
class RFM95: public SX1278 {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
RFM95(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 868.0 MHz to 915.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 12.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 915.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
/*!
\brief FSK modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 525.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz.
Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met.
\param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of FSK preamble in bits.
\param enableOOK Use OOK modulation instead of FSK.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 868.0 MHz to 915.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
+91
View File
@@ -0,0 +1,91 @@
#include "RFM96.h"
#if !defined(RADIOLIB_EXCLUDE_RFM9X)
RFM96::RFM96(Module* mod) : SX1278(mod) {
}
int16_t RFM96::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, syncWord, preambleLength);
if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) {
// SX127X_REG_VERSION might be set 0x12
state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
} else if(state != RADIOLIB_ERR_NONE) {
// some other error
return(state);
}
RADIOLIB_DEBUG_PRINTLN(F("M\tSX1278"));
RADIOLIB_DEBUG_PRINTLN(F("M\tRFM96"));
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t RFM96::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
// execute common part
int16_t state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, br, freqDev, rxBw, preambleLength, enableOOK);
if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) {
// SX127X_REG_VERSION might be set 0x12
state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, br, freqDev, rxBw, preambleLength, enableOOK);
RADIOLIB_ASSERT(state);
} else if(state != RADIOLIB_ERR_NONE) {
// some other error
return(state);
}
RADIOLIB_DEBUG_PRINTLN(F("M\tSX1278"));
RADIOLIB_DEBUG_PRINTLN(F("M\tRFM96"));
// configure settings not accessible by API
state = configFSK();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
if(enableOOK) {
state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setDataShaping(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
}
return(state);
}
int16_t RFM96::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 410.0, 525.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_freq = freq;
}
return(state);
}
#endif
+108
View File
@@ -0,0 +1,108 @@
#if !defined(_RADIOLIB_RFM96_H)
#define _RADIOLIB_RFM96_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_RFM9X)
#include "../../Module.h"
#include "../SX127x/SX127x.h"
#include "../SX127x/SX1278.h"
// SX127X_REG_VERSION
#define RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL 0x11
#define RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL 0x12 // according to datasheet, only 0x11 should be possible, but some modules seem to have 0x12
/*!
\class RFM96
\brief Derived class for %RFM96 modules. Overrides some methods from SX1278 due to different parameter ranges.
*/
class RFM96: public SX1278 {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
RFM96(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 433.0 MHz to 470.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 12.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
/*!
\brief FSK modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 525.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz.
Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met.
\param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of FSK preamble in bits.
\param enableOOK Use OOK modulation instead of FSK.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 433.0 MHz to 470.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
/*!
\class RFM98
\brief Only exists as alias for RFM96, since there seems to be no difference between %RFM96 and %RFM98 modules.
*/
RADIOLIB_TYPE_ALIAS(RFM96, RFM98);
#endif
#endif
+42
View File
@@ -0,0 +1,42 @@
#include "RFM97.h"
#if !defined(RADIOLIB_EXCLUDE_RFM9X)
RFM97::RFM97(Module* mod) : RFM95(mod) {
};
int16_t RFM97::setSpreadingFactor(uint8_t sf) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
uint8_t newSpreadingFactor;
// check allowed spreading factor values
switch(sf) {
case 6:
newSpreadingFactor = RADIOLIB_SX127X_SF_6;
break;
case 7:
newSpreadingFactor = RADIOLIB_SX127X_SF_7;
break;
case 8:
newSpreadingFactor = RADIOLIB_SX127X_SF_8;
break;
case 9:
newSpreadingFactor = RADIOLIB_SX127X_SF_9;
break;
default:
return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
}
// set spreading factor and if successful, save the new setting
int16_t state = SX1278::setSpreadingFactorRaw(newSpreadingFactor);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_sf = sf;
}
return(state);
}
#endif
+49
View File
@@ -0,0 +1,49 @@
#if !defined(_RADIOLIB_RFM97_H)
#define _RADIOLIB_RFM97_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_RFM9X)
#include "../../Module.h"
#include "../SX127x/SX127x.h"
#include "../SX127x/SX1278.h"
#include "RFM95.h"
/*!
\class RFM97
\brief Derived class for %RFM97 modules. Overrides some methods from RFM95 due to different parameter ranges.
*/
class RFM97: public RFM95 {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
RFM97(Module* mod);
// configuration methods
/*!
\brief Sets %LoRa link spreading factor. Allowed values range from 6 to 9. Only available in %LoRa mode.
\param sf %LoRa link spreading factor to be set.
\returns \ref status_codes
*/
int16_t setSpreadingFactor(uint8_t sf);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
+101
View File
@@ -0,0 +1,101 @@
#include "SX1231.h"
#if !defined(RADIOLIB_EXCLUDE_SX1231)
SX1231::SX1231(Module* mod) : RF69(mod) {
}
int16_t SX1231::begin(float freq, float br, float freqDev, float rxBw, int8_t power, uint8_t preambleLen) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getRst(), OUTPUT);
// try to find the SX1231 chip
uint8_t i = 0;
bool flagFound = false;
while((i < 10) && !flagFound) {
int16_t version = getChipVersion();
if((version == 0x21) || (version == 0x22) || (version == 0x23)) {
flagFound = true;
_chipRevision = version;
} else {
#if defined(RADIOLIB_DEBUG)
RADIOLIB_DEBUG_PRINT(F("SX1231 not found! ("));
RADIOLIB_DEBUG_PRINT(i + 1);
RADIOLIB_DEBUG_PRINT(F(" of 10 tries) RF69_REG_VERSION == "));
char buffHex[12];
sprintf(buffHex, "0x%04X", version);
RADIOLIB_DEBUG_PRINT(buffHex);
RADIOLIB_DEBUG_PRINT(F(", expected 0x0021 / 0x0022 / 0x0023"));
RADIOLIB_DEBUG_PRINTLN();
#endif
_mod->delay(10);
i++;
}
}
if(!flagFound) {
RADIOLIB_DEBUG_PRINTLN(F("No SX1231 found!"));
_mod->term();
return(RADIOLIB_ERR_CHIP_NOT_FOUND);
}
RADIOLIB_DEBUG_PRINTLN(F("M\tSX1231"));
// configure settings not accessible by API
int16_t state = config();
RADIOLIB_ASSERT(state);
RADIOLIB_DEBUG_PRINTLN(F("M\tRF69"));
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
// configure bitrate
_rxBw = 125.0;
state = setBitRate(br);
RADIOLIB_ASSERT(state);
// configure default RX bandwidth
state = setRxBandwidth(rxBw);
RADIOLIB_ASSERT(state);
// configure default frequency deviation
state = setFrequencyDeviation(freqDev);
RADIOLIB_ASSERT(state);
// configure default TX output power
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
// configure default preamble length
state = setPreambleLength(preambleLen);
RADIOLIB_ASSERT(state);
// default sync word values 0x2D01 is the same as the default in LowPowerLab RFM69 library
uint8_t syncWord[] = {0x2D, 0x01};
state = setSyncWord(syncWord, 2);
RADIOLIB_ASSERT(state);
// set default packet length mode
state = variablePacketLengthMode();
if (state != RADIOLIB_ERR_NONE) {
return(state);
}
// SX1231 V2a only
if(_chipRevision == RADIOLIB_SX1231_CHIP_REVISION_2_A) {
// modify default OOK threshold value
state = _mod->SPIsetRegValue(RADIOLIB_SX1231_REG_TEST_OOK, RADIOLIB_SX1231_OOK_DELTA_THRESHOLD);
RADIOLIB_ASSERT(state);
// enable OCP with 95 mA limit
state = _mod->SPIsetRegValue(RADIOLIB_RF69_REG_OCP, RADIOLIB_RF69_OCP_ON | RADIOLIB_RF69_OCP_TRIM, 4, 0);
RADIOLIB_ASSERT(state);
}
return(RADIOLIB_ERR_NONE);
}
#endif
+130
View File
@@ -0,0 +1,130 @@
#if !defined(_RADIOLIB_SX1231_H)
#define _RADIOLIB_SX1231_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX1231)
#include "../../Module.h"
#include "../RF69/RF69.h"
#define RADIOLIB_SX1231_CHIP_REVISION_2_A 0x21
#define RADIOLIB_SX1231_CHIP_REVISION_2_B 0x22
#define RADIOLIB_SX1231_CHIP_REVISION_2_C 0x23
//SX1231 specific register map
#define RADIOLIB_SX1231_REG_TEST_OOK 0x6E
//SX1231_REG_TEST_OOK
#define RADIOLIB_SX1231_OOK_DELTA_THRESHOLD 0x0C
// SX1231_REG_DIO_MAPPING_1
#define RADIOLIB_SX1231_DIO0_CONT_LOW_BAT 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO0_CONT_MODE_READY 0b11000000 // 7 6
#define RADIOLIB_SX1231_DIO0_CONT_PLL_LOCK 0b00000000 // 7 6
#define RADIOLIB_SX1231_DIO0_CONT_SYNC_ADDRESS 0b00000000 // 7 6
#define RADIOLIB_SX1231_DIO0_CONT_TIMEOUT 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO0_CONT_RSSI 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO0_CONT_MODE_READY 0b11000000 // 7 6
#define RADIOLIB_SX1231_DIO0_CONT_TX_READY 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_LOW_BAT 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_PLL_LOCK 0b11000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_CRC_OK 0b00000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_PAYLOAD_READY 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_SYNC_ADDRESS 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_RSSI 0b11000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_PACKET_SENT 0b00000000 // 7 6
#define RADIOLIB_SX1231_DIO0_PACK_TX_READY 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO1_CONT_LOW_BAT 0b00100000 // 5 4
#define RADIOLIB_SX1231_DIO1_CONT_PLL_LOCK 0b00110000 // 5 4
#define RADIOLIB_SX1231_DIO1_CONT_DCLK 0b00000000 // 5 4
#define RADIOLIB_SX1231_DIO1_CONT_RX_READY 0b00010000 // 5 4
#define RADIOLIB_SX1231_DIO1_CONT_SYNC_ADDRESS 0b00110000 // 5 4
#define RADIOLIB_SX1231_DIO1_CONT_TX_READY 0b00010000 // 5 4
#define RADIOLIB_SX1231_DIO1_PACK_FIFO_LEVEL 0b00000000 // 5 4
#define RADIOLIB_SX1231_DIO1_PACK_FIFO_FULL 0b00010000 // 5 4
#define RADIOLIB_SX1231_DIO1_PACK_FIFO_NOT_EMPTY 0b00100000 // 5 4
#define RADIOLIB_SX1231_DIO1_PACK_PLL_LOCK 0b00110000 // 5 4
#define RADIOLIB_SX1231_DIO1_PACK_TIMEOUT 0b00110000 // 5 4
#define RADIOLIB_SX1231_DIO2_CONT_DATA 0b00000000 // 3 2
#define RADIOLIB_SX1231_DIO2_PACK_FIFO_NOT_EMPTY 0b00000000 // 3 2
#define RADIOLIB_SX1231_DIO2_PACK_LOW_BAT 0b00001000 // 3 2
#define RADIOLIB_SX1231_DIO2_PACK_AUTO_MODE 0b00001100 // 3 2
#define RADIOLIB_SX1231_DIO2_PACK_DATA 0b00000100 // 3 2
#define RADIOLIB_SX1231_DIO3_CONT_AUTO_MODE 0b00000010 // 0 1
#define RADIOLIB_SX1231_DIO3_CONT_RSSI 0b00000000 // 0 1
#define RADIOLIB_SX1231_DIO3_CONT_RX_READY 0b00000001 // 0 1
#define RADIOLIB_SX1231_DIO3_CONT_TIMEOUT 0b00000011 // 0 1
#define RADIOLIB_SX1231_DIO3_CONT_TX_READY 0b00000001 // 0 1
#define RADIOLIB_SX1231_DIO3_PACK_FIFO_FULL 0b00000000 // 0 1
#define RADIOLIB_SX1231_DIO3_PACK_LOW_BAT 0b00000010 // 0 1
#define RADIOLIB_SX1231_DIO3_PACK_PLL_LOCK 0b00000011 // 0 1
#define RADIOLIB_SX1231_DIO3_PACK_RSSI 0b00000001 // 0 1
#define RADIOLIB_SX1231_DIO3_PACK_SYNC_ADDRESSS 0b00000010 // 0 1
#define RADIOLIB_SX1231_DIO3_PACK_TX_READY 0b00000001 // 0 1
// SX1231_REG_DIO_MAPPING_2
#define RADIOLIB_SX1231_DIO4_CONT_LOW_BAT 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO4_CONT_PLL_LOCK 0b11000000 // 7 6
#define RADIOLIB_SX1231_DIO4_CONT_TIMEOUT 0b00000000 // 7 6
#define RADIOLIB_SX1231_DIO4_CONT_RX_READY 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO4_CONT_SYNC_ADDRESS 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO4_CONT_TX_READY 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO4_PACK_LOW_BAT 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO4_PACK_PLL_LOCK 0b11000000 // 7 6
#define RADIOLIB_SX1231_DIO4_PACK_TIMEOUT 0b00000000 // 7 6
#define RADIOLIB_SX1231_DIO4_PACK_RSSI 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO4_PACK_RX_READY 0b10000000 // 7 6
#define RADIOLIB_SX1231_DIO4_PACK_MODE_READY 0b00000000 // 7 6
#define RADIOLIB_SX1231_DIO4_PACK_TX_READY 0b01000000 // 7 6
#define RADIOLIB_SX1231_DIO5_CONT_LOW_BAT 0b00100000 // 5 4
#define RADIOLIB_SX1231_DIO5_CONT_MODE_READY 0b00110000 // 5 4
#define RADIOLIB_SX1231_DIO5_CONT_CLK_OUT 0b00000000 // 5 4
#define RADIOLIB_SX1231_DIO5_CONT_RSSI 0b00010000 // 5 4
#define RADIOLIB_SX1231_DIO5_PACK_LOW_BAT 0b00100000 // 5 4
#define RADIOLIB_SX1231_DIO5_PACK_MODE_READY 0b00110000 // 5 4
#define RADIOLIB_SX1231_DIO5_PACK_CLK_OUT 0b00000000 // 5 4
#define RADIOLIB_SX1231_DIO5_PACK_DATA 0b00010000 // 5 4
/*!
\class SX1231
\brief Control class for %SX1231 module. Overrides some methods from RF69 due to different register values.
*/
class SX1231: public RF69 {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX1231(Module* mod);
/*!
\brief Initialization method.
\param freq Carrier frequency in MHz. Defaults to 434.0 MHz.
\param br Bit rate to be used in kbps. Defaults to 4.8 kbps.
\param freqDev Frequency deviation from carrier frequency in kHz Defaults to 5.0 kHz.
\param rxBw Receiver bandwidth in kHz. Defaults to 125.0 kHz.
\param power Output power in dBm. Defaults to 10 dBm.
\param preambleLen Preamble Length in bits. Defaults to 16 bits.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint8_t preambleLen = 16);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
uint8_t _chipRevision = 0;
};
#endif
#endif
@@ -0,0 +1,29 @@
#include "SX1261.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
SX1261::SX1261(Module* mod): SX1262(mod) {
}
int16_t SX1261::setOutputPower(int8_t power) {
RADIOLIB_CHECK_RANGE(power, -17, 14, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
// get current OCP configuration
uint8_t ocp = 0;
int16_t state = readRegister(RADIOLIB_SX126X_REG_OCP_CONFIGURATION, &ocp, 1);
RADIOLIB_ASSERT(state);
// set PA config
state = SX126x::setPaConfig(0x04, RADIOLIB_SX126X_PA_CONFIG_SX1261, 0x00);
RADIOLIB_ASSERT(state);
// set output power
/// \todo power ramp time configuration
state = SX126x::setTxParams(power);
RADIOLIB_ASSERT(state);
// restore OCP configuration
return(writeRegister(RADIOLIB_SX126X_REG_OCP_CONFIGURATION, &ocp, 1));
}
#endif
+46
View File
@@ -0,0 +1,46 @@
#if !defined(_RADIOLIB_SX1261_H)
#define _RADIOLIB_SX1261_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
#include "../../Module.h"
#include "SX126x.h"
#include "SX1262.h"
//RADIOLIB_SX126X_CMD_SET_PA_CONFIG
#define RADIOLIB_SX126X_PA_CONFIG_SX1261 0x01
/*!
\class SX1261
\brief Derived class for %SX1261 modules.
*/
class SX1261 : public SX1262 {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX1261(Module* mod);
/*!
\brief Sets output power. Allowed values are in range from -17 to 14 dBm.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
+101
View File
@@ -0,0 +1,101 @@
#include "SX1262.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
SX1262::SX1262(Module* mod) : SX126x(mod) {
}
int16_t SX1262::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
// execute common part
int16_t state = SX126x::begin(cr, syncWord, preambleLength, tcxoVoltage, useRegulatorLDO);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = SX126x::fixPaClamping();
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1262::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
// execute common part
int16_t state = SX126x::beginFSK(br, freqDev, rxBw, preambleLength, tcxoVoltage, useRegulatorLDO);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = SX126x::fixPaClamping();
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1262::setFrequency(float freq, bool calibrate) {
RADIOLIB_CHECK_RANGE(freq, 150.0, 960.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// calibrate image
if(calibrate) {
uint8_t data[2];
if(freq > 900.0) {
data[0] = RADIOLIB_SX126X_CAL_IMG_902_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_902_MHZ_2;
} else if(freq > 850.0) {
data[0] = RADIOLIB_SX126X_CAL_IMG_863_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_863_MHZ_2;
} else if(freq > 770.0) {
data[0] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_2;
} else if(freq > 460.0) {
data[0] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_2;
} else {
data[0] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_2;
}
int16_t state = SX126x::calibrateImage(data);
RADIOLIB_ASSERT(state);
}
// set frequency
return(SX126x::setFrequencyRaw(freq));
}
int16_t SX1262::setOutputPower(int8_t power) {
RADIOLIB_CHECK_RANGE(power, -17, 22, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
// get current OCP configuration
uint8_t ocp = 0;
int16_t state = readRegister(RADIOLIB_SX126X_REG_OCP_CONFIGURATION, &ocp, 1);
RADIOLIB_ASSERT(state);
// set PA config
state = SX126x::setPaConfig(0x04, RADIOLIB_SX126X_PA_CONFIG_SX1262);
RADIOLIB_ASSERT(state);
// set output power
/// \todo power ramp time configuration
state = SX126x::setTxParams(power);
RADIOLIB_ASSERT(state);
// restore OCP configuration
return(writeRegister(RADIOLIB_SX126X_REG_OCP_CONFIGURATION, &ocp, 1));
}
#endif
+106
View File
@@ -0,0 +1,106 @@
#if !defined(_RADIOLIB_SX1262_H)
#define _RADIOLIB_SX1262_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
#include "../../Module.h"
#include "SX126x.h"
//RADIOLIB_SX126X_CMD_SET_PA_CONFIG
#define RADIOLIB_SX126X_PA_CONFIG_SX1262 0x00
/*!
\class SX1262
\brief Derived class for %SX1262 modules.
*/
class SX1262: public SX126x {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX1262(Module* mod);
// basic methods
/*!
\brief Initialization method for LoRa modem.
\param freq Carrier frequency in MHz. Defaults to 434.0 MHz.
\param bw LoRa bandwidth in kHz. Defaults to 125.0 kHz.
\param sf LoRa spreading factor. Defaults to 9.
\param cr LoRa coding rate denominator. Defaults to 7 (coding rate 4/7).
\param syncWord 2-byte LoRa sync word. Defaults to RADIOLIB_SX126X_SYNC_WORD_PRIVATE (0x12).
\param power Output power in dBm. Defaults to 10 dBm.
\param preambleLength LoRa preamble length in symbols. Defaults to 8 symbols.
\param tcxoVoltage TCXO reference voltage to be set on DIO3. Defaults to 1.6 V, set to 0 to skip.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX126X_SYNC_WORD_PRIVATE, int8_t power = 10, uint16_t preambleLength = 8, float tcxoVoltage = 1.6, bool useRegulatorLDO = false);
/*!
\brief Initialization method for FSK modem.
\param freq Carrier frequency in MHz. Defaults to 434.0 MHz.
\param br FSK bit rate in kbps. Defaults to 4.8 kbps.
\param freqDev Frequency deviation from carrier frequency in kHz. Defaults to 5.0 kHz.
\param rxBw Receiver bandwidth in kHz. Defaults to 156.2 kHz.
\param power Output power in dBm. Defaults to 10 dBm.
\parma preambleLength FSK preamble length in bits. Defaults to 16 bits.
\param tcxoVoltage TCXO reference voltage to be set on DIO3. Defaults to 1.6 V, set to 0 to skip.
\param useRegulatorLDO Whether to use only LDO regulator (true) or DC-DC regulator (false). Defaults to false.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 156.2, int8_t power = 10, uint16_t preambleLength = 16, float tcxoVoltage = 1.6, bool useRegulatorLDO = false);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values are in range from 150.0 to 960.0 MHz.
\param freq Carrier frequency to be set in MHz.
\param calibrate Run image calibration.
\returns \ref status_codes
*/
int16_t setFrequency(float freq, bool calibrate = true);
/*!
\brief Sets output power. Allowed values are in range from -17 to 22 dBm.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
@@ -0,0 +1,96 @@
#include "SX1268.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
SX1268::SX1268(Module* mod) : SX126x(mod) {
}
int16_t SX1268::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
// execute common part
int16_t state = SX126x::begin(cr, syncWord, preambleLength, tcxoVoltage, useRegulatorLDO);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = SX126x::fixPaClamping();
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1268::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
// execute common part
int16_t state = SX126x::beginFSK(br, freqDev, rxBw, preambleLength, tcxoVoltage, useRegulatorLDO);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = SX126x::fixPaClamping();
RADIOLIB_ASSERT(state);
return(state);
}
/// \todo integers only (all modules - frequency, data rate, bandwidth etc.)
int16_t SX1268::setFrequency(float freq, bool calibrate) {
RADIOLIB_CHECK_RANGE(freq, 410.0, 810.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// calibrate image
if(calibrate) {
uint8_t data[2];
if(freq > 770.0) {
data[0] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_2;
} else if(freq > 460.0) {
data[0] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_2;
} else {
data[0] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_1;
data[1] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_2;
}
int16_t state = SX126x::calibrateImage(data);
RADIOLIB_ASSERT(state);
}
// set frequency
return(SX126x::setFrequencyRaw(freq));
}
int16_t SX1268::setOutputPower(int8_t power) {
RADIOLIB_CHECK_RANGE(power, -9, 22, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
// get current OCP configuration
uint8_t ocp = 0;
int16_t state = readRegister(RADIOLIB_SX126X_REG_OCP_CONFIGURATION, &ocp, 1);
RADIOLIB_ASSERT(state);
// set PA config
state = SX126x::setPaConfig(0x04, RADIOLIB_SX126X_PA_CONFIG_SX1268);
RADIOLIB_ASSERT(state);
// set output power
/// \todo power ramp time configuration
state = SX126x::setTxParams(power);
RADIOLIB_ASSERT(state);
// restore OCP configuration
return(writeRegister(RADIOLIB_SX126X_REG_OCP_CONFIGURATION, &ocp, 1));
}
#endif
+106
View File
@@ -0,0 +1,106 @@
#if !defined(_RADIOLIB_SX1268_H)
#define _RADIOLIB_SX1268_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX126X)
#include "../../Module.h"
#include "SX126x.h"
//RADIOLIB_SX126X_CMD_SET_PA_CONFIG
#define RADIOLIB_SX126X_PA_CONFIG_SX1268 0x00
/*!
\class SX1268
\brief Derived class for %SX1268 modules.
*/
class SX1268: public SX126x {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX1268(Module* mod);
// basic methods
/*!
\brief Initialization method for LoRa modem.
\param freq Carrier frequency in MHz. Defaults to 434.0 MHz.
\param bw LoRa bandwidth in kHz. Defaults to 125.0 kHz.
\param sf LoRa spreading factor. Defaults to 9.
\param cr LoRa coding rate denominator. Defaults to 7 (coding rate 4/7).
\param syncWord 2-byte LoRa sync word. Defaults to RADIOLIB_SX126X_SYNC_WORD_PRIVATE (0x12).
\param power Output power in dBm. Defaults to 10 dBm.
\param preambleLength LoRa preamble length in symbols. Defaults to 8 symbols.
\param tcxoVoltage TCXO reference voltage to be set on DIO3. Defaults to 1.6 V, set to 0 to skip.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX126X_SYNC_WORD_PRIVATE, int8_t power = 10, uint16_t preambleLength = 8, float tcxoVoltage = 1.6, bool useRegulatorLDO = false);
/*!
\brief Initialization method for FSK modem.
\param freq Carrier frequency in MHz. Defaults to 434.0 MHz.
\param br FSK bit rate in kbps. Defaults to 4.8 kbps.
\param freqDev Frequency deviation from carrier frequency in kHz. Defaults to 5.0 kHz.
\param rxBw Receiver bandwidth in kHz. Defaults to 156.2 kHz.
\param power Output power in dBm. Defaults to 10 dBm.
\parma preambleLength FSK preamble length in bits. Defaults to 16 bits.
\param tcxoVoltage TCXO reference voltage to be set on DIO3. Defaults to 1.6 V, set to 0 to skip.
\param useRegulatorLDO Whether to use only LDO regulator (true) or DC-DC regulator (false). Defaults to false.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 156.2, int8_t power = 10, uint16_t preambleLength = 16, float tcxoVoltage = 1.6, bool useRegulatorLDO = false);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values are in range from 410.0 to 810.0 MHz.
\param freq Carrier frequency to be set in MHz.
\param calibrate Run image calibration.
\returns \ref status_codes
*/
int16_t setFrequency(float freq, bool calibrate = true);
/*!
\brief Sets output power. Allowed values are in range from -9 to 22 dBm.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+513
View File
@@ -0,0 +1,513 @@
#include "SX1272.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1272::SX1272(Module* mod) : SX127x(mod) {
}
int16_t SX1272::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_SX1272_CHIP_VERSION, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1272::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
// execute common part
int16_t state = SX127x::beginFSK(RADIOLIB_SX1272_CHIP_VERSION, br, freqDev, rxBw, preambleLength, enableOOK);
RADIOLIB_ASSERT(state);
// configure settings not accessible by API
state = configFSK();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
if(enableOOK) {
state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setDataShaping(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
}
return(state);
}
void SX1272::reset() {
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(5);
}
int16_t SX1272::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 860.0, 1020.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_freq = freq;
}
return(state);
}
int16_t SX1272::setBandwidth(float bw) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
uint8_t newBandwidth;
// check allowed bandwidth values
if(fabs(bw - 125.0) <= 0.001) {
newBandwidth = RADIOLIB_SX1272_BW_125_00_KHZ;
} else if(fabs(bw - 250.0) <= 0.001) {
newBandwidth = RADIOLIB_SX1272_BW_250_00_KHZ;
} else if(fabs(bw - 500.0) <= 0.001) {
newBandwidth = RADIOLIB_SX1272_BW_500_00_KHZ;
} else {
return(RADIOLIB_ERR_INVALID_BANDWIDTH);
}
// set bandwidth and if successful, save the new setting
int16_t state = SX1272::setBandwidthRaw(newBandwidth);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_bw = bw;
// calculate symbol length and set low data rate optimization, if auto-configuration is enabled
if(_ldroAuto) {
float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw;
RADIOLIB_DEBUG_PRINT("Symbol length: ");
RADIOLIB_DEBUG_PRINT(symbolLength);
RADIOLIB_DEBUG_PRINTLN(" ms");
if(symbolLength >= 16.0) {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_LOW_DATA_RATE_OPT_ON, 0, 0);
} else {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_LOW_DATA_RATE_OPT_OFF, 0, 0);
}
}
}
return(state);
}
int16_t SX1272::setSpreadingFactor(uint8_t sf) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
uint8_t newSpreadingFactor;
// check allowed spreading factor values
switch(sf) {
case 6:
newSpreadingFactor = RADIOLIB_SX127X_SF_6;
break;
case 7:
newSpreadingFactor = RADIOLIB_SX127X_SF_7;
break;
case 8:
newSpreadingFactor = RADIOLIB_SX127X_SF_8;
break;
case 9:
newSpreadingFactor = RADIOLIB_SX127X_SF_9;
break;
case 10:
newSpreadingFactor = RADIOLIB_SX127X_SF_10;
break;
case 11:
newSpreadingFactor = RADIOLIB_SX127X_SF_11;
break;
case 12:
newSpreadingFactor = RADIOLIB_SX127X_SF_12;
break;
default:
return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
}
// set spreading factor and if successful, save the new setting
int16_t state = SX1272::setSpreadingFactorRaw(newSpreadingFactor);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_sf = sf;
// calculate symbol length and set low data rate optimization, if auto-configuration is enabled
if(_ldroAuto) {
float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw;
RADIOLIB_DEBUG_PRINT("Symbol length: ");
RADIOLIB_DEBUG_PRINT(symbolLength);
RADIOLIB_DEBUG_PRINTLN(" ms");
if(symbolLength >= 16.0) {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_LOW_DATA_RATE_OPT_ON, 0, 0);
} else {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_LOW_DATA_RATE_OPT_OFF, 0, 0);
}
}
}
return(state);
}
int16_t SX1272::setCodingRate(uint8_t cr) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
uint8_t newCodingRate;
// check allowed coding rate values
switch(cr) {
case 5:
newCodingRate = RADIOLIB_SX1272_CR_4_5;
break;
case 6:
newCodingRate = RADIOLIB_SX1272_CR_4_6;
break;
case 7:
newCodingRate = RADIOLIB_SX1272_CR_4_7;
break;
case 8:
newCodingRate = RADIOLIB_SX1272_CR_4_8;
break;
default:
return(RADIOLIB_ERR_INVALID_CODING_RATE);
}
// set coding rate and if successful, save the new setting
int16_t state = SX1272::setCodingRateRaw(newCodingRate);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_cr = cr;
}
return(state);
}
int16_t SX1272::setOutputPower(int8_t power, bool useRfo) {
// check allowed power range
if(useRfo) {
RADIOLIB_CHECK_RANGE(power, -1, 14, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
} else {
RADIOLIB_CHECK_RANGE(power, 2, 20, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
}
// set mode to standby
int16_t state = SX127x::standby();
if(useRfo) {
// RFO output
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX127X_PA_SELECT_RFO, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, (power + 1), 3, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX1272_REG_PA_DAC, RADIOLIB_SX127X_PA_BOOST_OFF, 2, 0);
} else {
if(power <= 17) {
// power is 2 - 17 dBm, enable PA1 + PA2 on PA_BOOST
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX127X_PA_SELECT_BOOST, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, (power - 2), 3, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX1272_REG_PA_DAC, RADIOLIB_SX127X_PA_BOOST_OFF, 2, 0);
} else {
// power is 18 - 20 dBm, enable PA1 + PA2 on PA_BOOST and enable high power control
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX127X_PA_SELECT_BOOST, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, (power - 5), 3, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX1272_REG_PA_DAC, RADIOLIB_SX127X_PA_BOOST_ON, 2, 0);
}
}
return(state);
}
int16_t SX1272::setGain(uint8_t gain) {
// check allowed range
if(gain > 6) {
return(RADIOLIB_ERR_INVALID_GAIN);
}
// set mode to standby
int16_t state = SX127x::standby();
// get modem
int16_t modem = getActiveModem();
if(modem == RADIOLIB_SX127X_LORA){
// set gain
if(gain == 0) {
// gain set to 0, enable AGC loop
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX1272_AGC_AUTO_ON, 2, 2);
} else {
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX1272_AGC_AUTO_OFF, 2, 2);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_LNA, (gain << 5) | RADIOLIB_SX127X_LNA_BOOST_ON);
}
} else if(modem == RADIOLIB_SX127X_FSK_OOK) {
// set gain
if(gain == 0) {
// gain set to 0, enable AGC loop
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RX_CONFIG, RADIOLIB_SX127X_AGC_AUTO_ON, 3, 3);
} else {
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RX_CONFIG, RADIOLIB_SX127X_AGC_AUTO_ON, 3, 3);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_LNA, (gain << 5) | RADIOLIB_SX127X_LNA_BOOST_ON);
}
}
return(state);
}
int16_t SX1272::setDataShaping(uint8_t sh) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_FSK_OOK) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
// check modulation
if(SX127x::_ook) {
return(RADIOLIB_ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
RADIOLIB_ASSERT(state);
// set data shaping
switch(sh) {
case RADIOLIB_SHAPING_NONE:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_OP_MODE, RADIOLIB_SX1272_NO_SHAPING, 4, 3));
case RADIOLIB_SHAPING_0_3:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_OP_MODE, RADIOLIB_SX1272_FSK_GAUSSIAN_0_3, 4, 3));
case RADIOLIB_SHAPING_0_5:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_OP_MODE, RADIOLIB_SX1272_FSK_GAUSSIAN_0_5, 4, 3));
case RADIOLIB_SHAPING_1_0:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_OP_MODE, RADIOLIB_SX1272_FSK_GAUSSIAN_1_0, 4, 3));
default:
return(RADIOLIB_ERR_INVALID_DATA_SHAPING);
}
}
int16_t SX1272::setDataShapingOOK(uint8_t sh) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_FSK_OOK) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
// check modulation
if(!SX127x::_ook) {
return(RADIOLIB_ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
// set data shaping
switch(sh) {
case 0:
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_OP_MODE, RADIOLIB_SX1272_NO_SHAPING, 4, 3);
break;
case 1:
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_OP_MODE, RADIOLIB_SX1272_OOK_FILTER_BR, 4, 3);
break;
case 2:
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_OP_MODE, RADIOLIB_SX1272_OOK_FILTER_2BR, 4, 3);
break;
default:
state = RADIOLIB_ERR_INVALID_DATA_SHAPING;
break;
}
return(state);
}
float SX1272::getRSSI(bool skipReceive) {
if(getActiveModem() == RADIOLIB_SX127X_LORA) {
// RSSI calculation uses different constant for low-frequency and high-frequency ports
float lastPacketRSSI = -139 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE);
// spread-spectrum modulation signal can be received below noise floor
// check last packet SNR and if it's less than 0, add it to reported RSSI to get the correct value
float lastPacketSNR = SX127x::getSNR();
if(lastPacketSNR < 0.0) {
lastPacketRSSI += lastPacketSNR;
}
return(lastPacketRSSI);
} else {
// enable listen mode
if(!skipReceive) {
startReceive();
}
// read the value for FSK
float rssi = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE_FSK) / -2.0;
// set mode back to standby
if(!skipReceive) {
standby();
}
// return the value
return(rssi);
}
}
int16_t SX1272::setCRC(bool enable, bool mode) {
if(getActiveModem() == RADIOLIB_SX127X_LORA) {
// set LoRa CRC
SX127x::_crcEnabled = enable;
if(enable) {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX1272_RX_CRC_MODE_ON, 2, 2));
} else {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX1272_RX_CRC_MODE_OFF, 2, 2));
}
} else {
// set FSK CRC
int16_t state = RADIOLIB_ERR_NONE;
if(enable) {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_ON, 4, 4);
} else {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_OFF, 4, 4);
}
RADIOLIB_ASSERT(state);
// set FSK CRC mode
if(mode) {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_WHITENING_TYPE_IBM, 0, 0));
} else {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_WHITENING_TYPE_CCITT, 0, 0));
}
}
}
int16_t SX1272::forceLDRO(bool enable) {
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
_ldroAuto = false;
if(enable) {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_LOW_DATA_RATE_OPT_ON, 0, 0));
} else {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_LOW_DATA_RATE_OPT_OFF, 0, 0));
}
}
int16_t SX1272::autoLDRO() {
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
_ldroAuto = true;
return(RADIOLIB_ERR_NONE);
}
int16_t SX1272::implicitHeader(size_t len) {
return(setHeaderType(RADIOLIB_SX1272_HEADER_IMPL_MODE, len));
}
int16_t SX1272::explicitHeader() {
return(setHeaderType(RADIOLIB_SX1272_HEADER_EXPL_MODE));
}
int16_t SX1272::setBandwidthRaw(uint8_t newBandwidth) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, newBandwidth, 7, 6);
return(state);
}
int16_t SX1272::setSpreadingFactorRaw(uint8_t newSpreadingFactor) {
// set mode to standby
int16_t state = SX127x::standby();
// write registers
if(newSpreadingFactor == RADIOLIB_SX127X_SF_6) {
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_HEADER_IMPL_MODE | (SX127x::_crcEnabled ? RADIOLIB_SX1272_RX_CRC_MODE_ON : RADIOLIB_SX1272_RX_CRC_MODE_OFF), 2, 1);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX127X_SF_6 | RADIOLIB_SX127X_TX_MODE_SINGLE, 7, 3);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECT_OPTIMIZE, RADIOLIB_SX127X_DETECT_OPTIMIZE_SF_6, 2, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECTION_THRESHOLD, RADIOLIB_SX127X_DETECTION_THRESHOLD_SF_6);
} else {
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1272_HEADER_EXPL_MODE | (SX127x::_crcEnabled ? RADIOLIB_SX1272_RX_CRC_MODE_ON : RADIOLIB_SX1272_RX_CRC_MODE_OFF), 2, 1);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, newSpreadingFactor | RADIOLIB_SX127X_TX_MODE_SINGLE, 7, 3);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECT_OPTIMIZE, RADIOLIB_SX127X_DETECT_OPTIMIZE_SF_7_12, 2, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECTION_THRESHOLD, RADIOLIB_SX127X_DETECTION_THRESHOLD_SF_7_12);
}
return(state);
}
int16_t SX1272::setCodingRateRaw(uint8_t newCodingRate) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, newCodingRate, 5, 3);
return(state);
}
int16_t SX1272::setHeaderType(uint8_t headerType, size_t len) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
// set requested packet mode
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, headerType, 2, 2);
RADIOLIB_ASSERT(state);
// set length to register
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PAYLOAD_LENGTH, len);
RADIOLIB_ASSERT(state);
// update cached value
_packetLength = len;
return(state);
}
int16_t SX1272::configFSK() {
// configure common registers
int16_t state = SX127x::configFSK();
RADIOLIB_ASSERT(state);
// set fast PLL hop
state = _mod->SPIsetRegValue(RADIOLIB_SX1272_REG_PLL_HOP, RADIOLIB_SX127X_FAST_HOP_ON, 7, 7);
return(state);
}
void SX1272::errataFix(bool rx) {
(void)rx;
// mitigation of receiver spurious response
// see SX1272/73 Errata, section 2.2 for details
_mod->SPIsetRegValue(0x31, 0b10000000, 7, 7);
}
#endif
+317
View File
@@ -0,0 +1,317 @@
#if !defined(_RADIOLIB_SX1272_H)
#define _RADIOLIB_SX1272_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
#include "../../Module.h"
#include "SX127x.h"
// SX1272 specific register map
#define RADIOLIB_SX1272_REG_AGC_REF 0x43
#define RADIOLIB_SX1272_REG_AGC_THRESH_1 0x44
#define RADIOLIB_SX1272_REG_AGC_THRESH_2 0x45
#define RADIOLIB_SX1272_REG_AGC_THRESH_3 0x46
#define RADIOLIB_SX1272_REG_PLL_HOP 0x4B
#define RADIOLIB_SX1272_REG_TCXO 0x58
#define RADIOLIB_SX1272_REG_PA_DAC 0x5A
#define RADIOLIB_SX1272_REG_PLL 0x5C
#define RADIOLIB_SX1272_REG_PLL_LOW_PN 0x5E
#define RADIOLIB_SX1272_REG_FORMER_TEMP 0x6C
#define RADIOLIB_SX1272_REG_BIT_RATE_FRAC 0x70
// SX1272 LoRa modem settings
// RADIOLIB_SX1272_REG_FRF_MSB + REG_FRF_MID + REG_FRF_LSB
#define RADIOLIB_SX1272_FRF_MSB 0xE4 // 7 0 carrier frequency setting: f_RF = (F(XOSC) * FRF)/2^19
#define RADIOLIB_SX1272_FRF_MID 0xC0 // 7 0 where F(XOSC) = 32 MHz
#define RADIOLIB_SX1272_FRF_LSB 0x00 // 7 0 FRF = 3 byte value of FRF registers
// RADIOLIB_SX127X_REG_MODEM_CONFIG_1
#define RADIOLIB_SX1272_BW_125_00_KHZ 0b00000000 // 7 6 bandwidth: 125 kHz
#define RADIOLIB_SX1272_BW_250_00_KHZ 0b01000000 // 7 6 250 kHz
#define RADIOLIB_SX1272_BW_500_00_KHZ 0b10000000 // 7 6 500 kHz
#define RADIOLIB_SX1272_CR_4_5 0b00001000 // 5 3 error coding rate: 4/5
#define RADIOLIB_SX1272_CR_4_6 0b00010000 // 5 3 4/6
#define RADIOLIB_SX1272_CR_4_7 0b00011000 // 5 3 4/7
#define RADIOLIB_SX1272_CR_4_8 0b00100000 // 5 3 4/8
#define RADIOLIB_SX1272_HEADER_EXPL_MODE 0b00000000 // 2 2 explicit header mode
#define RADIOLIB_SX1272_HEADER_IMPL_MODE 0b00000100 // 2 2 implicit header mode
#define RADIOLIB_SX1272_RX_CRC_MODE_OFF 0b00000000 // 1 1 CRC disabled
#define RADIOLIB_SX1272_RX_CRC_MODE_ON 0b00000010 // 1 1 CRC enabled
#define RADIOLIB_SX1272_LOW_DATA_RATE_OPT_OFF 0b00000000 // 0 0 low data rate optimization disabled
#define RADIOLIB_SX1272_LOW_DATA_RATE_OPT_ON 0b00000001 // 0 0 low data rate optimization enabled, mandatory for SF 11 and 12 with BW 125 kHz
// RADIOLIB_SX127X_REG_MODEM_CONFIG_2
#define RADIOLIB_SX1272_AGC_AUTO_OFF 0b00000000 // 2 2 LNA gain set by REG_LNA
#define RADIOLIB_SX1272_AGC_AUTO_ON 0b00000100 // 2 2 LNA gain set by internal AGC loop
// RADIOLIB_SX127X_REG_VERSION
#define RADIOLIB_SX1272_CHIP_VERSION 0x22
// SX1272 FSK modem settings
// RADIOLIB_SX127X_REG_OP_MODE
#define RADIOLIB_SX1272_NO_SHAPING 0b00000000 // 4 3 data shaping: no shaping (default)
#define RADIOLIB_SX1272_FSK_GAUSSIAN_1_0 0b00001000 // 4 3 FSK modulation Gaussian filter, BT = 1.0
#define RADIOLIB_SX1272_FSK_GAUSSIAN_0_5 0b00010000 // 4 3 FSK modulation Gaussian filter, BT = 0.5
#define RADIOLIB_SX1272_FSK_GAUSSIAN_0_3 0b00011000 // 4 3 FSK modulation Gaussian filter, BT = 0.3
#define RADIOLIB_SX1272_OOK_FILTER_BR 0b00001000 // 4 3 OOK modulation filter, f_cutoff = BR
#define RADIOLIB_SX1272_OOK_FILTER_2BR 0b00010000 // 4 3 OOK modulation filter, f_cutoff = 2*BR
// RADIOLIB_SX127X_REG_PA_RAMP
#define RADIOLIB_SX1272_LOW_PN_TX_PLL_OFF 0b00010000 // 4 4 use standard PLL in transmit mode (default)
#define RADIOLIB_SX1272_LOW_PN_TX_PLL_ON 0b00000000 // 4 4 use lower phase noise PLL in transmit mode
// RADIOLIB_SX127X_REG_SYNC_CONFIG
#define RADIOLIB_SX1272_FIFO_FILL_CONDITION_SYNC_ADDRESS 0b00000000 // 3 3 FIFO will be filled when sync address interrupt occurs (default)
#define RADIOLIB_SX1272_FIFO_FILL_CONDITION_ALWAYS 0b00001000 // 3 3 FIFO will be filled as long as this bit is set
// RADIOLIB_SX1272_REG_AGC_REF
#define RADIOLIB_SX1272_AGC_REFERENCE_LEVEL 0x13 // 5 0 floor reference for AGC thresholds: AgcRef = -174 + 10*log(2*RxBw) + 8 + AGC_REFERENCE_LEVEL [dBm]
// RADIOLIB_SX1272_REG_AGC_THRESH_1
#define RADIOLIB_SX1272_AGC_STEP_1 0x0E // 4 0 1st AGC threshold
// RADIOLIB_SX1272_REG_AGC_THRESH_2
#define RADIOLIB_SX1272_AGC_STEP_2 0x50 // 7 4 2nd AGC threshold
#define RADIOLIB_SX1272_AGC_STEP_3 0x0B // 4 0 3rd AGC threshold
// RADIOLIB_SX1272_REG_AGC_THRESH_3
#define RADIOLIB_SX1272_AGC_STEP_4 0xD0 // 7 4 4th AGC threshold
#define RADIOLIB_SX1272_AGC_STEP_5 0x0B // 4 0 5th AGC threshold
// RADIOLIB_SX1272_REG_PLL_LOW_PN
#define RADIOLIB_SX1272_PLL_LOW_PN_BANDWIDTH_75_KHZ 0b00000000 // 7 6 low phase noise PLL bandwidth: 75 kHz
#define RADIOLIB_SX1272_PLL_LOW_PN_BANDWIDTH_150_KHZ 0b01000000 // 7 6 150 kHz
#define RADIOLIB_SX1272_PLL_LOW_PN_BANDWIDTH_225_KHZ 0b10000000 // 7 6 225 kHz
#define RADIOLIB_SX1272_PLL_LOW_PN_BANDWIDTH_300_KHZ 0b11000000 // 7 6 300 kHz (default)
/*!
\class SX1272
\brief Derived class for %SX1272 modules. Also used as base class for SX1273.
Both modules use the same basic hardware and only differ in parameter ranges.
*/
class SX1272: public SX127x {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
SX1272(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 860.0 MHz to 1020.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 12.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param currentLimit Trim value for OCP (over current protection) in mA. Can be set to multiplies of 5 in range 45 to 120 mA and to multiples of 10 in range 120 to 240 mA.
Set to 0 to disable OCP (not recommended).
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 915.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
/*!
\brief FSK modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 860.0 MHz to 1020.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz.
Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met.
\param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of FSK preamble in bits.
\param enableOOK Use OOK modulation instead of FSK.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 915.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false);
/*!
\brief Reset method. Will reset the chip to the default state using RST pin.
*/
void reset() override;
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 860.0 MHz to 1020.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
/*!
\brief Sets %LoRa link bandwidth. Allowed values are 125, 250 and 500 kHz. Only available in %LoRa mode.
\param bw %LoRa link bandwidth to be set in kHz.
\returns \ref status_codes
*/
int16_t setBandwidth(float bw);
/*!
\brief Sets %LoRa link spreading factor. Allowed values range from 6 to 12. Only available in %LoRa mode.
\param sf %LoRa link spreading factor to be set.
\returns \ref status_codes
*/
int16_t setSpreadingFactor(uint8_t sf);
/*!
\brief Sets %LoRa link coding rate denominator. Allowed values range from 5 to 8. Only available in %LoRa mode.
\param cr %LoRa link coding rate denominator to be set.
\returns \ref status_codes
*/
int16_t setCodingRate(uint8_t cr);
/*!
\brief Sets transmission output power. Allowed values range from -1 to 14 dBm (RFO pin) or +2 to +20 dBm (PA_BOOST pin).
\param power Transmission output power in dBm.
\param useRfo Whether to use the RFO (true) or the PA_BOOST (false) pin for the RF output. Defaults to PA_BOOST.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power, bool useRfo = false);
/*!
\brief Sets gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended). Only available in %LoRa mode.
\param gain Gain of receiver LNA (low-noise amplifier) to be set.
\returns \ref status_codes
*/
int16_t setGain(uint8_t gain);
/*!
\brief Sets Gaussian filter bandwidth-time product that will be used for data shaping. Only available in FSK mode with FSK modulation.
Allowed values are RADIOLIB_SHAPING_0_3, RADIOLIB_SHAPING_0_5 or RADIOLIB_SHAPING_1_0. Set to RADIOLIB_SHAPING_NONE to disable data shaping.
\param sh Gaussian shaping bandwidth-time product that will be used for data shaping
\returns \ref status_codes
*/
int16_t setDataShaping(uint8_t sh) override;
/*!
\brief Sets filter cutoff frequency that will be used for data shaping.
Allowed values are 1 for frequency equal to bit rate and 2 for frequency equal to 2x bit rate. Set to 0 to disable data shaping.
Only available in FSK mode with OOK modulation.
\param sh Cutoff frequency that will be used for data shaping
\returns \ref status_codes
*/
int16_t setDataShapingOOK(uint8_t sh);
/*!
\brief Gets recorded signal strength indicator of the latest received packet for LoRa modem, or current RSSI level for FSK modem.
\param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FKS/OOK mode.
\returns Last packet RSSI for LoRa modem, or current RSSI level for FSK modem.
*/
float getRSSI(bool skipReceive = false);
/*!
\brief Enables/disables CRC check of received packets.
\param enable Enable (true) or disable (false) CRC.
\param mode Set CRC mode to RADIOLIB_SX127X_CRC_WHITENING_TYPE_CCITT for CCITT, polynomial X16 + X12 + X5 + 1 (false) or RADIOLIB_SX127X_CRC_WHITENING_TYPE_IBM for IBM, polynomial X16 + X15 + X2 + 1 (true). Only valid in FSK mode.
\returns \ref status_codes
*/
int16_t setCRC(bool enable, bool mode = false);
/*!
\brief Forces LoRa low data rate optimization. Only available in LoRa mode. After calling this method, LDRO will always be set to
the provided value, regardless of symbol length. To re-enable automatic LDRO configuration, call SX1278::autoLDRO()
\param enable Force LDRO to be always enabled (true) or disabled (false).
\returns \ref status_codes
*/
int16_t forceLDRO(bool enable);
/*!
\brief Re-enables automatic LDRO configuration. Only available in LoRa mode. After calling this method, LDRO will be enabled automatically
when symbol length exceeds 16 ms.
\returns \ref status_codes
*/
int16_t autoLDRO();
/*!
\brief Set implicit header mode for future reception/transmission. Required for spreading factor 6.
\param len Payload length in bytes.
\returns \ref status_codes
*/
int16_t implicitHeader(size_t len);
/*!
\brief Set explicit header mode for future reception/transmission.
\returns \ref status_codes
*/
int16_t explicitHeader();
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
int16_t setBandwidthRaw(uint8_t newBandwidth);
int16_t setSpreadingFactorRaw(uint8_t newSpreadingFactor);
int16_t setCodingRateRaw(uint8_t newCodingRate);
int16_t setHeaderType(uint8_t headerType, size_t len = 0xFF);
int16_t configFSK();
void errataFix(bool rx);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
bool _ldroAuto = true;
bool _ldroEnabled = false;
};
#endif
#endif
@@ -0,0 +1,65 @@
#include "SX1273.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1273::SX1273(Module* mod) : SX1272(mod) {
}
int16_t SX1273::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_SX1272_CHIP_VERSION, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1273::setSpreadingFactor(uint8_t sf) {
uint8_t newSpreadingFactor;
// check allowed spreading factor values
switch(sf) {
case 6:
newSpreadingFactor = RADIOLIB_SX127X_SF_6;
break;
case 7:
newSpreadingFactor = RADIOLIB_SX127X_SF_7;
break;
case 8:
newSpreadingFactor = RADIOLIB_SX127X_SF_8;
break;
case 9:
newSpreadingFactor = RADIOLIB_SX127X_SF_9;
break;
default:
return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
}
// set spreading factor and if successful, save the new setting
int16_t state = setSpreadingFactorRaw(newSpreadingFactor);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_sf = sf;
}
return(state);
}
#endif
+73
View File
@@ -0,0 +1,73 @@
#if !defined(_RADIOLIB_SX1273_H)
#define _RADIOLIB_SX1273_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
#include "SX1272.h"
/*!
\class SX1273
\brief Derived class for %SX1273 modules. Overrides some methods from SX1272 due to different parameter ranges.
*/
class SX1273: public SX1272 {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
SX1273(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 860.0 MHz to 1020.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 9.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 915.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
// configuration methods
/*!
\brief Sets %LoRa link spreading factor. Allowed values range from 6 to 9. Only available in %LoRa mode.
\param sf %LoRa link spreading factor to be set.
\returns \ref status_codes
*/
int16_t setSpreadingFactor(uint8_t sf);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
@@ -0,0 +1,73 @@
#include "SX1276.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1276::SX1276(Module* mod) : SX1278(mod) {
}
int16_t SX1276::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1276::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
// execute common part
int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, br, freqDev, rxBw, preambleLength, enableOOK);
RADIOLIB_ASSERT(state);
// configure settings not accessible by API
state = configFSK();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
if(enableOOK) {
state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setDataShaping(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
}
return(state);
}
int16_t SX1276::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 137.0, 1020.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_freq = freq;
}
return(state);
}
#endif
+95
View File
@@ -0,0 +1,95 @@
#if !defined(_RADIOLIB_SX1276_H)
#define _RADIOLIB_SX1276_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
#include "SX1278.h"
/*!
\class SX1276
\brief Derived class for %SX1276 modules. Overrides some methods from SX1278 due to different parameter ranges.
*/
class SX1276: public SX1278 {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
SX1276(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 1020.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 12.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
/*!
\brief FSK modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 1020.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz.
Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met.
\param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of FSK preamble in bits.
\param enableOOK Use OOK modulation instead of FSK.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 137.0 MHz to 1020.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
+103
View File
@@ -0,0 +1,103 @@
#include "SX1277.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1277::SX1277(Module* mod) : SX1278(mod) {
}
int16_t SX1277::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1277::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
// execute common part
int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, br, freqDev, rxBw, preambleLength, enableOOK);
RADIOLIB_ASSERT(state);
// configure settings not accessible by API
state = configFSK();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
if(enableOOK) {
state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setDataShaping(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
}
return(state);
}
int16_t SX1277::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 137.0, 1020.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_freq = freq;
}
return(state);
}
int16_t SX1277::setSpreadingFactor(uint8_t sf) {
uint8_t newSpreadingFactor;
// check allowed spreading factor values
switch(sf) {
case 6:
newSpreadingFactor = RADIOLIB_SX127X_SF_6;
break;
case 7:
newSpreadingFactor = RADIOLIB_SX127X_SF_7;
break;
case 8:
newSpreadingFactor = RADIOLIB_SX127X_SF_8;
break;
case 9:
newSpreadingFactor = RADIOLIB_SX127X_SF_9;
break;
default:
return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
}
// set spreading factor and if successful, save the new setting
int16_t state = SX1278::setSpreadingFactorRaw(newSpreadingFactor);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_sf = sf;
}
return(state);
}
#endif
+104
View File
@@ -0,0 +1,104 @@
#if !defined(_RADIOLIB_SX1277_H)
#define _RADIOLIB_SX1277_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
#include "SX1278.h"
/*!
\class SX1277
\brief Derived class for %SX1277 modules. Overrides some methods from SX1278 due to different parameter ranges.
*/
class SX1277: public SX1278 {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
SX1277(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 1020.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 9.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
/*!
\brief FSK modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 525.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz.
Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met.
\param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of FSK preamble in bits.
\param enableOOK Use OOK modulation instead of FSK.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 137.0 MHz to 1020.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
/*!
\brief Sets %LoRa link spreading factor. Allowed values range from 6 to 9. Only available in %LoRa mode.
\param sf %LoRa link spreading factor to be set.
\returns \ref status_codes
*/
int16_t setSpreadingFactor(uint8_t sf);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
+631
View File
@@ -0,0 +1,631 @@
#include "SX1278.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1278::SX1278(Module* mod) : SX127x(mod) {
}
int16_t SX1278::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1278::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
// execute common part
int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, br, freqDev, rxBw, preambleLength, enableOOK);
RADIOLIB_ASSERT(state);
// configure settings not accessible by API
state = configFSK();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
if(enableOOK) {
state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setDataShaping(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
}
return(state);
}
void SX1278::reset() {
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(5);
}
int16_t SX1278::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 137.0, 525.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_freq = freq;
}
return(state);
}
int16_t SX1278::setBandwidth(float bw) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
uint8_t newBandwidth;
// check allowed bandwidth values
if(fabs(bw - 7.8) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_7_80_KHZ;
} else if(fabs(bw - 10.4) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_10_40_KHZ;
} else if(fabs(bw - 15.6) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_15_60_KHZ;
} else if(fabs(bw - 20.8) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_20_80_KHZ;
} else if(fabs(bw - 31.25) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_31_25_KHZ;
} else if(fabs(bw - 41.7) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_41_70_KHZ;
} else if(fabs(bw - 62.5) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_62_50_KHZ;
} else if(fabs(bw - 125.0) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_125_00_KHZ;
} else if(fabs(bw - 250.0) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_250_00_KHZ;
} else if(fabs(bw - 500.0) <= 0.001) {
newBandwidth = RADIOLIB_SX1278_BW_500_00_KHZ;
} else {
return(RADIOLIB_ERR_INVALID_BANDWIDTH);
}
// set bandwidth and if successful, save the new setting
int16_t state = SX1278::setBandwidthRaw(newBandwidth);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_bw = bw;
// calculate symbol length and set low data rate optimization, if auto-configuration is enabled
if(_ldroAuto) {
float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw;
RADIOLIB_DEBUG_PRINT("Symbol length: ");
RADIOLIB_DEBUG_PRINT(symbolLength);
RADIOLIB_DEBUG_PRINTLN(" ms");
if(symbolLength >= 16.0) {
state = _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_LOW_DATA_RATE_OPT_ON, 3, 3);
} else {
state = _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_LOW_DATA_RATE_OPT_OFF, 3, 3);
}
}
}
return(state);
}
int16_t SX1278::setSpreadingFactor(uint8_t sf) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
uint8_t newSpreadingFactor;
// check allowed spreading factor values
switch(sf) {
case 6:
newSpreadingFactor = RADIOLIB_SX127X_SF_6;
break;
case 7:
newSpreadingFactor = RADIOLIB_SX127X_SF_7;
break;
case 8:
newSpreadingFactor = RADIOLIB_SX127X_SF_8;
break;
case 9:
newSpreadingFactor = RADIOLIB_SX127X_SF_9;
break;
case 10:
newSpreadingFactor = RADIOLIB_SX127X_SF_10;
break;
case 11:
newSpreadingFactor = RADIOLIB_SX127X_SF_11;
break;
case 12:
newSpreadingFactor = RADIOLIB_SX127X_SF_12;
break;
default:
return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
}
// set spreading factor and if successful, save the new setting
int16_t state = SX1278::setSpreadingFactorRaw(newSpreadingFactor);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_sf = sf;
// calculate symbol length and set low data rate optimization, if auto-configuration is enabled
if(_ldroAuto) {
float symbolLength = (float)(uint32_t(1) << SX127x::_sf) / (float)SX127x::_bw;
RADIOLIB_DEBUG_PRINT("Symbol length: ");
RADIOLIB_DEBUG_PRINT(symbolLength);
RADIOLIB_DEBUG_PRINTLN(" ms");
if(symbolLength >= 16.0) {
state = _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_LOW_DATA_RATE_OPT_ON, 3, 3);
} else {
state = _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_LOW_DATA_RATE_OPT_OFF, 3, 3);
}
}
}
return(state);
}
int16_t SX1278::setCodingRate(uint8_t cr) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
uint8_t newCodingRate;
// check allowed coding rate values
switch(cr) {
case 5:
newCodingRate = RADIOLIB_SX1278_CR_4_5;
break;
case 6:
newCodingRate = RADIOLIB_SX1278_CR_4_6;
break;
case 7:
newCodingRate = RADIOLIB_SX1278_CR_4_7;
break;
case 8:
newCodingRate = RADIOLIB_SX1278_CR_4_8;
break;
default:
return(RADIOLIB_ERR_INVALID_CODING_RATE);
}
// set coding rate and if successful, save the new setting
int16_t state = SX1278::setCodingRateRaw(newCodingRate);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_cr = cr;
}
return(state);
}
int16_t SX1278::setOutputPower(int8_t power, bool useRfo) {
// check allowed power range
if(useRfo) {
// RFO output
RADIOLIB_CHECK_RANGE(power, -3, 15, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
} else {
// PA_BOOST output, check high-power operation
if(power != 20) {
RADIOLIB_CHECK_RANGE(power, 2, 17, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
}
}
// set mode to standby
int16_t state = SX127x::standby();
if(useRfo) {
uint8_t paCfg = 0;
if(power < 0) {
// low power mode RFO output
paCfg = RADIOLIB_SX1278_LOW_POWER | (power + 3);
} else {
// high power mode RFO output
paCfg = RADIOLIB_SX1278_MAX_POWER | power;
}
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX127X_PA_SELECT_RFO, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, paCfg, 6, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_PA_DAC, RADIOLIB_SX127X_PA_BOOST_OFF, 2, 0);
} else {
if(power != 20) {
// power is 2 - 17 dBm, enable PA1 + PA2 on PA_BOOST
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX127X_PA_SELECT_BOOST, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX1278_MAX_POWER | (power - 2), 6, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_PA_DAC, RADIOLIB_SX127X_PA_BOOST_OFF, 2, 0);
} else {
// power is 20 dBm, enable PA1 + PA2 on PA_BOOST and enable high power control
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX127X_PA_SELECT_BOOST, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_CONFIG, RADIOLIB_SX1278_MAX_POWER | 0x0F, 6, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_PA_DAC, RADIOLIB_SX127X_PA_BOOST_ON, 2, 0);
}
}
return(state);
}
int16_t SX1278::setGain(uint8_t gain) {
// check allowed range
if(gain > 6) {
return(RADIOLIB_ERR_INVALID_GAIN);
}
// set mode to standby
int16_t state = SX127x::standby();
// get modem
int16_t modem = getActiveModem();
if(modem == RADIOLIB_SX127X_LORA){
// set gain
if(gain == 0) {
// gain set to 0, enable AGC loop
state |= _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_AGC_AUTO_ON, 2, 2);
} else {
state |= _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_AGC_AUTO_OFF, 2, 2);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_LNA, (gain << 5) | RADIOLIB_SX127X_LNA_BOOST_ON);
}
} else if(modem == RADIOLIB_SX127X_FSK_OOK) {
// set gain
if(gain == 0) {
// gain set to 0, enable AGC loop
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RX_CONFIG, RADIOLIB_SX127X_AGC_AUTO_ON, 3, 3);
} else {
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RX_CONFIG, RADIOLIB_SX1278_AGC_AUTO_OFF, 3, 3);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_LNA, (gain << 5) | RADIOLIB_SX127X_LNA_BOOST_ON);
}
}
return(state);
}
int16_t SX1278::setDataShaping(uint8_t sh) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_FSK_OOK) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
// check modulation
if(SX127x::_ook) {
// we're in OOK mode, the only thing we can do is disable
if(sh == RADIOLIB_SHAPING_NONE) {
return(setDataShapingOOK(0));
}
return(RADIOLIB_ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
RADIOLIB_ASSERT(state);
// set data shaping
switch(sh) {
case RADIOLIB_SHAPING_NONE:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_RAMP, RADIOLIB_SX1278_NO_SHAPING, 6, 5));
case RADIOLIB_SHAPING_0_3:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_RAMP, RADIOLIB_SX1278_FSK_GAUSSIAN_0_3, 6, 5));
case RADIOLIB_SHAPING_0_5:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_RAMP, RADIOLIB_SX1278_FSK_GAUSSIAN_0_5, 6, 5));
case RADIOLIB_SHAPING_1_0:
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_RAMP, RADIOLIB_SX1278_FSK_GAUSSIAN_1_0, 6, 5));
default:
return(RADIOLIB_ERR_INVALID_DATA_SHAPING);
}
}
int16_t SX1278::setDataShapingOOK(uint8_t sh) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_FSK_OOK) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
// check modulation
if(!SX127x::_ook) {
return(RADIOLIB_ERR_INVALID_MODULATION);
}
// set mode to standby
int16_t state = SX127x::standby();
// set data shaping
switch(sh) {
case 0:
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_RAMP, RADIOLIB_SX1278_NO_SHAPING, 6, 5);
break;
case 1:
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_RAMP, RADIOLIB_SX1278_OOK_FILTER_BR, 6, 5);
break;
case 2:
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PA_RAMP, RADIOLIB_SX1278_OOK_FILTER_2BR, 6, 5);
break;
default:
return(RADIOLIB_ERR_INVALID_DATA_SHAPING);
}
return(state);
}
float SX1278::getRSSI(bool skipReceive) {
if(getActiveModem() == RADIOLIB_SX127X_LORA) {
// for LoRa, get RSSI of the last packet
float lastPacketRSSI;
// RSSI calculation uses different constant for low-frequency and high-frequency ports
if(_freq < 868.0) {
lastPacketRSSI = -164 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE);
} else {
lastPacketRSSI = -157 + _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PKT_RSSI_VALUE);
}
// spread-spectrum modulation signal can be received below noise floor
// check last packet SNR and if it's less than 0, add it to reported RSSI to get the correct value
float lastPacketSNR = SX127x::getSNR();
if(lastPacketSNR < 0.0) {
lastPacketRSSI += lastPacketSNR;
}
return(lastPacketRSSI);
} else {
// enable listen mode
if(!skipReceive) {
startReceive();
}
// read the value for FSK
float rssi = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_RSSI_VALUE_FSK) / -2.0;
// set mode back to standby
if(!skipReceive) {
standby();
}
// return the value
return(rssi);
}
}
int16_t SX1278::setCRC(bool enable, bool mode) {
if(getActiveModem() == RADIOLIB_SX127X_LORA) {
// set LoRa CRC
SX127x::_crcEnabled = enable;
if(enable) {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX1278_RX_CRC_MODE_ON, 2, 2));
} else {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX1278_RX_CRC_MODE_OFF, 2, 2));
}
} else {
// set FSK CRC
int16_t state = RADIOLIB_ERR_NONE;
if(enable) {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_ON, 4, 4);
} else {
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_OFF, 4, 4);
}
RADIOLIB_ASSERT(state);
// set FSK CRC mode
if(mode) {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_WHITENING_TYPE_IBM, 0, 0));
} else {
return(_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, RADIOLIB_SX127X_CRC_WHITENING_TYPE_CCITT, 0, 0));
}
}
}
int16_t SX1278::forceLDRO(bool enable) {
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
_ldroAuto = false;
if(enable) {
return(_mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_LOW_DATA_RATE_OPT_ON, 3, 3));
} else {
return(_mod->SPIsetRegValue(RADIOLIB_SX1278_REG_MODEM_CONFIG_3, RADIOLIB_SX1278_LOW_DATA_RATE_OPT_OFF, 3, 3));
}
}
int16_t SX1278::autoLDRO() {
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
_ldroAuto = true;
return(RADIOLIB_ERR_NONE);
}
int16_t SX1278::implicitHeader(size_t len) {
return(setHeaderType(RADIOLIB_SX1278_HEADER_IMPL_MODE, len));
}
int16_t SX1278::explicitHeader() {
return(setHeaderType(RADIOLIB_SX1278_HEADER_EXPL_MODE));
}
int16_t SX1278::setBandwidthRaw(uint8_t newBandwidth) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, newBandwidth, 7, 4);
return(state);
}
int16_t SX1278::setSpreadingFactorRaw(uint8_t newSpreadingFactor) {
// set mode to standby
int16_t state = SX127x::standby();
// write registers
if(newSpreadingFactor == RADIOLIB_SX127X_SF_6) {
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1278_HEADER_IMPL_MODE, 0, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, RADIOLIB_SX127X_SF_6 | RADIOLIB_SX127X_TX_MODE_SINGLE | (SX127x::_crcEnabled ? RADIOLIB_SX1278_RX_CRC_MODE_ON : RADIOLIB_SX1278_RX_CRC_MODE_OFF), 7, 2);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECT_OPTIMIZE, RADIOLIB_SX127X_DETECT_OPTIMIZE_SF_6, 2, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECTION_THRESHOLD, RADIOLIB_SX127X_DETECTION_THRESHOLD_SF_6);
} else {
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, RADIOLIB_SX1278_HEADER_EXPL_MODE, 0, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, newSpreadingFactor | RADIOLIB_SX127X_TX_MODE_SINGLE | (SX127x::_crcEnabled ? RADIOLIB_SX1278_RX_CRC_MODE_ON : RADIOLIB_SX1278_RX_CRC_MODE_OFF), 7, 2);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECT_OPTIMIZE, RADIOLIB_SX127X_DETECT_OPTIMIZE_SF_7_12, 2, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DETECTION_THRESHOLD, RADIOLIB_SX127X_DETECTION_THRESHOLD_SF_7_12);
}
return(state);
}
int16_t SX1278::setCodingRateRaw(uint8_t newCodingRate) {
// set mode to standby
int16_t state = SX127x::standby();
// write register
state |= _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, newCodingRate, 3, 1);
return(state);
}
int16_t SX1278::setHeaderType(uint8_t headerType, size_t len) {
// check active modem
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
// set requested packet mode
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, headerType, 0, 0);
RADIOLIB_ASSERT(state);
// set length to register
state = _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PAYLOAD_LENGTH, len);
RADIOLIB_ASSERT(state);
// update cached value
_packetLength = len;
return(state);
}
int16_t SX1278::configFSK() {
// configure common registers
int16_t state = SX127x::configFSK();
RADIOLIB_ASSERT(state);
// set fast PLL hop
state = _mod->SPIsetRegValue(RADIOLIB_SX1278_REG_PLL_HOP, RADIOLIB_SX127X_FAST_HOP_ON, 7, 7);
return(state);
}
void SX1278::errataFix(bool rx) {
// only apply in LoRa mode
if(getActiveModem() != RADIOLIB_SX127X_LORA) {
return;
}
// sensitivity optimization for 500kHz bandwidth
// see SX1276/77/78 Errata, section 2.1 for details
if(fabs(_bw - 500.0) <= 0.001) {
if((_freq >= 862.0) && (_freq <= 1020.0)) {
_mod->SPIwriteRegister(0x36, 0x02);
_mod->SPIwriteRegister(0x3a, 0x64);
} else if((_freq >= 410.0) && (_freq <= 525.0)) {
_mod->SPIwriteRegister(0x36, 0x02);
_mod->SPIwriteRegister(0x3a, 0x7F);
}
}
// mitigation of receiver spurious response
// see SX1276/77/78 Errata, section 2.3 for details
// figure out what we need to set
uint8_t fixedRegs[3] = { 0x00, 0x00, 0x00 };
float rxFreq = _freq;
if(fabs(_bw - 7.8) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x48;
fixedRegs[2] = 0x00;
rxFreq += 0.00781;
} else if(fabs(_bw - 10.4) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x44;
fixedRegs[2] = 0x00;
rxFreq += 0.01042;
} else if(fabs(_bw - 15.6) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x44;
fixedRegs[2] = 0x00;
rxFreq += 0.01562;
} else if(fabs(_bw - 20.8) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x44;
fixedRegs[2] = 0x00;
rxFreq += 0.02083;
} else if(fabs(_bw - 31.25) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x44;
fixedRegs[2] = 0x00;
rxFreq += 0.03125;
} else if(fabs(_bw - 41.7) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x44;
fixedRegs[2] = 0x00;
rxFreq += 0.04167;
} else if(fabs(_bw - 62.5) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x40;
fixedRegs[2] = 0x00;
} else if(fabs(_bw - 125.0) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x40;
fixedRegs[2] = 0x00;
} else if(fabs(_bw - 250.0) <= 0.001) {
fixedRegs[0] = 0b0000000;
fixedRegs[1] = 0x40;
fixedRegs[2] = 0x00;
} else if(fabs(_bw - 500.0) <= 0.001) {
fixedRegs[0] = 0b1000000;
fixedRegs[1] = _mod->SPIreadRegister(0x2F);
fixedRegs[2] = _mod->SPIreadRegister(0x30);
} else {
return;
}
// first, go to standby
standby();
// shift the freqency up when receiving, or restore the original when transmitting
if(rx) {
SX127x::setFrequencyRaw(rxFreq);
} else {
SX127x::setFrequencyRaw(_freq);
}
// finally, apply errata fixes
_mod->SPIsetRegValue(0x31, fixedRegs[0], 7, 7);
_mod->SPIsetRegValue(0x2F, fixedRegs[1]);
_mod->SPIsetRegValue(0x30, fixedRegs[2]);
}
#endif
+326
View File
@@ -0,0 +1,326 @@
#if !defined(_RADIOLIB_SX1278_H)
#define _RADIOLIB_SX1278_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
#include "../../Module.h"
#include "SX127x.h"
// SX1278 specific register map
#define RADIOLIB_SX1278_REG_MODEM_CONFIG_3 0x26
#define RADIOLIB_SX1278_REG_PLL_HOP 0x44
#define RADIOLIB_SX1278_REG_TCXO 0x4B
#define RADIOLIB_SX1278_REG_PA_DAC 0x4D
#define RADIOLIB_SX1278_REG_FORMER_TEMP 0x5B
#define RADIOLIB_SX1278_REG_REG_BIT_RATE_FRAC 0x5D
#define RADIOLIB_SX1278_REG_AGC_REF 0x61
#define RADIOLIB_SX1278_REG_AGC_THRESH_1 0x62
#define RADIOLIB_SX1278_REG_AGC_THRESH_2 0x63
#define RADIOLIB_SX1278_REG_AGC_THRESH_3 0x64
#define RADIOLIB_SX1278_REG_PLL 0x70
// SX1278 LoRa modem settings
// RADIOLIB_SX1278_REG_OP_MODE MSB LSB DESCRIPTION
#define RADIOLIB_SX1278_HIGH_FREQ 0b00000000 // 3 3 access HF test registers
#define RADIOLIB_SX1278_LOW_FREQ 0b00001000 // 3 3 access LF test registers
// RADIOLIB_SX1278_REG_FRF_MSB + REG_FRF_MID + REG_FRF_LSB
#define RADIOLIB_SX1278_FRF_MSB 0x6C // 7 0 carrier frequency setting: f_RF = (F(XOSC) * FRF)/2^19
#define RADIOLIB_SX1278_FRF_MID 0x80 // 7 0 where F(XOSC) = 32 MHz
#define RADIOLIB_SX1278_FRF_LSB 0x00 // 7 0 FRF = 3 byte value of FRF registers
// RADIOLIB_SX1278_REG_PA_CONFIG
#define RADIOLIB_SX1278_MAX_POWER 0b01110000 // 6 4 max power: P_max = 10.8 + 0.6*MAX_POWER [dBm]; P_max(MAX_POWER = 0b111) = 15 dBm
#define RADIOLIB_SX1278_LOW_POWER 0b00100000 // 6 4
// RADIOLIB_SX1278_REG_LNA
#define RADIOLIB_SX1278_LNA_BOOST_LF_OFF 0b00000000 // 4 3 default LNA current
// SX127X_REG_MODEM_CONFIG_1
#define RADIOLIB_SX1278_BW_7_80_KHZ 0b00000000 // 7 4 bandwidth: 7.80 kHz
#define RADIOLIB_SX1278_BW_10_40_KHZ 0b00010000 // 7 4 10.40 kHz
#define RADIOLIB_SX1278_BW_15_60_KHZ 0b00100000 // 7 4 15.60 kHz
#define RADIOLIB_SX1278_BW_20_80_KHZ 0b00110000 // 7 4 20.80 kHz
#define RADIOLIB_SX1278_BW_31_25_KHZ 0b01000000 // 7 4 31.25 kHz
#define RADIOLIB_SX1278_BW_41_70_KHZ 0b01010000 // 7 4 41.70 kHz
#define RADIOLIB_SX1278_BW_62_50_KHZ 0b01100000 // 7 4 62.50 kHz
#define RADIOLIB_SX1278_BW_125_00_KHZ 0b01110000 // 7 4 125.00 kHz
#define RADIOLIB_SX1278_BW_250_00_KHZ 0b10000000 // 7 4 250.00 kHz
#define RADIOLIB_SX1278_BW_500_00_KHZ 0b10010000 // 7 4 500.00 kHz
#define RADIOLIB_SX1278_CR_4_5 0b00000010 // 3 1 error coding rate: 4/5
#define RADIOLIB_SX1278_CR_4_6 0b00000100 // 3 1 4/6
#define RADIOLIB_SX1278_CR_4_7 0b00000110 // 3 1 4/7
#define RADIOLIB_SX1278_CR_4_8 0b00001000 // 3 1 4/8
#define RADIOLIB_SX1278_HEADER_EXPL_MODE 0b00000000 // 0 0 explicit header mode
#define RADIOLIB_SX1278_HEADER_IMPL_MODE 0b00000001 // 0 0 implicit header mode
// SX127X_REG_MODEM_CONFIG_2
#define RADIOLIB_SX1278_RX_CRC_MODE_OFF 0b00000000 // 2 2 CRC disabled
#define RADIOLIB_SX1278_RX_CRC_MODE_ON 0b00000100 // 2 2 CRC enabled
// RADIOLIB_SX1278_REG_MODEM_CONFIG_3
#define RADIOLIB_SX1278_LOW_DATA_RATE_OPT_OFF 0b00000000 // 3 3 low data rate optimization disabled
#define RADIOLIB_SX1278_LOW_DATA_RATE_OPT_ON 0b00001000 // 3 3 low data rate optimization enabled
#define RADIOLIB_SX1278_AGC_AUTO_OFF 0b00000000 // 2 2 LNA gain set by REG_LNA
#define RADIOLIB_SX1278_AGC_AUTO_ON 0b00000100 // 2 2 LNA gain set by internal AGC loop
// SX127X_REG_VERSION
#define RADIOLIB_SX1278_CHIP_VERSION 0x12
// SX1278 FSK modem settings
// SX127X_REG_PA_RAMP
#define RADIOLIB_SX1278_NO_SHAPING 0b00000000 // 6 5 data shaping: no shaping (default)
#define RADIOLIB_SX1278_FSK_GAUSSIAN_1_0 0b00100000 // 6 5 FSK modulation Gaussian filter, BT = 1.0
#define RADIOLIB_SX1278_FSK_GAUSSIAN_0_5 0b01000000 // 6 5 FSK modulation Gaussian filter, BT = 0.5
#define RADIOLIB_SX1278_FSK_GAUSSIAN_0_3 0b01100000 // 6 5 FSK modulation Gaussian filter, BT = 0.3
#define RADIOLIB_SX1278_OOK_FILTER_BR 0b00100000 // 6 5 OOK modulation filter, f_cutoff = BR
#define RADIOLIB_SX1278_OOK_FILTER_2BR 0b01000000 // 6 5 OOK modulation filter, f_cutoff = 2*BR
// RADIOLIB_SX1278_REG_AGC_REF
#define RADIOLIB_SX1278_AGC_REFERENCE_LEVEL_LF 0x19 // 5 0 floor reference for AGC thresholds: AgcRef = -174 + 10*log(2*RxBw) + 8 + AGC_REFERENCE_LEVEL [dBm]: below 525 MHz
#define RADIOLIB_SX1278_AGC_REFERENCE_LEVEL_HF 0x1C // 5 0 above 779 MHz
// RADIOLIB_SX1278_REG_AGC_THRESH_1
#define RADIOLIB_SX1278_AGC_STEP_1_LF 0x0C // 4 0 1st AGC threshold: below 525 MHz
#define RADIOLIB_SX1278_AGC_STEP_1_HF 0x0E // 4 0 above 779 MHz
// RADIOLIB_SX1278_REG_AGC_THRESH_2
#define RADIOLIB_SX1278_AGC_STEP_2_LF 0x40 // 7 4 2nd AGC threshold: below 525 MHz
#define RADIOLIB_SX1278_AGC_STEP_2_HF 0x50 // 7 4 above 779 MHz
#define RADIOLIB_SX1278_AGC_STEP_3 0x0B // 3 0 3rd AGC threshold
// RADIOLIB_SX1278_REG_AGC_THRESH_3
#define RADIOLIB_SX1278_AGC_STEP_4 0xC0 // 7 4 4th AGC threshold
#define RADIOLIB_SX1278_AGC_STEP_5 0x0C // 4 0 5th AGC threshold
/*!
\class SX1278
\brief Derived class for %SX1278 modules. Also used as base class for SX1276, SX1277, SX1279, RFM95 and RFM96.
All of these modules use the same basic hardware and only differ in parameter ranges (and names).
*/
class SX1278: public SX127x {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
SX1278(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 525.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 12.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
/*!
\brief FSK modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 525.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz.
Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met.
\param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of FSK preamble in bits.
\param enableOOK Use OOK modulation instead of FSK.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false);
/*!
\brief Reset method. Will reset the chip to the default state using RST pin.
*/
void reset() override;
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 137.0 MHz to 525.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
/*!
\brief Sets %LoRa link bandwidth. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz. Only available in %LoRa mode.
\param bw %LoRa link bandwidth to be set in kHz.
\returns \ref status_codes
*/
int16_t setBandwidth(float bw);
/*!
\brief Sets %LoRa link spreading factor. Allowed values range from 6 to 12. Only available in %LoRa mode.
\param sf %LoRa link spreading factor to be set.
\returns \ref status_codes
*/
int16_t setSpreadingFactor(uint8_t sf);
/*!
\brief Sets %LoRa link coding rate denominator. Allowed values range from 5 to 8. Only available in %LoRa mode.
\param cr %LoRa link coding rate denominator to be set.
\returns \ref status_codes
*/
int16_t setCodingRate(uint8_t cr);
/*!
\brief Sets transmission output power. Allowed values range from -3 to 15 dBm (RFO pin) or +2 to +17 dBm (PA_BOOST pin).
High power +20 dBm operation is also supported, on the PA_BOOST pin.
\param power Transmission output power in dBm.
\param useRfo Whether to use the RFO (true) or the PA_BOOST (false) pin for the RF output. Defaults to PA_BOOST.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power, bool useRfo = false);
/*!
\brief Sets gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended). Only available in %LoRa mode.
\param gain Gain of receiver LNA (low-noise amplifier) to be set.
\returns \ref status_codes
*/
int16_t setGain(uint8_t gain);
/*!
\brief Sets Gaussian filter bandwidth-time product that will be used for data shaping. Only available in FSK mode with FSK modulation.
Allowed values are RADIOLIB_SHAPING_0_3, RADIOLIB_SHAPING_0_5 or RADIOLIB_SHAPING_1_0. Set to RADIOLIB_SHAPING_NONE to disable data shaping.
\param sh Gaussian shaping bandwidth-time product that will be used for data shaping
\returns \ref status_codes
*/
int16_t setDataShaping(uint8_t sh) override;
/*!
\brief Sets filter cutoff frequency that will be used for data shaping.
Allowed values are 1 for frequency equal to bit rate and 2 for frequency equal to 2x bit rate. Set to 0 to disable data shaping.
Only available in FSK mode with OOK modulation.
\param sh Cutoff frequency that will be used for data shaping
\returns \ref status_codes
*/
int16_t setDataShapingOOK(uint8_t sh);
/*!
\brief Gets recorded signal strength indicator of the latest received packet for LoRa modem, or current RSSI level for FSK modem.
\param skipReceive Set to true to skip putting radio in receive mode for the RSSI measurement in FKS/OOK mode.
\returns Last packet RSSI for LoRa modem, or current RSSI level for FSK modem.
*/
float getRSSI(bool skipReceive = false);
/*!
\brief Enables/disables CRC check of received packets.
\param enable Enable (true) or disable (false) CRC.
\param mode Set CRC mode to SX127X_CRC_WHITENING_TYPE_CCITT for CCITT, polynomial X16 + X12 + X5 + 1 (false) or SX127X_CRC_WHITENING_TYPE_IBM for IBM, polynomial X16 + X15 + X2 + 1 (true). Only valid in FSK mode.
\returns \ref status_codes
*/
int16_t setCRC(bool enable, bool mode = false);
/*!
\brief Forces LoRa low data rate optimization. Only available in LoRa mode. After calling this method, LDRO will always be set to
the provided value, regardless of symbol length. To re-enable automatic LDRO configuration, call SX1278::autoLDRO()
\param enable Force LDRO to be always enabled (true) or disabled (false).
\returns \ref status_codes
*/
int16_t forceLDRO(bool enable);
/*!
\brief Re-enables automatic LDRO configuration. Only available in LoRa mode. After calling this method, LDRO will be enabled automatically
when symbol length exceeds 16 ms.
\returns \ref status_codes
*/
int16_t autoLDRO();
/*!
\brief Set implicit header mode for future reception/transmission. Required for spreading factor 6.
\param len Payload length in bytes.
\returns \ref status_codes
*/
int16_t implicitHeader(size_t len);
/*!
\brief Set explicit header mode for future reception/transmission.
\returns \ref status_codes
*/
int16_t explicitHeader();
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
int16_t setBandwidthRaw(uint8_t newBandwidth);
int16_t setSpreadingFactorRaw(uint8_t newSpreadingFactor);
int16_t setCodingRateRaw(uint8_t newCodingRate);
int16_t setHeaderType(uint8_t headerType, size_t len = 0xFF);
int16_t configFSK();
void errataFix(bool rx);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
bool _ldroAuto = true;
bool _ldroEnabled = false;
};
#endif
#endif
@@ -0,0 +1,73 @@
#include "SX1279.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1279::SX1279(Module* mod) : SX1278(mod) {
}
int16_t SX1279::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
// execute common part
int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength);
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setBandwidth(bw);
RADIOLIB_ASSERT(state);
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setSpreadingFactor(sf);
RADIOLIB_ASSERT(state);
state = setCodingRate(cr);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
state = setGain(gain);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t SX1279::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
// execute common part
int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, br, freqDev, rxBw, preambleLength, enableOOK);
RADIOLIB_ASSERT(state);
// configure settings not accessible by API
state = configFSK();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
if(enableOOK) {
state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setDataShaping(RADIOLIB_SHAPING_NONE);
RADIOLIB_ASSERT(state);
}
return(state);
}
int16_t SX1279::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 137.0, 960.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency and if successful, save the new setting
int16_t state = SX127x::setFrequencyRaw(freq);
if(state == RADIOLIB_ERR_NONE) {
SX127x::_freq = freq;
}
return(state);
}
#endif
+95
View File
@@ -0,0 +1,95 @@
#if !defined(_RADIOLIB_SX1279_H)
#define _RADIOLIB_SX1279_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX127X)
#include "SX1278.h"
/*!
\class SX1279
\brief Derived class for %SX1279 modules. Overrides some methods from SX1278 due to different parameter ranges.
*/
class SX1279: public SX1278 {
public:
// constructor
/*!
\brief Default constructor. Called from Arduino sketch when creating new LoRa instance.
\param mod Instance of Module that will be used to communicate with the %LoRa chip.
*/
SX1279(Module* mod);
// basic methods
/*!
\brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 960.0 MHz.
\param bw %LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
\param sf %LoRa link spreading factor. Allowed values range from 6 to 12.
\param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8.
\param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number.
Allowed values range from 6 to 65535.
\param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain.
Set to 0 to enable automatic gain control (recommended).
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0);
/*!
\brief FSK modem initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 137.0 MHz to 525.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz.
Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met.
\param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz.
\param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm.
\param preambleLength Length of FSK preamble in bits.
\param enableOOK Use OOK modulation instead of FSK.
\returns \ref status_codes
*/
int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 137.0 MHz to 960.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+172
View File
@@ -0,0 +1,172 @@
#include "SX1280.h"
#if !defined(RADIOLIB_EXCLUDE_SX128X)
SX1280::SX1280(Module* mod) : SX1281(mod) {
}
int16_t SX1280::range(bool master, uint32_t addr) {
// start ranging
int16_t state = startRanging(master, addr);
RADIOLIB_ASSERT(state);
// wait until ranging is finished
uint32_t start = _mod->millis();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->millis() - start > 10000) {
clearIrqStatus();
standby();
return(RADIOLIB_ERR_RANGING_TIMEOUT);
}
}
// clear interrupt flags
state = clearIrqStatus();
RADIOLIB_ASSERT(state);
// set mode to standby
state = standby();
return(state);
}
int16_t SX1280::startRanging(bool master, uint32_t addr) {
// check active modem
uint8_t modem = getPacketType();
if(!((modem == RADIOLIB_SX128X_PACKET_TYPE_LORA) || (modem == RADIOLIB_SX128X_PACKET_TYPE_RANGING))) {
return(RADIOLIB_ERR_WRONG_MODEM);
}
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// ensure modem is set to ranging
if(modem == RADIOLIB_SX128X_PACKET_TYPE_LORA) {
state = setPacketType(RADIOLIB_SX128X_PACKET_TYPE_RANGING);
RADIOLIB_ASSERT(state);
}
// set modulation parameters
state = setModulationParams(_sf, _bw, _cr);
RADIOLIB_ASSERT(state);
// set packet parameters
state = setPacketParamsLoRa(_preambleLengthLoRa, _headerType, _payloadLen, _crcLoRa);
RADIOLIB_ASSERT(state);
// check all address bits
uint8_t regValue;
state = readRegister(RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_WIDTH, &regValue, 1);
RADIOLIB_ASSERT(state);
regValue &= 0b00111111;
regValue |= 0b11000000;
state = writeRegister(RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_WIDTH, &regValue, 1);
RADIOLIB_ASSERT(state);
// set remaining parameter values
uint32_t addrReg = RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_BYTE_3;
uint32_t irqMask = RADIOLIB_SX128X_IRQ_RANGING_SLAVE_RESP_DONE | RADIOLIB_SX128X_IRQ_RANGING_SLAVE_REQ_DISCARD;
uint32_t irqDio1 = RADIOLIB_SX128X_IRQ_RANGING_SLAVE_RESP_DONE;
if(master) {
addrReg = RADIOLIB_SX128X_REG_MASTER_RANGING_ADDRESS_BYTE_3;
irqMask = RADIOLIB_SX128X_IRQ_RANGING_MASTER_RES_VALID | RADIOLIB_SX128X_IRQ_RANGING_MASTER_TIMEOUT;
irqDio1 = RADIOLIB_SX128X_IRQ_RANGING_MASTER_RES_VALID;
}
// set ranging address
uint8_t addrBuff[] = { (uint8_t)((addr >> 24) & 0xFF), (uint8_t)((addr >> 16) & 0xFF), (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF) };
state = writeRegister(addrReg, addrBuff, 4);
RADIOLIB_ASSERT(state);
// set DIO mapping
state = setDioIrqParams(irqMask, irqDio1);
RADIOLIB_ASSERT(state);
// set calibration values
uint8_t index = (_sf >> 4) - 5;
static const uint16_t calTable[3][6] = {
{ 10299, 10271, 10244, 10242, 10230, 10246 },
{ 11486, 11474, 11453, 11426, 11417, 11401 },
{ 13308, 13493, 13528, 13515, 13430, 13376 }
};
uint16_t val = 0;
switch(_bw) {
case(RADIOLIB_SX128X_LORA_BW_406_25):
val = calTable[0][index];
break;
case(RADIOLIB_SX128X_LORA_BW_812_50):
val = calTable[1][index];
break;
case(RADIOLIB_SX128X_LORA_BW_1625_00):
val = calTable[2][index];
break;
default:
return(RADIOLIB_ERR_INVALID_BANDWIDTH);
}
uint8_t calBuff[] = { (uint8_t)((val >> 8) & 0xFF), (uint8_t)(val & 0xFF) };
state = writeRegister(RADIOLIB_SX128X_REG_RANGING_CALIBRATION_MSB, calBuff, 2);
RADIOLIB_ASSERT(state);
// set role and start ranging
if(master) {
state = setRangingRole(RADIOLIB_SX128X_RANGING_ROLE_MASTER);
RADIOLIB_ASSERT(state);
state = setTx(RADIOLIB_SX128X_TX_TIMEOUT_NONE);
RADIOLIB_ASSERT(state);
} else {
state = setRangingRole(RADIOLIB_SX128X_RANGING_ROLE_SLAVE);
RADIOLIB_ASSERT(state);
state = setRx(RADIOLIB_SX128X_RX_TIMEOUT_INF);
RADIOLIB_ASSERT(state);
}
return(state);
}
float SX1280::getRangingResult() {
// set mode to standby XOSC
int16_t state = standby(RADIOLIB_SX128X_STANDBY_XOSC);
RADIOLIB_ASSERT(state);
// enable clock
uint8_t data[4];
state = readRegister(RADIOLIB_SX128X_REG_RANGING_LORA_CLOCK_ENABLE, data, 1);
RADIOLIB_ASSERT(state);
data[0] |= (1 << 1);
state = writeRegister(RADIOLIB_SX128X_REG_RANGING_LORA_CLOCK_ENABLE, data, 1);
RADIOLIB_ASSERT(state);
// set result type to filtered
state = readRegister(RADIOLIB_SX128X_REG_RANGING_TYPE, data, 1);
RADIOLIB_ASSERT(state);
data[0] &= 0xCF;
data[0] |= (1 << 4);
state = writeRegister(RADIOLIB_SX128X_REG_RANGING_TYPE, data, 1);
RADIOLIB_ASSERT(state);
// read the register values
state = readRegister(RADIOLIB_SX128X_REG_RANGING_RESULT_MSB, &data[0], 1);
RADIOLIB_ASSERT(state);
state = readRegister(RADIOLIB_SX128X_REG_RANGING_RESULT_MID, &data[1], 1);
RADIOLIB_ASSERT(state);
state = readRegister(RADIOLIB_SX128X_REG_RANGING_RESULT_LSB, &data[2], 1);
RADIOLIB_ASSERT(state);
// set mode to standby RC
state = standby();
RADIOLIB_ASSERT(state);
// calculate the real result
uint32_t raw = ((uint32_t)data[0] << 16) | ((uint32_t)data[1] << 8) | data[2];
return((float)raw * 150.0 / (4.096 * _bwKhz));
}
#endif
+63
View File
@@ -0,0 +1,63 @@
#if !defined(_RADIOLIB_SX1280_H)
#define _RADIOLIB_SX1280_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX128X)
#include "../../Module.h"
#include "SX128x.h"
#include "SX1281.h"
/*!
\class SX1280
\brief Derived class for %SX1280 modules.
*/
class SX1280: public SX1281 {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX1280(Module* mod);
/*!
\brief Blocking ranging method.
\param master Whether to execute ranging in master mode (true) or slave mode (false).
\param addr Ranging address to be used.
\returns \ref status_codes
*/
int16_t range(bool master, uint32_t addr);
/*!
\brief Interrupt-driven ranging method.
\param master Whether to execute ranging in master mode (true) or slave mode (false).
\param addr Ranging address to be used.
\returns \ref status_codes
*/
int16_t startRanging(bool master, uint32_t addr);
/*!
\brief Gets ranging result of the last ranging exchange.
\returns Ranging result in meters.
*/
float getRangingResult();
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
@@ -0,0 +1,8 @@
#include "SX1281.h"
#if !defined(RADIOLIB_EXCLUDE_SX128X)
SX1281::SX1281(Module* mod) : SX128x(mod) {
}
#endif
+33
View File
@@ -0,0 +1,33 @@
#if !defined(_RADIOLIB_SX1281_H)
#define _RADIOLIB_SX1281_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX128X)
#include "../../Module.h"
#include "SX128x.h"
/*!
\class SX1281
\brief Derived class for %SX1281 modules.
*/
class SX1281: public SX128x {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX1281(Module* mod);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
@@ -0,0 +1,9 @@
#include "SX1282.h"
#if !defined(RADIOLIB_EXCLUDE_SX128X)
/// \todo implement advanced ranging
SX1282::SX1282(Module* mod) : SX1280(mod) {
}
#endif
+34
View File
@@ -0,0 +1,34 @@
#if !defined(_RADIOLIB_SX1282_H)
#define _RADIOLIB_SX1282_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX128X)
#include "../../Module.h"
#include "SX128x.h"
#include "SX1280.h"
/*!
\class SX1282
\brief Derived class for %SX1282 modules.
*/
class SX1282: public SX1280 {
public:
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX1282(Module* mod);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
File diff suppressed because it is too large Load Diff
+904
View File
@@ -0,0 +1,904 @@
#if !defined(_RADIOLIB_SX128X_H)
#define _RADIOLIB_SX128X_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SX128X)
#include "../../Module.h"
#include "../../protocols/PhysicalLayer/PhysicalLayer.h"
// SX128X physical layer properties
#define RADIOLIB_SX128X_FREQUENCY_STEP_SIZE 198.3642578
#define RADIOLIB_SX128X_MAX_PACKET_LENGTH 255
#define RADIOLIB_SX128X_CRYSTAL_FREQ 52.0
#define RADIOLIB_SX128X_DIV_EXPONENT 18
// SX128X SPI commands
#define RADIOLIB_SX128X_CMD_NOP 0x00
#define RADIOLIB_SX128X_CMD_GET_STATUS 0xC0
#define RADIOLIB_SX128X_CMD_WRITE_REGISTER 0x18
#define RADIOLIB_SX128X_CMD_READ_REGISTER 0x19
#define RADIOLIB_SX128X_CMD_WRITE_BUFFER 0x1A
#define RADIOLIB_SX128X_CMD_READ_BUFFER 0x1B
#define RADIOLIB_SX128X_CMD_SET_SLEEP 0x84
#define RADIOLIB_SX128X_CMD_SET_STANDBY 0x80
#define RADIOLIB_SX128X_CMD_SET_FS 0xC1
#define RADIOLIB_SX128X_CMD_SET_TX 0x83
#define RADIOLIB_SX128X_CMD_SET_RX 0x82
#define RADIOLIB_SX128X_CMD_SET_RX_DUTY_CYCLE 0x94
#define RADIOLIB_SX128X_CMD_SET_CAD 0xC5
#define RADIOLIB_SX128X_CMD_SET_TX_CONTINUOUS_WAVE 0xD1
#define RADIOLIB_SX128X_CMD_SET_TX_CONTINUOUS_PREAMBLE 0xD2
#define RADIOLIB_SX128X_CMD_SET_PACKET_TYPE 0x8A
#define RADIOLIB_SX128X_CMD_GET_PACKET_TYPE 0x03
#define RADIOLIB_SX128X_CMD_SET_RF_FREQUENCY 0x86
#define RADIOLIB_SX128X_CMD_SET_TX_PARAMS 0x8E
#define RADIOLIB_SX128X_CMD_SET_CAD_PARAMS 0x88
#define RADIOLIB_SX128X_CMD_SET_BUFFER_BASE_ADDRESS 0x8F
#define RADIOLIB_SX128X_CMD_SET_MODULATION_PARAMS 0x8B
#define RADIOLIB_SX128X_CMD_SET_PACKET_PARAMS 0x8C
#define RADIOLIB_SX128X_CMD_GET_RX_BUFFER_STATUS 0x17
#define RADIOLIB_SX128X_CMD_GET_PACKET_STATUS 0x1D
#define RADIOLIB_SX128X_CMD_GET_RSSI_INST 0x1F
#define RADIOLIB_SX128X_CMD_SET_DIO_IRQ_PARAMS 0x8D
#define RADIOLIB_SX128X_CMD_GET_IRQ_STATUS 0x15
#define RADIOLIB_SX128X_CMD_CLEAR_IRQ_STATUS 0x97
#define RADIOLIB_SX128X_CMD_SET_REGULATOR_MODE 0x96
#define RADIOLIB_SX128X_CMD_SET_SAVE_CONTEXT 0xD5
#define RADIOLIB_SX128X_CMD_SET_AUTO_TX 0x98
#define RADIOLIB_SX128X_CMD_SET_AUTO_FS 0x9E
#define RADIOLIB_SX128X_CMD_SET_PERF_COUNTER_MODE 0x9C
#define RADIOLIB_SX128X_CMD_SET_LONG_PREAMBLE 0x9B
#define RADIOLIB_SX128X_CMD_SET_UART_SPEED 0x9D
#define RADIOLIB_SX128X_CMD_SET_RANGING_ROLE 0xA3
#define RADIOLIB_SX128X_CMD_SET_ADVANCED_RANGING 0x9A
// SX128X register map
#define RADIOLIB_SX128X_REG_GAIN_MODE 0x0891
#define RADIOLIB_SX128X_REG_MANUAL_GAIN_CONTROL_ENABLE_2 0x0895
#define RADIOLIB_SX128X_REG_MANUAL_GAIN_SETTING 0x089E
#define RADIOLIB_SX128X_REG_MANUAL_GAIN_CONTROL_ENABLE_1 0x089F
#define RADIOLIB_SX128X_REG_SYNCH_PEAK_ATTENUATION 0x08C2
#define RADIOLIB_SX128X_REG_LORA_FIXED_PAYLOAD_LENGTH 0x0901
#define RADIOLIB_SX128X_REG_LORA_HEADER_MODE 0x0903
#define RADIOLIB_SX128X_REG_MASTER_RANGING_ADDRESS_BYTE_3 0x0912
#define RADIOLIB_SX128X_REG_MASTER_RANGING_ADDRESS_BYTE_2 0x0913
#define RADIOLIB_SX128X_REG_MASTER_RANGING_ADDRESS_BYTE_1 0x0914
#define RADIOLIB_SX128X_REG_MASTER_RANGING_ADDRESS_BYTE_0 0x0915
#define RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_BYTE_3 0x0916
#define RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_BYTE_2 0x0917
#define RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_BYTE_1 0x0918
#define RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_BYTE_0 0x0919
#define RADIOLIB_SX128X_REG_RANGING_FILTER_WINDOW_SIZE 0x091E
#define RADIOLIB_SX128X_REG_RANGING_FILTER_RESET 0x0923
#define RADIOLIB_SX128X_REG_RANGING_TYPE 0x0924
#define RADIOLIB_SX128X_REG_LORA_SF_CONFIG 0x0925
#define RADIOLIB_SX128X_REG_RANGING_ADDRESS_SWITCH 0x0927
#define RADIOLIB_SX128X_REG_RANGING_CALIBRATION_BYTE_2 0x092B
#define RADIOLIB_SX128X_REG_RANGING_CALIBRATION_MSB 0x092C
#define RADIOLIB_SX128X_REG_RANGING_CALIBRATION_LSB 0x092D
#define RADIOLIB_SX128X_REG_SLAVE_RANGING_ADDRESS_WIDTH 0x0931
#define RADIOLIB_SX128X_REG_FREQ_ERROR_CORRECTION 0x093C
#define RADIOLIB_SX128X_REG_LORA_SYNC_WORD_MSB 0x0944
#define RADIOLIB_SX128X_REG_LORA_SYNC_WORD_LSB 0x0945
#define RADIOLIB_SX128X_REG_RANGING_FILTER_RSSI_OFFSET 0x0953
#define RADIOLIB_SX128X_REG_FEI_MSB 0x0954
#define RADIOLIB_SX128X_REG_FEI_MID 0x0955
#define RADIOLIB_SX128X_REG_FEI_LSB 0x0956
#define RADIOLIB_SX128X_REG_RANGING_ADDRESS_MSB 0x095F
#define RADIOLIB_SX128X_REG_RANGING_ADDRESS_LSB 0x0960
#define RADIOLIB_SX128X_REG_RANGING_RESULT_MSB 0x0961
#define RADIOLIB_SX128X_REG_RANGING_RESULT_MID 0x0962
#define RADIOLIB_SX128X_REG_RANGING_RESULT_LSB 0x0963
#define RADIOLIB_SX128X_REG_RANGING_RSSI 0x0964
#define RADIOLIB_SX128X_REG_RANGING_LORA_CLOCK_ENABLE 0x097F
#define RADIOLIB_SX128X_REG_PACKET_PREAMBLE_SETTINGS 0x09C1
#define RADIOLIB_SX128X_REG_WHITENING_INITIAL_VALUE 0x09C5
#define RADIOLIB_SX128X_REG_CRC_POLYNOMIAL_MSB 0x09C6
#define RADIOLIB_SX128X_REG_CRC_POLYNOMIAL_LSB 0x09C7
#define RADIOLIB_SX128X_REG_CRC_INITIAL_MSB 0x09C8
#define RADIOLIB_SX128X_REG_CRC_INITIAL_LSB 0x09C9
#define RADIOLIB_SX128X_REG_BLE_CRC_INITIAL_MSB 0x09C7
#define RADIOLIB_SX128X_REG_BLE_CRC_INITIAL_MID (RADIOLIB_SX128X_REG_CRC_INITIAL_MSB)
#define RADIOLIB_SX128X_REG_BLE_CRC_INITIAL_LSB (RADIOLIB_SX128X_REG_CRC_INITIAL_LSB)
#define RADIOLIB_SX128X_REG_SYNCH_ADDRESS_CONTROL 0x09CD
#define RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_4 0x09CE
#define RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_3 0x09CF
#define RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_2 0x09D0
#define RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_1 0x09D1
#define RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_0 0x09D2
#define RADIOLIB_SX128X_REG_SYNC_WORD_2_BYTE_4 0x09D3
#define RADIOLIB_SX128X_REG_SYNC_WORD_2_BYTE_3 0x09D4
#define RADIOLIB_SX128X_REG_SYNC_WORD_2_BYTE_2 0x09D5
#define RADIOLIB_SX128X_REG_SYNC_WORD_2_BYTE_1 0x09D6
#define RADIOLIB_SX128X_REG_SYNC_WORD_2_BYTE_0 0x09D7
#define RADIOLIB_SX128X_REG_SYNC_WORD_3_BYTE_4 0x09D8
#define RADIOLIB_SX128X_REG_SYNC_WORD_3_BYTE_3 0x09D9
#define RADIOLIB_SX128X_REG_SYNC_WORD_3_BYTE_2 0x09DA
#define RADIOLIB_SX128X_REG_SYNC_WORD_3_BYTE_1 0x09DB
#define RADIOLIB_SX128X_REG_SYNC_WORD_3_BYTE_0 0x09DC
#define RADIOLIB_SX128X_REG_ACCESS_ADDRESS_BYTE_3 (RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_3)
#define RADIOLIB_SX128X_REG_ACCESS_ADDRESS_BYTE_2 (RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_2)
#define RADIOLIB_SX128X_REG_ACCESS_ADDRESS_BYTE_1 (RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_1)
#define RADIOLIB_SX128X_REG_ACCESS_ADDRESS_BYTE_0 (RADIOLIB_SX128X_REG_SYNC_WORD_1_BYTE_0)
// SX128X SPI command variables
//RADIOLIB_SX128X_CMD_GET_STATUS MSB LSB DESCRIPTION
#define RADIOLIB_SX128X_STATUS_MODE_STDBY_RC 0b01000000 // 7 5 current chip mode: STDBY_RC
#define RADIOLIB_SX128X_STATUS_MODE_STDBY_XOSC 0b01100000 // 7 5 STDBY_XOSC
#define RADIOLIB_SX128X_STATUS_MODE_FS 0b10000000 // 7 5 FS
#define RADIOLIB_SX128X_STATUS_MODE_RX 0b10100000 // 7 5 Rx
#define RADIOLIB_SX128X_STATUS_MODE_TX 0b11000000 // 7 5 Tx
#define RADIOLIB_SX128X_STATUS_CMD_PROCESSED 0b00000100 // 4 2 command status: processing OK
#define RADIOLIB_SX128X_STATUS_DATA_AVAILABLE 0b00001000 // 4 2 data available
#define RADIOLIB_SX128X_STATUS_CMD_TIMEOUT 0b00001100 // 4 2 timeout
#define RADIOLIB_SX128X_STATUS_CMD_ERROR 0b00010000 // 4 2 processing error
#define RADIOLIB_SX128X_STATUS_CMD_FAILED 0b00010100 // 4 2 failed to execute
#define RADIOLIB_SX128X_STATUS_TX_DONE 0b00011000 // 4 2 transmission finished
#define RADIOLIB_SX128X_STATUS_BUSY 0b00000001 // 0 0 chip busy
#define RADIOLIB_SX128X_STATUS_SPI_FAILED 0b11111111 // 7 0 SPI transaction failed
//RADIOLIB_SX128X_CMD_SET_SLEEP
#define RADIOLIB_SX128X_SLEEP_DATA_BUFFER_FLUSH 0b00000000 // 1 1 data buffer behavior in sleep mode: flush
#define RADIOLIB_SX128X_SLEEP_DATA_BUFFER_RETAIN 0b00000010 // 1 1 retain
#define RADIOLIB_SX128X_SLEEP_DATA_RAM_FLUSH 0b00000000 // 0 0 data RAM (configuration) behavior in sleep mode: flush
#define RADIOLIB_SX128X_SLEEP_DATA_RAM_RETAIN 0b00000001 // 0 0 retain
//RADIOLIB_SX128X_CMD_SET_STANDBY
#define RADIOLIB_SX128X_STANDBY_RC 0x00 // 7 0 standby mode: 13 MHz RC oscillator
#define RADIOLIB_SX128X_STANDBY_XOSC 0x01 // 7 0 52 MHz crystal oscillator
//RADIOLIB_SX128X_CMD_SET_TX + RADIOLIB_SX128X_CMD_SET_RX + RADIOLIB_SX128X_CMD_SET_RX_DUTY_CYCLE
#define RADIOLIB_SX128X_PERIOD_BASE_15_625_US 0x00 // 7 0 time period step: 15.625 us
#define RADIOLIB_SX128X_PERIOD_BASE_62_5_US 0x01 // 7 0 62.5 us
#define RADIOLIB_SX128X_PERIOD_BASE_1_MS 0x02 // 7 0 1 ms
#define RADIOLIB_SX128X_PERIOD_BASE_4_MS 0x03 // 7 0 4 ms
//RADIOLIB_SX128X_CMD_SET_TX
#define RADIOLIB_SX128X_TX_TIMEOUT_NONE 0x0000 // 15 0 Tx timeout duration: no timeout (Tx single mode)
//RADIOLIB_SX128X_CMD_SET_RX
#define RADIOLIB_SX128X_RX_TIMEOUT_NONE 0x0000 // 15 0 Rx timeout duration: no timeout (Rx single mode)
#define RADIOLIB_SX128X_RX_TIMEOUT_INF 0xFFFF // 15 0 infinite (Rx continuous mode)
//RADIOLIB_SX128X_CMD_SET_PACKET_TYPE
#define RADIOLIB_SX128X_PACKET_TYPE_GFSK 0x00 // 7 0 packet type: (G)FSK
#define RADIOLIB_SX128X_PACKET_TYPE_LORA 0x01 // 7 0 LoRa
#define RADIOLIB_SX128X_PACKET_TYPE_RANGING 0x02 // 7 0 ranging engine
#define RADIOLIB_SX128X_PACKET_TYPE_FLRC 0x03 // 7 0 FLRC
#define RADIOLIB_SX128X_PACKET_TYPE_BLE 0x04 // 7 0 BLE
//RADIOLIB_SX128X_CMD_SET_TX_PARAMS
#define RADIOLIB_SX128X_PA_RAMP_02_US 0x00 // 7 0 PA ramp time: 2 us
#define RADIOLIB_SX128X_PA_RAMP_04_US 0x20 // 7 0 4 us
#define RADIOLIB_SX128X_PA_RAMP_06_US 0x40 // 7 0 6 us
#define RADIOLIB_SX128X_PA_RAMP_08_US 0x60 // 7 0 8 us
#define RADIOLIB_SX128X_PA_RAMP_10_US 0x80 // 7 0 10 us
#define RADIOLIB_SX128X_PA_RAMP_12_US 0xA0 // 7 0 12 us
#define RADIOLIB_SX128X_PA_RAMP_16_US 0xC0 // 7 0 16 us
#define RADIOLIB_SX128X_PA_RAMP_20_US 0xE0 // 7 0 20 us
//RADIOLIB_SX128X_CMD_SET_CAD_PARAMS
#define RADIOLIB_SX128X_CAD_ON_1_SYMB 0x00 // 7 0 number of symbols used for CAD: 1
#define RADIOLIB_SX128X_CAD_ON_2_SYMB 0x20 // 7 0 2
#define RADIOLIB_SX128X_CAD_ON_4_SYMB 0x40 // 7 0 4
#define RADIOLIB_SX128X_CAD_ON_8_SYMB 0x60 // 7 0 8
#define RADIOLIB_SX128X_CAD_ON_16_SYMB 0x80 // 7 0 16
//RADIOLIB_SX128X_CMD_SET_MODULATION_PARAMS
#define RADIOLIB_SX128X_BLE_GFSK_BR_2_000_BW_2_4 0x04 // 7 0 GFSK/BLE bit rate and bandwidth setting: 2.0 Mbps 2.4 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_1_600_BW_2_4 0x28 // 7 0 1.6 Mbps 2.4 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_1_000_BW_2_4 0x4C // 7 0 1.0 Mbps 2.4 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_1_000_BW_1_2 0x45 // 7 0 1.0 Mbps 1.2 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_800_BW_2_4 0x70 // 7 0 0.8 Mbps 2.4 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_800_BW_1_2 0x69 // 7 0 0.8 Mbps 1.2 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_500_BW_1_2 0x8D // 7 0 0.5 Mbps 1.2 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_500_BW_0_6 0x86 // 7 0 0.5 Mbps 0.6 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_400_BW_1_2 0xB1 // 7 0 0.4 Mbps 1.2 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_400_BW_0_6 0xAA // 7 0 0.4 Mbps 0.6 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_250_BW_0_6 0xCE // 7 0 0.25 Mbps 0.6 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_250_BW_0_3 0xC7 // 7 0 0.25 Mbps 0.3 MHz
#define RADIOLIB_SX128X_BLE_GFSK_BR_0_125_BW_0_3 0xEF // 7 0 0.125 Mbps 0.3 MHz
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_0_35 0x00 // 7 0 GFSK/BLE modulation index: 0.35
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_0_50 0x01 // 7 0 0.50
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_0_75 0x02 // 7 0 0.75
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_1_00 0x03 // 7 0 1.00
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_1_25 0x04 // 7 0 1.25
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_1_50 0x05 // 7 0 1.50
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_1_75 0x06 // 7 0 1.75
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_2_00 0x07 // 7 0 2.00
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_2_25 0x08 // 7 0 2.25
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_2_50 0x09 // 7 0 2.50
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_2_75 0x0A // 7 0 2.75
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_3_00 0x0B // 7 0 3.00
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_3_25 0x0C // 7 0 3.25
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_3_50 0x0D // 7 0 3.50
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_3_75 0x0E // 7 0 3.75
#define RADIOLIB_SX128X_BLE_GFSK_MOD_IND_4_00 0x0F // 7 0 4.00
#define RADIOLIB_SX128X_BLE_GFSK_BT_OFF 0x00 // 7 0 GFSK Gaussian filter BT product: filter disabled
#define RADIOLIB_SX128X_BLE_GFSK_BT_1_0 0x10 // 7 0 1.0
#define RADIOLIB_SX128X_BLE_GFSK_BT_0_5 0x20 // 7 0 0.5
#define RADIOLIB_SX128X_FLRC_BR_1_300_BW_1_2 0x45 // 7 0 FLRC bit rate and bandwidth setting: 1.3 Mbps 1.2 MHz
#define RADIOLIB_SX128X_FLRC_BR_1_000_BW_1_2 0x69 // 7 0 1.04 Mbps 1.2 MHz
#define RADIOLIB_SX128X_FLRC_BR_0_650_BW_0_6 0x86 // 7 0 0.65 Mbps 0.6 MHz
#define RADIOLIB_SX128X_FLRC_BR_0_520_BW_0_6 0xAA // 7 0 0.52 Mbps 0.6 MHz
#define RADIOLIB_SX128X_FLRC_BR_0_325_BW_0_3 0xC7 // 7 0 0.325 Mbps 0.3 MHz
#define RADIOLIB_SX128X_FLRC_BR_0_260_BW_0_3 0xEB // 7 0 0.260 Mbps 0.3 MHz
#define RADIOLIB_SX128X_FLRC_CR_1_2 0x00 // 7 0 FLRC coding rate: 1/2
#define RADIOLIB_SX128X_FLRC_CR_3_4 0x02 // 7 0 3/4
#define RADIOLIB_SX128X_FLRC_CR_1_0 0x04 // 7 0 1/1
#define RADIOLIB_SX128X_FLRC_BT_OFF 0x00 // 7 0 FLRC Gaussian filter BT product: filter disabled
#define RADIOLIB_SX128X_FLRC_BT_1_0 0x10 // 7 0 1.0
#define RADIOLIB_SX128X_FLRC_BT_0_5 0x20 // 7 0 0.5
#define RADIOLIB_SX128X_LORA_SF_5 0x50 // 7 0 LoRa spreading factor: 5
#define RADIOLIB_SX128X_LORA_SF_6 0x60 // 7 0 6
#define RADIOLIB_SX128X_LORA_SF_7 0x70 // 7 0 7
#define RADIOLIB_SX128X_LORA_SF_8 0x80 // 7 0 8
#define RADIOLIB_SX128X_LORA_SF_9 0x90 // 7 0 9
#define RADIOLIB_SX128X_LORA_SF_10 0xA0 // 7 0 10
#define RADIOLIB_SX128X_LORA_SF_11 0xB0 // 7 0 11
#define RADIOLIB_SX128X_LORA_SF_12 0xC0 // 7 0 12
#define RADIOLIB_SX128X_LORA_BW_1625_00 0x0A // 7 0 LoRa bandwidth: 1625.0 kHz
#define RADIOLIB_SX128X_LORA_BW_812_50 0x18 // 7 0 812.5 kHz
#define RADIOLIB_SX128X_LORA_BW_406_25 0x26 // 7 0 406.25 kHz
#define RADIOLIB_SX128X_LORA_BW_203_125 0x34 // 7 0 203.125 kHz
#define RADIOLIB_SX128X_LORA_CR_4_5 0x01 // 7 0 LoRa coding rate: 4/5
#define RADIOLIB_SX128X_LORA_CR_4_6 0x02 // 7 0 4/6
#define RADIOLIB_SX128X_LORA_CR_4_7 0x03 // 7 0 4/7
#define RADIOLIB_SX128X_LORA_CR_4_8 0x04 // 7 0 4/8
#define RADIOLIB_SX128X_LORA_CR_4_5_LI 0x05 // 7 0 4/5, long interleaving
#define RADIOLIB_SX128X_LORA_CR_4_6_LI 0x06 // 7 0 4/6, long interleaving
#define RADIOLIB_SX128X_LORA_CR_4_7_LI 0x07 // 7 0 4/7, long interleaving
//RADIOLIB_SX128X_CMD_SET_PACKET_PARAMS
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_OFF 0x00 // 7 0 GFSK/FLRC sync word used: none
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_1 0x10 // 7 0 sync word 1
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_2 0x20 // 7 0 sync word 2
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_1_2 0x30 // 7 0 sync words 1 and 2
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_3 0x40 // 7 0 sync word 3
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_1_3 0x50 // 7 0 sync words 1 and 3
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_2_3 0x60 // 7 0 sync words 2 and 3
#define RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_1_2_3 0x70 // 7 0 sync words 1, 2 and 3
#define RADIOLIB_SX128X_GFSK_FLRC_PACKET_FIXED 0x00 // 7 0 GFSK/FLRC packet length mode: fixed
#define RADIOLIB_SX128X_GFSK_FLRC_PACKET_VARIABLE 0x20 // 7 0 variable
#define RADIOLIB_SX128X_GFSK_FLRC_CRC_OFF 0x00 // 7 0 GFSK/FLRC packet CRC: none
#define RADIOLIB_SX128X_GFSK_FLRC_CRC_1_BYTE 0x10 // 7 0 1 byte
#define RADIOLIB_SX128X_GFSK_FLRC_CRC_2_BYTE 0x20 // 7 0 2 bytes
#define RADIOLIB_SX128X_GFSK_FLRC_CRC_3_BYTE 0x30 // 7 0 3 bytes (FLRC only)
#define RADIOLIB_SX128X_GFSK_BLE_WHITENING_ON 0x00 // 7 0 GFSK/BLE whitening: enabled
#define RADIOLIB_SX128X_GFSK_BLE_WHITENING_OFF 0x08 // 7 0 disabled
#define RADIOLIB_SX128X_BLE_PAYLOAD_LENGTH_MAX_31 0x00 // 7 0 BLE maximum payload length: 31 bytes
#define RADIOLIB_SX128X_BLE_PAYLOAD_LENGTH_MAX_37 0x20 // 7 0 37 bytes
#define RADIOLIB_SX128X_BLE_PAYLOAD_LENGTH_TEST 0x40 // 7 0 63 bytes (test mode)
#define RADIOLIB_SX128X_BLE_PAYLOAD_LENGTH_MAX_255 0x80 // 7 0 255 bytes (Bluetooth 4.2 and above)
#define RADIOLIB_SX128X_BLE_CRC_OFF 0x00 // 7 0 BLE packet CRC: none
#define RADIOLIB_SX128X_BLE_CRC_3_BYTE 0x10 // 7 0 3 byte
#define RADIOLIB_SX128X_BLE_PRBS_9 0x00 // 7 0 BLE test payload contents: PRNG sequence using x^9 + x^5 + x
#define RADIOLIB_SX128X_BLE_EYELONG 0x04 // 7 0 repeated 0xF0
#define RADIOLIB_SX128X_BLE_EYESHORT 0x08 // 7 0 repeated 0xAA
#define RADIOLIB_SX128X_BLE_PRBS_15 0x0C // 7 0 PRNG sequence using x^15 + x^14 + x^13 + x^12 + x^2 + x + 1
#define RADIOLIB_SX128X_BLE_ALL_1 0x10 // 7 0 repeated 0xFF
#define RADIOLIB_SX128X_BLE_ALL_0 0x14 // 7 0 repeated 0x00
#define RADIOLIB_SX128X_BLE_EYELONG_INV 0x18 // 7 0 repeated 0x0F
#define RADIOLIB_SX128X_BLE_EYESHORT_INV 0x1C // 7 0 repeated 0x55
#define RADIOLIB_SX128X_FLRC_SYNC_WORD_OFF 0x00 // 7 0 FLRC sync word: disabled
#define RADIOLIB_SX128X_FLRC_SYNC_WORD_ON 0x04 // 7 0 enabled
#define RADIOLIB_SX128X_LORA_HEADER_EXPLICIT 0x00 // 7 0 LoRa header mode: explicit
#define RADIOLIB_SX128X_LORA_HEADER_IMPLICIT 0x80 // 7 0 implicit
#define RADIOLIB_SX128X_LORA_CRC_OFF 0x00 // 7 0 LoRa packet CRC: disabled
#define RADIOLIB_SX128X_LORA_CRC_ON 0x20 // 7 0 enabled
#define RADIOLIB_SX128X_LORA_IQ_STANDARD 0x40 // 7 0 LoRa IQ: standard
#define RADIOLIB_SX128X_LORA_IQ_INVERTED 0x00 // 7 0 inverted
//RADIOLIB_SX128X_CMD_GET_PACKET_STATUS
#define RADIOLIB_SX128X_PACKET_STATUS_SYNC_ERROR 0b01000000 // 6 6 packet status errors byte: sync word error
#define RADIOLIB_SX128X_PACKET_STATUS_LENGTH_ERROR 0b00100000 // 5 5 packet length error
#define RADIOLIB_SX128X_PACKET_STATUS_CRC_ERROR 0b00010000 // 4 4 CRC error
#define RADIOLIB_SX128X_PACKET_STATUS_ABORT_ERROR 0b00001000 // 3 3 packet reception aborted
#define RADIOLIB_SX128X_PACKET_STATUS_HEADER_RECEIVED 0b00000100 // 2 2 header received
#define RADIOLIB_SX128X_PACKET_STATUS_PACKET_RECEIVED 0b00000010 // 1 1 packet received
#define RADIOLIB_SX128X_PACKET_STATUS_PACKET_CTRL_BUSY 0b00000001 // 0 0 packet controller is busy
#define RADIOLIB_SX128X_PACKET_STATUS_RX_PID 0b11000000 // 7 6 packet status status byte: PID field of the received packet
#define RADIOLIB_SX128X_PACKET_STATUS_NO_ACK 0b00100000 // 5 5 NO_ACK field of the received packet
#define RADIOLIB_SX128X_PACKET_STATUS_RX_PID_ERROR 0b00010000 // 4 4 PID field error
#define RADIOLIB_SX128X_PACKET_STATUS_PACKET_SENT 0b00000001 // 0 0 packet sent
#define RADIOLIB_SX128X_PACKET_STATUS_SYNC_DET_ERROR 0b00000000 // 2 0 packet status sync byte: sync word detection error
#define RADIOLIB_SX128X_PACKET_STATUS_SYNC_DET_1 0b00000001 // 2 0 detected sync word 1
#define RADIOLIB_SX128X_PACKET_STATUS_SYNC_DET_2 0b00000010 // 2 0 detected sync word 2
#define RADIOLIB_SX128X_PACKET_STATUS_SYNC_DET_3 0b00000100 // 2 0 detected sync word 3
//RADIOLIB_SX128X_CMD_SET_DIO_IRQ_PARAMS
#define RADIOLIB_SX128X_IRQ_RADIOLIB_PREAMBLE_DETECTED 0x8000 // 15 15 interrupt source: preamble detected
#define RADIOLIB_SX128X_IRQ_ADVANCED_RANGING_DONE 0x8000 // 15 15 advanced ranging done
#define RADIOLIB_SX128X_IRQ_RX_TX_TIMEOUT 0x4000 // 14 14 Rx or Tx timeout
#define RADIOLIB_SX128X_IRQ_CAD_DETECTED 0x2000 // 13 13 channel activity detected
#define RADIOLIB_SX128X_IRQ_CAD_DONE 0x1000 // 12 12 CAD finished
#define RADIOLIB_SX128X_IRQ_RANGING_SLAVE_REQ_VALID 0x0800 // 11 11 ranging request valid (slave)
#define RADIOLIB_SX128X_IRQ_RANGING_MASTER_TIMEOUT 0x0400 // 10 10 ranging timeout (master)
#define RADIOLIB_SX128X_IRQ_RANGING_MASTER_RES_VALID 0x0200 // 9 9 ranging result valid (master)
#define RADIOLIB_SX128X_IRQ_RANGING_SLAVE_REQ_DISCARD 0x0100 // 8 8 ranging result valid (master)
#define RADIOLIB_SX128X_IRQ_RANGING_SLAVE_RESP_DONE 0x0080 // 7 7 ranging response complete (slave)
#define RADIOLIB_SX128X_IRQ_CRC_ERROR 0x0040 // 6 6 CRC error
#define RADIOLIB_SX128X_IRQ_HEADER_ERROR 0x0020 // 5 5 header error
#define RADIOLIB_SX128X_IRQ_HEADER_VALID 0x0010 // 4 4 header valid
#define RADIOLIB_SX128X_IRQ_SYNC_WORD_ERROR 0x0008 // 3 3 sync word error
#define RADIOLIB_SX128X_IRQ_SYNC_WORD_VALID 0x0004 // 2 2 sync word valid
#define RADIOLIB_SX128X_IRQ_RX_DONE 0x0002 // 1 1 Rx done
#define RADIOLIB_SX128X_IRQ_TX_DONE 0x0001 // 0 0 Tx done
#define RADIOLIB_SX128X_IRQ_NONE 0x0000 // 15 0 none
#define RADIOLIB_SX128X_IRQ_ALL 0xFFFF // 15 0 all
//RADIOLIB_SX128X_CMD_SET_REGULATOR_MODE
#define RADIOLIB_SX128X_REGULATOR_LDO 0x00 // 7 0 set regulator mode: LDO (default)
#define RADIOLIB_SX128X_REGULATOR_DC_DC 0x01 // 7 0 DC-DC
//RADIOLIB_SX128X_CMD_SET_RANGING_ROLE
#define RADIOLIB_SX128X_RANGING_ROLE_MASTER 0x01 // 7 0 ranging role: master
#define RADIOLIB_SX128X_RANGING_ROLE_SLAVE 0x00 // 7 0 slave
//RADIOLIB_SX128X_REG_LORA_SYNC_WORD_1 - RADIOLIB_SX128X_REG_LORA_SYNC_WORD_2
#define RADIOLIB_SX128X_SYNC_WORD_PRIVATE 0x12
/*!
\class SX128x
\brief Base class for %SX128x series. All derived classes for %SX128x (e.g. SX1280 or SX1281) inherit from this base class.
This class should not be instantiated directly from Arduino sketch, only from its derived classes.
*/
class SX128x: public PhysicalLayer {
public:
// introduce PhysicalLayer overloads
using PhysicalLayer::transmit;
using PhysicalLayer::receive;
using PhysicalLayer::startTransmit;
using PhysicalLayer::readData;
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
SX128x(Module* mod);
Module* getMod();
// basic methods
/*!
\brief Initialization method for LoRa modem.
\param freq Carrier frequency in MHz. Defaults to 2400.0 MHz.
\param bw LoRa bandwidth in kHz. Defaults to 812.5 kHz.
\param sf LoRa spreading factor. Defaults to 9.
\param cr LoRa coding rate denominator. Defaults to 7 (coding rate 4/7).
\param syncWord 2-byte LoRa sync word. Defaults to RADIOLIB_SX128X_SYNC_WORD_PRIVATE (0x12).
\param power Output power in dBm. Defaults to 10 dBm.
\param preambleLength LoRa preamble length in symbols. Defaults to 12 symbols.
\returns \ref status_codes
*/
int16_t begin(float freq = 2400.0, float bw = 812.5, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX128X_SYNC_WORD_PRIVATE, int8_t power = 10, uint16_t preambleLength = 12);
/*!
\brief Initialization method for GFSK modem.
\param freq Carrier frequency in MHz. Defaults to 2400.0 MHz.
\param br FSK bit rate in kbps. Defaults to 800 kbps.
\param freqDev Frequency deviation from carrier frequency in kHz. Defaults to 400.0 kHz.
\param power Output power in dBm. Defaults to 10 dBm.
\parma preambleLength FSK preamble length in bits. Defaults to 16 bits.
\returns \ref status_codes
*/
int16_t beginGFSK(float freq = 2400.0, uint16_t br = 800, float freqDev = 400.0, int8_t power = 10, uint16_t preambleLength = 16);
/*!
\brief Initialization method for BLE modem.
\param freq Carrier frequency in MHz. Defaults to 2400.0 MHz.
\param br BLE bit rate in kbps. Defaults to 800 kbps.
\param freqDev Frequency deviation from carrier frequency in kHz. Defaults to 400.0 kHz.
\param power Output power in dBm. Defaults to 10 dBm.
\param dataShaping Time-bandwidth product of the Gaussian filter to be used for shaping. Defaults to 0.5.
\returns \ref status_codes
*/
int16_t beginBLE(float freq = 2400.0, uint16_t br = 800, float freqDev = 400.0, int8_t power = 10, uint8_t dataShaping = RADIOLIB_SHAPING_0_5);
/*!
\brief Initialization method for FLRC modem.
\param freq Carrier frequency in MHz. Defaults to 2400.0 MHz.
\param br FLRC bit rate in kbps. Defaults to 650 kbps.
\param cr FLRC coding rate. Defaults to 3 (coding rate 3/4).
\param power Output power in dBm. Defaults to 10 dBm.
\parma preambleLength FLRC preamble length in bits. Defaults to 16 bits.
\param dataShaping Time-bandwidth product of the Gaussian filter to be used for shaping. Defaults to 0.5.
\returns \ref status_codes
*/
int16_t beginFLRC(float freq = 2400.0, uint16_t br = 650, uint8_t cr = 3, int8_t power = 10, uint16_t preambleLength = 16, uint8_t dataShaping = RADIOLIB_SHAPING_0_5);
/*!
\brief Reset method. Will reset the chip to the default state using RST pin.
\param verify Whether correct module startup should be verified. When set to true, RadioLib will attempt to verify the module has started correctly
by repeatedly issuing setStandby command. Enabled by default.
\returns \ref status_codes
*/
int16_t reset(bool verify = true);
/*!
\brief Blocking binary transmit method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\param addr Address to send the data to. Unsupported, compatibility only.
\returns \ref status_codes
*/
int16_t transmit(uint8_t* data, size_t len, uint8_t addr = 0) override;
/*!
\brief Blocking binary receive method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\returns \ref status_codes
*/
int16_t receive(uint8_t* data, size_t len) override;
/*!
\brief Starts direct mode transmission.
\param frf Raw RF frequency value. Defaults to 0, required for quick frequency shifts in RTTY.
\returns \ref status_codes
*/
int16_t transmitDirect(uint32_t frf = 0) override;
/*!
\brief Starts direct mode reception. Only implemented for PhysicalLayer compatibility, as %SX128x series does not support direct mode reception.
Will always return RADIOLIB_ERR_UNKNOWN.
\returns \ref status_codes
*/
int16_t receiveDirect() override;
/*!
\brief Performs scan for LoRa transmission in the current channel. Detects both preamble and payload.
\returns \ref status_codes
*/
int16_t scanChannel();
/*!
\brief Sets the module to sleep mode.
\param retainConfig Set to true to retain configuration and data buffer or to false to discard current configuration and data buffer. Defaults to true.
\returns \ref status_codes
*/
int16_t sleep(bool retainConfig = true);
/*!
\brief Sets the module to standby mode (overload for PhysicalLayer compatibility, uses 13 MHz RC oscillator).
\returns \ref status_codes
*/
int16_t standby() override;
/*!
\brief Sets the module to standby mode.
\param mode Oscillator to be used in standby mode. Can be set to RADIOLIB_SX128X_STANDBY_RC (13 MHz RC oscillator) or RADIOLIB_SX128X_STANDBY_XOSC (52 MHz external crystal oscillator).
\returns \ref status_codes
*/
int16_t standby(uint8_t mode);
// interrupt methods
/*!
\brief Sets interrupt service routine to call when DIO1 activates.
\param func ISR to call.
*/
void setDio1Action(void (*func)(void));
/*!
\brief Clears interrupt service routine to call when DIO1 activates.
*/
void clearDio1Action();
/*!
\brief Interrupt-driven binary transmit method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\param addr Address to send the data to. Unsupported, compatibility only.
\returns \ref status_codes
*/
int16_t startTransmit(uint8_t* data, size_t len, uint8_t addr = 0) override;
/*!
\brief Interrupt-driven receive method. DIO1 will be activated when full packet is received.
\param timeout Raw timeout value, expressed as multiples of 15.625 us. Defaults to RADIOLIB_SX128X_RX_TIMEOUT_INF for infinite timeout (Rx continuous mode), set to RADIOLIB_SX128X_RX_TIMEOUT_NONE for no timeout (Rx single mode).
\returns \ref status_codes
*/
int16_t startReceive(uint16_t timeout = RADIOLIB_SX128X_RX_TIMEOUT_INF);
/*!
\brief Reads data received after calling startReceive method.
\param data Pointer to array to save the received binary data.
\param len Number of bytes that will be read. When set to 0, the packet length will be retreived automatically.
When more bytes than received are requested, only the number of bytes requested will be returned.
\returns \ref status_codes
*/
int16_t readData(uint8_t* data, size_t len) override;
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values are in range from 2400.0 to 2500.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
/*!
\brief Sets LoRa bandwidth. Allowed values are 203.125, 406.25, 812.5 and 1625.0 kHz.
\param bw LoRa bandwidth to be set in kHz.
\returns \ref status_codes
*/
int16_t setBandwidth(float bw);
/*!
\brief Sets LoRa spreading factor. Allowed values range from 5 to 12.
\param sf LoRa spreading factor to be set.
\returns \ref status_codes
*/
int16_t setSpreadingFactor(uint8_t sf);
/*!
\brief Sets LoRa coding rate denominator. Allowed values range from 5 to 8.
\param cr LoRa coding rate denominator to be set.
\param longInterleaving Whether to enable long interleaving mode. Not available for coding rate 4/7, defaults to false.
\returns \ref status_codes
*/
int16_t setCodingRate(uint8_t cr, bool longInterleaving = false);
/*!
\brief Sets output power. Allowed values are in range from -18 to 13 dBm.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
/*!
\brief Sets preamble length for currently active modem. Allowed values range from 1 to 65535.
\param preambleLength Preamble length to be set in symbols (LoRa) or bits (FSK/BLE/FLRC).
\returns \ref status_codes
*/
int16_t setPreambleLength(uint32_t preambleLength);
/*!
\brief Sets FSK or FLRC bit rate. Allowed values are 125, 250, 400, 500, 800, 1000, 1600 and 2000 kbps (for FSK modem) or 260, 325, 520, 650, 1000 and 1300 (for FLRC modem).
\param br FSK/FLRC bit rate to be set in kbps.
\returns \ref status_codes
*/
int16_t setBitRate(uint16_t br);
/*!
\brief Sets FSK frequency deviation. Allowed values range from 0.0 to 3200.0 kHz.
\param freqDev FSK frequency deviation to be set in kHz.
\returns \ref status_codes
*/
int16_t setFrequencyDeviation(float freqDev) override;
/*!
\brief Sets time-bandwidth product of Gaussian filter applied for shaping.
Allowed values are RADIOLIB_SHAPING_0_5 or RADIOLIB_SHAPING_1_0. Set to RADIOLIB_SHAPING_NONE to disable data shaping.
\param sh Time-bandwidth product of Gaussian filter to be set.
\returns \ref status_codes
*/
int16_t setDataShaping(uint8_t sh) override;
/*!
\brief Sets FSK/FLRC sync word in the form of array of up to 5 bytes (FSK). For FLRC modem, the sync word must be exactly 4 bytes long
\param syncWord Sync word to be set.
\param len Sync word length in bytes.
\returns \ref status_codes
*/
int16_t setSyncWord(uint8_t* syncWord, uint8_t len);
/*!
\brief Sets LoRa sync word.
\param syncWord LoRa sync word to be set.
\param controlBits Undocumented control bits, required for compatibility purposes.
\returns \ref status_codes
*/
int16_t setSyncWord(uint8_t syncWord, uint8_t controlBits = 0x44);
/*!
\brief Sets CRC configuration.
\param len CRC length in bytes, Allowed values are 1, 2 or 3, set to 0 to disable CRC.
\param initial Initial CRC value. Defaults to 0x1D0F (CCIT CRC), not available for LoRa modem.
\param polynomial Polynomial for CRC calculation. Defaults to 0x1021 (CCIT CRC), not available for LoRa or BLE modem.
\returns \ref status_codes
*/
int16_t setCRC(uint8_t len, uint32_t initial = 0x1D0F, uint16_t polynomial = 0x1021);
/*!
\brief Sets whitening parameters, not available for LoRa or FLRC modem.
\param enabled Set to true to enable whitening.
\returns \ref status_codes
*/
int16_t setWhitening(bool enabled);
/*!
\brief Sets BLE access address.
\param addr BLE access address.
\returns \ref status_codes
*/
int16_t setAccessAddress(uint32_t addr);
/*!
\brief Enables or disables receiver high sensitivity mode.
\param True to enable and false to disable.
\returns 0
*/
int16_t setHighSensitivityMode(bool hsm = false);
/*!
\brief Enables or disables receiver manual gain control.
\param gain Use 0 for automatic gain, 1 for minimum gain and up to 13 for maximum gain.
\returns 0
*/
int16_t setGainControl(uint8_t gain = 0);
/*!
\brief Gets RSSI (Recorded Signal Strength Indicator) of the last received packet.
\returns RSSI of the last received packet in dBm.
*/
float getRSSI();
/*!
\brief Gets SNR (Signal to Noise Ratio) of the last received packet. Only available for LoRa or ranging modem.
\returns SNR of the last received packet in dB.
*/
float getSNR();
/*!
\brief Gets frequency error of the latest received packet.
\returns Frequency error in Hz.
*/
float getFrequencyError();
/*!
\brief Query modem for the packet length of received payload.
\param update Update received packet length. Will return cached value when set to false.
\returns Length of last received packet in bytes.
*/
size_t getPacketLength(bool update = true) override;
/*!
\brief Get expected time-on-air for a given size of payload.
\param len Payload length in bytes.
\returns Expected time-on-air in microseconds.
*/
uint32_t getTimeOnAir(size_t len);
/*!
\brief Set implicit header mode for future reception/transmission.
\returns \ref status_codes
*/
int16_t implicitHeader(size_t len);
/*!
\brief Set explicit header mode for future reception/transmission.
\param len Payload length in bytes.
\returns \ref status_codes
*/
int16_t explicitHeader();
/*!
\brief Sets transmission encoding. Serves only as alias for PhysicalLayer compatibility.
\param encoding Encoding to be used. Set to 0 for NRZ, and 2 for whitening.
\returns \ref status_codes
*/
int16_t setEncoding(uint8_t encoding) override;
/*!
\brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state.
When using automatic RF switch control, DO NOT change the pin mode of rxEn or txEn from Arduino sketch!
\param rxEn RX enable pin.
\param txEn TX enable pin.
*/
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
/*!
\brief Dummy random method, to ensure PhysicalLayer compatibility.
\returns Always returns 0.
*/
uint8_t randomByte();
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
/*!
\brief Dummy method, to ensure PhysicalLayer compatibility.
\param func Ignored.
*/
void setDirectAction(void (*func)(void));
/*!
\brief Dummy method, to ensure PhysicalLayer compatibility.
\param pin Ignored.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
#endif
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)
protected:
#endif
Module* _mod;
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
// cached LoRa parameters
float _bwKhz = 0;
uint8_t _bw = 0, _sf = 0, _cr = 0;
uint8_t _preambleLengthLoRa = 0, _headerType = 0, _payloadLen = 0, _crcLoRa = 0;
// SX128x SPI command implementations
uint8_t getStatus();
int16_t writeRegister(uint16_t addr, uint8_t* data, uint8_t numBytes);
int16_t readRegister(uint16_t addr, uint8_t* data, uint8_t numBytes);
int16_t writeBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset = 0x00);
int16_t readBuffer(uint8_t* data, uint8_t numBytes);
int16_t setTx(uint16_t periodBaseCount = RADIOLIB_SX128X_TX_TIMEOUT_NONE, uint8_t periodBase = RADIOLIB_SX128X_PERIOD_BASE_15_625_US);
int16_t setRx(uint16_t periodBaseCount, uint8_t periodBase = RADIOLIB_SX128X_PERIOD_BASE_15_625_US);
int16_t setCad();
uint8_t getPacketType();
int16_t setRfFrequency(uint32_t frf);
int16_t setTxParams(uint8_t power, uint8_t rampTime = RADIOLIB_SX128X_PA_RAMP_10_US);
int16_t setBufferBaseAddress(uint8_t txBaseAddress = 0x00, uint8_t rxBaseAddress = 0x00);
int16_t setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_t modParam3);
int16_t setPacketParamsGFSK(uint8_t preambleLen, uint8_t syncWordLen, uint8_t syncWordMatch, uint8_t crcLen, uint8_t whitening, uint8_t payloadLen = 0xFF, uint8_t headerType = RADIOLIB_SX128X_GFSK_FLRC_PACKET_VARIABLE);
int16_t setPacketParamsBLE(uint8_t connState, uint8_t crcLen, uint8_t bleTestPayload, uint8_t whitening);
int16_t setPacketParamsLoRa(uint8_t preambleLen, uint8_t headerType, uint8_t payloadLen, uint8_t crc, uint8_t invertIQ = RADIOLIB_SX128X_LORA_IQ_STANDARD);
int16_t setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask = RADIOLIB_SX128X_IRQ_NONE, uint16_t dio3Mask = RADIOLIB_SX128X_IRQ_NONE);
uint16_t getIrqStatus();
int16_t clearIrqStatus(uint16_t clearIrqParams = RADIOLIB_SX128X_IRQ_ALL);
int16_t setRangingRole(uint8_t role);
int16_t setPacketType(uint8_t type);
int16_t setHeaderType(uint8_t headerType, size_t len = 0xFF);
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)
private:
#endif
// common low-level SPI interface
int16_t SPIwriteCommand(uint8_t cmd, uint8_t* data, uint8_t numBytes, bool waitForBusy = true);
int16_t SPIwriteCommand(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, uint8_t numBytes, bool waitForBusy = true);
int16_t SPIreadCommand(uint8_t cmd, uint8_t* data, uint8_t numBytes, bool waitForBusy = true);
int16_t SPIreadCommand(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, uint8_t numBytes, bool waitForBusy = true);
int16_t SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes, bool waitForBusy, uint32_t timeout = 5000);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
// common parameters
uint8_t _pwr = 0;
// cached GFSK parameters
float _modIndexReal = 0;
uint16_t _brKbps = 0;
uint8_t _br = 0, _modIndex = 0, _shaping = 0;
uint8_t _preambleLengthGFSK = 0, _syncWordLen = 0, _syncWordMatch = 0, _crcGFSK = 0, _whitening = 0;
// cached FLRC parameters
uint8_t _crFLRC = 0;
// cached BLE parameters
uint8_t _connectionState = 0, _crcBLE = 0, _bleTestPayload = 0;
int16_t config(uint8_t modem);
};
#endif
#endif
@@ -0,0 +1,38 @@
#include "Si4430.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
Si4430::Si4430(Module* mod) : Si4432(mod) {
}
int16_t Si4430::begin(float freq, float br, float freqDev, float rxBw, int8_t power, uint8_t preambleLen) {
// execute common part
int16_t state = Si443x::begin(br, freqDev, rxBw, preambleLen);
RADIOLIB_ASSERT(state);
RADIOLIB_DEBUG_PRINTLN(F("M\tSi4430"));
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t Si4430::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 900.0, 960.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency
return(Si443x::setFrequencyRaw(freq));
}
int16_t Si4430::setOutputPower(int8_t power) {
RADIOLIB_CHECK_RANGE(power, -8, 13, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
// set output power
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_TX_POWER, (uint8_t)((power + 8) / 3), 2, 0));
}
#endif
+81
View File
@@ -0,0 +1,81 @@
#if !defined(_RADIOLIB_SI4430_H)
#define _RADIOLIB_SI4430_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
#include "../../Module.h"
#include "Si4432.h"
/*!
\class Si4430
\brief Derived class for %Si4430 modules.
*/
class Si4430: public Si4432 {
public:
// constructor
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio chip.
*/
Si4430(Module* mod);
// basic methods
/*!
\brief Initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 900.0 MHz to 960.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 0.123 to 256.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.625 to 320.0 kbps.
\param rxBw Receiver bandwidth in kHz. Allowed values range from 2.6 to 620.7 kHz.
\param power Transmission output power in dBm. Allowed values range from -8 to 13 dBm in 3 dBm steps.
\param preambleLen Preamble Length in bits. Defaults to 16 bits.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 181.1, int8_t power = 10, uint8_t preambleLen = 16);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 900.0 MHz to 960.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
/*!
\brief Sets output power. Allowed values range from -8 to 13 dBm in 3 dBm steps.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
@@ -0,0 +1,31 @@
#include "Si4431.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
Si4431::Si4431(Module* mod) : Si4432(mod) {
}
int16_t Si4431::begin(float freq, float br, float freqDev, float rxBw, int8_t power, uint8_t preambleLen) {
// execute common part
int16_t state = Si443x::begin(br, freqDev, rxBw, preambleLen);
RADIOLIB_ASSERT(state);
RADIOLIB_DEBUG_PRINTLN(F("M\tSi4431"));
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t Si4431::setOutputPower(int8_t power) {
RADIOLIB_CHECK_RANGE(power, -8, 13, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
// set output power
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_TX_POWER, (uint8_t)((power + 8) / 3), 2, 0));
}
#endif
+72
View File
@@ -0,0 +1,72 @@
#if !defined(_RADIOLIB_SI4431_H)
#define _RADIOLIB_SI4431_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
#include "../../Module.h"
#include "Si4432.h"
/*!
\class Si4431
\brief Derived class for %Si4431 modules.
*/
class Si4431: public Si4432 {
public:
// constructor
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio chip.
*/
Si4431(Module* mod);
// basic methods
/*!
\brief Initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 240.0 MHz to 930.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 0.123 to 256.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.625 to 320.0 kbps.
\param rxBw Receiver bandwidth in kHz. Allowed values range from 2.6 to 620.7 kHz.
\param power Transmission output power in dBm. Allowed values range from -8 to 13 dBm in 3 dBm steps.
\param preambleLen Preamble Length in bits. Defaults to 16 bits.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 181.1, int8_t power = 10, uint8_t preambleLen = 16);
// configuration methods
/*!
\brief Sets output power. Allowed values range from -8 to 13 dBm in 3 dBm steps.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
@@ -0,0 +1,38 @@
#include "Si4432.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
Si4432::Si4432(Module* mod) : Si443x(mod) {
}
int16_t Si4432::begin(float freq, float br, float freqDev, float rxBw, int8_t power, uint8_t preambleLen) {
// execute common part
int16_t state = Si443x::begin(br, freqDev, rxBw, preambleLen);
RADIOLIB_ASSERT(state);
RADIOLIB_DEBUG_PRINTLN(F("M\tSi4432"));
// configure publicly accessible settings
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t Si4432::setFrequency(float freq) {
RADIOLIB_CHECK_RANGE(freq, 240.0, 930.0, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency
return(Si443x::setFrequencyRaw(freq));
}
int16_t Si4432::setOutputPower(int8_t power) {
RADIOLIB_CHECK_RANGE(power, -1, 20, RADIOLIB_ERR_INVALID_OUTPUT_POWER);
// set output power
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_TX_POWER, (uint8_t)((power + 1) / 3), 2, 0));
}
#endif
+81
View File
@@ -0,0 +1,81 @@
#if !defined(_RADIOLIB_SI4432_H)
#define _RADIOLIB_SI4432_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
#include "../../Module.h"
#include "Si443x.h"
/*!
\class Si4432
\brief Derived class for %Si4432 modules.
*/
class Si4432: public Si443x {
public:
// constructor
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio chip.
*/
Si4432(Module* mod);
// basic methods
/*!
\brief Initialization method. Must be called at least once from Arduino sketch to initialize the module.
\param freq Carrier frequency in MHz. Allowed values range from 240.0 MHz to 930.0 MHz.
\param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 0.123 to 256.0 kbps.
\param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.625 to 320.0 kbps.
\param rxBw Receiver bandwidth in kHz. Allowed values range from 2.6 to 620.7 kHz.
\param power Transmission output power in dBm. Allowed values range from -1 to 20 dBm in 3 dBm steps.
\param preambleLen Preamble Length in bits. Defaults to 16 bits.
\returns \ref status_codes
*/
int16_t begin(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 181.1, int8_t power = 10, uint8_t preambleLen = 16);
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 240.0 MHz to 930.0 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(float freq);
/*!
\brief Sets output power. Allowed values range from -1 to 20 dBm in 3 dBm steps.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
#if !defined(RADIOLIB_GODMODE)
private:
#endif
};
#endif
#endif
+800
View File
@@ -0,0 +1,800 @@
#include "Si443x.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
Si443x::Si443x(Module* mod) : PhysicalLayer(RADIOLIB_SI443X_FREQUENCY_STEP_SIZE, RADIOLIB_SI443X_MAX_PACKET_LENGTH) {
_mod = mod;
}
Module* Si443x::getMod() {
return(_mod);
}
int16_t Si443x::begin(float br, float freqDev, float rxBw, uint8_t preambleLen) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
// try to find the Si443x chip
if(!Si443x::findChip()) {
RADIOLIB_DEBUG_PRINTLN(F("No Si443x found!"));
_mod->term();
return(RADIOLIB_ERR_CHIP_NOT_FOUND);
} else {
RADIOLIB_DEBUG_PRINTLN(F("M\tSi443x"));
}
// reset the device
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_SOFTWARE_RESET);
// clear POR interrupt
clearIRQFlags();
// configure settings not accessible by API
int16_t state = config();
RADIOLIB_ASSERT(state);
// configure publicly accessible settings
state = setBitRate(br);
RADIOLIB_ASSERT(state);
state = setFrequencyDeviation(freqDev);
RADIOLIB_ASSERT(state);
state = setRxBandwidth(rxBw);
RADIOLIB_ASSERT(state);
state = setPreambleLength(preambleLen);
RADIOLIB_ASSERT(state);
uint8_t syncWord[] = {0x12, 0xAD};
state = setSyncWord(syncWord, sizeof(syncWord));
RADIOLIB_ASSERT(state);
state = packetMode();
RADIOLIB_ASSERT(state);
state = setDataShaping(0);
RADIOLIB_ASSERT(state);
state = setEncoding(0);
RADIOLIB_ASSERT(state);
state = variablePacketLengthMode();
return(state);
}
void Si443x::reset() {
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(100);
}
int16_t Si443x::transmit(uint8_t* data, size_t len, uint8_t addr) {
// calculate timeout (5ms + 500 % of expected time-on-air)
uint32_t timeout = 5000000 + (uint32_t)((((float)(len * 8)) / (_br * 1000.0)) * 5000000.0);
// start transmission
int16_t state = startTransmit(data, len, addr);
RADIOLIB_ASSERT(state);
// wait for transmission end or timeout
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
standby();
clearIRQFlags();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
}
// clear interrupt flags
clearIRQFlags();
// set mode to standby
standby();
return(state);
}
int16_t Si443x::receive(uint8_t* data, size_t len) {
// calculate timeout (500 ms + 400 full 64-byte packets at current bit rate)
uint32_t timeout = 500000 + (1.0/(_br*1000.0))*(RADIOLIB_SI443X_MAX_PACKET_LENGTH*400.0);
// start reception
int16_t state = startReceive();
RADIOLIB_ASSERT(state);
// wait for packet reception or timeout
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
if(_mod->micros() - start > timeout) {
standby();
clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT);
}
}
// read packet data
return(readData(data, len));
}
int16_t Si443x::sleep() {
// set RF switch (if present)
_mod->setRfSwitchState(LOW, LOW);
// disable wakeup timer interrupt
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_1, 0x00);
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_2, 0x00);
RADIOLIB_ASSERT(state);
// enable wakeup timer to set mode to sleep
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_ENABLE_WAKEUP_TIMER);
return(state);
}
int16_t Si443x::standby() {
// set RF switch (if present)
_mod->setRfSwitchState(LOW, LOW);
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_XTAL_ON, 7, 0, 10));
}
int16_t Si443x::transmitDirect(uint32_t frf) {
// set RF switch (if present)
_mod->setRfSwitchState(LOW, HIGH);
// user requested to start transmitting immediately (required for RTTY)
if(frf != 0) {
// convert the 24-bit frequency to the format accepted by the module
/// \todo integers only
float newFreq = frf / 6400.0;
// check high/low band
uint8_t bandSelect = RADIOLIB_SI443X_BAND_SELECT_LOW;
uint8_t freqBand = (newFreq / 10) - 24;
if(newFreq >= 480.0) {
bandSelect = RADIOLIB_SI443X_BAND_SELECT_HIGH;
freqBand = (newFreq / 20) - 24;
}
// calculate register values
uint16_t freqCarrier = ((newFreq / (10 * ((bandSelect >> 5) + 1))) - freqBand - 24) * (uint32_t)64000;
// update registers
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_FREQUENCY_BAND_SELECT, RADIOLIB_SI443X_SIDE_BAND_SELECT_LOW | bandSelect | freqBand);
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_1, (uint8_t)((freqCarrier & 0xFF00) >> 8));
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_0, (uint8_t)(freqCarrier & 0xFF));
// start direct transmission
directMode();
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_TX_ON | RADIOLIB_SI443X_XTAL_ON);
return(RADIOLIB_ERR_NONE);
}
// activate direct mode
int16_t state = directMode();
RADIOLIB_ASSERT(state);
// start transmitting
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_TX_ON | RADIOLIB_SI443X_XTAL_ON);
return(state);
}
int16_t Si443x::receiveDirect() {
// set RF switch (if present)
_mod->setRfSwitchState(HIGH, LOW);
// activate direct mode
int16_t state = directMode();
RADIOLIB_ASSERT(state);
// start receiving
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_RX_ON | RADIOLIB_SI443X_XTAL_ON);
return(state);
}
int16_t Si443x::packetMode() {
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_2, RADIOLIB_SI443X_MODULATION_FSK, 1, 0);
RADIOLIB_ASSERT(state);
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_2, RADIOLIB_SI443X_TX_DATA_SOURCE_FIFO, 5, 4));
}
void Si443x::setIrqAction(void (*func)(void)) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, FALLING);
}
void Si443x::clearIrqAction() {
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
}
int16_t Si443x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
// check packet length
if(len > RADIOLIB_SI443X_MAX_PACKET_LENGTH) {
return(RADIOLIB_ERR_PACKET_TOO_LONG);
}
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// clear Tx FIFO
_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_2, RADIOLIB_SI443X_TX_FIFO_RESET, 0, 0);
_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_2, RADIOLIB_SI443X_TX_FIFO_CLEAR, 0, 0);
// clear interrupt flags
clearIRQFlags();
// set packet length
if (_packetLengthConfig == RADIOLIB_SI443X_FIXED_PACKET_LENGTH_OFF) {
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_TRANSMIT_PACKET_LENGTH, len);
}
/// \todo use header as address field?
(void)addr;
// write packet to FIFO
_mod->SPIwriteRegisterBurst(RADIOLIB_SI443X_REG_FIFO_ACCESS, data, len);
// set RF switch (if present)
_mod->setRfSwitchState(LOW, HIGH);
// set interrupt mapping
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_1, RADIOLIB_SI443X_PACKET_SENT_ENABLED);
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_2, 0x00);
// set mode to transmit
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_TX_ON | RADIOLIB_SI443X_XTAL_ON);
return(state);
}
int16_t Si443x::startReceive() {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// clear Rx FIFO
_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_2, RADIOLIB_SI443X_RX_FIFO_RESET, 1, 1);
_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_2, RADIOLIB_SI443X_RX_FIFO_CLEAR, 1, 1);
// clear interrupt flags
clearIRQFlags();
// set RF switch (if present)
_mod->setRfSwitchState(HIGH, LOW);
// set interrupt mapping
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_1, RADIOLIB_SI443X_VALID_PACKET_RECEIVED_ENABLED | RADIOLIB_SI443X_CRC_ERROR_ENABLED);
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_2, 0x00);
// set mode to receive
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_RX_ON | RADIOLIB_SI443X_XTAL_ON);
return(state);
}
int16_t Si443x::readData(uint8_t* data, size_t len) {
// clear interrupt flags
clearIRQFlags();
// get packet length
size_t length = getPacketLength();
size_t dumpLen = 0;
if((len != 0) && (len < length)) {
// user requested less data than we got, only return what was requested
dumpLen = length - len;
length = len;
}
// read packet data
_mod->SPIreadRegisterBurst(RADIOLIB_SI443X_REG_FIFO_ACCESS, length, data);
// dump the bytes that weren't requested
if(dumpLen != 0) {
clearFIFO(dumpLen);
}
// clear internal flag so getPacketLength can return the new packet length
_packetLengthQueried = false;
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// clear interrupt flags
clearIRQFlags();
return(RADIOLIB_ERR_NONE);
}
int16_t Si443x::setBitRate(float br) {
RADIOLIB_CHECK_RANGE(br, 0.123, 256.0, RADIOLIB_ERR_INVALID_BIT_RATE);
// check high data rate
uint8_t dataRateMode = RADIOLIB_SI443X_LOW_DATA_RATE_MODE;
uint8_t exp = 21;
if(br >= 30.0) {
// bit rate above 30 kbps
dataRateMode = RADIOLIB_SI443X_HIGH_DATA_RATE_MODE;
exp = 16;
}
// calculate raw data rate value
uint16_t txDr = (br * ((uint32_t)1 << exp)) / 1000.0;
// update registers
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1, dataRateMode, 5, 5);
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_TX_DATA_RATE_1, (uint8_t)((txDr & 0xFF00) >> 8));
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_TX_DATA_RATE_0, (uint8_t)(txDr & 0xFF));
if(state == RADIOLIB_ERR_NONE) {
_br = br;
}
RADIOLIB_ASSERT(state);
// update clock recovery
state = updateClockRecovery();
return(state);
}
int16_t Si443x::setFrequencyDeviation(float freqDev) {
// set frequency deviation to lowest available setting (required for digimodes)
float newFreqDev = freqDev;
if(freqDev < 0.0) {
newFreqDev = 0.625;
}
RADIOLIB_CHECK_RANGE(newFreqDev, 0.625, 320.0, RADIOLIB_ERR_INVALID_FREQUENCY_DEVIATION);
// calculate raw frequency deviation value
uint16_t fdev = (uint16_t)(newFreqDev / 0.625);
// update registers
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_2, (uint8_t)((fdev & 0x0100) >> 6), 2, 2);
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_FREQUENCY_DEVIATION, (uint8_t)(fdev & 0xFF));
if(state == RADIOLIB_ERR_NONE) {
_freqDev = newFreqDev;
}
return(state);
}
int16_t Si443x::setRxBandwidth(float rxBw) {
RADIOLIB_CHECK_RANGE(rxBw, 2.6, 620.7, RADIOLIB_ERR_INVALID_RX_BANDWIDTH);
// decide which approximation to use for decimation rate and filter tap calculation
uint8_t bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_OFF;
uint8_t decRate = RADIOLIB_SI443X_IF_FILTER_DEC_RATE;
uint8_t filterSet = RADIOLIB_SI443X_IF_FILTER_COEFF_SET;
// this is the "well-behaved" section - can be linearly approximated
if((rxBw >= 2.6) && (rxBw <= 4.5)) {
decRate = 5;
filterSet = ((rxBw - 2.1429)/0.3250 + 0.5);
} else if((rxBw > 4.5) && (rxBw <= 8.8)) {
decRate = 4;
filterSet = ((rxBw - 3.9857)/0.6643 + 0.5);
} else if((rxBw > 8.8) && (rxBw <= 17.5)) {
decRate = 3;
filterSet = ((rxBw - 7.6714)/1.3536 + 0.5);
} else if((rxBw > 17.5) && (rxBw <= 34.7)) {
decRate = 2;
filterSet = ((rxBw - 15.2000)/2.6893 + 0.5);
} else if((rxBw > 34.7) && (rxBw <= 69.2)) {
decRate = 1;
filterSet = ((rxBw - 30.2430)/5.3679 + 0.5);
} else if((rxBw > 69.2) && (rxBw <= 137.9)) {
decRate = 0;
filterSet = ((rxBw - 60.286)/10.7000 + 0.5);
// this is the "Lord help thee who tread 'ere" section - no way to approximate this mess
/// \todo float tolerance equality as macro?
} else if(fabs(rxBw - 142.8) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 1;
filterSet = 4;
} else if(fabs(rxBw - 167.8) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 1;
filterSet = 5;
} else if(fabs(rxBw - 181.1) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 1;
filterSet = 6;
} else if(fabs(rxBw - 191.5) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 15;
} else if(fabs(rxBw - 225.1) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 1;
} else if(fabs(rxBw - 248.8) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 2;
} else if(fabs(rxBw - 269.3) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 3;
} else if(fabs(rxBw - 284.8) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 4;
} else if(fabs(rxBw -335.5) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 8;
} else if(fabs(rxBw - 391.8) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 9;
} else if(fabs(rxBw - 420.2) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 10;
} else if(fabs(rxBw - 468.4) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 11;
} else if(fabs(rxBw - 518.8) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 12;
} else if(fabs(rxBw - 577.0) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 13;
} else if(fabs(rxBw - 620.7) <= 0.001) {
bypass = RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON;
decRate = 0;
filterSet = 14;
} else {
return(RADIOLIB_ERR_INVALID_RX_BANDWIDTH);
}
// shift decimation rate bits
decRate <<= 4;
// update register
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_IF_FILTER_BANDWIDTH, bypass | decRate | filterSet);
RADIOLIB_ASSERT(state);
// update clock recovery
state = updateClockRecovery();
return(state);
}
int16_t Si443x::setSyncWord(uint8_t* syncWord, size_t len) {
RADIOLIB_CHECK_RANGE(len, 1, 4, RADIOLIB_ERR_INVALID_SYNC_WORD);
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// set sync word length
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_HEADER_CONTROL_2, (uint8_t)(len - 1) << 1, 2, 1);
RADIOLIB_ASSERT(state);
// set sync word bytes
_mod->SPIwriteRegisterBurst(RADIOLIB_SI443X_REG_SYNC_WORD_3, syncWord, len);
return(state);
}
int16_t Si443x::setPreambleLength(uint8_t preambleLen) {
// Si443x configures preamble length in 4-bit nibbles
if(preambleLen % 4 != 0) {
return(RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH);
}
// set default preamble length
uint8_t preLenNibbles = preambleLen / 4;
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_PREAMBLE_LENGTH, preLenNibbles);
RADIOLIB_ASSERT(state);
// set default preamble detection threshold to 5/8 of preamble length (in units of 4 bits)
uint8_t preThreshold = 5*preLenNibbles / 8;
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_PREAMBLE_DET_CONTROL, preThreshold << 3, 7, 3));
}
size_t Si443x::getPacketLength(bool update) {
if(!_packetLengthQueried && update) {
if (_packetLengthConfig == RADIOLIB_SI443X_FIXED_PACKET_LENGTH_ON) {
_packetLength = _mod->SPIreadRegister(RADIOLIB_SI443X_REG_TRANSMIT_PACKET_LENGTH);
} else {
_packetLength = _mod->SPIreadRegister(RADIOLIB_SI443X_REG_RECEIVED_PACKET_LENGTH);
}
_packetLengthQueried = true;
}
return(_packetLength);
}
int16_t Si443x::setEncoding(uint8_t encoding) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// set encoding
/// \todo - add inverted Manchester?
switch(encoding) {
case RADIOLIB_ENCODING_NRZ:
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1, RADIOLIB_SI443X_MANCHESTER_INVERTED_OFF | RADIOLIB_SI443X_MANCHESTER_OFF | RADIOLIB_SI443X_WHITENING_OFF, 2, 0));
case RADIOLIB_ENCODING_MANCHESTER:
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1, RADIOLIB_SI443X_MANCHESTER_INVERTED_OFF | RADIOLIB_SI443X_MANCHESTER_ON | RADIOLIB_SI443X_WHITENING_OFF, 2, 0));
case RADIOLIB_ENCODING_WHITENING:
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1, RADIOLIB_SI443X_MANCHESTER_INVERTED_OFF | RADIOLIB_SI443X_MANCHESTER_OFF | RADIOLIB_SI443X_WHITENING_ON, 2, 0));
default:
return(RADIOLIB_ERR_INVALID_ENCODING);
}
}
int16_t Si443x::setDataShaping(uint8_t sh) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// set data shaping
switch(sh) {
case RADIOLIB_SHAPING_NONE:
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1, RADIOLIB_SI443X_MANCHESTER_INVERTED_OFF | RADIOLIB_SI443X_MANCHESTER_OFF | RADIOLIB_SI443X_WHITENING_OFF, 2, 0));
case RADIOLIB_SHAPING_0_3:
case RADIOLIB_SHAPING_0_5:
case RADIOLIB_SHAPING_1_0:
/// \todo implement fiter configuration - docs claim this should be possible, but seems undocumented
return(_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1, RADIOLIB_SI443X_MANCHESTER_INVERTED_OFF | RADIOLIB_SI443X_MANCHESTER_OFF | RADIOLIB_SI443X_WHITENING_ON, 2, 0));
default:
return(RADIOLIB_ERR_INVALID_ENCODING);
}
}
void Si443x::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
uint8_t Si443x::randomByte() {
// set mode to Rx
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_RX_ON | RADIOLIB_SI443X_XTAL_ON);
// wait a bit for the RSSI reading to stabilise
_mod->delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
for(uint8_t i = 0; i < 8; i++) {
randByte |= ((_mod->SPIreadRegister(RADIOLIB_SI443X_REG_RSSI) & 0x01) << i);
}
// set mode to standby
standby();
return(randByte);
}
int16_t Si443x::getChipVersion() {
return(_mod->SPIgetRegValue(RADIOLIB_SI443X_REG_DEVICE_VERSION));
}
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
void Si443x::setDirectAction(void (*func)(void)) {
setIrqAction(func);
}
void Si443x::readBit(RADIOLIB_PIN_TYPE pin) {
updateDirectBuffer((uint8_t)digitalRead(pin));
}
#endif
int16_t Si443x::fixedPacketLengthMode(uint8_t len) {
return(Si443x::setPacketMode(RADIOLIB_SI443X_FIXED_PACKET_LENGTH_ON, len));
}
int16_t Si443x::variablePacketLengthMode(uint8_t maxLen) {
return(Si443x::setPacketMode(RADIOLIB_SI443X_FIXED_PACKET_LENGTH_OFF, maxLen));
}
int16_t Si443x::setFrequencyRaw(float newFreq) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// check high/low band
uint8_t bandSelect = RADIOLIB_SI443X_BAND_SELECT_LOW;
uint8_t freqBand = (newFreq / 10) - 24;
uint8_t afcLimiter = 80;
_freq = newFreq;
if(newFreq >= 480.0) {
bandSelect = RADIOLIB_SI443X_BAND_SELECT_HIGH;
freqBand = (newFreq / 20) - 24;
afcLimiter = 40;
}
// calculate register values
uint16_t freqCarrier = ((newFreq / (10 * ((bandSelect >> 5) + 1))) - freqBand - 24) * (uint32_t)64000;
// update registers
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_FREQUENCY_BAND_SELECT, bandSelect | freqBand, 5, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_1, (uint8_t)((freqCarrier & 0xFF00) >> 8));
state |= _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_0, (uint8_t)(freqCarrier & 0xFF));
state |= _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_AFC_LIMITER, afcLimiter);
return(state);
}
int16_t Si443x::setPacketMode(uint8_t mode, uint8_t len) {
// check packet length
if (len > RADIOLIB_SI443X_MAX_PACKET_LENGTH) {
return(RADIOLIB_ERR_PACKET_TOO_LONG);
}
// set to fixed packet length
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_HEADER_CONTROL_2, mode, 3, 3);
RADIOLIB_ASSERT(state);
// set length to register
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_TRANSMIT_PACKET_LENGTH, len);
RADIOLIB_ASSERT(state);
// update cached value
_packetLengthConfig = mode;
return(state);
}
bool Si443x::findChip() {
uint8_t i = 0;
bool flagFound = false;
while((i < 10) && !flagFound) {
// reset the module
reset();
// check version register
uint8_t version = _mod->SPIreadRegister(RADIOLIB_SI443X_REG_DEVICE_VERSION);
if(version == RADIOLIB_SI443X_DEVICE_VERSION) {
flagFound = true;
} else {
#if defined(RADIOLIB_DEBUG)
RADIOLIB_DEBUG_PRINT(F("Si443x not found! ("));
RADIOLIB_DEBUG_PRINT(i + 1);
RADIOLIB_DEBUG_PRINT(F(" of 10 tries) RADIOLIB_SI443X_REG_DEVICE_VERSION == "));
char buffHex[5];
sprintf(buffHex, "0x%02X", version);
RADIOLIB_DEBUG_PRINT(buffHex);
RADIOLIB_DEBUG_PRINT(F(", expected 0x00"));
RADIOLIB_DEBUG_PRINTLN(RADIOLIB_SI443X_DEVICE_VERSION, HEX);
#endif
_mod->delay(10);
i++;
}
}
return(flagFound);
}
void Si443x::clearIRQFlags() {
uint8_t buff[2];
_mod->SPIreadRegisterBurst(RADIOLIB_SI443X_REG_INTERRUPT_STATUS_1, 2, buff);
}
void Si443x::clearFIFO(size_t count) {
while(count) {
_mod->SPIreadRegister(RADIOLIB_SI443X_REG_FIFO_ACCESS);
count--;
}
}
int16_t Si443x::config() {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// disable POR and chip ready interrupts
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_2, 0x00);
// enable AGC
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_AGC_OVERRIDE_1, RADIOLIB_SI443X_AGC_GAIN_INCREASE_ON | RADIOLIB_SI443X_AGC_ON, 6, 5);
RADIOLIB_ASSERT(state);
// disable packet header
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_HEADER_CONTROL_2, RADIOLIB_SI443X_SYNC_WORD_TIMEOUT_OFF | RADIOLIB_SI443X_HEADER_LENGTH_HEADER_NONE, 7, 4);
RADIOLIB_ASSERT(state);
// set antenna switching
_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_GPIO0_CONFIG, RADIOLIB_SI443X_GPIOX_TX_STATE_OUT, 4, 0);
_mod->SPIsetRegValue(RADIOLIB_SI443X_REG_GPIO1_CONFIG, RADIOLIB_SI443X_GPIOX_RX_STATE_OUT, 4, 0);
// disable packet header checking
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_HEADER_CONTROL_1, RADIOLIB_SI443X_BROADCAST_ADDR_CHECK_NONE | RADIOLIB_SI443X_RECEIVED_HEADER_CHECK_NONE);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t Si443x::updateClockRecovery() {
// get the parameters
uint8_t bypass = _mod->SPIgetRegValue(RADIOLIB_SI443X_REG_IF_FILTER_BANDWIDTH, 7, 7) >> 7;
uint8_t decRate = _mod->SPIgetRegValue(RADIOLIB_SI443X_REG_IF_FILTER_BANDWIDTH, 6, 4) >> 4;
uint8_t manch = _mod->SPIgetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1, 1, 1) >> 1;
// calculate oversampling ratio, NCO offset and clock recovery gain
int8_t ndecExp = (int8_t)decRate - 3;
float ndec = 0;
if(ndecExp > 0) {
ndec = (uint16_t)1 << ndecExp;
} else {
ndecExp *= -1;
ndec = 1.0/(float)((uint16_t)1 << ndecExp);
}
float rxOsr = ((float)(500 * (1 + 2*bypass))) / (ndec * _br * ((float)(1 + manch)));
uint32_t ncoOff = (_br * (1 + manch) * ((uint32_t)(1) << (20 + decRate))) / (500 * (1 + 2*bypass));
uint16_t crGain = 2 + (((float)(65536.0 * (1 + manch)) * _br) / (rxOsr * (_freqDev / 0.625)));
uint16_t rxOsr_fixed = (uint16_t)rxOsr;
// print that whole mess
RADIOLIB_DEBUG_PRINTLN(bypass, HEX);
RADIOLIB_DEBUG_PRINTLN(decRate, HEX);
RADIOLIB_DEBUG_PRINTLN(manch, HEX);
RADIOLIB_DEBUG_PRINT(rxOsr, 3);
RADIOLIB_DEBUG_PRINT('\t');
RADIOLIB_DEBUG_PRINT(rxOsr_fixed);
RADIOLIB_DEBUG_PRINT('\t');
RADIOLIB_DEBUG_PRINTLN(rxOsr_fixed, HEX);
RADIOLIB_DEBUG_PRINT(ncoOff);
RADIOLIB_DEBUG_PRINT('\t');
RADIOLIB_DEBUG_PRINTLN(ncoOff, HEX);
RADIOLIB_DEBUG_PRINT(crGain);
RADIOLIB_DEBUG_PRINT('\t');
RADIOLIB_DEBUG_PRINTLN(crGain, HEX);
// update oversampling ratio
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_2, (uint8_t)((rxOsr_fixed & 0x0700) >> 3), 7, 5);
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_OVERSAMP_RATIO, (uint8_t)(rxOsr_fixed & 0x00FF));
RADIOLIB_ASSERT(state);
// update NCO offset
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_2, (uint8_t)((ncoOff & 0x0F0000) >> 16), 3, 0);
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_1, (uint8_t)((ncoOff & 0x00FF00) >> 8));
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_0, (uint8_t)(ncoOff & 0x0000FF));
RADIOLIB_ASSERT(state);
// update clock recovery loop gain
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_TIMING_LOOP_GAIN_1, (uint8_t)((crGain & 0x0700) >> 8), 2, 0);
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_TIMING_LOOP_GAIN_0, (uint8_t)(crGain & 0x00FF));
RADIOLIB_ASSERT(state);
return(state);
}
int16_t Si443x::directMode() {
int16_t state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_2, RADIOLIB_SI443X_TX_DATA_SOURCE_GPIO, 5, 4);
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_GPIO1_CONFIG, RADIOLIB_SI443X_GPIOX_TX_RX_DATA_CLK_OUT, 4, 0);
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_GPIO2_CONFIG, RADIOLIB_SI443X_GPIOX_TX_DATA_IN, 4, 0);
RADIOLIB_ASSERT(state);
state = _mod->SPIsetRegValue(RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_2, RADIOLIB_SI443X_MODULATION_FSK, 1, 0);
return(state);
}
#endif
+872
View File
@@ -0,0 +1,872 @@
#if !defined(_RADIOLIB_SI443X_H)
#define _RADIOLIB_SI443X_H
#include "../../TypeDef.h"
#if !defined(RADIOLIB_EXCLUDE_SI443X)
#include "../../Module.h"
#include "../../protocols/PhysicalLayer/PhysicalLayer.h"
// Si443x physical layer properties
#define RADIOLIB_SI443X_FREQUENCY_STEP_SIZE 156.25
#define RADIOLIB_SI443X_MAX_PACKET_LENGTH 64
// Si443x series common registers
#define RADIOLIB_SI443X_REG_DEVICE_TYPE 0x00
#define RADIOLIB_SI443X_REG_DEVICE_VERSION 0x01
#define RADIOLIB_SI443X_REG_DEVICE_STATUS 0x02
#define RADIOLIB_SI443X_REG_INTERRUPT_STATUS_1 0x03
#define RADIOLIB_SI443X_REG_INTERRUPT_STATUS_2 0x04
#define RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_1 0x05
#define RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_2 0x06
#define RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1 0x07
#define RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_2 0x08
#define RADIOLIB_SI443X_REG_XOSC_LOAD_CAPACITANCE 0x09
#define RADIOLIB_SI443X_REG_MCU_OUTPUT_CLOCK 0x0A
#define RADIOLIB_SI443X_REG_GPIO0_CONFIG 0x0B
#define RADIOLIB_SI443X_REG_GPIO1_CONFIG 0x0C
#define RADIOLIB_SI443X_REG_GPIO2_CONFIG 0x0D
#define RADIOLIB_SI443X_REG_IO_PORT_CONFIG 0x0E
#define RADIOLIB_SI443X_REG_ADC_CONFIG 0x0F
#define RADIOLIB_SI443X_REG_ADC_SENSOR_AMP_OFFSET 0x10
#define RADIOLIB_SI443X_REG_ADC_VALUE 0x11
#define RADIOLIB_SI443X_REG_TEMP_SENSOR_CONTROL 0x12
#define RADIOLIB_SI443X_REG_TEMP_VALUE_OFFSET 0x13
#define RADIOLIB_SI443X_REG_WAKEUP_TIMER_PERIOD_1 0x14
#define RADIOLIB_SI443X_REG_WAKEUP_TIMER_PERIOD_2 0x15
#define RADIOLIB_SI443X_REG_WAKEUP_TIMER_PERIOD_3 0x16
#define RADIOLIB_SI443X_REG_WAKEUP_TIMER_VALUE_1 0x17
#define RADIOLIB_SI443X_REG_WAKEUP_TIMER_VALUE_2 0x18
#define RADIOLIB_SI443X_REG_LOW_DC_MODE_DURATION 0x19
#define RADIOLIB_SI443X_REG_LOW_BATT_DET_THRESHOLD 0x1A
#define RADIOLIB_SI443X_REG_BATT_VOLTAGE_LEVEL 0x1B
#define RADIOLIB_SI443X_REG_IF_FILTER_BANDWIDTH 0x1C
#define RADIOLIB_SI443X_REG_AFC_LOOP_GEARSHIFT_OVERRIDE 0x1D
#define RADIOLIB_SI443X_REG_AFC_TIMING_CONTROL 0x1E
#define RADIOLIB_SI443X_REG_CLOCK_REC_GEARSHIFT_OVERRIDE 0x1F
#define RADIOLIB_SI443X_REG_CLOCK_REC_OVERSAMP_RATIO 0x20
#define RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_2 0x21
#define RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_1 0x22
#define RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_0 0x23
#define RADIOLIB_SI443X_REG_CLOCK_REC_TIMING_LOOP_GAIN_1 0x24
#define RADIOLIB_SI443X_REG_CLOCK_REC_TIMING_LOOP_GAIN_0 0x25
#define RADIOLIB_SI443X_REG_RSSI 0x26
#define RADIOLIB_SI443X_REG_RSSI_CLEAR_CHANNEL_THRESHOLD 0x27
#define RADIOLIB_SI443X_REG_ANTENNA_DIVERSITY_1 0x28
#define RADIOLIB_SI443X_REG_ANTENNA_DIVERSITY_2 0x29
#define RADIOLIB_SI443X_REG_AFC_LIMITER 0x2A
#define RADIOLIB_SI443X_REG_AFC_CORRECTION 0x2B
#define RADIOLIB_SI443X_REG_OOK_COUNTER_1 0x2C
#define RADIOLIB_SI443X_REG_OOK_COUNTER_2 0x2D
#define RADIOLIB_SI443X_REG_SLICER_PEAK_HOLD 0x2E
#define RADIOLIB_SI443X_REG_DATA_ACCESS_CONTROL 0x30
#define RADIOLIB_SI443X_REG_EZMAC_STATUS 0x31
#define RADIOLIB_SI443X_REG_HEADER_CONTROL_1 0x32
#define RADIOLIB_SI443X_REG_HEADER_CONTROL_2 0x33
#define RADIOLIB_SI443X_REG_PREAMBLE_LENGTH 0x34
#define RADIOLIB_SI443X_REG_PREAMBLE_DET_CONTROL 0x35
#define RADIOLIB_SI443X_REG_SYNC_WORD_3 0x36
#define RADIOLIB_SI443X_REG_SYNC_WORD_2 0x37
#define RADIOLIB_SI443X_REG_SYNC_WORD_1 0x38
#define RADIOLIB_SI443X_REG_SYNC_WORD_0 0x39
#define RADIOLIB_SI443X_REG_TRANSMIT_HEADER_3 0x3A
#define RADIOLIB_SI443X_REG_TRANSMIT_HEADER_2 0x3B
#define RADIOLIB_SI443X_REG_TRANSMIT_HEADER_1 0x3C
#define RADIOLIB_SI443X_REG_TRANSMIT_HEADER_0 0x3D
#define RADIOLIB_SI443X_REG_TRANSMIT_PACKET_LENGTH 0x3E
#define RADIOLIB_SI443X_REG_CHECK_HEADER_3 0x3F
#define RADIOLIB_SI443X_REG_CHECK_HEADER_2 0x40
#define RADIOLIB_SI443X_REG_CHECK_HEADER_1 0x41
#define RADIOLIB_SI443X_REG_CHECK_HEADER_0 0x42
#define RADIOLIB_SI443X_REG_HEADER_ENABLE_3 0x43
#define RADIOLIB_SI443X_REG_HEADER_ENABLE_2 0x44
#define RADIOLIB_SI443X_REG_HEADER_ENABLE_1 0x45
#define RADIOLIB_SI443X_REG_HEADER_ENABLE_0 0x46
#define RADIOLIB_SI443X_REG_RECEIVED_HEADER_3 0x47
#define RADIOLIB_SI443X_REG_RECEIVED_HEADER_2 0x48
#define RADIOLIB_SI443X_REG_RECEIVED_HEADER_1 0x49
#define RADIOLIB_SI443X_REG_RECEIVED_HEADER_0 0x4A
#define RADIOLIB_SI443X_REG_RECEIVED_PACKET_LENGTH 0x4B
#define RADIOLIB_SI443X_REG_ADC8_CONTROL 0x4F
#define RADIOLIB_SI443X_REG_CHANNEL_FILTER_COEFF 0x60
#define RADIOLIB_SI443X_REG_XOSC_CONTROL_TEST 0x62
#define RADIOLIB_SI443X_REG_AGC_OVERRIDE_1 0x69
#define RADIOLIB_SI443X_REG_TX_POWER 0x6D
#define RADIOLIB_SI443X_REG_TX_DATA_RATE_1 0x6E
#define RADIOLIB_SI443X_REG_TX_DATA_RATE_0 0x6F
#define RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1 0x70
#define RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_2 0x71
#define RADIOLIB_SI443X_REG_FREQUENCY_DEVIATION 0x72
#define RADIOLIB_SI443X_REG_FREQUENCY_OFFSET_1 0x73
#define RADIOLIB_SI443X_REG_FREQUENCY_OFFSET_2 0x74
#define RADIOLIB_SI443X_REG_FREQUENCY_BAND_SELECT 0x75
#define RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_1 0x76
#define RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_0 0x77
#define RADIOLIB_SI443X_REG_FREQUENCY_HOPPING_CHANNEL_SEL 0x79
#define RADIOLIB_SI443X_REG_FREQUENCY_HOPPING_STEP_SIZE 0x7A
#define RADIOLIB_SI443X_REG_TX_FIFO_CONTROL_1 0x7C
#define RADIOLIB_SI443X_REG_TX_FIFO_CONTROL_2 0x7D
#define RADIOLIB_SI443X_REG_RX_FIFO_CONTROL 0x7E
#define RADIOLIB_SI443X_REG_FIFO_ACCESS 0x7F
// RADIOLIB_SI443X_REG_DEVICE_TYPE MSB LSB DESCRIPTION
#define RADIOLIB_SI443X_DEVICE_TYPE 0x08 // 4 0 device identification register
// RADIOLIB_SI443X_REG_DEVICE_VERSION
#define RADIOLIB_SI443X_DEVICE_VERSION 0x06 // 4 0 chip version register
// RADIOLIB_SI443X_REG_DEVICE_STATUS
#define RADIOLIB_SI443X_RX_TX_FIFO_OVERFLOW 0b10000000 // 7 7 Rx/Tx FIFO overflow flag
#define RADIOLIB_SI443X_RX_TX_FIFO_UNDERFLOW 0b01000000 // 6 6 Rx/Tx FIFO underflow flag
#define RADIOLIB_SI443X_RX_FIFO_EMPTY 0b00100000 // 5 5 Rx FIFO empty flag
#define RADIOLIB_SI443X_HEADER_ERROR 0b00010000 // 4 4 header error flag
#define RADIOLIB_SI443X_FREQUENCY_ERROR 0b00001000 // 3 3 frequency error flag (frequency outside allowed range)
#define RADIOLIB_SI443X_TX 0b00000010 // 1 0 power state: Tx
#define RADIOLIB_SI443X_RX 0b00000001 // 1 0 Rx
#define RADIOLIB_SI443X_IDLE 0b00000000 // 1 0 idle
// RADIOLIB_SI443X_REG_INTERRUPT_STATUS_1
#define RADIOLIB_SI443X_FIFO_LEVEL_ERROR_INTERRUPT 0b10000000 // 7 7 Tx/Rx FIFO overflow or underflow
#define RADIOLIB_SI443X_TX_FIFO_ALMOST_FULL_INTERRUPT 0b01000000 // 6 6 Tx FIFO almost full
#define RADIOLIB_SI443X_TX_FIFO_ALMOST_EMPTY_INTERRUPT 0b00100000 // 5 5 Tx FIFO almost empty
#define RADIOLIB_SI443X_RX_FIFO_ALMOST_FULL_INTERRUPT 0b00010000 // 4 4 Rx FIFO almost full
#define RADIOLIB_SI443X_EXTERNAL_INTERRUPT 0b00001000 // 3 3 external interrupt occurred on GPIOx
#define RADIOLIB_SI443X_PACKET_SENT_INTERRUPT 0b00000100 // 2 2 packet transmission done
#define RADIOLIB_SI443X_VALID_PACKET_RECEIVED_INTERRUPT 0b00000010 // 1 1 valid packet has been received
#define RADIOLIB_SI443X_CRC_ERROR_INTERRUPT 0b00000001 // 0 0 CRC failed
// RADIOLIB_SI443X_REG_INTERRUPT_STATUS_2
#define RADIOLIB_SI443X_SYNC_WORD_DETECTED_INTERRUPT 0b10000000 // 7 7 sync word has been detected
#define RADIOLIB_SI443X_VALID_RADIOLIB_PREAMBLE_DETECTED_INTERRUPT 0b01000000 // 6 6 valid preamble has been detected
#define RADIOLIB_SI443X_INVALID_RADIOLIB_PREAMBLE_DETECTED_INTERRUPT 0b00100000 // 5 5 invalid preamble has been detected
#define RADIOLIB_SI443X_RSSI_INTERRUPT 0b00010000 // 4 4 RSSI exceeded programmed threshold
#define RADIOLIB_SI443X_WAKEUP_TIMER_INTERRUPT 0b00001000 // 3 3 wake-up timer expired
#define RADIOLIB_SI443X_LOW_BATTERY_INTERRUPT 0b00000100 // 2 2 low battery detected
#define RADIOLIB_SI443X_CHIP_READY_INTERRUPT 0b00000010 // 1 1 chip ready event detected
#define RADIOLIB_SI443X_POWER_ON_RESET_INTERRUPT 0b00000001 // 0 0 power-on-reset detected
// RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_1
#define RADIOLIB_SI443X_FIFO_LEVEL_ERROR_ENABLED 0b10000000 // 7 7 Tx/Rx FIFO overflow or underflow interrupt enabled
#define RADIOLIB_SI443X_TX_FIFO_ALMOST_FULL_ENABLED 0b01000000 // 6 6 Tx FIFO almost full interrupt enabled
#define RADIOLIB_SI443X_TX_FIFO_ALMOST_EMPTY_ENABLED 0b00100000 // 5 5 Tx FIFO almost empty interrupt enabled
#define RADIOLIB_SI443X_RX_FIFO_ALMOST_FULL_ENABLED 0b00010000 // 4 4 Rx FIFO almost full interrupt enabled
#define RADIOLIB_SI443X_EXTERNAL_ENABLED 0b00001000 // 3 3 external interrupt interrupt enabled
#define RADIOLIB_SI443X_PACKET_SENT_ENABLED 0b00000100 // 2 2 packet transmission done interrupt enabled
#define RADIOLIB_SI443X_VALID_PACKET_RECEIVED_ENABLED 0b00000010 // 1 1 valid packet received interrupt enabled
#define RADIOLIB_SI443X_CRC_ERROR_ENABLED 0b00000001 // 0 0 CRC failed interrupt enabled
// RADIOLIB_SI443X_REG_INTERRUPT_ENABLE_2
#define RADIOLIB_SI443X_SYNC_WORD_DETECTED_ENABLED 0b10000000 // 7 7 sync word interrupt enabled
#define RADIOLIB_SI443X_VALID_RADIOLIB_PREAMBLE_DETECTED_ENABLED 0b01000000 // 6 6 valid preamble interrupt enabled
#define RADIOLIB_SI443X_INVALID_RADIOLIB_PREAMBLE_DETECTED_ENABLED 0b00100000 // 5 5 invalid preamble interrupt enabled
#define RADIOLIB_SI443X_RSSI_ENABLED 0b00010000 // 4 4 RSSI exceeded programmed threshold interrupt enabled
#define RADIOLIB_SI443X_WAKEUP_TIMER_ENABLED 0b00001000 // 3 3 wake-up timer interrupt enabled
#define RADIOLIB_SI443X_LOW_BATTERY_ENABLED 0b00000100 // 2 2 low battery interrupt enabled
#define RADIOLIB_SI443X_CHIP_READY_ENABLED 0b00000010 // 1 1 chip ready event interrupt enabled
#define RADIOLIB_SI443X_POWER_ON_RESET_ENABLED 0b00000001 // 0 0 power-on-reset interrupt enabled
// RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1
#define RADIOLIB_SI443X_SOFTWARE_RESET 0b10000000 // 7 7 reset all registers to default values
#define RADIOLIB_SI443X_ENABLE_LOW_BATTERY_DETECT 0b01000000 // 6 6 enable low battery detection
#define RADIOLIB_SI443X_ENABLE_WAKEUP_TIMER 0b00100000 // 5 5 enable wakeup timer
#define RADIOLIB_SI443X_32_KHZ_RC 0b00000000 // 4 4 32.768 kHz source: RC oscillator (default)
#define RADIOLIB_SI443X_32_KHZ_XOSC 0b00010000 // 4 4 crystal oscillator
#define RADIOLIB_SI443X_TX_ON 0b00001000 // 3 3 Tx on in manual transmit mode
#define RADIOLIB_SI443X_RX_ON 0b00000100 // 2 2 Rx on in manual receive mode
#define RADIOLIB_SI443X_PLL_ON 0b00000010 // 1 1 PLL on (tune mode)
#define RADIOLIB_SI443X_XTAL_OFF 0b00000000 // 0 0 crystal oscillator: off (standby mode)
#define RADIOLIB_SI443X_XTAL_ON 0b00000001 // 0 0 on (ready mode)
// RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_2
#define RADIOLIB_SI443X_ANT_DIV_TR_HL_IDLE_L 0b00000000 // 7 5 GPIO1/2 states: Tx/Rx GPIO1 H, GPIO2 L; idle low (default)
#define RADIOLIB_SI443X_ANT_DIV_TR_LH_IDLE_L 0b00100000 // 7 5 Tx/Rx GPIO1 L, GPIO2 H; idle low
#define RADIOLIB_SI443X_ANT_DIV_TR_HL_IDLE_H 0b01000000 // 7 5 Tx/Rx GPIO1 H, GPIO2 L; idle high
#define RADIOLIB_SI443X_ANT_DIV_TR_LH_IDLE_H 0b01100000 // 7 5 Tx/Rx GPIO1 L, GPIO2 H; idle high
#define RADIOLIB_SI443X_ANT_DIV_TR_ALG_IDLE_L 0b10000000 // 7 5 Tx/Rx diversity algorithm; idle low
#define RADIOLIB_SI443X_ANT_DIV_TR_ALG_IDLE_H 0b10100000 // 7 5 Tx/Rx diversity algorithm; idle high
#define RADIOLIB_SI443X_ANT_DIV_TR_ALG_BEACON_IDLE_L 0b11000000 // 7 5 Tx/Rx diversity algorithm (beacon); idle low
#define RADIOLIB_SI443X_ANT_DIV_TR_ALG_BEACON_IDLE_H 0b11100000 // 7 5 Tx/Rx diversity algorithm (beacon); idle high
#define RADIOLIB_SI443X_RX_MULTIPACKET_OFF 0b00000000 // 4 4 Rx multipacket: disabled (default)
#define RADIOLIB_SI443X_RX_MULTIPACKET_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_SI443X_AUTO_TX_OFF 0b00000000 // 3 3 Tx autotransmit on FIFO almost full: disabled (default)
#define RADIOLIB_SI443X_AUTO_TX_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_SI443X_LOW_DUTY_CYCLE_OFF 0b00000000 // 2 2 low duty cycle mode: disabled (default)
#define RADIOLIB_SI443X_LOW_DUTY_CYCLE_ON 0b00000100 // 2 2 enabled
#define RADIOLIB_SI443X_RX_FIFO_RESET 0b00000010 // 1 1 Rx FIFO reset/clear: reset (call first)
#define RADIOLIB_SI443X_RX_FIFO_CLEAR 0b00000000 // 1 1 clear (call second)
#define RADIOLIB_SI443X_TX_FIFO_RESET 0b00000001 // 0 0 Tx FIFO reset/clear: reset (call first)
#define RADIOLIB_SI443X_TX_FIFO_CLEAR 0b00000000 // 0 0 clear (call second)
// RADIOLIB_SI443X_REG_XOSC_LOAD_CAPACITANCE
#define RADIOLIB_SI443X_XTAL_SHIFT 0b00000000 // 7 7 crystal capacitance configuration:
#define RADIOLIB_SI443X_XTAL_LOAD_CAPACITANCE 0b01111111 // 6 0 C_int = 1.8 pF + 0.085 pF * RADIOLIB_SI443X_XTAL_LOAD_CAPACITANCE + 3.7 pF * RADIOLIB_SI443X_XTAL_SHIFT
// RADIOLIB_SI443X_REG_MCU_OUTPUT_CLOCK
#define RADIOLIB_SI443X_CLOCK_TAIL_CYCLES_OFF 0b00000000 // 5 4 additional clock cycles: none (default)
#define RADIOLIB_SI443X_CLOCK_TAIL_CYCLES_128 0b00010000 // 5 4 128
#define RADIOLIB_SI443X_CLOCK_TAIL_CYCLES_256 0b00100000 // 5 4 256
#define RADIOLIB_SI443X_CLOCK_TAIL_CYCLES_512 0b00110000 // 5 4 512
#define RADIOLIB_SI443X_LOW_FREQ_CLOCK_OFF 0b00000000 // 3 3 32.768 kHz clock output: disabled (default)
#define RADIOLIB_SI443X_LOW_FREQ_CLOCK_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_SI443X_MCU_CLOCK_30_MHZ 0b00000000 // 2 0 GPIO clock output: 30 MHz
#define RADIOLIB_SI443X_MCU_CLOCK_15_MHZ 0b00000001 // 2 0 15 MHz
#define RADIOLIB_SI443X_MCU_CLOCK_10_MHZ 0b00000010 // 2 0 10 MHz
#define RADIOLIB_SI443X_MCU_CLOCK_4_MHZ 0b00000011 // 2 0 4 MHz
#define RADIOLIB_SI443X_MCU_CLOCK_3_MHZ 0b00000100 // 2 0 3 MHz
#define RADIOLIB_SI443X_MCU_CLOCK_2_MHZ 0b00000101 // 2 0 2 MHz
#define RADIOLIB_SI443X_MCU_CLOCK_1_MHZ 0b00000110 // 2 0 1 MHz (default)
#define RADIOLIB_SI443X_MCU_CLOCK_32_KHZ 0b00000111 // 2 0 32.768 kHz
// RADIOLIB_SI443X_REG_GPIO0_CONFIG + RADIOLIB_SI443X_REG_GPIO1_CONFIG + RADIOLIB_SI443X_REG_GPIO2_CONFIG
#define RADIOLIB_SI443X_GPIOX_DRIVE_STRENGTH 0b00000000 // 7 6 GPIOx drive strength (higher number = stronger drive)
#define RADIOLIB_SI443X_GPIOX_PULLUP_OFF 0b00000000 // 5 5 GPIOx internal 200k pullup: disabled (default)
#define RADIOLIB_SI443X_GPIOX_PULLUP_ON 0b00100000 // 5 5 enabled
#define RADIOLIB_SI443X_GPIO0_POWER_ON_RESET_OUT 0b00000000 // 4 0 GPIOx function: power-on-reset output (GPIO0 only, default)
#define RADIOLIB_SI443X_GPIO1_POWER_ON_RESET_INV_OUT 0b00000000 // 4 0 inverted power-on-reset output (GPIO1 only, default)
#define RADIOLIB_SI443X_GPIO2_MCU_CLOCK_OUT 0b00000000 // 4 0 MCU clock output (GPIO2 only, default)
#define RADIOLIB_SI443X_GPIOX_WAKEUP_OUT 0b00000001 // 4 0 wakeup timer expired output
#define RADIOLIB_SI443X_GPIOX_LOW_BATTERY_OUT 0b00000010 // 4 0 low battery detect output
#define RADIOLIB_SI443X_GPIOX_DIGITAL_OUT 0b00000011 // 4 0 direct digital output
#define RADIOLIB_SI443X_GPIOX_EXT_INT_FALLING_IN 0b00000100 // 4 0 external interrupt, falling edge
#define RADIOLIB_SI443X_GPIOX_EXT_INT_RISING_IN 0b00000101 // 4 0 external interrupt, rising edge
#define RADIOLIB_SI443X_GPIOX_EXT_INT_CHANGE_IN 0b00000110 // 4 0 external interrupt, state change
#define RADIOLIB_SI443X_GPIOX_ADC_IN 0b00000111 // 4 0 ADC analog input
#define RADIOLIB_SI443X_GPIOX_ANALOG_TEST_N_IN 0b00001000 // 4 0 analog test N input
#define RADIOLIB_SI443X_GPIOX_ANALOG_TEST_P_IN 0b00001001 // 4 0 analog test P input
#define RADIOLIB_SI443X_GPIOX_DIGITAL_IN 0b00001010 // 4 0 direct digital input
#define RADIOLIB_SI443X_GPIOX_DIGITAL_TEST_OUT 0b00001011 // 4 0 digital test output
#define RADIOLIB_SI443X_GPIOX_ANALOG_TEST_N_OUT 0b00001100 // 4 0 analog test N output
#define RADIOLIB_SI443X_GPIOX_ANALOG_TEST_P_OUT 0b00001101 // 4 0 analog test P output
#define RADIOLIB_SI443X_GPIOX_REFERENCE_VOLTAGE_OUT 0b00001110 // 4 0 reference voltage output
#define RADIOLIB_SI443X_GPIOX_TX_RX_DATA_CLK_OUT 0b00001111 // 4 0 Tx/Rx clock output in direct mode
#define RADIOLIB_SI443X_GPIOX_TX_DATA_IN 0b00010000 // 4 0 Tx data input direct mode
#define RADIOLIB_SI443X_GPIOX_EXT_RETRANSMIT_REQUEST_IN 0b00010001 // 4 0 external retransmission request input
#define RADIOLIB_SI443X_GPIOX_TX_STATE_OUT 0b00010010 // 4 0 Tx state output
#define RADIOLIB_SI443X_GPIOX_TX_FIFO_ALMOST_FULL_OUT 0b00010011 // 4 0 Tx FIFO almost full output
#define RADIOLIB_SI443X_GPIOX_RX_DATA_OUT 0b00010100 // 4 0 Rx data output
#define RADIOLIB_SI443X_GPIOX_RX_STATE_OUT 0b00010101 // 4 0 Rx state output
#define RADIOLIB_SI443X_GPIOX_RX_FIFO_ALMOST_FULL_OUT 0b00010110 // 4 0 Rx FIFO almost full output
#define RADIOLIB_SI443X_GPIOX_ANT_DIV_1_OUT 0b00010111 // 4 0 antenna diversity output 1
#define RADIOLIB_SI443X_GPIOX_ANT_DIV_2_OUT 0b00011000 // 4 0 antenna diversity output 2
#define RADIOLIB_SI443X_GPIOX_VALID_PREAMBLE_OUT 0b00011001 // 4 0 valid preamble detected output
#define RADIOLIB_SI443X_GPIOX_INVALID_PREAMBLE_OUT 0b00011010 // 4 0 invalid preamble detected output
#define RADIOLIB_SI443X_GPIOX_SYNC_WORD_DETECTED_OUT 0b00011011 // 4 0 sync word detected output
#define RADIOLIB_SI443X_GPIOX_CLEAR_CHANNEL_OUT 0b00011100 // 4 0 clear channel assessment output
#define RADIOLIB_SI443X_GPIOX_VDD 0b00011101 // 4 0 VDD
#define RADIOLIB_SI443X_GPIOX_GND 0b00011110 // 4 0 GND
// RADIOLIB_SI443X_REG_IO_PORT_CONFIG
#define RADIOLIB_SI443X_GPIO2_EXT_INT_STATE_MASK 0b01000000 // 6 6 external interrupt state mask for: GPIO2
#define RADIOLIB_SI443X_GPIO1_EXT_INT_STATE_MASK 0b00100000 // 5 5 GPIO1
#define RADIOLIB_SI443X_GPIO0_EXT_INT_STATE_MASK 0b00010000 // 4 4 GPIO0
#define RADIOLIB_SI443X_IRQ_BY_SDO_OFF 0b00000000 // 3 3 output IRQ state on SDO pin: disabled (default)
#define RADIOLIB_SI443X_IRQ_BY_SDO_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_SI443X_GPIO2_DIGITAL_STATE_MASK 0b00000100 // 2 2 digital state mask for: GPIO2
#define RADIOLIB_SI443X_GPIO1_DIGITAL_STATE_MASK 0b00000010 // 1 1 GPIO1
#define RADIOLIB_SI443X_GPIO0_DIGITAL_STATE_MASK 0b00000001 // 0 0 GPIO0
// RADIOLIB_SI443X_REG_ADC_CONFIG
#define RADIOLIB_SI443X_ADC_START 0b10000000 // 7 7 ADC control: start measurement
#define RADIOLIB_SI443X_ADC_RUNNING 0b00000000 // 7 7 measurement in progress
#define RADIOLIB_SI443X_ADC_DONE 0b10000000 // 7 7 done
#define RADIOLIB_SI443X_ADC_SOURCE_TEMPERATURE 0b00000000 // 6 4 ADC source: internal temperature sensor (default)
#define RADIOLIB_SI443X_ADC_SOURCE_GPIO0_SINGLE 0b00010000 // 6 4 single-ended on GPIO0
#define RADIOLIB_SI443X_ADC_SOURCE_GPIO1_SINGLE 0b00100000 // 6 4 single-ended on GPIO1
#define RADIOLIB_SI443X_ADC_SOURCE_GPIO2_SINGLE 0b00110000 // 6 4 single-ended on GPIO2
#define RADIOLIB_SI443X_ADC_SOURCE_GPIO01_DIFF 0b01000000 // 6 4 differential on GPIO0 (+) and GPIO1 (-)
#define RADIOLIB_SI443X_ADC_SOURCE_GPIO12_DIFF 0b01010000 // 6 4 differential on GPIO1 (+) and GPIO2 (-)
#define RADIOLIB_SI443X_ADC_SOURCE_GPIO02_DIFF 0b01100000 // 6 4 differential on GPIO0 (+) and GPIO2 (-)
#define RADIOLIB_SI443X_ADC_SOURCE_GND 0b01110000 // 6 4 GND
#define RADIOLIB_SI443X_ADC_REFERNCE_BAND_GAP 0b00000000 // 3 2 ADC reference: internal bandgap 1.2 V (default)
#define RADIOLIB_SI443X_ADC_REFERNCE_VDD_3 0b00001000 // 3 2 VDD/3
#define RADIOLIB_SI443X_ADC_REFERNCE_VDD_2 0b00001100 // 3 2 VDD/2
#define RADIOLIB_SI443X_ADC_GAIN 0b00000000 // 1 0 ADC amplifier gain
// RADIOLIB_SI443X_REG_ADC_SENSOR_AMP_OFFSET
#define RADIOLIB_SI443X_ADC_OFFSET 0b00000000 // 3 0 ADC offset
// RADIOLIB_SI443X_REG_TEMP_SENSOR_CONTROL
#define RADIOLIB_SI443X_TEMP_SENSOR_RANGE_64_TO_64_C 0b00000000 // 7 6 temperature sensor range: -64 to 64 deg. C, 0.5 deg. C resolution (default)
#define RADIOLIB_SI443X_TEMP_SENSOR_RANGE_64_TO_192_C 0b01000000 // 7 6 -64 to 192 deg. C, 1.0 deg. C resolution
#define RADIOLIB_SI443X_TEMP_SENSOR_RANGE_0_TO_128_C 0b11000000 // 7 6 0 to 128 deg. C, 0.5 deg. C resolution
#define RADIOLIB_SI443X_TEMP_SENSOR_RANGE_40_TO_216_F 0b10000000 // 7 6 -40 to 216 deg. F, 1.0 deg. F resolution
#define RADIOLIB_SI443X_TEMP_SENSOR_KELVIN_TO_CELSIUS_OFF 0b00000000 // 5 5 Kelvin to Celsius offset: disabled
#define RADIOLIB_SI443X_TEMP_SENSOR_KELVIN_TO_CELSIUS_ON 0b00100000 // 5 5 enabled (default)
#define RADIOLIB_SI443X_TEMP_SENSOR_TRIM_OFF 0b00000000 // 4 4 temperature sensor trim: disabled (default)
#define RADIOLIB_SI443X_TEMP_SENSOR_TRIM_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_SI443X_TEMP_SENSOR_TRIM_VALUE 0b00000000 // 3 0 temperature sensor trim value
// RADIOLIB_SI443X_REG_WAKEUP_TIMER_PERIOD_1
#define RADIOLIB_SI443X_WAKEUP_TIMER_EXPONENT 0b00000011 // 4 0 wakeup timer value exponent
// RADIOLIB_SI443X_REG_WAKEUP_TIMER_PERIOD_2 + RADIOLIB_SI443X_REG_WAKEUP_TIMER_PERIOD_3
#define RADIOLIB_SI443X_WAKEUP_TIMER_MANTISSA_MSB 0x00 // 7 0 wakeup timer value:
#define RADIOLIB_SI443X_WAKEUP_TIMER_MANTISSA_LSB 0x01 // 7 0 T = (4 * RADIOLIB_SI443X_WAKEUP_TIMER_MANTISSA * 2 ^ RADIOLIB_SI443X_WAKEUP_TIMER_EXPONENT) / 32.768 ms
// RADIOLIB_SI443X_REG_LOW_DC_MODE_DURATION
#define RADIOLIB_SI443X_LOW_DC_MODE_DURATION_MANTISSA 0x01 // 7 0 low duty cycle mode duration: T = (4 * RADIOLIB_SI443X_LOW_DC_MODE_DURATION_MANTISSA * 2 ^ RADIOLIB_SI443X_WAKEUP_TIMER_EXPONENT) / 32.768 ms
// RADIOLIB_SI443X_REG_LOW_BATT_DET_THRESHOLD
#define RADIOLIB_SI443X_LOW_BATT_DET_THRESHOLD 0b00010100 // 4 0 low battery detection threshold: Vth = 1.7 + RADIOLIB_SI443X_LOW_BATT_DET_THRESHOLD * 0.05 V (defaults to 2.7 V)
// RADIOLIB_SI443X_REG_IF_FILTER_BANDWIDTH
#define RADIOLIB_SI443X_BYPASS_DEC_BY_3_OFF 0b00000000 // 7 7 bypass decimate-by-3 stage: disabled (default)
#define RADIOLIB_SI443X_BYPASS_DEC_BY_3_ON 0b10000000 // 7 7 enabled
#define RADIOLIB_SI443X_IF_FILTER_DEC_RATE 0b00000000 // 6 4 IF filter decimation rate
#define RADIOLIB_SI443X_IF_FILTER_COEFF_SET 0b00000001 // 3 0 IF filter coefficient set selection
// RADIOLIB_SI443X_REG_AFC_LOOP_GEARSHIFT_OVERRIDE
#define RADIOLIB_SI443X_AFC_WIDEBAND_OFF 0b00000000 // 7 7 AFC wideband: disabled (default)
#define RADIOLIB_SI443X_AFC_WIDEBAND_ON 0b10000000 // 7 7 enabled
#define RADIOLIB_SI443X_AFC_OFF 0b00000000 // 6 6 AFC: disabled
#define RADIOLIB_SI443X_AFC_ON 0b01000000 // 6 6 enabled (default)
#define RADIOLIB_SI443X_AFC_HIGH_GEAR_SETTING 0b00000000 // 5 3 AFC high gear setting
#define RADIOLIB_SI443X_SECOND_PHASE_BIAS_0_DB 0b00000100 // 2 2 second phase antenna selection bias: 0 dB (default)
#define RADIOLIB_SI443X_SECOND_PHASE_BIAS_1_5_DB 0b00000000 // 2 2 1.5 dB
#define RADIOLIB_SI443X_MOVING_AVERAGE_TAP_8 0b00000010 // 1 1 moving average filter tap length: 8*Tb
#define RADIOLIB_SI443X_MOVING_AVERAGE_TAP_4 0b00000000 // 1 1 4*Tb after first preamble (default)
#define RADIOLIB_SI443X_ZERO_PHASE_RESET_5 0b00000000 // 0 0 reset preamble detector after: 5 zero phases (default)
#define RADIOLIB_SI443X_ZERO_PHASE_RESET_2 0b00000001 // 0 0 3 zero phases
// RADIOLIB_SI443X_REG_AFC_TIMING_CONTROL
#define RADIOLIB_SI443X_SW_ANT_TIMER 0b00000000 // 7 6 number of periods to wait for RSSI to stabilize during antenna switching
#define RADIOLIB_SI443X_SHORT_WAIT 0b00001000 // 5 3 period to wait after AFC correction
#define RADIOLIB_SI443X_ANTENNA_SWITCH_WAIT 0b00000010 // 2 0 antenna switching wait time
// RADIOLIB_SI443X_REG_CLOCK_REC_GEARSHIFT_OVERRIDE
#define RADIOLIB_SI443X_CLOCK_RECOVER_FAST_GEARSHIFT 0b00000000 // 5 3 clock recovery fast gearshift value
#define RADIOLIB_SI443X_CLOCK_RECOVER_SLOW_GEARSHIFT 0b00000011 // 2 0 clock recovery slow gearshift value
// RADIOLIB_SI443X_REG_CLOCK_REC_OVERSAMP_RATIO
#define RADIOLIB_SI443X_CLOCK_REC_OVERSAMP_RATIO_LSB 0b01100100 // 7 0 oversampling rate LSB, defaults to 12.5 clock cycles per bit
// RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_2
#define RADIOLIB_SI443X_CLOCK_REC_OVERSAMP_RATIO_MSB 0b00000000 // 7 5 oversampling rate MSB, defaults to 12.5 clock cycles per bit
#define RADIOLIB_SI443X_SECOND_PHASE_SKIP_THRESHOLD 0b00000000 // 4 4 skip seconds phase antenna diversity threshold
#define RADIOLIB_SI443X_NCO_OFFSET_MSB 0b00000001 // 3 0 NCO offset MSB
// RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_1
#define RADIOLIB_SI443X_NCO_OFFSET_MID 0b01000111 // 7 0 NCO offset MID
// RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_0
#define RADIOLIB_SI443X_NCO_OFFSET_LSB 0b10101110 // 7 0 NCO offset LSB
// RADIOLIB_SI443X_REG_CLOCK_REC_TIMING_LOOP_GAIN_1
#define RADIOLIB_SI443X_RX_COMPENSATION_OFF 0b00000000 // 4 4 Rx compensation for high data rate: disabled (default)
#define RADIOLIB_SI443X_RX_COMPENSATION_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_SI443X_CLOCK_REC_GAIN_DOUBLE_OFF 0b00000000 // 3 3 clock recovery gain doubling: disabled (default)
#define RADIOLIB_SI443X_CLOCK_REC_GAIN_DOUBLE_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_SI443X_CLOCK_REC_LOOP_GAIN_MSB 0b00000010 // 2 0 clock recovery timing loop gain MSB
// RADIOLIB_SI443X_REG_CLOCK_REC_TIMING_LOOP_GAIN_0
#define RADIOLIB_SI443X_CLOCK_REC_LOOP_GAIN_LSB 0b10001111 // 7 0 clock recovery timing loop gain LSB
// RADIOLIB_SI443X_REG_RSSI_CLEAR_CHANNEL_THRESHOLD
#define RADIOLIB_SI443X_RSSI_CLEAR_CHANNEL_THRESHOLD 0b00011110 // 7 0 RSSI clear channel interrupt threshold
// RADIOLIB_SI443X_REG_AFC_LIMITER
#define RADIOLIB_SI443X_AFC_LIMITER 0x00 // 7 0 AFC limiter value
// RADIOLIB_SI443X_REG_OOK_COUNTER_1
#define RADIOLIB_SI443X_OOK_FREEZE_OFF 0b00000000 // 5 5 OOK moving average detector freeze: disabled (default)
#define RADIOLIB_SI443X_OOK_FREEZE_ON 0b00100000 // 5 5 enabled
#define RADIOLIB_SI443X_PEAK_DETECTOR_OFF 0b00000000 // 4 4 peak detector: disabled
#define RADIOLIB_SI443X_PEAK_DETECTOR_ON 0b00010000 // 4 4 enabled (default)
#define RADIOLIB_SI443X_OOK_MOVING_AVERAGE_OFF 0b00000000 // 3 3 OOK moving average: disabled
#define RADIOLIB_SI443X_OOK_MOVING_AVERAGE_ON 0b00001000 // 3 3 enabled (default)
#define RADIOLIB_SI443X_OOK_COUNTER_MSB 0b00000000 // 2 0 OOK counter MSB
// RADIOLIB_SI443X_REG_OOK_COUNTER_2
#define RADIOLIB_SI443X_OOK_COUNTER_LSB 0b10111100 // 7 0 OOK counter LSB
// RADIOLIB_SI443X_REG_SLICER_PEAK_HOLD
#define RADIOLIB_SI443X_PEAK_DETECTOR_ATTACK 0b00010000 // 6 4 OOK peak detector attach time
#define RADIOLIB_SI443X_PEAK_DETECTOR_DECAY 0b00001100 // 3 0 OOK peak detector decay time
// RADIOLIB_SI443X_REG_DATA_ACCESS_CONTROL
#define RADIOLIB_SI443X_PACKET_RX_HANDLING_OFF 0b00000000 // 7 7 packet Rx handling: disabled
#define RADIOLIB_SI443X_PACKET_RX_HANDLING_ON 0b10000000 // 7 7 enabled (default)
#define RADIOLIB_SI443X_LSB_FIRST_OFF 0b00000000 // 6 6 LSB first transmission: disabled (default)
#define RADIOLIB_SI443X_LSB_FIRST_ON 0b01000000 // 6 6 enabled
#define RADIOLIB_SI443X_CRC_DATA_ONLY_OFF 0b00000000 // 5 5 CRC calculated only from data fields: disabled (default)
#define RADIOLIB_SI443X_CRC_DATA_ONLY_ON 0b00100000 // 5 5 enabled
#define RADIOLIB_SI443X_SKIP_SECOND_PHASE_PREAMBLE_DET_OFF 0b00000000 // 4 4 skip second phase of preamble detection: disabled (default)
#define RADIOLIB_SI443X_SKIP_SECOND_PHASE_PREAMBLE_DET_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_SI443X_PACKET_TX_HANDLING_OFF 0b00000000 // 3 3 packet Tx handling: disabled
#define RADIOLIB_SI443X_PACKET_TX_HANDLING_ON 0b00001000 // 3 3 enabled (default)
#define RADIOLIB_SI443X_CRC_OFF 0b00000000 // 2 2 CRC: disabled
#define RADIOLIB_SI443X_CRC_ON 0b00000100 // 2 2 enabled (default)
#define RADIOLIB_SI443X_CRC_CCITT 0b00000000 // 1 0 CRC type: CCITT
#define RADIOLIB_SI443X_CRC_IBM_CRC16 0b00000001 // 1 0 IBM CRC-16 (default)
#define RADIOLIB_SI443X_CRC_IEC16 0b00000010 // 1 0 IEC-16
#define RADIOLIB_SI443X_CRC_BIACHEVA 0b00000011 // 1 0 Biacheva
// RADIOLIB_SI443X_REG_EZMAC_STATUS
#define RADIOLIB_SI443X_CRC_ALL_ONE 0b01000000 // 6 6 last received CRC was all ones
#define RADIOLIB_SI443X_PACKET_SEARCHING 0b00100000 // 5 5 radio is searching for a valid packet
#define RADIOLIB_SI443X_PACKET_RECEIVING 0b00010000 // 4 4 radio is currently receiving packet
#define RADIOLIB_SI443X_VALID_PACKET_RECEIVED 0b00001000 // 3 3 valid packet was received
#define RADIOLIB_SI443X_CRC_ERROR 0b00000100 // 2 2 CRC check failed
#define RADIOLIB_SI443X_PACKET_TRANSMITTING 0b00000010 // 1 1 radio is currently transmitting packet
#define RADIOLIB_SI443X_PACKET_SENT 0b00000001 // 0 0 packet sent
// RADIOLIB_SI443X_REG_HEADER_CONTROL_1
#define RADIOLIB_SI443X_BROADCAST_ADDR_CHECK_NONE 0b00000000 // 7 4 broadcast address check: none (default)
#define RADIOLIB_SI443X_BROADCAST_ADDR_CHECK_BYTE0 0b00010000 // 7 4 on byte 0
#define RADIOLIB_SI443X_BROADCAST_ADDR_CHECK_BYTE1 0b00100000 // 7 4 on byte 1
#define RADIOLIB_SI443X_BROADCAST_ADDR_CHECK_BYTE2 0b01000000 // 7 4 on byte 2
#define RADIOLIB_SI443X_BROADCAST_ADDR_CHECK_BYTE3 0b10000000 // 7 4 on byte 3
#define RADIOLIB_SI443X_RECEIVED_HEADER_CHECK_NONE 0b00000000 // 3 0 received header check: none
#define RADIOLIB_SI443X_RECEIVED_HEADER_CHECK_BYTE0 0b00000001 // 3 0 on byte 0
#define RADIOLIB_SI443X_RECEIVED_HEADER_CHECK_BYTE1 0b00000010 // 3 0 on byte 1
#define RADIOLIB_SI443X_RECEIVED_HEADER_CHECK_BYTE2 0b00000100 // 3 0 on byte 2 (default)
#define RADIOLIB_SI443X_RECEIVED_HEADER_CHECK_BYTE3 0b00001000 // 3 0 on byte 3 (default)
// RADIOLIB_SI443X_REG_HEADER_CONTROL_2
#define RADIOLIB_SI443X_SYNC_WORD_TIMEOUT_OFF 0b00000000 // 7 7 ignore timeout period when searching for sync word: disabled (default)
#define RADIOLIB_SI443X_SYNC_WORD_TIMEOUT_ON 0b10000000 // 7 7 enabled
#define RADIOLIB_SI443X_HEADER_LENGTH_HEADER_NONE 0b00000000 // 6 4 header length: none
#define RADIOLIB_SI443X_HEADER_LENGTH_HEADER_3 0b00010000 // 6 4 header 3
#define RADIOLIB_SI443X_HEADER_LENGTH_HEADER_32 0b00100000 // 6 4 header 3 and 2
#define RADIOLIB_SI443X_HEADER_LENGTH_HEADER_321 0b00110000 // 6 4 header 3, 2 and 1 (default)
#define RADIOLIB_SI443X_HEADER_LENGTH_HEADER_3210 0b01000000 // 6 4 header 3, 2, 1, and 0
#define RADIOLIB_SI443X_FIXED_PACKET_LENGTH_OFF 0b00000000 // 3 3 fixed packet length mode: disabled (default)
#define RADIOLIB_SI443X_FIXED_PACKET_LENGTH_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_SI443X_SYNC_LENGTH_SYNC_3 0b00000000 // 2 1 sync word length: sync 3
#define RADIOLIB_SI443X_SYNC_LENGTH_SYNC_32 0b00000010 // 2 1 sync 3 and 2 (default)
#define RADIOLIB_SI443X_SYNC_LENGTH_SYNC_321 0b00000100 // 2 1 sync 3, 2 and 1
#define RADIOLIB_SI443X_SYNC_LENGTH_SYNC_3210 0b00000110 // 2 1 sync 3, 2, 1 and 0
#define RADIOLIB_SI443X_PREAMBLE_LENGTH_MSB 0b00000000 // 0 0 preamble length MSB
// RADIOLIB_SI443X_REG_PREAMBLE_LENGTH
#define RADIOLIB_SI443X_PREAMBLE_LENGTH_LSB 0b00001000 // 0 0 preamble length LSB, defaults to 32 bits
// RADIOLIB_SI443X_REG_PREAMBLE_DET_CONTROL
#define RADIOLIB_SI443X_PREAMBLE_DET_THRESHOLD 0b00101000 // 7 3 number of 4-bit nibbles in valid preamble, defaults to 20 bits
#define RADIOLIB_SI443X_RSSI_OFFSET 0b00000010 // 2 0 RSSI calculation offset, defaults to +8 dB
// RADIOLIB_SI443X_REG_SYNC_WORD_3 - RADIOLIB_SI443X_REG_SYNC_WORD_0
#define RADIOLIB_SI443X_SYNC_WORD_3 0x2D // 7 0 sync word: 4th byte (MSB)
#define RADIOLIB_SI443X_SYNC_WORD_2 0xD4 // 7 0 3rd byte
#define RADIOLIB_SI443X_SYNC_WORD_1 0x00 // 7 0 2nd byte
#define RADIOLIB_SI443X_SYNC_WORD_0 0x00 // 7 0 1st byte (LSB)
// RADIOLIB_SI443X_REG_CHANNEL_FILTER_COEFF
#define RADIOLIB_SI443X_INVALID_PREAMBLE_THRESHOLD 0b00000000 // 7 4 invalid preamble threshold in nibbles
// RADIOLIB_SI443X_REG_XOSC_CONTROL_TEST
#define RADIOLIB_SI443X_STATE_LOW_POWER 0b00000000 // 7 5 chip power state: low power
#define RADIOLIB_SI443X_STATE_READY 0b00100000 // 7 5 ready
#define RADIOLIB_SI443X_STATE_TUNE 0b01100000 // 7 5 tune
#define RADIOLIB_SI443X_STATE_TX 0b01000000 // 7 5 Tx
#define RADIOLIB_SI443X_STATE_RX 0b11100000 // 7 5 Rx
// RADIOLIB_SI443X_REG_AGC_OVERRIDE_1
#define RADIOLIB_SI443X_AGC_GAIN_INCREASE_OFF 0b00000000 // 6 6 AGC gain increase override: disabled (default)
#define RADIOLIB_SI443X_AGC_GAIN_INCREASE_ON 0b01000000 // 6 6 enabled
#define RADIOLIB_SI443X_AGC_OFF 0b00000000 // 5 5 AGC loop: disabled
#define RADIOLIB_SI443X_AGC_ON 0b00100000 // 5 5 enabled (default)
#define RADIOLIB_SI443X_LNA_GAIN_MIN 0b00000000 // 4 4 LNA gain select: 5 dB (default)
#define RADIOLIB_SI443X_LNA_GAIN_MAX 0b00010000 // 4 4 25 dB
#define RADIOLIB_SI443X_PGA_GAIN_OVERRIDE 0b00000000 // 3 0 PGA gain override, gain = RADIOLIB_SI443X_PGA_GAIN_OVERRIDE * 3 dB
// RADIOLIB_SI443X_REG_TX_POWER
#define RADIOLIB_SI443X_LNA_SWITCH_OFF 0b00000000 // 3 3 LNA switch control: disabled
#define RADIOLIB_SI443X_LNA_SWITCH_ON 0b00001000 // 3 3 enabled (default)
#define RADIOLIB_SI443X_OUTPUT_POWER 0b00000000 // 2 0 output power in 3 dB steps, 0 is chip min, 7 is chip max
// RADIOLIB_SI443X_REG_TX_DATA_RATE_1 + RADIOLIB_SI443X_REG_TX_DATA_RATE_0
#define RADIOLIB_SI443X_DATA_RATE_MSB 0x0A // 7 0 data rate: DR = 10^6 * (RADIOLIB_SI443X_DATA_RATE / 2^16) in high data rate mode or
#define RADIOLIB_SI443X_DATA_RATE_LSB 0x3D // 7 0 DR = 10^6 * (RADIOLIB_SI443X_DATA_RATE / 2^21) in low data rate mode (defaults to 40 kbps)
// RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_1
#define RADIOLIB_SI443X_HIGH_DATA_RATE_MODE 0b00000000 // 5 5 data rate: above 30 kbps (default)
#define RADIOLIB_SI443X_LOW_DATA_RATE_MODE 0b00100000 // 5 5 below 30 kbps
#define RADIOLIB_SI443X_PACKET_HANDLER_POWER_DOWN_OFF 0b00000000 // 4 4 power off packet handler in low power mode: disabled (default)
#define RADIOLIB_SI443X_PACKET_HANDLER_POWER_DOWN_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_SI443X_MANCHESTER_PREAMBLE_POL_LOW 0b00000000 // 3 3 preamble polarity in Manchester mode: low
#define RADIOLIB_SI443X_MANCHESTER_PREAMBLE_POL_HIGH 0b00001000 // 3 3 high (default)
#define RADIOLIB_SI443X_MANCHESTER_INVERTED_OFF 0b00000000 // 2 2 inverted Manchester encoding: disabled
#define RADIOLIB_SI443X_MANCHESTER_INVERTED_ON 0b00000100 // 2 2 enabled (default)
#define RADIOLIB_SI443X_MANCHESTER_OFF 0b00000000 // 1 1 Manchester encoding: disabled (default)
#define RADIOLIB_SI443X_MANCHESTER_ON 0b00000010 // 1 1 enabled
#define RADIOLIB_SI443X_WHITENING_OFF 0b00000000 // 0 0 data whitening: disabled (default)
#define RADIOLIB_SI443X_WHITENING_ON 0b00000001 // 0 0 enabled
// RADIOLIB_SI443X_REG_MODULATION_MODE_CONTROL_2
#define RADIOLIB_SI443X_TX_DATA_CLOCK_NONE 0b00000000 // 7 6 Tx data clock: disabled (default)
#define RADIOLIB_SI443X_TX_DATA_CLOCK_GPIO 0b01000000 // 7 6 GPIO pin
#define RADIOLIB_SI443X_TX_DATA_CLOCK_SDI 0b10000000 // 7 6 SDI pin
#define RADIOLIB_SI443X_TX_DATA_CLOCK_NIRQ 0b11000000 // 7 6 nIRQ pin
#define RADIOLIB_SI443X_TX_DATA_SOURCE_GPIO 0b00000000 // 5 4 Tx data source in direct mode: GPIO pin (default)
#define RADIOLIB_SI443X_TX_DATA_SOURCE_SDI 0b00010000 // 5 4 SDI pin
#define RADIOLIB_SI443X_TX_DATA_SOURCE_FIFO 0b00100000 // 5 4 FIFO
#define RADIOLIB_SI443X_TX_DATA_SOURCE_PN9 0b00110000 // 5 4 PN9 internal
#define RADIOLIB_SI443X_TX_RX_INVERTED_OFF 0b00000000 // 3 3 Tx/Rx data inverted: disabled (default)
#define RADIOLIB_SI443X_TX_RX_INVERTED_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_SI443X_FREQUENCY_DEVIATION_MSB 0b00000000 // 2 2 frequency deviation MSB
#define RADIOLIB_SI443X_MODULATION_NONE 0b00000000 // 1 0 modulation type: unmodulated carrier (default)
#define RADIOLIB_SI443X_MODULATION_OOK 0b00000001 // 1 0 OOK
#define RADIOLIB_SI443X_MODULATION_FSK 0b00000010 // 1 0 FSK
#define RADIOLIB_SI443X_MODULATION_GFSK 0b00000011 // 1 0 GFSK
// RADIOLIB_SI443X_REG_FREQUENCY_DEVIATION
#define RADIOLIB_SI443X_FREQUENCY_DEVIATION_LSB 0b00100000 // 7 0 frequency deviation LSB, Fd = 625 Hz * RADIOLIB_SI443X_FREQUENCY_DEVIATION, defaults to 20 kHz
// RADIOLIB_SI443X_REG_FREQUENCY_OFFSET_1 + RADIOLIB_SI443X_REG_FREQUENCY_OFFSET_2
#define RADIOLIB_SI443X_FREQUENCY_OFFSET_MSB 0x00 // 7 0 frequency offset:
#define RADIOLIB_SI443X_FREQUENCY_OFFSET_LSB 0x00 // 1 0 Foff = 156.25 Hz * (RADIOLIB_SI443X_BAND_SELECT + 1) * RADIOLIB_SI443X_FREQUENCY_OFFSET, defaults to 156.25 Hz
// RADIOLIB_SI443X_REG_FREQUENCY_BAND_SELECT
#define RADIOLIB_SI443X_SIDE_BAND_SELECT_LOW 0b00000000 // 6 6 Rx LO tuning: below channel frequency (default)
#define RADIOLIB_SI443X_SIDE_BAND_SELECT_HIGH 0b01000000 // 6 6 above channel frequency
#define RADIOLIB_SI443X_BAND_SELECT_LOW 0b00000000 // 5 5 band select: low, 240 - 479.9 MHz
#define RADIOLIB_SI443X_BAND_SELECT_HIGH 0b00100000 // 5 5 high, 480 - 960 MHz (default)
#define RADIOLIB_SI443X_FREQUENCY_BAND_SELECT 0b00010101 // 4 0 frequency band select
// RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_1 + RADIOLIB_SI443X_REG_NOM_CARRIER_FREQUENCY_0
#define RADIOLIB_SI443X_NOM_CARRIER_FREQUENCY_MSB 0b10111011 // 7 0 nominal carrier frequency:
#define RADIOLIB_SI443X_NOM_CARRIER_FREQUENCY_LSB 0b10000000 // 7 0 Fc = (RADIOLIB_SI443X_BAND_SELECT + 1)*10*(RADIOLIB_SI443X_FREQUENCY_BAND_SELECT + 24) + (RADIOLIB_SI443X_NOM_CARRIER_FREQUENCY - RADIOLIB_SI443X_FREQUENCY_OFFSET)/6400 [MHz]
// RADIOLIB_SI443X_REG_FREQUENCY_HOPPING_CHANNEL_SEL
#define RADIOLIB_SI443X_FREQUENCY_HOPPING_CHANNEL 0x00 // 7 0 frequency hopping channel number
// RADIOLIB_SI443X_REG_FREQUENCY_HOPPING_STEP_SIZE
#define RADIOLIB_SI443X_FREQUENCY_HOPPING_STEP_SIZE 0x00 // 7 0 frequency hopping step size
// RADIOLIB_SI443X_REG_TX_FIFO_CONTROL_1
#define RADIOLIB_SI443X_TX_FIFO_ALMOST_FULL_THRESHOLD 0x37 // 5 0 Tx FIFO almost full threshold
// RADIOLIB_SI443X_REG_TX_FIFO_CONTROL_2
#define RADIOLIB_SI443X_TX_FIFO_ALMOST_EMPTY_THRESHOLD 0x04 // 5 0 Tx FIFO almost full threshold
// RADIOLIB_SI443X_REG_RX_FIFO_CONTROL
#define RADIOLIB_SI443X_RX_FIFO_ALMOST_FULL_THRESHOLD 0x37 // 5 0 Rx FIFO almost full threshold
/*!
\class Si443x
\brief Base class for Si443x series. All derived classes for Si443x (e.g. Si4431 or Si4432) inherit from this base class.
This class should not be instantiated directly from Arduino sketch, only from its derived classes.
*/
class Si443x: public PhysicalLayer {
public:
// introduce PhysicalLayer overloads
using PhysicalLayer::transmit;
using PhysicalLayer::receive;
using PhysicalLayer::startTransmit;
using PhysicalLayer::readData;
// constructor
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
Si443x(Module* mod);
Module* getMod();
// basic methods
/*!
\brief Initialization method.
\param br Bit rate of the FSK transmission in kbps (kilobits per second).
\param freqDev Frequency deviation of the FSK transmission in kHz.
\param rxBw Receiver bandwidth in kHz.
\param preambleLen Preamble Length in bits.
\returns \ref status_codes
*/
int16_t begin(float br, float freqDev, float rxBw, uint8_t preambleLen);
/*!
\brief Reset method. Will reset the chip to the default state using SDN pin.
*/
void reset();
/*!
\brief Binary transmit method. Will transmit arbitrary binary data up to 64 bytes long.
For overloads to transmit Arduino String or C-string, see PhysicalLayer::transmit.
\param data Binary data that will be transmitted.
\param len Length of binary data to transmit (in bytes).
\param addr Node address to transmit the packet to.
\returns \ref status_codes
*/
int16_t transmit(uint8_t* data, size_t len, uint8_t addr = 0) override;
/*!
\brief Binary receive method. Will attempt to receive arbitrary binary data up to 64 bytes long.
For overloads to receive Arduino String, see PhysicalLayer::receive.
\param data Pointer to array to save the received binary data.
\param len Number of bytes that will be received. Must be known in advance for binary transmissions.
\returns \ref status_codes
*/
int16_t receive(uint8_t* data, size_t len) override;
/*!
\brief Sets the module to sleep to save power. %Module will not be able to transmit or receive any data while in sleep mode.
%Module will wake up automatically when methods like transmit or receive are called.
\returns \ref status_codes
*/
int16_t sleep();
/*!
\brief Sets the module to standby.
\returns \ref status_codes
*/
int16_t standby() override;
/*!
\brief Enables direct transmission mode. While in direct mode, the module will not be able to transmit or receive packets.
\param FRF 24-bit raw frequency value to start transmitting at. Required for quick frequency shifts in RTTY.
\returns \ref status_codes
*/
int16_t transmitDirect(uint32_t frf = 0) override;
/*!
\brief Enables direct reception mode. While in direct mode, the module will not be able to transmit or receive packets.
\returns \ref status_codes
*/
int16_t receiveDirect() override;
/*!
\brief Disables direct mode and enables packet mode, allowing the module to receive packets.
\returns \ref status_codes
*/
int16_t packetMode();
// interrupt methods
/*!
\brief Sets interrupt service routine to call when IRQ activates.
\param func ISR to call.
*/
void setIrqAction(void (*func)(void));
/*!
\brief Clears interrupt service routine to call when IRQ activates.
*/
void clearIrqAction();
/*!
\brief Interrupt-driven binary transmit method. Will start transmitting arbitrary binary data up to 64 bytes long.
\param data Binary data that will be transmitted.
\param len Length of binary data to transmit (in bytes).
\param addr Node address to transmit the packet to.
\returns \ref status_codes
*/
int16_t startTransmit(uint8_t* data, size_t len, uint8_t addr = 0) override;
/*!
\brief Interrupt-driven receive method. IRQ will be activated when full valid packet is received.
\returns \ref status_codes
*/
int16_t startReceive();
/*!
\brief Reads data that was received after calling startReceive method. This method reads len characters.
\param data Pointer to array to save the received binary data.
\param len Number of bytes that will be read. When set to 0, the packet length will be retreived automatically.
When more bytes than received are requested, only the number of bytes requested will be returned.
\returns \ref status_codes
*/
int16_t readData(uint8_t* data, size_t len) override;
// configuration methods
/*!
\brief Sets FSK bit rate. Allowed values range from 0.123 to 256.0 kbps.
\param br Bit rate to be set (in kbps).
\returns \ref status_codes
*/
int16_t setBitRate(float br);
/*!
\brief Sets FSK frequency deviation from carrier frequency. Allowed values range from 0.625 to 320.0 kHz.
\param freqDev Frequency deviation to be set (in kHz).
\returns \ref status_codes
*/
int16_t setFrequencyDeviation(float freqDev) override;
/*!
\brief Sets receiver bandwidth. Allowed values range from 2.6 to 620.7 kHz.
\param rxBw Receiver bandwidth to be set in kHz.
\returns \ref status_codes
*/
int16_t setRxBandwidth(float rxBw);
/*!
\brief Sets sync word. Up to 4 bytes can be set as sync word.
\param syncWord Pointer to the array of sync word bytes.
\param len Sync word length in bytes.
*/
int16_t setSyncWord(uint8_t* syncWord, size_t len);
/*!
\brief Sets preamble length.
\param preambleLen Preamble length to be set (in bits).
\returns \ref status_codes
*/
int16_t setPreambleLength(uint8_t preambleLen);
/*!
\brief Query modem for the packet length of received payload.
\param update Update received packet length. Will return cached value when set to false.
\returns Length of last received packet in bytes.
*/
size_t getPacketLength(bool update = true) override;
/*!
\brief Sets transmission encoding. Only available in FSK mode.
Allowed values are RADIOLIB_ENCODING_NRZ, RADIOLIB_ENCODING_MANCHESTER and RADIOLIB_ENCODING_WHITENING.
\param encoding Encoding to be used.
\returns \ref status_codes
*/
int16_t setEncoding(uint8_t encoding) override;
/*!
\brief Sets Gaussian filter bandwidth-time product that will be used for data shaping. Only available in FSK mode with FSK modulation.
Allowed values are RADIOLIB_SHAPING_0_3, RADIOLIB_SHAPING_0_5 or RADIOLIB_SHAPING_1_0. Set to RADIOLIB_SHAPING_NONE to disable data shaping.
\param sh Gaussian shaping bandwidth-time product that will be used for data shaping
\returns \ref status_codes
*/
int16_t setDataShaping(uint8_t sh) override;
/*!
\brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state.
When using automatic RF switch control, DO NOT change the pin mode of rxEn or txEn from Arduino sketch!
\param rxEn RX enable pin.
\param txEn TX enable pin.
*/
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
/*!
\brief Get one truly random byte from RSSI noise.
\returns TRNG byte.
*/
uint8_t randomByte();
/*!
\brief Read version SPI register. Should return RADIOLIB_SI443X_DEVICE_VERSION (0x06) if Si443x is connected and working.
\returns Version register contents or \ref status_codes
*/
int16_t getChipVersion();
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
/*!
\brief Set interrupt service routine function to call when data bit is receveid in direct mode.
\param func Pointer to interrupt service routine.
*/
void setDirectAction(void (*func)(void));
/*!
\brief Function to read and process data bit in direct reception mode.
\param pin Pin on which to read.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
#endif
/*!
\brief Set modem in fixed packet length mode.
\param len Packet length.
\returns \ref status_codes
*/
int16_t fixedPacketLengthMode(uint8_t len = RADIOLIB_SI443X_MAX_PACKET_LENGTH);
/*!
\brief Set modem in variable packet length mode.
\param len Maximum packet length.
\returns \ref status_codes
*/
int16_t variablePacketLengthMode(uint8_t maxLen = RADIOLIB_SI443X_MAX_PACKET_LENGTH);
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)
protected:
#endif
Module* _mod;
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
float _br = 0;
float _freqDev = 0;
float _freq = 0;
size_t _packetLength = 0;
bool _packetLengthQueried = false;
uint8_t _packetLengthConfig = RADIOLIB_SI443X_FIXED_PACKET_LENGTH_ON;
int16_t setFrequencyRaw(float newFreq);
int16_t setPacketMode(uint8_t mode, uint8_t len);
#if !defined(RADIOLIB_GODMODE)
private:
#endif
bool findChip();
void clearIRQFlags();
void clearFIFO(size_t count);
int16_t config();
int16_t updateClockRecovery();
int16_t directMode();
};
#endif
#endif
+606
View File
@@ -0,0 +1,606 @@
#include "nRF24.h"
#if !defined(RADIOLIB_EXCLUDE_NRF24)
nRF24::nRF24(Module* mod) : PhysicalLayer(RADIOLIB_NRF24_FREQUENCY_STEP_SIZE, RADIOLIB_NRF24_MAX_PACKET_LENGTH) {
_mod = mod;
}
Module* nRF24::getMod() {
return(_mod);
}
int16_t nRF24::begin(int16_t freq, int16_t dataRate, int8_t power, uint8_t addrWidth) {
// set module properties
_mod->SPIreadCommand = RADIOLIB_NRF24_CMD_READ;
_mod->SPIwriteCommand = RADIOLIB_NRF24_CMD_WRITE;
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
// set pin mode on RST (connected to nRF24 CE pin)
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
// wait for minimum power-on reset duration
_mod->delay(100);
// check SPI connection
int16_t val = _mod->SPIgetRegValue(RADIOLIB_NRF24_REG_SETUP_AW);
if(!((val >= 0) && (val <= 3))) {
RADIOLIB_DEBUG_PRINTLN(F("No nRF24 found!"));
_mod->term();
return(RADIOLIB_ERR_CHIP_NOT_FOUND);
}
RADIOLIB_DEBUG_PRINTLN(F("M\tnRF24"));
// configure settings inaccessible by public API
int16_t state = config();
RADIOLIB_ASSERT(state);
// set mode to standby
state = standby();
RADIOLIB_ASSERT(state);
// set frequency
state = setFrequency(freq);
RADIOLIB_ASSERT(state);
// set data rate
state = setDataRate(dataRate);
RADIOLIB_ASSERT(state);
// set output power
state = setOutputPower(power);
RADIOLIB_ASSERT(state);
// set address width
state = setAddressWidth(addrWidth);
RADIOLIB_ASSERT(state);
// set CRC
state = setCrcFiltering(true);
RADIOLIB_ASSERT(state);
// set auto-ACK on all pipes
state = setAutoAck(true);
RADIOLIB_ASSERT(state);
return(state);
}
int16_t nRF24::sleep() {
return(_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_POWER_DOWN, 1, 1));
}
int16_t nRF24::standby() {
// make sure carrier output is disabled
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_CONT_WAVE_OFF, 7, 7);
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_PLL_LOCK_OFF, 4, 4);
_mod->digitalWrite(_mod->getRst(), LOW);
// use standby-1 mode
return(_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_POWER_UP, 1, 1));
}
int16_t nRF24::transmit(uint8_t* data, size_t len, uint8_t addr) {
// start transmission
int16_t state = startTransmit(data, len, addr);
RADIOLIB_ASSERT(state);
// wait until transmission is finished
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
// check maximum number of retransmits
if(getStatus(RADIOLIB_NRF24_MAX_RT)) {
standby();
clearIRQ();
return(RADIOLIB_ERR_ACK_NOT_RECEIVED);
}
// check timeout: 15 retries * 4ms (max Tx time as per datasheet)
if(_mod->micros() - start >= 60000) {
standby();
clearIRQ();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
}
// clear interrupts
clearIRQ();
return(state);
}
int16_t nRF24::receive(uint8_t* data, size_t len) {
// start reception
int16_t state = startReceive();
RADIOLIB_ASSERT(state);
// wait for Rx_DataReady or timeout
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
// check timeout: 15 retries * 4ms (max Tx time as per datasheet)
if(_mod->micros() - start >= 60000) {
standby();
clearIRQ();
return(RADIOLIB_ERR_RX_TIMEOUT);
}
}
// read the received data
return(readData(data, len));
}
int16_t nRF24::transmitDirect(uint32_t frf) {
// set raw frequency value
if(frf != 0) {
uint8_t freqRaw = frf - 2400;
_mod->SPIwriteRegister(RADIOLIB_NRF24_REG_RF_CH, freqRaw & 0b01111111);
}
// output carrier
int16_t state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_PTX, 0, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_CONT_WAVE_ON, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_PLL_LOCK_ON, 4, 4);
_mod->digitalWrite(_mod->getRst(), HIGH);
return(state);
}
int16_t nRF24::receiveDirect() {
// nRF24 is unable to directly output demodulated data
// this method is implemented only for PhysicalLayer compatibility
return(RADIOLIB_ERR_NONE);
}
void nRF24::setIrqAction(void (*func)(void)) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, FALLING);
}
int16_t nRF24::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
// suppress unused variable warning
(void)addr;
// check packet length
if(len > RADIOLIB_NRF24_MAX_PACKET_LENGTH) {
return(RADIOLIB_ERR_PACKET_TOO_LONG);
}
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// enable primary Tx mode
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_PTX, 0, 0);
// clear interrupts
clearIRQ();
// enable Tx_DataSent interrupt
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_MASK_TX_DS_IRQ_ON, 5, 5);
RADIOLIB_ASSERT(state);
// flush Tx FIFO
SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_TX);
// fill Tx FIFO
uint8_t buff[32];
memset(buff, 0x00, 32);
memcpy(buff, data, len);
SPIwriteTxPayload(data, len);
// CE high to start transmitting
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), LOW);
return(state);
}
int16_t nRF24::startReceive() {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// enable primary Rx mode
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_PRX, 0, 0);
RADIOLIB_ASSERT(state);
// enable Rx_DataReady interrupt
clearIRQ();
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_MASK_RX_DR_IRQ_ON, 6, 6);
RADIOLIB_ASSERT(state);
// flush Rx FIFO
SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_RX);
// CE high to start receiving
_mod->digitalWrite(_mod->getRst(), HIGH);
// wait to enter Rx state
_mod->delay(1);
return(state);
}
int16_t nRF24::readData(uint8_t* data, size_t len) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// get packet length
size_t length = getPacketLength();
if((len != 0) && (len < length)) {
// user requested less data than we got, only return what was requested
length = len;
}
// read packet data
SPIreadRxPayload(data, length);
// clear interrupt
clearIRQ();
return(RADIOLIB_ERR_NONE);
}
int16_t nRF24::setFrequency(int16_t freq) {
RADIOLIB_CHECK_RANGE(freq, 2400, 2525, RADIOLIB_ERR_INVALID_FREQUENCY);
// set frequency
uint8_t freqRaw = freq - 2400;
return(_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_CH, freqRaw, 6, 0));
}
int16_t nRF24::setDataRate(int16_t dataRate) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// set data rate
if(dataRate == 250) {
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_250_KBPS, 5, 5);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_250_KBPS, 3, 3);
} else if(dataRate == 1000) {
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_1_MBPS, 5, 5);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_1_MBPS, 3, 3);
} else if(dataRate == 2000) {
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_2_MBPS, 5, 5);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_2_MBPS, 3, 3);
} else {
return(RADIOLIB_ERR_INVALID_DATA_RATE);
}
return(state);
}
int16_t nRF24::setOutputPower(int8_t power) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// check allowed values
uint8_t powerRaw = 0;
switch(power) {
case -18:
powerRaw = RADIOLIB_NRF24_RF_PWR_18_DBM;
break;
case -12:
powerRaw = RADIOLIB_NRF24_RF_PWR_12_DBM;
break;
case -6:
powerRaw = RADIOLIB_NRF24_RF_PWR_6_DBM;
break;
case 0:
powerRaw = RADIOLIB_NRF24_RF_PWR_0_DBM;
break;
default:
return(RADIOLIB_ERR_INVALID_OUTPUT_POWER);
}
// write new register value
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, powerRaw, 2, 1);
return(state);
}
int16_t nRF24::setAddressWidth(uint8_t addrWidth) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// set address width
switch(addrWidth) {
case 2:
// Even if marked as 'Illegal' on the datasheet this will work:
// http://travisgoodspeed.blogspot.com/2011/02/promiscuity-is-nrf24l01s-duty.html
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_2_BYTES, 1, 0);
break;
case 3:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_3_BYTES, 1, 0);
break;
case 4:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_4_BYTES, 1, 0);
break;
case 5:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_5_BYTES, 1, 0);
break;
default:
return(RADIOLIB_ERR_INVALID_ADDRESS_WIDTH);
}
// save address width
_addrWidth = addrWidth;
return(state);
}
int16_t nRF24::setTransmitPipe(uint8_t* addr) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// set transmit address
_mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_TX_ADDR, addr, _addrWidth);
// set Rx pipe 0 address (for ACK)
_mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_RX_ADDR_P0, addr, _addrWidth);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P0_ON, 0, 0);
return(state);
}
int16_t nRF24::setReceivePipe(uint8_t pipeNum, uint8_t* addr) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// write full pipe 0 - 1 address and enable the pipe
switch(pipeNum) {
case 0:
_mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_RX_ADDR_P0, addr, _addrWidth);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P0_ON, 0, 0);
break;
case 1:
_mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_RX_ADDR_P1, addr, _addrWidth);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P1_ON, 1, 1);
break;
default:
return(RADIOLIB_ERR_INVALID_PIPE_NUMBER);
}
return(state);
}
int16_t nRF24::setReceivePipe(uint8_t pipeNum, uint8_t addrByte) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
// write unique pipe 2 - 5 address and enable the pipe
switch(pipeNum) {
case 2:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P2, addrByte);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P2_ON, 2, 2);
break;
case 3:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P3, addrByte);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P3_ON, 3, 3);
break;
case 4:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P4, addrByte);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P4_ON, 4, 4);
break;
case 5:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P5, addrByte);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P5_ON, 5, 5);
break;
default:
return(RADIOLIB_ERR_INVALID_PIPE_NUMBER);
}
return(state);
}
int16_t nRF24::disablePipe(uint8_t pipeNum) {
// set mode to standby
int16_t state = standby();
RADIOLIB_ASSERT(state);
switch(pipeNum) {
case 0:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P0_OFF, 0, 0);
break;
case 1:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P1_OFF, 1, 1);
break;
case 2:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P2_OFF, 2, 2);
break;
case 3:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P3_OFF, 3, 3);
break;
case 4:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P4_OFF, 4, 4);
break;
case 5:
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P5_OFF, 5, 5);
break;
default:
return(RADIOLIB_ERR_INVALID_PIPE_NUMBER);
}
return(state);
}
int16_t nRF24::getStatus(uint8_t mask) {
return(_mod->SPIgetRegValue(RADIOLIB_NRF24_REG_STATUS) & mask);
}
bool nRF24::isCarrierDetected() {
return(_mod->SPIgetRegValue(RADIOLIB_NRF24_REG_RPD, 0, 0) == 1);
}
int16_t nRF24::setFrequencyDeviation(float freqDev) {
// nRF24 is unable to set frequency deviation
// this method is implemented only for PhysicalLayer compatibility
(void)freqDev;
return(RADIOLIB_ERR_NONE);
}
size_t nRF24::getPacketLength(bool update) {
(void)update;
uint8_t length = 0;
SPItransfer(RADIOLIB_NRF24_CMD_READ_RX_PAYLOAD_WIDTH, false, NULL, &length, 1);
return((size_t)length);
}
int16_t nRF24::setCrcFiltering(bool crcOn) {
// Auto Ack needs to be disabled in order to disable CRC.
if (!crcOn) {
int16_t status = setAutoAck(false);
RADIOLIB_ASSERT(status)
}
// Disable CRC
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, (crcOn ? RADIOLIB_NRF24_CRC_ON : RADIOLIB_NRF24_CRC_OFF), 3, 3);
}
int16_t nRF24::setAutoAck(bool autoAckOn){
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_ALL_ON : RADIOLIB_NRF24_AA_ALL_OFF), 5, 0);
}
int16_t nRF24::setAutoAck(uint8_t pipeNum, bool autoAckOn){
switch(pipeNum) {
case 0:
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P0_ON : RADIOLIB_NRF24_AA_P0_OFF), 0, 0);
break;
case 1:
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P1_ON : RADIOLIB_NRF24_AA_P1_OFF), 1, 1);
break;
case 2:
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P2_ON : RADIOLIB_NRF24_AA_P2_OFF), 2, 2);
break;
case 3:
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P3_ON : RADIOLIB_NRF24_AA_P3_OFF), 3, 3);
break;
case 4:
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P4_ON : RADIOLIB_NRF24_AA_P4_OFF), 4, 4);
break;
case 5:
return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P5_ON : RADIOLIB_NRF24_AA_P5_OFF), 5, 5);
break;
default:
return (RADIOLIB_ERR_INVALID_PIPE_NUMBER);
}
}
int16_t nRF24::setDataShaping(uint8_t sh) {
// nRF24 is unable to set data shaping
// this method is implemented only for PhysicalLayer compatibility
(void)sh;
return(RADIOLIB_ERR_NONE);
}
int16_t nRF24::setEncoding(uint8_t encoding) {
// nRF24 is unable to set encoding
// this method is implemented only for PhysicalLayer compatibility
(void)encoding;
return(RADIOLIB_ERR_NONE);
}
uint8_t nRF24::randomByte() {
// nRF24 is unable to measure RSSI, hence no TRNG
// this method is implemented only for PhysicalLayer compatibility
return(0);
}
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
void nRF24::setDirectAction(void (*func)(void)) {
// nRF24 is unable to perform direct mode actions
// this method is implemented only for PhysicalLayer compatibility
(void)func;
}
void nRF24::readBit(RADIOLIB_PIN_TYPE pin) {
// nRF24 is unable to perform direct mode actions
// this method is implemented only for PhysicalLayer compatibility
(void)pin;
}
#endif
void nRF24::clearIRQ() {
// clear status bits
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_STATUS, RADIOLIB_NRF24_RX_DR | RADIOLIB_NRF24_TX_DS | RADIOLIB_NRF24_MAX_RT, 6, 4);
// disable interrupts
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_MASK_RX_DR_IRQ_OFF | RADIOLIB_NRF24_MASK_TX_DS_IRQ_OFF | RADIOLIB_NRF24_MASK_MAX_RT_IRQ_OFF, 6, 4);
}
int16_t nRF24::config() {
// enable 16-bit CRC
int16_t state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_CRC_ON | RADIOLIB_NRF24_CRC_16, 3, 2);
RADIOLIB_ASSERT(state);
// set 15 retries and delay 1500 (5*250) us
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_RETR, (5 << 4) | 5);
// set features: dynamic payload on, payload with ACK packets off, dynamic ACK off
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_FEATURE, RADIOLIB_NRF24_DPL_ON | RADIOLIB_NRF24_ACK_PAY_OFF | RADIOLIB_NRF24_DYN_ACK_OFF, 2, 0);
RADIOLIB_ASSERT(state);
// enable dynamic payloads
state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_DYNPD, RADIOLIB_NRF24_DPL_ALL_ON, 5, 0);
RADIOLIB_ASSERT(state);
// reset IRQ
clearIRQ();
// clear status
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_STATUS, RADIOLIB_NRF24_RX_DR | RADIOLIB_NRF24_TX_DS | RADIOLIB_NRF24_MAX_RT, 6, 4);
// flush FIFOs
SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_TX);
SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_RX);
// power up
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_POWER_UP, 1, 1);
_mod->delay(5);
return(state);
}
void nRF24::SPIreadRxPayload(uint8_t* data, uint8_t numBytes) {
SPItransfer(RADIOLIB_NRF24_CMD_READ_RX_PAYLOAD, false, NULL, data, numBytes);
}
void nRF24::SPIwriteTxPayload(uint8_t* data, uint8_t numBytes) {
SPItransfer(RADIOLIB_NRF24_CMD_WRITE_TX_PAYLOAD, true, data, NULL, numBytes);
}
void nRF24::SPItransfer(uint8_t cmd, bool write, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes) {
// start transfer
_mod->digitalWrite(_mod->getCs(), LOW);
_mod->SPIbeginTransaction();
// send command
_mod->SPItransfer(cmd);
// send data
if(write) {
for(uint8_t i = 0; i < numBytes; i++) {
_mod->SPItransfer(dataOut[i]);
}
} else {
for(uint8_t i = 0; i < numBytes; i++) {
dataIn[i] = _mod->SPItransfer(0x00);
}
}
// stop transfer
_mod->SPIendTransaction();
_mod->digitalWrite(_mod->getCs(), HIGH);
}
#endif
+511
View File
@@ -0,0 +1,511 @@
#if !defined(_RADIOLIB_NRF24_H) && !defined(RADIOLIB_EXCLUDE_NRF24)
#define _RADIOLIB_NRF24_H
#include "../../Module.h"
#include "../../TypeDef.h"
#include "../../protocols/PhysicalLayer/PhysicalLayer.h"
// nRF24 physical layer properties
#define RADIOLIB_NRF24_FREQUENCY_STEP_SIZE 1000000.0
#define RADIOLIB_NRF24_MAX_PACKET_LENGTH 32
// nRF24 SPI commands
#define RADIOLIB_NRF24_CMD_READ 0b00000000
#define RADIOLIB_NRF24_CMD_WRITE 0b00100000
#define RADIOLIB_NRF24_CMD_READ_RX_PAYLOAD 0b01100001
#define RADIOLIB_NRF24_CMD_WRITE_TX_PAYLOAD 0b10100000
#define RADIOLIB_NRF24_CMD_FLUSH_TX 0b11100001
#define RADIOLIB_NRF24_CMD_FLUSH_RX 0b11100010
#define RADIOLIB_NRF24_CMD_REUSE_TX_PAXLOAD 0b11100011
#define RADIOLIB_NRF24_CMD_READ_RX_PAYLOAD_WIDTH 0b01100000
#define RADIOLIB_NRF24_CMD_WRITE_ACK_PAYLOAD 0b10101000
#define RADIOLIB_NRF24_CMD_WRITE_TX_PAYLOAD_NOACK 0b10110000
#define RADIOLIB_NRF24_CMD_NOP 0b11111111
// nRF24 register map
#define RADIOLIB_NRF24_REG_CONFIG 0x00
#define RADIOLIB_NRF24_REG_EN_AA 0x01
#define RADIOLIB_NRF24_REG_EN_RXADDR 0x02
#define RADIOLIB_NRF24_REG_SETUP_AW 0x03
#define RADIOLIB_NRF24_REG_SETUP_RETR 0x04
#define RADIOLIB_NRF24_REG_RF_CH 0x05
#define RADIOLIB_NRF24_REG_RF_SETUP 0x06
#define RADIOLIB_NRF24_REG_STATUS 0x07
#define RADIOLIB_NRF24_REG_OBSERVE_TX 0x08
#define RADIOLIB_NRF24_REG_RPD 0x09
#define RADIOLIB_NRF24_REG_RX_ADDR_P0 0x0A
#define RADIOLIB_NRF24_REG_RX_ADDR_P1 0x0B
#define RADIOLIB_NRF24_REG_RX_ADDR_P2 0x0C
#define RADIOLIB_NRF24_REG_RX_ADDR_P3 0x0D
#define RADIOLIB_NRF24_REG_RX_ADDR_P4 0x0E
#define RADIOLIB_NRF24_REG_RX_ADDR_P5 0x0F
#define RADIOLIB_NRF24_REG_TX_ADDR 0x10
#define RADIOLIB_NRF24_REG_RX_PW_P0 0x11
#define RADIOLIB_NRF24_REG_RX_PW_P1 0x12
#define RADIOLIB_NRF24_REG_RX_PW_P2 0x13
#define RADIOLIB_NRF24_REG_RX_PW_P3 0x14
#define RADIOLIB_NRF24_REG_RX_PW_P4 0x15
#define RADIOLIB_NRF24_REG_RX_PW_P5 0x16
#define RADIOLIB_NRF24_REG_FIFO_STATUS 0x17
#define RADIOLIB_NRF24_REG_DYNPD 0x1C
#define RADIOLIB_NRF24_REG_FEATURE 0x1D
// NRF24_REG_CONFIG MSB LSB DESCRIPTION
#define RADIOLIB_NRF24_MASK_RX_DR_IRQ_OFF 0b01000000 // 6 6 RX_DR will not be reflected on IRQ pin
#define RADIOLIB_NRF24_MASK_RX_DR_IRQ_ON 0b00000000 // 6 6 RX_DR will be reflected on IRQ pin as active low (default)
#define RADIOLIB_NRF24_MASK_TX_DS_IRQ_OFF 0b00100000 // 5 5 TX_DS will not be reflected on IRQ pin
#define RADIOLIB_NRF24_MASK_TX_DS_IRQ_ON 0b00000000 // 5 5 TX_DS will be reflected on IRQ pin as active low (default)
#define RADIOLIB_NRF24_MASK_MAX_RT_IRQ_OFF 0b00010000 // 4 4 MAX_RT will not be reflected on IRQ pin
#define RADIOLIB_NRF24_MASK_MAX_RT_IRQ_ON 0b00000000 // 4 4 MAX_RT will be reflected on IRQ pin as active low (default)
#define RADIOLIB_NRF24_CRC_OFF 0b00000000 // 3 3 CRC calculation: disabled
#define RADIOLIB_NRF24_CRC_ON 0b00001000 // 3 3 enabled (default)
#define RADIOLIB_NRF24_CRC_8 0b00000000 // 2 2 CRC scheme: CRC8 (default)
#define RADIOLIB_NRF24_CRC_16 0b00000100 // 2 2 CRC16
#define RADIOLIB_NRF24_POWER_UP 0b00000010 // 1 1 power up
#define RADIOLIB_NRF24_POWER_DOWN 0b00000000 // 1 1 power down
#define RADIOLIB_NRF24_PTX 0b00000000 // 0 0 enable primary Tx
#define RADIOLIB_NRF24_PRX 0b00000001 // 0 0 enable primary Rx
// NRF24_REG_EN_AA
#define RADIOLIB_NRF24_AA_ALL_OFF 0b00000000 // 5 0 auto-ACK on all pipes: disabled
#define RADIOLIB_NRF24_AA_ALL_ON 0b00111111 // 5 0 enabled (default)
#define RADIOLIB_NRF24_AA_P5_OFF 0b00000000 // 5 5 auto-ACK on pipe 5: disabled
#define RADIOLIB_NRF24_AA_P5_ON 0b00100000 // 5 5 enabled (default)
#define RADIOLIB_NRF24_AA_P4_OFF 0b00000000 // 4 4 auto-ACK on pipe 4: disabled
#define RADIOLIB_NRF24_AA_P4_ON 0b00010000 // 4 4 enabled (default)
#define RADIOLIB_NRF24_AA_P3_OFF 0b00000000 // 3 3 auto-ACK on pipe 3: disabled
#define RADIOLIB_NRF24_AA_P3_ON 0b00001000 // 3 3 enabled (default)
#define RADIOLIB_NRF24_AA_P2_OFF 0b00000000 // 2 2 auto-ACK on pipe 2: disabled
#define RADIOLIB_NRF24_AA_P2_ON 0b00000100 // 2 2 enabled (default)
#define RADIOLIB_NRF24_AA_P1_OFF 0b00000000 // 1 1 auto-ACK on pipe 1: disabled
#define RADIOLIB_NRF24_AA_P1_ON 0b00000010 // 1 1 enabled (default)
#define RADIOLIB_NRF24_AA_P0_OFF 0b00000000 // 0 0 auto-ACK on pipe 0: disabled
#define RADIOLIB_NRF24_AA_P0_ON 0b00000001 // 0 0 enabled (default)
// NRF24_REG_EN_RXADDR
#define RADIOLIB_NRF24_P5_OFF 0b00000000 // 5 5 receive pipe 5: disabled (default)
#define RADIOLIB_NRF24_P5_ON 0b00100000 // 5 5 enabled
#define RADIOLIB_NRF24_P4_OFF 0b00000000 // 4 4 receive pipe 4: disabled (default)
#define RADIOLIB_NRF24_P4_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_NRF24_P3_OFF 0b00000000 // 3 3 receive pipe 3: disabled (default)
#define RADIOLIB_NRF24_P3_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_NRF24_P2_OFF 0b00000000 // 2 2 receive pipe 2: disabled (default)
#define RADIOLIB_NRF24_P2_ON 0b00000100 // 2 2 enabled
#define RADIOLIB_NRF24_P1_OFF 0b00000000 // 1 1 receive pipe 1: disabled
#define RADIOLIB_NRF24_P1_ON 0b00000010 // 1 1 enabled (default)
#define RADIOLIB_NRF24_P0_OFF 0b00000000 // 0 0 receive pipe 0: disabled
#define RADIOLIB_NRF24_P0_ON 0b00000001 // 0 0 enabled (default)
// NRF24_REG_SETUP_AW
#define RADIOLIB_NRF24_ADDRESS_2_BYTES 0b00000000 // 1 0 address width: 2 bytes
#define RADIOLIB_NRF24_ADDRESS_3_BYTES 0b00000001 // 1 0 3 bytes
#define RADIOLIB_NRF24_ADDRESS_4_BYTES 0b00000010 // 1 0 4 bytes
#define RADIOLIB_NRF24_ADDRESS_5_BYTES 0b00000011 // 1 0 5 bytes (default)
// NRF24_REG_SETUP_RETR
#define RADIOLIB_NRF24_ARD 0b00000000 // 7 4 auto retransmit delay: t[us] = (NRF24_ARD + 1) * 250 us
#define RADIOLIB_NRF24_ARC_OFF 0b00000000 // 3 0 auto retransmit count: auto retransmit disabled
#define RADIOLIB_NRF24_ARC 0b00000011 // 3 0 up to 3 retransmits on AA fail (default)
// NRF24_REG_RF_CH
#define RADIOLIB_NRF24_RF_CH 0b00000010 // 6 0 RF channel: f_CH[MHz] = 2400 MHz + NRF24_RF_CH
// NRF24_REG_RF_SETUP
#define RADIOLIB_NRF24_CONT_WAVE_OFF 0b00000000 // 7 7 continuous carrier transmit: disabled (default)
#define RADIOLIB_NRF24_CONT_WAVE_ON 0b10000000 // 7 7 enabled
#define RADIOLIB_NRF24_DR_250_KBPS 0b00100000 // 5 5 data rate: 250 kbps
#define RADIOLIB_NRF24_DR_1_MBPS 0b00000000 // 3 3 1 Mbps (default)
#define RADIOLIB_NRF24_DR_2_MBPS 0b00001000 // 3 3 2 Mbps
#define RADIOLIB_NRF24_PLL_LOCK_ON 0b00010000 // 4 4 force PLL lock: enabled
#define RADIOLIB_NRF24_PLL_LOCK_OFF 0b00000000 // 4 4 disabled (default)
#define RADIOLIB_NRF24_RF_PWR_18_DBM 0b00000000 // 2 1 output power: -18 dBm
#define RADIOLIB_NRF24_RF_PWR_12_DBM 0b00000010 // 2 1 -12 dBm
#define RADIOLIB_NRF24_RF_PWR_6_DBM 0b00000100 // 2 1 -6 dBm
#define RADIOLIB_NRF24_RF_PWR_0_DBM 0b00000110 // 2 1 0 dBm (default)
// NRF24_REG_STATUS
#define RADIOLIB_NRF24_RX_DR 0b01000000 // 6 6 Rx data ready
#define RADIOLIB_NRF24_TX_DS 0b00100000 // 5 5 Tx data sent
#define RADIOLIB_NRF24_MAX_RT 0b00010000 // 4 4 maximum number of retransmits reached (must be cleared to continue)
#define RADIOLIB_NRF24_RX_FIFO_EMPTY 0b00001110 // 3 1 Rx FIFO is empty
#define RADIOLIB_NRF24_RX_P_NO 0b00000000 // 3 1 number of data pipe that received data
#define RADIOLIB_NRF24_TX_FIFO_FULL 0b00000001 // 0 0 Tx FIFO is full
// NRF24_REG_OBSERVE_TX
#define RADIOLIB_NRF24_PLOS_CNT 0b00000000 // 7 4 number of lost packets
#define RADIOLIB_NRF24_ARC_CNT 0b00000000 // 3 0 number of retransmitted packets
// NRF24_REG_RPD
#define RADIOLIB_NRF24_RP_BELOW_64_DBM 0b00000000 // 0 0 received power in the current channel: less than -64 dBm
#define RADIOLIB_NRF24_RP_ABOVE_64_DBM 0b00000001 // 0 0 more than -64 dBm
// NRF24_REG_FIFO_STATUS
#define RADIOLIB_NRF24_TX_REUSE 0b01000000 // 6 6 reusing last transmitted payload
#define RADIOLIB_NRF24_TX_FIFO_FULL_FLAG 0b00100000 // 5 5 Tx FIFO is full
#define RADIOLIB_NRF24_TX_FIFO_EMPTY_FLAG 0b00010000 // 4 4 Tx FIFO is empty
#define RADIOLIB_NRF24_RX_FIFO_FULL_FLAG 0b00000010 // 1 1 Rx FIFO is full
#define RADIOLIB_NRF24_RX_FIFO_EMPTY_FLAG 0b00000001 // 0 0 Rx FIFO is empty
// NRF24_REG_DYNPD
#define RADIOLIB_NRF24_DPL_P5_OFF 0b00000000 // 5 5 dynamic payload length on pipe 5: disabled (default)
#define RADIOLIB_NRF24_DPL_P5_ON 0b00100000 // 5 5 enabled
#define RADIOLIB_NRF24_DPL_P4_OFF 0b00000000 // 4 4 dynamic payload length on pipe 4: disabled (default)
#define RADIOLIB_NRF24_DPL_P4_ON 0b00010000 // 4 4 enabled
#define RADIOLIB_NRF24_DPL_P3_OFF 0b00000000 // 3 3 dynamic payload length on pipe 3: disabled (default)
#define RADIOLIB_NRF24_DPL_P3_ON 0b00001000 // 3 3 enabled
#define RADIOLIB_NRF24_DPL_P2_OFF 0b00000000 // 2 2 dynamic payload length on pipe 2: disabled (default)
#define RADIOLIB_NRF24_DPL_P2_ON 0b00000100 // 2 2 enabled
#define RADIOLIB_NRF24_DPL_P1_OFF 0b00000000 // 1 1 dynamic payload length on pipe 1: disabled (default)
#define RADIOLIB_NRF24_DPL_P1_ON 0b00000010 // 1 1 enabled
#define RADIOLIB_NRF24_DPL_P0_OFF 0b00000000 // 0 0 dynamic payload length on pipe 0: disabled (default)
#define RADIOLIB_NRF24_DPL_P0_ON 0b00000001 // 0 0 enabled
#define RADIOLIB_NRF24_DPL_ALL_OFF 0b00000000 // 5 0 disable all dynamic payloads
#define RADIOLIB_NRF24_DPL_ALL_ON 0b00111111 // 5 0 enable all dynamic payloads
// NRF24_REG_FEATURE
#define RADIOLIB_NRF24_DPL_OFF 0b00000000 // 2 2 dynamic payload length: disabled (default)
#define RADIOLIB_NRF24_DPL_ON 0b00000100 // 2 2 enabled
#define RADIOLIB_NRF24_ACK_PAY_OFF 0b00000000 // 1 1 payload with ACK packets: disabled (default)
#define RADIOLIB_NRF24_ACK_PAY_ON 0b00000010 // 1 1 enabled
#define RADIOLIB_NRF24_DYN_ACK_OFF 0b00000000 // 0 0 payloads without ACK: disabled (default)
#define RADIOLIB_NRF24_DYN_ACK_ON 0b00000001 // 0 0 enabled
/*!
\class nRF24
\brief Control class for %nRF24 module.
*/
class nRF24: public PhysicalLayer {
public:
// introduce PhysicalLayer overloads
using PhysicalLayer::transmit;
using PhysicalLayer::receive;
using PhysicalLayer::startTransmit;
using PhysicalLayer::readData;
/*!
\brief Default constructor.
\param mod Instance of Module that will be used to communicate with the radio.
*/
nRF24(Module* mod);
Module* getMod();
// basic methods
/*!
\brief Initialization method.
\param freq Carrier frequency in MHz. Defaults to 2400 MHz.
\param dataRate Data rate to be used in kbps. Defaults to 1000 kbps.
\param power Output power in dBm. Defaults to -12 dBm.
\param addrWidth Address width in bytes. Defaults to 5 bytes.
\returns \ref status_codes
*/
int16_t begin(int16_t freq = 2400, int16_t dataRate = 1000, int8_t power = -12, uint8_t addrWidth = 5);
/*!
\brief Sets the module to sleep mode.
\returns \ref status_codes
*/
int16_t sleep();
/*!
\brief Sets the module to standby mode.
\returns \ref status_codes
*/
int16_t standby() override;
/*!
\brief Blocking binary transmit method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\param addr Dummy address parameter, to ensure PhysicalLayer compatibility.
\returns \ref status_codes
*/
int16_t transmit(uint8_t* data, size_t len, uint8_t addr) override;
/*!
\brief Blocking binary receive method.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\returns \ref status_codes
*/
int16_t receive(uint8_t* data, size_t len) override;
/*!
\brief Starts direct mode transmission.
\param frf Raw RF frequency value. Defaults to 0, required for quick frequency shifts in RTTY.
\returns \ref status_codes
*/
int16_t transmitDirect(uint32_t frf = 0) override;
/*!
\brief Dummy direct mode reception method, to ensure PhysicalLayer compatibility.
\returns \ref status_codes
*/
int16_t receiveDirect() override;
// interrupt methods
/*!
\brief Sets interrupt service routine to call when IRQ activates.
\param func ISR to call.
*/
void setIrqAction(void (*func)(void));
/*!
\brief Interrupt-driven binary transmit method. IRQ will be activated when full packet is transmitted.
Overloads for string-based transmissions are implemented in PhysicalLayer.
\param data Binary data to be sent.
\param len Number of bytes to send.
\param addr Dummy address parameter, to ensure PhysicalLayer compatibility.
\returns \ref status_codes
*/
int16_t startTransmit(uint8_t* data, size_t len, uint8_t addr) override;
/*!
\brief Interrupt-driven receive method. IRQ will be activated when full packet is received.
\returns \ref status_codes
*/
int16_t startReceive();
/*!
\brief Reads data received after calling startReceive method.
\param data Pointer to array to save the received binary data.
\param len Number of bytes that will be received. Must be known in advance for binary transmissions.
\returns \ref status_codes
*/
int16_t readData(uint8_t* data, size_t len) override;
// configuration methods
/*!
\brief Sets carrier frequency. Allowed values range from 2400 MHz to 2525 MHz.
\param freq Carrier frequency to be set in MHz.
\returns \ref status_codes
*/
int16_t setFrequency(int16_t freq);
/*!
\brief Sets data rate. Allowed values are 2000, 1000 or 250 kbps.
\param dataRate Data rate to be set in kbps.
\returns \ref status_codes
*/
int16_t setDataRate(int16_t dataRate);
/*!
\brief Sets output power. Allowed values are -18, -12, -6 or 0 dBm.
\param power Output power to be set in dBm.
\returns \ref status_codes
*/
int16_t setOutputPower(int8_t power);
/*!
\brief Sets address width of transmit and receive pipes in bytes. Allowed values are 3, 4 or 5 bytes.
\param addrWidth Address width to be set in bytes.
\returns \ref status_codes
*/
int16_t setAddressWidth(uint8_t addrWidth);
/*!
\brief Sets address of transmit pipe. The address width must be the same as the same as the configured in setAddressWidth.
\param addr Address to which the next packet shall be transmitted.
\returns \ref status_codes
*/
int16_t setTransmitPipe(uint8_t* addr);
/*!
\brief Sets address of receive pipes 0 or 1. The address width must be the same as the same as the configured in setAddressWidth.
\param pipeNum Number of pipe to which the address shall be set. Either 0 or 1, other pipes are handled using overloaded method.
\param addr Address from which %nRF24 shall receive new packets on the specified pipe.
\returns \ref status_codes
*/
int16_t setReceivePipe(uint8_t pipeNum, uint8_t* addr);
/*!
\brief Sets address of receive pipes 2 - 5. The first 2 - 4 address bytes for these pipes are the same as for address pipe 1, only the last byte can be set.
\param pipeNum Number of pipe to which the address shall be set. Allowed values range from 2 to 5.
\param addrByte LSB of address from which %nRF24 shall receive new packets on the specified pipe.
\returns \ref status_codes
*/
int16_t setReceivePipe(uint8_t pipeNum, uint8_t addrByte);
/*!
\brief Disables specified receive pipe.
\param pipeNum Receive pipe to be disabled.
\returns \ref status_codes
*/
int16_t disablePipe(uint8_t pipeNum);
/*!
\brief Gets nRF24 status register.
\param mask Bit mask to be used on the returned register value.
\returns Status register value or \ref status_codes
*/
int16_t getStatus(uint8_t mask = 0xFF);
/*!
\brief Checks if carrier was detected during last RX
\returns Whatever the carrier was above threshold.
*/
bool isCarrierDetected();
/*!
\brief Dummy configuration method, to ensure PhysicalLayer compatibility.
\param freqDev Dummy frequency deviation parameter, no configuration will be changed.
\returns \ref status_codes
*/
int16_t setFrequencyDeviation(float freqDev) override;
/*!
\brief Query modem for the packet length of received payload.
\param update Update received packet length. Will return cached value when set to false.
\returns Length of last received packet in bytes.
*/
size_t getPacketLength(bool update = true) override;
/*!
\brief Enable CRC filtering and generation.
\param crcOn Set or unset CRC check.
\returns \ref status_codes
*/
int16_t setCrcFiltering(bool crcOn = true);
/*!
\brief Enable or disable auto-acknowledge packets on all pipes
\param autoAckOn Enable (true) or disable (false) auto-acks.
\returns \ref status_codes
*/
int16_t setAutoAck(bool autoAckOn = true);
/*!
\brief Enable or disable auto-acknowledge packets on given pipe.
\param pipeNum Number of pipe to which enable / disable auto-acks.
\param autoAckOn Enable (true) or disable (false) auto-acks.
\returns \ref status_codes
*/
int16_t setAutoAck(uint8_t pipeNum, bool autoAckOn);
/*!
\brief Dummy data shaping configuration method, to ensure PhysicalLayer compatibility.
\param sh Ignored.
\returns \ref status_codes
*/
int16_t setDataShaping(uint8_t sh) override;
/*!
\brief Dummy encoding configuration method, to ensure PhysicalLayer compatibility.
\param sh Ignored.
\returns \ref status_codes
*/
int16_t setEncoding(uint8_t encoding) override;
/*!
\brief Dummy random method, to ensure PhysicalLayer compatibility.
\returns Always returns 0.
*/
uint8_t randomByte();
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
/*!
\brief Dummy method, to ensure PhysicalLayer compatibility.
\param func Ignored.
*/
void setDirectAction(void (*func)(void));
/*!
\brief Dummy method, to ensure PhysicalLayer compatibility.
\param pin Ignored.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
#endif
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)
protected:
#endif
Module* _mod;
void SPIreadRxPayload(uint8_t* data, uint8_t numBytes);
void SPIwriteTxPayload(uint8_t* data, uint8_t numBytes);
void SPItransfer(uint8_t cmd, bool write = false, uint8_t* dataOut = NULL, uint8_t* dataIn = NULL, uint8_t numBytes = 0);
#if !defined(RADIOLIB_GODMODE)
protected:
#endif
uint8_t _addrWidth = 0;
int16_t config();
void clearIRQ();
};
#endif