|
1 | 1 | /***
|
2 | 2 | Written by Christopher Andrews.
|
3 | 3 | CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ).
|
4 |
| - |
| 4 | +
|
5 | 5 | A CRC is a simple way of checking whether data has changed or become corrupted.
|
6 | 6 | This example calculates a CRC value directly on the EEPROM values.
|
7 | 7 | The purpose of this example is to highlight how the EEPROM object can be used just like an array.
|
|
10 | 10 | #include <Arduino.h>
|
11 | 11 | #include <EEPROM.h>
|
12 | 12 |
|
13 |
| -void setup(){ |
14 |
| - |
| 13 | +void setup() { |
| 14 | + |
15 | 15 | //Start serial
|
16 | 16 | Serial.begin(9600);
|
17 | 17 | while (!Serial) {
|
18 | 18 | ; // wait for serial port to connect. Needed for Leonardo only
|
19 | 19 | }
|
20 | 20 |
|
21 | 21 | //Print length of data to run CRC on.
|
22 |
| - Serial.print( "EEPROM length: " ); |
23 |
| - Serial.println( EEPROM.length() ); |
24 |
| - |
| 22 | + Serial.print("EEPROM length: "); |
| 23 | + Serial.println(EEPROM.length()); |
| 24 | + |
25 | 25 | //Print the result of calling eeprom_crc()
|
26 |
| - Serial.print( "CRC32 of EEPROM data: 0x" ); |
27 |
| - Serial.println( eeprom_crc(), HEX ); |
28 |
| - Serial.print( "\n\nDone!" ); |
| 26 | + Serial.print("CRC32 of EEPROM data: 0x"); |
| 27 | + Serial.println(eeprom_crc(), HEX); |
| 28 | + Serial.print("\n\nDone!"); |
29 | 29 | }
|
30 | 30 |
|
31 |
| -void loop(){ /* Empty loop */ } |
| 31 | +void loop() { |
| 32 | + /* Empty loop */ |
| 33 | +} |
| 34 | + |
| 35 | +unsigned long eeprom_crc(void) { |
32 | 36 |
|
33 |
| -unsigned long eeprom_crc( void ){ |
34 |
| - |
35 | 37 | const unsigned long crc_table[16] = {
|
36 |
| - 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, |
37 |
| - 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
38 |
| - 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, |
39 |
| - 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c |
40 |
| - }; |
41 |
| - |
| 38 | + 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, |
| 39 | + 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
| 40 | + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, |
| 41 | + 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c |
| 42 | + }; |
| 43 | + |
42 | 44 | unsigned long crc = ~0L;
|
43 |
| - |
44 |
| - for( int index = 0 ; index < EEPROM.length() ; ++index ){ |
45 |
| - crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4); |
46 |
| - crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4); |
| 45 | + |
| 46 | + for (int index = 0 ; index < EEPROM.length() ; ++index) { |
| 47 | + crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4); |
| 48 | + crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4); |
47 | 49 | crc = ~crc;
|
48 | 50 | }
|
49 | 51 | return crc;
|
|
0 commit comments