2021-09-28 04:06:55 -04:00
|
|
|
/*
|
2024-01-26 09:20:19 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-09-28 04:06:55 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2016-11-17 03:36:10 -05:00
|
|
|
#include <stdio.h>
|
2018-04-18 13:20:34 -04:00
|
|
|
#include <stdlib.h>
|
2016-11-17 03:36:10 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/queue.h"
|
2018-04-18 13:20:34 -04:00
|
|
|
#include "freertos/semphr.h"
|
2016-11-17 03:36:10 -05:00
|
|
|
#include "freertos/ringbuf.h"
|
2022-01-02 03:19:49 -05:00
|
|
|
#include "driver/gptimer.h"
|
2022-06-27 03:24:07 -04:00
|
|
|
#include "esp_private/spi_flash_os.h"
|
2023-06-24 09:30:53 -04:00
|
|
|
#include "esp_memory_utils.h"
|
2019-03-04 01:27:06 -05:00
|
|
|
#include "esp_heap_caps.h"
|
2022-06-27 03:24:07 -04:00
|
|
|
#include "spi_flash_mmap.h"
|
2016-11-17 03:36:10 -05:00
|
|
|
#include "unity.h"
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2018-04-18 13:20:34 -04:00
|
|
|
|
|
|
|
//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
|
2021-09-28 04:06:55 -04:00
|
|
|
#define MEDIUM_ITEM_SIZE ((3 * SMALL_ITEM_SIZE) >> 1) //12 bytes
|
|
|
|
#define LARGE_ITEM_SIZE (2 * SMALL_ITEM_SIZE) //16 bytes
|
2018-04-18 13:20:34 -04:00
|
|
|
#define BUFFER_SIZE 160 //4Byte aligned size
|
|
|
|
|
|
|
|
static 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,
|
2022-01-02 03:19:49 -05:00
|
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
|
|
|
|
};
|
2018-04-18 13:20:34 -04:00
|
|
|
static RingbufHandle_t buffer_handles[NO_OF_RB_TYPES];
|
|
|
|
static 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)
|
|
|
|
{
|
|
|
|
BaseType_t ret;
|
|
|
|
if (in_isr) {
|
|
|
|
ret = xRingbufferSendFromISR(handle, (void *)item, item_size, NULL);
|
|
|
|
} else {
|
|
|
|
ret = xRingbufferSend(handle, (void *)item, item_size, ticks_to_wait);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to send item");
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
static void send_item_and_check_failure(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) {
|
|
|
|
ret = xRingbufferSendFromISR(handle, (void *)item, item_size, NULL);
|
|
|
|
} else {
|
|
|
|
ret = xRingbufferSend(handle, (void *)item, item_size, ticks_to_wait);
|
|
|
|
}
|
|
|
|
TEST_ASSERT_MESSAGE(ret == pdFALSE, "Sent an item to a full buffer");
|
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
//Receive item from no-split buffer
|
|
|
|
size_t item_size;
|
|
|
|
uint8_t *item;
|
|
|
|
if (in_isr) {
|
|
|
|
item = (uint8_t *)xRingbufferReceiveFromISR(handle, &item_size);
|
|
|
|
} else {
|
|
|
|
item = (uint8_t *)xRingbufferReceive(handle, &item_size, ticks_to_wait);
|
|
|
|
}
|
|
|
|
TEST_ASSERT_MESSAGE(item != NULL, "Failed to receive item");
|
|
|
|
TEST_ASSERT_MESSAGE(item_size == expected_size, "Item size is incorrect");
|
|
|
|
//Check data of received item
|
|
|
|
for (int i = 0; i < item_size; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(item[i] == expected_data[i], "Item data is invalid");
|
|
|
|
}
|
|
|
|
//Return item
|
|
|
|
if (in_isr) {
|
|
|
|
vRingbufferReturnItemFromISR(handle, (void *)item, NULL);
|
|
|
|
} else {
|
|
|
|
vRingbufferReturnItem(handle, (void *)item);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
//Receive item
|
|
|
|
size_t item_size1, item_size2;
|
|
|
|
uint8_t *item1, *item2;
|
|
|
|
BaseType_t ret;
|
|
|
|
if (in_isr) {
|
2022-01-02 03:19:49 -05:00
|
|
|
ret = xRingbufferReceiveSplitFromISR(handle, (void **)&item1, (void **)&item2, &item_size1, &item_size2);
|
2018-04-18 13:20:34 -04:00
|
|
|
} else {
|
2022-01-02 03:19:49 -05:00
|
|
|
ret = xRingbufferReceiveSplit(handle, (void **)&item1, (void **)&item2, &item_size1, &item_size2, ticks_to_wait);
|
2018-04-18 13:20:34 -04:00
|
|
|
}
|
|
|
|
TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive item");
|
|
|
|
TEST_ASSERT_MESSAGE(item1 != NULL, "Failed to receive item");
|
|
|
|
|
|
|
|
//Check data of received item(s) and return them
|
|
|
|
if (item2 == NULL) {
|
|
|
|
TEST_ASSERT_MESSAGE(item_size1 == expected_size, "Item size is incorrect");
|
|
|
|
for (int i = 0; i < item_size1; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(item1[i] == expected_data[i], "Item data is invalid");
|
|
|
|
}
|
|
|
|
//Return item
|
|
|
|
if (in_isr) {
|
|
|
|
vRingbufferReturnItemFromISR(handle, (void *)item1, NULL);
|
|
|
|
} else {
|
|
|
|
vRingbufferReturnItem(handle, (void *)item1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Item was split
|
|
|
|
TEST_ASSERT_MESSAGE(item_size1 + item_size2 == expected_size, "Total item size is incorrect");
|
|
|
|
for (int i = 0; i < item_size1; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(item1[i] == expected_data[i], "Head item data is invalid");
|
|
|
|
}
|
|
|
|
for (int i = 0; i < item_size2; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(item2[i] == expected_data[item_size1 + i], "Head item data is invalid");
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
//Return Items
|
|
|
|
if (in_isr) {
|
|
|
|
vRingbufferReturnItemFromISR(handle, (void *)item1, NULL);
|
|
|
|
vRingbufferReturnItemFromISR(handle, (void *)item2, NULL);
|
|
|
|
} else {
|
|
|
|
vRingbufferReturnItem(handle, (void *)item1);
|
|
|
|
vRingbufferReturnItem(handle, (void *)item2);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
//Receive item
|
|
|
|
size_t item_size;
|
|
|
|
uint8_t *item;
|
|
|
|
if (in_isr) {
|
|
|
|
item = (uint8_t *)xRingbufferReceiveUpToFromISR(handle, &item_size, expected_size);
|
|
|
|
} else {
|
|
|
|
item = (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size, ticks_to_wait, expected_size); //Limit amount of bytes returned to the size of one item
|
|
|
|
}
|
|
|
|
TEST_ASSERT_MESSAGE(item != NULL, "Failed to receive item");
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Check data of received item
|
|
|
|
for (int i = 0; i < item_size; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(item[i] == expected_data[i], "Item data is invalid");
|
|
|
|
}
|
|
|
|
//Return item
|
|
|
|
if (in_isr) {
|
|
|
|
vRingbufferReturnItemFromISR(handle, (void *)item, NULL);
|
|
|
|
} else {
|
|
|
|
vRingbufferReturnItem(handle, (void *)item);
|
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Check if item wrapped around
|
|
|
|
if (item_size < expected_size) {
|
|
|
|
//Item is wrapped, receive second portion
|
|
|
|
size_t item_size2;
|
|
|
|
uint8_t *item2;
|
|
|
|
if (in_isr) {
|
|
|
|
item2 = (uint8_t *)xRingbufferReceiveUpToFromISR(handle, &item_size2, expected_size - item_size);
|
2016-11-17 03:36:10 -05:00
|
|
|
} else {
|
2018-04-18 13:20:34 -04:00
|
|
|
item2 = (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size2, ticks_to_wait, expected_size - item_size);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
//= (uint8_t *)xRingbufferReceiveUpTo(handle, &item_size2, ticks_to_wait, expected_size - item_size);
|
|
|
|
TEST_ASSERT_MESSAGE(item2 != NULL, "Failed to receive item");
|
|
|
|
TEST_ASSERT_MESSAGE(item_size + item_size2 == expected_size, "Total item size is incorrect");
|
|
|
|
for (int i = 0; i < item_size2; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(item2[i] == expected_data[item_size + i], "Item data is invalid");
|
|
|
|
}
|
|
|
|
if (in_isr) {
|
|
|
|
vRingbufferReturnItemFromISR(handle, (void *)item2, NULL);
|
|
|
|
} else {
|
|
|
|
vRingbufferReturnItem(handle, (void *)item2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT_MESSAGE(item_size == expected_size, "Item size is incorrect");
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------- Basic ring buffer behavior tests cases --------------------
|
2021-09-28 04:06:55 -04:00
|
|
|
* The following set of test cases will test basic send, receive, wrap around and buffer full
|
|
|
|
* behavior of each type of ring buffer.
|
|
|
|
* Test Case 1:
|
|
|
|
* 1) Send multiple items (nearly fill the buffer)
|
|
|
|
* 2) Receive and check the sent items (also prepares the buffer for a wrap around)
|
|
|
|
* 3) Send a final item that causes a wrap around
|
|
|
|
* 4) Receive and check the wrapped item
|
|
|
|
*
|
|
|
|
* Test Case 2:
|
|
|
|
* 1) Send multiple items (completely fill the buffer)
|
|
|
|
* 2) Send another item and verify that send failure occurs
|
|
|
|
* 3) Receive one item and verify that space is freed up in the buffer
|
|
|
|
* 4) Receive and check the remaining sent items
|
|
|
|
*
|
|
|
|
* Test Case 3:
|
|
|
|
* 1) Send multiple items (nearly fill the buffer)
|
|
|
|
* 2) Send another small sized item to fill the buffer without causing a wrap around (not needed in case of byte buffer)
|
|
|
|
* 2) Send another item and verify that send failure occurs
|
|
|
|
* 4) Receive and check the sent items
|
2018-04-18 13:20:34 -04:00
|
|
|
*/
|
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
TEST_CASE("TC#1: No-Split", "[esp_ringbuf]")
|
2018-04-18 13:20:34 -04:00
|
|
|
{
|
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
2021-09-28 04:06:55 -04:00
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE/2 - ITEM_HDR_SIZE.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect max item size received");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
|
|
|
|
int no_of_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + SMALL_ITEM_SIZE)) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
2021-09-28 04:06:55 -04:00
|
|
|
|
|
|
|
//Verify items waiting matches with the number of items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Test receiving items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Write pointer should be near the end, test wrap around
|
2020-12-30 03:10:37 -05:00
|
|
|
UBaseType_t write_pos_before, write_pos_after;
|
2019-06-14 02:10:48 -04:00
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
|
2018-04-18 13:20:34 -04:00
|
|
|
//Send large item that causes wrap around
|
|
|
|
send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
//Receive wrapped item
|
|
|
|
receive_check_and_return_item_no_split(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
|
2019-06-14 02:10:48 -04:00
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
|
2018-04-18 13:20:34 -04:00
|
|
|
TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
TEST_CASE("TC#2: No-Split", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE/2 - ITEM_HDR_SIZE.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect max item size received");
|
|
|
|
|
|
|
|
//Calculate number of items to send. Aim to fill the buffer
|
|
|
|
int no_of_items = (BUFFER_SIZE) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify items waiting matches with the number of items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//At this point, the buffer should be full.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "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);
|
|
|
|
|
|
|
|
//Receive one item
|
|
|
|
receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
|
|
|
|
//At this point, the buffer should not be full any more
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) > 0, "Buffer should have free space");
|
|
|
|
|
|
|
|
//Test receiving remaining items
|
|
|
|
for (int i = 0; i < no_of_items - 1; i++) {
|
|
|
|
receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("TC#3: No-Split", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE/2 - ITEM_HDR_SIZE.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == ((BUFFER_SIZE >> 1) - ITEM_HDR_SIZE), "Incorrect max item size received");
|
|
|
|
|
|
|
|
//Calculate number of medium items to send. Aim to almost fill the buffer
|
|
|
|
int no_of_medium_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE)) / (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE);
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_medium_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify items waiting matches with the number of medium items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_medium_items, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Send one small sized item. This will ensure that the item fits at the end of the buffer without causing the write pointer to wrap around.
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
|
|
|
|
//The buffer should not have any free space as the number of bytes remaining should be < ITEM_HDR_SIZE.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "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);
|
|
|
|
|
|
|
|
//Test receiving medium items
|
|
|
|
for (int i = 0; i < no_of_medium_items; i++) {
|
|
|
|
receive_check_and_return_item_no_split(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Test receiving small item
|
|
|
|
receive_check_and_return_item_no_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
|
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("TC#1: Allow-Split", "[esp_ringbuf]")
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
2018-04-18 13:20:34 -04:00
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
2021-09-28 04:06:55 -04:00
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE - (ITEM_HDR_SIZE * 2).
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect max item size received");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
|
|
|
|
int no_of_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + SMALL_ITEM_SIZE)) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
2021-09-28 04:06:55 -04:00
|
|
|
|
|
|
|
//Verify items waiting matches with the number of items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Test receiving items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Write pointer should be near the end, test wrap around
|
2020-12-30 03:10:37 -05:00
|
|
|
UBaseType_t write_pos_before, write_pos_after;
|
2019-06-14 02:10:48 -04:00
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
|
2018-04-18 13:20:34 -04:00
|
|
|
//Send large item that causes wrap around
|
|
|
|
send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
//Receive wrapped item
|
|
|
|
receive_check_and_return_item_allow_split(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
|
2019-06-14 02:10:48 -04:00
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
|
2018-04-18 13:20:34 -04:00
|
|
|
TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
2017-10-18 09:09:53 -04:00
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
TEST_CASE("TC#2: Allow-Split", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE - (ITEM_HDR_SIZE * 2).
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect max item size received");
|
|
|
|
|
|
|
|
//Calculate number of items to send. Aim to fill the buffer
|
|
|
|
int no_of_items = (BUFFER_SIZE) / (ITEM_HDR_SIZE + SMALL_ITEM_SIZE);
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify items waiting matches with the number of items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_items, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//At this point, the buffer should be full.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "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);
|
|
|
|
|
|
|
|
//Receive one item
|
|
|
|
receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
|
|
|
|
//At this point, the buffer should not be full any more
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) > 0, "Buffer should have free space");
|
|
|
|
|
|
|
|
//Test receiving remaining items
|
|
|
|
for (int i = 0; i < no_of_items - 1; i++) {
|
|
|
|
receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("TC#3: Allow-Split", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE - (ITEM_HDR_SIZE * 2).
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == (BUFFER_SIZE - (ITEM_HDR_SIZE * 2)), "Incorrect max item size received");
|
|
|
|
|
|
|
|
//Calculate number of medium items to send. Aim to almost fill the buffer
|
|
|
|
int no_of_medium_items = (BUFFER_SIZE - (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE)) / (ITEM_HDR_SIZE + MEDIUM_ITEM_SIZE);
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_medium_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify items waiting matches with the number of medium items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_medium_items, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Send one small sized item. This will ensure that the item fits at the end of the buffer without causing the write pointer to wrap around.
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
|
|
|
|
//The buffer should not have any free space as the number of bytes remaining should be < ITEM_HDR_SIZE.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "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);
|
|
|
|
|
|
|
|
//Test receiving medium items
|
|
|
|
for (int i = 0; i < no_of_medium_items; i++) {
|
|
|
|
receive_check_and_return_item_allow_split(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Test receiving small item
|
|
|
|
receive_check_and_return_item_allow_split(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
|
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("TC#1: Byte buffer", "[esp_ringbuf]")
|
2017-10-18 09:09:53 -04:00
|
|
|
{
|
2018-04-18 13:20:34 -04:00
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
2021-09-28 04:06:55 -04:00
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == BUFFER_SIZE, "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == BUFFER_SIZE, "Incorrect max item size received");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Calculate number of items to send. Aim to almost fill buffer to setup for wrap around
|
|
|
|
int no_of_items = (BUFFER_SIZE - SMALL_ITEM_SIZE) / SMALL_ITEM_SIZE;
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
2021-09-28 04:06:55 -04:00
|
|
|
|
|
|
|
//Verify items waiting matches with the number of items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_items * SMALL_ITEM_SIZE, "Incorrect number of bytes waiting");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Test receiving items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
receive_check_and_return_item_byte_buffer(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect number of bytes waiting");
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Write pointer should be near the end, test wrap around
|
2020-12-30 03:10:37 -05:00
|
|
|
UBaseType_t write_pos_before, write_pos_after;
|
2019-06-14 02:10:48 -04:00
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_before, NULL, NULL);
|
2018-04-18 13:20:34 -04:00
|
|
|
//Send large item that causes wrap around
|
|
|
|
send_item_and_check(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
//Receive wrapped item
|
|
|
|
receive_check_and_return_item_byte_buffer(buffer_handle, large_item, LARGE_ITEM_SIZE, TIMEOUT_TICKS, false);
|
2019-06-14 02:10:48 -04:00
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, &write_pos_after, NULL, NULL);
|
2018-04-18 13:20:34 -04:00
|
|
|
TEST_ASSERT_MESSAGE(write_pos_after < write_pos_before, "Failed to wrap around");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:06:55 -04:00
|
|
|
TEST_CASE("TC#2: Byte buffer", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == BUFFER_SIZE, "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == BUFFER_SIZE, "Incorrect max item size received");
|
|
|
|
|
|
|
|
//Calculate number of items to send. Aim to fill the buffer
|
|
|
|
int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify items waiting matches with the number of items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == no_of_items * SMALL_ITEM_SIZE, "Incorrect number of bytes waiting");
|
|
|
|
|
|
|
|
//At this point, the buffer should be full.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == 0, "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);
|
|
|
|
|
|
|
|
//Receive one item
|
|
|
|
receive_check_and_return_item_byte_buffer(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
|
|
|
|
//At this point, the buffer should not be full any more
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) > 0, "Buffer should have free space");
|
|
|
|
|
|
|
|
//Test receiving remaining items
|
|
|
|
for (int i = 0; i < no_of_items - 1; i++) {
|
|
|
|
receive_check_and_return_item_byte_buffer(buffer_handle, small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("TC#3: Byte buffer", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
//Create buffer
|
|
|
|
RingbufHandle_t buffer_handle = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handle != NULL, "Failed to create ring buffer");
|
|
|
|
|
|
|
|
//Check buffer free size and max item size upon buffer creation. Should be BUFFER_SIZE.
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetCurFreeSize(buffer_handle) == BUFFER_SIZE, "Incorrect buffer free size received");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferGetMaxItemSize(buffer_handle) == BUFFER_SIZE, "Incorrect max item size received");
|
|
|
|
|
|
|
|
//Calculate number of medium items to send. Aim to almost fill the buffer
|
|
|
|
int no_of_medium_items = BUFFER_SIZE / MEDIUM_ITEM_SIZE;
|
|
|
|
|
|
|
|
//Test sending items
|
|
|
|
for (int i = 0; i < no_of_medium_items; i++) {
|
|
|
|
send_item_and_check(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify items waiting matches with the number of medium items sent
|
|
|
|
UBaseType_t items_waiting;
|
|
|
|
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");
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
|
|
//Test receiving medium items
|
|
|
|
for (int i = 0; i < no_of_medium_items; i++) {
|
|
|
|
receive_check_and_return_item_byte_buffer(buffer_handle, large_item, MEDIUM_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify that no items are waiting
|
|
|
|
vRingbufferGetInfo(buffer_handle, NULL, NULL, NULL, NULL, &items_waiting);
|
|
|
|
TEST_ASSERT_MESSAGE(items_waiting == 0, "Incorrect items waiting");
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(buffer_handle);
|
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
/* ----------------------- Ring buffer queue sets test ------------------------
|
|
|
|
* The following test case will test receiving from ring buffers that have been
|
|
|
|
* added to a queue set. The test case will do the following...
|
|
|
|
* 1) Ring buffer of each type is created and added to the queue set
|
|
|
|
* 2) A receiving task is created to select from the queue set and read from the appropriate ring buffer
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void queue_set_receiving_task(void *queue_set_handle)
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
2018-04-18 13:20:34 -04:00
|
|
|
QueueSetHandle_t queue_set = (QueueSetHandle_t)queue_set_handle;
|
|
|
|
|
|
|
|
//Receive multiple items via queue set
|
|
|
|
BaseType_t done = pdFALSE;
|
|
|
|
int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
|
|
|
|
int items_rec_count[NO_OF_RB_TYPES] = {0};
|
|
|
|
while (done != pdTRUE) {
|
2022-02-08 04:39:38 -05:00
|
|
|
QueueSetMemberHandle_t member = xQueueSelectFromSet(queue_set, TIMEOUT_TICKS);
|
2018-04-18 13:20:34 -04:00
|
|
|
//Read from selected ring buffer
|
|
|
|
if (xRingbufferCanRead(buffer_handles[0], member) == pdTRUE) {
|
|
|
|
//No-split buffer
|
|
|
|
receive_check_and_return_item_no_split(buffer_handles[0], small_item, SMALL_ITEM_SIZE, 0, false);
|
|
|
|
items_rec_count[0] ++;
|
|
|
|
} else if (xRingbufferCanRead(buffer_handles[1], member) == pdTRUE) {
|
|
|
|
//Allow-split buffer
|
|
|
|
receive_check_and_return_item_allow_split(buffer_handles[1], small_item, SMALL_ITEM_SIZE, 0, false);
|
|
|
|
items_rec_count[1] ++;
|
2022-01-02 03:19:49 -05:00
|
|
|
} else if (xRingbufferCanRead(buffer_handles[2], member) == pdTRUE) {
|
2018-04-18 13:20:34 -04:00
|
|
|
//Byte buffer
|
|
|
|
receive_check_and_return_item_byte_buffer(buffer_handles[2], small_item, SMALL_ITEM_SIZE, 0, false);
|
|
|
|
items_rec_count[2] ++;
|
|
|
|
} else {
|
2024-01-26 09:20:19 -05:00
|
|
|
TEST_ASSERT_MESSAGE(false, "Error with queue set member");
|
2018-04-18 13:20:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check for completion
|
|
|
|
if (items_rec_count[0] == no_of_items &&
|
2022-01-02 03:19:49 -05:00
|
|
|
items_rec_count[1] == no_of_items &&
|
|
|
|
items_rec_count[2] == no_of_items) {
|
2018-04-18 13:20:34 -04:00
|
|
|
done = pdTRUE;
|
|
|
|
}
|
2017-11-17 06:53:43 -05:00
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
xSemaphoreGive(done_sem);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2019-03-04 01:27:06 -05:00
|
|
|
TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf]")
|
2018-04-18 13:20:34 -04:00
|
|
|
{
|
|
|
|
QueueSetHandle_t queue_set = xQueueCreateSet(NO_OF_RB_TYPES);
|
|
|
|
done_sem = xSemaphoreCreateBinary();
|
|
|
|
|
|
|
|
//Create ring buffer of each type, then add them to a queue set
|
|
|
|
for (int i = 0; i < NO_OF_RB_TYPES; i++) {
|
|
|
|
buffer_handles[i] = xRingbufferCreate(BUFFER_SIZE, i);
|
|
|
|
TEST_ASSERT_MESSAGE(buffer_handles[i] != NULL, "Failed to create ring buffer");
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferAddToQueueSetRead(buffer_handles[i], queue_set) == pdPASS, "Failed to add to read queue set");
|
|
|
|
}
|
|
|
|
//Create a task to send items to each ring buffer
|
|
|
|
int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE;
|
2022-10-04 07:19:07 -04:00
|
|
|
xTaskCreatePinnedToCore(queue_set_receiving_task, "rec tsk", 2048, (void *)queue_set, 10, NULL, 0);
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
//Send multiple items to each type of ring buffer
|
|
|
|
for (int i = 0; i < no_of_items; i++) {
|
|
|
|
for (int j = 0; j < NO_OF_RB_TYPES; j++) {
|
|
|
|
send_item_and_check(buffer_handles[j], small_item, SMALL_ITEM_SIZE, TIMEOUT_TICKS, false);
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
xSemaphoreTake(done_sem, portMAX_DELAY);
|
|
|
|
vSemaphoreDelete(done_sem);
|
2022-01-02 03:19:49 -05:00
|
|
|
//Remove and delete ring buffers from queue sets
|
2018-04-18 13:20:34 -04:00
|
|
|
for (int i = 0; i < NO_OF_RB_TYPES; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferRemoveFromQueueSetRead(buffer_handles[i], queue_set) == pdTRUE, "Failed to remove from read queue set");
|
|
|
|
vRingbufferDelete(buffer_handles[i]);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
vQueueDelete(queue_set);
|
2022-10-04 07:19:07 -04:00
|
|
|
vTaskDelay(1);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
/* -------------------------- 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;
|
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
static bool on_timer_alarm(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
2022-01-02 03:19:49 -05:00
|
|
|
bool need_yield = false;
|
2018-04-18 13:20:34 -04:00
|
|
|
|
|
|
|
//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
|
2022-01-02 03:19:49 -05:00
|
|
|
goto out;
|
2018-04-18 13:20:34 -04:00
|
|
|
} else {
|
|
|
|
//Signal complete
|
|
|
|
BaseType_t task_woken = pdFALSE;
|
|
|
|
xSemaphoreGiveFromISR(done_sem, &task_woken);
|
|
|
|
if (task_woken == pdTRUE) {
|
|
|
|
buf_type++;
|
2022-01-02 03:19:49 -05:00
|
|
|
need_yield = true;
|
2018-04-18 13:20:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 06:53:43 -05:00
|
|
|
|
2022-01-02 03:19:49 -05:00
|
|
|
out:
|
|
|
|
return need_yield;
|
2017-11-17 06:53:43 -05:00
|
|
|
}
|
|
|
|
|
2023-06-20 07:57:08 -04:00
|
|
|
// IDF-6471 - test hangs up on QEMU
|
|
|
|
TEST_CASE("Test ring buffer ISR", "[esp_ringbuf][qemu-ignore]")
|
2017-11-17 06:53:43 -05:00
|
|
|
{
|
2022-01-02 03:19:49 -05:00
|
|
|
gptimer_handle_t gptimer;
|
2018-04-18 13:20:34 -04:00
|
|
|
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;
|
2022-01-02 03:19:49 -05:00
|
|
|
|
|
|
|
//Setup timer for ISR
|
|
|
|
gptimer_config_t config = {
|
2022-04-13 01:12:30 -04:00
|
|
|
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
|
2022-01-02 03:19:49 -05:00
|
|
|
.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));
|
2022-04-23 06:59:38 -04:00
|
|
|
TEST_ESP_OK(gptimer_enable(gptimer));
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_start(gptimer));
|
2018-04-18 13:20:34 -04:00
|
|
|
//Wait for ISR to complete multiple iterations
|
|
|
|
xSemaphoreTake(done_sem, portMAX_DELAY);
|
|
|
|
|
|
|
|
//Cleanup
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_stop(gptimer));
|
2022-04-23 06:59:38 -04:00
|
|
|
TEST_ESP_OK(gptimer_disable(gptimer));
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ESP_OK(gptimer_del_timer(gptimer));
|
2018-04-18 13:20:34 -04:00
|
|
|
vSemaphoreDelete(done_sem);
|
|
|
|
for (int i = 0; i < NO_OF_RB_TYPES; i++) {
|
|
|
|
vRingbufferDelete(buffer_handles[i]);
|
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
/* ---------------------------- 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SRAND_SEED 3 //Arbitrarily chosen srand() seed
|
|
|
|
#define SMP_TEST_ITERATIONS 4
|
|
|
|
|
|
|
|
static const char continuous_data[] = {"A_very_long_string_that_will_be_split_into_"
|
|
|
|
"items_of_random_lengths_and_sent_to_the_ring_"
|
|
|
|
"buffer._The_maximum_random_length_will_also_"
|
|
|
|
"be_increased_over_multiple_iterations_in_this"
|
2022-01-02 03:19:49 -05:00
|
|
|
"_test"
|
|
|
|
};
|
2018-04-18 13:20:34 -04:00
|
|
|
#define CONT_DATA_LEN sizeof(continuous_data)
|
2019-03-04 01:27:06 -05:00
|
|
|
//32-bit aligned size that guarantees a wrap around at some point
|
|
|
|
#define CONT_DATA_TEST_BUFF_LEN (((CONT_DATA_LEN/2) + 0x03) & ~0x3)
|
2017-08-28 07:31:26 -04:00
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
typedef struct {
|
|
|
|
RingbufHandle_t buffer;
|
2019-03-04 01:27:06 -05:00
|
|
|
RingbufferType_t type;
|
2018-04-18 13:20:34 -04:00
|
|
|
} task_args_t;
|
|
|
|
|
|
|
|
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)
|
2017-08-28 07:31:26 -04:00
|
|
|
{
|
2018-04-18 13:20:34 -04:00
|
|
|
for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
|
|
|
|
size_t bytes_sent = 0; //Number of data bytes sent in this iteration
|
|
|
|
size_t next_item_size; //Size of next item to send
|
|
|
|
|
|
|
|
while (bytes_sent < CONT_DATA_LEN) {
|
|
|
|
//Get size of next item
|
|
|
|
next_item_size = rand() % (max_item_size + 1);
|
|
|
|
if (next_item_size + bytes_sent > CONT_DATA_LEN) {
|
|
|
|
next_item_size = CONT_DATA_LEN - bytes_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Send item
|
2022-01-02 03:19:49 -05:00
|
|
|
TEST_ASSERT_MESSAGE(xRingbufferSend(buffer, (void *) & (continuous_data[bytes_sent]), next_item_size, TIMEOUT_TICKS) == pdTRUE, "Failed to send an item");
|
2018-04-18 13:20:34 -04:00
|
|
|
bytes_sent += next_item_size;
|
2017-08-28 07:31:26 -04:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
xSemaphoreGive(tx_done);
|
|
|
|
xSemaphoreTake(rx_done, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:27:06 -05:00
|
|
|
static void read_from_buffer(RingbufHandle_t buffer, RingbufferType_t buf_type, size_t max_rec_size)
|
2018-04-18 13:20:34 -04:00
|
|
|
{
|
|
|
|
for (int iter = 0; iter < SMP_TEST_ITERATIONS; iter++) {
|
|
|
|
size_t bytes_rec = 0; //Number of data bytes received in this iteration
|
|
|
|
while (bytes_rec < CONT_DATA_LEN) {
|
|
|
|
size_t item_size, item_size2; //Possible for allow split buffers to receive two items
|
|
|
|
char *item_data, *item_data2;
|
|
|
|
|
|
|
|
//Select appropriate receive function for type of ring buffer
|
|
|
|
if (buf_type == RINGBUF_TYPE_NOSPLIT) {
|
|
|
|
item_data = (char *)xRingbufferReceive(buffer, &item_size, TIMEOUT_TICKS);
|
|
|
|
} else if (buf_type == RINGBUF_TYPE_ALLOWSPLIT) {
|
|
|
|
BaseType_t ret = xRingbufferReceiveSplit(buffer, (void **)&item_data, (void **)&item_data2, &item_size, &item_size2, TIMEOUT_TICKS);
|
|
|
|
TEST_ASSERT_MESSAGE(ret == pdTRUE, "Failed to receive any item");
|
|
|
|
} else {
|
|
|
|
item_data = (char *)xRingbufferReceiveUpTo(buffer, &item_size, TIMEOUT_TICKS, max_rec_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check received item and return it
|
|
|
|
TEST_ASSERT_MESSAGE(item_data != NULL, "Failed to receive an item");
|
|
|
|
if (buf_type == RINGBUF_TYPE_BYTEBUF) {
|
|
|
|
TEST_ASSERT_MESSAGE(item_size <= max_rec_size, "Received data exceeds max size");
|
|
|
|
}
|
|
|
|
for (int i = 0; i < item_size; i++) {
|
|
|
|
//Check item_data is valid
|
|
|
|
TEST_ASSERT_MESSAGE(item_data[i] == continuous_data[bytes_rec + i], "Received data is corrupted");
|
|
|
|
}
|
|
|
|
bytes_rec += item_size;
|
|
|
|
vRingbufferReturnItem(buffer, item_data);
|
|
|
|
if (buf_type == RINGBUF_TYPE_ALLOWSPLIT && item_data2 != NULL) {
|
|
|
|
//Check item_data2 is valid
|
|
|
|
for (int i = 0; i < item_size2; i++) {
|
|
|
|
TEST_ASSERT_MESSAGE(item_data2[i] == continuous_data[bytes_rec + i], "Received split data is corrupted");
|
|
|
|
}
|
|
|
|
bytes_rec += item_size2;
|
|
|
|
vRingbufferReturnItem(buffer, item_data2);
|
|
|
|
}
|
2017-08-28 07:31:26 -04:00
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
TEST_ASSERT_MESSAGE(bytes_rec == CONT_DATA_LEN, "Total length of received data is incorrect");
|
|
|
|
xSemaphoreGive(rx_done);
|
|
|
|
xSemaphoreTake(tx_done, portMAX_DELAY);
|
2017-08-28 07:31:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:34 -04:00
|
|
|
static void send_task(void *args)
|
|
|
|
{
|
|
|
|
RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
|
|
|
|
size_t max_item_len = xRingbufferGetMaxItemSize(buffer);
|
|
|
|
|
|
|
|
//Test sending short length items
|
|
|
|
send_to_buffer(buffer, 1);
|
|
|
|
//Test sending mid length items
|
2022-01-02 03:19:49 -05:00
|
|
|
send_to_buffer(buffer, max_item_len / 2);
|
2018-04-18 13:20:34 -04:00
|
|
|
//Test sending long length items
|
|
|
|
send_to_buffer(buffer, max_item_len);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rec_task(void *args)
|
|
|
|
{
|
|
|
|
RingbufHandle_t buffer = ((task_args_t *)args)->buffer;
|
|
|
|
size_t max_rec_len = xRingbufferGetMaxItemSize(buffer);
|
|
|
|
|
|
|
|
//Test receiving short length items
|
|
|
|
read_from_buffer(buffer, ((task_args_t *)args)->type, 1);
|
|
|
|
//Test receiving mid length items
|
2022-01-02 03:19:49 -05:00
|
|
|
read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len / 2);
|
2018-04-18 13:20:34 -04:00
|
|
|
//Test receiving long length items
|
|
|
|
read_from_buffer(buffer, ((task_args_t *)args)->type, max_rec_len);
|
|
|
|
|
|
|
|
xSemaphoreGive(tasks_done);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void setup(void)
|
2018-04-18 13:20:34 -04:00
|
|
|
{
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_printf("Size of test data: %d\n", CONT_DATA_LEN);
|
2018-04-18 13:20:34 -04:00
|
|
|
tx_done = xSemaphoreCreateBinary(); //Semaphore to indicate send is done for a particular iteration
|
|
|
|
rx_done = xSemaphoreCreateBinary(); //Semaphore to indicate receive is done for a particular iteration
|
2019-03-04 01:27:06 -05:00
|
|
|
tasks_done = xSemaphoreCreateBinary(); //Semaphore used to to indicate send and receive tasks completed running
|
2018-04-18 13:20:34 -04:00
|
|
|
srand(SRAND_SEED); //Seed RNG
|
2019-03-04 01:27:06 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void cleanup(void)
|
2019-03-04 01:27:06 -05:00
|
|
|
{
|
|
|
|
//Cleanup
|
|
|
|
vSemaphoreDelete(tx_done);
|
|
|
|
vSemaphoreDelete(rx_done);
|
|
|
|
vSemaphoreDelete(tasks_done);
|
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
|
2019-03-04 01:27:06 -05:00
|
|
|
TEST_CASE("Test ring buffer SMP", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
setup();
|
2018-04-18 13:20:34 -04:00
|
|
|
//Iterate through buffer types (No split, split, then byte buff)
|
2019-03-04 01:27:06 -05:00
|
|
|
for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
|
2018-04-18 13:20:34 -04:00
|
|
|
//Create buffer
|
|
|
|
task_args_t task_args;
|
|
|
|
task_args.buffer = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, buf_type); //Create buffer of selected type
|
|
|
|
task_args.type = buf_type;
|
2019-03-04 01:27:06 -05:00
|
|
|
TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
|
2018-04-18 13:20:34 -04:00
|
|
|
|
|
|
|
for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
|
|
|
|
//Test every permutation of core affinity
|
|
|
|
for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
|
|
|
|
for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
|
2018-04-18 13:20:34 -04:00
|
|
|
xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
|
|
|
|
xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
|
|
|
|
xSemaphoreTake(tasks_done, portMAX_DELAY);
|
|
|
|
vTaskDelay(5); //Allow idle to clean up
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete ring buffer
|
|
|
|
vRingbufferDelete(task_args.buffer);
|
|
|
|
vTaskDelay(10);
|
|
|
|
}
|
2019-03-04 01:27:06 -05:00
|
|
|
cleanup();
|
|
|
|
}
|
2018-04-18 13:20:34 -04:00
|
|
|
|
2019-03-04 01:27:06 -05:00
|
|
|
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
|
|
|
|
TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]")
|
|
|
|
{
|
|
|
|
setup();
|
|
|
|
//Iterate through buffer types (No split, split, then byte buff)
|
|
|
|
for (RingbufferType_t buf_type = 0; buf_type < RINGBUF_TYPE_MAX; buf_type++) {
|
|
|
|
StaticRingbuffer_t *buffer_struct;
|
|
|
|
uint8_t *buffer_storage;
|
|
|
|
//Allocate memory and create semaphores
|
|
|
|
#if CONFIG_SPIRAM_USE_CAPS_ALLOC //When SPIRAM can only be allocated using heap_caps_malloc()
|
|
|
|
buffer_struct = (StaticRingbuffer_t *)heap_caps_malloc(sizeof(StaticRingbuffer_t), MALLOC_CAP_SPIRAM);
|
2022-01-02 03:19:49 -05:00
|
|
|
buffer_storage = (uint8_t *)heap_caps_malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN, MALLOC_CAP_SPIRAM);
|
2019-03-04 01:27:06 -05:00
|
|
|
#else //Case where SPIRAM is disabled or when SPIRAM is allocatable through malloc()
|
|
|
|
buffer_struct = (StaticRingbuffer_t *)malloc(sizeof(StaticRingbuffer_t));
|
2022-01-02 03:19:49 -05:00
|
|
|
buffer_storage = (uint8_t *)malloc(sizeof(uint8_t) * CONT_DATA_TEST_BUFF_LEN);
|
2019-03-04 01:27:06 -05:00
|
|
|
#endif
|
|
|
|
TEST_ASSERT(buffer_struct != NULL && buffer_storage != NULL);
|
|
|
|
|
|
|
|
//Create buffer
|
|
|
|
task_args_t task_args;
|
|
|
|
task_args.buffer = xRingbufferCreateStatic(CONT_DATA_TEST_BUFF_LEN, buf_type, buffer_storage, buffer_struct); //Create buffer of selected type
|
|
|
|
task_args.type = buf_type;
|
|
|
|
TEST_ASSERT_MESSAGE(task_args.buffer != NULL, "Failed to create ring buffer");
|
|
|
|
|
|
|
|
for (int prior_mod = -1; prior_mod < 2; prior_mod++) { //Test different relative priorities
|
|
|
|
//Test every permutation of core affinity
|
|
|
|
for (int send_core = 0; send_core < portNUM_PROCESSORS; send_core++) {
|
|
|
|
for (int rec_core = 0; rec_core < portNUM_PROCESSORS; rec_core ++) {
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_printf("Type: %d, PM: %d, SC: %d, RC: %d\n", buf_type, prior_mod, send_core, rec_core);
|
2019-03-04 01:27:06 -05:00
|
|
|
xTaskCreatePinnedToCore(send_task, "send tsk", 2048, (void *)&task_args, 10 + prior_mod, NULL, send_core);
|
|
|
|
xTaskCreatePinnedToCore(rec_task, "rec tsk", 2048, (void *)&task_args, 10, NULL, rec_core);
|
|
|
|
xSemaphoreTake(tasks_done, portMAX_DELAY);
|
|
|
|
vTaskDelay(5); //Allow idle to clean up
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete ring buffer
|
|
|
|
vRingbufferDelete(task_args.buffer);
|
|
|
|
|
|
|
|
//Deallocate memory
|
|
|
|
free(buffer_storage);
|
|
|
|
free(buffer_struct);
|
|
|
|
vTaskDelay(10);
|
|
|
|
}
|
|
|
|
cleanup();
|
2018-04-18 13:20:34 -04:00
|
|
|
}
|
2019-03-04 01:27:06 -05:00
|
|
|
#endif
|
|
|
|
|
2022-10-04 07:19:07 -04:00
|
|
|
#if !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH
|
2019-03-04 01:27:06 -05:00
|
|
|
/* -------------------------- Test ring buffer IRAM ------------------------- */
|
2018-10-09 07:56:14 -04:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void)
|
2018-10-09 07:56:14 -04:00
|
|
|
{
|
|
|
|
bool result = true;
|
2022-06-30 03:43:45 -04:00
|
|
|
uint8_t item[4];
|
|
|
|
size_t item_size;
|
2018-10-09 07:56:14 -04:00
|
|
|
RingbufHandle_t handle = xRingbufferCreate(CONT_DATA_TEST_BUFF_LEN, RINGBUF_TYPE_NOSPLIT);
|
|
|
|
result = result && (handle != NULL);
|
2020-04-10 02:39:32 -04:00
|
|
|
spi_flash_guard_get()->start(); // Disables flash cache
|
2022-06-30 03:43:45 -04:00
|
|
|
|
2018-10-09 07:56:14 -04:00
|
|
|
xRingbufferGetMaxItemSize(handle);
|
2022-06-30 03:43:45 -04:00
|
|
|
xRingbufferSendFromISR(handle, (void *)item, sizeof(item), NULL);
|
|
|
|
xRingbufferReceiveFromISR(handle, &item_size);
|
|
|
|
|
2018-10-09 07:56:14 -04:00
|
|
|
spi_flash_guard_get()->end(); // Re-enables flash cache
|
2021-01-19 18:32:31 -05:00
|
|
|
vRingbufferDelete(handle);
|
2018-10-09 07:56:14 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:27:06 -05:00
|
|
|
TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ringbuf]")
|
2018-10-09 07:56:14 -04:00
|
|
|
{
|
2024-01-26 09:20:19 -05:00
|
|
|
TEST_ASSERT(iram_ringbuf_test());
|
2018-10-09 07:56:14 -04:00
|
|
|
}
|
2022-10-04 07:19:07 -04:00
|
|
|
#endif /* !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH */
|
2023-02-28 02:23:24 -05:00
|
|
|
|
|
|
|
/* ------------------------ 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]")
|
|
|
|
{
|
|
|
|
RingbufHandle_t no_split_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT);
|
|
|
|
RingbufHandle_t allow_split_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);
|
|
|
|
RingbufHandle_t byte_rb = xRingbufferCreate(BUFFER_SIZE, RINGBUF_TYPE_BYTEBUF);
|
|
|
|
TEST_ASSERT_MESSAGE(no_split_rb && allow_split_rb && byte_rb, "Failed to create ring buffers");
|
|
|
|
|
|
|
|
void *pvItem1;
|
|
|
|
void *pvItem2;
|
|
|
|
size_t xItemSize1;
|
|
|
|
size_t xItemSize2;
|
|
|
|
|
|
|
|
//Test that 0 item size on no split buffers should only send a header with no data
|
|
|
|
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSend(no_split_rb, NULL, 0, 0));
|
|
|
|
TEST_ASSERT_NOT_EQUAL(NULL, xRingbufferReceive(no_split_rb, &xItemSize1, 0));
|
|
|
|
TEST_ASSERT_EQUAL(0, xItemSize1);
|
|
|
|
|
|
|
|
//Test that acquiring 0 item size on no split buffers should only send a header without reserving a data buffer
|
|
|
|
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSendAcquire(no_split_rb, &pvItem1, 0, 0));
|
|
|
|
TEST_ASSERT_NOT_EQUAL(NULL, pvItem1);
|
|
|
|
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSendComplete(no_split_rb, pvItem1));
|
|
|
|
TEST_ASSERT_NOT_EQUAL(NULL, xRingbufferReceive(no_split_rb, &xItemSize1, 0));
|
|
|
|
TEST_ASSERT_EQUAL(0, xItemSize1);
|
|
|
|
|
|
|
|
//Test that 0 item size on allow split buffers should only send a header with no data
|
|
|
|
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSend(allow_split_rb, NULL, 0, 0));
|
|
|
|
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferReceiveSplit(allow_split_rb, &pvItem1, &pvItem2, &xItemSize1, &xItemSize2, 0));
|
|
|
|
TEST_ASSERT_NOT_EQUAL(NULL, pvItem1);
|
|
|
|
TEST_ASSERT_EQUAL(NULL, pvItem2);
|
|
|
|
TEST_ASSERT_EQUAL(0, xItemSize1);
|
|
|
|
|
|
|
|
//Test that 0 item size on byte buffers should send nothing
|
|
|
|
TEST_ASSERT_EQUAL(pdTRUE, xRingbufferSend(byte_rb, NULL, 0, 0));
|
|
|
|
TEST_ASSERT_EQUAL(pdFALSE, xRingbufferReceiveUpTo(byte_rb, &xItemSize1, 0, BUFFER_SIZE));
|
|
|
|
|
|
|
|
//Cleanup
|
|
|
|
vRingbufferDelete(no_split_rb);
|
|
|
|
vRingbufferDelete(allow_split_rb);
|
|
|
|
vRingbufferDelete(byte_rb);
|
|
|
|
}
|
2023-06-24 09:30:53 -04:00
|
|
|
|
|
|
|
/* --------------------- 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
|
2024-01-26 09:20:19 -05:00
|
|
|
rb_handle = xRingbufferCreateWithCaps(BUFFER_SIZE, RINGBUF_TYPE_NOSPLIT, (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
|
2023-06-24 09:30:53 -04:00
|
|
|
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);
|
|
|
|
}
|