diff --git a/components/sx127x_driver/sx127x_driver.c b/components/sx127x_driver/sx127x_driver.c index ec750a4..c04d1dd 100644 --- a/components/sx127x_driver/sx127x_driver.c +++ b/components/sx127x_driver/sx127x_driver.c @@ -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); 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)); 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) { esp_err_t ret; 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)); uint8_t mode = op_mode & SX127X_MODE; 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_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); if (queue == hndl->intr_semaphore) { BaseType_t didRecv = xSemaphoreTake(hndl->intr_semaphore, 0); @@ -307,7 +307,7 @@ void sx127x_task(void *arg) { } } 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 } @@ -322,7 +322,7 @@ esp_err_t sx127x_start(sx127x_t *hndl) { hndl->rx_packet_queue = 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 = xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)hndl, 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", ESP_ERR_INVALID_STATE); - hndl->task_state = SX127X_TASK_STOPPING; + atomic_store(&hndl->task_state, SX127X_TASK_STOPPING); xTaskNotifyGive(hndl->task_handle); ret = gpio_isr_handler_remove(hndl->config.irq_io_num); SX127X_ERROR_CHECK2(ret, gpio_isr_handler_remove); gpio_uninstall_isr_service(); - while (hndl->task_state != SX127X_TASK_STOPPED) { + while (atomic_load(&hndl->task_state) != SX127X_TASK_STOPPED) { vTaskDelay(10); } 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, 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); SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", 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, 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); BaseType_t pdRet = xQueueReceive(hndl->rx_packet_queue, packet, ticks_to_wait); diff --git a/components/sx127x_driver/sx127x_internal.h b/components/sx127x_driver/sx127x_internal.h index 765b90c..238e967 100644 --- a/components/sx127x_driver/sx127x_internal.h +++ b/components/sx127x_driver/sx127x_internal.h @@ -3,6 +3,7 @@ #include #include #include +#include // TODO: these should be in Kconfig #define TASK_STACK_SIZE (2 * 1024) @@ -128,7 +129,7 @@ typedef struct sx127x { sx127x_config_t config; spi_device_handle_t device_handle; TaskHandle_t task_handle; - sx127x_task_state_t task_state; + _Atomic(sx127x_task_state_t) task_state; // binary semaphore which is given when an interrupt is received from the // radio SemaphoreHandle_t intr_semaphore;