Skip to content

Commit 6182218

Browse files
ValentiWorkLearningVKorniienko
authored and
VKorniienko
committed
Fixed draft FS coroutines example #17
1 parent d59fa07 commit 6182218

File tree

5 files changed

+60
-52
lines changed

5 files changed

+60
-52
lines changed

Firmware/3rdparty/littlefs_lib/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ target_include_directories(littlefs PUBLIC
1414
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/littlefs>
1515
)
1616

17-
target_compile_definitions(littlefs PUBLIC LFS_YES_TRACE)
17+
target_compile_definitions(littlefs PUBLIC LFS_YES_TRACE )

Firmware/firmware_tests/coroutine/coroutine_thoughts.cpp

+10-25
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,24 @@
2424
#include "wrapper/heap_block_device.hpp"
2525
#include <spdlog/spdlog.h>
2626

27-
CoroUtils::Task<int> coroutineTask()
28-
{
29-
int b = 32;
30-
31-
co_return b;
32-
}
33-
34-
void showFlashDeviceId()
35-
{
36-
CoroUtils::Task<int> task = coroutineTask();
37-
int testValue = co_await task;
38-
}
3927
using TFilesystem = Platform::Fs::Holder<Wrapper::HeapBlockDevice<
4028
Wrapper::kBlockSize,
4129
Wrapper::kSectorsCount,
4230
Wrapper::kReadSize,
4331
Wrapper::kEraseSize>>;
4432

45-
CoroUtils::VoidTask simpleRwTest(TFilesystem& filesystem, std::string_view fileName, std::string_view fileData)
33+
CoroUtils::VoidTask simpleRwTest(
34+
TFilesystem& filesystem,
35+
std::string_view fileName,
36+
std::string_view fileData)
4637
{
4738
spdlog::warn("simpleRwTest begin");
4839
auto lfs = filesystem.fsInstance();
4940
{
5041
spdlog::warn("FILE open begin");
51-
auto holdedFile = filesystem.openFile(fileName);
52-
co_await holdedFile.write(std::span(reinterpret_cast<const std::uint8_t*>(fileData.data()), fileData.size()));
42+
auto filename{filesystem.openFile(fileName)};
43+
co_await filename.write(
44+
std::span(reinterpret_cast<const std::uint8_t*>(fileData.data()), fileData.size()));
5345
spdlog::warn("FILE open finalize");
5446
}
5547

@@ -59,18 +51,17 @@ CoroUtils::VoidTask simpleRwTest(TFilesystem& filesystem, std::string_view fileN
5951
{
6052
spdlog::warn("FILE read begin");
6153
auto holdedFile = filesystem.openFile(fileName);
62-
//co_await holdedFile.read(std::span(readFrom.data(), fileData.size()));
54+
co_await holdedFile.read(std::span(readFrom.data(), fileData.size()));
6355
spdlog::warn("FILE read finalize");
6456
}
6557

66-
auto kCompareStringView{std::string_view{reinterpret_cast<const char*>(readFrom.data()), readFrom.size()}};
58+
auto kCompareStringView{
59+
std::string_view{reinterpret_cast<const char*>(readFrom.data()), readFrom.size()}};
6760
assert(fileData == kCompareStringView);
6861
spdlog::warn("simpleRwTest finalize");
6962
}
7063
CoroUtils::VoidTask fileTest(TFilesystem& filesystem)
7164
{
72-
73-
/* auto fileWrapped = filesystem.openFile("test");*/
7465
constexpr auto kFileData = std::string_view("Hello world!");
7566
simpleRwTest(filesystem, "helloworld.txt", kFileData);
7667

@@ -109,10 +100,6 @@ CoroUtils::VoidTask fileTest(TFilesystem& filesystem)
109100
};
110101
co_await simpleRwTest(filesystem, "nmeaData.txt", kNmeaDataExample);
111102

112-
/*co_await file.write(
113-
{reinterpret_cast<const std::uint8_t*>(kFileData.data()), kFileData.size()});
114-
auto data = co_await file.read(kFileData.size());*/
115-
116103
co_return;
117104
}
118105

