-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialinterface.hpp
65 lines (57 loc) · 1.5 KB
/
serialinterface.hpp
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
#ifndef SERIALINTERFACE_HPP
#define SERIALINTERFACE_HPP
#include <Arduino.h>
#include <CRC32.h>
#include "memorychip.hpp"
#define FRAMUNE_PROTOCOL_VERSION 0
class SerialInterface
{
public:
SerialInterface(Stream* serial, MemoryChip* memoryChip);
bool update();
private:
void _turnMemoryOnTemporarily();
void _returnMemoryPowerState();
int _readByteWithTimeout(uint8_t& n);
int _readUint32WithTimeout(uint32_t& n);
void _writeUint16(uint16_t n);
void _writeUint32(uint32_t n);
bool _checkForCommand();
bool _commandSetAndAnalyzeChip();
int _receiveMemoryChipProperties(
MemoryChipKnownProperties& knownProperties,
MemoryChipProperties& properties
);
void _sendMemoryChipProperties(
MemoryChipKnownProperties& knownProperties,
MemoryChipProperties& properties
);
int _readAddressAndSize(uint16_t& address, uint32_t& size);
bool _commandRead();
bool _stateReading();
bool _commandWrite();
bool _stateWriting();
enum class SerialState
{
WAITING_FOR_COMMAND,
READING,
WRITING
};
enum class SerialCommand : uint8_t
{
GET_VERSION,
SET_AND_ANALYZE_CHIP,
READ,
WRITE
};
Stream* _serial;
MemoryChip* _memoryChip;
SerialState _state;
bool _prevMemoryPowerState;
uint16_t _currentOperationStart;
uint32_t _currentOperationSize;
uint16_t _currentAddress;
uint32_t _currentBytesLeft;
CRC32 _currentCrc32;
};
#endif