FPGA test

原创 2012年03月29日 15:38:37

/*************************************************************************
 * Filename: fpga_test.c
 * Author:  Kung
 * Description: Test FPGA : 1.Test unchanged data;
 							2.Test randam data;
 							3.Test I/O;
 * Version: Formal
 * Date: 2012-03-29
 * History: none
 *************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <assert.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <arpa/inet.h>
#include <sys/socket.h>


#define		FPGA_VERSION_ADDR	0x7003
#define		INPUT_PORT_STATUE	0x7ffe
#define		OUTPUT_TEST_ADDR	0x0003	

#define		RANDAM_DATA_IN		0x0000    //test randam number
#define		RANDAM_DATA_OUT		0x7fff
#define		TEST_RANGE			0xffff

#define 	IO_DATA_IN 			0x0003
#define 	IO_DATA_OUT 		0x7ffe

#define  UNCHANGED_DATA_ADDR	0x7018		//store 4 unchanged-datas to four sequntial address. this is the first 													unchanged-data address at where you can read 0x1234. 
#define     UNCHANGED_DATA1		0x1234
#define		UNCHANGED_DATA2		0x5678
#define		UNCHANGED_DATA3		0x90ab
#define		UNCHANGED_DATA4		0xcdef

#define FPGA_INTERCON_ADDR 0X0001
#define FPGA_CONFIG_ADDR 0X0002

#define FPGA_INTERCON 0X0001
#define FPGA_CONFIG 0x007c

#define FPGA_CONFIG_ADDR_READ         0X7002
#define FPGA_INTERCONT_ADDR_READ     0X7004

void write_fpga(int fd,unsigned short address,unsigned short data)
{
	write(fd, &address, 1);	//write write-address
	write(fd, &data, 1);    //write data
}

void read_fpga(int fd,unsigned short address,unsigned short *data)
{
	write(fd, &address, 1);	//write read-address
	read(fd, data, 1);		//read data
}
 
int main(void)
{   
	int fpga_fd;
	unsigned int count = 0;
	unsigned int i,j;
	unsigned int test_unchagned_data_success_times = 0;
	unsigned int test_random_data_success_times = 0;
	unsigned int test_io_data_success_times = 0;
	unsigned int unchanged_data[4] = {UNCHANGED_DATA1, UNCHANGED_DATA2, UNCHANGED_DATA3, UNCHANGED_DATA4};
	unsigned short write_addr,read_addr;
	unsigned short  write_data, read_data = 0;
	char scan_buf[20];

    if ((fpga_fd = open("/dev/fpga0", O_RDWR)) < 0)    //open fpga
    {
        perror("Open fpga\n");
        exit(1);
    }
    
    usleep(100);

#if 1	//read fpga version
	read_addr = FPGA_VERSION_ADDR;
	read_fpga(fpga_fd, read_addr,&read_data);
	printf("FPGA version : %x\n", read_data);
#endif

#if 1	//test unchanged data
	read_data = 0;	
	printf("Read unchanged data");	
	for (i = 0; i < 4; ++i)
	{
		read_addr = UNCHANGED_DATA_ADDR + i;
		read_fpga(fpga_fd, read_addr,&read_data);
		if (read_data == unchanged_data[i])
		{
			test_unchagned_data_success_times++;
			printf("0x%x ", read_data);
		}
	} 
	printf("\n");
#endif

#if 1	//test random data
	write_addr = RANDAM_DATA_IN;
	read_addr = RANDAM_DATA_OUT;

	for (i = 0; i < 16; i++)
	{
		write_data = i<<i;
		
		write_fpga(fpga_fd, write_addr, write_data);				
		read_fpga(fpga_fd, read_addr,&read_data);

		if (write_data == read_data)
			test_random_data_success_times++;
//		else printf("FPGA & ARM data wire have problem at :  %2d\n",i);
	}
	
	for (i = 0; i < 16; i++)
	{
		write_data = ~(i<<i);
		
		write_fpga(fpga_fd, write_addr, write_data);				
		read_fpga(fpga_fd, read_addr,&read_data);

		if (write_data == read_data)
			test_random_data_success_times++;
//		else printf("FPGA & ARM data wire have problem at :  %2d\n",i);
	}
#endif

	//test IO data, configure the fpga before test IO
	for(i = 0; i < 3; i++) //set interrupt control information
	{
		write_addr = FPGA_INTERCON_ADDR;
		read_addr  = FPGA_INTERCONT_ADDR_READ;
		write_data = FPGA_INTERCON;
		
		write_fpga(fpga_fd,write_addr,write_data);
		read_fpga(fpga_fd,read_addr,&read_data);
//		printf("Read interrupt control configure : %d\n",read_data);
		if(read_data == FPGA_INTERCON)
			break;
		sleep(1);
	}
	if(3 == i)
		printf("FPGA EEROR : set interrupt control failed!\n");
	
	for(j = 0; j < 3; j++) //set FPGA configuration
	{
		write_addr = FPGA_CONFIG_ADDR;
		read_addr  = FPGA_CONFIG_ADDR_READ;
		write_data = FPGA_CONFIG;
		
		write_fpga(fpga_fd,write_addr,write_data);		
		read_fpga(fpga_fd,read_addr,&read_data);
//		printf("Read fpga system configre : %d\n",read_data);
		if(read_data == FPGA_CONFIG)
			break;
	}
	if(3 == j)
		printf("FPGA EEROR : set system control failed!\n");
	
	if (3 == i || 3 == j)	
		goto RESULT;

#if 1
	/* begin test IO port */
	write_addr = IO_DATA_IN;
	read_addr  = IO_DATA_OUT;
	printf("Please connect fpga output port wire!(ADDR0-4-->AD0-4)\n");
	printf("Are you ready ?(Y) : ");
	scanf("%s",scan_buf);
	while(strcasecmp(scan_buf,"y"))
	{
		printf("EEROR :invalid input!\n");
		scanf("%s",scan_buf);
	}

	for(i = 0; i < 5; i++)
	{
		write_data = 1<<i;
		write_fpga(fpga_fd,write_addr,write_data);
		read_fpga(fpga_fd,read_addr,&read_data);

		if(write_data == read_data)
		{
			test_io_data_success_times++;
			//printf("read_data = %d\n",read_data);
		}
		else
		{		
			printf("FPGA & AD WIRE have problem at AD%d--ADDR%d\n ",i,i);
			printf("write data is %d,read data is %d\n",write_data,read_data);
		}
	}

	for(i = 0; i < 5; i++)
	{
		write_data = (0x1f)&(~(1<<i));
		write_fpga(fpga_fd, write_addr, write_data);	
		read_fpga(fpga_fd, read_addr, &read_data);
		
		if(write_data == read_data)
			test_io_data_success_times++;
		else
		{		
			printf("FPGA & AD WIRE have problem at AD%d--ADDR%d\n ",i,i);
			printf("write data is %x,read data is %x\n",write_data,read_data);
		}
	}		
#endif		

	printf("\nPlease connect fpga output port wire!(ADDR0-4-->AD5-9)\n");
	printf("Are you ready ?(Y) : ");
	scanf("%s",scan_buf);
	while(strcasecmp(scan_buf,"y"))
	{
		printf("EEROR :invalid input!\n");
		scanf("%s",scan_buf);
	}

	for(i = 0;i < 5; i++)
	{
		write_data = 1<<i;
		write_fpga(fpga_fd, write_addr, write_data);	
		read_fpga(fpga_fd, read_addr, &read_data);
		
		switch(i)
		{
			case 0:
				write_data = 0x20;
				break;
			case 1:
				write_data = 0x40;
				break;
			case 2:
				write_data = 0x80;
				break;
			case 3:
				write_data = 0x100;
				break;
			case 4:
				write_data = 0x600;
				break;
			default:
				break;
		}
			
		if(write_data != read_data)
		{
			printf("FPGA & AD WIRE have problem in AD%d--ADDR%d\n ",i+5,i+5);
			printf("write data is %x,read data is %x\n",write_data,read_data);
		}
		else test_io_data_success_times++;
	}
	
	for(i = 0; i < 5; i++)
	{
		write_data = ~(1<<i);
		write_fpga(fpga_fd,write_addr,write_data);	
		read_fpga(fpga_fd,read_addr,&read_data);
		
		switch(i)
		{
			case 0:
				write_data = 0x7c0;
				break;
			case 1:
				write_data = 0x7a0;
				break;
			case 2:
				write_data = 0x760;
				break;
			case 3:
				write_data = 0x6e0;
				break;
			case 4:
				write_data = 0x1e0;
				break;
			default:
				break;
		}
		
		if(write_data != read_data)
		{
			printf("FPGA & AD WIRE have problem expect AD%d--ADDR%d\n ",i+5,i+5);
			printf("write data is %x,read data is %x\n",write_data,read_data);
		}
		else test_io_data_success_times++;
	}
	
RESULT:
#if 1	//test result
	printf("\n\nTest result :\n");
	count = 0;	
	if (4 == test_unchagned_data_success_times)
	{
		printf("Test unchanged data success!\n");
		count++;
	}
	else
		printf("Test unchanged data failed!\n");

	if (32 == test_random_data_success_times)
	{
		printf("Test random data success!\n");
		count++;
	}
	else
		printf("Test random data failed!\n");
		
	if (20 == test_io_data_success_times)
	{
		printf("Test IO port success!\n");
		count++;
	}
	else
		printf("Test IO port failed!\n");

	if (3 == count)
	{
		printf("**********************FPGA is OK!!*************************\n");
	}
	else
	printf("********************BAD FPGA************************\n");

	
#endif

     close(fpga_fd);

     return 0;
}

 


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

test ARM+FPGA

  • 2013-07-17 23:40
  • 1.11MB
  • 下载

认识FPGA触发器的亚稳态

边沿型触发器的输出有两个稳定状态: 高电平或者低电平。为保证可靠操作, 必须要满足触发器的时序要求,也就是我们熟知的建立时间和保持时间。如果输入信号违反了触发器的时序要求, 那么触发器的输出信号就有可...

【奔跑的FPGA】part eight Qsys工具的使用方法简明手册

Altera公司在Quartus II 11.0 之后推出了Qsys集成开发工具。在该版本还没有取消SOPC builder,不过取消了之前版本的快捷方式,取而代之的是Qsys快捷方式,具体在菜单T...

基于Altera FPGA的千兆以太网实现方案

1 引言  在系统设备不断向小型化、集成化、网络化发展的今天,嵌入式开发成为新技术发展的最前沿,改变着系统的整体结构。FPGA由于其自身特点,成为嵌入式开发的最佳平台。Altera公司结合其最新一代...

基于FPGA和USB的高速数据采集及显示系统

为了完成自己的本科毕业设计,我设计完成了本系统:ADC采集数据源的数据,在FPGA的控制下,将数据通过USB总线技术传送给上位机,上位机将数据进行处理,以数据波形的形式显示出来。

FPGA那些事(黑金动力社区)-笔记

掌握verilog的秘诀是: 建模和时序 C语言和verilog语言的区别 综合语言常用的关键字举例 always@( )的多样性

FPGA 学习之路(九)SPI协议通信

SPI是同步串行通信接口设计

FIR滤波器的FPGA实现

1.FIR滤波器简介 FIR(Finite Impulse Response)滤波器:有限长单位冲激响应滤波器,又称为非递归型滤波器,是数字信号处理系统中最基本的元件,它可以在保证任意幅频特性的同时...

基于FPGA的简易数字时钟

基于FPGA的可显示数字时钟,设计思路为自底向上,包括三个子模块:时钟模块,进制转换模块,led显示模块。所用到的FPGA晶振频率为50Mhz,首先利用它得到1hz的时钟然后然后得到时钟模块,把时钟模...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)