-
Notifications
You must be signed in to change notification settings - Fork 87
/
command_parser.hpp
55 lines (47 loc) · 1.88 KB
/
command_parser.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
#pragma once
#include <mculib/small_function.hpp>
#include <stdint.h>
/*
-- command types:
-- bytes (hex):
-- 0 1 2 3 4 5
-- 00 : nop
-- 0d : return ascii '2' (0x32)
-- 10 AA : read register (address in AA)
-- 11 AA : read 2-byte register (address in AA)
-- 12 AA : read 4-byte register (address in AA)
-- 13 AA : read 8-byte register (address in AA)
-- 18 AA NN : read up to N values from FIFO (bytes per value is implementation defined)
-- 20 AA XX : write register (address in AA, value in XX)
-- 21 AA XX XX : write 2-byte register (address in AA, values in XX)
-- 22 AA XX XX XX XX : write 4-byte register (address in AA, values in XX)
-- 23 AA XX XX XX XX : write 8-byte register (address in AA, values in XX)
-- 28 AA NN : write N bytes into FIFO
*/
class CommandParser {
public:
// user provided handlers
// read fifo command handler
small_function<void(int address, int nValues)> handleReadFIFO;
// write fifo command handler.
// nBytes is the number of bytes in data.
// If nonzero, totalBytes is the total bytes in this transaction.
// If totalValues is 0, this is a continuation of an earlier transaction.
small_function<void(int address, int totalBytes, int nBytes, const uint8_t* data)> handleWriteFIFO;
// called when a register is written
small_function<void(int address)> handleWrite;
// send data to the stream
small_function<void(const uint8_t* s, int len)> send;
// user provided registers area
uint8_t* registers = nullptr;
int registersSizeMask = 0; // size of registers - 1
// process stream data
void handleInput(const uint8_t* s, int len);
// state variables
int cmdPhase = 0;
uint8_t cmdOpcode = 0;
uint8_t cmdAddress = 0xff;
uint8_t cmdEndAddress = 0xff;
uint8_t cmdStartAddress = 0;
int writeFIFOBytesLeft = 0;
};