Compare commits
No commits in common. "development" and "c5dc9c961df3356fb01371cffa2e0cd75f3bfa29" have entirely different histories.
developmen
...
c5dc9c961d
2 changed files with 49 additions and 90 deletions
|
@ -23,7 +23,7 @@
|
||||||
#define REG_IRQ_FLAGS 0x12
|
#define REG_IRQ_FLAGS 0x12
|
||||||
#define REG_RX_NB_BYTES 0x13
|
#define REG_RX_NB_BYTES 0x13
|
||||||
#define REG_PKT_RSSI_VALUE 0x1a
|
#define REG_PKT_RSSI_VALUE 0x1a
|
||||||
#define REG_RSSI_VALUE 0x1b
|
#define REG_PKT_SNR_VALUE 0x1b
|
||||||
#define REG_MODEM_CONFIG_1 0x1d
|
#define REG_MODEM_CONFIG_1 0x1d
|
||||||
#define REG_MODEM_CONFIG_2 0x1e
|
#define REG_MODEM_CONFIG_2 0x1e
|
||||||
#define REG_PREAMBLE_MSB 0x20
|
#define REG_PREAMBLE_MSB 0x20
|
||||||
|
@ -80,26 +80,6 @@
|
||||||
|
|
||||||
#define ERR_LOR_VERSION_MISMATCH (01)
|
#define ERR_LOR_VERSION_MISMATCH (01)
|
||||||
|
|
||||||
#define LORA32_DEFAULT_CONFIG {\
|
|
||||||
.bandwidth = B250,\
|
|
||||||
.codingRate = 5,\
|
|
||||||
.frequency = 915000000,\
|
|
||||||
.spreadingFactor = 11,\
|
|
||||||
.preamble = DEFAULT_PREAMBLE,\
|
|
||||||
.implicitHeader = false,\
|
|
||||||
.useCRC = false,\
|
|
||||||
.fifoIdx = 0,\
|
|
||||||
.copi = -1,\
|
|
||||||
.cipo = -1,\
|
|
||||||
.clk = -1,\
|
|
||||||
.dio0 = -1,\
|
|
||||||
.dio1 = -1,\
|
|
||||||
.dio2 = -1,\
|
|
||||||
.nss = -1,\
|
|
||||||
.reset = -1,\
|
|
||||||
.spi_host = 1,\
|
|
||||||
}
|
|
||||||
|
|
||||||
enum freq {
|
enum freq {
|
||||||
F433, F866, F915
|
F433, F866, F915
|
||||||
} lora32_freq;
|
} lora32_freq;
|
||||||
|
@ -123,17 +103,17 @@ typedef struct {
|
||||||
} lora32_handle_t;
|
} lora32_handle_t;
|
||||||
|
|
||||||
typedef struct lora32_cfg_t {
|
typedef struct lora32_cfg_t {
|
||||||
|
uint8_t nss;
|
||||||
|
uint8_t cipo;
|
||||||
|
uint8_t copi;
|
||||||
|
uint8_t clk;
|
||||||
|
uint8_t reset;
|
||||||
uint8_t fifoIdx;
|
uint8_t fifoIdx;
|
||||||
uint8_t channel;
|
uint8_t channel;
|
||||||
uint8_t bandwidth;
|
uint8_t bandwidth;
|
||||||
uint8_t spreadingFactor;
|
uint8_t spreadingFactor;
|
||||||
uint8_t codingRate;
|
uint8_t codingRate;
|
||||||
|
|
||||||
int8_t nss;
|
|
||||||
int8_t cipo;
|
|
||||||
int8_t copi;
|
|
||||||
int8_t clk;
|
|
||||||
int8_t reset;
|
|
||||||
int8_t dio0;
|
int8_t dio0;
|
||||||
int8_t dio1;
|
int8_t dio1;
|
||||||
int8_t dio2;
|
int8_t dio2;
|
||||||
|
@ -161,8 +141,6 @@ typedef struct lora32_cfg_t {
|
||||||
uint8_t lora32_spi_init(lora32_cfg_t *config);
|
uint8_t lora32_spi_init(lora32_cfg_t *config);
|
||||||
uint8_t lora32_init(lora32_cfg_t *config);
|
uint8_t lora32_init(lora32_cfg_t *config);
|
||||||
uint8_t lora32_data_available(lora32_cfg_t *lora);
|
uint8_t lora32_data_available(lora32_cfg_t *lora);
|
||||||
int16_t lora32_get_packet_rssi(lora32_cfg_t *lora);
|
|
||||||
int16_t lora32_get_rssi(lora32_cfg_t *lora);
|
|
||||||
|
|
||||||
double lora32_calc_datarate(lora32_cfg_t *lora);
|
double lora32_calc_datarate(lora32_cfg_t *lora);
|
||||||
void lora32_dump_regs(lora32_cfg_t *lora);
|
void lora32_dump_regs(lora32_cfg_t *lora);
|
||||||
|
@ -176,7 +154,6 @@ void lora32_send(lora32_cfg_t *config, uint8_t *data, uint8_t len);
|
||||||
void lora32_set_bandwidth(lora32_cfg_t *lora, uint8_t bw);
|
void lora32_set_bandwidth(lora32_cfg_t *lora, uint8_t bw);
|
||||||
void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t cr);
|
void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t cr);
|
||||||
void lora32_set_spreadfactor(lora32_cfg_t *lora, uint8_t factor);
|
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_read_data(lora32_cfg_t *lora, uint8_t *data);
|
||||||
void lora32_sleep(lora32_cfg_t *lora);
|
void lora32_sleep(lora32_cfg_t *lora);
|
||||||
void lora32_standby(lora32_cfg_t *lora);
|
void lora32_standby(lora32_cfg_t *lora);
|
||||||
|
|
|
@ -74,7 +74,7 @@ double lora32_calc_datarate(lora32_cfg_t *lora) {
|
||||||
double cr = (4.0 / (long)lora->codingRate);
|
double cr = (4.0 / (long)lora->codingRate);
|
||||||
double sf = pow(2, lora->spreadingFactor);
|
double sf = pow(2, lora->spreadingFactor);
|
||||||
double c2 = sf / bandwidths[lora->bandwidth];
|
double c2 = sf / bandwidths[lora->bandwidth];
|
||||||
ESP_LOGD(TAG, "codingRate: %d cr: %f sf: %f c2: %f", lora->codingRate, cr, sf, c2);
|
ESP_LOGI(TAG, "codingRate: %d cr: %f sf: %f c2: %f", lora->codingRate, cr, sf, c2);
|
||||||
|
|
||||||
return lora->spreadingFactor * cr / c2 * 1000;
|
return lora->spreadingFactor * cr / c2 * 1000;
|
||||||
}
|
}
|
||||||
|
@ -151,12 +151,12 @@ void lora32_send(lora32_cfg_t *lora, uint8_t *data, uint8_t len) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void lora32_set_frequency(lora32_cfg_t *lora, long frequency) {
|
void lora32_set_frequency(lora32_cfg_t *lora, long frequency) {
|
||||||
ESP_LOGD(TAG, "lora32_set_frequency: %lu", frequency);
|
ESP_LOGI(TAG, "lora32_set_frequency: %lu", frequency);
|
||||||
uint64_t frf = ((uint64_t)frequency << 19) / 32000000;
|
uint64_t frf = ((uint64_t)frequency << 19) / 32000000;
|
||||||
|
|
||||||
ESP_LOGD(TAG, "REG_FRF_MSB: 0x%2X", (uint8_t)(frf >> 16));
|
ESP_LOGI(TAG, "REG_FRF_MSB: 0x%2X", (uint8_t)(frf >> 16));
|
||||||
ESP_LOGD(TAG, "REG_FRF_MID: 0x%2X", (uint8_t)(frf >> 8));
|
ESP_LOGI(TAG, "REG_FRF_MID: 0x%2X", (uint8_t)(frf >> 8));
|
||||||
ESP_LOGD(TAG, "REG_FRF_LSB: 0x%2X", (uint8_t)(frf >> 0));
|
ESP_LOGI(TAG, "REG_FRF_LSB: 0x%2X", (uint8_t)(frf >> 0));
|
||||||
|
|
||||||
lora32_write_reg(lora, REG_FRF_MSB, (uint8_t)(frf >> 16));
|
lora32_write_reg(lora, REG_FRF_MSB, (uint8_t)(frf >> 16));
|
||||||
lora32_write_reg(lora, REG_FRF_MID, (uint8_t)(frf >> 8));
|
lora32_write_reg(lora, REG_FRF_MID, (uint8_t)(frf >> 8));
|
||||||
|
@ -194,7 +194,7 @@ void lora32_set_tx_power(lora32_cfg_t *lora, uint8_t level, uint8_t output) {
|
||||||
lora32_write_reg(lora, REG_PA_CONFIG, PA_BOOST | (level - 2));
|
lora32_write_reg(lora, REG_PA_CONFIG, PA_BOOST | (level - 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "set_tx_power(%d, %d)", level, output);
|
ESP_LOGI(TAG, "set_tx_power(%d, %d)", level, output);
|
||||||
|
|
||||||
ESP_LOGD(TAG, "REG_PA_CONFIG: 0x%2X", lora32_read_reg(lora, REG_PA_CONFIG));
|
ESP_LOGD(TAG, "REG_PA_CONFIG: 0x%2X", lora32_read_reg(lora, REG_PA_CONFIG));
|
||||||
}
|
}
|
||||||
|
@ -231,7 +231,7 @@ void lora32_dump_regs(lora32_cfg_t *lora) {
|
||||||
|
|
||||||
void lora32_toggle_reset(lora32_cfg_t *config) {
|
void lora32_toggle_reset(lora32_cfg_t *config) {
|
||||||
// toggle reset (L/H)
|
// toggle reset (L/H)
|
||||||
ESP_LOGD(TAG, "Toggling reset pin %d", config->reset);
|
ESP_LOGI(TAG, "Toggling reset pin %d", config->reset);
|
||||||
|
|
||||||
gpio_set_level(config->reset, 0);
|
gpio_set_level(config->reset, 0);
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS); // requires 100us
|
vTaskDelay(100 / portTICK_PERIOD_MS); // requires 100us
|
||||||
|
@ -240,7 +240,7 @@ void lora32_toggle_reset(lora32_cfg_t *config) {
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS); // 5ms before available
|
vTaskDelay(100 / portTICK_PERIOD_MS); // 5ms before available
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t lora32_get_spreadfactor(lora32_cfg_t *lora) {
|
uint8_t lora32_get_spreadingfactor(lora32_cfg_t *lora) {
|
||||||
return (lora32_read_reg(lora, REG_MODEM_CONFIG_2) >> 4);
|
return (lora32_read_reg(lora, REG_MODEM_CONFIG_2) >> 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,11 +257,9 @@ void lora32_set_spreadfactor(lora32_cfg_t *lora, uint8_t factor) {
|
||||||
lora32_write_reg(lora, REG_DETECTION_THRESHOLD, DETECT_THRES_OTHER);
|
lora32_write_reg(lora, REG_DETECTION_THRESHOLD, DETECT_THRES_OTHER);
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGV(TAG, "lora32_set_spreadfactor: %d", factor);
|
ESP_LOGI(TAG, "lora32_set_spreadfactor: %d", factor);
|
||||||
|
|
||||||
lora32_write_reg(lora, REG_MODEM_CONFIG_2, (lora32_read_reg(lora, REG_MODEM_CONFIG_2) & 0x0F) | ((factor << 4) & 0xF0));
|
lora32_write_reg(lora, REG_MODEM_CONFIG_2, (lora32_read_reg(lora, REG_MODEM_CONFIG_2) & 0x0F) | ((factor << 4) & 0xF0));
|
||||||
|
|
||||||
lora->spreadingFactor = factor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lora32_enable_single_rx(lora32_cfg_t *lora) {
|
void lora32_enable_single_rx(lora32_cfg_t *lora) {
|
||||||
|
@ -300,44 +298,34 @@ void lora32_enable_cad(lora32_cfg_t *lora) {
|
||||||
lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_CAD_DETECT);
|
lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_CAD_DETECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t lora32_get_rssi(lora32_cfg_t *lora) {
|
|
||||||
// TODO support LF output constant of -164
|
|
||||||
return -157 + lora32_read_reg(lora, REG_RSSI_VALUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t lora32_get_packet_rssi(lora32_cfg_t *lora) {
|
|
||||||
// TODO support LF output constant of -164
|
|
||||||
return -157 + lora32_read_reg(lora, REG_PKT_RSSI_VALUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
long lora32_get_bandwidth(lora32_cfg_t *lora) {
|
long lora32_get_bandwidth(lora32_cfg_t *lora) {
|
||||||
uint8_t bw = (lora32_read_reg(lora, REG_MODEM_CONFIG_1) >> 4);
|
uint8_t bw = (lora32_read_reg(lora, REG_MODEM_CONFIG_1) >> 4);
|
||||||
ESP_LOGV(TAG, "lora32_get_bandwidth: %d", bw);
|
ESP_LOGI(TAG, "lora32_get_bandwidth: %d", bw);
|
||||||
|
|
||||||
if(bw > 9) return -1;
|
if(bw > 9) return -1;
|
||||||
|
|
||||||
ESP_LOGD(TAG, "lora32_get_bandwidth: %ld", bandwidths[bw]);
|
ESP_LOGI(TAG, "lora32_get_bandwidth: %ld", bandwidths[bw]);
|
||||||
|
|
||||||
return bandwidths[bw];
|
return bandwidths[bw];
|
||||||
}
|
}
|
||||||
|
|
||||||
void lora32_set_ldo_flag(lora32_cfg_t *lora) {
|
void lora32_set_ldo_flag(lora32_cfg_t *lora) {
|
||||||
long symbolDuration = 1000 / (lora32_get_bandwidth(lora) / (1L << lora32_get_spreadfactor(lora)));
|
long symbolDuration = 1000 / (lora32_get_bandwidth(lora) / (1L << lora32_get_spreadingfactor(lora)));
|
||||||
|
|
||||||
ESP_LOGD(TAG, "symbolDuration: %ld", symbolDuration);
|
ESP_LOGI(TAG, "symbolDuration: %ld", symbolDuration);
|
||||||
|
|
||||||
bool ldoOn = symbolDuration > 16;
|
bool ldoOn = symbolDuration > 16;
|
||||||
ESP_LOGD(TAG, "ldoOn: %d", ldoOn);
|
ESP_LOGI(TAG, "ldoOn: %d", ldoOn);
|
||||||
|
|
||||||
uint8_t modem_config_3 = lora32_read_reg(lora, REG_MODEM_CONFIG_3);
|
uint8_t modem_config_3 = lora32_read_reg(lora, REG_MODEM_CONFIG_3);
|
||||||
ESP_LOGD(TAG, "modem_config_3: %d", modem_config_3);
|
ESP_LOGI(TAG, "modem_config_3: %d", modem_config_3);
|
||||||
modem_config_3 |= ldoOn << 3;
|
modem_config_3 |= ldoOn << 3;
|
||||||
ESP_LOGD(TAG, "modem_config_3: %d", modem_config_3);
|
ESP_LOGI(TAG, "modem_config_3: %d", modem_config_3);
|
||||||
lora32_write_reg(lora, REG_MODEM_CONFIG_3, modem_config_3);
|
lora32_write_reg(lora, REG_MODEM_CONFIG_3, modem_config_3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lora32_set_bandwidth(lora32_cfg_t *lora, uint8_t bw) {
|
void lora32_set_bandwidth(lora32_cfg_t *lora, uint8_t bw) {
|
||||||
ESP_LOGD(TAG, "lora32_set_bandwidth: %d", bw);
|
ESP_LOGI(TAG, "lora32_set_bandwidth: %d", bw);
|
||||||
|
|
||||||
uint8_t modem_config_1 = lora32_read_reg(lora, REG_MODEM_CONFIG_1);
|
uint8_t modem_config_1 = lora32_read_reg(lora, REG_MODEM_CONFIG_1);
|
||||||
lora32_write_reg(lora, REG_MODEM_CONFIG_1, (modem_config_1 & 0x0F) | (bw << 4));
|
lora32_write_reg(lora, REG_MODEM_CONFIG_1, (modem_config_1 & 0x0F) | (bw << 4));
|
||||||
|
@ -351,7 +339,7 @@ void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t cr) {
|
||||||
|
|
||||||
cr = cr - 4;
|
cr = cr - 4;
|
||||||
|
|
||||||
ESP_LOGD(TAG, "lora32_set_coding_rate: %d", cr + 4);
|
ESP_LOGI(TAG, "lora32_set_coding_rate: %d", cr + 4);
|
||||||
|
|
||||||
lora32_write_reg(lora, REG_MODEM_CONFIG_1, (lora32_read_reg(lora, REG_MODEM_CONFIG_1) & 0xF1) | (cr << 1));
|
lora32_write_reg(lora, REG_MODEM_CONFIG_1, (lora32_read_reg(lora, REG_MODEM_CONFIG_1) & 0xF1) | (cr << 1));
|
||||||
}
|
}
|
||||||
|
@ -365,7 +353,7 @@ void lora32_set_lna(lora32_cfg_t *lora, uint8_t gain) {
|
||||||
uint8_t lna = lora32_read_reg(lora, REG_LNA);
|
uint8_t lna = lora32_read_reg(lora, REG_LNA);
|
||||||
|
|
||||||
lora32_write_reg(lora, REG_LNA, lna | gain);
|
lora32_write_reg(lora, REG_LNA, lna | gain);
|
||||||
ESP_LOGD(TAG, "set lna: 0x%2X", lna | gain);
|
ESP_LOGI(TAG, "set lna: 0x%2X", lna | gain);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void lora32_handle_receive(lora32_cfg_t *lora) {
|
static void lora32_handle_receive(lora32_cfg_t *lora) {
|
||||||
|
@ -384,11 +372,11 @@ static void lora32_handle_receive(lora32_cfg_t *lora) {
|
||||||
static void IRAM_ATTR lora32_dio_task(void *arg) {
|
static void IRAM_ATTR lora32_dio_task(void *arg) {
|
||||||
// allocate lora32_cfg_t to receive config from Queu
|
// allocate lora32_cfg_t to receive config from Queu
|
||||||
lora32_cfg_t *lora = malloc(sizeof(lora32_cfg_t));
|
lora32_cfg_t *lora = malloc(sizeof(lora32_cfg_t));
|
||||||
ESP_LOGD(TAG, "starting DIO handler task");
|
ESP_LOGI(TAG, "starting DIO handler task");
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
// wait for event over Queue
|
// wait for event over Queue
|
||||||
if(xQueueReceive(dio_event_queue, (void*)&lora, portMAX_DELAY) != pdPASS) continue;
|
if(xQueueReceive(dio_event_queue, (void*)lora, portMAX_DELAY) != pdPASS) continue;
|
||||||
|
|
||||||
// need a better way to log which event and from which config
|
// need a better way to log which event and from which config
|
||||||
//ESP_LOGI(TAG, "handling DIO0 on GPIO%d", lora->dio0);
|
//ESP_LOGI(TAG, "handling DIO0 on GPIO%d", lora->dio0);
|
||||||
|
@ -431,12 +419,12 @@ static void IRAM_ATTR lora32_dio_task(void *arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if((irqs & IRQ_FHSS_CHANGE) == IRQ_FHSS_CHANGE) {
|
if((irqs & IRQ_FHSS_CHANGE) == IRQ_FHSS_CHANGE) {
|
||||||
ESP_LOGD(TAG, "switching channel %d: %d", lora->channel, lora->channels[lora->channel]);
|
ESP_LOGI(TAG, "switching channel %d: %d", lora->channel, lora->channels[lora->channel]);
|
||||||
|
|
||||||
if(lora->channel == 0 && lora->channels[lora->channel] == 0) continue;
|
if(lora->channel == 0 && lora->channels[lora->channel] == 0) continue;
|
||||||
|
|
||||||
if(lora->channels[lora->channel] == 0) {
|
if(lora->channels[lora->channel] == 0) {
|
||||||
ESP_LOGD(TAG, "reseting to channel 0");
|
ESP_LOGI(TAG, "reseting to channel 0");
|
||||||
lora->channel = 0;
|
lora->channel = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,7 +439,7 @@ void lora32_read_data(lora32_cfg_t *lora, uint8_t *data) {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
uint8_t len = lora32_read_reg(lora, (lora->implicitHeader ? REG_PAYLOAD_LENGTH : REG_RX_NB_BYTES));
|
uint8_t len = lora32_read_reg(lora, (lora->implicitHeader ? REG_PAYLOAD_LENGTH : REG_RX_NB_BYTES));
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Reading %d bytes", len);
|
ESP_LOGI(TAG, "Reading %d bytes", len);
|
||||||
|
|
||||||
for(i = 0; i < len; i++) {
|
for(i = 0; i < len; i++) {
|
||||||
data[i] = lora32_read_reg(lora, REG_FIFO);
|
data[i] = lora32_read_reg(lora, REG_FIFO);
|
||||||
|
@ -459,11 +447,11 @@ void lora32_read_data(lora32_cfg_t *lora, uint8_t *data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void IRAM_ATTR lora32_on_dio(void *arg) {
|
static void IRAM_ATTR lora32_on_dio(void *arg) {
|
||||||
xQueueSendFromISR(dio_event_queue, (void*)&arg, pdFALSE);
|
xQueueSend(dio_event_queue, arg, (TickType_t)0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t lora32_spi_init(lora32_cfg_t *lora) {
|
uint8_t lora32_spi_init(lora32_cfg_t *lora) {
|
||||||
ESP_LOGD(TAG, "Initializing SPI bus");
|
ESP_LOGI(TAG, "Initializing SPI bus");
|
||||||
|
|
||||||
esp_err_t err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
|
|
||||||
|
@ -499,7 +487,7 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
|
||||||
|
|
||||||
lora32_toggle_reset(lora);
|
lora32_toggle_reset(lora);
|
||||||
// set NSS high
|
// set NSS high
|
||||||
ESP_LOGD(TAG, "Bringing NSS high: %d", lora->nss);
|
ESP_LOGI(TAG, "Bringing NSS high: %d", lora->nss);
|
||||||
gpio_set_level(lora->nss, 1);
|
gpio_set_level(lora->nss, 1);
|
||||||
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||||
|
@ -521,18 +509,16 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
|
||||||
uint8_t version = lora32_read_reg(lora, REG_VERSION);
|
uint8_t version = lora32_read_reg(lora, REG_VERSION);
|
||||||
ESP_LOGD(TAG, "lora32_get_id() == 0x%2X", version);
|
ESP_LOGD(TAG, "lora32_get_id() == 0x%2X", version);
|
||||||
|
|
||||||
lora->version = version;
|
|
||||||
|
|
||||||
// if ID does not match, something is likely wrong on the SPI bus
|
// if ID does not match, something is likely wrong on the SPI bus
|
||||||
if(version != 0x12) {
|
if(version != 0x12) {
|
||||||
ESP_LOGE(TAG, "REG_VERSION returned incorrectly. Expected 0x12 got 0x%02X", version);
|
ESP_LOGD(TAG, "REG_VERSION returned incorrectly. Expected 0x12 got 0x%02X", version);
|
||||||
|
|
||||||
return ERR_LOR_VERSION_MISMATCH;
|
return ERR_LOR_VERSION_MISMATCH;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: confirm this is happening. Before/after power measurements?
|
// TODO: confirm this is happening. Before/after power measurements?
|
||||||
lora32_sleep(lora);
|
lora32_sleep(lora);
|
||||||
ESP_LOGV(TAG, "lora32_sleep");
|
ESP_LOGI(TAG, "lora32_sleep");
|
||||||
|
|
||||||
// TODO: VERIFY
|
// TODO: VERIFY
|
||||||
lora32_set_frequency(lora, lora->frequency);
|
lora32_set_frequency(lora, lora->frequency);
|
||||||
|
@ -542,7 +528,7 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
|
||||||
|
|
||||||
lora32_write_reg(lora, REG_FIFO_TX_BASE_ADDR, 0x00);
|
lora32_write_reg(lora, REG_FIFO_TX_BASE_ADDR, 0x00);
|
||||||
lora32_write_reg(lora, REG_FIFO_RX_BASE_ADDR, 0x00);
|
lora32_write_reg(lora, REG_FIFO_RX_BASE_ADDR, 0x00);
|
||||||
ESP_LOGV(TAG, "clear rx/tx fifos");
|
ESP_LOGI(TAG, "clear rx/tx fifos");
|
||||||
|
|
||||||
lora32_set_lna(lora, 0x03);
|
lora32_set_lna(lora, 0x03);
|
||||||
|
|
||||||
|
@ -551,10 +537,10 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
|
||||||
|
|
||||||
// TODO make based on config
|
// TODO make based on config
|
||||||
lora32_set_tx_power(lora, 17, PA_OUTPUT_PA_BOOST_PIN);
|
lora32_set_tx_power(lora, 17, PA_OUTPUT_PA_BOOST_PIN);
|
||||||
ESP_LOGV(TAG, "lora32_set_tx_power");
|
ESP_LOGI(TAG, "lora32_set_tx_power");
|
||||||
|
|
||||||
lora32_standby(lora);
|
lora32_standby(lora);
|
||||||
ESP_LOGV(TAG, "lora32_standby");
|
ESP_LOGI(TAG, "lora32_standby");
|
||||||
|
|
||||||
if(lora->implicitHeader)
|
if(lora->implicitHeader)
|
||||||
lora23_set_implicit_header(lora);
|
lora23_set_implicit_header(lora);
|
||||||
|
@ -563,40 +549,36 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
|
||||||
|
|
||||||
// TODO setup shouldn't be based on just receive callback
|
// TODO setup shouldn't be based on just receive callback
|
||||||
if(lora->receive != NULL) {
|
if(lora->receive != NULL) {
|
||||||
ESP_LOGV(TAG, "Setting GPIO Interrupt");
|
ESP_LOGI(TAG, "Setting GPIO Interrupt");
|
||||||
|
|
||||||
// TODO check at least one DIOx pin is not NULL
|
// TODO check at least one DIOx pin is not NULL
|
||||||
|
|
||||||
uint64_t pin_bit_mask = 0;
|
|
||||||
if(lora->dio0 > -1)
|
|
||||||
pin_bit_mask |= (1 << lora->dio0);
|
|
||||||
if(lora->dio1 > -1)
|
|
||||||
pin_bit_mask |= (1 << lora->dio1);
|
|
||||||
if(lora->dio2 > -1)
|
|
||||||
pin_bit_mask |= (1 << lora->dio2);
|
|
||||||
|
|
||||||
io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
|
io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
|
||||||
io_conf.pin_bit_mask = pin_bit_mask;
|
io_conf.pin_bit_mask = ((1ULL << lora->dio0) | (1ULL << lora->dio1) | (1ULL << lora->dio2));
|
||||||
io_conf.mode = GPIO_MODE_INPUT;
|
io_conf.mode = GPIO_MODE_INPUT;
|
||||||
io_conf.pull_down_en = 1;
|
io_conf.pull_down_en = 0;
|
||||||
io_conf.pull_up_en = 0;
|
io_conf.pull_up_en = 0;
|
||||||
gpio_config(&io_conf);
|
gpio_config(&io_conf);
|
||||||
|
|
||||||
|
gpio_set_intr_type(lora->dio0, GPIO_INTR_POSEDGE);
|
||||||
|
gpio_set_intr_type(lora->dio1, GPIO_INTR_POSEDGE);
|
||||||
|
gpio_set_intr_type(lora->dio2, GPIO_INTR_POSEDGE);
|
||||||
|
|
||||||
// the DIO interrupt handling for every device is done from one task
|
// the DIO interrupt handling for every device is done from one task
|
||||||
if(dio_task_handle == NULL) {
|
if(dio_task_handle == NULL) {
|
||||||
ESP_LOGV(TAG, "Setting callback handler and ISR service");
|
ESP_LOGI(TAG, "Setting callback handler and ISR service");
|
||||||
|
|
||||||
// enable global ISR service
|
// enable global ISR service
|
||||||
gpio_install_isr_service(0);
|
gpio_install_isr_service(0);
|
||||||
|
|
||||||
dio_event_queue = xQueueCreate(10, sizeof(lora32_cfg_t *));
|
dio_event_queue = xQueueCreate(10, sizeof(lora32_cfg_t));
|
||||||
|
|
||||||
// this should probably be high priority
|
// this should probably be high priority
|
||||||
xTaskCreate(&lora32_dio_task, "lora32_dio_task", 4096, NULL, 6, &dio_task_handle);
|
xTaskCreate(&lora32_dio_task, "lora32_dio_task", 4096, NULL, 6, &dio_task_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add ISR handler to the global service started (once) above
|
// add ISR handler to the global service started (once) above
|
||||||
ESP_LOGV(TAG, "Installing ISR handler for GPIO%d", lora->dio0);
|
ESP_LOGI(TAG, "Installing ISR handler for GPIO%d", lora->dio0);
|
||||||
if(lora->dio0 > -1)
|
if(lora->dio0 > -1)
|
||||||
gpio_isr_handler_add(lora->dio0, lora32_on_dio, (void*)lora);
|
gpio_isr_handler_add(lora->dio0, lora32_on_dio, (void*)lora);
|
||||||
|
|
||||||
|
@ -607,5 +589,5 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
|
||||||
gpio_isr_handler_add(lora->dio2, lora32_on_dio, (void*)lora);
|
gpio_isr_handler_add(lora->dio2, lora32_on_dio, (void*)lora);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ESP_OK;
|
return 1;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue