WIP dump of multidev support

This commit is contained in:
Morgan 'ARR\!' Allen 2021-07-22 21:32:06 -07:00
parent 04768f460a
commit a445ed539f
3 changed files with 51 additions and 118 deletions

47
Kconfig
View File

@ -1,47 +0,0 @@
menuconfig LORA32_ENABLED
bool "LORA32"
default y
help
Select this option to enable LORA32 driver and show the submodule with configuration
config LORA32_NSS_PIN
int "GPIO to handle NSS"
depends on LORA32_ENABLED
default 18
help
GPIO to handle NSS
config LORA32_RESET_PIN
int "GPIO to handle RESET"
depends on LORA32_ENABLED
default 14
help
GPIO to handle RESET
config LORA32_DIO0_PIN
int "GPIO to handle DIO0"
depends on LORA32_ENABLED
default 26
help
GPIO to handle DIO0
config LORA32_MISO_PIN
int "GPIO to handle MISO"
depends on LORA32_ENABLED
default 19
help
GPIO to handle MISO
config LORA32_MOSI_PIN
int "GPIO to handle MOSI"
depends on LORA32_ENABLED
default 27
help
GPIO to handle MOSI
config LORA32_CLK_PIN
int "GPIO to handle CLK"
depends on LORA32_ENABLED
default 5
help
GPIO to handle CLK

View File

