|
|
|
@ -145,8 +145,7 @@ esp_err_t sx127x_free(sx127x_t *handle) {
@@ -145,8 +145,7 @@ esp_err_t sx127x_free(sx127x_t *handle) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void IRAM_ATTR sx127x_isr(void *arg) { |
|
|
|
|
sx127x_t *handle = (sx127x_t *)arg; |
|
|
|
|
ESP_LOGI(SX127X_TAG, "sx127x_isr"); |
|
|
|
|
sx127x_t * handle = (sx127x_t *)arg; |
|
|
|
|
BaseType_t hpTaskWoken; |
|
|
|
|
xSemaphoreGiveFromISR(handle->intr_semaphore, &hpTaskWoken); |
|
|
|
|
if (hpTaskWoken) { |
|
|
|
@ -160,7 +159,7 @@ void IRAM_ATTR sx127x_isr(void *arg) {
@@ -160,7 +159,7 @@ void IRAM_ATTR sx127x_isr(void *arg) {
|
|
|
|
|
goto error; \ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void sx127x_do_tx(sx127x_t *handle, sx127x_packet_t *packet) { |
|
|
|
|
static void sx127x_do_tx(sx127x_t *handle, sx127x_packet_t *packet) { |
|
|
|
|
esp_err_t ret; |
|
|
|
|
uint8_t op_mode, irq_flags, config_2; |
|
|
|
|
while (handle->task_state == SX127X_TASK_RUNNING) { |
|
|
|
@ -218,24 +217,88 @@ error:
@@ -218,24 +217,88 @@ error:
|
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void sx127x_do_rx(sx127x_t *handle) { |
|
|
|
|
uint8_t irq_flags, packet_len; |
|
|
|
|
sx127x_rx_packet_t packet; |
|
|
|
|
esp_err_t ret; |
|
|
|
|
BaseType_t pdRet; |
|
|
|
|
|
|
|
|
|
_TX_CHECK(sx127x_read_register(handle, REG_IRQ_FLAGS, &irq_flags)); |
|
|
|
|
// clear irq flags
|
|
|
|
|
_TX_CHECK(sx127x_write_register(handle, REG_IRQ_FLAGS, irq_flags)); |
|
|
|
|
|
|
|
|
|
if (irq_flags & IRQ_PAYLOAD_CRC_ERROR_MASK) { |
|
|
|
|
ESP_LOGW(SX127X_TAG, "rx crc error"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
if ((irq_flags & IRQ_RX_DONE_MASK) == 0) { |
|
|
|
|
ESP_LOGD(SX127X_TAG, "sx127x_do_rx called but no rx done"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sx127x_write_register(handle, REG_OP_MODE, |
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_STDBY); |
|
|
|
|
|
|
|
|
|
// TODO: implicit header receive?
|
|
|
|
|
_TX_CHECK(sx127x_read_register(handle, REG_RX_NB_BYTES, &packet_len)); |
|
|
|
|
_TX_CHECK(sx127x_write_register(handle, REG_FIFO_ADDR_PTR, |
|
|
|
|
REG_FIFO_RX_CURRENT_ADDR)); |
|
|
|
|
|
|
|
|
|
packet.data_len = packet_len; |
|
|
|
|
packet.data = heap_caps_malloc( |
|
|
|
|
packet.data_len, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_DMA); |
|
|
|
|
if (!packet.data) { |
|
|
|
|
ESP_LOGE(SX127X_TAG, "malloc error"); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
_TX_CHECK(sx127x_read_fifo(handle, packet.data, packet.data_len)); |
|
|
|
|
|
|
|
|
|
sx127x_read_pkt_rssi(handle, &packet.rssi); |
|
|
|
|
sx127x_read_pkt_snr(handle, &packet.snr); |
|
|
|
|
|
|
|
|
|
pdRet = xQueueSend(handle->rx_packet_queue, &packet, 0); |
|
|
|
|
if (pdRet != pdTRUE) { |
|
|
|
|
ESP_LOGE(SX127X_TAG, "rx queue full"); |
|
|
|
|
free(packet.data); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
error: |
|
|
|
|
if (ret != ESP_OK) { |
|
|
|
|
const char *error_name = esp_err_to_name(ret); |
|
|
|
|
ESP_LOGE(SX127X_TAG, "rx error: %s (%d)", error_name, ret); |
|
|
|
|
} |
|
|
|
|
sx127x_write_register(handle, REG_FIFO_ADDR_PTR, 0); |
|
|
|
|
// go back to rx mode
|
|
|
|
|
sx127x_write_register(handle, REG_OP_MODE, |
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void sx127x_task(void *arg) { |
|
|
|
|
sx127x_t * handle = (sx127x_t *)arg; |
|
|
|
|
TickType_t delay_time = 0; |
|
|
|
|
TickType_t delay_time = portMAX_DELAY; |
|
|
|
|
QueueSetHandle_t qSet = xQueueCreateSet(8); |
|
|
|
|
xQueueAddToSet(handle->intr_semaphore, qSet); |
|
|
|
|
xQueueAddToSet(handle->tx_packet_queue, qSet); |
|
|
|
|
sx127x_packet_t packet; |
|
|
|
|
|
|
|
|
|
// be in rx mode by default
|
|
|
|
|
sx127x_write_register(handle, REG_OP_MODE, |
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); |
|
|
|
|
|
|
|
|
|
while (handle->task_state == SX127X_TASK_RUNNING) { |
|
|
|
|
QueueSetMemberHandle_t queue = xQueueSelectFromSet(qSet, delay_time); |
|
|
|
|
if (queue == handle->intr_semaphore) { |
|
|
|
|
BaseType_t didRecv = xSemaphoreTake(handle->intr_semaphore, 0); |
|
|
|
|
if (didRecv) { |
|
|
|
|
ESP_LOGI(SX127X_TAG, "recv from isr: %d", didRecv); |
|
|
|
|
ESP_LOGV(SX127X_TAG, "recv from isr"); |
|
|
|
|
|
|
|
|
|
sx127x_do_rx(handle); |
|
|
|
|
} |
|
|
|
|
} else if (queue == handle->tx_packet_queue) { |
|
|
|
|
BaseType_t didRecv = xQueueReceive(handle->tx_packet_queue, &packet, 0); |
|
|
|
|
if (didRecv) { |
|
|
|
|
ESP_LOGI(SX127X_TAG, "tx packet: %.*s", packet.data_len, packet.data); |
|
|
|
|
ESP_LOGV(SX127X_TAG, "tx packet: %.*s", packet.data_len, packet.data); |
|
|
|
|
|
|
|
|
|
sx127x_do_tx(handle, &packet); |
|
|
|
|
|
|
|
|
@ -254,9 +317,10 @@ esp_err_t sx127x_start(sx127x_t *handle) {
@@ -254,9 +317,10 @@ esp_err_t sx127x_start(sx127x_t *handle) {
|
|
|
|
|
SX127X_CHECK(handle->task_handle == NULL, "task already running", |
|
|
|
|
ESP_ERR_INVALID_STATE); |
|
|
|
|
|
|
|
|
|
handle->intr_semaphore = xSemaphoreCreateBinary(); |
|
|
|
|
// handle->recv_packet_queue = xQueueCreate(8, 10);
|
|
|
|
|
handle->tx_packet_queue = xQueueCreate(8, sizeof(sx127x_packet_t)); |
|
|
|
|
handle->intr_semaphore = xSemaphoreCreateBinary(); |
|
|
|
|
handle->tx_packet_queue = xQueueCreate(TX_QUEUE_LEN, sizeof(sx127x_packet_t)); |
|
|
|
|
handle->rx_packet_queue = |
|
|
|
|
xQueueCreate(RX_QUEUE_LEN, sizeof(sx127x_rx_packet_t)); |
|
|
|
|
|
|
|
|
|
handle->task_state = SX127X_TASK_RUNNING; |
|
|
|
|
BaseType_t pdRet = |
|
|
|
@ -308,12 +372,13 @@ esp_err_t sx127x_stop(sx127x_t *handle) {
@@ -308,12 +372,13 @@ esp_err_t sx127x_stop(sx127x_t *handle) {
|
|
|
|
|
esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, |
|
|
|
|
size_t data_len, TickType_t ticks_to_wait) { |
|
|
|
|
SX127X_CHECK(handle->task_state == SX127X_TASK_RUNNING, "task not running", |
|
|
|
|
ESP_FAIL); |
|
|
|
|
ESP_ERR_INVALID_STATE); |
|
|
|
|
SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", |
|
|
|
|
ESP_FAIL, data_len); |
|
|
|
|
ESP_ERR_INVALID_ARG, data_len); |
|
|
|
|
sx127x_packet_t packet; |
|
|
|
|
packet.data_len = data_len; |
|
|
|
|
packet.data = heap_caps_malloc(data_len, MALLOC_CAP_DMA); |
|
|
|
|
packet.data = heap_caps_malloc( |
|
|
|
|
data_len, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_DMA); |
|
|
|
|
SX127X_CHECK(packet.data != NULL, "malloc error", ESP_ERR_NO_MEM); |
|
|
|
|
memcpy(packet.data, data, data_len); |
|
|
|
|
BaseType_t pdRet = |
|
|
|
@ -321,3 +386,19 @@ esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data,
@@ -321,3 +386,19 @@ esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data,
|
|
|
|
|
SX127X_CHECK(pdRet == pdTRUE, "tx queue full", ESP_ERR_TIMEOUT); |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_recv_packet(sx127x_t *handle, sx127x_rx_packet_t *packet, |
|
|
|
|
TickType_t ticks_to_wait) { |
|
|
|
|
SX127X_CHECK(packet != NULL, "packet must not be NULL", ESP_ERR_INVALID_ARG); |
|
|
|
|
BaseType_t pdRet = |
|
|
|
|
xQueueReceive(handle->rx_packet_queue, packet, ticks_to_wait); |
|
|
|
|
if (pdRet != pdTRUE) { |
|
|
|
|
ESP_LOGV(SX127X_TAG, "timeout on recv_packet"); |
|
|
|
|
return ESP_ERR_TIMEOUT; |
|
|
|
|
} |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QueueHandle_t sx127x_get_recv_queue(sx127x_t *handle) { return handle->rx_packet_queue; } |
|
|
|
|
|
|
|
|
|
void sx127x_packet_rx_free(sx127x_rx_packet_t *packet) { free(packet->data); } |