Merge branch 'refactor/esp_ringbuf_linux' into 'master'

refactor(esp_ringbuf): Adjusted unit tests so some of them run on Linux

Closes IDF-9369

See merge request espressif/esp-idf!32683
This commit is contained in:
Jakob Hasse 2024-08-15 18:29:11 +08:00
commit deb18eb487
8 changed files with 319 additions and 231 deletions

View File

@ -2,12 +2,8 @@
components/esp_ringbuf/test_apps:
enable:
- if: IDF_TARGET in ["esp32", "esp32c3", "esp32s2"]
- if: IDF_TARGET in ["esp32", "esp32c3", "esp32s2", "linux"]
reason: covers all target types
disable_test:
- if: IDF_TARGET in ["esp32s2"]
temporary: true
reason: the other targets are not tested yet
depends_components:
- freertos
- esp_ringbuf

View File

@ -1,3 +1,3 @@
| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 |
| ----------------- | ----- | -------- | -------- |
| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | Linux |
| ----------------- | ----- | -------- | -------- | ----- |

View File

@ -1,6 +1,15 @@
idf_build_get_property(target IDF_TARGET)
set(srcs "test_ringbuf_main.c"
"test_ringbuf.c")
"test_ringbuf_common.c")
set(priv_requires esp_ringbuf spi_flash unity)
if(NOT ${target} STREQUAL "linux")
list(APPEND srcs "test_ringbuf_target.c")
list(APPEND priv_requires esp_driver_gptimer)
endif()
idf_component_register(SRCS ${srcs}
PRIV_REQUIRES esp_ringbuf esp_driver_gptimer spi_flash unity
PRIV_REQUIRES ${priv_requires}
WHOLE_ARCHIVE)

View File

@ -0,0 +1,53 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "freertos/ringbuf.h"
//Definitions used in multiple test cases
#define NO_OF_RB_TYPES 3
#define SMALL_ITEM_SIZE 8
#define ITEM_HDR_SIZE (sizeof(size_t) + sizeof(UBaseType_t))
#define MEDIUM_ITEM_SIZE ((3 * SMALL_ITEM_SIZE) >> 1) //12 bytes
#define BUFFER_SIZE 4 * (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE) //4Byte aligned size
#define CONT_DATA_LEN continuous_test_string_len()
//32-bit aligned size that guarantees a wrap around at some point
#define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
typedef struct {
RingbufHandle_t buffer;
RingbufferType_t type;
} task_args_t;
extern const uint8_t small_item[SMALL_ITEM_SIZE];
extern RingbufHandle_t buffer_handles[NO_OF_RB_TYPES];
extern SemaphoreHandle_t done_sem;
extern SemaphoreHandle_t tasks_done;
extern SemaphoreHandle_t tx_done;
extern SemaphoreHandle_t rx_done;
void setup(void);
void cleanup(void);
void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size);
void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size);
void send_item_and_check(RingbufHandle_t handle, const uint8_t *item, size_t item_size, TickType_t ticks_to_wait, bool in_isr);
void receive_check_and_return_item_no_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr);
void receive_check_and_return_item_allow_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr);
void receive_check_and_return_item_byte_buffer(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr);
void send_task(void *args);
void rec_task(void *args);
size_t continuous_test_string_len(void);

View File

