-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#pragma once | ||
|
||
|
||
#include <Sts1CobcSw/Periphery/Fram.hpp> | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <span> | ||
|
||
|
||
namespace sts1cobcsw::fram | ||
{ | ||
|
||
template<class T, std::size_t poolSize> | ||
class RingBuffer | ||
{ | ||
public: | ||
Address startAddress; | ||
std::uint32_t writeIndex = 0; | ||
std::uint32_t readIndex = 0; | ||
std::uint32_t currentWrite = UINT32_MAX; | ||
std::array<T, poolSize> vals; | ||
|
||
public: | ||
std::uint64_t writeCnt = 0; | ||
std::uint64_t readCnt = 0; | ||
std::uint32_t occupiedCnt = 0; | ||
|
||
RingBuffer(Address startAddress) : startAddress(startAddress) | ||
{ | ||
} | ||
|
||
T * getNextEntryToPut() | ||
{ | ||
return &vals[writeIndex]; | ||
} | ||
|
||
void put(T const & newdata) | ||
{ | ||
currentWrite = writeIndex; | ||
Address address = startAddress + writeIndex * sizeof(T); | ||
|
||
WriteTo(address, reinterpret_cast<Byte const *>(&newdata), sizeof(T), 0); | ||
writeIndex++; | ||
|
||
if(writeIndex >= poolSize) | ||
{ | ||
writeIndex = 0; | ||
} | ||
if(occupiedCnt < poolSize) | ||
{ | ||
occupiedCnt++; | ||
} | ||
writeCnt++; | ||
currentWrite = UINT32_MAX; | ||
} | ||
|
||
// Get next item | ||
void get(T & fromRing) | ||
{ | ||
// Jump the current being written record | ||
if(readIndex == currentWrite) | ||
{ | ||
readIndex++; | ||
} | ||
if(readIndex >= poolSize) | ||
{ | ||
readIndex = 0; | ||
} | ||
|
||
// readIndex should not pass write index if there is no data after it | ||
if(writeCnt < poolSize && readIndex >= writeIndex) | ||
{ | ||
readIndex = 0; | ||
} | ||
|
||
Address address = startAddress + readIndex * sizeof(T); | ||
std::array<Byte, sizeof(T)> buffer; | ||
ReadFrom(address, buffer, 0); | ||
fromRing = *reinterpret_cast<T *>(buffer.data()); | ||
|
||
readIndex++; | ||
if(readIndex >= poolSize) | ||
{ | ||
readIndex = 0; | ||
} | ||
|
||
if(occupiedCnt > 0) | ||
{ | ||
occupiedCnt--; | ||
} | ||
readCnt++; | ||
} | ||
|
||
int getLen() | ||
{ | ||
return poolSize; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <cstdint> | ||
#include <rodos/support/support-libs/ringbuffer.h> | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
|
||
TEST_CASE("RODOS ringbuffer put and get operations") | ||
{ | ||
|
||
RODOS::RingBuffer<int, 10> buffer; | ||
|
||
REQUIRE(buffer.getLen() == 10); | ||
|
||
int value = 42; | ||
int result; | ||
buffer.put(value); | ||
buffer.get(result); | ||
REQUIRE(value == result); | ||
} | ||
|
||
TEST_CASE("RODOS ringbuffer overwrite") | ||
{ | ||
|
||
RODOS::RingBuffer<int, 10> buffer; | ||
REQUIRE(buffer.getLen() == 10); | ||
|
||
for(int i = 0; i < 15; i++) | ||
{ | ||
buffer.put(i); | ||
} | ||
|
||
int result; | ||
for(int i = 10; i < 15; ++i) | ||
{ | ||
buffer.get(result); | ||
REQUIRE(result == i); | ||
} | ||
} | ||
|
||
TEST_CASE("RODOS ringbuffer wrapping") | ||
{ | ||
|
||
RODOS::RingBuffer<int, 10> buffer; | ||
for(int i = 0; i < 30; ++i) | ||
{ | ||
buffer.put(i); | ||
} | ||
|
||
int result; | ||
for(int i = 20; i < 30; ++i) | ||
{ | ||
buffer.get(result); | ||
REQUIRE(result == i); | ||
} | ||
|
||
|
||
} | ||
|
||
// TODO: Split in differents tests or sections | ||
TEST_CASE("RingBuffer put and get operations") | ||
{ | ||
} |