-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserialinterface.cpp
326 lines (286 loc) · 9.59 KB
/
serialinterface.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
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
#include "serialinterface.hpp"
SerialInterface::SerialInterface(Stream* serial, MemoryChip* memoryChip) :
_serial(serial), _memoryChip(memoryChip) {}
bool SerialInterface::update()
{
// Return true if busy (i.e. next update will continue a task), false if not.
switch (_state) {
case SerialState::WAITING_FOR_COMMAND:
return _checkForCommand();
break;
case SerialState::READING:
return _stateReading();
break;
case SerialState::WRITING:
return _stateWriting();
break;
}
return false;
}
void SerialInterface::_turnMemoryOnTemporarily()
{
_prevMemoryPowerState = _memoryChip->getIsOn();
if (!_prevMemoryPowerState) {
_memoryChip->powerOn();
}
}
void SerialInterface::_returnMemoryPowerState()
{
if (_prevMemoryPowerState && !_memoryChip->getIsOn()) {
_memoryChip->powerOn();
} else if (!_prevMemoryPowerState && _memoryChip->getIsOn()) {
_memoryChip->powerOff();
}
}
int SerialInterface::_readByteWithTimeout(uint8_t& n)
{
if (!_serial->available()) {
unsigned long int timeout = _serial->getTimeout();
unsigned long int startedWaitingForByte = millis();
while (!_serial->available()) {
if (millis() - startedWaitingForByte >= timeout) {
return 1;
}
}
}
n = _serial->read();
return 0;
}
int SerialInterface::_readUint32WithTimeout(uint32_t& n)
{
int errorCode;
uint32_t x = 0;
uint8_t b;
for (int shift = (sizeof(uint32_t) - 1) * 8; shift >= 0; shift -= 8) {
if ((errorCode = _readByteWithTimeout(b)) != 0) {
return errorCode;
}
x |= static_cast<uint32_t>(b) << shift;
}
n = x;
return 0;
}
// I couuuuld turn these two into a template, but it's not quite worth
// the structuring headache that C++ and the Arduino IDE impose together.
void SerialInterface::_writeUint16(uint16_t n)
{
_serial->write(static_cast<uint8_t>(n >> 8));
_serial->write(static_cast<uint8_t>(n));
}
void SerialInterface::_writeUint32(uint32_t n)
{
for (int shift = (sizeof(uint32_t) - 1) * 8; shift >= 0; shift -= 8) {
_serial->write(static_cast<uint8_t>(n >> shift));
}
}
bool SerialInterface::_checkForCommand()
{
if (_serial->available()) {
uint8_t command = _serial->read();
_serial->write(command);
uint8_t ack;
if (_readByteWithTimeout(ack) != 0) {return false;}
if (ack != 0) {
return false;
}
// When implementing a new command, make sure to call
// _turnMemoryOnTemporarily before and _returnMemoryPowerState after.
switch (command) {
case static_cast<uint8_t>(SerialCommand::GET_VERSION):
_writeUint16(FRAMUNE_PROTOCOL_VERSION);
break;
case static_cast<uint8_t>(SerialCommand::SET_AND_ANALYZE_CHIP):
return _commandSetAndAnalyzeChip();
break;
case static_cast<uint8_t>(SerialCommand::READ):
return _commandRead();
break;
case static_cast<uint8_t>(SerialCommand::WRITE):
return _commandWrite();
break;
}
}
return false;
}
bool SerialInterface::_commandSetAndAnalyzeChip()
{
MemoryChipKnownProperties receivedKnownProperties;
MemoryChipProperties receivedProperties;
if (_receiveMemoryChipProperties(receivedKnownProperties,
receivedProperties) != 0) {
return false;
}
_turnMemoryOnTemporarily();
_memoryChip->setProperties(&receivedKnownProperties,
&receivedProperties);
_memoryChip->analyzeUnknownProperties();
_memoryChip->getProperties(&receivedKnownProperties,
&receivedProperties);
_returnMemoryPowerState();
_sendMemoryChipProperties(receivedKnownProperties, receivedProperties);
return false;
}
int SerialInterface::_receiveMemoryChipProperties(
MemoryChipKnownProperties& knownProperties,
MemoryChipProperties& properties
)
{
int errorCode;
uint8_t n;
if ((errorCode = _readByteWithTimeout(n)) != 0) {return errorCode;}
knownProperties.isOperational = n;
if ((errorCode = _readByteWithTimeout(n)) != 0) {return errorCode;}
knownProperties.size = n;
if ((errorCode = _readByteWithTimeout(n)) != 0) {return errorCode;}
knownProperties.isNonVolatile = n;
if ((errorCode = _readByteWithTimeout(n)) != 0) {return errorCode;}
knownProperties.isSlow = n;
if ((errorCode = _readByteWithTimeout(n)) != 0) {return errorCode;}
properties.isOperational = n;
if ((errorCode = _readUint32WithTimeout(properties.size)) != 0) {
return errorCode;
}
if ((errorCode = _readByteWithTimeout(n)) != 0) {return errorCode;}
properties.isNonVolatile = n;
if ((errorCode = _readByteWithTimeout(n)) != 0) {return errorCode;}
properties.isSlow = n;
return 0;
}
void SerialInterface::_sendMemoryChipProperties(
MemoryChipKnownProperties &knownProperties,
MemoryChipProperties &properties
)
{
_serial->write(knownProperties.isOperational);
_serial->write(knownProperties.size);
_serial->write(knownProperties.isNonVolatile);
_serial->write(knownProperties.isSlow);
_serial->write(properties.isOperational);
_writeUint32(properties.size);
_serial->write(properties.isNonVolatile);
_serial->write(properties.isSlow);
}
int SerialInterface::_readAddressAndSize(uint16_t& address, uint32_t& size)
{
uint32_t address32Bits;
int errorCode;
if ((errorCode = _readUint32WithTimeout(address32Bits)) != 0) {return errorCode;}
if ((errorCode = _readUint32WithTimeout(size)) != 0) {return errorCode;}
MemoryChipKnownProperties knownProperties;
MemoryChipProperties properties;
_memoryChip->getProperties(&knownProperties, &properties);
if (address32Bits > 0xFFFF) {
size = 0;
address = 0;
} else {
address = static_cast<uint16_t>(address32Bits);
}
if (knownProperties.size) {
if (address > properties.size - 1) {
size = 0;
} else if (properties.size - address < size) {
size = properties.size - address;
}
}
return 0;
}
bool SerialInterface::_commandRead()
{
_turnMemoryOnTemporarily();
uint16_t address;
uint32_t size;
if (_readAddressAndSize(address, size) != 0) {return false;}
_writeUint32(size);
_currentOperationStart = address;
_currentAddress = address;
_currentOperationSize = size;
_currentBytesLeft = size;
_currentCrc32.reset();
_memoryChip->switchToReadMode();
_state = SerialState::READING;
return true;
}
bool SerialInterface::_stateReading()
{
if (_currentBytesLeft) {
uint8_t n = _memoryChip->readByte(_currentAddress);
_currentCrc32.update(n);
_serial->write(n);
_currentAddress++;
_currentBytesLeft--;
return true;
} else {
_returnMemoryPowerState();
_writeUint32(_currentCrc32.finalize());
_state = SerialState::WAITING_FOR_COMMAND;
return false;
}
}
bool SerialInterface::_commandWrite()
{
_turnMemoryOnTemporarily();
MemoryChipKnownProperties knownProperties;
MemoryChipProperties properties;
_memoryChip->getProperties(&knownProperties, &properties);
// Unused at the moment.
_serial->write(knownProperties.isSlow && properties.isSlow);
uint16_t address;
uint32_t size;
if (_readAddressAndSize(address, size) != 0) {return false;}
_writeUint32(size);
_currentOperationStart = address;
_currentAddress = address;
_currentOperationSize = size;
_currentBytesLeft = size;
_currentCrc32.reset();
_memoryChip->switchToWriteMode();
_state = SerialState::WRITING;
return true;
}
bool SerialInterface::_stateWriting()
{
if (_currentBytesLeft) {
uint8_t n;
if (_readByteWithTimeout(n) != 0) {
_state = SerialState::WAITING_FOR_COMMAND;
return false;
}
_memoryChip->writeByte(_currentAddress, n);
_currentAddress++;
_currentBytesLeft--;
return true;
} else {
_memoryChip->switchToReadMode();
uint32_t end = _currentOperationStart + _currentOperationSize;
bool all_bytes_seem_pulled = true;
for (uint32_t address = _currentOperationStart; address < end; address++) {
uint8_t n = _memoryChip->readByte(address);
_currentCrc32.update(n);
if (n != 0xFF && n != 0x00) {
all_bytes_seem_pulled = false;
}
}
_writeUint32(_currentCrc32.finalize());
// If all the bytes written were 0x00 or 0xFF, and the data lines have
// pull-downs or pull-ups (respectively) on them, it's impossible to
// tell whether the data was written successfully without performing
// an extra write like this.
uint8_t errorCode = 0;
if (all_bytes_seem_pulled) {
_memoryChip->switchToReadMode();
uint8_t prevByte = _memoryChip->readByte(_currentOperationStart);
_memoryChip->switchToWriteMode();
_memoryChip->writeByte(_currentOperationStart, 0xA5);
_memoryChip->switchToReadMode();
if (_memoryChip->readByte(_currentOperationStart) != 0xA5) {
errorCode = 1;
}
_memoryChip->switchToWriteMode();
_memoryChip->writeByte(_currentOperationStart, prevByte);
}
_serial->write(errorCode);
_returnMemoryPowerState();
_state = SerialState::WAITING_FOR_COMMAND;
return false;
}
}