@@ -140,8 +127,6 @@ int main()
140127
//};
141128
// displayCoro.fillRectangle(0, 0, 0, 0, nullptr);
142129

143-
showFlashDeviceId();
144-
145130
while (true)
146131
{
147132
using namespace std::chrono_literals;

Firmware/firmware_tests/coroutine/fs_ideas/filesystem_holder.hpp

+47-24
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ template <typename TBlockDeviceEntity> class Holder
3636
using This_t = Holder<TBlockDeviceEntity>;
3737

3838
public:
39-
constexpr Holder()noexcept
39+
constexpr Holder() noexcept
4040
{
4141
m_fsConfig = createLfsConfig();
4242
auto error = lfs_mount(&m_fsInstance, &m_fsConfig);
@@ -46,18 +46,24 @@ template <typename TBlockDeviceEntity> class Holder
4646
lfs_mount(&m_fsInstance, &m_fsConfig);
4747
}
4848
}
49-
constexpr auto fsInstance() noexcept
49+
50+
constexpr decltype(auto) fsInstance() noexcept
5051
{
51-
return m_fsInstance;
52+
return &m_fsInstance;
5253
}
53-
File<This_t> openFile(std::string_view path)noexcept
54+
File<This_t> openFile(std::string_view path) noexcept
5455
{
55-
lfs_file_t file{};
56-
lfs_file_open(&m_fsInstance, &file, path.data(), LFS_O_RDWR | LFS_O_CREAT);
57-
return File<This_t>{std::move(file), &m_fsInstance };
56+
File<This_t> retFile{&m_fsInstance};
57+
lfs_file_open(
58+
&m_fsInstance,
59+
retFile.nativeHandle(FilesystemPasskey{}),
60+
path.data(),
61+
LFS_O_RDWR | LFS_O_CREAT);
62+
63+
return retFile;
5864
}
5965

