39#ifndef _FREERTOS_DRIVERS_ARDUINO_ARDUINOFS_HXX_
40#define _FREERTOS_DRIVERS_ARDUINO_ARDUINOFS_HXX_
42#ifndef ARDUINO_ARCH_STM32
43#error This module only works for STM32 boards.
46#ifndef OPENMRN_HAVE_POSIX_FD
47#error You must add a file build_opt.h to the sketch directory and add -DOPENMRN_HAVE_POSIX_FD to it to make use of this module.
50#include "stm32_eeprom.h"
53#define EEPROM_FILENAME "/dev/eeprom"
61 static constexpr unsigned MAX_FD = 8;
112 for (
int fd = 0; fd <
MAX_FD; ++fd)
114 if (!
fds[fd].in_use())
129 eeprom_buffer_flush();
147int _open_r(
struct _reent *reent,
const char *path,
int flags,
int mode)
149 if (strcmp(path, EEPROM_FILENAME) != 0)
156 eeprom_buffer_fill();
162int _close_r(
struct _reent *reent,
int fd)
174ssize_t _read_r(
struct _reent *reent,
int fd,
void *buf,
size_t count)
182 uint8_t *dst = (uint8_t *)buf;
183 int left = (int)E2END - (
int)finfo->
offset;
194 *dst = eeprom_buffered_read_byte(finfo->
offset);
203ssize_t _write_r(
struct _reent *reent,
int fd,
const void *buf,
size_t count)
211 const uint8_t *src = (
const uint8_t *)buf;
212 int left = (int)E2END - (
int)finfo->
offset;
227 eeprom_buffered_write_byte(finfo->
offset, *src);
247int _stat_r(
struct _reent *reent,
const char *path,
struct stat *stat)
249 if (strcmp(path, EEPROM_FILENAME) != 0)
254 memset(stat, 0,
sizeof(*stat));
255 stat->st_size = E2END;
259int _fstat_r(
struct _reent *reent,
int fd,
struct stat *stat)
266 memset(stat, 0,
sizeof(*stat));
267 stat->st_size = E2END;
271_off_t _lseek_r(
struct _reent *reent,
int fd, _off_t offset,
int whence)
278 off_t new_offset = finfo->
offset;
285 new_offset += offset;
288 new_offset = E2END + offset;
291 new_offset = E2END + 1;
293 if (new_offset > E2END)
298 finfo->
offset = new_offset;
Class that holds information and code for the single-file filesystem for emulating eeprom.
static FileInfo fds[MAX_FD]
Stores all file descriptors.
static constexpr unsigned MAX_FD
Number of possible open file desriptors.
static uint8_t loaded_
1 if we have filled the eeprom buffer at least once since startup.
static constexpr off_t UNUSED_FILE
Offset markng that a file descriptor is not in use.
static void flush_if_dirty()
If there is unflushed writes, performs the flash write.
static FileInfo * get_file(int fd)
Lookup a file descriptor.
static uint8_t dirty_
1 if we have unflushed written data in the eeprom buffer.
static int new_fd()
Allocates a new file descriptor.
We have one of these for each open file descriptor.
off_t offset
POSIX file offset.
void open()
Marks the file descriptor to be in use.
void close()
Marks the file descriptor to be not in use.