@ -1,9 +1,14 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* This file contains all esp_ringbuf unit tests which run on both
* the chip target as well as on the Linux target.
*/
#include "sdkconfig.h"
#include <stdio.h>
#include <stdlib.h>
@ -12,31 +17,23 @@
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/ringbuf.h"
#include "driver/gptimer.h"
#include "esp_private/spi_flash_os.h"
#include "esp_memory_utils.h"
#include "esp_heap_caps.h"
#include "spi_flash_mmap.h"
#include "unity.h"
#include "esp_rom_sys.h"
#include "test_functions.h"
//Definitions used in multiple test cases
#define TIMEOUT_TICKS 10
#define NO_OF_RB_TYPES 3
#define ITEM_HDR_SIZE 8
#define SMALL_ITEM_SIZE 8
#define MEDIUM_ITEM_SIZE ((3 * SMALL_ITEM_SIZE) >> 1) //12 bytes
#define LARGE_ITEM_SIZE (2 * SMALL_ITEM_SIZE) //16 bytes
#define BUFFER_SIZE 160 //4Byte aligned size
static const uint8_t small_item[SMALL_ITEM_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
const uint8_t small_item[SMALL_ITEM_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
static const uint8_t large_item[LARGE_ITEM_SIZE] = { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
};
static RingbufHandle_t buffer_handles[NO_OF_RB_TYPES];
static SemaphoreHandle_t done_sem;
RingbufHandle_t buffer_handles[NO_OF_RB_TYPES];
SemaphoreHandle_t done_sem;
static void send_item_and_check(RingbufHandle_t handle, const uint8_t *item, size_t item_size, TickType_t ticks_to_wait, bool in_isr)
void send_item_and_check(RingbufHandle_t handle, const uint8_t *item, size_t item_size, TickType_t ticks_to_wait, bool in_isr)
{
BaseType_t ret;
if (in_isr) {
@ -58,7 +55,7 @@ static void send_item_and_check_failure(RingbufHandle_t handle, const uint8_t *i
TEST_ASSERT_MESSAGE(ret == pdFALSE, "Sent an item to a full buffer");
}
static void receive_check_and_return_item_no_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
void receive_check_and_return_item_no_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
{
//Receive item from no-split buffer
size_t item_size;
@ -83,7 +80,7 @@ static void receive_check_and_return_item_no_split(RingbufHandle_t handle, const
}
static void receive_check_and_return_item_allow_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
void receive_check_and_return_item_allow_split(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
{
//Receive item
size_t item_size1, item_size2;
@ -129,7 +126,7 @@ static void receive_check_and_return_item_allow_split(RingbufHandle_t handle, co
}
}
static void receive_check_and_return_item_byte_buffer(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
void receive_check_and_return_item_byte_buffer(RingbufHandle_t handle, const uint8_t *expected_data, size_t expected_size, TickType_t ticks_to_wait, bool in_isr)
{
//Receive item
size_t item_size;
@ -200,7 +197,7 @@ static void receive_check_and_return_item_byte_buffer(RingbufHandle_t handle, co
* 4) Receive and check the sent items
*/
TEST_CASE("TC#1: No-Split", "[esp_ringbuf]")
TEST_CASE("TC#1: No-Split", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
@ -246,7 +243,7 @@ TEST_CASE("TC#1: No-Split", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#2: No-Split", "[esp_ringbuf]")
TEST_CASE("TC#2: No-Split", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
@ -294,7 +291,7 @@ TEST_CASE("TC#2: No-Split", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#3: No-Split", "[esp_ringbuf]")
TEST_CASE("TC#3: No-Split", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
@ -342,7 +339,7 @@ TEST_CASE("TC#3: No-Split", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#1: Allow-Split", "[esp_ringbuf]")
TEST_CASE("TC#1: Allow-Split", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
@ -388,7 +385,7 @@ TEST_CASE("TC#1: Allow-Split", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#2: Allow-Split", "[esp_ringbuf]")
TEST_CASE("TC#2: Allow-Split", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
@ -436,7 +433,7 @@ TEST_CASE("TC#2: Allow-Split", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#3: Allow-Split", "[esp_ringbuf]")
TEST_CASE("TC#3: Allow-Split", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
@ -484,7 +481,7 @@ TEST_CASE("TC#3: Allow-Split", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#1: Byte buffer", "[esp_ringbuf]")
TEST_CASE("TC#1: Byte buffer", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
@ -530,7 +527,7 @@ TEST_CASE("TC#1: Byte buffer", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#2: Byte buffer", "[esp_ringbuf]")
TEST_CASE("TC#2: Byte buffer", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
@ -578,7 +575,7 @@ TEST_CASE("TC#2: Byte buffer", "[esp_ringbuf]")
vRingbufferDelete(buffer_handle);
}
TEST_CASE("TC#3: Byte buffer", "[esp_ringbuf]")
TEST_CASE("TC#3: Byte buffer", "[esp_ringbuf][linux]")
{
//Create buffer
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
@ -601,11 +598,11 @@ TEST_CASE("TC#3: Byte buffer", "[esp_ringbuf]")
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
TEST_ASSERT_MESSAGE(items_waiting == no_of_medium_items * MEDIUM_ITEM_SIZE, "Incorrect number of bytes waiting");
//The buffer should not have any free space for one small item.
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) < SMALL_ITEM_SIZE, "Buffer full not achieved");
//The buffer should not have any free space for another item.
TEST_ASSERT_LESS_THAN_MESSAGE(MEDIUM_ITEM_SIZE, xRingbufferGetCurFreeSize(buffer_handle), "Buffer full not achieved");
//Send an item. The item should not be sent to a full buffer.
send_item_and_check_failure(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
send_item_and_check_failure(buffer_handle, small_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
//Test receiving medium items
for (int i = 0; i < no_of_medium_items; i++) {
@ -666,7 +663,7 @@ static void queue_set_receiving_task(void *queue_set_handle)
vTaskDelete(NULL);
}
TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf]")
TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf][linux]")
{
QueueSetHandle_t queue_set = xQueueCreateSet(NO_OF_RB_TYPES);
done_sem = xSemaphoreCreateBinary();
@ -699,105 +696,6 @@ TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf]")
vTaskDelay(1);
}
/* -------------------------- Test ring buffer ISR -----------------------------
* The following test case tests ring buffer ISR API. A timer is used to trigger
* the ISR. The test case will do the following
* 1) ISR will be triggered periodically by timer
* 2) The ISR will iterate through all ring buffer types where each iteration
* will send then receive an item to a ring buffer.
*/
#define ISR_ITERATIONS ((BUFFER_SIZE / SMALL_ITEM_SIZE) * 2)
static int buf_type;
static int iterations;
static bool on_timer_alarm(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
bool need_yield = false;
//Test sending to buffer from ISR from ISR
if (buf_type < NO_OF_RB_TYPES) {
send_item_and_check(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
}
//Receive item from ISR
if (buf_type == RINGBUF_TYPE_NOSPLIT) {
//Test receive from ISR for no-split buffer
receive_check_and_return_item_no_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
buf_type++;
} else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
//Test send from ISR to allow-split buffer
receive_check_and_return_item_allow_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
buf_type++;
} else if (buf_type == RINGBUF_TYPE_BYTEBUF) {
//Test receive from ISR for byte buffer
receive_check_and_return_item_byte_buffer(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
buf_type++;
} else if (buf_type == NO_OF_RB_TYPES) {
//Check if all iterations complete
if (iterations < ISR_ITERATIONS) {
iterations++;
buf_type = 0; //Reset and iterate through each buffer type again
goto out;
} else {
//Signal complete
BaseType_t task_woken = pdFALSE;
xSemaphoreGiveFromISR(done_sem, &task_woken);
if (task_woken == pdTRUE) {
buf_type++;
need_yield = true;
}
}
}
out:
return need_yield;
}
// IDF-6471 - test hangs up on QEMU
TEST_CASE("Test ring buffer ISR", "[esp_ringbuf][qemu-ignore]")
{
gptimer_handle_t gptimer;
for (int i = 0; i < NO_OF_RB_TYPES; i++) {
buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
}
done_sem = xSemaphoreCreateBinary();
buf_type = 0;
iterations = 0;
//Setup timer for ISR
gptimer_config_t config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000,
};
TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
gptimer_alarm_config_t alarm_config = {
.reload_count = 0,
.alarm_count = 2000,
.flags.auto_reload_on_alarm = true,
};
gptimer_event_callbacks_t cbs = {
.on_alarm = on_timer_alarm,
};
TEST_ESP_OK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
TEST_ESP_OK(gptimer_enable(gptimer));
TEST_ESP_OK(gptimer_start(gptimer));
//Wait for ISR to complete multiple iterations
xSemaphoreTake(done_sem, portMAX_DELAY);
//Cleanup
TEST_ESP_OK(gptimer_stop(gptimer));
TEST_ESP_OK(gptimer_disable(gptimer));
TEST_ESP_OK(gptimer_del_timer(gptimer));
vSemaphoreDelete(done_sem);
for (int i = 0; i < NO_OF_RB_TYPES; i++) {
vRingbufferDelete(buffer_handles[i]);
}
}
/* ---------------------------- Test ring buffer SMP ---------------------------
* The following test case tests each type of ring buffer in an SMP fashion. A
* sending task and a receiving task is created. The sending task will split
@ -816,20 +714,16 @@ static const char continuous_data[] = {"A_very_long_string_that_will_be_split_in
"be_increased_over_multiple_iterations_in_this"
"_test"
};
#define CONT_DATA_LEN sizeof(continuous_data)
//32-bit aligned size that guarantees a wrap around at some point
#define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
SemaphoreHandle_t tasks_done;
SemaphoreHandle_t tx_done;
SemaphoreHandle_t rx_done;
typedef struct {
RingbufHandle_t buffer;
RingbufferType_t type;
} task_args_t;
size_t continuous_test_string_len(void)
{
return sizeof(continuous_data);
}
static SemaphoreHandle_t tasks_done;
static SemaphoreHandle_t tx_done;
static SemaphoreHandle_t rx_done;
static void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
{
for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
size_t bytes_sent = 0; //Number of data bytes sent in this iteration
@ -851,7 +745,7 @@ static void send_to_buffer(RingbufHandle_t buffer, size_t max_item_size)
}
}
static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
{
for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
size_t bytes_rec = 0; //Number of data bytes received in this iteration
@ -895,7 +789,7 @@ static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type,
}
}
static void send_task(void *args)
void send_task(void *args)
{
RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
size_t max_item_len = xRingbufferGetMaxItemSize(buffer);
@ -909,7 +803,7 @@ static void send_task(void *args)
vTaskDelete(NULL);
}
static void rec_task(void *args)
void rec_task(void *args)
{
RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
size_t max_rec_len = xRingbufferGetMaxItemSize(buffer);
@ -925,7 +819,7 @@ static void rec_task(void *args)
vTaskDelete(NULL);
}
static void setup(void)
void setup(void)
{
esp_rom_printf("Size of test data: %d\n", CONT_DATA_LEN);
tx_done = xSemaphoreCreateBinary(); //Semaphore to indicate send is done for a particular iteration
@ -934,7 +828,7 @@ static void setup(void)
srand(SRAND_SEED); //Seed RNG
}
static void cleanup(void)
void cleanup(void)
{
//Cleanup
vSemaphoreDelete(tx_done);
@ -942,7 +836,7 @@ static void cleanup(void)
vSemaphoreDelete(tasks_done);
}
TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
TEST_CASE("Test ring buffer SMP", "[esp_ringbuf][linux]")
{
setup();
//Iterate through buffer types (No split, split, then byte buff)
@ -974,7 +868,7 @@ TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
}
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf][linux]")
{
setup();
//Iterate through buffer types (No split, split, then byte buff)
@ -1022,40 +916,12 @@ TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
}
#endif
#if !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH
/* -------------------------- Test ring buffer IRAM ------------------------- */
static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
{
bool result = true;
uint8_t item[4];
size_t item_size;
RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
result = result && (handle != NULL);
spi_flash_guard_get()->start(); // Disables flash cache
xRingbufferGetMaxItemSize(handle);
xRingbufferSendFromISR(handle, (void *)item, sizeof(item), NULL);
xRingbufferReceiveFromISR(handle, &item_size);
spi_flash_guard_get()->end(); // Re-enables flash cache
vRingbufferDelete(handle);
return result;
}
TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
{
TEST_ASSERT(iram_ringbuf_test());
}
#endif /* !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH */
/* ------------------------ Test ring buffer 0 Item Size -----------------------
* The following test case tests that sending/acquiring an item/bytes of 0 size
* is permissible.
*/
TEST_CASE("Test ringbuffer 0 item size", "[esp_ringbuf]")
TEST_CASE("Test ringbuffer 0 item size", "[esp_ringbuf][linux]")
{
RingbufHandle_t no_split_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
RingbufHandle_t allow_split_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
@ -1095,31 +961,3 @@ TEST_CASE("Test ringbuffer 0 item size", "[esp_ringbuf]")
vRingbufferDelete(allow_split_rb);
vRingbufferDelete(byte_rb);
}
/* --------------------- Test ring buffer create with caps ---------------------
* The following test case tests ring buffer creation with caps. Specifically
* the following APIs:
*
* - xRingbufferCreateWithCaps()
* - vRingbufferDeleteWithCaps()
* - xRingbufferGetStaticBuffer()
*/
TEST_CASE("Test ringbuffer with caps", "[esp_ringbuf]")
{
RingbufHandle_t rb_handle;
uint8_t *rb_storage;
StaticRingbuffer_t *rb_obj;
// Create ring buffer with caps
rb_handle = xRingbufferCreateWithCaps(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT, (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
TEST_ASSERT_NOT_EQUAL(NULL, rb_handle);
// Get the ring buffer's memory
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferGetStaticBuffer(rb_handle, &rb_storage, &rb_obj));
TEST_ASSERT(esp_ptr_in_dram(rb_storage));
TEST_ASSERT(esp_ptr_in_dram(rb_obj));
// Free the ring buffer
vRingbufferDeleteWithCaps(rb_handle);
}

View File

@ -8,35 +8,23 @@
#include "freertos/task.h"
#include "unity.h"
#include "unity_test_runner.h"
#include "unity_test_utils_memory.h"
#include "esp_heap_caps.h"
// Some resources are lazy allocated in the sleep code, the threshold is left for that case
#define TEST_MEMORY_LEAK_THRESHOLD (-500)
static size_t before_free_8bit;
static size_t before_free_32bit;
static void check_leak(size_t before_free, size_t after_free, const char *type)
{
ssize_t delta = after_free - before_free;
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
}
void setUp(void)
{
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
unity_utils_set_leak_level(TEST_MEMORY_LEAK_THRESHOLD);
unity_utils_record_free_mem();
}
void tearDown(void)
{
// Add a short delay of 100ms to allow the idle task to free any remaining memory
vTaskDelay(pdMS_TO_TICKS(100));
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
check_leak(before_free_8bit, after_free_8bit, "8BIT");
check_leak(before_free_32bit, after_free_32bit, "32BIT");
unity_utils_evaluate_leaks();
}
void app_main(void)

View File

@ -0,0 +1,195 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* This file contains esp_ringbuf unit tests which run only on the chip target or QEMU,
* but NOT on the Linux target since they are too hardware-specific.
*/
#include "sdkconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/ringbuf.h"
#include "driver/gptimer.h"
#include "esp_private/spi_flash_os.h"
#include "esp_memory_utils.h"
#include "esp_heap_caps.h"
#include "unity.h"
#include "esp_rom_sys.h"
#include "test_functions.h"
/* -------------------------- Test ring buffer ISR -----------------------------
* The following test case tests ring buffer ISR API. A timer is used to trigger
* the ISR. The test case will do the following
* 1) ISR will be triggered periodically by timer
* 2) The ISR will iterate through all ring buffer types where each iteration
* will send then receive an item to a ring buffer.
*/
#define ISR_ITERATIONS ((BUFFER_SIZE / SMALL_ITEM_SIZE) * 2)
static int buf_type;
static int iterations;
static bool on_timer_alarm(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
bool need_yield = false;
//Test sending to buffer from ISR from ISR
if (buf_type < NO_OF_RB_TYPES) {
send_item_and_check(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
}
//Receive item from ISR
if (buf_type == RINGBUF_TYPE_NOSPLIT) {
//Test receive from ISR for no-split buffer
receive_check_and_return_item_no_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
buf_type++;
} else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
//Test send from ISR to allow-split buffer
receive_check_and_return_item_allow_split(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
buf_type++;
} else if (buf_type == RINGBUF_TYPE_BYTEBUF) {
//Test receive from ISR for byte buffer
receive_check_and_return_item_byte_buffer(buffer_handles[buf_type], (void *)small_item, SMALL_ITEM_SIZE, 0, true);
buf_type++;
} else if (buf_type == NO_OF_RB_TYPES) {
//Check if all iterations complete
if (iterations < ISR_ITERATIONS) {
iterations++;
buf_type = 0; //Reset and iterate through each buffer type again
goto out;
} else {
//Signal complete
BaseType_t task_woken = pdFALSE;
xSemaphoreGiveFromISR(done_sem, &task_woken);
if (task_woken == pdTRUE) {
buf_type++;
need_yield = true;
}
}
}
out:
return need_yield;
}
// IDF-6471 - test hangs up on QEMU
TEST_CASE("Test ring buffer ISR", "[esp_ringbuf][qemu-ignore]")
{
gptimer_handle_t gptimer;
for (int i = 0; i < NO_OF_RB_TYPES; i++) {
buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
}
done_sem = xSemaphoreCreateBinary();
buf_type = 0;
iterations = 0;
//Setup timer for ISR
gptimer_config_t config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000,
};
TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
gptimer_alarm_config_t alarm_config = {
.reload_count = 0,
.alarm_count = 2000,
.flags.auto_reload_on_alarm = true,
};
gptimer_event_callbacks_t cbs = {
.on_alarm = on_timer_alarm,
};
TEST_ESP_OK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
TEST_ESP_OK(gptimer_enable(gptimer));
TEST_ESP_OK(gptimer_start(gptimer));
//Wait for ISR to complete multiple iterations
xSemaphoreTake(done_sem, portMAX_DELAY);
//Cleanup
TEST_ESP_OK(gptimer_stop(gptimer));
TEST_ESP_OK(gptimer_disable(gptimer));
TEST_ESP_OK(gptimer_del_timer(gptimer));
vSemaphoreDelete(done_sem);
for (int i = 0; i < NO_OF_RB_TYPES; i++) {
vRingbufferDelete(buffer_handles[i]);
}
}
/* ---------------------------- Test ring buffer SMP ---------------------------
* The following test case tests each type of ring buffer in an SMP fashion. A
* sending task and a receiving task is created. The sending task will split
* a continuous piece of data into items of random length and send it to a ring
* buffer. The receiving task will receive and check those items.
* Every permutation of core pinning of the sending and receiving task will be
* tested.
*/
#if !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH
/* -------------------------- Test ring buffer IRAM ------------------------- */
static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
{
bool result = true;
uint8_t item[4];
size_t item_size;
RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
result = result && (handle != NULL);
spi_flash_guard_get()->start(); // Disables flash cache
xRingbufferGetMaxItemSize(handle);
xRingbufferSendFromISR(handle, (void *)item, sizeof(item), NULL);
xRingbufferReceiveFromISR(handle, &item_size);
spi_flash_guard_get()->end(); // Re-enables flash cache
vRingbufferDelete(handle);
return result;
}
TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
{
TEST_ASSERT(iram_ringbuf_test());
}
#endif /* !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH */
/* ------------------------ Test ring buffer 0 Item Size -----------------------
* The following test case tests that sending/acquiring an item/bytes of 0 size
* is permissible.
*/
/* --------------------- Test ring buffer create with caps ---------------------
* The following test case tests ring buffer creation with caps. Specifically
* the following APIs:
*
* - xRingbufferCreateWithCaps()
* - vRingbufferDeleteWithCaps()
* - xRingbufferGetStaticBuffer()
*/
TEST_CASE("Test ringbuffer with caps", "[esp_ringbuf]")
{
RingbufHandle_t rb_handle;
uint8_t *rb_storage;
StaticRingbuffer_t *rb_obj;
// Create ring buffer with caps
rb_handle = xRingbufferCreateWithCaps(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT, (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
TEST_ASSERT_NOT_EQUAL(NULL, rb_handle);
// Get the ring buffer's memory
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferGetStaticBuffer(rb_handle, &rb_storage, &rb_obj));
TEST_ASSERT(esp_ptr_in_dram(rb_storage));
TEST_ASSERT(esp_ptr_in_dram(rb_obj));
// Free the ring buffer
vRingbufferDeleteWithCaps(rb_handle);
}

View File

@ -5,6 +5,9 @@ from pytest_embedded import Dut
@pytest.mark.generic
@pytest.mark.esp32
@pytest.mark.esp32c3
@pytest.mark.esp32s2
@pytest.mark.parametrize('config', [
'default',
'ringbuf_flash',
@ -27,3 +30,9 @@ def test_esp_ringbuf_qemu(dut: Dut) -> None:
for case in dut.test_menu:
if 'qemu-ignore' not in case.groups and not case.is_ignored and case.type == 'normal':
dut._run_normal_case(case)
@pytest.mark.linux
@pytest.mark.host_test
def test_esp_ringbuf_posix_simulator(dut: Dut) -> None:
dut.run_all_single_board_cases(group='linux')