diff --git a/components/sx127x_driver/sx127x_driver.c b/components/sx127x_driver/sx127x_driver.c index c04d1dd..b471ffc 100644 --- a/components/sx127x_driver/sx127x_driver.c +++ b/components/sx127x_driver/sx127x_driver.c @@ -88,7 +88,7 @@ esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr) { .max_transfer_sz = SX127X_MAX_TRANSFER, }; - ret = spi_bus_initialize(config->spi_host, &bus_config, 1); + ret = spi_bus_initialize(config->spi_host, &bus_config, SX127X_SPI_DMA_CHAN); SX127X_ERROR_CHECK2(ret, spi_bus_initialize) spi_device_interface_config_t device_config = { @@ -103,7 +103,7 @@ esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr) { .input_delay_ns = 0, .spics_io_num = config->cs_io_num, .flags = 0, - .queue_size = 8, + .queue_size = SX127X_SPI_QUEUE_SIZE, .pre_cb = NULL, .post_cb = NULL, }; @@ -187,8 +187,7 @@ static void sx127x_do_tx(sx127x_t *hndl, sx127x_packet_t *packet) { _TX_CHECK(sx127x_write_register(hndl, REG_PAYLOAD_LENGTH, 0)); _TX_CHECK(sx127x_write_fifo(hndl, packet->data, packet->data_len)); - _TX_CHECK( - sx127x_write_register(hndl, REG_PAYLOAD_LENGTH, packet->data_len)); + _TX_CHECK(sx127x_write_register(hndl, REG_PAYLOAD_LENGTH, packet->data_len)); _TX_CHECK(sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_FS_TX)); @@ -241,8 +240,8 @@ static void sx127x_do_rx(sx127x_t *hndl) { // TODO: implicit header receive? _TX_CHECK(sx127x_read_register(hndl, REG_RX_NB_BYTES, &packet_len)); - _TX_CHECK(sx127x_write_register(hndl, REG_FIFO_ADDR_PTR, - REG_FIFO_RX_CURRENT_ADDR)); + _TX_CHECK( + sx127x_write_register(hndl, REG_FIFO_ADDR_PTR, REG_FIFO_RX_CURRENT_ADDR)); packet.data_len = packet_len; packet.data = heap_caps_malloc( @@ -275,7 +274,7 @@ error: } void sx127x_task(void *arg) { - sx127x_t * hndl = (sx127x_t *)arg; + sx127x_t * hndl = (sx127x_t *)arg; TickType_t delay_time = portMAX_DELAY; QueueSetHandle_t qSet = xQueueCreateSet(8); xQueueAddToSet(hndl->intr_semaphore, qSet); @@ -317,15 +316,16 @@ esp_err_t sx127x_start(sx127x_t *hndl) { SX127X_CHECK(hndl->task_handle == NULL, "task already running", ESP_ERR_INVALID_STATE); - hndl->intr_semaphore = xSemaphoreCreateBinary(); - hndl->tx_packet_queue = xQueueCreate(TX_QUEUE_LEN, sizeof(sx127x_packet_t)); + hndl->intr_semaphore = xSemaphoreCreateBinary(); + hndl->tx_packet_queue = + xQueueCreate(SX127X_TX_QUEUE_LEN, sizeof(sx127x_packet_t)); hndl->rx_packet_queue = - xQueueCreate(RX_QUEUE_LEN, sizeof(sx127x_rx_packet_t)); + xQueueCreate(SX127X_RX_QUEUE_LEN, sizeof(sx127x_rx_packet_t)); 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); + xTaskCreate(sx127x_task, "sx127x_task", SX127X_TASK_STACK_SIZE, + (void *)hndl, SX127X_TASK_PRIORITY, &hndl->task_handle); SX127X_CHECK(pdRet == pdPASS, "failed to create task", ESP_FAIL); ret = sx127x_write_register(hndl, REG_DIO_MAPPING_1, 0x00); @@ -342,8 +342,7 @@ esp_err_t sx127x_start(sx127x_t *hndl) { ret = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1); SX127X_ERROR_CHECK2(ret, gpio_install_isr_service); - ret = gpio_isr_handler_add(hndl->config.irq_io_num, sx127x_isr, - (void *)hndl); + ret = gpio_isr_handler_add(hndl->config.irq_io_num, sx127x_isr, (void *)hndl); SX127X_ERROR_CHECK2(ret, gpio_isr_handler_add); return ESP_OK; @@ -369,10 +368,10 @@ esp_err_t sx127x_stop(sx127x_t *hndl) { return ESP_OK; } -esp_err_t sx127x_send_packet(sx127x_t *hndl, const char *data, - size_t data_len, TickType_t ticks_to_wait) { - SX127X_CHECK(atomic_load(&hndl->task_state) == SX127X_TASK_RUNNING, "task not running", - ESP_ERR_INVALID_STATE); +esp_err_t sx127x_send_packet(sx127x_t *hndl, const char *data, size_t data_len, + TickType_t ticks_to_wait) { + 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); sx127x_packet_t packet; @@ -381,16 +380,15 @@ esp_err_t sx127x_send_packet(sx127x_t *hndl, const char *data, 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 = - xQueueSend(hndl->tx_packet_queue, &packet, ticks_to_wait); + BaseType_t pdRet = xQueueSend(hndl->tx_packet_queue, &packet, ticks_to_wait); SX127X_CHECK(pdRet == pdTRUE, "tx queue full", ESP_ERR_TIMEOUT); return ESP_OK; } 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(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); @@ -401,6 +399,8 @@ esp_err_t sx127x_recv_packet(sx127x_t *hndl, sx127x_rx_packet_t *packet, return ESP_OK; } -QueueHandle_t sx127x_get_recv_queue(sx127x_t *hndl) { return hndl->rx_packet_queue; } +QueueHandle_t sx127x_get_recv_queue(sx127x_t *hndl) { + return hndl->rx_packet_queue; +} void sx127x_packet_rx_free(sx127x_rx_packet_t *packet) { free(packet->data); } \ No newline at end of file diff --git a/components/sx127x_driver/sx127x_internal.h b/components/sx127x_driver/sx127x_internal.h index 238e967..5ea40f5 100644 --- a/components/sx127x_driver/sx127x_internal.h +++ b/components/sx127x_driver/sx127x_internal.h @@ -6,10 +6,12 @@ #include // TODO: these should be in Kconfig -#define TASK_STACK_SIZE (2 * 1024) -#define TASK_PRIORITY 3 -#define RX_QUEUE_LEN 8 -#define TX_QUEUE_LEN 8 +#define SX127X_TASK_STACK_SIZE (2 * 1024) +#define SX127X_TASK_PRIORITY 3 +#define SX127X_RX_QUEUE_LEN 8 +#define SX127X_TX_QUEUE_LEN 8 +#define SX127X_SPI_QUEUE_SIZE 8 +#define SX127X_SPI_DMA_CHAN 1 #define SX127X_MAX_TRANSFER (256) #define RESET_DELAY (pdMS_TO_TICKS(15))