@ -88,10 +88,12 @@ typedef enum {
const long long frequencies[3];
const long bandwidths[10];
typedef void (*receiveCallback)(uint8_t size);
typedef void (*txdoneCallback)();
typedef void (*cadDoneCallback)(bool detected);
typedef void (*cadDetectedCallback)();
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;
@ -99,6 +101,9 @@ typedef struct {
typedef struct lora32_cfg_t {
uint8_t nss;
uint8_t cipo;
uint8_t copi;
uint8_t clk;
uint8_t dio0;
uint8_t reset;
uint8_t fifoIdx;
@ -118,12 +123,13 @@ typedef struct lora32_cfg_t {
cadDoneCallback cad_done;
cadDetectedCallback cad_detected;
uint8_t spi_host;
spi_device_handle_t spi;
lora32_handle_t handle;
} lora32_cfg_t;
lora32_cfg_t lora32_create();
uint8_t lora32_spi_init(lora32_cfg_t *config);
uint8_t lora32_init(lora32_cfg_t *config);
uint8_t lora32_data_available(lora32_cfg_t *lora);

View File

@ -10,16 +10,13 @@
#include "driver/gpio.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
#include "driver/spi_common_internal.h"
#include "esp32-lora.h"
#define READ_REG 0x7F
#define WRITE_REG 0x80
#define PIN_NUM_MISO CONFIG_LORA32_MISO_PIN
#define PIN_NUM_MOSI CONFIG_LORA32_MOSI_PIN
#define PIN_NUM_CLK CONFIG_LORA32_CLK_PIN
#define PA_OUTPUT_RFO_PIN 0
#define PA_OUTPUT_PA_BOOST_PIN 1
@ -28,44 +25,6 @@ const long bandwidths[] = { 7.8e3, 10.4e3, 15.6e3, 20.8e3, 31.25e3, 41.7e3, 62.5
const char *TAG = "LoRa32";
lora32_cfg_t lora32_create() {
static spi_device_handle_t spi;
return (lora32_cfg_t){
.bandwidth = B125,
.codingRate = DEFAULT_CR,
.dio0 = CONFIG_LORA32_DIO0_PIN,
.implicitHeader = false,
.nss = CONFIG_LORA32_NSS_PIN,
.reset = CONFIG_LORA32_RESET_PIN,
.frequency = 915000000,
.preamble = DEFAULT_PREAMBLE,
.spreadingFactor = DEFAULT_SF,
.spi = spi,
.receive = NULL,
.useCRC = false,
.fifoIdx = 0
};
}
void lora32_send_cmd(lora32_cfg_t lora, uint8_t cmd, uint8_t value, uint8_t len, uint8_t *rx_buffer) {
uint8_t tx_buffer[2];
tx_buffer[0] = cmd;
tx_buffer[1] = value;
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = 8 * len;
t.rxlength = 8 * len;
t.tx_buffer = &tx_buffer;
t.rx_buffer = rx_buffer;
ESP_ERROR_CHECK(spi_device_transmit(lora.spi, &t));
//ESP_LOGI(TAG, "send_cmd rx_data: 0x%2X 0x%2X", rx_buffer[0], rx_buffer[1]);
}
uint8_t lora32_read_reg(lora32_cfg_t *lora, uint8_t address) {
spi_transaction_t t;
memset(&t, 0, sizeof(spi_transaction_t));
@ -145,6 +104,8 @@ void lora32_enable_tx(lora32_cfg_t *lora) {
}
void lora32_send(lora32_cfg_t *lora, uint8_t *data, uint8_t len) {
spi_device_acquire_bus(lora->spi, portMAX_DELAY);
lora32_write_reg(lora, REG_DIO_MAPPING_1, DIO0_MODE_TXDONE);
lora32_enable_tx(lora);
@ -156,6 +117,8 @@ void lora32_send(lora32_cfg_t *lora, uint8_t *data, uint8_t len) {
lora32_write_reg(lora, REG_PAYLOAD_LENGTH, len);
lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
spi_device_release_bus(lora->spi);
}
void lora32_set_frequency(lora32_cfg_t *lora, long frequency) {
@ -360,7 +323,7 @@ static void lora32_handle_receive(lora32_cfg_t *lora) {
lora32_write_reg(lora, REG_FIFO_ADDR_PTR, fifo_addr);
lora->receive(len);
lora->receive(lora, len);
}
static void IRAM_ATTR lora32_dio0_task(void *arg) {
@ -368,13 +331,15 @@ static void IRAM_ATTR lora32_dio0_task(void *arg) {
ESP_LOGD(TAG, "starting DIO0 handler task");
while(1) {
EventBits_t evbits = xEventGroupWaitBits(lora->handle.events, EV_DIO0, pdTRUE, pdFALSE, 5000 / portTICK_PERIOD_MS);
EventBits_t evbits = xEventGroupWaitBits(lora->handle.events, EV_DIO0, pdTRUE, pdFALSE, portMAX_DELAY);
// timed out, loop and continue to wait
if(evbits == 0) continue;
ESP_LOGD(TAG, "handling DIO0");
spi_device_acquire_bus(lora->spi, portMAX_DELAY);
// read IRQ flags
uint8_t irqs = lora32_read_reg(lora, REG_IRQ_FLAGS);
ESP_LOGD(TAG, "reading irqs: %02X", irqs);
@ -388,7 +353,7 @@ static void IRAM_ATTR lora32_dio0_task(void *arg) {
}
if((irqs & IRQ_TX_DONE) == IRQ_TX_DONE) {
if(lora->tx_done != NULL) lora->tx_done();
if(lora->tx_done != NULL) lora->tx_done(lora);
}
bool cad_detected = false;
@ -398,14 +363,16 @@ static void IRAM_ATTR lora32_dio0_task(void *arg) {
cad_detected = true;
// no need for arg, cad_detected callback is always presummed true
if(lora->cad_detected != NULL) lora->cad_detected();
if(lora->cad_detected != NULL) lora->cad_detected(lora);
}
if((irqs & IRQ_CAD_DONE) == IRQ_CAD_DONE) {
// cad_done gets true/false from above, when activity is detected
// these *should* fire at the same time, defaults to false
if(lora->cad_done != NULL) lora->cad_done(cad_detected);
if(lora->cad_done != NULL) lora->cad_done(lora, cad_detected);
}
spi_device_release_bus(lora->spi);
}
}
@ -426,6 +393,27 @@ static void IRAM_ATTR lora32_on_dio0(void *arg) {
xEventGroupSetBitsFromISR(((lora32_cfg_t*)arg)->handle.events, EV_DIO0, &woken);
}
uint8_t lora32_spi_init(lora32_cfg_t *lora) {
ESP_LOGI(TAG, "Initializing SPI bus");
esp_err_t err = ESP_OK;
spi_bus_config_t buscfg = {
.miso_io_num = lora->cipo,
.mosi_io_num = lora->copi,
.sclk_io_num = lora->clk,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};
if(spi_bus_get_attr(lora->spi_host) == NULL) {
// 2 should be SPI_DMA_CH_AUTO ??? but it doesn't seem to be defined
err = spi_bus_initialize(lora->spi_host, &buscfg, 2);
}
return err;
}
uint8_t lora32_init(lora32_cfg_t *lora) {
gpio_config_t io_conf;
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
@ -443,19 +431,7 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
vTaskDelay(10 / portTICK_PERIOD_MS);
// init spi
ESP_LOGI(TAG, "Initializing SPI bus");
// TODO fix this awkward log along with pin assignments in general
ESP_LOGI(TAG, "\n MISO: %d\nMOSI: %d\nCLK: %d\nNSS: %d", PIN_NUM_MISO, PIN_NUM_MOSI, PIN_NUM_CLK, lora->nss);
spi_bus_config_t buscfg = {
.miso_io_num = PIN_NUM_MISO,
.mosi_io_num = PIN_NUM_MOSI,
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};
// SPI device setup
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 8E6,
.flags = 0,
@ -464,8 +440,7 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
.queue_size = 7,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &buscfg, 0));
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &devcfg, &lora->spi));
ESP_ERROR_CHECK(spi_bus_add_device(lora->spi_host, &devcfg, &lora->spi));
// initialize event groups
lora->handle.events = xEventGroupCreate();
@ -505,16 +480,15 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
ESP_LOGI(TAG, "Setting callback handler");
io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
io_conf.pin_bit_mask = (1ULL << CONFIG_LORA32_DIO0_PIN);
io_conf.pin_bit_mask = (1ULL << lora->dio0);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
gpio_set_intr_type(CONFIG_LORA32_DIO0_PIN, GPIO_INTR_POSEDGE);
gpio_install_isr_service(0);
gpio_set_intr_type(lora->dio0, GPIO_INTR_POSEDGE);
gpio_isr_handler_add(CONFIG_LORA32_DIO0_PIN, lora32_on_dio0, (void*)lora);
gpio_isr_register((void*)lora32_on_dio0, (void*)lora, 0, NULL);
// this should probably be high priority
xTaskCreate(&lora32_dio0_task, "lora32_dio0_task", 14048, (void*)lora, 6, NULL);