freertos: Add unit tests for ...WithCaps() functions

This commit is contained in:
Darian Leung 2023-04-07 16:13:10 +08:00
parent 4e7cd2e706
commit fa4491a56b

View File

@ -0,0 +1,139 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/stream_buffer.h"
#include "freertos/message_buffer.h"
#include "freertos/event_groups.h"
#include "freertos/idf_additions.h"
#include "esp_memory_utils.h"
#include "unity.h"
/*
Test ...Create...WithCaps() functions
Purpose:
- Test that the ___Create___WithCaps() functions create objects using with correct memory capabilities
Procedure:
- Create different FreeRTOS objects (e.g. queues, event groups etc) using the various ___Create___WithCaps()
functions with (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT) capabilities.
- Call the ___GetStaticBuffers() functions to get the address of the memory of the created objects
- Check that the memory has (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT) capabilities by calling esp_ptr_in_dram()
Expected:
- ___Create___WithCaps() should create objects in DRAM
*/
#define OBJECT_MEMORY_CAPS (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT)
TEST_CASE("IDF additions: Queue creation with memory caps", "[freertos]")
{
QueueHandle_t queue_handle;
uint8_t *queue_storage;
StaticQueue_t *queue_obj;
// Create queue with caps
queue_handle = xQueueCreateWithCaps(5, sizeof(uint32_t), OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, queue_handle);
// Get the queue's memory
TEST_ASSERT_EQUAL(pdTRUE, xQueueGetStaticBuffers(queue_handle, &queue_storage, &queue_obj));
TEST_ASSERT(esp_ptr_in_dram(queue_storage));
TEST_ASSERT(esp_ptr_in_dram(queue_obj));
// Free the queue
vQueueDeleteWithCaps(queue_handle);
}
TEST_CASE("IDF additions: Semaphore creation with memory caps", "[freertos]")
{
SemaphoreHandle_t sem_handle;
StaticSemaphore_t *sem_obj;
// Create binary semaphore with caps
sem_handle = xSemaphoreCreateBinaryWithCaps(OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, sem_handle);
// Get the binary semaphore's memory
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreGetStaticBuffer(sem_handle, &sem_obj));
TEST_ASSERT(esp_ptr_in_dram(sem_obj));
// Free the binary semaphore
vSemaphoreDeleteWithCaps(sem_handle);
// Create counting semaphore with caps
sem_handle = xSemaphoreCreateCountingWithCaps(4, 0, OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, sem_handle);
// Get the counting semaphore's memory
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreGetStaticBuffer(sem_handle, &sem_obj));
TEST_ASSERT(esp_ptr_in_dram(sem_obj));
// Free the counting semaphore
vSemaphoreDeleteWithCaps(sem_handle);
// Create mutex semaphore with caps
sem_handle = xSemaphoreCreateMutexWithCaps(OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, sem_handle);
// Get the mutex semaphore's memory
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreGetStaticBuffer(sem_handle, &sem_obj));
TEST_ASSERT(esp_ptr_in_dram(sem_obj));
// Free the mutex semaphore
vSemaphoreDeleteWithCaps(sem_handle);
// Create recursive mutex with caps
sem_handle = xSemaphoreCreateRecursiveMutexWithCaps(OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, sem_handle);
// Get the recursive mutex's memory
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreGetStaticBuffer(sem_handle, &sem_obj));
TEST_ASSERT(esp_ptr_in_dram(sem_obj));
// Free the recursive mutex
vSemaphoreDeleteWithCaps(sem_handle);
}
TEST_CASE("IDF additions: Stream & message buffer creation with memory caps", "[freertos]")
{
StreamBufferHandle_t stream_buffer_handle;
uint8_t *stream_buffer_storage;
StaticStreamBuffer_t *stream_buffer_obj;
// Create stream buffer with caps
stream_buffer_handle = xStreamBufferCreateWithCaps(16, 0, OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, stream_buffer_handle);
// Get the stream buffer's memory
TEST_ASSERT_EQUAL(pdTRUE, xStreamBufferGetStaticBuffers(stream_buffer_handle, &stream_buffer_storage, &stream_buffer_obj));
TEST_ASSERT(esp_ptr_in_dram(stream_buffer_storage));
TEST_ASSERT(esp_ptr_in_dram(stream_buffer_obj));
// Free the stream buffer
vStreamBufferDelete(stream_buffer_handle);
MessageBufferHandle_t msg_buffer_handle;
uint8_t *msg_buffer_storage;
StaticMessageBuffer_t *msg_buffer_obj;
// Create message buffer with caps
msg_buffer_handle = xMessageBufferCreateWithCaps(16, OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, msg_buffer_handle);
// Get the message buffer's memory
TEST_ASSERT_EQUAL(pdTRUE, xMessageBufferGetStaticBuffers(msg_buffer_handle, &msg_buffer_storage, &msg_buffer_obj));
TEST_ASSERT(esp_ptr_in_dram(msg_buffer_storage));
TEST_ASSERT(esp_ptr_in_dram(msg_buffer_obj));
// Free the message buffer
vMessageBufferDelete(msg_buffer_handle);
}
TEST_CASE("IDF additions: Event group creation with memory caps", "[freertos]")
{
EventGroupHandle_t evt_group_handle;
StaticEventGroup_t *evt_group_obj;
// Create event group with caps
evt_group_handle = xEventGroupCreateWithCaps(OBJECT_MEMORY_CAPS);
TEST_ASSERT_NOT_EQUAL(NULL, evt_group_handle);
// Get the event group's memory
TEST_ASSERT_EQUAL(pdTRUE, xEventGroupGetStaticBuffer(evt_group_handle, &evt_group_obj));
TEST_ASSERT(esp_ptr_in_dram(evt_group_obj));
// Free the event group
vEventGroupDelete(evt_group_handle);
}