Skip to content

Instantly share code, notes, and snippets.

@gnull
Created April 12, 2015 20:04
Show Gist options
  • Save gnull/31bf180f2dda6e5a4a41 to your computer and use it in GitHub Desktop.
Save gnull/31bf180f2dda6e5a4a41 to your computer and use it in GitHub Desktop.
Microcontrollers Course Homework
#define bytes_to_long(a,b,c,d) ( ((d) << 24) | ((c) << 16) | ((b) << 8) | (a) )
/**
* @brief Computes the 32-bit CRC of a given buffer of data word(32-bit).
* @param pBuffer: pointer to the buffer containing the data to be computed
* @param BufferLength: length of the buffer to be computed
* @retval 32-bit CRC
*/
uint32_t CRC_CalcBlockCRC(uint8_t pBuffer[], uint32_t BufferLength)
{
uint32_t index = 0;
uint8_t a, b, c, d;
for(index = 0; index + 3 < BufferLength; index += 4)
{
a = pBuffer[index];
b = pBuffer[index + 1];
c = pBuffer[index + 2];
d = pBuffer[index + 3];
CRC->DR = bytes_to_long(a , b, c, d);
}
a = index < BufferLength ? pBuffer[index++] : 0;
b = index < BufferLength ? pBuffer[index++] : 0;
c = index < BufferLength ? pBuffer[index++] : 0;
d = index < BufferLength ? pBuffer[index++] : 0;
CRC->DR = bytes_to_long(a, b, c, d);
return (CRC->DR);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment