Skip to content

Commit

Permalink
Clean includes and move members from public to private
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromehue committed May 28, 2024
1 parent 7513f7b commit d3f244e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 53 deletions.
Empty file.
77 changes: 28 additions & 49 deletions Sts1CobcSw/Periphery/FramRingBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,84 @@


#include <Sts1CobcSw/Periphery/Fram.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Serial/Serial.hpp>
#include <Sts1CobcSw/Utility/Span.hpp>

#include <rodos-debug.h>

#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <span>

#include "Sts1CobcSw/Utility/Span.hpp"


namespace sts1cobcsw::fram
{
using sts1cobcsw::Byte;
using sts1cobcsw::Span;
using sts1cobcsw::operator""_b; // NOLINT(misc-unused-using-decls)
using sts1cobcsw::TriviallySerializable;


// TODO: Handle non trivially serializable types/class
// template<typename T, std::size_t poolSize>
template<typename 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;
// TODO: Check that this is correct, or that it should be
std::array<T, poolSize> vals;

// NOLINTNEXTLINE(*non-private-member-variables-in-classes)
std::uint64_t writeCnt = 0;
// NOLINTNEXTLINE(*non-private-member-variables-in-classes)
std::uint64_t readCnt = 0;
// NOLINTNEXTLINE(*non-private-member-variables-in-classes)
std::uint32_t occupiedCnt = 0;

explicit RingBuffer(Address address) : startAddress(address)
{
}

auto GetNextEntryToPut() -> T *
explicit RingBuffer(Address address) : startAddress_(address)
{
return &vals[writeIndex];
}

void Put(T const & newdata)
{
currentWrite = writeIndex;
// NOTE: For trivially serializable types, sizeof and serialSize should be the same.
auto const address = startAddress + writeIndex * serialSize<T>;
currentWrite_ = writeIndex_;
auto const address = startAddress_ + writeIndex_ * serialSize<T>;
fram::WriteTo(address, Span(Serialize(newdata)), 0);
writeIndex++;
writeIndex_++;

if(writeIndex >= poolSize)
if(writeIndex_ >= poolSize)
{
writeIndex = 0;
writeIndex_ = 0;
}
if(occupiedCnt < poolSize)
{
occupiedCnt++;
}
writeCnt++;
currentWrite = UINT32_MAX;
currentWrite_ = UINT32_MAX;
}

// Get next item
void Get(T & fromRing)
{
// Jump the current being written record
if(readIndex == currentWrite)
if(readIndex_ == currentWrite_)
{
readIndex++;
readIndex_++;
}
if(readIndex >= poolSize)
if(readIndex_ >= poolSize)
{
readIndex = 0;
readIndex_ = 0;
}

// readIndex should not pass write index if there is no data after it
if(writeCnt < poolSize && readIndex >= writeIndex)
if(writeCnt < poolSize && readIndex_ >= writeIndex_)
{
readIndex = 0;
readIndex_ = 0;
}

auto const address = startAddress + readIndex * serialSize<T>;
// TODO: Deal with timeout
auto const address = startAddress_ + readIndex_ * serialSize<T>;
// TODO: Add a real timeout value (spiTimeout in hw tests is 30ms, but for much more data)
auto readData = fram::ReadFrom<serialSize<T>>(address, 0);
fromRing = Deserialize<T>(std::span(readData));

// DEBUG PRINT
std::printf("Printing read data in buffer.Get() method :\n");
for(auto byte : readData)
readIndex_++;
if(readIndex_ >= poolSize)
{
std::printf("0x%02x ", static_cast<unsigned char>(byte));
}
std::printf("\n");
// END DEBUG PRINT

readIndex++;
if(readIndex >= poolSize)
{
readIndex = 0;
readIndex_ = 0;
}

if(occupiedCnt > 0)
Expand All @@ -120,5 +93,11 @@ class RingBuffer
{
return poolSize;
}

private:
Address startAddress_;
std::uint32_t writeIndex_ = 0;
std::uint32_t readIndex_ = 0;
std::uint32_t currentWrite_ = UINT32_MAX;
};
}
5 changes: 4 additions & 1 deletion Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ target_link_libraries(
)

add_program(RingBuffer FramRingBuffer.test.cpp)
target_link_libraries(Sts1CobcSwTests_RingBuffer PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Periphery Sts1CobcSw_Serial)
target_link_libraries(
Sts1CobcSwTests_RingBuffer PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Periphery
Sts1CobcSw_Serial
)

# TODO: Enable again once problem with segmentation violation on CI is fixed
add_program(LfsRam LfsRam.test.cpp)
Expand Down
8 changes: 5 additions & 3 deletions Tests/UnitTests/FramRingBuffer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Sts1CobcSw/Periphery/Fram.hpp>
#include <Sts1CobcSw/Periphery/FramMock.hpp>
#include <Sts1CobcSw/Periphery/FramRingBuffer.hpp>
#include <Sts1CobcSw/ProgramId/ProgramId.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Serial/Serial.hpp>

Expand Down Expand Up @@ -76,7 +77,7 @@ TEST_CASE("RODOS ringbuffer wrapping")
}
}

// TODO: Split in differents tests or sections
// TODO: Split in different tests or sections
TEST_CASE("RingBuffer put and get operations")
{
fram::ram::SetAllDoFunctions();
Expand Down Expand Up @@ -177,8 +178,9 @@ TEST_CASE("Program status and history")

// Put() and Get() a ProgramStatusHistoryEntry variable.
auto ringBuffer = fram::RingBuffer<ProgramStatusHistoryEntry, 10>(0);
auto value = ProgramStatusHistoryEntry{
.programId = 12, .startTime = 34, .status = ProgramStatus::programExecutionFailed};
auto value = ProgramStatusHistoryEntry{.programId = sts1cobcsw::ProgramId(12),
.startTime = 34,
.status = ProgramStatus::programExecutionFailed};
auto result = ProgramStatusHistoryEntry{};

ringBuffer.Put(value);
Expand Down

0 comments on commit d3f244e

Please sign in to comment.