Loading...
Searching...
No Matches
sx1272.h
Go to the documentation of this file.
1
6
7// ALLOW FORMATTING
8#ifndef _LORA_H
9#define _LORA_H
10
11#include "lora.h"
12
13#include "spi.h"
14#include "gpiopin.h"
15
16#define SX1272_REG_END 0x46
17
18#define SX1272_REG_FIFO 0x00
19#define SX1272_REG_FIFO_ADDR_PTR 0x0D
20#define SX1272_REG_FIFO_TX_BASE_ADDR 0x0E
21#define SX1272_REG_FIFO_RX_BASE_ADDR 0x0F
22#define SX1272_REG_FIFO_RX_CURR_ADDR 0x10
23
24#define SX1272_REG_RX_BYTES 0x13
25
26#define SX1272_REG_DIO_MAPPING1 0x40
27#define SX1272_DIO_MAPPING_DIO0_Pos 0x06
28#define SX1272_DIO_MAPPING_DIO1_Pos 0x04
29#define SX1272_DIO_MAPPING_DIO2_Pos 0x02
30#define SX1272_DIO_MAPPING_DIO3_Pos 0x00
31#define SX1272_DIO_MAPPING_DIO4_Pos 0x06
32#define SX1272_DIO_MAPPING_DIO5_Pos 0x04
33
34#define SX1272_LORA_DIO_RXDONE 0x00 << SX1272_DIO_MAPPING_DIO0_Pos
35#define SX1272_LORA_DIO_TXDONE 0x01 << SX1272_DIO_MAPPING_DIO0_Pos
36
37#define SX1272_REG_IRQ_FLAGS_MASK 0x11
38#define SX1272_REG_IRQ_FLAGS 0x12
39#define SX1272_LORA_IRQ_RXDONE 0x40
40#define SX1272_LORA_IRQ_TXDONE 0x08
41
42#define SX1272_REG_OP_MODE 0x01
43#define SX1272_OP_MODE_LONG_RANGE 0x80
44#define SX1272_OP_MODE_MODE_Pos 0x00
45
46#define SX1272_REG_OCP 0x0B
47#define SX1272_OCP_ON 0x20
48#define SX1272_OCP_TRIM 0x1F
49
50#define SX1272_REG_FR_MSB 0x06
51#define SX1272_REG_FR_MIB 0x07
52#define SX1272_REG_FR_LSB 0x08
53
54#define SX1272_REG_MODEM_CONFIG1 0x1D
55#define SX1272_MODEM_CONFIG1_CRC 0x02
56#define SX1272_MODEM_CONFIG1_IMPLICIT_HEADER 0x04
57#define SX1272_MODEM_CONFIG1_BW_Pos 0x06
58#define SX1272_MODEM_CONFIG1_CR_Pos 0x03
59
60#define SX1272_REG_MODEM_CONFIG2 0x1E
61#define SX1272_MODEM_CONFIG2_AGC_AUTO_ON 0x04
62#define SX1272_MODEM_CONFIG2_SF_Pos 0x04
63
64#define SX1272_REG_PA_CONFIG 0x09
65#define SX1272_PA_SELECT 0x80
66
67#define SX1272_REG_LNA 0x0C
68
69#define SX1272_REG_PAYLOAD_LENGTH 0x22
70#define SX1272_REG_MAX_PAYLOAD_LENGTH 0x23
71
72#define SX1272_REG_PA_DAC 0x5A
73
74// Macro definitions for device config literals
75//
76// clang-format off
77
78#define SX1272_CONFIG_DEFAULT \
79 (SX1272_Config) { \
80 .freq = 915.0f, \
81 .bw = SX1272_BW500, \
82 .sf = SX1272_SF9, \
83 .cr = SX1272_CR5, \
84 .implicitHeader = false, \
85 .crc = false, \
86 .paSelect = true, \
87 .outputPower = 0x1F, \
88 .ocp = true, \
89 .ocpTrim = 0x1B, \
90 .rxFifoBaseAddr = 0x00, \
91 .txFifoBaseAddr = 0x00, \
92 .maxPayloadLength = 0x20 \
93 }
94// clang-format on
95
102
107typedef enum {
108 SX1272_BW125, // 125kHz
109 SX1272_BW250, // 250kHz
110 SX1272_BW500, // 500kHz
112
118typedef enum {
119 SX1272_CR5 = 1, // 4/5
120 SX1272_CR6, // 4/6
121 SX1272_CR7, // 4/7
122 SX1272_CR8, // 4/8
124
130typedef enum {
131 SX1272_SF6 = 6,
132 SX1272_SF7,
133 SX1272_SF8,
134 SX1272_SF9,
135 SX1272_SF10,
136 SX1272_SF11,
137 SX1272_SF12,
139
144typedef enum {
145 SX1272_MODE_SLEEP, // Low power mode. Only SPI and config registers available
146 SX1272_MODE_STDBY, // Standby mode. Chip is active, RF is disabled
147 SX1272_MODE_FSTX, // Frequency synthesis transmission mode
148 SX1272_MODE_TX, // Transmission mode
149 SX1272_MODE_FSRX, // Frequency synthesis receive mode
150 SX1272_MODE_RXCONTINUOUS, // Continuous receive mode
151 SX1272_MODE_RXSINGLE, // Single receive mode
152 SX1272_MODE_CAD // Channel activity detection mode
154
159typedef struct {
160 float freq;
161
162 // LoRa modem configuration
163 // clang-format off
168 bool crc;
169
170 // LoRa power amplifier configuration
171 bool paSelect;
172 uint8_t outputPower;
173
174 // LoRa over current protection configuration
175 bool ocp;
176 uint8_t ocpTrim;
177
178 // FIFO configuration
182 // clang-format on
184
189typedef struct SX1272 {
195 void (*standby)(struct SX1272 *);
196 bool (*updateConfig)(struct SX1272 *, SX1272_Config *);
197} SX1272_t;
198
199bool SX1272_init(SX1272_t *lora, SPI_t *spi, GPIOpin_t cs, SX1272_Config *config);
200bool SX1272_updateConfig(SX1272_t *lora, SX1272_Config *config);
201
202void SX1272_transmit(LoRa_t *lora, uint8_t *data, uint8_t length);
203void SX1272_startReceive(LoRa_t *lora);
204uint8_t SX1272_readReceive(LoRa_t *lora, uint8_t *data, uint8_t byffSize);
205
207void SX1272_clearIRQ(LoRa_t *, uint8_t);
208
209void _SX1272_setMode(SX1272_t *, SX1272_Mode);
210
211uint8_t SX1272_readRegister(SX1272_t *, uint8_t);
212
214#endif
Definition lora.h:30
Struct definition for a GPIO pin.
Definition gpiopin.h:151
SX1272_CodingRate cr
SX1272 LoRa modem coding rate.
Definition sx1272.h:166
uint8_t ocpTrim
SX1272 LoRa modem overcurrent protection trim.
Definition sx1272.h:176
SX1272_Config config
Configuration parameters for the SX1272 driver.
Definition sx1272.h:191
void(* standby)(struct SX1272 *)
SX1272 standby method.
Definition sx1272.h:195
float freq
SX1272 LoRa carrier frequency.
Definition sx1272.h:160
SX1272_SpreadingFactor sf
SX1272 LoRa modem spreading factor.
Definition sx1272.h:164
GPIOpin_t cs
Chip select GPIO.
Definition sx1272.h:193
LoRa_t base
Base LoRa API.
Definition sx1272.h:190
uint8_t rxFifoBaseAddr
Base address for RX fifo.
Definition sx1272.h:179
bool ocp
SX1272 LoRa modem overcurrent protection enable.
Definition sx1272.h:175
bool(* updateConfig)(struct SX1272 *, SX1272_Config *)
SX1272 configuration update method.
Definition sx1272.h:196
bool paSelect
SX1272 LoRa modem power amp output select.
Definition sx1272.h:171
uint8_t outputPower
SX1272 LoRa modem power amp output power.
Definition sx1272.h:172
bool implicitHeader
SX1272 LoRa modem implicit header enable.
Definition sx1272.h:167
uint8_t txFifoBaseAddr
Base address for TX fifo.
Definition sx1272.h:180
SX1272_Mode currentMode
Current operating mode.
Definition sx1272.h:194
SX1272_Bandwidth bw
SX1272 LoRa modem bandwidth.
Definition sx1272.h:165
SPI_t * spi
Parent SPI interface.
Definition sx1272.h:192
bool crc
SX1272 LoRa modem CRC enable.
Definition sx1272.h:168
uint8_t maxPayloadLength
Maximum allowed length of payload.
Definition sx1272.h:181
Struct definition for SPI interface. Provides the interface for API consumers to interact with the SP...
Definition spi.h:134
SX1272_Bandwidth
SX1272 bandwidth enum.
Definition sx1272.h:107
void SX1272_startReceive(LoRa_t *lora)
Begins continuous receive on the SX1272.
Definition sx1272.c:223
uint8_t SX1272_readReceive(LoRa_t *lora, uint8_t *data, uint8_t byffSize)
Reads contents of received packet to local buffer from the SX1272.
Definition sx1272.c:264
void SX1272_transmit(LoRa_t *lora, uint8_t *data, uint8_t length)
Transmits data using the SX1272.
Definition sx1272.c:179
SX1272_SpreadingFactor
SX1272 spreading factor enum.
Definition sx1272.h:130
bool SX1272_updateConfig(SX1272_t *lora, SX1272_Config *config)
Update SX1272 configuration.
Definition sx1272.c:319
SX1272_Mode
SX1272 operating mode enum.
Definition sx1272.h:144
void SX1272_standby(SX1272_t *)
Sets the operational mode of the LoRa module to standby.
Definition sx1272.c:167
SX1272_CodingRate
SX1272 coding rate enum.
Definition sx1272.h:118
bool SX1272_init(SX1272_t *lora, SPI_t *spi, GPIOpin_t cs, SX1272_Config *config)
Initializes the LoRa module with specified configuration parameters.
Definition sx1272.c:30
void SX1272_clearIRQ(LoRa_t *, uint8_t)
Sets the value of RegIrqFlags in the SX1272 to the provided argument value. Writing a 1 to a bit in t...
Definition sx1272.c:305
SX1272 LoRa configuration struct.
Definition sx1272.h:159
Struct definition for SX1272. Provides the interface for API consumers to interact with the SX1272 Lo...
Definition sx1272.h:189
Defines the API for LoRa communication.