From 82e93af2e878e725e18b09521d3cffd6cb8d8a1c Mon Sep 17 00:00:00 2001 From: dernasherbrezon Date: Tue, 10 Dec 2024 19:46:37 +0000 Subject: [PATCH] optimize performances --- test/test_app/main/main.c | 28 ++++++---------- test/test_app/main/sx127x_fixture.c | 51 ++++++++++++++++++++++------- test/test_app/main/sx127x_fixture.h | 5 +++ 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/test/test_app/main/main.c b/test/test_app/main/main.c index 8db3fa0..00ba712 100644 --- a/test/test_app/main/main.c +++ b/test/test_app/main/main.c @@ -146,16 +146,13 @@ TEST_CASE("sx127x_test_fsk_tx_variable_length", "[fsk]") { TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_create(&tx_fixture_config, SX127x_MODULATION_FSK, &fixture)); setup_gpio_interrupts((gpio_num_t) tx_fixture_config.dio1, fixture, GPIO_INTR_NEGEDGE); sx127x_tx_set_callback(tx_callback, fixture->device); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture->device)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture)); xSemaphoreTake(fixture->tx_done, TIMEOUT); vTaskDelay(pdMS_TO_TICKS(50)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_max_single_batch, sizeof(fsk_max_single_batch), fixture->device)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_max_single_batch, sizeof(fsk_max_single_batch), fixture)); xSemaphoreTake(fixture->tx_done, TIMEOUT); vTaskDelay(pdMS_TO_TICKS(50)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_max_variable, sizeof(fsk_max_variable), fixture->device)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_max_variable, sizeof(fsk_max_variable), fixture)); xSemaphoreTake(fixture->tx_done, TIMEOUT); } @@ -191,8 +188,7 @@ TEST_CASE("sx127x_test_fsk_tx_fixed_small", "[fsk]") { TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_set_packet_format(SX127X_FIXED, sizeof(fsk_small_message), fixture->device)); setup_gpio_interrupts((gpio_num_t) tx_fixture_config.dio1, fixture, GPIO_INTR_NEGEDGE); sx127x_tx_set_callback(tx_callback, fixture->device); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture->device)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture)); xSemaphoreTake(fixture->tx_done, TIMEOUT); } @@ -211,8 +207,7 @@ TEST_CASE("sx127x_test_fsk_tx_fixed_batch", "[fsk]") { TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_set_packet_format(SX127X_FIXED, sizeof(fsk_max_single_batch_fixed), fixture->device)); setup_gpio_interrupts((gpio_num_t) tx_fixture_config.dio1, fixture, GPIO_INTR_NEGEDGE); sx127x_tx_set_callback(tx_callback, fixture->device); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_max_single_batch_fixed, sizeof(fsk_max_single_batch_fixed), fixture->device)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_max_single_batch_fixed, sizeof(fsk_max_single_batch_fixed), fixture)); xSemaphoreTake(fixture->tx_done, TIMEOUT); } @@ -240,9 +235,8 @@ TEST_CASE("sx127x_test_fsk_tx_fixed_max", "[fsk]") { for (int i = 0; i < FIXED_MAX_LENGTH; i++) { expected[i] = (i % 10); } - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(expected, FIXED_MAX_LENGTH, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(expected, FIXED_MAX_LENGTH, fixture)); free(expected); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); xSemaphoreTake(fixture->tx_done, TIMEOUT); } @@ -266,8 +260,7 @@ TEST_CASE("sx127x_test_fsk_tx_max_baud", "[fsk]") { TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_set_fdev(100000.0, fixture->device)); setup_gpio_interrupts((gpio_num_t) tx_fixture_config.dio1, fixture, GPIO_INTR_NEGEDGE); sx127x_tx_set_callback(tx_callback, fixture->device); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture->device)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture)); xSemaphoreTake(fixture->tx_done, TIMEOUT); } @@ -289,8 +282,7 @@ TEST_CASE("sx127x_test_fsk_tx_filtered", "[fsk]") { if (i != 0) { vTaskDelay(pdMS_TO_TICKS(50)); } - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission_with_address(fsk_small_message, sizeof(fsk_small_message), addresses[i], fixture->device)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission_with_address(fsk_small_message, sizeof(fsk_small_message), addresses[i], fixture)); xSemaphoreTake(fixture->tx_done, TIMEOUT); } } @@ -401,11 +393,11 @@ TEST_CASE("sx127x_test_ook_tx_variable_length", "[ook]") { TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_create(&tx_fixture_config, SX127x_MODULATION_OOK, &fixture)); setup_gpio_interrupts((gpio_num_t) tx_fixture_config.dio1, fixture, GPIO_INTR_NEGEDGE); sx127x_tx_set_callback(tx_callback, fixture->device); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_small_message, sizeof(fsk_small_message), fixture)); TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_OOK, fixture->device)); xSemaphoreTake(fixture->tx_done, TIMEOUT); vTaskDelay(pdMS_TO_TICKS(50)); - TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fsk_ook_tx_set_for_transmission(fsk_max_single_batch, sizeof(fsk_max_single_batch), fixture->device)); + TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_fixture_fsk_ook_tx_set_for_transmission(fsk_max_single_batch, sizeof(fsk_max_single_batch), fixture)); TEST_ASSERT_EQUAL_INT(SX127X_OK, sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_OOK, fixture->device)); xSemaphoreTake(fixture->tx_done, TIMEOUT); } diff --git a/test/test_app/main/sx127x_fixture.c b/test/test_app/main/sx127x_fixture.c index 254bdae..24f5fd8 100644 --- a/test/test_app/main/sx127x_fixture.c +++ b/test/test_app/main/sx127x_fixture.c @@ -17,20 +17,22 @@ } while (0) static const char *TAG = "sx127x_test"; -static SemaphoreHandle_t xBinarySemaphore; +const UBaseType_t xArrayIndex = 0; void IRAM_ATTR handle_interrupt_fromisr(void *arg) { + sx127x_fixture_t *fixture = (sx127x_fixture_t *) arg; BaseType_t xHigherPriorityTaskWoken = pdFALSE; - xSemaphoreGiveFromISR(xBinarySemaphore, &xHigherPriorityTaskWoken); - if (xHigherPriorityTaskWoken == pdTRUE) { - portYIELD_FROM_ISR(); - } + vTaskNotifyGiveIndexedFromISR(fixture->handle_interrupt, xArrayIndex, &xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } void handle_interrupt_task(void *arg) { + sx127x_fixture_t *fixture = (sx127x_fixture_t *) arg; while (1) { - if (xSemaphoreTake(xBinarySemaphore, portMAX_DELAY) == pdTRUE) { - sx127x_handle_interrupt((sx127x *) arg); + if (ulTaskNotifyTakeIndexed(xArrayIndex, pdTRUE, portMAX_DELAY) > 0) { + xSemaphoreTake(fixture->xMutex, portMAX_DELAY); + sx127x_handle_interrupt(fixture->device); + xSemaphoreGive(fixture->xMutex); } } } @@ -64,7 +66,7 @@ int sx127x_fixture_create_base(sx127x_fixture_config_t *config, sx127x_fixture_t .mode = 0}; spi_device_handle_t spi_device; sx127x *device = malloc(sizeof(struct sx127x_t)); - if( device == NULL ) { + if (device == NULL) { return -1; } ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &spi_device)); @@ -144,14 +146,14 @@ int sx127x_fixture_create(sx127x_fixture_config_t *config, sx127x_modulation_t m return SX127X_ERR_INVALID_ARG; } - xBinarySemaphore = xSemaphoreCreateBinary(); - if (xBinarySemaphore == NULL) { + result->xMutex = xSemaphoreCreateMutex(); + if (result->xMutex == NULL) { ESP_LOGE(TAG, "unable to create semaphore"); sx127x_fixture_destroy(result); return SX127X_ERR_INVALID_ARG; } - BaseType_t task_code = xTaskCreatePinnedToCore(handle_interrupt_task, "handle interrupt", 8196, result->device, 2, &(result->handle_interrupt), xPortGetCoreID()); + BaseType_t task_code = xTaskCreatePinnedToCore(handle_interrupt_task, "handle interrupt", 8196, result, 2, &(result->handle_interrupt), xPortGetCoreID()); if (task_code != pdPASS) { ESP_LOGE(TAG, "can't create task %d", task_code); sx127x_fixture_destroy(result); @@ -166,6 +168,30 @@ int sx127x_fixture_create(sx127x_fixture_config_t *config, sx127x_modulation_t m return SX127X_OK; } +int sx127x_fixture_fsk_ook_tx_set_for_transmission(const uint8_t *data, uint16_t data_length, sx127x_fixture_t *fixture) { + xSemaphoreTake(fixture->xMutex, portMAX_DELAY); + int code = sx127x_fsk_ook_tx_set_for_transmission(data, data_length, fixture->device); + if (code != SX127X_OK) { + xSemaphoreGive(fixture->xMutex); + return code; + } + code = sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device); + xSemaphoreGive(fixture->xMutex); + return code; +} + +int sx127x_fixture_fsk_ook_tx_set_for_transmission_with_address(const uint8_t *data, uint16_t data_length, uint8_t address_to, sx127x_fixture_t *fixture) { + xSemaphoreTake(fixture->xMutex, portMAX_DELAY); + int code = sx127x_fsk_ook_tx_set_for_transmission_with_address(data, data_length, address_to, fixture->device); + if (code != SX127X_OK) { + xSemaphoreGive(fixture->xMutex); + return code; + } + code = sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, fixture->device); + xSemaphoreGive(fixture->xMutex); + return code; +} + void sx127x_fixture_destroy(sx127x_fixture_t *fixture) { if (fixture == NULL) { return; @@ -188,5 +214,8 @@ void sx127x_fixture_destroy(sx127x_fixture_t *fixture) { if (fixture->rx_done != NULL) { vSemaphoreDelete(fixture->rx_done); } + if (fixture->xMutex != NULL) { + vSemaphoreDelete(fixture->xMutex); + } free(fixture); } \ No newline at end of file diff --git a/test/test_app/main/sx127x_fixture.h b/test/test_app/main/sx127x_fixture.h index f5fd92b..3e60bbf 100644 --- a/test/test_app/main/sx127x_fixture.h +++ b/test/test_app/main/sx127x_fixture.h @@ -26,6 +26,7 @@ typedef struct { spi_device_handle_t spi_device; SemaphoreHandle_t tx_done; SemaphoreHandle_t rx_done; + SemaphoreHandle_t xMutex; TaskHandle_t handle_interrupt; uint8_t rx_data[2048]; @@ -36,6 +37,10 @@ int sx127x_fixture_create(sx127x_fixture_config_t *config, sx127x_modulation_t m int sx127x_fixture_create_base(sx127x_fixture_config_t *config, sx127x_fixture_t **fixture); +int sx127x_fixture_fsk_ook_tx_set_for_transmission(const uint8_t *data, uint16_t data_length, sx127x_fixture_t *fixture); + +int sx127x_fixture_fsk_ook_tx_set_for_transmission_with_address(const uint8_t *data, uint16_t data_length, uint8_t address_to, sx127x_fixture_t *fixture); + void sx127x_fixture_destroy(sx127x_fixture_t *fixture); void setup_gpio_interrupts(gpio_num_t gpio, sx127x_fixture_t *fixture, gpio_int_type_t type);