Skip to content

Commit

Permalink
optimize performances
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Dec 10, 2024
1 parent bd34a75 commit 82e93af
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
28 changes: 10 additions & 18 deletions test/test_app/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
Expand Down
51 changes: 40 additions & 11 deletions test/test_app/main/sx127x_fixture.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
5 changes: 5 additions & 0 deletions test/test_app/main/sx127x_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand Down

0 comments on commit 82e93af

Please sign in to comment.