esp32-lora/include/esp32-lora.h

212 lines
5.7 KiB
C
Raw Normal View History

2018-07-28 14:33:15 -04:00
#ifndef _LORA32_H__
#define _LORA32_H__
#include "driver/spi_common.h"
#include "driver/spi_master.h"
#include "freertos/event_groups.h"
2018-07-28 14:33:15 -04:00
#define REG_FIFO 0x00
#define REG_OP_MODE 0x01
#define REG_BR_MSB 0x02
#define REG_BR_LSB 0x03
#define REG_FD_MSB 0x04
#define REG_FD_LSB 0x05
#define REG_FRF_MSB 0x06
#define REG_FRF_MID 0x07
#define REG_FRF_LSB 0x08
#define REG_PA_CONFIG 0x09
#define REG_LNA 0x0c
#define REG_FIFO_ADDR_PTR 0x0d
#define REG_FIFO_TX_BASE_ADDR 0x0e
#define REG_FIFO_RX_BASE_ADDR 0x0f
#define REG_FIFO_RX_CURRENT_ADDR 0x10
#define REG_IRQ_FLAGS 0x12
#define REG_RX_NB_BYTES 0x13
2023-09-12 17:28:48 -04:00
#define REG_MODEM_STATUS 0x18
2018-07-28 14:33:15 -04:00
#define REG_PKT_RSSI_VALUE 0x1a
2021-12-10 01:13:50 -05:00
#define REG_RSSI_VALUE 0x1b
2018-07-28 14:33:15 -04:00
#define REG_MODEM_CONFIG_1 0x1d
#define REG_MODEM_CONFIG_2 0x1e
#define REG_PREAMBLE_MSB 0x20
#define REG_PREAMBLE_LSB 0x21
#define REG_PAYLOAD_LENGTH 0x22
2021-12-02 12:59:44 -05:00
#define REG_HOP_PERIOD 0x24
2018-07-28 14:33:15 -04:00
#define REG_MODEM_CONFIG_3 0x26
#define REG_RSSI_WIDEBAND 0x2c
#define REG_DETECTION_OPTIMIZE 0x31
#define REG_DETECTION_THRESHOLD 0x37
#define REG_SYNC_WORD 0x39
#define REG_DIO_MAPPING_1 0x40
2023-09-12 17:28:48 -04:00
#define REG_DIO_MAPPING_2 0x41
2018-07-28 14:33:15 -04:00
#define REG_VERSION 0x42
2020-04-16 23:57:50 -04:00
#define REG_PA_DAC 0x4D
2018-07-28 14:33:15 -04:00
// modes
#define MODE_SLEEP 0x00
#define MODE_STANDBY 0x01
#define MODE_TX 0x03
#define MODE_RX_CONTINUOUS 0x05
#define MODE_RX_SINGLE 0x06
2020-02-22 11:33:34 -05:00
#define MODE_CAD_DETECT 0x07
#define MODE_LONG_RANGE_MODE 0x80
2018-07-28 14:33:15 -04:00
// PA config
#define PA_BOOST 0x80
// IRQ masks
2020-02-22 10:05:42 -05:00
#define IRQ_RX_TIMEOUT 0x80
#define IRQ_RX_DONE 0x40
#define IRQ_PAYLOAD_CRC_ERROR 0x20
#define IRQ_VALID_HEADER 0x10
#define IRQ_TX_DONE 0x08
#define IRQ_CAD_DONE 0x04
#define IRQ_FHSS_CHANGE 0x02
#define IRQ_CAD_DETECTED 0x01
2018-07-28 14:33:15 -04:00
#define MAX_PKT_LENGTH 255
2018-07-28 14:33:15 -04:00
#define DETECT_OPT_SF6 0xC5
#define DETECT_THRES_SF6 0x0C
#define DETECT_OPT_OTHER 0xC3
#define DETECT_THRES_OTHER 0x0A
2018-07-28 14:33:15 -04:00
#define DEFAULT_SF 7
#define DEFAULT_PREAMBLE 8
#define DEFAULT_CR 5
2023-09-12 17:28:48 -04:00
#define DIO0_MODE_SHIFT 6
#define DIO0_MODE_RXDONE 0b00
#define DIO0_MODE_TXDONE 0b01
#define DIO0_MODE_CADDON 0b10
2020-02-21 11:55:23 -05:00
2023-09-12 17:28:48 -04:00
#define DIO1_MODE_SHIFT 4
#define DIO1_MODE_RXTIME 0b00
#define DIO1_MODE_FHSSCC 0b01
#define DIO1_MODE_CADDET 0b10
#define DIO3_MODE_SHIFT 2
#define DIO3_MODE_CADDON 0b00
#define DIO3_MODE_HEADVA 0b01
#define DIO3_MODE_CRCERR 0b10
#define DIO4_MODE_CADDET 0b00
#define DIO4_MODE_PLLLOC 0b10
#define DIO4_MODE_SHIFT 6
#define DIO5_MODE_SHIFT 4
#define ERR_LOR_VERSION_MISMATCH (01)
2021-11-17 16:49:32 -05:00
#define LORA32_DEFAULT_CONFIG {\
2023-09-12 17:28:48 -04:00
.bandwidth = 7,\
.codingRate = 5,\
.frequency = 915000000,\
.spreadingFactor = 11,\
.preamble = DEFAULT_PREAMBLE,\
.implicitHeader = false,\
.useCRC = false,\
.fifoIdx = 0,\
2023-09-12 17:28:48 -04:00
.copi = 23,\
.cipo = 19,\
.clk = 18,\
.dio0 = -1,\
.dio1 = -1,\
.dio2 = -1,\
.nss = -1,\
.reset = -1,\
.spi_host = 1,\
}
2018-07-28 14:33:15 -04:00
enum freq {
F433, F866, F915
} lora32_freq;
2020-03-01 10:41:11 -05:00
typedef enum {
B78, B104, B156, B208, B3125, B417, B625, B125, B250, B500
2020-03-01 10:41:11 -05:00
} bandwidth;
2018-07-28 14:33:15 -04:00
2023-09-12 17:28:48 -04:00
extern const long long frequencies[3];
extern const long bandwidths[10];
2021-07-23 00:32:06 -04:00
typedef struct lora32_cfg_t lora32_cfg_t;
typedef void (*receiveCallback)(lora32_cfg_t *lora, uint8_t size);
typedef void (*txdoneCallback)(lora32_cfg_t *lora);
typedef void (*cadDoneCallback)(lora32_cfg_t *lora, bool detected);
typedef void (*cadDetectedCallback)(lora32_cfg_t *lora);
typedef struct {
EventGroupHandle_t events;
} lora32_handle_t;
2018-07-28 14:33:15 -04:00
2023-09-12 17:28:48 -04:00
typedef struct lora32_modem_status_t {
unsigned int detected:1;
unsigned int synced:1;
unsigned int rx:1;
unsigned int valid:1;
unsigned int clear:1;
unsigned int cr:3;
} lora32_modem_status_t;
2018-07-28 14:33:15 -04:00
typedef struct lora32_cfg_t {
uint8_t fifoIdx;
uint8_t channel;
uint8_t bandwidth;
2018-07-28 14:33:15 -04:00
uint8_t spreadingFactor;
uint8_t codingRate;
2023-09-12 17:28:48 -04:00
uint8_t version; // to be written on init by the driver
int8_t nss;
int8_t cipo;
int8_t copi;
int8_t clk;
int8_t reset;
int8_t dio0;
int8_t dio1;
int8_t dio2;
int8_t channels[64];
long frequency;
2018-07-28 14:33:15 -04:00
uint16_t preamble;
bool useCRC;
bool implicitHeader;
bool enableFHSS;
2018-07-28 14:33:15 -04:00
receiveCallback receive;
2020-02-22 10:54:14 -05:00
txdoneCallback tx_done;
2020-02-22 11:33:50 -05:00
cadDoneCallback cad_done;
cadDetectedCallback cad_detected;
2021-07-23 00:32:06 -04:00
uint8_t spi_host;
2018-07-28 14:33:15 -04:00
spi_device_handle_t spi;
lora32_handle_t handle;
2018-07-28 14:33:15 -04:00
} lora32_cfg_t;
2021-07-23 00:32:06 -04:00
uint8_t lora32_spi_init(lora32_cfg_t *config);
2018-07-28 14:33:15 -04:00
uint8_t lora32_init(lora32_cfg_t *config);
uint8_t lora32_data_available(lora32_cfg_t *lora);
2021-12-10 01:13:50 -05:00
int16_t lora32_get_packet_rssi(lora32_cfg_t *lora);
int16_t lora32_get_rssi(lora32_cfg_t *lora);
2020-02-26 23:31:20 -05:00
double lora32_calc_datarate(lora32_cfg_t *lora);
2018-07-28 14:33:15 -04:00
void lora32_dump_regs(lora32_cfg_t *lora);
void lora32_enable_fhss(lora32_cfg_t *lora, uint8_t period);
2020-02-21 01:16:06 -05:00
void lora32_enable_continuous_rx(lora32_cfg_t *lora);
void lora32_enable_single_rx(lora32_cfg_t *lora);
2020-02-22 11:33:50 -05:00
void lora32_enable_cad(lora32_cfg_t *lora);
void lora32_disable_fhss(lora32_cfg_t *lora);
2021-11-17 16:49:32 -05:00
void lora32_toggle_reset(lora32_cfg_t *lora);
2020-02-26 23:31:20 -05:00
void lora32_send(lora32_cfg_t *config, uint8_t *data, uint8_t len);
2023-09-12 17:28:48 -04:00
void lora32_set_bandwidth(lora32_cfg_t *lora, bandwidth bw);
void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t cr);
2020-02-26 23:31:20 -05:00
void lora32_set_spreadfactor(lora32_cfg_t *lora, uint8_t factor);
uint8_t lora32_get_spreadfactor(lora32_cfg_t *lora);
void lora32_read_data(lora32_cfg_t *lora, uint8_t *data);
void lora32_sleep(lora32_cfg_t *lora);
void lora32_standby(lora32_cfg_t *lora);
2018-07-28 14:33:15 -04:00
#endif // _LORA32_H__