Skip to content

Instantly share code, notes, and snippets.

@kuro68k
Created October 19, 2015 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuro68k/1179ad0640c44c341afa to your computer and use it in GitHub Desktop.
Save kuro68k/1179ad0640c44c341afa to your computer and use it in GitHub Desktop.
XMEGA NVM flash memory CRC compatible implementation in C
/**************************************************************************************************
* XMEGA NVM compatible CRC32
*/
#define XMEGA_CRC32_POLY 0x0080001B // Polynomial for use with Xmega devices
uint32_t xmega_nvm_crc32(uint8_t *buffer, uint32_t buffer_length)
{
uint32_t address;
uint32_t data_reg, help_a, help_b;
uint32_t crc_reg = 0;
for (address = 0; address < buffer_length; address += 2)
{
help_a = crc_reg << 1;
help_a &= 0x00FFFFFE;
help_b = crc_reg & (1 << 23);
if (help_b > 0)
help_b = 0x00FFFFFF;
data_reg = (uint32_t)((buffer[address + 1] << 8) | buffer[address] & 0xFFFF);
crc_reg = (help_a ^ data_reg) ^ (help_b & XMEGA_CRC32_POLY);
crc_reg = crc_reg & 0x00FFFFFF;
}
return crc_reg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment