-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFstopComms.cpp
264 lines (219 loc) · 5.5 KB
/
FstopComms.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
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
/*
Copyright (C) 2011-2012 William Brodie-Tyrrell
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "Fstopcomms.h"
const char *FstopComms::BAD_WRITE= " Bad Write ";
const char *FstopComms::BAD_READ= " Bad Read ";
const char *FstopComms::CONNECTED= " Host Connected ";
const char *FstopComms::CHECKSUM_FAIL=" Checksum Fail ";
FstopComms::FstopComms(LiquidCrystal &l)
: disp(l)
{
lastlcd=lasttx=lastrx=micros();
connected=false;
}
void FstopComms::begin()
{
Serial.begin(COM_BAUD);
reset();
}
void FstopComms::reset()
{
connected=false;
incmd=false;
buflen=0;
bufwant=1;
lasttx=micros();
}
bool FstopComms::poll()
{
while(Serial.available()){
rx((char)Serial.read());
}
unsigned long now=micros();
// check for timeouts
if(connected && now-lastrx > COM_TIMEOUT){
reset();
}
if(incmd && now-lastrx > COM_CMDTIMEOUT){
reset();
}
if(now-lasttx > COM_PERIOD)
tx(COM_KEEPALIVE);
if(now-lastlcd > COM_LCDTIMEOUT){
disp.setCursor(0, 2);
disp.print(" ");
lastlcd=now;
}
// did we completely fail (timeout or bad command)
return !connected;
}
void FstopComms::rx(char ch)
{
lastrx=micros();
// store byte
if(buflen >= PKT_BUFFER)
buflen=PKT_BUFFER-1;
cmd[buflen++]=ch;
incmd=true;
// got the data we expected...
if(buflen >= bufwant){
if(!connected){
connected=true;
disp.clear();
disp.print(CONNECTED);
lastlcd=lastrx;
}
switch(cmd[0]){
// keepalive; do nothing
case COM_KEEPALIVE:
bufwant=1;
buflen=0;
incmd=false;
break;
// request to read data
case COM_READ:
if(bufwant == 1){
// wait for the rest of the packet
bufwant=PKT_HEADER;
}
else{
respondRead();
}
break;
// request to write data
case COM_WRITE:
if(bufwant == 1){
// wait for rest of header
bufwant=PKT_SHORTHDR;
}
else if(bufwant == PKT_SHORTHDR){
// got length, decide how much packet to wait for
char len=cmd[PKT_LEN];
if(len < 1 || len > PKT_MAXREQ){
nak(BAD_WRITE);
}
else{
bufwant=PKT_HEADER+len; // header+data+checksum
}
}
else{
// got whole packet
respondWrite();
}
break;
// disconnect
default:
reset();
}
}
}
void FstopComms::respondRead()
{
if(!checkcheck())
return;
unsigned int addr=getAddr();
char len=cmd[PKT_LEN];
if(len < 1 || len > PKT_MAXREQ || addr < EEPROM_MIN_READ || addr+len > EEPROM_MAX_READ){
nak(BAD_READ);
return;
}
// read from EEPROM into buffer
cmd[PKT_CMD]=COM_READACK;
for(char i=0;i<len;++i){
cmd[i+PKT_SHORTHDR]=EEPROM.read(addr++);
}
buflen=len+PKT_SHORTHDR;
// append checksum and send.
txCmd();
}
void FstopComms::respondWrite()
{
if(!checkcheck())
return;
unsigned int addr=getAddr();
char len=cmd[PKT_LEN];
if(len < 1 || len > PKT_MAXREQ || addr < EEPROM_MIN_WRITE || addr+len > EEPROM_MAX_WRITE){
nak(BAD_WRITE);
return;
}
// copy from buffer to EEPROM
char bp=PKT_SHORTHDR;
for(char i=0;i<len;++i){
EEPROM.write(addr++, cmd[bp++]);
}
cmd[PKT_CMD]=COM_WRITEACK;
buflen=PKT_SHORTHDR;
// recompute checksum, send without data
txCmd();
}
unsigned int FstopComms::getAddr()
{
unsigned char c1=cmd[PKT_ADDR], c0=cmd[PKT_ADDR+1]; // big-endian
return (int(c1)<<8) + int(c0);
}
bool FstopComms::checkcheck()
{
char chk=checksum();
incmd=false;
buflen=0;
bufwant=1;
if(chk != 0){
error(CHECKSUM_FAIL);
tx(COM_CHKFAIL);
return false;
}
return true;
}
char FstopComms::checksum()
{
char sum=0;
for(int i=0;i<buflen;++i)
sum ^= cmd[i];
return sum;
}
void FstopComms::tx(char ch)
{
lasttx=micros();
Serial.write(ch);
}
void FstopComms::txCmd()
{
// compute & append checksum if there is room
if(buflen < PKT_BUFFER){
cmd[buflen++]=checksum();
}
// transmit
for(int i=0;i<buflen;++i){
Serial.write(cmd[i]);
}
lasttx=micros();
incmd=false;
buflen=0;
bufwant=1;
}
void FstopComms::nak(const char *s)
{
error(s);
tx(COM_NAK);
incmd=false;
buflen=0;
bufwant=1;
}
void FstopComms::error(const char *s)
{
disp.setCursor(0, 2);
disp.print(s);
lastlcd=micros();
}