-
Notifications
You must be signed in to change notification settings - Fork 0
/
gep.ino
199 lines (167 loc) · 4.17 KB
/
gep.ino
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
#include "common.h"
#include "scom.h"
#include "at28c.h"
enum Command {
READ=1,
WRITE=2,
CLEAR=3,
HANDSHAKE=4
};
enum Response {
ACK=10,
ERROR=20
};
// -----------------------------------------------
// Helper functions for communication protocol
// -----------------------------------------------
Command read_cmd()
{
byte b;
scom_read_bytes(&b,1,"Failed reading command.");
return static_cast<Command>(b);
}
void write_ack()
{
uint8_t b = (uint8_t)ACK;
scom_write_bytes(&b,1,"Failed writing ACK");
}
void check_ack()
{
byte b;
scom_read_bytes(&b,1,"Failed reading response.");
Response resp = static_cast<Response>(b);
if(resp != ACK) {
char buf[30];
sprintf(buf,"Did not receive ACK, instead %d",resp);
BAIL(buf);
}
}
// ----------------------------------------
// High-level EEPROM functions
// ----------------------------------------
#define DATA_SIZE 512
byte data[DATA_SIZE];
void gep_read_data(uint16_t offset, uint16_t n_bytes)
{
for(uint16_t i=0; i<n_bytes; i++) {
data[i] = at28c_read_byte(offset+i);
}
}
void gep_write_data(uint16_t offset, uint16_t n_bytes) {
uint16_t i = 0;
while(i < n_bytes) {
const uint16_t bytes_left = n_bytes-i;
const uint8_t offset_in_page = ((offset+i)%AT28C_PAGE_SIZE);
uint8_t page_size = AT28C_PAGE_SIZE-offset_in_page;
if(bytes_left < (uint16_t)page_size) {
page_size = (uint8_t)bytes_left;
}
at28c_write_page(offset+i,&data[i], page_size);
i += page_size;
}
}
void gep_fill(byte v) {
const int32_t eeprom_size = at28c_get_nbytes();
uint16_t addr = 0;
while(addr < eeprom_size) {
uint8_t page_size = (uint8_t)(eeprom_size-addr);
if(page_size > AT28C_PAGE_SIZE) {
page_size = AT28C_PAGE_SIZE;
}
at28c_fill_page(addr, v, page_size);
addr += page_size;
}
}
void gep_clear() {
gep_fill(0xff);
}
// ----------------------------------------
// Main command handler
// ----------------------------------------
void process_cmd()
{
if(scom_available()) {
log_println("serial data available");
status_set(true);
Command cmd = read_cmd();
switch(cmd) {
case HANDSHAKE:
{
log_println("Received HANDSHAKE command");
uint32_t data = scom_read_u32();
write_ack();
data = ~data;
scom_write_u32(data);
check_ack();
write_ack();
}
break;
case READ:
{
log_println("Received READ command");
uint16_t offset = scom_read_u16();
uint16_t nbytes = scom_read_u16();
char buf[50];
sprintf(buf,"Parameters: offset=%u nbytes=%u",offset,nbytes);
log_println(buf);
write_ack();
gep_read_data(offset,nbytes);
log_println("Read data");
scom_write_bytes(data,nbytes);
log_println("Transmitted data");
check_ack();
write_ack();
}
break;
case WRITE:
{
log_println("Received WRITE command");
uint16_t offset = scom_read_u16();
uint16_t nbytes = scom_read_u16();
char buf[50];
sprintf(buf,"Parameters: offset=%u nbytes=%u",offset,nbytes);
log_println(buf);
write_ack();
scom_read_bytes(data,nbytes);
log_println("Received data");
gep_write_data(offset,nbytes);
log_println("Wrote data");
write_ack();
}
break;
case CLEAR:
log_println("Received CLEAR command");
gep_clear();
write_ack();
break;
default:
BAIL("Invalid command given");
break;
}
log_println("Done processing command");
status_set(false);
}
}
void setup()
{
log_init();
status_init();
scom_init();
ChipConfig at28c256 = {28, 15,
{10, 9, 8, 7, 6, 5, 4, 3, 25, 24, 21, 23, 2, 26, 1},
27, 20, 22, 8, {11, 12, 13, 15, 16, 17, 18, 19}};
ChipConfig at28c16 = {24, 11,
{8, 7, 6, 5, 4, 3, 2, 1, 23, 22, 19},
21, 18, 20, 8, {9, 10, 11, 13, 14, 15, 16, 17}};
/* at28c_init(at28c16); */
at28c_init(at28c256);
status_set(true);
delay(1000);
status_set(false);
log_println("Starting up");
}
void loop()
{
process_cmd();
delay(250);
}