amiro-os / include / amiro / memoryLayout.h @ 58fe0e0b
History | View | Annotate | Download (8.902 KB)
| 1 |
#ifndef AMIRO_FILESYSTEMIO_H_
|
|---|---|
| 2 |
#define AMIRO_FILESYSTEMIO_H_
|
| 3 |
|
| 4 |
/*! \brief memory layouts for the eeproms
|
| 5 |
*
|
| 6 |
* This header defines the memory layout.
|
| 7 |
* Every memory layout starts with a preamble
|
| 8 |
* of 3 byte. The first one is a magic byte
|
| 9 |
* for checking purpose. The second one declares
|
| 10 |
* the byte structure major version (BSMV) and the third
|
| 11 |
* the byte structure minor version (bsmv) of the subsequently
|
| 12 |
* bytes in the memory. With different major versions, the
|
| 13 |
* layout of the memory may totaly change, so one should
|
| 14 |
* not read from such a different layout if the version
|
| 15 |
* differs from the expected one. With greater minor
|
| 16 |
* versions the old memory layout may not change, but
|
| 17 |
* new elements emerge.
|
| 18 |
*/
|
| 19 |
|
| 20 |
#include <amiro/eeprom/at24.hpp> |
| 21 |
#include <chprintf.h> |
| 22 |
|
| 23 |
namespace amiro {
|
| 24 |
namespace fileSystemIo {
|
| 25 |
|
| 26 |
class FileSystemIoBase {
|
| 27 |
|
| 28 |
public:
|
| 29 |
AT24 *at24c01; |
| 30 |
const uint8_t BSMV;
|
| 31 |
const uint8_t bsmv;
|
| 32 |
const uint8_t HMV;
|
| 33 |
const uint8_t hmv;
|
| 34 |
const uint8_t magicByteValue = 0xAAu; |
| 35 |
|
| 36 |
// TODO implement CRC check
|
| 37 |
struct preamble {
|
| 38 |
uint8_t magicByte; |
| 39 |
uint8_t byteStructureMajorVersion; |
| 40 |
uint8_t byteStructureMinorVersion; |
| 41 |
uint8_t hardwareMajorVersion; |
| 42 |
uint8_t hardwareMinorVersion; |
| 43 |
uint8_t reserved_0x04_0x14[15];
|
| 44 |
}; |
| 45 |
|
| 46 |
enum{
|
| 47 |
OK = 0x00u,
|
| 48 |
NOT_IMPLEMENTED = 0x01u,
|
| 49 |
WRONG_BSMV = 0x02u,
|
| 50 |
WRONG_bsmv = 0x03u,
|
| 51 |
IO_ERROR = 0x04u,
|
| 52 |
WRONG_MAGICBYTE_VALUE = 0x05u,
|
| 53 |
WRONG_INDEX = 0x06u,
|
| 54 |
WRONG_HMV = 0x07u,
|
| 55 |
WRONG_hmv = 0x08u,
|
| 56 |
}; |
| 57 |
|
| 58 |
public:
|
| 59 |
|
| 60 |
FileSystemIoBase(AT24 *at24c01, uint8_t BSMV, uint8_t bsmv, uint8_t HMV, uint8_t hmv) |
| 61 |
: at24c01(at24c01), |
| 62 |
BSMV(BSMV), |
| 63 |
bsmv(bsmv), |
| 64 |
HMV(HMV), |
| 65 |
hmv(hmv) {
|
| 66 |
} |
| 67 |
|
| 68 |
msg_t getCheck(){
|
| 69 |
|
| 70 |
// Get the preamble
|
| 71 |
const uint8_t bufferSize = sizeof(FileSystemIoBase::preamble); |
| 72 |
uint8_t buffer[bufferSize]; |
| 73 |
chFileStreamSeek((BaseFileStream*)at24c01, offsetof(FileSystemIoBase::preamble, magicByte)); |
| 74 |
msg_t bytesWritten = chSequentialStreamRead((BaseFileStream*)at24c01, (uint8_t*) buffer, sizeof(FileSystemIoBase::preamble));
|
| 75 |
|
| 76 |
if (bytesWritten != bufferSize) {
|
| 77 |
return IO_ERROR;
|
| 78 |
} else if (buffer[offsetof(FileSystemIoBase::preamble, magicByte)] != magicByteValue) { |
| 79 |
return WRONG_MAGICBYTE_VALUE;
|
| 80 |
} else if (buffer[offsetof(FileSystemIoBase::preamble, byteStructureMajorVersion)] != BSMV) { |
| 81 |
return WRONG_BSMV;
|
| 82 |
} else if (buffer[offsetof(FileSystemIoBase::preamble, byteStructureMinorVersion)] < bsmv) { |
| 83 |
return WRONG_bsmv;
|
| 84 |
} else if (buffer[offsetof(FileSystemIoBase::preamble, hardwareMajorVersion)] != HMV) { |
| 85 |
return WRONG_HMV;
|
| 86 |
} else if (buffer[offsetof(FileSystemIoBase::preamble, hardwareMinorVersion)] < hmv) { |
| 87 |
return WRONG_hmv;
|
| 88 |
} else {
|
| 89 |
return OK;
|
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
msg_t initMemory(){
|
| 94 |
|
| 95 |
// Get the preamble
|
| 96 |
const uint8_t bufferSize = offsetof(FileSystemIoBase::preamble, hardwareMinorVersion) + 1; |
| 97 |
uint8_t buffer[bufferSize]; |
| 98 |
|
| 99 |
chFileStreamSeek((BaseFileStream*)at24c01, offsetof(FileSystemIoBase::preamble, magicByte)); |
| 100 |
|
| 101 |
buffer[offsetof(FileSystemIoBase::preamble, magicByte)] = magicByteValue; |
| 102 |
buffer[offsetof(FileSystemIoBase::preamble, byteStructureMajorVersion)] = BSMV; |
| 103 |
buffer[offsetof(FileSystemIoBase::preamble, byteStructureMinorVersion)] = bsmv; |
| 104 |
buffer[offsetof(FileSystemIoBase::preamble, hardwareMajorVersion)] = HMV; |
| 105 |
buffer[offsetof(FileSystemIoBase::preamble, hardwareMinorVersion)] = hmv; |
| 106 |
|
| 107 |
msg_t bytesWritten = chSequentialStreamWrite((BaseFileStream*)at24c01, (uint8_t*) buffer, bufferSize); |
| 108 |
|
| 109 |
if (bytesWritten != bufferSize) {
|
| 110 |
return IO_ERROR;
|
| 111 |
} else {
|
| 112 |
return OK;
|
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
class DiWheelDriveFSIO : public FileSystemIoBase {
|
| 122 |
private:
|
| 123 |
|
| 124 |
/** \brief Layout for DiWheelDriveFSIO with BSMV 1 and bsmv 1*/
|
| 125 |
struct DiWheelDrive_1_1 {
|
| 126 |
uint8_t reserved_0x00_0x14[20];
|
| 127 |
uint16_t vcnl4020offset[4]; // U [12, 13, 15, 16] |
| 128 |
uint8_t generalPurpose[96]; // 1Cx_7Cx |
| 129 |
uint8_t reserved_0x7D_0x80[4]; // TODO CRC |
| 130 |
}; |
| 131 |
|
| 132 |
public:
|
| 133 |
DiWheelDriveFSIO(AT24 *at24c01, uint8_t BSMV, uint8_t bsmv, uint8_t HMV, uint8_t hmv) |
| 134 |
: FileSystemIoBase(at24c01, BSMV, bsmv, HMV, hmv) {
|
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
msg_t getVcnl4020Offset (uint16_t *buffer, uint8_t idx) {
|
| 139 |
|
| 140 |
msg_t bytesWritten; |
| 141 |
const uint8_t bufferSize = sizeof(uint16_t); |
| 142 |
|
| 143 |
// Check if the vcnl index is in a valid range
|
| 144 |
if (idx >= sizeof(DiWheelDrive_1_1::vcnl4020offset)) { |
| 145 |
return WRONG_INDEX;
|
| 146 |
} |
| 147 |
|
| 148 |
// Get the data out of the memory
|
| 149 |
switch (this->BSMV) {
|
| 150 |
case 0x01u: |
| 151 |
// Set the pointer to the address
|
| 152 |
chFileStreamSeek((BaseFileStream*)at24c01, offsetof(DiWheelDriveFSIO::DiWheelDrive_1_1, vcnl4020offset[idx])); |
| 153 |
switch (this->bsmv) {
|
| 154 |
default:
|
| 155 |
bytesWritten = chSequentialStreamRead((BaseFileStream*)at24c01, (uint8_t*) buffer, bufferSize); |
| 156 |
break;
|
| 157 |
} |
| 158 |
break;
|
| 159 |
} |
| 160 |
|
| 161 |
if (bytesWritten != bufferSize) {
|
| 162 |
return IO_ERROR;
|
| 163 |
} else {
|
| 164 |
return OK;
|
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
msg_t setVcnl4020Offset (uint16_t *buffer, uint8_t idx) {
|
| 169 |
|
| 170 |
msg_t bytesWritten; |
| 171 |
const uint8_t bufferSize = sizeof(uint16_t); |
| 172 |
|
| 173 |
// Check if the vcnl index is in a valid range
|
| 174 |
if (idx >= sizeof(DiWheelDrive_1_1::vcnl4020offset)) { |
| 175 |
return WRONG_INDEX;
|
| 176 |
} |
| 177 |
|
| 178 |
// Get the data out of the memory
|
| 179 |
switch (this->BSMV) {
|
| 180 |
case 0x01u: |
| 181 |
// Set the pointer to the address
|
| 182 |
chFileStreamSeek((BaseFileStream*)at24c01, offsetof(DiWheelDriveFSIO::DiWheelDrive_1_1, vcnl4020offset[idx])); |
| 183 |
switch (this->bsmv) {
|
| 184 |
default:
|
| 185 |
bytesWritten = chSequentialStreamWrite((BaseFileStream*)at24c01, (const uint8_t*) buffer, bufferSize);
|
| 186 |
break;
|
| 187 |
} |
| 188 |
break;
|
| 189 |
} |
| 190 |
|
| 191 |
if (bytesWritten != bufferSize) {
|
| 192 |
return IO_ERROR;
|
| 193 |
} else {
|
| 194 |
return OK;
|
| 195 |
} |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
class PowerBoardFSIO : public FileSystemIoBase {
|
| 200 |
private:
|
| 201 |
|
| 202 |
/** \brief Layout for PowerBoardFSIO with BSMV 1 and bsmv 1*/
|
| 203 |
struct PowerBoard_1_1 {
|
| 204 |
uint8_t reserved_0x00_0x14[20];
|
| 205 |
uint16_t vcnl4020offset[8]; // U1.Proximity [11, 21, 31, 41, 12, 22, 32, 42] |
| 206 |
uint8_t generalPurpose[88]; // 24x_7Cx |
| 207 |
uint8_t reserved_0x7D_0x80[4]; // TODO CRC |
| 208 |
}; |
| 209 |
|
| 210 |
public:
|
| 211 |
PowerBoardFSIO(AT24 *at24c01, uint8_t BSMV, uint8_t bsmv, uint8_t HMV, uint8_t hmv) |
| 212 |
: FileSystemIoBase(at24c01, BSMV, bsmv, HMV, hmv) {
|
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
msg_t getVcnl4020Offset (uint16_t *buffer, uint8_t idx) {
|
| 217 |
|
| 218 |
msg_t bytesWritten; |
| 219 |
const uint8_t bufferSize = sizeof(uint16_t); |
| 220 |
|
| 221 |
// Check if the vcnl index is in a valid range
|
| 222 |
if (idx >= sizeof(PowerBoard_1_1::vcnl4020offset)) { |
| 223 |
return WRONG_INDEX;
|
| 224 |
} |
| 225 |
|
| 226 |
// Get the data out of the memory
|
| 227 |
switch (this->BSMV) {
|
| 228 |
case 0x01u: |
| 229 |
// Set the pointer to the address
|
| 230 |
chFileStreamSeek((BaseFileStream*)at24c01, offsetof(PowerBoardFSIO::PowerBoard_1_1, vcnl4020offset[idx])); |
| 231 |
switch (this->bsmv) {
|
| 232 |
default:
|
| 233 |
bytesWritten = chSequentialStreamRead((BaseFileStream*)at24c01, (uint8_t*) buffer, bufferSize); |
| 234 |
break;
|
| 235 |
} |
| 236 |
break;
|
| 237 |
} |
| 238 |
|
| 239 |
if (bytesWritten != bufferSize) {
|
| 240 |
return IO_ERROR;
|
| 241 |
} else {
|
| 242 |
return OK;
|
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
msg_t setVcnl4020Offset (uint16_t *buffer, uint8_t idx) {
|
| 247 |
|
| 248 |
msg_t bytesWritten; |
| 249 |
const uint8_t bufferSize = sizeof(uint16_t); |
| 250 |
|
| 251 |
// Check if the vcnl index is in a valid range
|
| 252 |
if (idx >= sizeof(PowerBoard_1_1::vcnl4020offset)) { |
| 253 |
return WRONG_INDEX;
|
| 254 |
} |
| 255 |
|
| 256 |
// Get the data out of the memory
|
| 257 |
switch (this->BSMV) {
|
| 258 |
case 0x01u: |
| 259 |
// Set the pointer to the address
|
| 260 |
chFileStreamSeek((BaseFileStream*)at24c01, offsetof(PowerBoardFSIO::PowerBoard_1_1, vcnl4020offset[idx])); |
| 261 |
switch (this->bsmv) {
|
| 262 |
default:
|
| 263 |
bytesWritten = chSequentialStreamWrite((BaseFileStream*)at24c01, (const uint8_t*) buffer, bufferSize);
|
| 264 |
break;
|
| 265 |
} |
| 266 |
break;
|
| 267 |
} |
| 268 |
|
| 269 |
if (bytesWritten != bufferSize) {
|
| 270 |
return IO_ERROR;
|
| 271 |
} else {
|
| 272 |
return OK;
|
| 273 |
} |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
class LightRingFSIO : public FileSystemIoBase {
|
| 278 |
private:
|
| 279 |
|
| 280 |
/** \brief Layout for LightRingFSIO with BSMV 1 and bsmv 1*/
|
| 281 |
struct LightRing_1_1 {
|
| 282 |
uint8_t reserved_0x00_0x14[20];
|
| 283 |
uint8_t generalPurpose[104]; // 15x_80x |
| 284 |
uint8_t reserved_0x7D_0x80[4]; // TODO CRC |
| 285 |
}; |
| 286 |
|
| 287 |
public:
|
| 288 |
LightRingFSIO(AT24 *at24c01, uint8_t BSMV, uint8_t bsmv, uint8_t HMV, uint8_t hmv) |
| 289 |
: FileSystemIoBase(at24c01, BSMV, bsmv, HMV, hmv) {
|
| 290 |
|
| 291 |
} |
| 292 |
}; |
| 293 |
|
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
#endif /* AMIRO_FILESYSTEMIO_H_ */ |