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