diff --git a/components/sx127x_driver/sx127x_driver.c b/components/sx127x_driver/sx127x_driver.c index 5dd1cb6..ec750a4 100644 --- a/components/sx127x_driver/sx127x_driver.c +++ b/components/sx127x_driver/sx127x_driver.c @@ -17,45 +17,45 @@ sx127x_config_t sx127x_config_default() { return config; } -static esp_err_t sx127x_write_config(sx127x_t *handle) { +static esp_err_t sx127x_write_config(sx127x_t *hndl) { esp_err_t ret; - const sx127x_config_t *config = &handle->config; + const sx127x_config_t *config = &hndl->config; - ret = sx127x_sleep(handle); + ret = sx127x_sleep(hndl); SX127X_ERROR_CHECK(ret); - ret = sx127x_set_frequency(handle, config->frequency); + ret = sx127x_set_frequency(hndl, config->frequency); SX127X_ERROR_CHECK(ret); - ret = sx127x_write_register(handle, REG_FIFO_TX_BASE_ADDR, 0); + ret = sx127x_write_register(hndl, REG_FIFO_TX_BASE_ADDR, 0); SX127X_ERROR_CHECK(ret); - sx127x_write_register(handle, REG_FIFO_RX_BASE_ADDR, 0); + sx127x_write_register(hndl, REG_FIFO_RX_BASE_ADDR, 0); SX127X_ERROR_CHECK(ret); uint8_t reg_lna; - ret = sx127x_read_register(handle, REG_LNA, ®_lna); + ret = sx127x_read_register(hndl, REG_LNA, ®_lna); SX127X_ERROR_CHECK(ret); reg_lna |= 0x03; // set LNA boost - ret = sx127x_write_register(handle, REG_LNA, reg_lna); + ret = sx127x_write_register(hndl, REG_LNA, reg_lna); SX127X_ERROR_CHECK(ret); // set auto AGC - ret = sx127x_write_register(handle, REG_MODEM_CONFIG_3, CONFIG3_AUTO_AGC); + ret = sx127x_write_register(hndl, REG_MODEM_CONFIG_3, CONFIG3_AUTO_AGC); SX127X_ERROR_CHECK(ret); - ret = sx127x_set_tx_power(handle, config->tx_power, true); + ret = sx127x_set_tx_power(hndl, config->tx_power, true); SX127X_ERROR_CHECK(ret); - ret = sx127x_set_spreading_factor(handle, config->spreading_factor); + ret = sx127x_set_spreading_factor(hndl, config->spreading_factor); SX127X_ERROR_CHECK(ret); - ret = sx127x_set_sync_word(handle, config->sync_word); + ret = sx127x_set_sync_word(hndl, config->sync_word); SX127X_ERROR_CHECK(ret); - ret = sx127x_set_crc(handle, config->crc); + ret = sx127x_set_crc(hndl, config->crc); SX127X_ERROR_CHECK(ret); - ret = sx127x_standby(handle); + ret = sx127x_standby(hndl); ESP_ERROR_CHECK(ret); return ESP_OK; @@ -63,12 +63,12 @@ static esp_err_t sx127x_write_config(sx127x_t *handle) { esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr) { esp_err_t ret; - sx127x_t *handle = malloc(sizeof(sx127x_t)); - SX127X_CHECK(handle != NULL, "malloc error", ESP_ERR_NO_MEM); + sx127x_t *hndl = malloc(sizeof(sx127x_t)); + SX127X_CHECK(hndl != NULL, "malloc error", ESP_ERR_NO_MEM); - handle->task_handle = NULL; - handle->task_state = SX127X_TASK_STOPPED; - memcpy(&handle->config, config, sizeof(sx127x_config_t)); + hndl->task_handle = NULL; + 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); SX127X_ERROR_CHECK2(ret, gpio_set_direction) @@ -108,46 +108,46 @@ esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr) { .post_cb = NULL, }; ret = spi_bus_add_device(config->spi_host, &device_config, - &handle->device_handle); + &hndl->device_handle); SX127X_ERROR_CHECK2(ret, spi_bus_add_device) // read version and check that it is compatible uint8_t version; - ret = sx127x_read_register(handle, REG_VERSION, &version); + ret = sx127x_read_register(hndl, REG_VERSION, &version); SX127X_ERROR_CHECK2(ret, sx127x_read_register); SX127X_CHECK(version == 0x12, "unsupported version %#x", ESP_ERR_INVALID_VERSION, version); - ret = sx127x_write_config(handle); + ret = sx127x_write_config(hndl); SX127X_ERROR_CHECK(ret); - *handle_ptr = handle; + *handle_ptr = hndl; return ESP_OK; } -esp_err_t sx127x_free(sx127x_t *handle) { +esp_err_t sx127x_free(sx127x_t *hndl) { esp_err_t ret; - if (handle->task_handle) { - ret = sx127x_stop(handle); + if (hndl->task_handle) { + ret = sx127x_stop(hndl); SX127X_ERROR_CHECK(ret); } - ret = spi_bus_remove_device(handle->device_handle); + ret = spi_bus_remove_device(hndl->device_handle); SX127X_ERROR_CHECK2(ret, spi_bus_remove_device) - ret = spi_bus_free(handle->config.spi_host); + ret = spi_bus_free(hndl->config.spi_host); SX127X_ERROR_CHECK2(ret, spi_bus_free) - free(handle); + free(hndl); return ESP_OK; } void IRAM_ATTR sx127x_isr(void *arg) { - sx127x_t * handle = (sx127x_t *)arg; + sx127x_t * hndl = (sx127x_t *)arg; BaseType_t hpTaskWoken; - xSemaphoreGiveFromISR(handle->intr_semaphore, &hpTaskWoken); + xSemaphoreGiveFromISR(hndl->intr_semaphore, &hpTaskWoken); if (hpTaskWoken) { portYIELD_FROM_ISR(); } @@ -159,11 +159,11 @@ void IRAM_ATTR sx127x_isr(void *arg) { goto error; \ } -static void sx127x_do_tx(sx127x_t *handle, sx127x_packet_t *packet) { +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 (handle->task_state == SX127X_TASK_RUNNING) { - _TX_CHECK(sx127x_read_register(handle, REG_OP_MODE, &op_mode)); + while (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) { break; @@ -171,41 +171,41 @@ static void sx127x_do_tx(sx127x_t *handle, sx127x_packet_t *packet) { vTaskDelay(1); // wait for finish transmitting } - _TX_CHECK(sx127x_standby(handle)); + _TX_CHECK(sx127x_standby(hndl)); - _TX_CHECK(sx127x_read_register(handle, REG_IRQ_FLAGS, &irq_flags)); + _TX_CHECK(sx127x_read_register(hndl, REG_IRQ_FLAGS, &irq_flags)); if (irq_flags & IRQ_TX_DONE_MASK) { // clear tx done bit - _TX_CHECK(sx127x_write_register(handle, REG_IRQ_FLAGS, IRQ_TX_DONE_MASK)); + _TX_CHECK(sx127x_write_register(hndl, REG_IRQ_FLAGS, IRQ_TX_DONE_MASK)); } - _TX_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_2, &config_2)); + _TX_CHECK(sx127x_read_register(hndl, REG_MODEM_CONFIG_2, &config_2)); config_2 &= ~0x01; // set explicit header mode TODO: implicit header? - _TX_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_2, config_2)); + _TX_CHECK(sx127x_write_register(hndl, REG_MODEM_CONFIG_2, config_2)); - _TX_CHECK(sx127x_write_register(handle, REG_FIFO_ADDR_PTR, 0)); - _TX_CHECK(sx127x_write_register(handle, REG_PAYLOAD_LENGTH, 0)); + _TX_CHECK(sx127x_write_register(hndl, REG_FIFO_ADDR_PTR, 0)); + _TX_CHECK(sx127x_write_register(hndl, REG_PAYLOAD_LENGTH, 0)); - _TX_CHECK(sx127x_write_fifo(handle, packet->data, packet->data_len)); + _TX_CHECK(sx127x_write_fifo(hndl, packet->data, packet->data_len)); _TX_CHECK( - sx127x_write_register(handle, REG_PAYLOAD_LENGTH, packet->data_len)); + sx127x_write_register(hndl, REG_PAYLOAD_LENGTH, packet->data_len)); - _TX_CHECK(sx127x_write_register(handle, REG_OP_MODE, + _TX_CHECK(sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_FS_TX)); vTaskDelay(pdMS_TO_TICKS(1)); - _TX_CHECK(sx127x_write_register(handle, REG_OP_MODE, + _TX_CHECK(sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_TX)); // wait for transmission to finish while (true) { - _TX_CHECK(sx127x_read_register(handle, REG_IRQ_FLAGS, &irq_flags)); + _TX_CHECK(sx127x_read_register(hndl, REG_IRQ_FLAGS, &irq_flags)); if (irq_flags & IRQ_TX_DONE_MASK) { // if the transmission is done break; } vTaskDelay(1); } // clear tx done bit - _TX_CHECK(sx127x_write_register(handle, REG_IRQ_FLAGS, IRQ_TX_DONE_MASK)); + _TX_CHECK(sx127x_write_register(hndl, REG_IRQ_FLAGS, IRQ_TX_DONE_MASK)); error: if (ret != ESP_OK) { @@ -213,19 +213,19 @@ error: ESP_LOGE(SX127X_TAG, "tx error: %s (%d)", error_name, ret); } // go back to rx mode - sx127x_write_register(handle, REG_OP_MODE, + sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); } -static void sx127x_do_rx(sx127x_t *handle) { +static void sx127x_do_rx(sx127x_t *hndl) { 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)); + _TX_CHECK(sx127x_read_register(hndl, REG_IRQ_FLAGS, &irq_flags)); // clear irq flags - _TX_CHECK(sx127x_write_register(handle, REG_IRQ_FLAGS, irq_flags)); + _TX_CHECK(sx127x_write_register(hndl, REG_IRQ_FLAGS, irq_flags)); if (irq_flags & IRQ_PAYLOAD_CRC_ERROR_MASK) { ESP_LOGW(SX127X_TAG, "rx crc error"); @@ -236,12 +236,12 @@ static void sx127x_do_rx(sx127x_t *handle) { goto error; } - sx127x_write_register(handle, REG_OP_MODE, + sx127x_write_register(hndl, 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, + _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)); packet.data_len = packet_len; @@ -251,12 +251,12 @@ static void sx127x_do_rx(sx127x_t *handle) { ESP_LOGE(SX127X_TAG, "malloc error"); goto error; } - _TX_CHECK(sx127x_read_fifo(handle, packet.data, packet.data_len)); + _TX_CHECK(sx127x_read_fifo(hndl, packet.data, packet.data_len)); - sx127x_read_pkt_rssi(handle, &packet.rssi); - sx127x_read_pkt_snr(handle, &packet.snr); + sx127x_read_pkt_rssi(hndl, &packet.rssi); + sx127x_read_pkt_snr(hndl, &packet.snr); - pdRet = xQueueSend(handle->rx_packet_queue, &packet, 0); + pdRet = xQueueSend(hndl->rx_packet_queue, &packet, 0); if (pdRet != pdTRUE) { ESP_LOGE(SX127X_TAG, "rx queue full"); free(packet.data); @@ -268,73 +268,73 @@ error: 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); + sx127x_write_register(hndl, REG_FIFO_ADDR_PTR, 0); // go back to rx mode - sx127x_write_register(handle, REG_OP_MODE, + sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); } void sx127x_task(void *arg) { - sx127x_t * handle = (sx127x_t *)arg; + sx127x_t * hndl = (sx127x_t *)arg; TickType_t delay_time = portMAX_DELAY; QueueSetHandle_t qSet = xQueueCreateSet(8); - xQueueAddToSet(handle->intr_semaphore, qSet); - xQueueAddToSet(handle->tx_packet_queue, qSet); + xQueueAddToSet(hndl->intr_semaphore, qSet); + xQueueAddToSet(hndl->tx_packet_queue, qSet); sx127x_packet_t packet; // be in rx mode by default - sx127x_write_register(handle, REG_OP_MODE, + sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_RX_CONT); - while (handle->task_state == SX127X_TASK_RUNNING) { + while (hndl->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 (queue == hndl->intr_semaphore) { + BaseType_t didRecv = xSemaphoreTake(hndl->intr_semaphore, 0); if (didRecv) { ESP_LOGV(SX127X_TAG, "recv from isr"); - sx127x_do_rx(handle); + sx127x_do_rx(hndl); } - } else if (queue == handle->tx_packet_queue) { - BaseType_t didRecv = xQueueReceive(handle->tx_packet_queue, &packet, 0); + } else if (queue == hndl->tx_packet_queue) { + BaseType_t didRecv = xQueueReceive(hndl->tx_packet_queue, &packet, 0); if (didRecv) { ESP_LOGV(SX127X_TAG, "tx packet: %.*s", packet.data_len, packet.data); - sx127x_do_tx(handle, &packet); + sx127x_do_tx(hndl, &packet); free(packet.data); } } } ESP_LOGI(SX127X_TAG, "sx127x_task exiting"); - handle->task_state = SX127X_TASK_STOPPED; + hndl->task_state = SX127X_TASK_STOPPED; vTaskDelete(NULL); // must delete own task } -esp_err_t sx127x_start(sx127x_t *handle) { +esp_err_t sx127x_start(sx127x_t *hndl) { esp_err_t ret; - SX127X_CHECK(handle->task_handle == NULL, "task already running", + SX127X_CHECK(hndl->task_handle == NULL, "task already running", ESP_ERR_INVALID_STATE); - handle->intr_semaphore = xSemaphoreCreateBinary(); - handle->tx_packet_queue = xQueueCreate(TX_QUEUE_LEN, sizeof(sx127x_packet_t)); - handle->rx_packet_queue = + hndl->intr_semaphore = xSemaphoreCreateBinary(); + hndl->tx_packet_queue = xQueueCreate(TX_QUEUE_LEN, sizeof(sx127x_packet_t)); + hndl->rx_packet_queue = xQueueCreate(RX_QUEUE_LEN, sizeof(sx127x_rx_packet_t)); - handle->task_state = SX127X_TASK_RUNNING; + hndl->task_state = SX127X_TASK_RUNNING; BaseType_t pdRet = - xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)handle, - TASK_PRIORITY, &handle->task_handle); + xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)hndl, + TASK_PRIORITY, &hndl->task_handle); SX127X_CHECK(pdRet == pdPASS, "failed to create task", ESP_FAIL); - ret = sx127x_write_register(handle, REG_DIO_MAPPING_1, 0x00); + ret = sx127x_write_register(hndl, REG_DIO_MAPPING_1, 0x00); SX127X_ERROR_CHECK(ret); gpio_config_t irq_io_config; irq_io_config.intr_type = GPIO_INTR_POSEDGE; irq_io_config.mode = GPIO_MODE_INPUT; - irq_io_config.pin_bit_mask = (1ULL << handle->config.irq_io_num); + irq_io_config.pin_bit_mask = (1ULL << hndl->config.irq_io_num); irq_io_config.pull_down_en = 0; irq_io_config.pull_up_en = 0; ret = gpio_config(&irq_io_config); @@ -342,36 +342,36 @@ esp_err_t sx127x_start(sx127x_t *handle) { ret = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1); SX127X_ERROR_CHECK2(ret, gpio_install_isr_service); - ret = gpio_isr_handler_add(handle->config.irq_io_num, sx127x_isr, - (void *)handle); + 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; } -esp_err_t sx127x_stop(sx127x_t *handle) { +esp_err_t sx127x_stop(sx127x_t *hndl) { esp_err_t ret; - SX127X_CHECK(handle->task_handle != NULL, "task has not been started", + SX127X_CHECK(hndl->task_handle != NULL, "task has not been started", ESP_ERR_INVALID_STATE); - handle->task_state = SX127X_TASK_STOPPING; - xTaskNotifyGive(handle->task_handle); + hndl->task_state = SX127X_TASK_STOPPING; + xTaskNotifyGive(hndl->task_handle); - ret = gpio_isr_handler_remove(handle->config.irq_io_num); + ret = gpio_isr_handler_remove(hndl->config.irq_io_num); SX127X_ERROR_CHECK2(ret, gpio_isr_handler_remove); gpio_uninstall_isr_service(); - while (handle->task_state != SX127X_TASK_STOPPED) { + while (hndl->task_state != SX127X_TASK_STOPPED) { vTaskDelay(10); } - handle->task_handle = NULL; + hndl->task_handle = NULL; return ESP_OK; } -esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, +esp_err_t sx127x_send_packet(sx127x_t *hndl, const char *data, size_t data_len, TickType_t ticks_to_wait) { - SX127X_CHECK(handle->task_state == SX127X_TASK_RUNNING, "task not running", + SX127X_CHECK(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); @@ -382,16 +382,16 @@ esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, SX127X_CHECK(packet.data != NULL, "malloc error", ESP_ERR_NO_MEM); memcpy(packet.data, data, data_len); BaseType_t pdRet = - xQueueSend(handle->tx_packet_queue, &packet, ticks_to_wait); + 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 *handle, sx127x_rx_packet_t *packet, +esp_err_t sx127x_recv_packet(sx127x_t *hndl, 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); + xQueueReceive(hndl->rx_packet_queue, packet, ticks_to_wait); if (pdRet != pdTRUE) { ESP_LOGV(SX127X_TAG, "timeout on recv_packet"); return ESP_ERR_TIMEOUT; @@ -399,6 +399,6 @@ esp_err_t sx127x_recv_packet(sx127x_t *handle, sx127x_rx_packet_t *packet, return ESP_OK; } -QueueHandle_t sx127x_get_recv_queue(sx127x_t *handle) { return handle->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_driver.h b/components/sx127x_driver/sx127x_driver.h index 1658044..9c7a8ab 100644 --- a/components/sx127x_driver/sx127x_driver.h +++ b/components/sx127x_driver/sx127x_driver.h @@ -79,24 +79,24 @@ typedef struct sx127x_packet_rx { int8_t snr; } sx127x_rx_packet_t; -typedef struct sx127x sx127x_t; +typedef struct sx127x* sx127x_hndl; sx127x_config_t sx127x_config_default(); -esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr); +esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_hndl *hndl_ptr); -esp_err_t sx127x_free(sx127x_t *handle); +esp_err_t sx127x_free(sx127x_hndl hndl); -esp_err_t sx127x_start(sx127x_t *handle); +esp_err_t sx127x_start(sx127x_hndl hndl); -esp_err_t sx127x_stop(sx127x_t *handle); +esp_err_t sx127x_stop(sx127x_hndl hndl); -esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, +esp_err_t sx127x_send_packet(sx127x_hndl hndl, const char *data, size_t data_len, TickType_t ticks_to_wait); -esp_err_t sx127x_recv_packet(sx127x_t *handle, sx127x_rx_packet_t *packet_out, +esp_err_t sx127x_recv_packet(sx127x_hndl hndl, sx127x_rx_packet_t *packet_out, TickType_t ticks_to_wait); -QueueHandle_t sx127x_get_recv_queue(sx127x_t *handle); +QueueHandle_t sx127x_get_recv_queue(sx127x_hndl hndl); void sx127x_packet_rx_free(sx127x_rx_packet_t *packet); diff --git a/components/sx127x_driver/sx127x_internal.h b/components/sx127x_driver/sx127x_internal.h index c46cd5a..765b90c 100644 --- a/components/sx127x_driver/sx127x_internal.h +++ b/components/sx127x_driver/sx127x_internal.h @@ -124,7 +124,7 @@ typedef enum sx127x_task_state { SX127X_TASK_STOPPING = 2, } sx127x_task_state_t; -struct sx127x { +typedef struct sx127x { sx127x_config_t config; spi_device_handle_t device_handle; TaskHandle_t task_handle; @@ -136,6 +136,6 @@ struct sx127x { QueueHandle_t rx_packet_queue; // queue of packets to send to the radio QueueHandle_t tx_packet_queue; -}; +} sx127x_t; const char *SX127X_TAG; diff --git a/components/sx127x_driver/sx127x_registers.c b/components/sx127x_driver/sx127x_registers.c index cebbfe3..9791e8f 100644 --- a/components/sx127x_driver/sx127x_registers.c +++ b/components/sx127x_driver/sx127x_registers.c @@ -3,15 +3,15 @@ #include -esp_err_t sx127x_read_register(sx127x_t *handle, uint8_t reg, uint8_t *value) { - return sx127x_single_transfer(handle, reg & 0x7f, 0x00, value); +esp_err_t sx127x_read_register(sx127x_t *hndl, uint8_t reg, uint8_t *value) { + return sx127x_single_transfer(hndl, reg & 0x7f, 0x00, value); } -esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value) { - return sx127x_single_transfer(handle, reg | 0x80, value, NULL); +esp_err_t sx127x_write_register(sx127x_t *hndl, uint8_t reg, uint8_t value) { + return sx127x_single_transfer(hndl, reg | 0x80, value, NULL); } -esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, +esp_err_t sx127x_single_transfer(sx127x_t *hndl, uint8_t addr, uint8_t to_slave, uint8_t *from_slave) { spi_transaction_t trans; memset(&trans, 0, sizeof(trans)); @@ -21,7 +21,7 @@ esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, trans.rxlength = 8; trans.tx_data[0] = to_slave; - esp_err_t ret = spi_device_transmit(handle->device_handle, &trans); + esp_err_t ret = spi_device_transmit(hndl->device_handle, &trans); SX127X_ERROR_CHECK2(ret, spi_device_transmit); if (from_slave) { *from_slave = trans.rx_data[0]; @@ -31,34 +31,34 @@ esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, return ESP_OK; } -esp_err_t sx127x_sleep(sx127x_t *handle) { - return sx127x_write_register(handle, REG_OP_MODE, +esp_err_t sx127x_sleep(sx127x_t *hndl) { + return sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_SLEEP); } -esp_err_t sx127x_standby(sx127x_t *handle) { - return sx127x_write_register(handle, REG_OP_MODE, +esp_err_t sx127x_standby(sx127x_t *hndl) { + return sx127x_write_register(hndl, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_STDBY); } -esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency) { +esp_err_t sx127x_set_frequency(sx127x_t *hndl, uint64_t frequency) { uint64_t frf = ((uint64_t)frequency << 19) / 32000000; esp_err_t ret; - ret = sx127x_write_register(handle, REG_FRF_MSB, (uint8_t)(frf)); + ret = sx127x_write_register(hndl, REG_FRF_MSB, (uint8_t)(frf)); SX127X_ERROR_CHECK(ret); frf >>= 8; - ret = sx127x_write_register(handle, REG_FRF_MID, (uint8_t)(frf)); + ret = sx127x_write_register(hndl, REG_FRF_MID, (uint8_t)(frf)); SX127X_ERROR_CHECK(ret); frf >>= 8; - ret = sx127x_write_register(handle, REG_FRF_LSB, (uint8_t)(frf)); + ret = sx127x_write_register(hndl, REG_FRF_LSB, (uint8_t)(frf)); SX127X_ERROR_CHECK(ret); - handle->config.frequency = frequency; + hndl->config.frequency = frequency; return ESP_OK; } -esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, +esp_err_t sx127x_set_tx_power(sx127x_t *hndl, uint8_t tx_power, sx127x_pa_boost_t pa_boost) { esp_err_t ret; if (pa_boost == SX127X_PA_BOOST_ENABLED) { @@ -66,20 +66,20 @@ esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, SX127X_CHECK(tx_power >= 2 && tx_power <= 20, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power); ret = - sx127x_write_register(handle, REG_PA_CONFIG, PA_BOOST | (tx_power - 2)); + sx127x_write_register(hndl, REG_PA_CONFIG, PA_BOOST | (tx_power - 2)); } else { // RFO SX127X_CHECK(tx_power <= 14, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power); - ret = sx127x_write_register(handle, REG_PA_CONFIG, 0x70 | tx_power); + ret = sx127x_write_register(hndl, REG_PA_CONFIG, 0x70 | tx_power); } SX127X_ERROR_CHECK(ret); - handle->config.tx_power = tx_power; + hndl->config.tx_power = tx_power; return ESP_OK; } -esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, +esp_err_t sx127x_set_spreading_factor(sx127x_t *hndl, uint8_t spreading_factor) { SX127X_CHECK(spreading_factor >= 6 && spreading_factor <= 12, "invalid spreading_factor", ESP_ERR_INVALID_ARG); @@ -92,53 +92,53 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, detection_optimize = 0xc3; detection_threshold = 0x0a; } - SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_OPTIMIZE, + SX127X_ERROR_CHECK(sx127x_write_register(hndl, REG_DETECTION_OPTIMIZE, detection_optimize)); - SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_THRESHOLD, + SX127X_ERROR_CHECK(sx127x_write_register(hndl, REG_DETECTION_THRESHOLD, detection_threshold)); uint8_t modem_config_3; SX127X_ERROR_CHECK( - sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); + sx127x_read_register(hndl, REG_MODEM_CONFIG_3, &modem_config_3)); modem_config_3 = (modem_config_3 & 0x03) | ((spreading_factor << 4) & 0xf0); SX127X_ERROR_CHECK( - sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); + sx127x_write_register(hndl, REG_MODEM_CONFIG_3, modem_config_3)); - handle->config.spreading_factor = spreading_factor; + hndl->config.spreading_factor = spreading_factor; return ESP_OK; } -esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, +esp_err_t sx127x_set_signal_bandwidth(sx127x_t *hndl, uint64_t signal_bandwidth) { uint8_t bw_reg = sx127x_bw_to_reg(signal_bandwidth); uint8_t modem_config_1; SX127X_ERROR_CHECK( - sx127x_read_register(handle, REG_MODEM_CONFIG_1, &modem_config_1)); + sx127x_read_register(hndl, REG_MODEM_CONFIG_1, &modem_config_1)); modem_config_1 = (modem_config_1 & 0x0f) | (bw_reg << 4); SX127X_ERROR_CHECK( - sx127x_write_register(handle, REG_MODEM_CONFIG_1, modem_config_1)); - handle->config.signal_bandwidth = signal_bandwidth; + sx127x_write_register(hndl, REG_MODEM_CONFIG_1, modem_config_1)); + hndl->config.signal_bandwidth = signal_bandwidth; // set low data rate optimization flag uint64_t bw = sx127x_reg_to_bw(bw_reg); - uint8_t sf = handle->config.spreading_factor; + uint8_t sf = hndl->config.spreading_factor; // section 4.1.1.5 uint64_t symbol_duration_ms = 1000 / (bw / (1L << sf)); // section 4.1.1.6 bool must_have_ldo = (symbol_duration_ms > 16); - bool ldo = must_have_ldo || (handle->config.ldo == SX127X_LDO_ENABLED); + bool ldo = must_have_ldo || (hndl->config.ldo == SX127X_LDO_ENABLED); uint8_t modem_config_3; SX127X_ERROR_CHECK( - sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); + sx127x_read_register(hndl, REG_MODEM_CONFIG_3, &modem_config_3)); if (ldo) { modem_config_3 |= (1 << 3); } else { modem_config_3 &= ~(1 << 3); } SX127X_ERROR_CHECK( - sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); + sx127x_write_register(hndl, REG_MODEM_CONFIG_3, modem_config_3)); return ESP_OK; } @@ -183,48 +183,48 @@ uint64_t sx127x_reg_to_bw(uint8_t bandwidth_reg) { } } -esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word) { - SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_SYNC_WORD, sync_word)); - handle->config.sync_word = sync_word; +esp_err_t sx127x_set_sync_word(sx127x_t *hndl, uint8_t sync_word) { + SX127X_ERROR_CHECK(sx127x_write_register(hndl, REG_SYNC_WORD, sync_word)); + hndl->config.sync_word = sync_word; return ESP_OK; } -esp_err_t sx127x_set_crc(sx127x_t *handle, sx127x_crc_t crc) { +esp_err_t sx127x_set_crc(sx127x_t *hndl, sx127x_crc_t crc) { uint8_t modem_config_2; SX127X_ERROR_CHECK( - sx127x_read_register(handle, REG_MODEM_CONFIG_2, &modem_config_2)); + sx127x_read_register(hndl, REG_MODEM_CONFIG_2, &modem_config_2)); if (crc == SX127X_CRC_ENABLED) { modem_config_2 |= CONFIG2_CRC; } else { modem_config_2 &= ~CONFIG2_CRC; } SX127X_ERROR_CHECK( - sx127x_write_register(handle, REG_MODEM_CONFIG_2, modem_config_2)); - handle->config.crc = crc; + sx127x_write_register(hndl, REG_MODEM_CONFIG_2, modem_config_2)); + hndl->config.crc = crc; return ESP_OK; } -esp_err_t sx127x_read_pkt_rssi(sx127x_t *handle, int32_t *rssi) { +esp_err_t sx127x_read_pkt_rssi(sx127x_t *hndl, int32_t *rssi) { SX127X_CHECK(rssi != NULL, "rssi can not be NULL", ESP_ERR_INVALID_ARG); uint8_t rssi_val; - uint64_t freq = handle->config.frequency; + uint64_t freq = hndl->config.frequency; int32_t min_rssi = (freq < 868E6) ? -164 : -157; SX127X_ERROR_CHECK( - sx127x_read_register(handle, REG_PKT_RSSI_VALUE, &rssi_val)) + sx127x_read_register(hndl, REG_PKT_RSSI_VALUE, &rssi_val)) *rssi = min_rssi + rssi_val; return ESP_OK; } -esp_err_t sx127x_read_pkt_snr(sx127x_t *handle, int8_t *snr) { +esp_err_t sx127x_read_pkt_snr(sx127x_t *hndl, int8_t *snr) { SX127X_CHECK(snr != NULL, "rssi can not be NULL", ESP_ERR_INVALID_ARG); int8_t snr_val; SX127X_ERROR_CHECK( - sx127x_read_register(handle, REG_PKT_SNR_VALUE, (uint8_t *)&snr_val)) + sx127x_read_register(hndl, REG_PKT_SNR_VALUE, (uint8_t *)&snr_val)) *snr = snr_val; return ESP_OK; } -esp_err_t sx127x_write_fifo(sx127x_t *handle, const char *data, +esp_err_t sx127x_write_fifo(sx127x_t *hndl, const char *data, size_t data_len) { spi_transaction_t trans; memset(&trans, 0, sizeof(trans)); @@ -235,13 +235,13 @@ esp_err_t sx127x_write_fifo(sx127x_t *handle, const char *data, trans.rxlength = 0; trans.rx_buffer = NULL; - esp_err_t ret = spi_device_transmit(handle->device_handle, &trans); + esp_err_t ret = spi_device_transmit(hndl->device_handle, &trans); SX127X_ERROR_CHECK2(ret, spi_device_transmit); ESP_LOGV(SX127X_TAG, "sx127x_write_fifo(%.*s)", data_len, data); return ESP_OK; } -esp_err_t sx127x_read_fifo(sx127x_t *handle, char *data_out, size_t data_len) { +esp_err_t sx127x_read_fifo(sx127x_t *hndl, char *data_out, size_t data_len) { spi_transaction_t trans; memset(&trans, 0, sizeof(trans)); trans.flags = 0; @@ -251,7 +251,7 @@ esp_err_t sx127x_read_fifo(sx127x_t *handle, char *data_out, size_t data_len) { trans.rxlength = 0; // match length trans.rx_buffer = data_out; - esp_err_t ret = spi_device_transmit(handle->device_handle, &trans); + esp_err_t ret = spi_device_transmit(hndl->device_handle, &trans); SX127X_ERROR_CHECK2(ret, spi_device_transmit); ESP_LOGV(SX127X_TAG, "sx127x_read_fifo(%.*s)", data_len, data_out); return ESP_OK; diff --git a/components/sx127x_driver/sx127x_registers.h b/components/sx127x_driver/sx127x_registers.h index 68d8849..a872806 100644 --- a/components/sx127x_driver/sx127x_registers.h +++ b/components/sx127x_driver/sx127x_registers.h @@ -1,34 +1,34 @@ #include "sx127x_driver.h" -esp_err_t sx127x_read_register(sx127x_t *handle, uint8_t reg, uint8_t *value); -esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value); -esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, +esp_err_t sx127x_read_register(sx127x_hndl hdnl, uint8_t reg, uint8_t *value); +esp_err_t sx127x_write_register(sx127x_hndl hdnl, uint8_t reg, uint8_t value); +esp_err_t sx127x_single_transfer(sx127x_hndl hdnl, uint8_t addr, uint8_t to_slave, uint8_t *from_slave); -esp_err_t sx127x_sleep(sx127x_t *handle); +esp_err_t sx127x_sleep(sx127x_hndl hdnl); -esp_err_t sx127x_standby(sx127x_t *handle); +esp_err_t sx127x_standby(sx127x_hndl hdnl); -esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency); +esp_err_t sx127x_set_frequency(sx127x_hndl hdnl, uint64_t frequency); -esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, +esp_err_t sx127x_set_tx_power(sx127x_hndl hdnl, uint8_t tx_power, sx127x_pa_boost_t pa_boost); -esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, +esp_err_t sx127x_set_spreading_factor(sx127x_hndl hdnl, uint8_t spreading_factor); -esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, +esp_err_t sx127x_set_signal_bandwidth(sx127x_hndl hdnl, uint64_t signal_bandwidth); uint8_t sx127x_bw_to_reg(uint64_t bandwidth); uint64_t sx127x_reg_to_bw(uint8_t bandwidth_reg); -esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word); +esp_err_t sx127x_set_sync_word(sx127x_hndl hdnl, uint8_t sync_word); -esp_err_t sx127x_set_crc(sx127x_t *handle, sx127x_crc_t crc); +esp_err_t sx127x_set_crc(sx127x_hndl hdnl, sx127x_crc_t crc); -esp_err_t sx127x_read_pkt_rssi(sx127x_t *handle, int32_t *rssi); -esp_err_t sx127x_read_pkt_snr(sx127x_t *handle, int8_t *snr); +esp_err_t sx127x_read_pkt_rssi(sx127x_hndl hdnl, int32_t *rssi); +esp_err_t sx127x_read_pkt_snr(sx127x_hndl hdnl, int8_t *snr); -esp_err_t sx127x_write_fifo(sx127x_t *handle, const char *data, size_t data_len); -esp_err_t sx127x_read_fifo(sx127x_t *handle, char *data_out, +esp_err_t sx127x_write_fifo(sx127x_hndl hdnl, const char *data, size_t data_len); +esp_err_t sx127x_read_fifo(sx127x_hndl hdnl, char *data_out, size_t data_len); diff --git a/main/ugv_main.c b/main/ugv_main.c index 94b5390..f41114d 100644 --- a/main/ugv_main.c +++ b/main/ugv_main.c @@ -31,7 +31,7 @@ u8g2_t u8g2; uint16_t packet_num; -sx127x_t *lora; +sx127x_hndl lora; void loraOnReceive(int packetSize); void loraTask(void *params);