60-
constexpr TBlockDeviceEntity& getBlockDevice()noexcept
66+
constexpr TBlockDeviceEntity& getBlockDevice() noexcept
6167
{
6268
return m_blockDeviceHolder;
6369
}
@@ -69,7 +75,7 @@ template <typename TBlockDeviceEntity> class Holder
6975
lfs_block_t block,
7076
lfs_off_t off,
7177
void* buffer,
72-
lfs_size_t size)noexcept
78+
lfs_size_t size) noexcept
7379
{
7480
auto pThis = reinterpret_cast<This_t*>(c->context);
7581
pThis->getBlockDevice().read(
@@ -82,7 +88,7 @@ template <typename TBlockDeviceEntity> class Holder
8288
lfs_block_t block,
8389
lfs_off_t off,
8490
const void* buffer,
85-
lfs_size_t size)noexcept
91+
lfs_size_t size) noexcept
8692
{
8793
auto pThis = reinterpret_cast<This_t*>(c->context);
8894
pThis->getBlockDevice().write(
@@ -91,13 +97,13 @@ template <typename TBlockDeviceEntity> class Holder
9197
return LFS_ERR_OK;
9298
}
9399

94-
constexpr static int eraseCall(const struct lfs_config* c, lfs_block_t block)noexcept
100+
constexpr static int eraseCall(const struct lfs_config* c, lfs_block_t block) noexcept
95101
{
96102
auto pThis = reinterpret_cast<This_t*>(c->context);
97103
return LFS_ERR_OK;
98104
}
99105

100-
constexpr static int syncCall(const struct lfs_config* c)noexcept
106+
constexpr static int syncCall(const struct lfs_config* c) noexcept
101107
{
102108
auto pThis = reinterpret_cast<This_t*>(c->context);
103109
return LFS_ERR_OK;
@@ -145,37 +151,54 @@ template <typename TBlockDeviceEntity> class Holder
145151
template <typename TFilesystemHolder> class File
146152
{
147153
public:
148-
explicit constexpr File()noexcept = default;
149-
explicit constexpr File(lfs_file_t&& fileHandle, lfs_t* pFsHolder)noexcept
150-
: m_pFileHandle{ std::move( fileHandle )}, m_pFsHolder{ pFsHolder }
154+
explicit constexpr File(lfs_t* pFsHolder) noexcept
155+
: m_pFileHandle{std::make_unique<lfs_file_t>()}, m_pFsHolder{pFsHolder}
156+
{
157+
}
158+
159+
constexpr File(File&& otherHandle) noexcept
160+
: m_pFileHandle{std::move(otherHandle.m_pFileHandle)}
161+
, m_pFsHolder{std::exchange(otherHandle.m_pFsHolder, nullptr)}
151162
{
152-
spdlog::warn("File::File()");
153163
}
164+
154165
~File()
155166
{
156-
lfs_file_close(m_pFsHolder, &m_pFileHandle);
167+
if (!m_pFsHolder)
168+
return;
169+
170+
lfs_file_close(m_pFsHolder, m_pFileHandle.get());
157171
spdlog::warn("~File::File()");
158172
}
159-
CoroUtils::VoidTask write(std::span<const std::uint8_t> dataHolder)noexcept
173+
174+
CoroUtils::VoidTask write(std::span<const std::uint8_t> dataHolder) noexcept
160175
{
161-
lfs_file_write(m_pFsHolder, &m_pFileHandle, dataHolder.data(), static_cast<lfs_size_t>(dataHolder.size()));
176+
lfs_file_write(
177+
m_pFsHolder,
178+
m_pFileHandle.get(),
179+
dataHolder.data(),
180+
static_cast<lfs_size_t>(dataHolder.size()));
162181
co_return;
163182
}
164183

165-
CoroUtils::Task<std::span<std::uint8_t>> read(std::span<std::uint8_t> outBuffer)noexcept
184+
CoroUtils::Task<std::span<std::uint8_t>> read(std::span<std::uint8_t> outBuffer) noexcept
166185
{
167-
lfs_file_read(m_pFsHolder, &m_pFileHandle, outBuffer.data(), static_cast<lfs_size_t>(outBuffer.size()));
186+
lfs_file_read(
187+
m_pFsHolder,
188+
m_pFileHandle.get(),
189+
outBuffer.data(),
190+
static_cast<lfs_size_t>(outBuffer.size()));
168191
co_return outBuffer;
169192
}
170193

171-
lfs_file_t nativeHandle(const FilesystemPasskey& passkey)noexcept
194+
lfs_file_t* nativeHandle(const FilesystemPasskey& passkey) noexcept
172195
{
173196
Meta::UnuseVar(passkey);
174-
return m_pFileHandle;
197+
return m_pFileHandle.get();
175198
}
176199

177200
private:
178-
lfs_file_t m_pFileHandle;
201+
std::unique_ptr<lfs_file_t> m_pFileHandle;
179202
lfs_t* m_pFsHolder;
180203
};
181204
} // namespace Platform::Fs

Firmware/firmware_tests/coroutine/wrapper/heap_block_device.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class HeapBlockDevice : public BlockDeviceEntity<HeapBlockDevice<BlockSize, Sect
5252
constexpr void write(
5353
std::uint32_t _address,
5454
const std::uint8_t* _blockData,
55-
std::size_t _blockSize) noexcept
55+
std::uint32_t _blockSize) noexcept
5656
{
5757
spdlog::info(
5858
"HeapBlockDevice::WRITE to: address:{0} blockData:{1} blockSize: {2}",

Firmware/firmware_tests/coroutine/wrapper/ih_block_device.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ template <typename TImpl> class BlockDeviceEntity
2727
return offspring()->getEraseSize();
2828
}
2929

30-
constexpr void write(std::uint32_t _address, const std::uint8_t* _blockData, std::size_t _blockSize) noexcept
30+
constexpr void write(std::uint32_t _address, const std::uint8_t* _blockData, std::uint32_t _blockSize) noexcept
3131
{
3232
offspring()->write(_address, _blockData, _blockSize);
3333
}

0 commit comments

Comments
 (0)