-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_io.c
471 lines (417 loc) · 15.3 KB
/
flash_io.c
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* flash_io.c
*
* Created: 4/16/2018 3:10:23 PM
* Author: Krystine
*/
#include "flash_io.h"
/* GLOBALS */
static FLASH_ADDRESS_DESCRIPTOR flash_address;
/*************************************************************
* FUNCTION: flash_io_init()
* -----------------------------------------------------------
* This function initializes the flash descriptor. This
* function initializes the active buffer, buffer index, and
* flash page size.
*
* Parameters:
* fd : Pointer to flash descriptor.
* page_size : Page size of flash device.
*
* Returns: void
*************************************************************/
void flash_io_init(FLASH_DESCRIPTOR *fd, int page_size)
{
uint32_t retVal; /* Return value for EEPROM erase/write operations. */
int i = 0; /* Loop control variable. */
/* Initialize the external flash device(s). */
while(i < MAX_NUM_CHIPS)
{
seal_set_active_chip(1);
seal_flash_init();
//seal_flash_erase_device();
i++;
}
/* Set the active chip back to chip 0. */
seal_set_active_chip(0);
flash_address.totalPagesWritten = 0; //TODO: this should only be reset if the device is ready for a new deployment. not if it lost power and data is just now being retrieved.
/* Initialize the buffer index to 0. */
fd->buffer_index = 0;
/* Set the page size for this flash. */
fd->PAGE_SIZE = page_size;
/* Initialize the address descriptor. */
flash_io_reset_addr();
/* Write out flash address. */
eeprom_data.current_flash_addr = flash_address.currentAddress;
eeprom_data.current_flash_chip = flash_address.currentChipInUse;
retVal = flash_erase(&FLASH_NVM, CONFIG_BLOCK_BASE_ADDR, sizeof(eeprom_data));
/* Write out new current address to EEPROM. */
if(retVal == ERR_NONE)
{
retVal = flash_write(&FLASH_NVM, CONFIG_BLOCK_BASE_ADDR, (uint8_t *) &eeprom_data, sizeof(eeprom_data));
}
}
/*************************************************************
* FUNCTION: flash_io_read()
* -----------------------------------------------------------
* This function reads a number of bytes specified by "count".
* If the value of count exceeds a page, only a full page of
* data will be returned through the buffer. The number of
* bytes actually read is returned from this function.
*
* Parameters:
* fd : File descriptor for IO buffers.
* buf : Buffer to return read bytes in.
* count : Number of bytes to read.
*
* Returns:
* size : Number of bytes actually read.
*************************************************************/
uint32_t flash_io_read(FLASH_DESCRIPTOR *fd, uint8_t *buf, size_t count)
{
uint32_t amountRead = 0; /* Number of bytes actually read. */
(void) fd;
if(flash_io_is_busy() == false)
{
/* Update next address pointer. */
update_next_address();
/* Read data into the active buffer. */
seal_flash_read(flash_address.currentAddress, 0x00, buf, PAGE_SIZE_LESS);
/* Check for busy. Will block until read is complete. */
if(flash_io_is_busy() == true)
{
/* Wait until device is done reading. */
seal_flash_wait_until_not_busy();
}
/* Update current address pointer. */
update_current_address();
/* Return amount. */
if(count <= PAGE_SIZE_LESS)
{
amountRead = count;
}
else
{
amountRead = PAGE_SIZE_LESS;
}
} /* END if(flash_io_is_busy() == false) */
else /* Device is busy, return error. */
{
amountRead = -1;
}
return (amountRead);
}
/*************************************************************
* FUNCTION: flash_io_write()
* -----------------------------------------------------------
* This function writes a specific number of bytes to the
* flash device's write buffer. The number of bytes to write
* is specified by "count". If count is greater than a page,
* the buffer will not overflow and all data will still be
* written to the device. The number of bytes actually
* written to the flash buffer is returned from this function.
*
* Parameters:
* fd : File descriptor for IO buffers.
* buf : Buffer containing bytes to write.
* count : Number of bytes to write.
*
* Returns:
* size : Number of bytes actually written.
*************************************************************/
uint32_t flash_io_write(FLASH_DESCRIPTOR *fd, uint8_t *buf, size_t count)
{
uint8_t status; /* Status of flash write operations. */
uint32_t amountWritten = 0; /* Amount of data actually written. */
bool failed = false; /* Holds success or failure of write operation. */
/* Write data to flash buffer if the device is not currently busy. */
if(flash_io_is_busy() == false)
{
/* Write data into the buffers until all data has been written or until the operation fails. */
do /* while((amountWritten < count) && (failed == false)) */
{
/* Write data from the active buffer. */
while((amountWritten < count) && (fd->buffer_index < PAGE_SIZE_LESS))
{
fd->buf_0[fd->buffer_index] = buf[amountWritten];
fd->buffer_index++;
amountWritten++;
}
/* If the buffer is full, write it out to the flash chip after switching which buffer is active. */
if(fd->buffer_index == PAGE_SIZE_LESS)
{
/* Update next address pointer. */
update_next_address();
/* Switch active buffer and reinitialize buffer index. */
fd->buffer_index = 0;
/* Flush data if device is not busy. */
if(flash_io_is_busy() == false) {
status = seal_flash_write(flash_address.currentAddress, 0x00, fd->buf_0, PAGE_SIZE_LESS);
flash_address.totalPagesWritten++;
} else {
failed = true;
}
/* Set failed flag if the write failed. */
if((status&PROG_FAIL) != 0)
{
failed = true;
}
/* Update current address pointer if the program operation didn't fail. */
if(failed == false)
{
update_current_address();
}
}
} while((amountWritten < count) && (failed == false));
/* Return amount of data actually written. */
if(failed == true)
{
if(amountWritten > PAGE_SIZE_LESS) {
amountWritten -= PAGE_SIZE_LESS;
} else {
amountWritten = 0;
}
}
} /* END if(flash_io_is_busy() == false) */
else /* Device is busy, return error. */
{
amountWritten = -1;
}
return (amountWritten);
}
/*************************************************************
* FUNCTION: flash_io_is_busy()
* -----------------------------------------------------------
* This function returns true if the flash device is currently
* busy. It returns false if the device is currently not busy.
*
* Parameters: none
*
* Returns:
* isBusy : Returns TRUE if flash is busy.
*************************************************************/
bool flash_io_is_busy() {
return (seal_flash_is_busy());
}
/*************************************************************
* FUNCTION: flash_io_flush()
* -----------------------------------------------------------
* This function flushes the write buffer. All data currently
* in the buffer will be written out to the flash device.
*
* Parameters:
* fd : Descriptor containing buffers.
*
* Returns: void
*************************************************************/
void flash_io_flush(FLASH_DESCRIPTOR *fd)
{
uint8_t status;
bool failed = false;
if(fd->buffer_index != 0)
{
/* Update next address pointer. */
update_next_address();
/* Flush data if device is not busy. */
if(flash_io_is_busy() == false) {
status = seal_flash_write(flash_address.currentAddress, 0x00, fd->buf_0, fd->buffer_index);
flash_address.totalPagesWritten++;
} else {
failed = true;
}
/* Write data from the active buffer. */
/* Reinitialize buffer index. */
fd->buffer_index = 0;
if((status&PROG_FAIL) != 0)
{
failed = true;
}
/* Update current address pointer if the program operation didn't fail. */
if(failed == false)
{
update_current_address();
}
}
}
/*************************************************************
* FUNCTION: flash_io_reset_addr()
* -----------------------------------------------------------
* This function resets the address pointer back to the
* beginning of the flash device. The pointer goes back to
* block one of the device, which is the first addressable
* block by the user.
*
* Parameters: none
*
* Returns: void
*************************************************************/
void flash_io_reset_addr()
{
/* Calls the NAND_Flash function to reset the address pointer. */
reset_address_info();
}
/*************************************************************
* FUNCTION: update_next_address()
* -----------------------------------------------------------
* This function goes to the next address that should be
* written to or read from. If the block currently being
* updated is at or past the final allowed value based on
* NUM_BLOCKS, then an error message is thrown.
*
* Parameters: none
*
* Returns:
* address : Updated address.
*************************************************************/
uint32_t update_next_address() {
/* Check if block out of main array. */
if(seal_calculate_block_offset(flash_address.currentAddress) >= NUM_BLOCKS)
{
/* Make sure there is still more space on the device. */
if(flash_address.currentChipInUse < (MAX_NUM_CHIPS - 1))
{
/* Switch chip and set address pointer. */
switch_flash_chips();
}
else
{
/* Device out of space! Set global flag. */
xEventGroupSetBits(xSYSEVENTS_handle, EVENT_FLASH_FULL);
}
}
else
{
flash_address.nextAddress++;
}
return (flash_address.nextAddress);
}
/*************************************************************
* FUNCTION: update_current_address()
* -----------------------------------------------------------
* This function goes to the next address that should be
* written to or read from. If the block currently being
* updated is at or past the final allowed value based on
* NUM_BLOCKS, then an error message is thrown.
*
* Parameters: none
*
* Returns:
* address : Updated address.
*************************************************************/
uint32_t update_current_address()
{
uint32_t retVal;
/* Check if block out of main array. */
if(seal_calculate_block_offset(flash_address.currentAddress) >= NUM_BLOCKS)
{
/* Make sure there is still more space on the device. */
if(flash_address.currentChipInUse < (MAX_NUM_CHIPS - 1))
{
/* Switch chip and set address pointer. */
switch_flash_chips();
}
else
{
/* Device out of space! Set global flag. */
xEventGroupSetBits(xSYSEVENTS_handle, EVENT_FLASH_FULL);
}
}
else
{
flash_address.currentAddress++;
}
/* Flash must be erased before a new value may be written to it. */
eeprom_data.current_flash_addr = flash_address.currentAddress;
eeprom_data.current_flash_chip = flash_address.currentChipInUse;
retVal = flash_erase(&FLASH_NVM, CONFIG_BLOCK_BASE_ADDR, sizeof(eeprom_data));
/* Write out new current address to EEPROM. */
if(retVal == ERR_NONE)
{
retVal = flash_write(&FLASH_NVM, CONFIG_BLOCK_BASE_ADDR, (uint8_t *) &eeprom_data, sizeof(eeprom_data));
}
return (flash_address.currentAddress);
}
/*************************************************************
* FUNCTION: get_current_address()
* -----------------------------------------------------------
* This function returns the value currently stored in the
* address descriptor's current address value.
*
* Parameters: none
*
* Returns:
* addressInfo.currentAddress : Current address value.
*************************************************************/
uint32_t get_current_address() {
return flash_address.currentAddress;
}
/*************************************************************
* FUNCTION: get_next_address()
* -----------------------------------------------------------
* This function returns the value currently stored in the
* address descriptor's current address value.
*
* Parameters: none
*
* Returns:
* addressInfo.currentAddress : Current address value.
*************************************************************/
uint32_t get_next_address() {
return flash_address.nextAddress;
}
/*************************************************************
* FUNCTION: reset_address_info()
* -----------------------------------------------------------
* This function reinitializes the address pointer back to the
* beginning of the device. It points back to block one of the
* device. Block zero is the superblock and is not addressable
* by the user.
*
* Parameters: none
*
* Returns: void
*************************************************************/
void reset_address_info()
{
/* Initialize the address descriptor. Initialize block address
* to block 1 (after the superblock). */
flash_address.currentAddress = 0x40;
flash_address.nextAddress = 0x40;
flash_address.currentChipInUse = 0x00;
seal_set_active_chip(0);
}
/*************************************************************
* FUNCTION: switch_flash_chips()
* -----------------------------------------------------------
* This function switches to the next available flash chip and
* sets the address pointer to the first user-addressable
* space.
*
* Parameters: none
*
* Returns: void
*************************************************************/
void switch_flash_chips()
{
/* Switch to next flash chip. */
flash_address.currentChipInUse++;
seal_set_active_chip(flash_address.currentChipInUse);
/* Set address pointer to beginning of new flash chip. */
flash_address.currentAddress = 0x40;
flash_address.nextAddress = 0x40;
}
/*************************************************************
* FUNCTION: num_pages_written()
* -----------------------------------------------------------
* This function returns the total number of pages currently
* written to flash.
*
* Parameters: none
*
* Returns:
* The total number of pages written in external flash.
*************************************************************/
uint32_t num_pages_written()
{
return (flash_address.totalPagesWritten);
}