31 #include "../includes/eeprom.h"
36 #include "../includes/config.h"
37 #include "../includes/mass_flow_meter.h"
38 #include "../includes/parameters.h"
43 #include <HardwareSerial.h>
44 #include <IWatchdog.h>
45 #include <OneButton.h>
48 #define EEPROM_I2C_ADDRESS 0x51
50 unsigned char EEPROM_Buffer[256];
59 #define EEPROM_VIRGIN_NOTINITIALIZED 0xFA
60 #define EEPROM_VIRGIN_TRUE 0xFF
61 #define EEPROM_VIRGIN_FALSE 0xAB
62 uint8_t eeprom_virgin = EEPROM_VIRGIN_NOTINITIALIZED;
64 inline void eeprom_wire_begin(
void) {
70 inline void eeprom_wire_end(
void) {
77 int32_t eeprom_read(
void) {
78 int32_t totalReadCount = 0;
83 readCount = Wire.requestFrom(EEPROM_I2C_ADDRESS, 5, 0XFB, 1,
true);
84 eeprom_crc32.c[0] = Wire.read();
85 eeprom_crc32.c[1] = Wire.read();
86 eeprom_crc32.c[2] = Wire.read();
87 eeprom_crc32.c[3] = Wire.read();
88 eeprom_virgin = Wire.read();
91 if (readCount != 5u) {
93 return EEPROM_ERROR_UNABLE_TO_RW;
96 if (
static_cast<uint8_t
>(EEPROM_VIRGIN_TRUE) == eeprom_virgin) {
98 return EEPROM_ERROR_READ_VIRGIN;
102 for (
unsigned int page = 0; page < ((
sizeof(EEProm_Content) / 16u) + 1u); page++) {
106 readCount = Wire.requestFrom(EEPROM_I2C_ADDRESS, 16, page * 16u, 1,
true);
107 totalReadCount +=
static_cast<int16_t
>(readCount);
108 for (
unsigned int i = 0;
i < 16u;
i++) {
109 EEPROM_Buffer[(page * 16u) +
i] = Wire.read();
115 int32_t returnVal = 0;
116 if (
static_cast<uint64_t
>(totalReadCount) >=
sizeof(EEProm_Content)) {
118 memcpy(&EEProm_Content, &EEPROM_Buffer,
sizeof(EEProm_Content));
120 returnVal = EEPROM_ERROR_UNABLE_TO_RW;
124 uint32_t crc = CRC32::calculate((
unsigned char*)&EEProm_Content,
sizeof(EEProm_Content));
125 if (crc != eeprom_crc32.crc) {
127 return EEPROM_ERROR_CORRUPTED;
134 int32_t eeprom_write(
void) {
135 int32_t totalWriteErrors = 0;
138 for (
unsigned int page = 0; page < ((
sizeof(EEProm_Content) / 16u) + 1u); page++) {
140 Wire.beginTransmission(EEPROM_I2C_ADDRESS);
141 Wire.write(page * 16u);
143 int remainingBytesInPage = ((
sizeof(EEProm_Content) - (page * 16u)) >= 16u)
145 :
sizeof(EEProm_Content) - (page * 16u);
146 Wire.write(&((
reinterpret_cast<uint8_t*
>(&EEProm_Content))[page * 16u]),
147 remainingBytesInPage);
148 totalWriteErrors += Wire.endTransmission();
154 eeprom_crc32.crc = CRC32::calculate((
unsigned char*)&EEProm_Content,
sizeof(EEProm_Content));
156 Wire.beginTransmission(EEPROM_I2C_ADDRESS);
158 Wire.write(eeprom_crc32.c[0]);
159 Wire.write(eeprom_crc32.c[1]);
160 Wire.write(eeprom_crc32.c[2]);
161 Wire.write(eeprom_crc32.c[3]);
162 totalWriteErrors += Wire.endTransmission();
167 if ((
static_cast<uint8_t
>(EEPROM_VIRGIN_NOTINITIALIZED) == eeprom_virgin)
168 || (
static_cast<uint8_t
>(EEPROM_VIRGIN_TRUE) == eeprom_virgin)) {
170 Wire.beginTransmission(EEPROM_I2C_ADDRESS);
172 Wire.write(EEPROM_VIRGIN_FALSE);
173 totalWriteErrors += Wire.endTransmission();
178 return ((totalWriteErrors == 0) ? 0 : EEPROM_ERROR_UNABLE_TO_RW);
181 #if MODE == MODE_EEPROM_TESTS
184 void eeprom_virginize(
void) {
186 for (
int page = 0; page < 16; page++) {
188 Wire.beginTransmission(EEPROM_I2C_ADDRESS);
189 Wire.write(page * 16);
190 for (
int j = 0; j < 16; j++) {
193 Wire.endTransmission();
200 Serial.begin(115200);
201 Serial.print(
"EEprom test program. size of struct =");
202 Serial.println(
sizeof(EEProm_Content));
203 Serial.println(
"commands available :");
204 Serial.println(
"r - Read eeprom");
205 Serial.println(
"w - Write eeprom");
206 Serial.println(
"d - Dump eeprom (I2C read)");
207 Serial.println(
"v - Virginize eeprom");
208 Serial.println(
"b - dump EEProm_Content buffer");
216 if (Serial.available()) {
217 inChar =
static_cast<char>(Serial.read());
220 Serial.println(
"dump EEPROM");
221 for (
int page = 0; page < 16; page++) {
223 int readCount = Wire.requestFrom(EEPROM_I2C_ADDRESS, 16, page * 16, 1,
true);
224 (void)snprintf(buffer,
sizeof(buffer),
"@%02X | ", page * 16);
225 Serial.print(buffer);
226 for (
int i = 0;
i < 16;
i++) {
227 (void)snprintf(buffer,
sizeof(buffer),
"%02X ", Wire.read());
228 Serial.print(buffer);
233 Serial.println(
"====");
237 Serial.println(
"dump EEProm_Content");
238 for (
int page = 0; page < 16; page++) {
239 (void)snprintf(buffer,
sizeof(buffer),
"@%02X | ", page * 16);
240 Serial.print(buffer);
241 for (
int i = 0;
i < 16;
i++) {
242 (void)snprintf(buffer,
sizeof(buffer),
"%02X ",
243 ((
unsigned char*)(&EEProm_Content))[page * 16 +
i]);
244 Serial.print(buffer);
248 Serial.println(
"====");
252 Serial.println(
"VIRGINIZING EEPROM ...");
257 Serial.print(
"writing eeprom... ");
258 int r = eeprom_write();
259 if (EEPROM_ERROR_UNABLE_TO_RW == r) {
260 Serial.println(
"ERROR, unable to write in EEPROM");
262 Serial.println(
"EEPROM written");
267 Serial.print(
"reading eeprom... ");
268 int r = eeprom_read();
269 if (EEPROM_ERROR_CORRUPTED == r) {
270 Serial.println(
"ERROR, eeprom corrupted");
271 }
else if (EEPROM_ERROR_READ_VIRGIN == r) {
272 Serial.println(
"ERROR, eeprom vierge");
273 }
else if (EEPROM_ERROR_UNABLE_TO_RW == r) {
274 Serial.println(
"ERROR, unable to read in EEPROM");
276 Serial.println(
"OK");
281 Serial.println(
"update bytes in the EEProm_Content ");
283 (*((
unsigned char*)&EEProm_Content))++;
volatile uint16_t MFM_force_release_I2C
bool MFM_init(void)
Initialize Mass Flow Meter.
#define MFM_FORCE_RELEASE_I2C_TRUE
#define MFM_FORCE_RELEASE_I2C_FALSE