add Chapter13

This commit is contained in:
Amar Mahmutbegovic
2024-11-03 14:41:27 +01:00
parent 4400c3532f
commit 60b08160ba
481 changed files with 650820 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <lfs_ramfs.h>
#define BLOCK_SZ 256
#define BLOCK_NUM 32
#define LFS_LOOKAHEAD_SIZE 8
#define LFS_BLOCK_CYCLES 500
#define LFS_READ_SIZE 16
#define LFS_PROG_SIZE 16
static uint8_t mem[BLOCK_NUM * BLOCK_SZ] = {0};
/*
* Read a region in a block. Negative error codes are propogated
* to the user.
*/
static int lfs_ramfs_read(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size);
/*
* Program a region in a block. The block must have previously
* been erased. Negative error codes are propogated to the user.
* May return LFS_ERR_CORRUPT if the block should be considered bad.
*/
static int lfs_ramfs_prog(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size);
/*
* Erase a block. A block must be erased before being programmed.
* The state of an erased block is undefined. Negative error codes
* are propogated to the user.
* May return LFS_ERR_CORRUPT if the block should be considered bad.
* Not needed for ramfs.
*/
static int lfs_ramfs_erase(const struct lfs_config *c, lfs_block_t block);
/*
* Sync the state of the underlying block device. Negative error codes
* are propogated to the user.
* Not needed for ramfs.
*/
static int lfs_ramfs_sync(const struct lfs_config *c);
/* configuration of the filesystem is provided by this struct */
const struct lfs_config lfs_ramfs_cfg = {
.read = lfs_ramfs_read,
.prog = lfs_ramfs_prog,
.erase = lfs_ramfs_erase,
.sync = lfs_ramfs_sync,
.read_size = LFS_READ_SIZE,
.prog_size = LFS_PROG_SIZE,
.block_size = BLOCK_SZ,
.block_count = BLOCK_NUM,
.cache_size = BLOCK_SZ,
.lookahead_size = LFS_LOOKAHEAD_SIZE,
.block_cycles = LFS_BLOCK_CYCLES,
};
struct lfs_config * get_ramfs_lfs_config() {
return &lfs_ramfs_cfg;
}
static int lfs_ramfs_read(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size)
{
assert(buffer != NULL);
memcpy(buffer, mem + (block * c->block_size) + off, size);
return 0;
}
static int lfs_ramfs_prog(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size)
{
assert(buffer != NULL);
memcpy(mem + (block * c->block_size) + off, buffer, size);
return LFS_ERR_OK;
}
static int lfs_ramfs_erase(const struct lfs_config *c, lfs_block_t block)
{
(void)c;
(void)block;
return LFS_ERR_OK;
}
static int lfs_ramfs_sync(const struct lfs_config *c)
{
(void)c;
return LFS_ERR_OK;
}

View File

@@ -0,0 +1,53 @@
#include <cstdint>
#include <cstdio>
#include <stm32f072xb.h>
#include <hal.hpp>
#include <stm32f0xx_hal_uart.hpp>
#include <uart_stm32.hpp>
#include <retarget.hpp>
#include <lfs.h>
#include <lfs_ramfs.h>
int main()
{
hal::init();
hal::uart_stm32<hal::stm32::uart> uart(USART2);
uart.init();
retarget::set_stdio_uart(&uart);
lfs_t lfs;
const lfs_config * lfs_ramfs_cfg = get_ramfs_lfs_config();
lfs_format(&lfs, lfs_ramfs_cfg);
lfs_mount(&lfs, lfs_ramfs_cfg);
lfs_file_t file;
if(lfs_file_open(&lfs, &file, "song.txt", LFS_O_WRONLY | LFS_O_CREAT) >= 0) {
const char * file_content = "These are some lyrics!";
lfs_file_write(&lfs,
&file,
reinterpret_cast<const void *>(file_content),
strlen(file_content));
lfs_file_close(&lfs, &file);
}
if(lfs_file_open(&lfs, &file, "song.txt", LFS_O_RDONLY) >= 0) {
std::array<char, 64> buff = {0};
lfs_file_read(&lfs,
&file,
reinterpret_cast<void *>(buff.data()),
buff.size() - 1);
printf("This is content from the file\r\n%s\r\n", buff.data());
lfs_file_close(&lfs, &file);
}
while(true)
{
}
}

View File

@@ -0,0 +1,90 @@
#include <cstdint>
#include <cstdio>
#include <stm32f072xb.h>
#include <hal.hpp>
#include <stm32f0xx_hal_uart.hpp>
#include <uart_stm32.hpp>
#include <retarget.hpp>
#include <lfs.h>
#include <lfs_ramfs.h>
namespace fs{
struct lfs {
static inline lfs_t fs_lfs;
static void init() {
const lfs_config * lfs_ramfs_cfg = get_ramfs_lfs_config();
lfs_format(&fs_lfs, lfs_ramfs_cfg);
lfs_mount(&fs_lfs, lfs_ramfs_cfg);
}
};
class file {
public:
file(const char * filename, int flags = LFS_O_RDONLY) {
if(lfs_file_open(&lfs::fs_lfs, &file_, filename, flags) >= 0) {
is_open_ = true;
}
}
~file() {
printf("Closing file in destructor.\r\n");
lfs_file_close(&lfs::fs_lfs, &file_);
}
[[nodiscard]] bool is_open() const {
return is_open_;
}
int read(std::span<char> buff) {
return lfs_file_read(&lfs::fs_lfs,
&file_,
reinterpret_cast<void *>(buff.data()),
buff.size() - 1);
}
void write(std::span<const char> buff) {
lfs_file_write(&lfs::fs_lfs,
&file_,
reinterpret_cast<const void *>(buff.data()),
buff.size());
}
private:
bool is_open_ = false;
lfs_file_t file_;
};
};
int main()
{
hal::init();
hal::uart_stm32<hal::stm32::uart> uart(USART2);
uart.init();
retarget::set_stdio_uart(&uart);
fs::lfs::init();
{
fs::file song_file("song.txt", LFS_O_WRONLY | LFS_O_CREAT);
if(song_file.is_open()) {
song_file.write("These are some lyrics!");
}
}
fs::file song_file("song.txt");
std::array<char, 64> buff = {0};
if(song_file.is_open()) {
song_file.read(buff);
printf("This is content from the file\r\n%s\r\n", buff.data());
}
while(true)
{
}
}