CRC calculation doesn't match actual value

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to understand the CRC engine in xmega (XMEGA128A3U).

Here my test code:

 

static inline uint32_t crc_bit_revert(uint32_t value) {
	uint32_t result;
	uint8_t i;
	
	for (result = i = 0; i < 32; i++) {
		result = (result << 1) + (value & 1);
		value >>= 1;
	}
	return result;
}

static inline uint32_t crc32_checksum_read(void) {
	uint32_t checksum;

	checksum = ((uint32_t)CRC_CHECKSUM0 & 0x000000FF);
	checksum |= (((uint32_t)CRC_CHECKSUM1 << 8) & 0x0000FF00);
	checksum |= (((uint32_t)CRC_CHECKSUM2 << 16) & 0x00FF0000);
	checksum |= (((uint32_t)CRC_CHECKSUM3 << 24) & 0xFF000000);
	
	checksum = crc_bit_revert(checksum);
	checksum = ~checksum;
	return checksum;
}

static inline bool crc_is_busy(void) {
	return (CRC.STATUS & CRC_BUSY_bm);
}

static inline uint32_t crc_checksum_read(void) {
	CRC.STATUS |= CRC_BUSY_bm;
	while (crc_is_busy());
	return crc32_checksum_read();
}

void crc32(uint32_t size) {
	uint32_t crc = 0;
	uint8_t c;

    // set initial value to 0xffffffff
	CRC.CTRL = CRC_RESET_RESET1_gc;
	nop();
	CRC.CTRL |= CRC_CRC32_bm | CRC_SOURCE_IO_gc;

	while (size) {
		WDT_Reset();
		
		// read byte from SPI
		c = SPI_read();
		// dump it to serial port
		USART_PutChar(&USARTC0, c);
			while(!USART_IsTXDataRegisterEmpty(&USARTC0));
		}
		CRC.DATAIN = c;
		size--;
	}	

	crc = crc_checksum_read();
	// printf crc
}

 

As you can see I dump to the serial port my test file which I capture from terminal.

Then I put the captured file in this site: http://www.sunshine2k.de/coding/... selecting the CRC32 option to calculate the CRC.

 

My code is wrong because the values don't match.

 

Do you see any evidence of mistakes in the code above?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CRCs have a bunch of possible seeds (I think that is the term). You can use a the same algorithm with a different seed and it will come out differently. There have been a number of discussions about this here, and involving XMEGA. Try: https://www.google.com/#q=site+a...

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the "seed" the initial value, isn't it?

 

In my code I used the "RESET1" that should init the checksum to 0xffffffff in the same way the site does.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I found this function works as expected:

 

http://www.avrfreaks.net/comment...

 

but I don't understand how to use the internal hardware CRC engine to achieve the same result!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller