Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ jobs:

# Add $version-available directives for the modules
echo "\$version-available dm_sw_ring $VERSIONS" >> versions.dmm
echo "\$version-available test_dm_sw_ring $VERSIONS" >> versions.dmm

echo "Generated versions.dmm:"
cat versions.dmm
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,4 @@ target_include_directories(${DMOD_MODULE_NAME} PRIVATE
# ======================================================================
# test_dm_sw_ring Application
# ======================================================================
# Add test_dm_sw_ring application subdirectory
# add_subdirectory(apps/test_dm_sw_ring)
add_subdirectory(tests)
2 changes: 1 addition & 1 deletion manifest.dmm
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ $include https://github.com/choco-technologies/dm_sw_ring/releases/download/vlat
dm_sw_ring https://github.com/choco-technologies/dm_sw_ring/releases/download/v<version>/dm_sw_ring-v<version>-<arch_name>.zip

# Test application
# test_dm_sw_ring https://github.com/choco-technologies/dm_sw_ring/releases/download/v<version>/dm_sw_ring-v<version>-<arch_name>.zip
test_dm_sw_ring https://github.com/choco-technologies/dm_sw_ring/releases/download/v<version>/dm_sw_ring-v<version>-<arch_name>.zip
72 changes: 28 additions & 44 deletions src/dm_sw_ring.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define DMOD_ENABLE_REGISTRATION ON
#include "dmod.h"
#include "dm_sw_ring.h"

Expand All @@ -16,6 +17,7 @@ struct dm_sw_ring
dm_sw_ring_capacity_t capacity; // Capacity of the ring buffer (number of elements)
dm_sw_ring_capacity_t head; // Index of the head (next element to read)
dm_sw_ring_capacity_t tail; // Index of the tail (next element to write)
dm_sw_ring_capacity_t count; // Number of elements currently stored in the ring buffer
uint8_t* buffer; // Pointer to the buffer memory
dm_sw_ring_flags_t flags; // Flags for ring buffer behavior
void* mutex; // Mutex for synchronization (if enabled)
Expand Down Expand Up @@ -59,21 +61,17 @@ void dmod_preinit(void)
// Nothing to do
}

/**
* @brief Module initialization
*/

int dmod_init(const Dmod_Config_t *Config)
{
(void)Config;
DMOD_LOG_INFO("DM Software Ring module initialized\n");
return 0;
}

/**
* @brief Module deinitialization
*/
void dmod_deinit(void)
int dmod_deinit(void)
{
// Nothing to do
DMOD_LOG_INFO("DM Software Ring module deinitialized\n");
return 0;
}

