Skip to content

Commit

Permalink
Make file system thread safe (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKa authored Nov 26, 2024
2 parents 32f72c9 + a50af0c commit 6572e5b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
1 change: 1 addition & 0 deletions Sts1CobcSw/FileSystem/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_FileSystem PRIVATE rodos::rodos Sts1CobcSw_Periphery)
else()
target_sources(Sts1CobcSw_FileSystem PRIVATE LfsRam.cpp)
target_link_libraries(Sts1CobcSw_FileSystem PRIVATE rodos::without_main_on_linux)
endif()
17 changes: 10 additions & 7 deletions Sts1CobcSw/FileSystem/LfsFlash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Sts1CobcSw/Periphery/Flash.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>

#include <rodos/api/rodos-semaphore.h>
#include <rodos_no_using_namespace.h>

#include <algorithm>
Expand All @@ -30,6 +31,12 @@ auto Lock(lfs_config const * config) -> int;
auto Unlock(lfs_config const * config) -> int;


// TODO: Test with real HW
// max. 3.5 ms acc. W25Q01JV datasheet
constexpr auto pageProgramTimeout = 5 * RODOS::MILLISECONDS;
// max. 400 ms acc. W25Q01JV datasheet (lfs_config.block_size = flash::sectorSize)
constexpr auto blockEraseTimeout = 500 * RODOS::MILLISECONDS;

auto readBuffer = std::array<Byte, lfsCacheSize>{};
auto programBuffer = decltype(readBuffer){};
auto lookaheadBuffer = std::array<Byte, 64>{}; // NOLINT(*magic-numbers)
Expand Down Expand Up @@ -63,11 +70,7 @@ lfs_config const lfsConfig = lfs_config{.context = nullptr,
.metadata_max = flash::sectorSize,
.inline_max = 0};

// TODO: Test with real HW
// max. 3.5 ms acc. W25Q01JV datasheet
constexpr auto pageProgramTimeout = 5 * RODOS::MILLISECONDS;
// max. 400 ms acc. W25Q01JV datasheet (lfs_config.block_size = flash::sectorSize)
constexpr auto blockEraseTimeout = 500 * RODOS::MILLISECONDS;
auto semaphore = RODOS::Semaphore();


auto Initialize() -> void
Expand Down Expand Up @@ -141,16 +144,16 @@ auto Sync([[maybe_unused]] lfs_config const * config) -> int
}


// TODO: Add a proper implementation
auto Lock([[maybe_unused]] lfs_config const * config) -> int
{
semaphore.enter();
return 0;
}


// TODO: Add a proper implementation
auto Unlock([[maybe_unused]] lfs_config const * config) -> int
{
semaphore.leave();
return 0;
}
}
8 changes: 6 additions & 2 deletions Sts1CobcSw/FileSystem/LfsRam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <Sts1CobcSw/FileSystem/LfsMemoryDevice.hpp> // IWYU pragma: associated
#include <Sts1CobcSw/Serial/Byte.hpp>

#include <rodos/api/rodos-semaphore.h>

#include <algorithm>
#include <array>
#include <vector>
Expand Down Expand Up @@ -68,6 +70,8 @@ lfs_config const lfsConfig = lfs_config{.context = nullptr,
.metadata_max = sectorSize,
.inline_max = 0};

auto semaphore = RODOS::Semaphore();


auto Initialize() -> void
{
Expand Down Expand Up @@ -112,16 +116,16 @@ auto Sync([[maybe_unused]] lfs_config const * config) -> int
}


// TODO: Add a proper implementation
auto Lock([[maybe_unused]] lfs_config const * config) -> int
{
semaphore.enter();
return 0;
}


// TODO: Add a proper implementation
auto Unlock([[maybe_unused]] lfs_config const * config) -> int
{
semaphore.leave();
return 0;
}
}
10 changes: 5 additions & 5 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ target_link_libraries(
Sts1CobcSw_Utility
)

