]> git.zerfleddert.de Git - fnordlicht-mini/blob - firmware/tools/fixcrc.c
add eeprom writing (including checksum)
[fnordlicht-mini] / firmware / tools / fixcrc.c
1 #include <stdio.h>
2 #include <inttypes.h>
3
4 /* EEPROM data size, not including checksum */
5 #define EEP_DATA_SIZE 494
6 /* FIXME: actually we should either find a way to include the declaration of
7 struct storage_t here or at least make this a command line parameter */
8
9 /* code from http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html */
10 uint16_t crc16_update(uint16_t crc, uint8_t a)
11 {
12 int i;
13
14 crc ^= a;
15 for (i = 0; i < 8; ++i)
16 {
17 if (crc & 1)
18 crc = (crc >> 1) ^ 0xA001;
19 else
20 crc = (crc >> 1);
21 }
22
23 return crc;
24 }
25
26 int main(int argc, char ** argv)
27 {
28 int i, c;
29 uint16_t checksum = 0;
30 for (i = 0; i < EEP_DATA_SIZE; i++) {
31
32 if ((c = getchar()) == EOF) {
33 return 1;
34 }
35
36 checksum = crc16_update(checksum, c);
37
38 if (putchar(c) == EOF) {
39 return 2;
40 }
41 }
42 putchar(checksum >> 8);
43 putchar(checksum & 0xff);
44
45 return 0;
46 }
47
Impressum, Datenschutz