示例#1
0
文件: main.c 项目: AndreyMostovov/asf
/**
 * \brief Generate the CRC value for the firmware
 */
static void generate_crc(void)
{
	uint32_t buffer_size = 0;

	/* Enable CRCCU peripheral clock */
	sysclk_enable_peripheral_clock(CRCCU);

	/* Reset the CRCCU */
	crccu_reset(CRCCU);

	/* Open the input file for CRC32 generation */
	f_open(&file_object1,
			(char const *)input_file_name,
			FA_OPEN_EXISTING | FA_READ);

	/* Generate the CRC32 for the input binary */
	while (true) {
		/* Read the data from the firmware */
		f_read(&file_object1, (uint8_t *)buffer, FLASH_BUFFER_SIZE,
				&buffer_size);

		/* Check if there is any buffer */
		if (!buffer_size) {
			break;
		}

		/* Set the memory address for CRCCU DMA transfer */
		crc_dscr.ul_tr_addr = (uint32_t) buffer;

		/* Transfer width: byte, interrupt disable */
		crc_dscr.ul_tr_ctrl = CRCCU_TR_CTRL_TRWIDTH_BYTE | buffer_size
								| CRCCU_TR_CTRL_IEN_DISABLE;

		/* Configure the CRCCU descriptor */
		crccu_configure_descriptor(CRCCU, (uint32_t) &crc_dscr);

		/* Configure CRCCU mode */
		crccu_configure_mode(CRCCU, CRCCU_MR_ENABLE | APP_CRC_POLYNOMIAL_TYPE);

		/* Start the CRC calculation */
		crccu_enable_dma(CRCCU);

		/* Wait for calculation ready */
		while ((crccu_get_dma_status(CRCCU) == CRCCU_DMA_SR_DMASR)) {
		}
	}

	/* Store the CRC32 Value */
	firmware_crc = crccu_read_crc_value(CRCCU);

	/* Enable CRCCU peripheral clock */
	sysclk_disable_peripheral_clock(CRCCU);

	/* Close the input file */
	f_close(&file_object1);
}
示例#2
0
/**
 * \brief Compute CRC of a buffer.
 *
 * \param p_buffer         The buffer holding the data.
 * \param ul_length          The buffer length.
 * \param ul_polynomial_type The polynomial type(CRCCU_MR_PTYPE_XXX).
 *
 * \return CRC of the buffer.
 */
static uint32_t compute_crc(uint8_t *p_buffer, uint32_t ul_length,
		uint32_t ul_polynomial_type)
{
	uint32_t ul_crc;
	uint32_t ul_timeout = 0;

	/* Reset the CRCCU */
	crccu_reset(CRCCU);

	memset((void *)&crc_dscr, 0, sizeof(crccu_dscr_type_t));

	crc_dscr.ul_tr_addr = (uint32_t) p_buffer;

	/* Transfer width: byte, interrupt enable */
	crc_dscr.ul_tr_ctrl =
			CRCCU_TR_CTRL_TRWIDTH_BYTE | ul_length |
			CRCCU_TR_CTRL_IEN_ENABLE;

	/* Configure the CRCCU descriptor */
	crccu_configure_descriptor(CRCCU, (uint32_t) &crc_dscr);

	/* Configure CRCCU mode */
	crccu_configure_mode(CRCCU, CRCCU_MR_ENABLE | ul_polynomial_type);

	/* Start the CRC calculation */
	crccu_enable_dma(CRCCU);

	/* Wait for calculation ready */
	while ((crccu_get_dma_status(CRCCU) == CRCCU_DMA_SR_DMASR) &&
			(ul_timeout++ < CRCCU_TIMEOUT)) {
	}

	/* Get CRC value */
	ul_crc = crccu_read_crc_value(CRCCU);

	/* Display CRC */
	if (ul_polynomial_type == CRCCU_MR_PTYPE_CCITT16) {
		/* 16-bits CRC */
		ul_crc &= 0xFFFF;
		printf("  CRC of the buffer is 0x%04x\n\r", ul_crc);
	} else {
		/* 32-bits CRC */
		printf("  CRC of the buffer is 0x%08x\n\r", ul_crc);
	}

	return ul_crc;
}