|
Open Model Railroad Network (OpenMRN)
|
Emulates EEPROM in SPI-FLASH for the CC32xx platform. More...
#include <CC32xxEEPROMEmulation.hxx>
Public Member Functions | |
| CC32xxEEPROMEmulation (const char *name, size_t file_size_bytes) | |
| Constructor. | |
| ~CC32xxEEPROMEmulation () | |
| Destructor. | |
| void | flush () |
| Instructs the driver to write all changed data to disk. | |
| void | mount () |
| Must be called exactly once after creation, after the simplelink stack has been started. | |
Public Member Functions inherited from EEPROM | |
| EEPROM (const char *name, size_t file_size) | |
| Constructor. | |
| size_t | file_size () |
Public Member Functions inherited from Device | |
| Device (const char *name) | |
| Constructor. | |
| virtual | ~Device () |
| Destructor. | |
Private Member Functions | |
| void | write (unsigned int index, const void *buf, size_t len) override |
| Write to the EEPROM. | |
| void | read (unsigned int index, void *buf, size_t len) override |
| Read from the EEPROM. | |
| int | open_file (unsigned sector, uint32_t open_mode, bool ignore_error=false) |
| Opens a SL file. | |
| void | flush_buffers () OVERRIDE |
| REQUIRED: lock_ is held. | |
Private Attributes | |
| unsigned | fileVersion_ {0} |
| This number is increased by one every time the contents are flushed to a file. | |
| uint8_t | readSector_ {0xff} |
| The sector number of the last sector we used for reading. | |
| uint8_t | isDirty_ {0} |
| non-zero if we have unflushed writes. | |
| uint8_t * | data_ |
| Holds the file payload in memory. | |
Static Private Attributes | |
| static const uint8_t | SECTOR_COUNT = 8 |
| The total number of files which we are round-robining. | |
Additional Inherited Members | |
Static Public Member Functions inherited from Device | |
| static int | open (struct _reent *reent, const char *path, int flags, int mode) |
| Open a file or device. | |
| static int | close (struct _reent *reent, int fd) |
| Close a file or device. | |
| static int | stat (struct _reent *reent, const char *path, struct stat *stat) |
| Get the status information of a file or device. | |
| static int | select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long long timeout) |
| POSIX select(). | |
| static void | select_clear () |
| Clears the current thread's select bits. | |
Static Public Member Functions inherited from FileIO | |
| static ssize_t | read (struct _reent *reent, int fd, void *buf, size_t count) |
| Read from a file or device. | |
| static ssize_t | write (struct _reent *reent, int fd, const void *buf, size_t count) |
| Write to a file or device. | |
| static _off_t | lseek (struct _reent *reent, int fd, _off_t offset, int whence) |
| Change the offset index of a file or device. | |
| static int | fstat (struct _reent *reent, int fd, struct stat *stat) |
| Get the status information of a file or device. | |
| static int | ioctl (int fd, unsigned long int key, unsigned long data) |
| Request and ioctl transaction. | |
| static int | fcntl (int fd, int cmd, unsigned long data) |
| Manipulate a file descriptor. | |
| static bool | is_device (int fd) |
| Test if the file descriptor belongs to a device. | |
Protected Member Functions inherited from EEPROM | |
| EEPROM (const char *name, size_t file_size) | |
| Constructor. | |
| ~EEPROM () | |
| Destructor. | |
| size_t | file_size () |
| Get the maximum file size of the EEPROM file. | |
Protected Member Functions inherited from Node | |
| Node (const char *name) | |
| Constructor. | |
| virtual | ~Node () |
| Destructor. | |
| int | open (File *, const char *, int, int) OVERRIDE |
| Open method. | |
| int | close (File *) OVERRIDE |
| Close method. | |
| virtual int | fstat (File *file, struct stat *stat) override |
| Get the status information of a file or device. | |
Protected Member Functions inherited from FileIO | |
| FileIO (const char *name) | |
| Constructor. | |
| virtual | ~FileIO () |
| Destructor. | |
| virtual int | ioctl (File *file, unsigned long int key, unsigned long data) |
| Request an ioctl transaction. | |
| virtual int | fcntl (File *file, int cmd, unsigned long data) |
| Manipulate a file descriptor. | |
| virtual bool | select (File *file, int mode) |
| Device select method. | |
Static Protected Member Functions inherited from Device | |
| static void | select_insert (SelectInfo *info) |
| Add client to list of clients needing woken. | |
| static void | select_wakeup (SelectInfo *info) |
| Wakeup the list of clients needing woken. | |
| static void | select_wakeup_from_isr (SelectInfo *info, int *woken) |
| Wakeup the list of clients needing woken. | |
Static Protected Member Functions inherited from FileIO | |
| static int | fd_alloc (void) |
| Allocate a free file descriptor. | |
| static void | fd_free (int fd) |
| Free up a file descriptor. | |
| static File * | file_lookup (int fd) |
| Looks up a reference to a File corresponding to a given file descriptor. | |
| static int | fd_lookup (File *file) |
| Looks up a file descriptor corresponding to a given File reference. | |
Protected Attributes inherited from Node | |
| OSMutex | lock_ |
| protects internal structures. | |
| mode_t | mode_ |
| File open mode, such as O_NONBLOCK. | |
| unsigned int | references_ |
| number of open references | |
Protected Attributes inherited from FileIO | |
| const char * | name |
| device name | |
Static Protected Attributes inherited from FileIO | |
| static const unsigned int | numOpenFiles = 20 |
| static File | files [] |
| File descriptor pool. | |
| static OSMutex | mutex |
| mutual exclusion for fileio | |
Emulates EEPROM in SPI-FLASH for the CC32xx platform.
Theory of operation:
We use one device file per sector. A sector, when erased, will start the file fresh and empty, in a read-write mode. The file will be in read-write mode until the device reboots. Upon the next boot, the files are opened in a read-only mode, and the available() value is set to zero. This will force upon the first write a new sector to be erased and all data to be copied over to it.
When the EEPROM driver wants to read a block, we load the data from the file into an in-memory cache, and return a pointer into that cache. This is how we map the file into memory. Fortunately we receive a call before the driver needs a different block, so we get a chance to update the memory mapped region.
One major difference is that there is no way to write the "used" block to the past file when the data has been copied over to the new sector. In spite of this we are using a different scheme for the used magic, and fake the writing of the used magic when data is loaded into the cache. The USED magic is virtually set for every sector except the sector last erased that has INTACT data. When we erase a sector we write a "rotation number" to the USED magic offset. The rotation number is incremented every time we jump from sector N-1 to sector 0. This way we can determine upon boot which sector was last erased.
Definition at line 69 of file CC32xxEEPROMEmulation.hxx.
| CC32xxEEPROMEmulation::CC32xxEEPROMEmulation | ( | const char * | name, |
| size_t | file_size_bytes | ||
| ) |
Constructor.
| name | device name |
| file_size_bytes | maximum file size that we can grow to. |
Definition at line 50 of file CC32xxEEPROMEmulation.cxx.
| CC32xxEEPROMEmulation::~CC32xxEEPROMEmulation | ( | ) |
Destructor.
Definition at line 98 of file CC32xxEEPROMEmulation.cxx.
|
inline |
Instructs the driver to write all changed data to disk.
Definition at line 83 of file CC32xxEEPROMEmulation.hxx.
|
privatevirtual |
| void CC32xxEEPROMEmulation::mount | ( | ) |
Must be called exactly once after creation, after the simplelink stack has been started.
Definition at line 59 of file CC32xxEEPROMEmulation.cxx.
|
private |
Opens a SL file.
| sector | number suffix of the filename |
| open_mode | one of the SimpleLink constants FS_MODE_* |
| ignore_error | when false, HASSERT fails upon error |
Definition at line 126 of file CC32xxEEPROMEmulation.cxx.
|
overrideprivatevirtual |
Read from the EEPROM.
| index | index within EEPROM address space to start read |
| buf | location to post read data |
| len | length in bytes of data to read |
Implements EEPROM.
Definition at line 152 of file CC32xxEEPROMEmulation.cxx.
|
overrideprivatevirtual |
Write to the EEPROM.
NOTE!!! This is not necessarily atomic across byte boundaries in the case of power loss. The user should take this into account as it relates to data integrity of a whole block.
| index | index within EEPROM address space to start write |
| buf | data to write |
| len | length in bytes of data to write |
Implements EEPROM.
Definition at line 140 of file CC32xxEEPROMEmulation.cxx.
|
private |
Holds the file payload in memory.
Definition at line 139 of file CC32xxEEPROMEmulation.hxx.
|
private |
This number is increased by one every time the contents are flushed to a file.
The version gets written to the file so that we can recognize which is the latest file.
Definition at line 130 of file CC32xxEEPROMEmulation.hxx.
|
private |
non-zero if we have unflushed writes.
Definition at line 136 of file CC32xxEEPROMEmulation.hxx.
|
private |
The sector number of the last sector we used for reading.
Definition at line 133 of file CC32xxEEPROMEmulation.hxx.
|
staticprivate |
The total number of files which we are round-robining.
Definition at line 125 of file CC32xxEEPROMEmulation.hxx.