Skip to content

Commit 333a0ad

Browse files
ValentiWorkLearningVKorniienko
authored and
VKorniienko
committed
Added draft of the littlefs system [FS] #17
1 parent 70bb5a6 commit 333a0ad

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "Firmware/3rdparty/cppcoro_lib/cppcoro"]
1414
path = Firmware/3rdparty/cppcoro_lib/cppcoro
1515
url = https://github.com/lewissbaker/cppcoro.git
16+
[submodule "Firmware/3rdparty/littlefs_lib/littlefs"]
17+
path = Firmware/3rdparty/littlefs_lib/littlefs
18+
url = https://github.com/littlefs-project/littlefs.git

Firmware/3rdparty/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ endif()
1010
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1111

1212
set( 3RD_PARTY_DIR CACHE STRING ${CMAKE_CURRENT_SOURCE_DIR} )# Libs root path
13-
add_subdirectory( etl )
14-
add_subdirectory( fmt )
13+
add_subdirectory(etl)
14+
add_subdirectory(fmt)
15+
add_subdirectory(littlefs_lib)
1516

1617
target_compile_definitions(fmt
1718
PUBLIC
Submodule littlefs added at ead5080

Firmware/firmware_tests/coroutine/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ target_link_libraries(
3737
watch_display
3838
UtilsLibrary
3939
logger_service
40+
littlefs
4041
)
4142
if(UNIX)
4243
target_link_libraries( coroutine_experiments_app PUBLIC Threads::Threads)

Firmware/firmware_tests/coroutine/coroutine_thoughts.cpp

+104
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
#include <display/display_coro_compact_gc9a01.hpp>
1313
#include <display/display_spi_common_coro.hpp>
1414
#include <logger/logger_service.hpp>
15+
#include <utils/CoroUtils.hpp>
1516

1617
#include <backends/spi_backend_desktop.hpp>
1718
#include <spi/spi_wrapper_async_templated.hpp>
1819

1920
#include "st7789_draft.hpp"
2021

22+
#include <lfs.h>
23+
2124
CoroUtils::Task<int> coroutineTask()
2225
{
2326
int b = 32;
@@ -31,6 +34,106 @@ void showFlashDeviceId()
3134
int testValue = co_await task;
3235
}
3336

37+
namespace Internals::Fs
38+
{
39+
40+
class FilesystemPasskey
41+
{
42+
friend class File;
43+
friend class Holder;
44+
45+
private:
46+
FilesystemPasskey()
47+
{
48+
}
49+
FilesystemPasskey(const FilesystemPasskey&) = default;
50+
FilesystemPasskey& operator=(const FilesystemPasskey&) = delete;
51+
};
52+
53+
class File
54+
{
55+
public:
56+
explicit File(lfs_file_t fileHandle) : m_pFileHandle{fileHandle}
57+
{
58+
}
59+
~File()
60+
{
61+
Holder::Instance().close(this);
62+
}
63+
CoroUtils::VoidTask write(std::span<const std::uint8_t> dataHolder)
64+
{
65+
co_return;
66+
}
67+
68+
CoroUtils::Task<std::span<std::uint8_t>> read(std::size_t dataSize)
69+
{
70+
co_return {};
71+
}
72+
73+
lfs_file_t nativeHandle(const FilesystemPasskey& passkey)
74+
{
75+
Meta::UnuseVar(passkey);
76+
return m_pFileHandle;
77+
}
78+
79+
private:
80+
lfs_file_t m_pFileHandle;
81+
};
82+
class Holder
83+
{
84+
public:
85+
static Holder& Instance()
86+
{
87+
static Holder fsHolder{};
88+
return fsHolder;
89+
}
90+
91+
CoroUtils::Task<File> openFile(std::string_view path)
92+
{
93+
lfs_file_t file{};
94+
lfs_file_open(&m_fsInstance, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
95+
co_return File{file};
96+
}
97+
98+
void close(File* pFile)
99+
{
100+
assert(pFile);
101+
if (!pFile)
102+
return;
103+
auto nativeHandle = pFile->nativeHandle(FilesystemPasskey{});
104+
lfs_file_close(&m_fsInstance, &nativeHandle);
105+
}
106+
107+
private:
108+
static constexpr inline lfs_config fsConfig{};
109+
110+
private:
111+
Holder()
112+
{
113+
auto error = lfs_mount(&m_fsInstance, &fsConfig);
114+
if (error)
115+
{
116+
lfs_format(&m_fsInstance, &fsConfig);
117+
lfs_mount(&m_fsInstance, &fsConfig);
118+
}
119+
}
120+
121+
private:
122+
lfs_t m_fsInstance;
123+
};
124+
} // namespace Internals::Fs
125+
126+
CoroUtils::VoidTask fileTest()
127+
{
128+
auto file = co_await Internals::Fs::Holder::Instance().openFile("test.txt");
129+
constexpr auto kFileData = std::string_view("Hello world!");
130+
co_await file.write(
131+
{reinterpret_cast<const std::uint8_t*>(kFileData.data()), kFileData.size()});
132+
auto data = co_await file.read(kFileData.size());
133+
134+
std::cout << std::string_view(reinterpret_cast<const char*>(data.data()), data.size());
135+
}
136+
34137
int main()
35138
{
36139
using TSpiBus = Interface::SpiTemplated::SpiBus<Interface::SpiTemplated::SpiBusDesktopBackend>;
@@ -42,6 +145,7 @@ int main()
42145
TDisplayDriver compactGc9A01;
43146
compactGc9A01.fillRectangle(0, 0, 0, 0, nullptr);
44147
compactGc9A01.initialize();
148+
45149
// ST7789Coroutine displayCoro{
46150
// Interface::Spi::createSpiBusAsync<Interface::Spi::SpiInstance::M1>()
47151
// , 240

0 commit comments

Comments
 (0)