|
|
@ -67,7 +67,7 @@ esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr) { |
|
|
|
SX127X_CHECK(hndl != NULL, "malloc error", ESP_ERR_NO_MEM); |
|
|
|
SX127X_CHECK(hndl != NULL, "malloc error", ESP_ERR_NO_MEM); |
|
|
|
|
|
|
|
|
|
|
|
hndl->task_handle = NULL; |
|
|
|
hndl->task_handle = NULL; |
|
|
|
hndl->task_state = SX127X_TASK_STOPPED; |
|
|
|
atomic_init(&hndl->task_state, SX127X_TASK_STOPPED); |
|
|
|
memcpy(&hndl->config, config, sizeof(sx127x_config_t)); |
|
|
|
memcpy(&hndl->config, config, sizeof(sx127x_config_t)); |
|
|
|
|
|
|
|
|
|
|
|
ret = gpio_set_direction(config->rst_io_num, GPIO_MODE_OUTPUT); |
|
|
|
ret = gpio_set_direction(config->rst_io_num, GPIO_MODE_OUTPUT); |
|
|
@ -162,7 +162,7 @@ void IRAM_ATTR sx127x_isr(void *arg) { |
|
|
|
static void sx127x_do_tx(sx127x_t *hndl, sx127x_packet_t *packet) { |
|
|
|
static void sx127x_do_tx(sx127x_t *hndl, sx127x_packet_t *packet) { |
|
|
|
esp_err_t ret; |
|
|
|
esp_err_t ret; |
|
|
|
uint8_t op_mode, irq_flags, config_2; |
|
|
|
uint8_t op_mode, irq_flags, config_2; |
|
|
|
while (hndl->task_state == SX127X_TASK_RUNNING) { |
|
|
|
while (atomic_load(&hndl->task_state) == SX127X_TASK_RUNNING) { |
|
|
|
_TX_CHECK(sx127x_read_register(hndl, REG_OP_MODE, &op_mode)); |
|
|
|
_TX_CHECK(sx127x_read_register(hndl, REG_OP_MODE, &op_mode)); |
|
|
|
uint8_t mode = op_mode & SX127X_MODE; |
|
|
|
uint8_t mode = op_mode & SX127X_MODE; |
|
|
|
if (mode != SX127X_MODE_TX && mode != SX127X_MODE_FS_TX) { |
|
|
|
if (mode != SX127X_MODE_TX && mode != SX127X_MODE_FS_TX) { |
|
|
@ -286,7 +286,7 @@ void sx127x_task(void *arg) { |
|
|
|
sx127x_write_register(hndl, REG_OP_MODE, |
|
|
|
sx127x_write_register(hndl, REG_OP_MODE, |
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); |
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); |
|
|
|
|
|
|
|
|
|
|
|
while (hndl->task_state == SX127X_TASK_RUNNING) { |
|
|
|
while (atomic_load(&hndl->task_state) == SX127X_TASK_RUNNING) { |
|
|
|
QueueSetMemberHandle_t queue = xQueueSelectFromSet(qSet, delay_time); |
|
|
|
QueueSetMemberHandle_t queue = xQueueSelectFromSet(qSet, delay_time); |
|
|
|
if (queue == hndl->intr_semaphore) { |
|
|
|
if (queue == hndl->intr_semaphore) { |
|
|
|
BaseType_t didRecv = xSemaphoreTake(hndl->intr_semaphore, 0); |
|
|
|
BaseType_t didRecv = xSemaphoreTake(hndl->intr_semaphore, 0); |
|
|
@ -307,7 +307,7 @@ void sx127x_task(void *arg) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
ESP_LOGI(SX127X_TAG, "sx127x_task exiting"); |
|
|
|
ESP_LOGI(SX127X_TAG, "sx127x_task exiting"); |
|
|
|
hndl->task_state = SX127X_TASK_STOPPED; |
|
|
|
atomic_store(&hndl->task_state, SX127X_TASK_STOPPED); |
|
|
|
vTaskDelete(NULL); // must delete own task
|
|
|
|
vTaskDelete(NULL); // must delete own task
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -322,7 +322,7 @@ esp_err_t sx127x_start(sx127x_t *hndl) { |
|
|
|
hndl->rx_packet_queue = |
|
|
|
hndl->rx_packet_queue = |
|
|
|
xQueueCreate(RX_QUEUE_LEN, sizeof(sx127x_rx_packet_t)); |
|
|
|
xQueueCreate(RX_QUEUE_LEN, sizeof(sx127x_rx_packet_t)); |
|
|
|
|
|
|
|
|
|
|
|
hndl->task_state = SX127X_TASK_RUNNING; |
|
|
|
atomic_store(&hndl->task_state, SX127X_TASK_RUNNING); |
|
|
|
BaseType_t pdRet = |
|
|
|
BaseType_t pdRet = |
|
|
|
xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)hndl, |
|
|
|
xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)hndl, |
|
|
|
TASK_PRIORITY, &hndl->task_handle); |
|
|
|
TASK_PRIORITY, &hndl->task_handle); |
|
|
@ -354,14 +354,14 @@ esp_err_t sx127x_stop(sx127x_t *hndl) { |
|
|
|
|
|
|
|
|
|
|
|
SX127X_CHECK(hndl->task_handle != NULL, "task has not been started", |
|
|
|
SX127X_CHECK(hndl->task_handle != NULL, "task has not been started", |
|
|
|
ESP_ERR_INVALID_STATE); |
|
|
|
ESP_ERR_INVALID_STATE); |
|
|
|
hndl->task_state = SX127X_TASK_STOPPING; |
|
|
|
atomic_store(&hndl->task_state, SX127X_TASK_STOPPING); |
|
|
|
xTaskNotifyGive(hndl->task_handle); |
|
|
|
xTaskNotifyGive(hndl->task_handle); |
|
|
|
|
|
|
|
|
|
|
|
ret = gpio_isr_handler_remove(hndl->config.irq_io_num); |
|
|
|
ret = gpio_isr_handler_remove(hndl->config.irq_io_num); |
|
|
|
SX127X_ERROR_CHECK2(ret, gpio_isr_handler_remove); |
|
|
|
SX127X_ERROR_CHECK2(ret, gpio_isr_handler_remove); |
|
|
|
gpio_uninstall_isr_service(); |
|
|
|
gpio_uninstall_isr_service(); |
|
|
|
|
|
|
|
|
|
|
|
while (hndl->task_state != SX127X_TASK_STOPPED) { |
|
|
|
while (atomic_load(&hndl->task_state) != SX127X_TASK_STOPPED) { |
|
|
|
vTaskDelay(10); |
|
|
|
vTaskDelay(10); |
|
|
|
} |
|
|
|
} |
|
|
|
hndl->task_handle = NULL; |
|
|
|
hndl->task_handle = NULL; |
|
|
@ -371,7 +371,7 @@ esp_err_t sx127x_stop(sx127x_t *hndl) { |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_send_packet(sx127x_t *hndl, const char *data, |
|
|
|
esp_err_t sx127x_send_packet(sx127x_t *hndl, const char *data, |
|
|
|
size_t data_len, TickType_t ticks_to_wait) { |
|
|
|
size_t data_len, TickType_t ticks_to_wait) { |
|
|
|
SX127X_CHECK(hndl->task_state == SX127X_TASK_RUNNING, "task not running", |
|
|
|
SX127X_CHECK(atomic_load(&hndl->task_state) == SX127X_TASK_RUNNING, "task not running", |
|
|
|
ESP_ERR_INVALID_STATE); |
|
|
|
ESP_ERR_INVALID_STATE); |
|
|
|
SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", |
|
|
|
SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", |
|
|
|
ESP_ERR_INVALID_ARG, data_len); |
|
|
|
ESP_ERR_INVALID_ARG, data_len); |
|
|
@ -389,6 +389,8 @@ esp_err_t sx127x_send_packet(sx127x_t *hndl, const char *data, |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_recv_packet(sx127x_t *hndl, sx127x_rx_packet_t *packet, |
|
|
|
esp_err_t sx127x_recv_packet(sx127x_t *hndl, sx127x_rx_packet_t *packet, |
|
|
|
TickType_t ticks_to_wait) { |
|
|
|
TickType_t ticks_to_wait) { |
|
|
|
|
|
|
|
SX127X_CHECK(atomic_load(&hndl->task_state) == SX127X_TASK_RUNNING, "task not running", |
|
|
|
|
|
|
|
ESP_ERR_INVALID_STATE); |
|
|
|
SX127X_CHECK(packet != NULL, "packet must not be NULL", ESP_ERR_INVALID_ARG); |
|
|
|
SX127X_CHECK(packet != NULL, "packet must not be NULL", ESP_ERR_INVALID_ARG); |
|
|
|
BaseType_t pdRet = |
|
|
|
BaseType_t pdRet = |
|
|
|
xQueueReceive(hndl->rx_packet_queue, packet, ticks_to_wait); |
|
|
|
xQueueReceive(hndl->rx_packet_queue, packet, ticks_to_wait); |
|
|
|