This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensirion_shdlc.c
380 lines (315 loc) · 12.6 KB
/
sensirion_shdlc.c
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright (c) 2018, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "sensirion_shdlc.h"
#include "sensirion_common.h"
#include "sensirion_config.h"
#include "sensirion_uart_hal.h"
#define SHDLC_START 0x7e
#define SHDLC_STOP 0x7e
#define SHDLC_MIN_TX_FRAME_SIZE 6
/** start/stop + (4 header + 255 data) * 2 because of byte stuffing */
#define SHDLC_FRAME_MAX_TX_FRAME_SIZE (2 + (4 + 255) * 2)
/** start/stop + (5 header + 255 data) * 2 because of byte stuffing */
#define SHDLC_FRAME_MAX_RX_FRAME_SIZE (2 + (5 + 255) * 2)
#define RX_DELAY_US 20000
static uint8_t sensirion_shdlc_checksum(uint8_t header_sum, uint8_t data_len,
const uint8_t* data) {
header_sum += data_len;
while (data_len--)
header_sum += *(data++);
return ~header_sum;
}
static uint16_t sensirion_shdlc_stuff_data(uint8_t data_len,
const uint8_t* data,
uint8_t* stuffed_data) {
uint16_t output_data_len = 0;
while (data_len--) {
uint8_t c = *(data++);
switch (c) {
case 0x11:
case 0x13:
case 0x7d:
case 0x7e:
/* byte stuffing is done by inserting 0x7d and inverting bit 5
*/
*(stuffed_data++) = 0x7d;
*(stuffed_data++) = c ^ (1 << 5);
output_data_len += 2;
break;
default:
*(stuffed_data++) = c;
output_data_len += 1;
}
}
return output_data_len;
}
static uint8_t sensirion_shdlc_check_unstuff(uint8_t data) {
return data == 0x7d;
}
static uint8_t sensirion_shdlc_unstuff_byte(uint8_t data) {
switch (data) {
case 0x31:
return 0x11;
case 0x33:
return 0x13;
case 0x5d:
return 0x7d;
case 0x5e:
return 0x7e;
default:
return data;
}
}
int16_t sensirion_shdlc_xcv(uint8_t addr, uint8_t cmd, uint8_t tx_data_len,
const uint8_t* tx_data, uint8_t max_rx_data_len,
struct sensirion_shdlc_rx_header* rx_header,
uint8_t* rx_data) {
int16_t ret;
ret = sensirion_shdlc_tx(addr, cmd, tx_data_len, tx_data);
if (ret != 0)
return ret;
sensirion_uart_hal_sleep_usec(RX_DELAY_US);
return sensirion_shdlc_rx(max_rx_data_len, rx_header, rx_data);
}
int16_t sensirion_shdlc_tx(uint8_t addr, uint8_t cmd, uint8_t data_len,
const uint8_t* data) {
uint16_t len = 0;
int16_t ret;
uint8_t crc;
uint8_t tx_frame_buf[SHDLC_FRAME_MAX_TX_FRAME_SIZE];
crc = sensirion_shdlc_checksum(addr + cmd, data_len, data);
tx_frame_buf[len++] = SHDLC_START;
len += sensirion_shdlc_stuff_data(1, &addr, tx_frame_buf + len);
len += sensirion_shdlc_stuff_data(1, &cmd, tx_frame_buf + len);
len += sensirion_shdlc_stuff_data(1, &data_len, tx_frame_buf + len);
len += sensirion_shdlc_stuff_data(data_len, data, tx_frame_buf + len);
len += sensirion_shdlc_stuff_data(1, &crc, tx_frame_buf + len);
tx_frame_buf[len++] = SHDLC_STOP;
ret = sensirion_uart_hal_tx(len, tx_frame_buf);
if (ret < 0)
return ret;
if (ret != len)
return SENSIRION_SHDLC_ERR_TX_INCOMPLETE;
return 0;
}
int16_t sensirion_shdlc_rx(uint8_t max_data_len,
struct sensirion_shdlc_rx_header* rxh,
uint8_t* data) {
int16_t len;
uint16_t i;
uint8_t rx_frame[SHDLC_FRAME_MAX_RX_FRAME_SIZE];
uint8_t* rx_header = (uint8_t*)rxh;
uint8_t j;
uint8_t crc;
uint8_t unstuff_next;
len = sensirion_uart_hal_rx(2 + (5 + (uint16_t)max_data_len) * 2, rx_frame);
if (len < 1 || rx_frame[0] != SHDLC_START)
return SENSIRION_SHDLC_ERR_MISSING_START;
for (unstuff_next = 0, i = 1, j = 0; j < sizeof(*rxh) && i < len - 2; ++i) {
if (unstuff_next) {
rx_header[j++] = sensirion_shdlc_unstuff_byte(rx_frame[i]);
unstuff_next = 0;
} else {
unstuff_next = sensirion_shdlc_check_unstuff(rx_frame[i]);
if (!unstuff_next)
rx_header[j++] = rx_frame[i];
}
}
if (j != sizeof(*rxh) || unstuff_next)
return SENSIRION_SHDLC_ERR_ENCODING_ERROR;
if (max_data_len < rxh->data_len)
return SENSIRION_SHDLC_ERR_FRAME_TOO_LONG; /* more data than expected */
for (unstuff_next = 0, j = 0; j < rxh->data_len && i < len - 2; ++i) {
if (unstuff_next) {
data[j++] = sensirion_shdlc_unstuff_byte(rx_frame[i]);
unstuff_next = 0;
} else {
unstuff_next = sensirion_shdlc_check_unstuff(rx_frame[i]);
if (!unstuff_next)
data[j++] = rx_frame[i];
}
}
if (unstuff_next)
return SENSIRION_SHDLC_ERR_ENCODING_ERROR;
if (j < rxh->data_len)
return SENSIRION_SHDLC_ERR_ENCODING_ERROR;
crc = rx_frame[i++];
if (sensirion_shdlc_check_unstuff(crc))
crc = sensirion_shdlc_unstuff_byte(rx_frame[i++]);
if (sensirion_shdlc_checksum(rxh->addr + rxh->cmd + rxh->state,
rxh->data_len, data) != crc)
return SENSIRION_SHDLC_ERR_CRC_MISMATCH;
if (i >= len || rx_frame[i] != SHDLC_STOP)
return SENSIRION_SHDLC_ERR_MISSING_STOP;
if (0x7F & rxh->state) {
return SENSIRION_SHDLC_ERR_EXECUTION_FAILURE;
}
return 0;
}
static void sensirion_shdlc_stuff_byte(struct sensirion_shdlc_buffer* tx_frame,
uint8_t data) {
switch (data) {
case 0x11:
case 0x13:
case 0x7d:
case 0x7e:
/* byte stuffing is done by inserting 0x7d and inverting bit 5 */
tx_frame->data[tx_frame->offset++] = 0x7d;
tx_frame->data[tx_frame->offset++] = data ^ (1 << 5);
return;
default:
tx_frame->data[tx_frame->offset++] = data;
return;
}
}
void sensirion_shdlc_add_uint8_t_to_frame(
struct sensirion_shdlc_buffer* tx_frame, uint8_t data) {
sensirion_shdlc_stuff_byte(tx_frame, data);
tx_frame->checksum += data;
}
void sensirion_shdlc_begin_frame(struct sensirion_shdlc_buffer* tx_frame,
uint8_t* buffer, uint8_t command,
uint8_t address, uint8_t data_length) {
tx_frame->data = buffer;
tx_frame->checksum = 0;
tx_frame->offset = 0;
tx_frame->data[tx_frame->offset++] = SHDLC_START;
sensirion_shdlc_add_uint8_t_to_frame(tx_frame, address);
sensirion_shdlc_add_uint8_t_to_frame(tx_frame, command);
sensirion_shdlc_add_uint8_t_to_frame(tx_frame, data_length);
}
void sensirion_shdlc_add_uint32_t_to_frame(
struct sensirion_shdlc_buffer* tx_frame, uint32_t data) {
sensirion_shdlc_add_uint8_t_to_frame(tx_frame,
(uint8_t)((data & 0xFF000000) >> 24));
sensirion_shdlc_add_uint8_t_to_frame(tx_frame,
(uint8_t)((data & 0x00FF0000) >> 16));
sensirion_shdlc_add_uint8_t_to_frame(tx_frame,
(uint8_t)((data & 0x0000FF00) >> 8));
sensirion_shdlc_add_uint8_t_to_frame(tx_frame,
(uint8_t)((data & 0x000000FF) >> 0));
}
void sensirion_shdlc_add_int32_t_to_frame(
struct sensirion_shdlc_buffer* tx_frame, int32_t data) {
sensirion_shdlc_add_uint32_t_to_frame(tx_frame, (uint32_t)data);
}
void sensirion_shdlc_add_uint16_t_to_frame(
struct sensirion_shdlc_buffer* tx_frame, uint16_t data) {
sensirion_shdlc_add_uint8_t_to_frame(tx_frame,
(uint8_t)((data & 0xFF00) >> 8));
sensirion_shdlc_add_uint8_t_to_frame(tx_frame,
(uint8_t)((data & 0x00FF) >> 0));
}
void sensirion_shdlc_add_int16_t_to_frame(
struct sensirion_shdlc_buffer* tx_frame, int16_t data) {
sensirion_shdlc_add_uint16_t_to_frame(tx_frame, (uint16_t)data);
}
void sensirion_shdlc_add_float_to_frame(struct sensirion_shdlc_buffer* tx_frame,
float data) {
union {
uint32_t uint32_data;
float float_data;
} convert;
convert.float_data = data;
sensirion_shdlc_add_uint32_t_to_frame(tx_frame, convert.uint32_data);
}
void sensirion_shdlc_add_bytes_to_frame(struct sensirion_shdlc_buffer* tx_frame,
uint8_t* data, uint16_t data_length) {
uint16_t i;
for (i = 0; i < data_length; i++) {
sensirion_shdlc_add_uint8_t_to_frame(tx_frame, data[i]);
}
}
void sensirion_shdlc_finish_frame(struct sensirion_shdlc_buffer* tx_frame) {
sensirion_shdlc_add_uint8_t_to_frame(tx_frame, ~(tx_frame->checksum));
tx_frame->data[tx_frame->offset++] = SHDLC_STOP;
}
int16_t sensirion_shdlc_tx_frame(struct sensirion_shdlc_buffer* tx_frame) {
int16_t tx_length;
tx_length = sensirion_uart_hal_tx(tx_frame->offset, tx_frame->data);
if (tx_length < 0) {
return tx_length;
}
if (tx_length != tx_frame->offset) {
return SENSIRION_SHDLC_ERR_TX_INCOMPLETE;
}
return NO_ERROR;
}
static uint8_t
sensirion_shdlc_unstuff_next_byte(struct sensirion_shdlc_buffer* rx_frame) {
uint8_t data = rx_frame->data[rx_frame->offset++];
if (data == 0x7d) {
data = rx_frame->data[rx_frame->offset++];
data = data ^ (1 << 5);
}
rx_frame->checksum += data;
return data;
}
int16_t sensirion_shdlc_rx_inplace(struct sensirion_shdlc_buffer* rx_frame,
uint8_t expected_data_length,
struct sensirion_shdlc_rx_header* header) {
int16_t rx_length;
uint16_t i;
rx_frame->offset = 0;
rx_frame->checksum = 0;
rx_length = sensirion_uart_hal_rx(
2 + (5 + (uint16_t)expected_data_length) * 2, rx_frame->data);
if (rx_length < 1 || rx_frame->data[rx_frame->offset++] != SHDLC_START) {
return SENSIRION_SHDLC_ERR_MISSING_START;
}
header->addr = sensirion_shdlc_unstuff_next_byte(rx_frame);
header->cmd = sensirion_shdlc_unstuff_next_byte(rx_frame);
header->state = sensirion_shdlc_unstuff_next_byte(rx_frame);
header->data_len = sensirion_shdlc_unstuff_next_byte(rx_frame);
if (expected_data_length < header->data_len) {
return SENSIRION_SHDLC_ERR_FRAME_TOO_LONG; /* more data than
expected */
}
for (i = 0; i < header->data_len && rx_frame->offset < rx_length - 2; i++) {
rx_frame->data[i] = sensirion_shdlc_unstuff_next_byte(rx_frame);
}
if (i < header->data_len) {
return SENSIRION_SHDLC_ERR_ENCODING_ERROR;
}
sensirion_shdlc_unstuff_next_byte(rx_frame);
/* (CHECKSUM + ~CHECKSUM) = 0xFF */
if (rx_frame->checksum != 0xFF) {
return SENSIRION_SHDLC_ERR_CRC_MISMATCH;
}
if (rx_frame->offset >= rx_length ||
rx_frame->data[rx_frame->offset] != SHDLC_STOP) {
return SENSIRION_SHDLC_ERR_MISSING_STOP;
}
if (0x7F & header->state) {
return SENSIRION_SHDLC_ERR_EXECUTION_FAILURE;
}
return NO_ERROR;
}