-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.c
57 lines (47 loc) · 1.4 KB
/
eeprom.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
#include "eeprom.h"
#include <string.h>
#include <stdlib.h>
#include <libdragon.h>
static byte cursorPosition = 0;
static const byte EEPROM_BLOCKS = 64;
static const byte EEPROM_BLOCK_SIZE = 8;
/**
* Gets the next available eeprom block.
* @return open eeprom block.
*/
byte getEepromCursorPosition() {
if (cursorPosition >= EEPROM_BLOCKS) {
cursorPosition = 0;
}
return cursorPosition;
}
/**
* Writes to eeprom
* @param block The 8-byte block of memory to start with.
* @param data The data to write.
* @return 0 if successful, otherwise an sub-zero error code.
*/
sByte writeToEeprom(const byte blockNumber, const ByteArray* stream) {
if (!eeprom_present()) {
return -1;
}
natural index = 0;
while(index <= stream->Size / EEPROM_BLOCK_SIZE) {
natural start = index * EEPROM_BLOCK_SIZE;
natural end = start + EEPROM_BLOCK_SIZE;
byte* block = calloc(EEPROM_BLOCK_SIZE, sizeof(byte));
if (end <= stream->Size) {
memcpy(block, stream->Data + start, EEPROM_BLOCK_SIZE);
} else {
// pad out the rest of the block with zeroes.
byte diff = end - stream->Size;
memcpy(block, stream->Data + start, EEPROM_BLOCK_SIZE - diff);
}
eeprom_write(blockNumber + index, block);
free(block);
block = null;
index++;
}
cursorPosition += index;
return 0;
}