Skip to content

Commit

Permalink
applications: nrf5340_audio: Add a deinitialize function
Browse files Browse the repository at this point in the history
    The data FIFO module did not have a deinitialize function so
	added one, with a twister test.

Signed-off-by: Graham Wacey <[email protected]>
  • Loading branch information
gWacey authored and rlubos committed Jul 29, 2024
1 parent 5fde323 commit c706bb2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
26 changes: 19 additions & 7 deletions include/data_fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ struct data_fifo {

#define DATA_FIFO_DEFINE(name, elements_max_in, block_size_max_in) \
char __aligned(WB_UP(1)) \
_msgq_buffer_##name[(elements_max_in) * sizeof(struct data_fifo_msgq)] = { 0 }; \
_msgq_buffer_##name[(elements_max_in) * sizeof(struct data_fifo_msgq)] = {0}; \
char __aligned(WB_UP(1)) \
_slab_buffer_##name[(elements_max_in) * (block_size_max_in)] = { 0 }; \
struct data_fifo name = { .msgq_buffer = _msgq_buffer_##name, \
.slab_buffer = _slab_buffer_##name, \
.block_size_max = block_size_max_in, \
.elements_max = elements_max_in, \
.initialized = false }
_slab_buffer_##name[(elements_max_in) * (block_size_max_in)] = {0}; \
struct data_fifo name = {.msgq_buffer = _msgq_buffer_##name, \
.slab_buffer = _slab_buffer_##name, \
.block_size_max = block_size_max_in, \
.elements_max = elements_max_in, \
.initialized = false}

/**
* @brief Get pointer to the first vacant block in slab.
Expand Down Expand Up @@ -150,6 +150,18 @@ int data_fifo_num_used_get(struct data_fifo *data_fifo, uint32_t *alloced_num,
*/
int data_fifo_empty(struct data_fifo *data_fifo);

/**
* @brief Deinitialize data_fifo.
*
* @note data_fifo is emptied first, so it is the user's responsibility to release any data items it
* has queued. The internal slab and message buffer are not released.
*
* @param data_fifo Pointer to the data_fifo structure.
*
* @retval 0 if success, error otherwise.
*/
int data_fifo_uninit(struct data_fifo *data_fifo);

/**
* @brief Initialise the data_fifo.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/data_fifo/data_fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ int data_fifo_empty(struct data_fifo *data_fifo)
return 0;
}

int data_fifo_uninit(struct data_fifo *data_fifo)
{
__ASSERT_NO_MSG(data_fifo != NULL);
__ASSERT_NO_MSG(data_fifo->initialized);
int ret;

ret = data_fifo_empty(data_fifo);
if (ret) {
return ret;
}

data_fifo->initialized = false;

return 0;
}

int data_fifo_init(struct data_fifo *data_fifo)
{
__ASSERT_NO_MSG(data_fifo != NULL);
Expand Down
46 changes: 44 additions & 2 deletions tests/lib/data_fifo/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,48 @@ static void internal_test_remaining_elements(struct data_fifo *data_fifo, uint32
num_locked, line);
}

ZTEST(suite_data_fifo, test_data_fifo_uninit_ok)
{
#define BLOCKS_NUM 10
DATA_FIFO_DEFINE(data_fifo, 10, 128);

int ret;

ret = data_fifo_init(&data_fifo);
zassert_equal(ret, 0, "init did not return 0");
zassert_equal(data_fifo.initialized, true, "init did not set initialize flag");

uint8_t *data_ptr;
size_t data_size = 5;

for (uint32_t i = 0; i < BLOCKS_NUM; i++) {
ret = data_fifo_pointer_first_vacant_get(&data_fifo, (void **)&data_ptr, K_NO_WAIT);
zassert_equal(ret, 0, "first_vacant_get did not return 0");
data_ptr[0] = 0xa1;
data_ptr[1] = 0xa2;
data_ptr[2] = 0xa3;
data_ptr[3] = 0xa4;
data_ptr[4] = 0xa5;

internal_test_remaining_elements(&data_fifo, i + 1, i, __LINE__);

ret = data_fifo_block_lock(&data_fifo, (void **)&data_ptr, data_size);
zassert_equal(ret, 0, "block_lock did not return 0");

internal_test_remaining_elements(&data_fifo, i + 1, i + 1, __LINE__);
}

ret = data_fifo_uninit(&data_fifo);
zassert_equal(ret, 0, "deinit did not return 0");
zassert_equal(data_fifo.initialized, false, "deinit did not reset initialize flag");

ret = data_fifo_init(&data_fifo);
zassert_equal(ret, 0, "init did not return 0");
zassert_equal(data_fifo.initialized, true, "init did not set initialize flag");

internal_test_remaining_elements(&data_fifo, 0, 0, __LINE__);
}

ZTEST(suite_data_fifo, test_data_fifo_init_ok)
{
DATA_FIFO_DEFINE(data_fifo, 8, 128);
Expand Down Expand Up @@ -61,7 +103,7 @@ ZTEST(suite_data_fifo, test_data_fifo_data_put_get_ok)
data_ptr[2] = 0xa3;
data_ptr[3] = 0xa4;
data_ptr[4] = 0xa5;
uint8_t data_1[DATA_SIZE] = { 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 };
uint8_t data_1[DATA_SIZE] = {0xa1, 0xa2, 0xa3, 0xa4, 0xa5};

internal_test_remaining_elements(&data_fifo, 1, 0, __LINE__);

Expand All @@ -79,7 +121,7 @@ ZTEST(suite_data_fifo, test_data_fifo_data_put_get_ok)
data_ptr[3] = 0xb4;
data_ptr[4] = 0xb5;
data_ptr[5] = 0xb6;
uint8_t data_2[DATA_SIZE + 1] = { 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6 };
uint8_t data_2[DATA_SIZE + 1] = {0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6};

internal_test_remaining_elements(&data_fifo, 2, 1, __LINE__);

Expand Down

0 comments on commit c706bb2

Please sign in to comment.