Skip to content

Commit

Permalink
Add FlashMock.cpp/hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilchmela authored and PatrickKa committed Aug 23, 2024
1 parent 8e8b7e2 commit 80b9b51
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sts1CobcSw/Periphery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
188 changes: 188 additions & 0 deletions Sts1CobcSw/Periphery/FlashMock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include <Sts1CobcSw/Periphery/FlashMock.hpp>

#include <strong_type/type.hpp>

#include <cstring>


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<void>
{
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<void> (*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<void>
{
return outcome_v2::success();
}


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

}
}
78 changes: 78 additions & 0 deletions Sts1CobcSw/Periphery/FlashMock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! @file
//! @brief Driver for the flash memory W25Q01JV.

#pragma once


#include <Sts1CobcSw/Hal/Spi.hpp>
#include <Sts1CobcSw/Outcome/Outcome.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>

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


namespace sts1cobcsw::flash
{
/*struct JedecId
{
std::uint8_t manufacturerId = 0;
std::uint16_t deviceId = 0;
};
enum class ErrorCode
{
timeout = 1
};
template<typename T>
using Result = outcome_v2::experimental::status_result<T, ErrorCode, RebootPolicy>;
[[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<Byte, pageSize>;
using PageSpan = std::span<Byte const, pageSize>;
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<void> (*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<void>;
auto DoActualBaudRate() -> std::int32_t;
}

0 comments on commit 80b9b51

Please sign in to comment.