Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock FRAM code #286

Merged
merged 12 commits into from
May 12, 2024
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "STS1 COBC Full",
"image": "tuwienspaceteam/sts1-cobc:1.6.0"
"image": "tuwienspaceteam/sts1-cobc:1.6.1"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ prefix/
CMakeLists.txt.user
CMakeUserPresets.json
compile_commands.json
FramMock.bin
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ add_library(Sts1CobcSw_FileSystem STATIC)
add_library(Sts1CobcSw_Outcome INTERFACE)
add_library(Sts1CobcSw_Serial INTERFACE)
add_library(Sts1CobcSw_Utility STATIC)
add_library(Sts1CobcSw_Periphery STATIC)
add_program(HelloDummy)
# add_program(Heartbeat)

if(CMAKE_SYSTEM_NAME STREQUAL Generic)
add_library(Sts1CobcSw_Periphery STATIC)
add_library(Sts1CobcSw_Hal STATIC)
add_program(CobcSw)
endif()
Expand Down
2 changes: 1 addition & 1 deletion Sts1CobcSw/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if(CMAKE_SYSTEM_NAME STREQUAL Generic)
add_subdirectory(Periphery)
add_subdirectory(Hal)
endif()
add_subdirectory(Periphery)
add_subdirectory(Edu)
add_subdirectory(FileSystem)
add_subdirectory(Outcome)
Expand Down
2 changes: 1 addition & 1 deletion Sts1CobcSw/Hal/Spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ auto Spi::TransferEnd() const -> std::int64_t
}


auto Spi::BaudRate() -> int32_t
auto Spi::BaudRate() -> std::int32_t
{
return this->spi_.status(RODOS::SPI_STATUS_BAUDRATE);
}
Expand Down
2 changes: 1 addition & 1 deletion Sts1CobcSw/Hal/Spi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Spi
friend auto ReadFrom(Spi * spi, std::span<T, extent> data, std::int64_t timeout) -> void;

auto TransferEnd() const -> std::int64_t;
auto BaudRate() -> int32_t;
auto BaudRate() -> std::int32_t;


private:
Expand Down
2 changes: 2 additions & 0 deletions Sts1CobcSw/Periphery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Generic)
)
target_link_libraries(Sts1CobcSw_Periphery PRIVATE Sts1CobcSw_Utility)
target_link_libraries(Sts1CobcSw_Periphery PUBLIC Sts1CobcSw_Hal)
else()
target_sources(Sts1CobcSw_Periphery PRIVATE FramMock.cpp)
endif()
2 changes: 1 addition & 1 deletion Sts1CobcSw/Periphery/Flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ auto WaitWhileBusy(std::int64_t timeout) -> Result<void>
}


auto ActualBaudRate() -> int32_t
auto ActualBaudRate() -> std::int32_t
{
return spi.BaudRate();
}
Expand Down
2 changes: 1 addition & 1 deletion Sts1CobcSw/Periphery/Flash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ auto Initialize() -> void;
auto ProgramPage(std::uint32_t address, PageSpan data) -> void;
auto EraseSector(std::uint32_t address) -> void;
[[nodiscard]] auto WaitWhileBusy(std::int64_t timeout) -> Result<void>;
auto ActualBaudRate() -> int32_t;
auto ActualBaudRate() -> std::int32_t;
}
183 changes: 183 additions & 0 deletions Sts1CobcSw/Periphery/FramMock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#include <Sts1CobcSw/Periphery/FramMock.hpp>

#include <cstring>


