/**
 * \brief Compute 16-bit CRC for EEPROM address range using hardware CRC module.
 *
 * This function returns the 16-bit CRC of the specified EEPROM address range.
 *
 * \param origDataptr Address of EEPROM location to start CRC computation at.
 * \param numBytes Number of bytes of the data.
 * \param pchecksum Pointer to the checksum stored in EEPROM.
 *
 * \note No sanity checking of addresses is done.
 */
uint16_t CLASSB_CRC16_EEPROM_HW(eepromptr_t origDataptr, crcbytenum_t numBytes,
		eeprom_uint16ptr_t pchecksum)
{
	uint32_t checksum;
	eeprom_uint8ptr_t dataptr = origDataptr;

	crc_set_initial_value(CRC16_INITIAL_REMAINDER);

	/* Ensure that EEPROM is memory mapped. */
	CLASSB_EEMAP_BEGIN();
	dataptr += MAPPED_EEPROM_START;

	checksum = crc_io_checksum((void*)dataptr, numBytes, CRC_16BIT);

#if defined(__ICCAVR__)
	/* Disable memory mapping of EEPROM, if necessary. */
	CLASSB_EEMAP_END();
	/* Compare checksums and handle error if necessary. */
	if (checksum != *pchecksum) {
		CLASSB_ERROR_HANDLER_CRC();
	}
#elif defined(__GNUC__)
	/* Compare checksums and handle error if necessary. */
	if (checksum != (*(eeprom_uint16ptr_t)(MAPPED_EEPROM_START
			+ (uintptr_t)pchecksum))) {
		CLASSB_ERROR_HANDLER_CRC();
	}
	/* Disable memory mapping of EEPROM, if necessary. */
	CLASSB_EEMAP_END();
#endif
	/* Return 16 bits */
	return checksum & (uint32_t)0x0000FFFF;
}
Example #2
0
/**
 * \brief Compute 32-bit CRC for EEPROM address range using table lookup.
 *
 * This function returns the 32-bit CRC of the specified EEPROM address range.
 *
 * \param origDataptr Address of EEPROM location to start CRC computation at.
 * \param numBytes Number of bytes of the data.
 * \param pchecksum Pointer to the checksum stored in EEPROM.
 *
 * \note No sanity checking of addresses is done.
 */
uint32_t CLASSB_CRC32_EEPROM_SW(eepromptr_t origDataptr, crcbytenum_t numBytes,
		eeprom_uint32ptr_t pchecksum)
{
	eeprom_uint8ptr_t dataptr = origDataptr;
	uint32_t remainder = CRC32_INITIAL_REMAINDER;
	uint8_t dataTemp;

	/* Ensure that EEPROM is memory mapped. */
	CLASSB_EEMAP_BEGIN();
	dataptr += MAPPED_EEPROM_START;

	/* Compute CRC for the specified data. */
	for (; numBytes != 0; numBytes--) {
		dataTemp = *dataptr++;
#if defined(CRC_USE_32BIT_LOOKUP_TABLE)
		CLASSB_CRC_REFL_TABLE_32(dataTemp, remainder,
				CLASSB_CRC32Table);
#else
		CLASSB_CRC_REFL(dataTemp, remainder, CRC32_POLYNOMIAL, 32);
#endif
	}

#ifdef CRC32_FINAL_XOR_VALUE
	remainder ^= CRC32_FINAL_XOR_VALUE;
#endif

#if defined(__ICCAVR__)
	/* Disable memory mapping of EEPROM, if necessary. */
	CLASSB_EEMAP_END();
	/* Compare checksums and handle error if necessary. */
	if (remainder != *pchecksum) {
		CLASSB_ERROR_HANDLER_CRC();
	}
#elif defined(__GCC__)
	/* Compare checksums and handle error if necessary. */
	if (remainder != (*(eeprom_uint32ptr_t)(MAPPED_EEPROM_START
			+ (uintptr_t) pchecksum))) {
		CLASSB_ERROR_HANDLER_CRC();
	}
	/* Disable memory mapping of EEPROM, if necessary. */
	CLASSB_EEMAP_END();
#endif

	return remainder;
}
/**
 * \brief Compute 16-bit CRC for Flash address range using hardware CRC module.
 *
 * This function returns the 16-bit CRC of the specified Flash address range.
 *
 * \param origDataptr Address of Flash location to start CRC computation at.
 * \param numBytes Number of bytes of the data.
 * \param pchecksum Pointer to the checksum stored in EEPROM.
 *
 * \note 16-bit flash CRC is much slower than 32-bit for flash checking.
 * \note No sanity checking of addresses is done.
 */
