From 2d1a05d447316d2c21b3c09a75f5c4d6386ae56d Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Sat, 22 Feb 2020 08:56:41 -0800 Subject: [PATCH] CAD! --- main/esp32-lora.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/main/esp32-lora.c b/main/esp32-lora.c index 3b5cb59..5245422 100644 --- a/main/esp32-lora.c +++ b/main/esp32-lora.c @@ -243,6 +243,18 @@ void lora32_enable_continuous_rx(lora32_cfg_t *lora) { lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS); } +void lora32_enable_cad(lora32_cfg_t *lora) { + if((lora->cad_done != NULL) | (lora->cad_detected != NULL)) { + ESP_LOGD(TAG, "Setting DIO0 to CAD Detect"); + + lora32_write_reg(lora, REG_DIO_MAPPING_1, DIO0_MODE_CADDET); + } + + ESP_LOGD(TAG, "Enabling CAD Detection Mode"); + + lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_CAD_DETECT); +} + void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t d) { if(d < 5) d = 5; else if(d > 8) d = 8; @@ -284,7 +296,6 @@ static void IRAM_ATTR lora32_dio0_task(void *arg) { ESP_LOGD(TAG, "clearing irqs"); lora32_write_reg(lora, REG_IRQ_FLAGS, irqs); - // is this the right check? seems to work but looks off // TODO handle header validation if((irqs & IRQ_RX_DONE) == IRQ_RX_DONE) { lora32_handle_receive(lora); @@ -293,6 +304,22 @@ 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(); } + + bool cad_detected = false; + + if((irqs & IRQ_CAD_DETECTED) == IRQ_CAD_DETECTED) { + // this is for the next clause, CAD Done callback gets true/false + cad_detected = true; + + // no need for arg, cad_detected callback is always presummed true + if(lora->cad_detected != NULL) lora->cad_detected(); + } + + 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); + } } }