📄 crcdma.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "altera_avalon_dma.h"
#include "alt_types.h"
#include <time.h>
#include <unistd.h>
#include <sys/alt_timestamp.h>
#include "sys/alt_dma.h"
#include "altera_avalon_dma_regs.h"
#include "alt_types.h"
// Global Variable to count hex digits:
alt_u32 hex_digits;
alt_dma_txchan tx;
volatile int dma_complete = 0;
/**********************************************************************
*
* 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: The 7-seg-decoded values of a hex digit input.
*
**********************************************************************/
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);
}
void dma_done (void* handle){
dma_complete = 1;
}
/**********************************************************************
*
* Function: crcCompute()
*
* Description: Compute the CRC checksum of a binary message block.
*
* Notes: This function expects that crcInit() has been called
* first to initialize the CRC lookup table.
*
* Returns: The CRC of the data.
*
**********************************************************************/
volatile alt_u16 crcCompute(unsigned long *data_block, unsigned int nWords, int hex)
{
alt_u16 word;
IOWR(CRC_PERIPH_BASE,0,0x0000FFFF);
alt_dma_txchan_ioctl(tx,
ALT_DMA_RX_STREAM_ON,
(void*)(CRC_PERIPH_BASE + 0x4));
alt_avalon_dma_send (tx,
data_block,
nWords ,
//dma_done,
NULL,
NULL);
while (IORD_ALTERA_AVALON_DMA_STATUS (AVALON_DMA_BASE) &
ALTERA_AVALON_DMA_STATUS_BUSY_MSK);
word = (IORD(CRC_PERIPH_BASE,1));
return (word);
} /* crcCompute() */
int main(void)
{
#define data_block_begin 0x600000
#define data_block_end 0x63ffff
#define data_sram_copy 0x8a0000
alt_u16 crc_result;
unsigned int hex_digits;
unsigned int* read_data;
unsigned int* write_data;
alt_u32 data_block_length;
alt_u32 num_ticks = 0;
alt_u32 time1, time2, timer_overhead;
data_block_length = (char*)data_block_end - (char*)data_block_begin + 1;
tx = alt_dma_txchan_open ("/dev/avalon_dma");
// 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++)
{
*write_data = *read_data;
write_data++;
}
// Start Timer:
if(alt_timestamp_start() < 0)
{
printf("Timer init failed \n");
exit(0);
}
// Get the number of clocks it takes + record time stamp:
time1 = alt_timestamp();
time2 = alt_timestamp();
timer_overhead = time2 - time1;
time1 = alt_timestamp();
for (hex_digits = 0; hex_digits <= 0x80; hex_digits++)
{
crc_result = crcCompute((long*)data_sram_copy,data_block_length,hex_digits);
sevenseg_set_hex(hex_digits);
}
time2 = alt_timestamp();
num_ticks = time2 - time1 - timer_overhead;
printf ("\nCRC for Data Block = 0x%x.\n", crc_result);
printf("CPU time(ms): %.*f\n", 2, ((float)num_ticks/(float)alt_timestamp_freq()) * (float)1000);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -