2018-07-28 14:33:15 -04:00
|
|
|
# ESP32 LoRa (SX127*)
|
|
|
|
|
|
|
|
Provides SPI driver for SX1276/SX1278 LoRa radio
|
|
|
|
|
2018-07-29 02:33:49 -04:00
|
|
|
## Install
|
2018-07-28 14:33:15 -04:00
|
|
|
|
2020-02-23 14:47:08 -05:00
|
|
|
Designed to be used as an `esp-idf component`. Suggested usage is with `git submodule`
|
2018-07-28 14:33:15 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
git submodule add https://gitlab.com/morganrallen/esp32-lora.git components/esp32-lora/
|
|
|
|
git submodule init
|
|
|
|
git submodule update
|
|
|
|
```
|
|
|
|
|
|
|
|
## Configure
|
|
|
|
|
2020-02-23 14:47:08 -05:00
|
|
|
Uses built in KConfig for configuration.
|
|
|
|
Run `make menuconfig` and find config under `Component config` -> `LORA32`.
|
2018-07-28 14:33:15 -04:00
|
|
|
Defaults targeted to TTGO LoRa OLED boards.
|
2018-07-29 02:33:49 -04:00
|
|
|
|
|
|
|
## Use
|
|
|
|
This is the most basic usage. Setting up the LoRa instance, setting it's receive handler, running init then sending a message.
|
|
|
|
|
|
|
|
```
|
2020-02-23 14:47:08 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "esp32-lora.h";
|
|
|
|
|
2018-07-29 02:33:49 -04:00
|
|
|
lora32_cfg_t lora;
|
|
|
|
|
2020-02-23 14:47:08 -05:00
|
|
|
static void handle_lora_receive(uint8_t size) {
|
|
|
|
char *message = malloc(size+1);
|
|
|
|
|
|
|
|
lora32_read_data(&lora, message);
|
|
|
|
|
|
|
|
// ensure null termination
|
|
|
|
message[size] = '\0';
|
|
|
|
|
|
|
|
printf("msg: %s\n", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main() {
|
|
|
|
lora = lora32_create();
|
|
|
|
lora.receive = &handle_lora_receive;
|
2018-07-29 02:33:49 -04:00
|
|
|
|
2020-02-23 14:47:08 -05:00
|
|
|
lora32_init(&lora);
|
|
|
|
|
|
|
|
lora32_send(&lora, "Hello, LoRa", 11);
|
|
|
|
}
|
2018-07-29 02:33:49 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
2020-02-23 14:47:08 -05:00
|
|
|
### `static lora32_cfg_t lora32_create()`
|
|
|
|
Creates a new LoRa instance.
|
2018-07-29 02:33:49 -04:00
|
|
|
|
|
|
|
### `uint8_t lora32_init(lora32_cfg_t *config)`
|
2020-02-23 14:47:08 -05:00
|
|
|
Initialized LoRa instance. This configures GPIOs, SPI, LoRa radio and receive handlers.
|
2018-07-29 02:33:49 -04:00
|
|
|
|
|
|
|
Returns 1 on success.
|
|
|
|
|
2020-02-27 12:41:15 -05:00
|
|
|
### `void lora32_set_bandwidth(lora32_cfg_t *lora, enum bandwidth bw)`
|
|
|
|
Set operating bandwidth. Accepts enum bandwidth
|
|
|
|
`B78`, `B104`, `B156`, `B208`, `B3125`, `B417`, `B625`, `B125`, `B250`, `B500` or `0`-`9`
|
|
|
|
|
2020-02-23 14:47:08 -05:00
|
|
|
### `void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t d)`
|
|
|
|
Set `Coding Rate`.
|
|
|
|
|
|
|
|
Accepts `5`-`8`.
|
|
|
|
|
2018-07-29 02:33:49 -04:00
|
|
|
### `uint8_t lora32_data_available(lora32_cfg_t *lora)`
|
2020-02-23 14:47:08 -05:00
|
|
|
Returns number of bytes available in FIFO buffer.
|
2018-07-29 02:33:49 -04:00
|
|
|
|
|
|
|
### `void lora32_send(lora32_cfg_t *config, uint8_t *data, uint8_t len)`
|
2020-02-23 14:47:08 -05:00
|
|
|
Transmits data over the LoRa radio. Puts `DIO0` into `TXDONE` mode.
|
2018-07-29 02:33:49 -04:00
|
|
|
|
|
|
|
### `void lora32_set_spreadfactor(lora32_cfg_t *lora, uint8_t factor)`
|
|
|
|
Sets LoRa Spread Factor.
|
|
|
|
|
|
|
|
Accepts `6`-`12`.
|
|
|
|
|
2020-03-03 00:13:17 -05:00
|
|
|
### `double lora32_calc_datarate(lora32_cfg_t *lora);`
|
|
|
|
Returns data rate in Bits Per Second (bps) for given `lora32_cfg_t` configuration.
|
|
|
|
|
2018-07-29 02:33:49 -04:00
|
|
|
### `void lora32_dump_regs(lora32_cfg_t *lora)`
|
2020-02-23 14:47:08 -05:00
|
|
|
Dumps all registers from SX1276.
|
|
|
|
|
|
|
|
### `void lora32_read_data(lora32_cfg_t *lora, uint8_t *data)`
|
|
|
|
Reads data out of FIFO buffer. This would typically be called after the callback `received` is triggered.
|
|
|
|
|
|
|
|
### `void lora32_enable_continuous_rx(lora32_cfg_t *lora)`
|
|
|
|
Enables continuous receive mode. If a receive callback has been setup, it will be trigger
|
|
|
|
on any incoming data until either an `OP_MODE` or `DIO0` mode are change.
|
|
|
|
|
|
|
|
### `void lora32_enable_single_rx(lora32_cfg_t *lora)`
|
|
|
|
* **NOTE** This requires `DIO1` for complete functionality.
|
|
|
|
* **NOTE** Implenentation incomplete.
|
|
|
|
|
|
|
|
Enables single receive mode. Will either trigger `RXDONE` on `DIO0` or `RXTIMEOUT` on `DIO1`
|
|
|
|
|
|
|
|
### `void lora32_enable_cad(lora32_cfg_t *lora)`
|
|
|
|
Enables Channel Activity Detection. Will trigger callback `cad_done` with `bool detected` on completion.
|
|
|
|
If activity was detected, it will also trigger `cad_detected`
|
|
|
|
|
|
|
|
|
|
|
|
## Callbacks
|
|
|
|
`lora32_cfg_t` provides several callbacks based on `DIO0` interrupts. Most functionality is provided through
|
|
|
|
these, favoring async operations over syncronous polling.
|
|
|
|
|
|
|
|
### `lora32_cfg_t->receive(uint8_t size)`
|
|
|
|
Triggered from `RXCONTINUOUS` and `RXSINGLE` modes. Provides the number of bytes available. Should be followed
|
|
|
|
up with a call to `lora32_read_data` to retrieve data from FIFO buffer.
|
|
|
|
|
|
|
|
### `lora32_cfg_t->tx_done()`
|
|
|
|
Triggers after call to `lora32_send` completes, as long as `DIO0` mode has not been changed.
|
|
|
|
|
|
|
|
### `lora32_cfg_t->cad_done(bool detected)`
|
|
|
|
Triggers after call to `lora32_enable_cad` completes (ie times out) or channel activity is detected.
|
|
|
|
`bool detected` will reflect these two states.
|
|
|
|
|
|
|
|
### `lora32_cfg_t->cad_detected()`
|
|
|
|
Triggers after call to `lora32_enable_cad` successfully detects channel activity.
|