Compare commits

..

4 commits

Author SHA1 Message Date
Morgan 'ARR\!' Allen
db9b74ec76 renamed idle to standby to better reflect datasheet
lowered some log levels
2021-11-17 19:33:44 -08:00
Morgan 'ARR\!' Allen
14cd5df1e3 implement single RX, still needs timeout handling 2021-11-17 19:31:10 -08:00
Morgan 'ARR\!' Allen
8f07873624 reimplementing semephore and bus locking seems to have helped overall stability, particularly on first init 2021-11-17 19:30:01 -08:00
Morgan 'ARR\!' Allen
dc78e9b137 remove assert(REG_VERSION) in favor of returning error so application recovery is possible 2021-11-17 19:28:46 -08:00
2 changed files with 38 additions and 15 deletions

View file

@ -77,7 +77,7 @@
#define EV_DIO0 (1 << 0) #define EV_DIO0 (1 << 0)
#define ERR_LOR_ID_MISMATCH (00) #define ERR_LOR_VERSION_MISMATCH (01)
enum freq { enum freq {
F433, F866, F915 F433, F866, F915
@ -138,6 +138,7 @@ uint8_t lora32_data_available(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);
void lora32_enable_continuous_rx(lora32_cfg_t *lora); void lora32_enable_continuous_rx(lora32_cfg_t *lora);
void lora32_enable_single_rx(lora32_cfg_t *lora);
void lora32_enable_cad(lora32_cfg_t *lora); void lora32_enable_cad(lora32_cfg_t *lora);
void lora32_toggle_reset(lora32_cfg_t *lora); void lora32_toggle_reset(lora32_cfg_t *lora);
void lora32_send(lora32_cfg_t *config, uint8_t *data, uint8_t len); void lora32_send(lora32_cfg_t *config, uint8_t *data, uint8_t len);
@ -145,5 +146,7 @@ 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);
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_standby(lora32_cfg_t *lora);
#endif // _LORA32_H__ #endif // _LORA32_H__

View file

@ -31,7 +31,7 @@ static TaskHandle_t dio_task_handle;
static SemaphoreHandle_t spi_semaphore; static SemaphoreHandle_t spi_semaphore;
uint8_t lora32_read_reg(lora32_cfg_t *lora, uint8_t address) { uint8_t lora32_read_reg(lora32_cfg_t *lora, uint8_t address) {
//xSemaphoreTake(spi_semaphore, portMAX_DELAY); xSemaphoreTake(spi_semaphore, portMAX_DELAY);
spi_transaction_t t; spi_transaction_t t;
memset(&t, 0, sizeof(spi_transaction_t)); memset(&t, 0, sizeof(spi_transaction_t));
@ -45,13 +45,13 @@ uint8_t lora32_read_reg(lora32_cfg_t *lora, uint8_t address) {
ESP_LOGV(TAG, "<%2X<%2X", address, t.rx_data[1]); ESP_LOGV(TAG, "<%2X<%2X", address, t.rx_data[1]);
//xSemaphoreGive(spi_semaphore); xSemaphoreGive(spi_semaphore);
return t.rx_data[1]; return t.rx_data[1];
} }
void lora32_write_reg(lora32_cfg_t *lora, uint8_t address, uint8_t value) { void lora32_write_reg(lora32_cfg_t *lora, uint8_t address, uint8_t value) {
//xSemaphoreTake(spi_semaphore, portMAX_DELAY); xSemaphoreTake(spi_semaphore, portMAX_DELAY);
spi_device_handle_t spi = lora->spi; spi_device_handle_t spi = lora->spi;
@ -67,7 +67,7 @@ void lora32_write_reg(lora32_cfg_t *lora, uint8_t address, uint8_t value) {
ESP_ERROR_CHECK(spi_device_transmit(spi, &t)); ESP_ERROR_CHECK(spi_device_transmit(spi, &t));
//xSemaphoreGive(spi_semaphore); xSemaphoreGive(spi_semaphore);
}; };
double lora32_calc_datarate(lora32_cfg_t *lora) { double lora32_calc_datarate(lora32_cfg_t *lora) {
@ -91,20 +91,20 @@ void lora23_set_implicit_header(lora32_cfg_t *lora) {
lora32_write_reg(lora, REG_MODEM_CONFIG_1, lora32_read_reg(lora, REG_MODEM_CONFIG_1) | 0x01); lora32_write_reg(lora, REG_MODEM_CONFIG_1, lora32_read_reg(lora, REG_MODEM_CONFIG_1) | 0x01);
} }
void lora32_idle(lora32_cfg_t *lora) { void lora32_standby(lora32_cfg_t *lora) {
ESP_LOGD(TAG, "MODE_STANDBY"); ESP_LOGV(TAG, "MODE_STANDBY");
lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_STANDBY); lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_STANDBY);
} }
void lora32_sleep(lora32_cfg_t *lora) { void lora32_sleep(lora32_cfg_t *lora) {
ESP_LOGD(TAG, "MODE_SLEEP"); ESP_LOGV(TAG, "MODE_SLEEP");
lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_SLEEP); lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_SLEEP);
} }
void lora32_enable_tx(lora32_cfg_t *lora) { void lora32_enable_tx(lora32_cfg_t *lora) {
lora32_idle(lora); lora32_standby(lora);
if(lora->implicitHeader) if(lora->implicitHeader)
lora23_set_implicit_header(lora); lora23_set_implicit_header(lora);
@ -117,7 +117,7 @@ void lora32_enable_tx(lora32_cfg_t *lora) {
} }
void lora32_send(lora32_cfg_t *lora, uint8_t *data, uint8_t len) { void lora32_send(lora32_cfg_t *lora, uint8_t *data, uint8_t len) {
spi_device_acquire_bus(lora->spi, portMAX_DELAY); ESP_ERROR_CHECK(spi_device_acquire_bus(lora->spi, portMAX_DELAY));
lora32_write_reg(lora, REG_DIO_MAPPING_1, DIO0_MODE_TXDONE); lora32_write_reg(lora, REG_DIO_MAPPING_1, DIO0_MODE_TXDONE);
@ -246,6 +246,18 @@ void lora32_set_spreadfactor(lora32_cfg_t *lora, uint8_t 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));
} }
void lora32_enable_single_rx(lora32_cfg_t *lora) {
ESP_LOGD(TAG, "MODE_RX_SINGLE");
if(lora->receive != NULL) {
lora32_write_reg(lora, REG_DIO_MAPPING_1, DIO0_MODE_RXDONE);
}
lora32_standby(lora);
lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_SINGLE);
}
void lora32_enable_continuous_rx(lora32_cfg_t *lora) { void lora32_enable_continuous_rx(lora32_cfg_t *lora) {
ESP_LOGD(TAG, "MODE_RX_CONTINUOUS"); ESP_LOGD(TAG, "MODE_RX_CONTINUOUS");
@ -253,6 +265,8 @@ void lora32_enable_continuous_rx(lora32_cfg_t *lora) {
lora32_write_reg(lora, REG_DIO_MAPPING_1, DIO0_MODE_RXDONE); lora32_write_reg(lora, REG_DIO_MAPPING_1, DIO0_MODE_RXDONE);
} }
lora32_standby(lora);
lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS); lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS);
} }
@ -361,6 +375,8 @@ static void IRAM_ATTR lora32_dio_task(void *arg) {
ESP_LOGD(TAG, "clearing irqs"); ESP_LOGD(TAG, "clearing irqs");
lora32_write_reg(lora, REG_IRQ_FLAGS, irqs); lora32_write_reg(lora, REG_IRQ_FLAGS, irqs);
spi_device_release_bus(lora->spi);
// TODO handle header validation // TODO handle header validation
if((irqs & IRQ_RX_DONE) == IRQ_RX_DONE) { if((irqs & IRQ_RX_DONE) == IRQ_RX_DONE) {
lora32_handle_receive(lora); lora32_handle_receive(lora);
@ -385,8 +401,6 @@ static void IRAM_ATTR lora32_dio_task(void *arg) {
// these *should* fire at the same time, defaults to false // these *should* fire at the same time, defaults to false
if(lora->cad_done != NULL) lora->cad_done(lora, cad_detected); if(lora->cad_done != NULL) lora->cad_done(lora, cad_detected);
} }
spi_device_release_bus(lora->spi);
} }
} }
@ -463,7 +477,13 @@ 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);
assert(version == 0x12);
// if ID does not match, something is likely wrong on the SPI bus
if(version != 0x12) {
ESP_LOGD(TAG, "REG_VERSION returned incorrectly. Expected 0x12 got 0x%02X", version);
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);
@ -488,8 +508,8 @@ uint8_t lora32_init(lora32_cfg_t *lora) {
lora32_set_tx_power(lora, 17, PA_OUTPUT_PA_BOOST_PIN); lora32_set_tx_power(lora, 17, PA_OUTPUT_PA_BOOST_PIN);
ESP_LOGI(TAG, "lora32_set_tx_power"); ESP_LOGI(TAG, "lora32_set_tx_power");
lora32_idle(lora); lora32_standby(lora);
ESP_LOGI(TAG, "lora32_idle"); ESP_LOGI(TAG, "lora32_standby");
// 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) {