Loading...
Searching...
No Matches
sx1272.h
1
6
7// ALLOW FORMATTING
8#ifndef _LORA_H
9#define _LORA_H
10
11#include "spi.h"
12#include "gpiopin.h"
13
14#define SX1272_REG_FIFO 0x00
15#define SX1272_REG_FIFO_ADDR_PTR 0x0D
16#define SX1272_REG_FIFO_TX_BASE_ADDR 0x0E
17#define SX1272_REG_FIFO_RX_BASE_ADDR 0x0F
18#define SX1272_REG_FIFO_RX_CURR_ADDR 0x10
19
20#define SX1272_REG_RX_BYTES 0x13
21
22#define SX1272_REG_DIO_MAPPING1 0x40
23#define SX1272_DIO_MAPPING_DIO0_Pos 0x06
24#define SX1272_DIO_MAPPING_DIO1_Pos 0x04
25#define SX1272_DIO_MAPPING_DIO2_Pos 0x02
26#define SX1272_DIO_MAPPING_DIO3_Pos 0x00
27#define SX1272_DIO_MAPPING_DIO4_Pos 0x06
28#define SX1272_DIO_MAPPING_DIO5_Pos 0x04
29
30#define SX1272_LORA_DIO_RXDONE 0x00 << SX1272_DIO_MAPPING_DIO0_Pos
31#define SX1272_LORA_DIO_TXDONE 0x01 << SX1272_DIO_MAPPING_DIO0_Pos
32
33#define SX1272_REG_IRQ_FLAGS_MASK 0x11
34#define SX1272_REG_IRQ_FLAGS 0x12
35#define SX1272_LORA_IRQ_RXDONE 0x40
36#define SX1272_LORA_IRQ_TXDONE 0x08
37
38#define SX1272_REG_OP_MODE 0x01
39#define SX1272_OP_MODE_LONG_RANGE_Pos 0x07
40#define SX1272_OP_MODE_MODE_Pos 0x00
41
42#define SX1272_REG_MODEM_CONFIG1 0x1D
43#define SX1272_REG_MODEM_CONFIG1_BW_Pos 0x06
44#define SX1272_REG_MODEM_CONFIG1_CR_Pos 0x03
45#define SX1272_REG_MODEM_CONFIG1_CRC_Pos 0x01
46
47#define SX1272_REG_MODEM_CONFIG2 0x1E
48#define SX1272_REG_MODEM_CONFIG2_SF_Pos 0x04
49
50#define SX1272_REG_PA_CONFIG 0x09
51#define SX1272_PA_SELECT 0x80
52
53#define SX1272_REG_LNA 0x0C
54
55#define SX1272_REG_PAYLOAD_LENGTH 0x22
56#define SX1272_REG_MAX_PAYLOAD_LENGTH 0x23
57
58// TODO:
59// Extract these out of the device driver
60// in favour of passing as argument
61
62#define LORA_MSG_LENGTH 0x20
63#define LORA_MSG_PAYLOAD_LENGTH (LORA_MSG_LENGTH - 1)
64
71
76typedef enum {
77 SX1272_BW125, // 125kHz
78 SX1272_BW250, // 250kHz
79 SX1272_BW500, // 500kHz
81
87typedef enum {
88 SX1272_CR5 = 1, // 4/5
89 SX1272_CR6, // 4/6
90 SX1272_CR7, // 4/7
91 SX1272_CR8, // 4/8
93
99typedef enum {
100 SX1272_SF6 = 6,
101 SX1272_SF7,
102 SX1272_SF8,
103 SX1272_SF9,
104 SX1272_SF10,
105 SX1272_SF11,
106 SX1272_SF12,
108
113typedef enum {
114 SX1272_MODE_SLEEP, // Low power mode. Only SPI and config registers available
115 SX1272_MODE_STDBY, // Standby mode. Chip is active, RF is disabled
116 SX1272_MODE_FSTX, // Frequency synthesis transmission mode
117 SX1272_MODE_TX, // Transmission mode
118 SX1272_MODE_FSRX, // Frequency synthesis receive mode
119 SX1272_MODE_RXCONTINUOUS, // Continuous receive mode
120 SX1272_MODE_RXSINGLE, // Single receive mode
121 SX1272_MODE_CAD // Channel activity detection mode
123
124typedef struct {
125 uint8_t id;
126 uint8_t data[LORA_MSG_PAYLOAD_LENGTH];
128
133typedef struct SX1272 {
137 void (*enableBoost)(struct SX1272 *, bool);
138 void (*standby)(struct SX1272 *);
139 void (*transmit)(struct SX1272 *, uint8_t *);
140 void (*startReceive)(struct SX1272 *);
141 bool (*readReceive)(struct SX1272 *, uint8_t *, uint8_t);
142 void (*clearIRQ)(struct SX1272 *, uint8_t);
143} SX1272_t;
144
146
147void SX1272_enableBoost(SX1272_t *, bool);
149void SX1272_transmit(SX1272_t *, uint8_t *);
151bool SX1272_readReceive(SX1272_t *, uint8_t *, uint8_t);
152void SX1272_clearIRQ(SX1272_t *, uint8_t);
153
155
156void SX1272_writeRegister(SX1272_t *, uint8_t, uint8_t);
157uint8_t SX1272_readRegister(SX1272_t *, uint8_t);
158
160#endif
Struct definition for a GPIO pin.
Definition gpiopin.h:151
Struct definition for SPI interface. Provides the interface for API consumers to interact with the SP...
Definition spi.h:134
uint8_t id
Packet header ID.
Definition sx1272.h:125
void(* enableBoost)(struct SX1272 *, bool)
Power amp boost toggle method.
Definition sx1272.h:137
void(* standby)(struct SX1272 *)
SX1272 standby method.
Definition sx1272.h:138
uint8_t data[LORA_MSG_PAYLOAD_LENGTH]
Packet payload.
Definition sx1272.h:126
void(* startReceive)(struct SX1272 *)
SX1272 LoRa continuous receive method.
Definition sx1272.h:140
bool(* readReceive)(struct SX1272 *, uint8_t *, uint8_t)
SX1272 LoRa receive buffer read method.
Definition sx1272.h:141
GPIOpin_t cs
Chip select GPIO.
Definition sx1272.h:135
void(* transmit)(struct SX1272 *, uint8_t *)
SX1272 LoRa transmit method.
Definition sx1272.h:139
SPI_t * base
Parent SPI interface.
Definition sx1272.h:134
void(* clearIRQ)(struct SX1272 *, uint8_t)
SX1272 LoRa IRQ flag clear method.
Definition sx1272.h:142
SX1272_Mode currentMode
Current operating mode.
Definition sx1272.h:136
void SX1272_transmit(SX1272_t *, uint8_t *)
Transmits data using the SX1272.
Definition sx1272.c:141
SX1272_Bandwidth
SX1272 bandwidth enum.
Definition sx1272.h:76
void _SX1272_setMode(SX1272_t *, SX1272_Mode)
Sets the operational mode of the LoRa module.
Definition sx1272.c:93
bool SX1272_readReceive(SX1272_t *, uint8_t *, uint8_t)
Reads contents of received packet to local buffer from the SX1272.
Definition sx1272.c:221
SX1272_SpreadingFactor
SX1272 spreading factor enum.
Definition sx1272.h:99
SX1272_Mode
SX1272 operating mode enum.
Definition sx1272.h:113
void SX1272_standby(SX1272_t *)
Sets the operational mode of the LoRa module to standby.
Definition sx1272.c:129
SX1272_t SX1272_init(SX1272_t *, SPI_t *, GPIOpin_t, SX1272_Bandwidth, SX1272_SpreadingFactor, SX1272_CodingRate)
Initializes the LoRa module with specified configuration parameters.
Definition sx1272.c:27
SX1272_CodingRate
SX1272 coding rate enum.
Definition sx1272.h:87
void SX1272_startReceive(SX1272_t *)
Begins continuous receive on the SX1272.
Definition sx1272.c:181
void SX1272_enableBoost(SX1272_t *, bool)
Enables/disables power amplifier boost.
Definition sx1272.c:114
void SX1272_clearIRQ(SX1272_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:261
Struct definition for SX1272. Provides the interface for API consumers to interact with the SX1272 Lo...
Definition sx1272.h:133