// ============================================================================
Expand Down Expand Up @@ -109,6 +107,7 @@ dmod_dm_sw_ring_api_declaration(1.0, dm_sw_ring_t, _create, (dm_sw_ring_capacity
ring->capacity = capacity;
ring->head = 0;
ring->tail = 0;
ring->count = 0;
ring->flags = flags;
ring->mutex = NULL;
ring->space_semaphore = NULL;
Expand Down Expand Up @@ -156,16 +155,21 @@ dmod_dm_sw_ring_api_declaration(1.0, void, _destroy, (dm_sw_ring_t ring))
if (lock_ring(ring))
{
ring->magic = 0;
void* mutex = ring->mutex;
ring->mutex = NULL;

if (ring->mutex != NULL)
if (mutex != NULL)
{
Dmod_Mutex_Unlock(mutex);
Dmod_Mutex_Delete(mutex);
}
else
{
Dmod_Mutex_Delete(ring->mutex);
Dmod_ExitCritical();
}

Dmod_Free(ring->buffer);
Dmod_Free(ring);

// No need to unlock since the instance is being destroyed
}
}

Expand Down Expand Up @@ -246,14 +250,7 @@ dmod_dm_sw_ring_api_declaration(1.0, dm_sw_ring_capacity_t, _size, (dm_sw_ring_t
dm_sw_ring_capacity_t size = 0;
if(lock_ring(ring))
{
if (ring->tail >= ring->head)
{
size = ring->tail - ring->head;
}
else
{
size = ring->capacity - (ring->head - ring->tail);
}
size = ring->count;
unlock_ring(ring);
}
return size;
Expand Down Expand Up @@ -315,6 +312,7 @@ dmod_dm_sw_ring_api_declaration(1.0, int32_t, _clear, (dm_sw_ring_t ring))
{
ring->head = 0;
ring->tail = 0;
ring->count = 0;
result = 0; // Success
unlock_ring(ring);
}
Expand Down Expand Up @@ -436,14 +434,7 @@ static bool validate_ring(dm_sw_ring_t ring)
*/
static dm_sw_ring_capacity_t available_space(dm_sw_ring_t ring)
{
if (ring->tail >= ring->head)
{
return ring->capacity - (ring->tail - ring->head);
}
else
{
return ring->head - ring->tail;
}
return ring->capacity - ring->count;
}

/**
Expand All @@ -453,14 +444,7 @@ static dm_sw_ring_capacity_t available_space(dm_sw_ring_t ring)
*/
static dm_sw_ring_capacity_t available_data(dm_sw_ring_t ring)
{
if (ring->tail >= ring->head)
{
return ring->tail - ring->head;
}
else
{
return ring->capacity - (ring->head - ring->tail);
}
return ring->count;
}

/**
Expand All @@ -470,7 +454,7 @@ static dm_sw_ring_capacity_t available_data(dm_sw_ring_t ring)
*/
static bool is_full(dm_sw_ring_t ring)
{
return available_space(ring) == 0;
return ring->count == ring->capacity;
}

/**
Expand All @@ -480,7 +464,7 @@ static bool is_full(dm_sw_ring_t ring)
*/
static bool is_empty(dm_sw_ring_t ring)
{
return ring->head == ring->tail;
return ring->count == 0;
}

/**
Expand All @@ -492,6 +476,7 @@ static void put_byte(dm_sw_ring_t ring, uint8_t data)
{
ring->buffer[ring->tail] = data;
ring->tail = (ring->tail + 1) % ring->capacity;
ring->count++;
}

/**
Expand All @@ -503,6 +488,7 @@ static uint8_t get_byte(dm_sw_ring_t ring)
{
uint8_t data = ring->buffer[ring->head];
ring->head = (ring->head + 1) % ring->capacity;
ring->count--;
return data;
}

Expand All @@ -523,6 +509,7 @@ static void discard(dm_sw_ring_t ring, dm_sw_ring_capacity_t length)
wait_for_data(ring, length);

ring->head = (ring->head + length) % ring->capacity;
ring->count -= length;

}

Expand Down Expand Up @@ -634,13 +621,10 @@ static dm_sw_ring_capacity_t peek_data(dm_sw_ring_t ring, uint8_t* buffer, dm_sw
{
dm_sw_ring_capacity_t peeked = 0;
dm_sw_ring_capacity_t index = ring->head;
dm_sw_ring_capacity_t available = ring->count;

for (peeked = 0; peeked < length; peeked++)
for (peeked = 0; peeked < length && peeked < available; peeked++)
{
if (index == ring->tail)
{
break; // Buffer is empty
}
buffer[peeked] = ring->buffer[index];
index = (index + 1) % ring->capacity;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(DMOD_MODULE_NAME test_dm_sw_ring)
set(DMOD_MODULE_VERSION ${PROJECT_VERSION})
set(DMOD_AUTHOR_NAME "Patryk Kubiak")
set(DMOD_STACK_SIZE 1024)
set(DMOD_PRIORITY 0)

dmod_add_executable(${DMOD_MODULE_NAME} ${DMOD_MODULE_VERSION}
test_dm_sw_ring.c
)

target_include_directories(${DMOD_MODULE_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../include
)

target_link_libraries(${DMOD_MODULE_NAME} dm_sw_ring_if)
Loading
Loading