-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.h
275 lines (225 loc) · 7.78 KB
/
Memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// uncomment to use I2C EEPROM instead of SAMD internal flash with EEPROM emulation
//#define I2C_EEPROM
// --------------------------------------------------------------------------------
#ifdef I2C_EEPROM // use I2C EEPROM
#include <extEEPROM.h> // "extEEPROM" via Arduino IDE Library Manager, see https://github.com/PaoloP74/extEEPROM v3.3.5
// One 24LC256 EEPROMs on the bus
extEEPROM i2cEEPROM(kbits_256, 1, 64); // device size, number of devices, page size
/* *****************************************************************************
* I2C MEMORY FUNCTIONS
* Later on, use without lib, like Eugen:
* https://github.com/KONNEKTING/KonnektingFirmware/blob/master/Multi-Interface/IR_Transmitter_1.0/mi.h#L102
*/
byte readMemory(int index) { return i2cEEPROM.read(index); }
void writeMemory(int index, byte val) { i2cEEPROM.write(index, val); }
void updateMemory(int index, byte val) { i2cEEPROM.update(index, val); }
void commitMemory() { } // nothing to do for I2C EEPROM
void setupMemory() {
// uint8_t status = i2cEEPROM.begin(i2cEEPROM.twiClock400kHz); // 400kHz Fast Mode
byte status = i2cEEPROM.begin(i2cEEPROM.twiClock100kHz); // 100kHz Normal Mode
if (status) {
Debug.println(F("extEEPROM.begin() failed, status = %i"), status);
while (1); // block any further processing
}
// Debug.println(F("extEEPROM clear first 2k ..."));
// for(int i=0;i<2048;i++) {
// writeMemory(i,0xff);
// Debug.print(F("."));
// if (i%32==1) {
// Debug.println(F(""));
// }
// }
// Debug.println(F("extEEPROM clear first 2k ... *done*"));
}
#else // use SAMD internal flash storage with EEPROM emulation
// let's have 2k of emulated EEPROM
#define EEPROM_EMULATION_SIZE 4096
// see: https://github.com/cmaglie/FlashStorage >0.7.0
#include <FlashAsEEPROM.h>
void setupMemory() { } // nothing to do when using SAMD Flash "EEPROM"
byte readMemory(int index) { return EEPROM.read(index); }
void writeMemory(int index, byte val) { EEPROM.write(index, val); }
void updateMemory(int index, byte val) { EEPROM.write(index, val); }
void commitMemory() {
Debug.println(F("Doing commit ... %i"), EEPROM.length());
EEPROM.commit();
}
#endif
// ==========================================================
// S P I F L A S H for storing files
// ==========================================================
#include <KonnektingDevice.h>
#include <SPI.h>
#include <SerialFlash.h> // https://github.com/PaulStoffregen/SerialFlash
#define SPIFLASH_CSPIN 9
// reference to current selected file
SerialFlashFile _currFile;
/**
* Formats the flash memory, must in code just before setupMemory(), otherwise function will not be found/known
*/
void formatFlash() {
uint8_t id[5];
SerialFlash.readID(id);
SerialFlash.eraseAll();
while (!SerialFlash.ready()) {
Debug.println(F("SerialFlash Format in progress ..."));
delay(100);
}
Debug.println(F("SerialFlash Format *DONE*"));
}
/**
* setup the flash memory
*/
void setupFlash() {
if (!SerialFlash.begin(SPIFLASH_CSPIN)) {
Debug.println(F("SerialFlash.begin() failed: Unable to access SPI Flash chip"));
while (1) {
};
}
Debug.println(F("SPI Flash initialized"));
//formatFlash(); // --> use KONNEKTING unload-feature to clear spi flash
}
/**
* For debugging purpose: List all files in SPI Flash
*/
void listFiles() {
SerialFlash.opendir();
while (1) {
char filen[64];
uint32_t filesize;
Debug.println("listFiles(): Folder:");
if (SerialFlash.readdir(filen, sizeof(filen), filesize)) {
Debug.println(F("\t'%s' -> %ld bytes"), filen, filesize);
} else {
Debug.println(F("listFiles(): end with folder"));
break; // no more files
}
}
}
/**
* Open file for writing
* @param type
* @param id
* @param size size of file
*/
bool dataOpenWrite(byte type, byte id, unsigned long size) {
char filename[10];
if (type == DATA_TYPE_ID_UPDATE) {
sprintf(filename, "UPDATE.BIN");
} else {
sprintf(filename, "FI_%03d_%03d", type, id);
}
Debug.println(F("dataOpenWrite(): filename=%s size=%d"), filename, size);
if (SerialFlash.exists(filename)) {
// get current file size and compare with new file size
// _currFile = SerialFlash.open(filename);
// unsigned long currsize = _currFile.size();
// _currFile.close();
// if (size != currsize) {
// Debug.println(F("dataOpenWrite(): can't overwrite file with size=%ld with different size=%ld"), currsize, size);
// return false;
// }
Debug.println(F("dataOpenWrite(): removing existing filename=%s"), filename);
SerialFlash.remove(filename);
}
//if (SerialFlash.createErasable(filename, size)) {
if (SerialFlash.create(filename, size)) {
_currFile = SerialFlash.open(filename);
Debug.println(F("dataOpenWrite(): created file with size %d"), size);
if (_currFile) {
listFiles();
return true;
} else {
Debug.println(F("dataOpenWrite(): error creating file on flash"));
return false;
}
}
}
/**
* write data to opened file
* @param data byte[] to write
* @param length number of bytes to write. Number must not be greater that array size
* @return false if file is available for write; true if data has been written
*/
bool dataWrite(byte data[], int length) {
Debug.println(F("dataWrite() length=%d"), length);
if (!_currFile) {
return false;
}
_currFile.write(data, length);
return true;
}
/**
* open file for reading
* @param type
* @param id
* @return file size
*/
unsigned long dataOpenRead(byte type, byte id) {
// just for debugging
listFiles();
char filename[10];
if (type == DATA_TYPE_ID_UPDATE) {
return -1;
} else {
sprintf(filename, "FI_%03d_%03d", type, id);
}
Debug.println(F("dataOpen(): filename=%s"), filename);
if (SerialFlash.exists(filename)) {
_currFile = SerialFlash.open(filename);
Debug.println(F("dataOpen(): filename=%s opened"), filename);
return _currFile.size();
} else {
Debug.println(F("dataOpen(): file '%s' does not exist"), filename);
return -1;
}
}
/**
* read data from opened file
* @param data byte[] to read data into
* @param length number of bytes to read
*
*/
bool dataRead(byte data[], int length) {
if (_currFile.available() > 0) {
_currFile.read(data, length);
} else {
Debug.println(F("dataRead(): nothing available"));
return false;
}
return true;
}
/**
* Close current opened file
*/
bool dataClose() {
if (_currFile != NULL) {
_currFile.close();
return true;
} else {
return false;
}
}
/**
* Removes a file from storage
* @param type
* @param id
* @return true if succeeded, false if remove is not allowed or does not exist
*/
bool dataRemove(byte type, byte id) {
char filename[10];
if (type == DATA_TYPE_ID_UPDATE) {
Debug.println(F("dataRemove(): not allowed to remove update: type=%i id=%i"), type, id);
return false;
}
sprintf(filename, "FI_%03d_%03d", type, id);
//Debug.println(F("dataRemove(): filename=%s"), filename);
if (SerialFlash.exists(filename)) {
//Debug.println(F("dataRemove(): removing existing filename=%s"), filename);
SerialFlash.remove(filename);
return true;
} else {
//Debug.println(F("dataRemove(): file does not exist filename=%s"), filename);
return false;
}
}