-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathEeprom.cpp
109 lines (97 loc) · 2.04 KB
/
Eeprom.cpp
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
#include "Eeprom.h"
#include <avr/eeprom.h>
#include <string.h>
#include "Host.h"
#include "GcodeQueue.h"
#include <avr/pgmspace.h>
#define EEPROM_MAX 4095
namespace eeprom
{
bool writing = false;
bool reading = false;
char const* GUARDSTR = "SJ14";
int const GUARDLEN = 4;
int eepromptr = 0;
void Stop()
{
if(writing)
{
writing = false;
eeprom_busy_wait();
eeprom_update_byte((uint8_t *)eepromptr, 0xFF);
}
if(reading)
reading = false;
}
bool beginRead()
{
if(writing || reading)
return false;
eepromptr = 0;
eeprom_busy_wait();
char vbuf[GUARDLEN+1] = { 0 };
eeprom_read_block(vbuf, (void *)0, GUARDLEN);
if(strcmp(GUARDSTR,vbuf) != 0)
return false;
eepromptr = GUARDLEN;
reading = true;
return true;
}
bool beginWrite()
{
if(reading || writing)
return false;
eepromptr = 0;
eeprom_busy_wait();
eeprom_update_block(GUARDSTR, (void *)0, GUARDLEN);
eepromptr = GUARDLEN;
writing = true;
return true;
}
bool writebytes(char const* bytes, int len)
{
if(!writing)
return false;
if(eepromptr + len >= EEPROM_MAX)
{
HOST.write_P(PSTR("EEPROM OVERFLOW"));
HOST.endl();
writing = false;
return false;
}
eeprom_busy_wait();
eeprom_update_block(bytes, (void *)eepromptr, len);
eepromptr += len;
return true;
}
void update()
{
if(!reading)
return;
if(GCODES.isFull())
return;
uint8_t buf[MAX_GCODE_FRAG_SIZE];
int x = 0;
for(;x<MAX_GCODE_FRAG_SIZE;x++)
{
eeprom_busy_wait();
buf[x] = eeprom_read_byte((uint8_t *)eepromptr);
//HOST.write(buf[x]);
eepromptr++;
if(eepromptr >= EEPROM_MAX)
{
reading = false;
buf[x] = '\n';
}
if(buf[x] <= 32)
break;
if(buf[x] == 0xFF)
{
HOST.write_P(PSTR("READPAST\n"));
buf[x] = '\n';
reading = false;
}
}
GCODES.parsebytes((char *) buf, x, EEPROM_SOURCE);
}
};