add_program(LfsRam LfsRam.test.cpp)
target_link_libraries(
Sts1CobcSwTests_LfsRam PRIVATE Catch2::Catch2WithMain littlefs::littlefs Sts1CobcSw_FileSystem
)

add_program(MajorityVote MajorityVote.test.cpp)
target_link_libraries(
Sts1CobcSwTests_MajorityVote PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Utility
Expand Down Expand Up @@ -86,6 +81,11 @@ target_link_libraries(
)
add_test(NAME FramRingArray COMMAND Sts1CobcSwTests_FramRingArray)

add_program(LfsRam LfsRam.test.cpp UnitTestThread.cpp)
target_link_libraries(
Sts1CobcSwTests_LfsRam PRIVATE rodos::rodos littlefs::littlefs Sts1CobcSw_FileSystem
)

add_program(LfsWrapper LfsWrapper.test.cpp UnitTestThread.cpp)
target_link_libraries(
Sts1CobcSwTests_LfsWrapper PRIVATE rodos::rodos littlefs::littlefs Sts1CobcSw_FileSystem
Expand Down
30 changes: 15 additions & 15 deletions Tests/UnitTests/LfsRam.test.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
#include <Sts1CobcSw/FileSystem/LfsMemoryDevice.hpp>
#include <Tests/UnitTests/UnitTestThread.hpp>

#include <catch2/catch_test_macros.hpp>
#include <Sts1CobcSw/FileSystem/LfsMemoryDevice.hpp>

#include <littlefs/lfs.h>


TEST_CASE("RAM memory device")
auto RunUnitTest() -> void
{
sts1cobcsw::fs::Initialize();
auto lfs = lfs_t{};
auto errorCode = lfs_format(&lfs, &sts1cobcsw::fs::lfsConfig);
REQUIRE(errorCode == 0);
Require(errorCode == 0);
errorCode = lfs_mount(&lfs, &sts1cobcsw::fs::lfsConfig);
REQUIRE(errorCode == 0);
Require(errorCode == 0);

auto const * directoryPath = "MyFolder";
errorCode = lfs_mkdir(&lfs, directoryPath);
REQUIRE(errorCode == 0);
Require(errorCode == 0);

auto const * filePath = "MyFolder/MyFile";
auto file = lfs_file_t{};
errorCode = lfs_file_open(&lfs, &file, filePath, LFS_O_WRONLY | LFS_O_CREAT);
REQUIRE(errorCode == 0);
Require(errorCode == 0);

int number = 123;
errorCode = lfs_file_write(&lfs, &file, &number, sizeof(number));
REQUIRE(errorCode == sizeof(number));
Require(errorCode == sizeof(number));

errorCode = lfs_file_close(&lfs, &file);
REQUIRE(errorCode == 0);
Require(errorCode == 0);
errorCode = lfs_unmount(&lfs);
REQUIRE(errorCode == 0);
Require(errorCode == 0);

errorCode = lfs_mount(&lfs, &sts1cobcsw::fs::lfsConfig);
REQUIRE(errorCode == 0);
Require(errorCode == 0);
errorCode = lfs_file_open(&lfs, &file, filePath, LFS_O_RDONLY);
REQUIRE(errorCode == 0);
Require(errorCode == 0);

int readNumber = 0;
errorCode = lfs_file_read(&lfs, &file, &readNumber, sizeof(number));
REQUIRE(errorCode == sizeof(number));
REQUIRE(readNumber == number);
Require(errorCode == sizeof(number));
Require(readNumber == number);

errorCode = lfs_file_close(&lfs, &file);
REQUIRE(errorCode == 0);
Require(errorCode == 0);
}
1 change: 1 addition & 0 deletions Tests/UnitTests/LfsWrapper.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <algorithm>
#include <array>


using sts1cobcsw::Byte;
using sts1cobcsw::Span;
using sts1cobcsw::fs::Path;
Expand Down

0 comments on commit 6572e5b

Please sign in to comment.