From 80b9b51b488f8c81ab04c1ede22b3af26a21b1a4 Mon Sep 17 00:00:00 2001 From: kamilchmela Date: Sun, 18 Aug 2024 12:27:38 +0000 Subject: [PATCH] Add FlashMock.cpp/hpp --- Sts1CobcSw/Periphery/CMakeLists.txt | 1 + Sts1CobcSw/Periphery/FlashMock.cpp | 188 ++++++++++++++++++++++++++++ Sts1CobcSw/Periphery/FlashMock.hpp | 78 ++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 Sts1CobcSw/Periphery/FlashMock.cpp create mode 100644 Sts1CobcSw/Periphery/FlashMock.hpp diff --git a/Sts1CobcSw/Periphery/CMakeLists.txt b/Sts1CobcSw/Periphery/CMakeLists.txt index deb48f95..e228d648 100644 --- a/Sts1CobcSw/Periphery/CMakeLists.txt +++ b/Sts1CobcSw/Periphery/CMakeLists.txt @@ -9,4 +9,5 @@ if(CMAKE_SYSTEM_NAME STREQUAL Generic) target_link_libraries(Sts1CobcSw_Periphery PUBLIC Sts1CobcSw_Hal) else() target_sources(Sts1CobcSw_Periphery PRIVATE FramMock.cpp) + target_sources(Sts1CobcSw_Periphery PRIVATE FlashMock.cpp) endif() diff --git a/Sts1CobcSw/Periphery/FlashMock.cpp b/Sts1CobcSw/Periphery/FlashMock.cpp new file mode 100644 index 00000000..55b6caef --- /dev/null +++ b/Sts1CobcSw/Periphery/FlashMock.cpp @@ -0,0 +1,188 @@ +#include + +#include + +#include + + +namespace sts1cobcsw::flash +{ +std::int64_t initializeDelay = 0; + + +auto doInitialize = empty::DoInitialize; +auto doReadJedecId = empty::DoReadJedecId; +auto doReadStatusRegister = empty::DoReadStatusRegister; + +auto doReadPage = empty::DoReadPage; +auto doProgramPage = empty::DoProgramPage; +auto doEraseSector = empty::DoEraseSector; +auto doWaitWhileBusy = empty::DoWaitWhileBusy; +auto doActualBaudRate = empty::DoActualBaudRate; + + +// --- Mocked functions --- + +auto Initialize() -> void +{ + return doInitialize(); +} + + +auto ReadJedecId() -> JedecId +{ + return doReadJedecId(); +} + + +auto ReadStatusRegister(std::int8_t registerNo) -> Byte +{ + return doReadStatusRegister(registerNo); +} + + +auto ReadPage(std::uint32_t address) -> Page +{ + return doReadPage(address); +} + + +auto ProgramPage(std::uint32_t address, PageSpan data) -> void +{ + return doProgramPage(address, data); +} + + +auto EraseSector(std::uint32_t address) -> void +{ + return doEraseSector(address); +} + + +auto WaitWhileBusy(std::int64_t timeout) -> Result +{ + return doWaitWhileBusy(timeout); +} + + +auto ActualBaudRate() -> std::int32_t +{ + return doActualBaudRate(); +} + + +// --- Set functions --- + +auto SetDoInitialize(void (*doInitializeFunction)()) -> void +{ + doInitialize = doInitializeFunction; +} + + +auto SetDoReadJedecId(JedecId (*doReadJedecIdFunction)()) -> void +{ + doReadJedecId = doReadJedecIdFunction; +} + + +auto SetDoReadStatusRegister(Byte (*doReadStatusRegisterFunction)(std::int8_t registerNo)) -> void +{ + doReadStatusRegister = doReadStatusRegisterFunction; +} + + +auto SetDoReadPage(Page (*doReadPageFunction)(std::uint32_t address)) -> void +{ + doReadPage = doReadPageFunction; +} + + +auto SetDoProgramPage(void (*doProgramPageFunction)(std::uint32_t address, PageSpan data)) -> void +{ + doProgramPage = doProgramPageFunction; +} + + +auto SetDoEraseSector(void (*doEraseSectorFunction)(std::uint32_t address)) -> void +{ + doEraseSector = doEraseSectorFunction; +} + + +auto SetDoWaitWhileBusyFunction(Result (*doWaitWhileBusy)(std::int64_t timeout)) -> void +{ + doWaitWhileBusy = doWaitWhileBusyFunction; +} + + +auto SetDoActualBaudRate(std::int32_t (*doActualBaudRateFunction)()) -> void +{ + doActualBaudRate = doActualBaudRateFunction; +} + + +// --- Predefined do functions --- + +namespace empty +{ +auto SetAllDoFunctions() -> void +{ + SetDoInitialize(DoInitialize); + SetDoReadJedecId(DoReadJedecId); + SetDoReadStatusRegister(DoReadStatusRegister); + + SetDoReadPage(DoReadPage); + SetDoProgramPage(DoProgramPage); + SetDoEraseSector(DoEraseSector); + SetDoWaitWhileBusy(DoWaitWhileBusy); + SetDoActualBaudRate(DoActualBaudRate); +} + + +auto DoInitialize() -> void +{ + RODOS::AT(initializeDelay); +} + + +auto DoReadJedecId() -> JedecId +{ + return JedecId; +} + + +auto DoReadStatusRegister(std::int8_t registerNo) -> Byte +{ + return 0x0000; +} + + +auto DoReadPage(std::uint32_t address) -> Page +{ + return 0; +} + + +auto DoProgramPage(std::uint32_t address, PageSpan data) -> void +{ +} + + +auto DoEraseSector(std::uint32_t address) -> void +{ +} + + +auto DoWaitWhileBusy(std::int64_t timeout) -> Result +{ + return outcome_v2::success(); +} + + +auto DoActualBaudRate() -> std::int32_t +{ + return 0; +} + +} +} \ No newline at end of file diff --git a/Sts1CobcSw/Periphery/FlashMock.hpp b/Sts1CobcSw/Periphery/FlashMock.hpp new file mode 100644 index 00000000..d8fbb505 --- /dev/null +++ b/Sts1CobcSw/Periphery/FlashMock.hpp @@ -0,0 +1,78 @@ +//! @file +//! @brief Driver for the flash memory W25Q01JV. + +#pragma once + + +#include +#include +#include + +#include +#include +#include +#include + + +namespace sts1cobcsw::flash +{ +/*struct JedecId +{ + std::uint8_t manufacturerId = 0; + std::uint16_t deviceId = 0; +}; + + +enum class ErrorCode +{ + timeout = 1 +}; + + +template +using Result = outcome_v2::experimental::status_result; + + +[[maybe_unused]] constexpr std::size_t pageSize = 256; // bytes +[[maybe_unused]] constexpr std::size_t sectorSize = 4 * 1024; // bytes +[[maybe_unused]] constexpr std::size_t smallBlockSize = 32 * 1024; // bytes +[[maybe_unused]] constexpr std::size_t largeBlockSize = 64 * 1024; // bytes +[[maybe_unused]] constexpr std::size_t flashSize = 128 * 1024 * 1024; // bytes +[[maybe_unused]] constexpr std::size_t nSectors = flashSize / sectorSize; +[[maybe_unused]] constexpr std::size_t nSmallBlocks = flashSize / smallBlockSize; +[[maybe_unused]] constexpr std::size_t nLargeBlocks = flashSize / largeBlockSize; + + +using Page = std::array; +using PageSpan = std::span; + + +inline constexpr auto correctJedecId = JedecId{.manufacturerId = 0xEF, .deviceId = 0x4021}; + +extern hal::Spi spi;*/ + +auto SetDoInitialize(void (*doInitializeFunction)()) -> void; +auto SetDoReadJedecId(JedecId (*doReadJedecIdFunction)()) -> void; +auto SetDoReadStatusRegister(Byte (*doReadStatusRegisterFunction)(std::int8_t registerNo)) -> void; + +auto SetDoReadPage(Page (*doReadPageFunction)(std::uint32_t address)) -> void; +auto SetDoProgramPage(void (*doProgramPageFunction)(std::uint32_t address, PageSpan data)) -> void; +auto SetDoEraseSector(void (*doEraseSectorFunction)(std::uint32_t address)) -> void; +auto SetDoWaitWhileBusyFunction(Result (*doWaitWhileBusy)(std::int64_t timeout)) -> void; +auto SetDoActualBaudRate(std::int32_t (*doActualBaudRateFunction)()) -> void; +} + +namespace empty +{ +auto SetAllDoFunctions() -> void; + +auto DoInitialize() -> void; +auto DoReadJedecId() -> JedecId; +auto DoReadStatusRegister(std::int8_t registerNo) -> Byte; + +auto DoReadPage(std::uint32_t address) -> Page; +auto DoProgramPage(std::uint32_t address, PageSpan data) -> void; +auto DoEraseSector(std::uint32_t address) -> void; +auto DoWaitWhileBusy(std::int64_t timeout) -> Result; +auto DoActualBaudRate() -> std::int32_t; +} \ No newline at end of file