-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspi_protocol.c
More file actions
286 lines (217 loc) · 7.95 KB
/
spi_protocol.c
File metadata and controls
286 lines (217 loc) · 7.95 KB
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
/*
* spi_protocol.c
*
* Created on: Mar 31, 2020
* Author: TheMarpe - Martin Peterlin
*
*/
#include <spi_protocol.h>
#include <spi_protocol.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <checksum.h>
#include <stdio.h>
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define STATE_RX_HEADER (0) // 1B start
#define STATE_RX_PAYLOAD (1) // PAYLOAD_SIZE bytes
#define STATE_RX_TAIL_CRC_0 (2) // LE first byte CRC
#define STATE_RX_TAIL_CRC_1 (3) // LE second byte CRC
#define STATE_RX_TAIL_END (4) // 1 byte end byte
static SpiProtocolPacket* get_parsed_packet(SpiProtocolInstance* instance){
assert(instance->currentPacketIndex == 0 || instance->currentPacketIndex == 1);
return instance->packet + ( 1 - instance->currentPacketIndex);
}
static SpiProtocolPacket* get_current_packet(SpiProtocolInstance* instance){
assert(instance->currentPacketIndex == 0 || instance->currentPacketIndex == 1);
return instance->packet + instance->currentPacketIndex;
}
static SpiProtocolPacket* switch_current_packet(SpiProtocolInstance* instance){
assert(instance->currentPacketIndex == 0 || instance->currentPacketIndex == 1);
instance->payloadOffset = 0;
instance->currentPacketIndex = 1 - instance->currentPacketIndex;
return instance->packet + instance->currentPacketIndex;
}
static int is_packet_ok(const SpiProtocolPacket* packet){
if(packet->start != START_BYTE_MAGIC){
return 0;
}
// Get CRC and compare with payload crc
uint16_t crc = (packet->crc[0] & 0xFF) | ((((uint16_t) packet->crc[1]) << 8) & 0xFF00);
// Calculated crc
uint16_t crc_calculated = crc_modbus(packet->data, SPI_PROTOCOL_PAYLOAD_SIZE);
// Compare
if(crc != crc_calculated){
return 0;
}
if(packet->end != END_BYTE_MAGIC){
return 0;
}
return 1;
}
/*
* instance - SpiProtocolInstance pointer;
*/
void spi_protocol_init(SpiProtocolInstance *instance){
instance->state = STATE_RX_HEADER;
instance->payloadOffset = 0;
instance->currentPacketIndex = 0;
init_crc16_tab();
}
/*
* instance - spi protocol instance pointer
* buffer - uint8_t pointer to buffer where packet bytes reside
* size - number of bytes to parse (max sizeof(SpiProtocolPacket))
*
* returns: SpiProtocolPacket pointer, NULL if packet wasn't parsed
*/
SpiProtocolPacket* spi_protocol_parse(SpiProtocolInstance* instance, const uint8_t* buffer, int size){
// max bytes to parse: SPI_PROTOCOL_PAYLOAD_SIZE
assert(size >= 0 && size <= (int) sizeof(SpiProtocolPacket));
// Get current packet
SpiProtocolPacket* packet = get_current_packet(instance);
// Packet counter
int packetCount = 0;
// Parse each byte individually (except payload)
for(int i = 0; i < size; i++){
uint8_t curByte = buffer[i];
switch(instance->state){
case STATE_RX_HEADER:
{
if(curByte == START_BYTE_MAGIC){
packet->start = curByte;
instance->state = STATE_RX_PAYLOAD;
}
}
break;
case STATE_RX_PAYLOAD:
{
int remainingBytesAvailable = size - i;
int remainingBytes = SPI_PROTOCOL_PAYLOAD_SIZE - instance->payloadOffset;
int numBytes = MIN(remainingBytes, remainingBytesAvailable);
memcpy(packet->data + instance->payloadOffset, buffer + i, numBytes);
// Increment payloadOffset
instance->payloadOffset += numBytes;
// Increment "i" to a position after numBytes
i += numBytes - 1; // one byte will be incremented by the for loop
// Assert: payload offset must be less or equal than actual payload size
assert(instance->payloadOffset <= SPI_PROTOCOL_PAYLOAD_SIZE);
// if payloadOffset is at the end, whole packet was read, change state
if(instance->payloadOffset == SPI_PROTOCOL_PAYLOAD_SIZE){
instance->state = STATE_RX_TAIL_CRC_0;
instance->payloadOffset = 0;
}
}
break;
case STATE_RX_TAIL_CRC_0:
{
packet->crc[0] = curByte;
instance->state = STATE_RX_TAIL_CRC_1;
}
break;
case STATE_RX_TAIL_CRC_1:
{
packet->crc[1] = curByte;
instance->state = STATE_RX_TAIL_END;
}
break;
case STATE_RX_TAIL_END:
{
packet->end = curByte;
// This is the end of the current packet, check if packet is okay
if(is_packet_ok(packet)){
// Increment packet count
packetCount++;
// Switch to other packet
packet = switch_current_packet(instance);
}
// Jump to beginning state
instance->state = STATE_RX_HEADER;
}
break;
default:
// if any other value is in state variable, instance wasn't initialized properly
assert(false);
}
}
if(packetCount > 0){
return get_parsed_packet(instance);
} else {
return NULL;
}
}
/*
* packet - pointer to SpiProtocolPacket where it will be written
* payload_buffer - Input buffer with payload data
* Returns: 0 OK, -1 packet is NULL, -2 payload_buffer is NULL
*/
int spi_protocol_write_packet(SpiProtocolPacket* packet, const uint8_t* payload_buffer, int size){
if(packet == NULL){
return SPI_PROTOCOL_PACKET_NULL;
}
if(payload_buffer == NULL){
return SPI_PROTOCOL_PAYLOAD_BUFFER_NULL;
}
// Start byte
packet->start = START_BYTE_MAGIC;
// Payload
memcpy(packet->data, payload_buffer, size);
// Zero out the rest of buffer
memset(packet->data + size, 0, SPI_PROTOCOL_PAYLOAD_SIZE - size);
uint16_t crc = crc_modbus(packet->data, SPI_PROTOCOL_PAYLOAD_SIZE);
// CRC - little endian
packet->crc[0] = crc & 0xFF;
packet->crc[1] = ((crc >> 8) & 0xFF);
// End byte
packet->end = END_BYTE_MAGIC;
return SPI_PROTOCOL_OK;
}
/*
* packet - pointer to SpiProtocolPacket where it will be written
* payload_buffer1 - Input buffer with payload data
* payload_buffer2 - Second input buffer with payload data
* Returns: 0 OK, -1 packet is NULL, -2 payload_buffer is NULL
*/
int spi_protocol_write_packet2(SpiProtocolPacket* packet, const uint8_t* payload_buffer1, const uint8_t* payload_buffer2, int size1, int size2){
if(packet == NULL){
return SPI_PROTOCOL_PACKET_NULL;
}
if(payload_buffer1 == NULL){
return SPI_PROTOCOL_PAYLOAD_BUFFER_NULL;
}
if(payload_buffer2 == NULL){
return SPI_PROTOCOL_PAYLOAD_BUFFER_NULL;
}
// Start byte
packet->start = START_BYTE_MAGIC;
// Payload
memcpy(packet->data, payload_buffer1, size1);
memcpy(packet->data + size1, payload_buffer2, size2);
// Zero out the rest of buffer
memset(packet->data + size1 + size2, 0, SPI_PROTOCOL_PAYLOAD_SIZE - (size1 + size2));
uint16_t crc = crc_modbus(packet->data, SPI_PROTOCOL_PAYLOAD_SIZE);
// CRC - little endian
packet->crc[0] = crc & 0xFF;
packet->crc[1] = ((crc >> 8) & 0xFF);
// End byte
packet->end = END_BYTE_MAGIC;
return SPI_PROTOCOL_OK;
}
/*
* packet - pointer to SpiProtocolPacket where header, crc and tail will be written
* Returns: 0 OK, -1 packet is NULL
*/
int spi_protocol_inplace_packet(SpiProtocolPacket* packet){
if(packet == NULL){
return SPI_PROTOCOL_PACKET_NULL;
}
packet->start = START_BYTE_MAGIC;
// Calculate CRC
uint16_t crc = crc_modbus(packet->data, SPI_PROTOCOL_PAYLOAD_SIZE);
// CRC - little endian
packet->crc[0] = crc & 0xFF;
packet->crc[1] = ((crc >> 8) & 0xFF);
// End byte
packet->end = END_BYTE_MAGIC;
return SPI_PROTOCOL_OK;
}