Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TivaEEPROMFile.hxx
Go to the documentation of this file.
1
35#ifndef _FREERTOS_DRIVER_TI_TIVAEEPROMFILE_HXX_
36#define _FREERTOS_DRIVER_TI_TIVAEEPROMFILE_HXX_
37
38#include "driverlib/rom.h"
39#include "driverlib/rom_map.h"
40
41#include "driverlib/eeprom.h"
42#include "driverlib/sysctl.h"
43
45
47{
48public:
49 TivaEEPROMFile(const char *name, unsigned byte_offset, unsigned byte_size)
51 , byteOffset_(byte_offset)
52 , byteSize_(byte_size)
53 {
54 MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0);
55 // Due to Erratum MEM#11 we must never use ROM_EEPROMInit.
56 EEPROMInit();
57 // This is for Tiva 129. For 123 it would be okay to have 2*64.
58 HASSERT(byte_offset % (8 * 64) == 0);
59 HASSERT(byte_size % (8 * 64) == 0);
60 HASSERT(byteOffset_ + byteSize_ <= MAP_EEPROMSizeGet());
61 }
62
64 int fstat(File *file, struct stat *stat) override
65 {
67 stat->st_size = byteSize_;
68 return 0;
69 }
70
76 ssize_t write(unsigned int index, const void *buf, size_t len) override
77 {
78 size_t count = 0;
79 uint8_t *b = (uint8_t *)buf;
80 // Partial write at the beginning.
81 if (index & 0x3)
82 {
83 uint32_t rd = 0;
84 MAP_EEPROMRead(&rd, (index + byteOffset_) & ~0x3, 4);
85 size_t actual_len = 4 - (index & 0x3);
86 actual_len = std::min(len, actual_len);
87 memcpy(((uint8_t*)&rd) + (index & 0x3), b, actual_len);
88 MAP_EEPROMProgram(&rd, (index + byteOffset_) & ~0x3, 4);
89 len -= actual_len;
90 index += actual_len;
91 b += actual_len;
92 count += actual_len;
93 }
94 // Full writes in the middle.
95 size_t word_len = len & ~0x3;
96 if (word_len)
97 {
98 HASSERT((index & 0x3) == 0);
99 MAP_EEPROMProgram((uint32_t *)b, index + byteOffset_, word_len);
100 index += word_len;
101 b += word_len;
102 len -= word_len;
103 count += word_len;
104 }
105 // Partial write at the end.
106 if (len & 0x3)
107 {
108 HASSERT((index & 0x3) == 0);
109 uint32_t rd = 0;
110 MAP_EEPROMRead(&rd, index + byteOffset_, 4);
111 memcpy(&rd, b, len);
112 MAP_EEPROMProgram(&rd, (index + byteOffset_), 4);
113 count += len;
114 }
115 return count;
116 }
117
123 ssize_t read(unsigned int index, void *buf, size_t len) override
124 {
125 size_t count = 0;
126 uint8_t *b = (uint8_t *)buf;
127 // Partial read at the beginning.
128 if (index & 0x3)
129 {
130 uint32_t rd = 0;
131 MAP_EEPROMRead(&rd, (index + byteOffset_) & ~0x3, 4);
132 size_t actual_len = 4 - (index & 0x3);
133 actual_len = std::min(len, actual_len);
134 memcpy(b, ((uint8_t *)&rd) + (index & 0x3), actual_len);
135 len -= actual_len;
136 index += actual_len;
137 b += actual_len;
138 count += actual_len;
139 }
140 // Full reads in the middle.
141 size_t word_len = len & ~0x3;
142 if (word_len)
143 {
144 HASSERT((index & 0x3) == 0);
145 MAP_EEPROMRead((uint32_t *)b, index + byteOffset_, word_len);
146 index += word_len;
147 b += word_len;
148 len -= word_len;
149 count += word_len;
150 }
151 // Partial read at the end.
152 if (len & 0x3)
153 {
154 HASSERT((index & 0x3) == 0);
155 uint32_t rd = 0;
156 MAP_EEPROMRead(&rd, index + byteOffset_, 4);
157 memcpy(b, &rd, len);
158 count += len;
159 }
160 return count;
161 }
162
163private:
165 uint16_t byteOffset_;
167 uint16_t byteSize_;
168};
169
170#endif // _FREERTOS_DRIVER_TI_TIVAEEPROMFILE_HXX_
Base class for implementing block devices and other file drivers which can be seeked and addressed by...
DeviceFile()
Default constructor.
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
ssize_t read(unsigned int index, void *buf, size_t len) override
Read from the eeprom.
uint16_t byteSize_
Total number of bytes in this file.
uint16_t byteOffset_
Address in the EEPROM of the first byte of this file.
ssize_t write(unsigned int index, const void *buf, size_t len) override
Write to the eeprom.
int fstat(File *file, struct stat *stat) override
Implements querying the file size.
#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