Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
FlashFile.hxx
Go to the documentation of this file.
1
36#ifndef _FREERTOS_DRIVERS_COMMON_FLASHFILE_HXX_
37#define _FREERTOS_DRIVERS_COMMON_FLASHFILE_HXX_
38
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <fcntl.h>
42
44
59template <class FLASH> class FlashFile : public DeviceFile
60{
61public:
71 FlashFile(const char *name, FLASH *flash, size_t address, size_t size)
73 , flash_(flash)
74 , flashStart_(address)
75 , size_(size)
76 {
77 auto start_sec = flash_->next_sector_address(flashStart_);
78 // Beginning of the file must be on a sector boundary.
79 HASSERT(start_sec == flashStart_);
80 }
81
83 int open(File *file, const char *path, int flags, int mode) override
84 {
85 if ((flags & O_TRUNC) && ((flags & O_ACCMODE) != O_RDONLY))
86 {
87 // erase entire file.
88 flash_->erase(flashStart_, size_);
89 flags &= ~O_TRUNC;
90 }
91 return DeviceFile::open(file, path, flags, mode);
92 }
93
95 int fstat(File *file, struct stat *stat) override
96 {
98 stat->st_size = size_;
99 return 0;
100 }
101
107 ssize_t write(unsigned int index, const void *buf, size_t len) override
108 {
109 if (index >= size_)
110 {
111 return 0; // EOF
112 }
113 if (len > size_ - index)
114 {
115 len = size_ - index;
116 }
117 size_t addr = flashStart_ + index;
118 size_t sec_addr = flash_->next_sector_address(addr);
119 if (addr == sec_addr)
120 {
122 size_t next_sec = flash_->next_sector_address(sec_addr + 1);
123 flash_->erase(sec_addr, next_sec - sec_addr);
124 sec_addr = next_sec;
125 }
126 if ((sec_addr - addr) < len)
127 {
128 len = sec_addr - addr;
129 }
130 flash_->write(addr, buf, len);
131 return len;
132 }
133
139 ssize_t read(unsigned int index, void *buf, size_t len) override
140 {
141 if (index >= size_)
142 {
143 return 0; // EOF
144 }
145 if (len > size_ - index)
146 {
147 len = size_ - index;
148 }
149 size_t addr = flashStart_ + index;
150 flash_->read(addr, buf, len);
151 return len;
152 }
153
154private:
156 FLASH *flash_;
160 size_t size_;
161};
162
163#endif
Base class for implementing block devices and other file drivers which can be seeked and addressed by...
int open(File *file, const char *path, int flags, int mode) OVERRIDE
Open a device.
static int fstat(struct _reent *reent, int fd, struct stat *stat)
Get the status information of a file or device.
Definition Fileio.cxx:186
const char * name
device name
Definition Devtab.hxx:266
FlashFile is a driver for a single file that is backed by flash.
Definition FlashFile.hxx:60
size_t flashStart_
Offset where our file start on flash.
ssize_t write(unsigned int index, const void *buf, size_t len) override
Write to the flash.
ssize_t read(unsigned int index, void *buf, size_t len) override
Read from the flash.
int open(File *file, const char *path, int flags, int mode) override
Overrides behavior of open for O_TRUNC.
Definition FlashFile.hxx:83
size_t size_
How many bytes our file is on flash.
int fstat(File *file, struct stat *stat) override
Implements querying the file size.
Definition FlashFile.hxx:95
FLASH * flash_
Accessor to the flash device.
FlashFile(const char *name, FLASH *flash, size_t address, size_t size)
Constructor.
Definition FlashFile.hxx:71
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
static int stat(struct _reent *reent, const char *path, struct stat *stat)
Get the status information of a file or device.
Definition Device.cxx:165
File information.
Definition Devtab.hxx:52