⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc.c

📁 NiosII DMA_用户指令_自定义逻辑应用实例 nios_DMA_CI_UL
💻 C
📖 第 1 页 / 共 2 页
字号:
 *
 **********************************************************************/


unsigned short faster_crcCompute(unsigned long *data_block, unsigned int nBytes)
{

	// Intitialise CRC to 0xFFFF;
	// Write the value 0xFFFF to the base address of the peripheral
	IOWR(CRC_ACCELERATOR_BASE,0,0xFFFF);


	// #### COMPLETE CODE SEGMENT BELOW ####
	// set-up write address for DMA transfer (crc peripheral)
	// call alt_dma_txchan_ioctl function to set RX stream on and
	// set the write address to the CRC peripheral location + 0x4
	//
	alt_dma_txchan_ioct(/*Insert code*/);


	// #### COMPLETE CODE SEGMENT BELOW ####
	// set mode of DMA to move 32 bits at a time
	// call alt_dma_txchan_ioctl and set DMA mode to 32 bits
	//
	alt_dma_txchan_ioct(/*Insert code*/);


	// #### COMPLETE CODE SEGMENT BELOW ####
	// Queue the DMA transfer
	// call the alt_dms_txchan_send function with the following parameters
	//   channel descriptor (set as a global variable at the top of this file)
	//   The start address of the data (passed to this function as data_block)
	//   The number of bytes to be transferred (passed to this function as nBytes)
	//   The name of the call back function (defined above as dma_done)
	//   NULL
	//
	alt_dma_txchan_send (/*Insert code*/);


	//wait for DMA transaction to complete
	while(!dma_complete);

	dma_complete = 0;  // reset flag

	return (IORD(CRC_ACCELERATOR_BASE,0));

}   /* faster_crcCompute() */



/**********************************************************************
 *
 * Function:    sevenseg_set_hex()
 *
 * Description: Decode hex number to format for 7-seg display.
 *              Also: sends data to the 7-seg display PIO
 *
 * Notes:
 *
 * Returns:     No return value
 *
 **********************************************************************/

static void sevenseg_set_hex(int hex)
{
	static alt_u8 segments[16] = {
    0x81, 0xCF, 0x92, 0x86, 0xCC, 0xA4, 0xA0, 0x8F, 0x80, 0x84, /* 0-9 */
    0x88, 0xE0, 0xF2, 0xC2, 0xB0, 0xB8 };                       /* a-f */

	unsigned int data = segments[hex & 15] | (segments[(hex >> 4) & 15] << 8);

	IOWR_ALTERA_AVALON_PIO_DATA(SEVEN_SEG_PIO_BASE, data);

}  /* sevenseg_set_hex */



/**********************************************************************
 *
 * Function:    crc_demo()
 *
 * Description: CRC demo that calls calculation and checks performance
 *
 * Notes:
 *
 * Returns:     No return value
 *
 **********************************************************************/

void crc_demo (void)
{
	unsigned short int crc_result;
	unsigned int data_block_length;
	unsigned int hex_digits;
	unsigned int time1, time2, num_ticks;

	data_block_length = DATA_BLOCK_END - (DATA_BLOCK_BEGIN) + 1;

	time1 = alt_timestamp_start();
	printf ("---------------------------------------------\n");
	printf ("Running CRC with table based software routine\n");

	for (hex_digits = 0; hex_digits <= CRC_RUNS; hex_digits++)
	{
		crc_result = crcCompute((char*)DATA_SRAM_COPY, DATA_BLOCK_END - (DATA_BLOCK_BEGIN) + 1);
   	sevenseg_set_hex(hex_digits);
	}

	time2 = alt_timestamp();
	num_ticks = time2 - time1 - timer_overhead;
	printf ("CRC for Data Block = 0x%x.\n", crc_result);
	printf ("CPU time(ms): %.*f\n", 2, ((float)num_ticks/(float)alt_timestamp_freq()) * (float)1000);

}  /* crc_demo */



/**********************************************************************
 *
 * Function:    fast_crc_demo()
 *
 * Description: CRC demo that calls calculation and checks performance
 *
 * Notes:
 *
 * Returns:     No return value
 *
 **********************************************************************/