namespace sts1cobcsw::fram
{
auto doInitialize = empty::DoInitialize;
auto doReadDeviceId = empty::DoReadDeviceId;
auto doActualBaudRate = empty::DoActualBaudRate;
auto doWriteTo = empty::DoWriteTo;
auto doReadFrom = empty::DoReadFrom;


// --- Mocked functions ---

auto Initialize() -> void
{
return doInitialize();
}


auto ReadDeviceId() -> DeviceId
{
return doReadDeviceId();
}


auto ActualBaudRate() -> std::int32_t
{
return doActualBaudRate();
}


namespace internal
{
auto WriteTo(Address address, void const * data, std::size_t nBytes, std::int64_t timeout) -> void
{
return doWriteTo(address, data, nBytes, timeout);
}


auto ReadFrom(Address address, void * data, std::size_t nBytes, std::int64_t timeout) -> void
{
return doReadFrom(address, data, nBytes, timeout);
}
}


// --- Set functions ---

auto SetDoInitialize(void (*doInitializeFunction)()) -> void
{
doInitialize = doInitializeFunction;
}


auto SetDoReadDeviceId(DeviceId (*doReadDeviceIdFunction)()) -> void
{
doReadDeviceId = doReadDeviceIdFunction;
}


void SetDoActualBaudRate(std::int32_t (*doActualBaudRateFunction)())
{
doActualBaudRate = doActualBaudRateFunction;
}


auto SetDoWriteTo(void (*doWriteToFunction)(
Address address, void const * data, std::size_t nBytes, std::int64_t timeout)) -> void
{
doWriteTo = doWriteToFunction;
}


auto SetDoReadFrom(void (*doReadFromFunction)(
Address address, void * data, std::size_t nBytes, std::int64_t timeout)) -> void
{
doReadFrom = doReadFromFunction;
}


// --- Predefined do functions ---

namespace empty
{
auto SetAllDoFunctions() -> void
{
SetDoInitialize(DoInitialize);
SetDoReadDeviceId(DoReadDeviceId);
SetDoActualBaudRate(DoActualBaudRate);
SetDoWriteTo(DoWriteTo);
SetDoReadFrom(DoReadFrom);
}


auto DoInitialize() -> void
{
}


auto DoReadDeviceId() -> DeviceId
{
return DeviceId{};
}


auto DoActualBaudRate() -> std::int32_t
{
return 0;
}


auto DoWriteTo([[maybe_unused]] Address address,
[[maybe_unused]] void const * data,
[[maybe_unused]] std::size_t nBytes,
[[maybe_unused]] std::int64_t timeout) -> void
{
}


auto DoReadFrom([[maybe_unused]] Address address,
[[maybe_unused]] void * data,
[[maybe_unused]] std::size_t nBytes,
[[maybe_unused]] std::int64_t timeout) -> void
{
}
}


namespace ram
{
std::array<Byte, storageSize> storage{};


auto SetAllDoFunctions() -> void
{
SetDoInitialize(DoInitialize);
SetDoReadDeviceId(DoReadDeviceId);
SetDoActualBaudRate(DoActualBaudRate);
SetDoWriteTo(DoWriteTo);
SetDoReadFrom(DoReadFrom);
}


auto DoInitialize() -> void
{
}


auto DoReadDeviceId() -> DeviceId
{
static constexpr auto deviceId =
std::to_array({0x03_b, 0x2E_b, 0xC2_b, 0x7F_b, 0x7F_b, 0x7F_b, 0x7F_b, 0x7F_b, 0x7F_b});
return deviceId;
}


auto DoActualBaudRate() -> std::int32_t
{
return 6'000'000; // NOLINT(*magic-numbers*)
}


auto DoWriteTo(Address address,
void const * data,
std::size_t nBytes,
[[maybe_unused]] std::int64_t timeout) -> void
{
std::memcpy(storage.data() + address, data, nBytes);
}


auto DoReadFrom(Address address,
void * data,
std::size_t nBytes,
[[maybe_unused]] std::int64_t timeout) -> void
{
std::memcpy(data, storage.data() + address, nBytes);
}
}
}
53 changes: 53 additions & 0 deletions Sts1CobcSw/Periphery/FramMock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once


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

#include <array>
#include <cstddef>
#include <cstdint>


namespace sts1cobcsw::fram
{
auto SetDoInitialize(void (*doInitializeFunction)()) -> void;
auto SetDoReadDeviceId(DeviceId (*doReadDeviceIdFunction)()) -> void;
auto SetDoActualBaudRate(std::int32_t (*doActualBaudRateFunction)()) -> void;
auto SetDoWriteTo(void (*doWriteToFunction)(
Address address, void const * data, std::size_t nBytes, std::int64_t timeout)) -> void;
auto SetDoReadFrom(void (*doReadFromFunction)(
Address address, void * data, std::size_t nBytes, std::int64_t timeout)) -> void;


// Empty do functions that do nothing; used to initialized function pointers
namespace empty
{
auto SetAllDoFunctions() -> void;

auto DoInitialize() -> void;
auto DoReadDeviceId() -> DeviceId;
auto DoActualBaudRate() -> std::int32_t;
auto DoWriteTo(Address address, void const * data, std::size_t nBytes, std::int64_t timeout)
-> void;
auto DoReadFrom(Address address, void * data, std::size_t nBytes, std::int64_t timeout) -> void;
}


// Do functions that simulate the FRAM in RAM
namespace ram
{
constexpr auto storageSize = (1U << 20U);
extern std::array<Byte, storageSize> storage;


auto SetAllDoFunctions() -> void;

auto DoInitialize() -> void;
auto DoReadDeviceId() -> DeviceId;
auto DoActualBaudRate() -> std::int32_t;
auto DoWriteTo(Address address, void const * data, std::size_t nBytes, std::int64_t timeout)
-> void;
auto DoReadFrom(Address address, void * data, std::size_t nBytes, std::int64_t timeout) -> void;
}
}
4 changes: 2 additions & 2 deletions Sts1CobcSw/Periphery/Rf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ auto SetTxType(TxType txType) -> void
{
// Constants for setting the TX type (morse, 2GFSK)
// MODEM_DATA_RATE: unused, 20 kBaud
static constexpr uint32_t dataRateMorse = 20'000U;
static constexpr std::uint32_t dataRateMorse = 20'000U;
// MODEM_DATA_RATE: For 9k6 Baud: (TX_DATA_RATE * MODEM_TX_NCO_MODE * TXOSR) / F_XTAL_Hz = (9600
// * 2600000 * 10) / 26000000 = 9600 = 0x002580
static constexpr uint32_t dataRate2Gfsk = 9'600U;
static constexpr std::uint32_t dataRate2Gfsk = 9'600U;
// MODEM_MODE_TYPE: TX data from GPIO0 pin, modulation OOK
static constexpr auto modemModTypeMorse = 0x09_b;
// MODEM_MODE_TYPE: TX data from packet handler, modulation 2GFSK
Expand Down
10 changes: 5 additions & 5 deletions Sts1CobcSw/Utility/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace sts1cobcsw::utility
//! @brief Print UTC system time in human readable format
void PrintFormattedSystemUtc()
{
int32_t year = 0;
int32_t month = 0;
int32_t day = 0;
int32_t hour = 0;
int32_t min = 0;
std::int32_t year = 0;
std::int32_t month = 0;
std::int32_t day = 0;
std::int32_t hour = 0;
std::int32_t min = 0;
double sec = 0;

auto sysUtc = RODOS::sysTime.getUTC();
Expand Down
2 changes: 1 addition & 1 deletion Tests/HardwareTests/Fram.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class FramTest : public RODOS::StaticThread<>
Check(deviceId == correctDeviceId);

RODOS::setRandSeed(static_cast<std::uint64_t>(RODOS::NOW()));
constexpr uint32_t nAdressBits = 20U;
constexpr std::uint32_t nAdressBits = 20U;
auto address = fram::Address{RODOS::uint32Rand() % (1U << nAdressBits)};

PRINTF("\n");
Expand Down
6 changes: 6 additions & 0 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ target_link_libraries(Sts1CobcSwTests_Dummy PRIVATE Catch2::Catch2WithMain Sts1C
add_program(FlatArray FlatArray.test.cpp)
target_link_libraries(Sts1CobcSwTests_FlatArray PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Utility)

add_program(Fram Fram.test.cpp)
target_link_libraries(
Sts1CobcSwTests_Fram PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Periphery Sts1CobcSw_Serial
Sts1CobcSw_Utility
)

# TODO: Enable again once problem with segmentation violation on CI is fixed
add_program(LfsRam LfsRam.test.cpp)
target_link_libraries(Sts1CobcSwTests_LfsRam PRIVATE Catch2::Catch2WithMain Sts1CobcSw_FileSystem)
Expand Down
Loading
Loading