uint16_t CLASSB_CRC16_Flash_HW(flashptr_t origDataptr, crcbytenum_t numBytes,
		eeprom_uint16ptr_t pchecksum)
{
	uint32_t checksum;
	flash_uint8ptr_t dataptr = origDataptr;
	uint8_t dataTemp;

	crc_set_initial_value(CRC16_INITIAL_REMAINDER);

	crc_io_checksum_byte_start(CRC_16BIT);

	/* Compute CRC for the specified data. */
	for (; numBytes != 0; numBytes--) {
#if (PROGMEM_SIZE >= 0x10000UL)
		dataTemp = PROGMEM_READ_BYTE(dataptr++);
#else
		dataTemp = PROGMEM_READ_BYTE(dataptr++);
#endif
		crc_io_checksum_byte_add(dataTemp);
	}

	checksum = crc_io_checksum_byte_stop();

#if defined(__ICCAVR__)
	/* Compare checksums and handle error if necessary. */
	if (checksum != *pchecksum) {
		CLASSB_ERROR_HANDLER_CRC();
	}
#elif defined(__GCC__)
	/* Ensure that EEPROM is memory mapped. */
	CLASSB_EEMAP_BEGIN();
	/* Compare checksums and handle error if necessary. */
	if (checksum != (*(eeprom_uint16ptr_t)(MAPPED_EEPROM_START
			+ (uintptr_t)pchecksum))) {
		CLASSB_ERROR_HANDLER_CRC();
	}
	/* Disable memory mapping of EEPROM, if necessary. */
	CLASSB_EEMAP_END();
#endif
	/* Return 16 bits */
	return checksum & (uint32_t)0x0000FFFF;
}
Example #4
0
/**
 * \brief Compute 16-bit CRC for Flash address range using table lookup.
 *
 * This function returns the 16-bit CRC of the specified Flash address range.
 *
 * \param origDataptr Address of Flash location to start CRC computation at.
 * \param numBytes Number of bytes of the data.
 * \param pchecksum Pointer to the checksum stored in EEPROM.
 *
 * \note No sanity checking of addresses is done.
 */
uint16_t CLASSB_CRC16_Flash_SW(flashptr_t origDataptr, crcbytenum_t numBytes,
		eeprom_uint16ptr_t pchecksum)
{
	flash_uint8ptr_t dataptr = origDataptr;
	uint16_t remainder = CRC16_INITIAL_REMAINDER;
	uint8_t dataTemp;

	/* Compute CRC for the specified data. */
	for (; numBytes != 0; numBytes--) {

#if (PROGMEM_SIZE >= 0x10000UL)
		dataTemp = PROGMEM_READ_BYTE(dataptr++);
#else
		dataTemp = PROGMEM_READ_BYTE(dataptr++);
#endif

#if defined(CRC_USE_16BIT_LOOKUP_TABLE)
		CLASSB_CRC_TABLE_16(dataTemp, remainder, CLASSB_CRC16Table);
#else
		CLASSB_CRC(dataTemp, remainder, CRC16_POLYNOMIAL, 16);
#endif
	}

#if defined(__ICCAVR__)
	/* Compare checksums and handle error if necessary. */
	if (remainder != *pchecksum) {
		CLASSB_ERROR_HANDLER_CRC();
	}
#elif defined(__GCC__)
	/* Ensure that EEPROM is memory mapped. */
	CLASSB_EEMAP_BEGIN();
	/* Compare checksums and handle error if necessary. */
	if (remainder != (*(eeprom_uint16ptr_t)(MAPPED_EEPROM_START
			+ (uintptr_t) pchecksum))) {
		CLASSB_ERROR_HANDLER_CRC();
	}
	/* Disable memory mapping of EEPROM, if necessary. */
	CLASSB_EEMAP_END();
#endif

	return (remainder);
}