void fast_crc_demo (void)
{
	unsigned short int crc_result;
	unsigned int data_block_length;
	unsigned int hex_digits;
	unsigned int time1, time2, num_ticks;

	data_block_length = (long*)DATA_BLOCK_END - (long*)DATA_BLOCK_BEGIN + 1;

	time1 = alt_timestamp_start();
	printf ("---------------------------------------------\n");
	printf ("Running CRC with custom instruction routine\n");

	for (hex_digits = 0; hex_digits <= CRC_RUNS; hex_digits++)
	{
		crc_result = fast_crcCompute((long*)DATA_SRAM_COPY, data_block_length);
		sevenseg_set_hex(hex_digits);
	}

	time2 = alt_timestamp();
	num_ticks = time2 - time1 - timer_overhead;
	printf ("CRC for Data Block = 0x%x.\n", crc_result);
	printf("CPU time(ms): %.*f\n", 2, ((float)num_ticks/(float)alt_timestamp_freq()) * (float)1000);

}  /* fast_crc_demo */



/**********************************************************************
 *
 * Function:    faster_crc_demo()
 *
 * Description: CRC demo that calls calculation and checks performance
 *
 * Notes:
 *
 * Returns:     No return value
 *
 **********************************************************************/

void faster_crc_demo (void)
{
	unsigned short int crc_result;
	unsigned int data_block_length;
	unsigned int hex_digits;
 	unsigned int time1, time2, num_ticks;

	data_block_length = (char*)DATA_BLOCK_END - (char*)DATA_BLOCK_BEGIN + 1;
	time1 = alt_timestamp_start();
	printf ("---------------------------------------------\n");
	printf ("Running CRC with DMA based routine\n");


	for (hex_digits = 0; hex_digits <= CRC_RUNS; hex_digits++)
	{
		crc_result = faster_crcCompute((long*)DATA_SRAM_COPY, data_block_length);
		sevenseg_set_hex(hex_digits);
	}

	time2 = alt_timestamp();
	num_ticks = time2 - time1 - timer_overhead;
	printf ("CRC for Data Block = 0x%x.\n", crc_result);
	printf("CPU time(ms): %.*f\n", 2, ((float)num_ticks/(float)alt_timestamp_freq()) * (float)1000);

}  /* faster_crc_demo */



/**********************************************************************
 *
 * Function:    main()
 *
 * Description: computes CRC for data block after copying from flash to SRAM
 *
 * Notes:       This function expects that crcInit() has been called
 *              first to initialize the CRC lookup table.
 *              This Fuction has been modified from the original file
 *              for the purposes of a Nios custom instruction lab/demo
 *
 * Returns:     n/a
 *
 **********************************************************************/

int main(void)
{
	unsigned int buttons;
	unsigned int* read_data;
	unsigned int* write_data;
	unsigned int time1, time2, timer_overhead;

#ifdef TEST
	// Copy data known data into sram
	write_data = (int*)DATA_SRAM_COPY;
	for (read_data = (int*)DATA_BLOCK_BEGIN; (int*)read_data <= (int*)DATA_BLOCK_END; read_data++)
	{
   	*write_data = 0x01234567;
   	write_data++;
	}
#else
	// Copy data block to SRAM so that Flash access time does not effect results
	write_data = (int*)DATA_SRAM_COPY;
	for (read_data = (int*)DATA_BLOCK_BEGIN; (int*)read_data <= (int*)DATA_BLOCK_END; read_data++)
	{
   	IOWR_32DIRECT(write_data, 0, *read_data);
   	write_data++;
	}
#endif

// simulation is only intended for the accelerated routines so dont bother
// initialising the crc table for the software routine during simulation
#ifndef SIM
	crcInit();
#endif

	// Start Timer:
	if(alt_timestamp_start() < 0)
	{
		printf("Timer init failed \n");
		exit(0);
	}

	// Determine time measurement overhead
	time1 = alt_timestamp_start();
	time2 = alt_timestamp();
	timer_overhead = time2 - time1;

	// Open DMA channel
	tx = alt_dma_txchan_open ("/dev/avalon_dma");

	while (1)
	{

#ifdef SIM
		printf("CRC\n");
#else
		printf ("\n=============================================\n");
		printf ("Nios II CRC Acceleration Demo\n");
		printf ("Press SW0 for software calculation\n");
		printf ("press SW1 for custom instruction calculation\n");
		printf ("press SW2 for DMA calculation\n");
#endif

		buttons = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE);
		while (buttons == NONE)
			buttons = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE);

		switch (buttons) {
			case SW0:
				crc_demo();
				break;
			case SW1:
				fast_crc_demo();
				break;
			case SW2:
				faster_crc_demo();
				break;
		}

		while (buttons != NONE)
			buttons = IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE);

		usleep (40000);  // debounce delay
  	}
}  /* main */

/**********************************************************************
 *
 * End of file.
 *
 **********************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -