-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
44 lines (33 loc) · 908 Bytes
/
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
#include "util.h"
#include "string.h"
#define SREG *((volatile char*) 0x5F)
#define C_SREG 0
unsigned char calc_checksum(unsigned char *data, unsigned int dsize)
{
//Storing checksum result in unsigned byte
unsigned char checksum_result;
for(int i=0;i<dsize;i++)
{
checksum_result += data[i];
if(SREG & (1<<C_SREG))
{
//carry wraparound
checksum_result+=1;
}
}
return ~checksum_result;
}
void update_checksum(unsigned char *data, unsigned int dsize)
{
unsigned char checksum_val = calc_checksum(data, dsize);
unsigned char last_byte = 0b11111111 + (~checksum_val + 0b00000001);
data[dsize-1]=last_byte;
}
int is_checksum_valid(unsigned char* data, unsigned int dsize)
{
unsigned char checksum_val = calc_checksum(data, dsize);
if(checksum_val==0)
return 1;
else
return 0;
}