forked from df8oe/RS41HUP
-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.c
54 lines (50 loc) · 1.1 KB
/
util.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
#include "util.h"
uint16_t string_CRC16_checksum(char *string) {
uint16_t crc = 0xffff;
char i;
while (*(string) != 0) {
crc = crc ^ (*(string++) << 8);
for (i = 0; i < 8; i++) {
if (crc & 0x8000)
crc = (uint16_t) ((crc << 1) ^ 0x1021);
else
crc <<= 1;
}
}
return crc;
}
uint16_t array_CRC16_checksum(char *string, int len) {
uint16_t crc = 0xffff;
char i;
int ptr = 0;
while (ptr < len) {
ptr++;
crc = crc ^ (*(string++) << 8);
for (i = 0; i < 8; i++) {
if (crc & 0x8000)
crc = (uint16_t) ((crc << 1) ^ 0x1021);
else
crc <<= 1;
}
}
return crc;
}
void print_hex(char *data, uint8_t length, char *tmp) // prints 8-bit data in hex
{
uint8_t first ;
int j=0;
for (uint8_t i=0; i<length; i++)
{
first = ((uint8_t)data[i] >> 4) | 48;
if (first > 57) tmp[j] = first + (uint8_t)39;
else tmp[j] = first ;
j++;
first = ((uint8_t)data[i] & 0x0F) | 48;
if (first > 57) tmp[j] = first + (uint8_t)39;
else tmp[j] = first;
j++;
}
tmp[length*2] = '\n';
tmp[length*2+1] = 0;
// Serial.println(tmp);
}