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

📄 fpga.c

📁 重要代码,FPGA CYCLONE FLASH DRIVER
💻 C
📖 第 1 页 / 共 5 页
字号:
    Conf_Stage_Address_Backup=(unsigned short*)(FLASH_BASE_ADD_bake+0x242);
    Conf_Controlstage_Address_Backup=(unsigned short*)(FLASH_BASE_ADD_bake+0xe82);
    Conf_Daydesign_Address_Backup=(unsigned short*)(FLASH_BASE_ADD_bake+0x13a2);
    Conf_Daydesigntable_Address_Backup=(unsigned short*)(FLASH_BASE_ADD_bake+0x1ce2);
    Conf_YaoKong_Address_Backup=(unsigned short*)(FLASH_BASE_ADD_bake+0x1d20);
    Conf_CurNumber_Address_Backup=(unsigned short*)(FLASH_BASE_ADD_bake+0x1d28);
    Conf_Solution_Address_Backup=(unsigned short*)(FLASH_BASE_ADD_bake+0x1d48);
    Flash_Mark_Address=0x3000;

    //初始化flash中的流量信息
	Flow_Information_Base=(unsigned short*)(FLASH_BASE_ADD+0x10000);
	Flow_Information_End=(unsigned short*)(FLASH_BASE_ADD+0x60000);

	for(i=0;i<16384;i++)
	{
		data1=(unsigned short*)(FLASH_BASE_ADD+0x80000+i*4);
		data2=(unsigned short*)(FLASH_BASE_ADD+0x90000+i*4);
		if(*data1==0xffff && *data2==0xffff)
		{
			now_flow_add=i;
			break;
		}
	}
	copy_flow_add();


	//初始化双口ram的六个寄存器
	for(i=0; i<6; i++)
	{
		MarkRx[i]=(unsigned char*)(BASE_ADD+0x50+5*i);
		p_Rx=MarkRx[i];
		*p_Rx=0x87;
		MarkTx[i]=(unsigned char*)(BASE_ADD+0x30+5*i);
		p_Tx=MarkTx[i];
//		*p_Tx=0x87;

        Rx_Head_High[i] =(unsigned char*)(BASE_ADD+0x51+5*i);
		Rx_Head_Low[i]  =(unsigned char*)(BASE_ADD+0x52+5*i);
		Rx_Tail_High[i] =(unsigned char*)(BASE_ADD+0x53+5*i);
		Rx_Tail_Low[i]  =(unsigned char*)(BASE_ADD+0x54+5*i);
		Tx_Head_High[i] =(unsigned char*)(BASE_ADD+0x31+5*i);
		Tx_Head_Low[i]  =(unsigned char*)(BASE_ADD+0x32+5*i);
		Tx_Tail_High[i] =(unsigned char*)(BASE_ADD+0x33+5*i);
		Tx_Tail_Low[i]  =(unsigned char*)(BASE_ADD+0x34+5*i);

	}

    //初始化0x82包
  delay(1000);
	Data_Number=7;
	CheckData[0]=0x00;
	CheckData[1]=0x00;
	CheckData[2]=0x00;
	CheckData[3]=0x00;
	CheckData[4]=0x82;
	CheckData[5]=0x00;
	CheckData[6]=0x00;	
	Finished_Receive=1;
//    Finished_Receive=Receive_From;
	Write_Comm_to_Ram();				

//	Parameter[0]=0x00;
//	Parameter[1]=0x43;
//	Parameter[2]=0;
//	Parameter[2]=0;
	nr_installuserisr(na_uart1_irq,ReceiveISRUart1,context1);
	nr_installuserisr(na_uart2_irq,ReceiveISRUart2,context2);
	na_uart1->np_uartcontrol=np_uartcontrol_irrdy_mask;
	na_uart2->np_uartcontrol=np_uartcontrol_irrdy_mask;

	while(1)
	{	
			
	count++;
	if(count>6000)count=0;

	if(count>3000){
	    leddd=(leddd&0x000b)|ledd[1];
	    na_led_pio->np_piodata=leddd;
                    }else{
	               leddd=(leddd&0x000b);
	               na_led_pio->np_piodata=leddd;
							}
    if(count>4000){
	    leddd=(leddd&0x0007)|ledd[0];
	    na_led_pio->np_piodata=leddd;
                    }else{
	                leddd=(leddd&0x0007);
	                na_led_pio->np_piodata=leddd;
							}

		//处理接收到的数据
		if(!ReceiveComm())
		  break;
		//处理将要发送的数据
		na_timer_dram->np_timercontrol = 0;
		Transmit();
		na_timer_dram->np_timercontrol = np_timercontrol_start_mask | np_timercontrol_cont_mask |np_timercontrol_ito_mask;

	}
}

/****************************************************************************
*
*发送包
*
*****************************************************************************/
void Transmit()
{  
		unsigned char* p_Tx;
		p_Tx=MarkTx[T_number];  //查询0X30系列发送寄存器
		switch(*p_Tx)
		{
			case 1 :            //通过串口1发送
			  Transmit_Number=T_number;
			  uart_use=na_uart1;
			  TransmitComm(uart_use);
			  
			 break;
			case 2 :            //通过串口2发送
			  Transmit_Number=T_number;
			  uart_use=na_uart2;
			  TransmitComm(uart_use);
			 break;
			case 3 :           //通过串口485发送
			  Transmit_Number=T_number;
			  Transmit485();
			 break;
			case 4 :           //通过以太网发送
			  Transmit_Number=T_number;
			  TransmitEnternet();
			 break;
			case 87 :
			 break;
			default :
			  //error
			 break;
		}
		if(++T_number==6)
		  T_number=0;
}


/****************************************************************************
*
*用于流量测试的函数
*
*****************************************************************************/
void getdataflow()
{
	static int month=7;
    static int day=1;
    static int hour=0;
    static int minuter=0;
    static int second=0;

	       if(second==59)
	       {
		      second=0;
		      minuter++;
		   }
		   else
		      second++;
	       if(minuter==59)
	       {
	          minuter=0;
	          hour++;
	       }
	       if(hour==23)
	       {
	          hour=0;
	          day++;
	       }
	       if(day==29)
	       {
	          day=1;
	          month++;
	       }
	       if(month==13)
	       {
	          month=1;
	       }
	flow_data[0]=month;
	flow_data[1]=day;
	flow_data[2]=hour;
	flow_data[3]=minuter;
	flow_data[4]=second;
		}
/*****************************************************************************
*
*读目前流量的备份地址
*
*****************************************************************************/
unsigned long read_flow_add_bake()
{
	unsigned long result;
	unsigned short *temp1;
	unsigned short *temp2;
	temp1=(unsigned short*)(FLASH_BASE_ADD+0x90000+now_flow_add*4);
	temp2=temp1+1;
	result=(*temp1<<16)+*temp2;
	return result;
}

/*****************************************************************************
*
*读目前流量的地址
*
******************************************************************************/
unsigned long read_flow_add()
{
	unsigned long result;
	unsigned short *temp1;
	unsigned short *temp2;
	temp1=(unsigned short*)(FLASH_BASE_ADD+0x80000+now_flow_add*4);

	temp2=temp1+1;
	result=(*temp1<<16)+*temp2;
	return result;
}

/***************************************************************************
*
*
*
*****************************************************************************/
unsigned long read_last_flow_add()
{
	unsigned long result;
	unsigned long result1;
	unsigned long result2;
	unsigned short *temp1;
	unsigned short *temp2;
	unsigned short *temp3;
	unsigned short *temp4;
	unsigned short *temp5;
	int numb;
	result=FLASH_BASE_ADD+0x10000;
	temp5=(unsigned short*)(FLASH_BASE_ADD+0x80000);
	temp3=(unsigned short*)(FLASH_BASE_ADD+0x80000+16383*4);
	temp4=(unsigned short*)(FLASH_BASE_ADD+0x90000+16383*4);
	numb=0;
	if(now_flow_add==0 && *temp3==0xffff && *temp4==0xffff)
	{
		result=FLASH_BASE_ADD+0x10000;
	}
	else
	{
		if(now_flow_add==0 && (*temp3!=0xffff || *temp4!=0xffff))
		{
flow0:			if(*temp3!=0xffff && *temp4!=0xffff)
			   {
				temp1=temp3;
				temp2=temp1+1;
				result1=(*temp1<<16)+*temp2;
				temp1=temp4;
				temp2=temp1+1;
				result2=(*temp1<<16)+*temp2;
				numb++;
				if(result1==result2)
				{
					result=result1+70*numb;
				}
				else
				{
					temp3=temp3-2;
					temp4=temp4-2;
					if(temp3>=temp5)
					  goto flow0;
					else
					  result=FLASH_BASE_ADD+0x10000;
				}
			}
		}
	}

	numb=1;
	if(now_flow_add==1 && *temp3==0xffff && *temp4==0xffff)
	{
		result=FLASH_BASE_ADD+0x10046;
	}
	else
	{
		if(now_flow_add==1 && (*temp3!=0xffff || *temp4!=0xffff))
		{
flow1:			if(*temp3!=0xffff && *temp4!=0xffff)
			   {
				temp1=temp3;
				temp2=temp1+1;
				result1=(*temp1<<16)+*temp2;
				temp1=temp4;
				temp2=temp1+1;
				result2=(*temp1<<16)+*temp2;
				numb++;
				if(result1==result2)
				{
					result=result1+70*numb;
				}
				else
				{
					temp3=temp3-2;
					temp4=temp4-2;
					if(temp3>=temp5)
					  goto flow1;
					else
					  result=FLASH_BASE_ADD+0x10000;
				}
			}
		}
	}
	numb=0;
	if(now_flow_add>1)
	{
		temp3=(unsigned short*)(FLASH_BASE_ADD+0x80000+(now_flow_add-1)*4);
		temp4=(unsigned short*)(FLASH_BASE_ADD+0x90000+(now_flow_add-1)*4);
flow2:		temp1=temp3;
		temp2=temp1+1;
		result1=(*temp1<<16)+*temp2;
		temp1=temp4;
		temp2=temp1+1;
		result2=(*temp1<<16)+*temp2;
		numb++;
		if(result1==result2)
		{
			result=result1+70*numb;
		}
		else
		{
			temp3=temp3-2;
			temp4=temp4-2;
			if(temp3>=temp5)
			  goto flow2;
			else
			  result=FLASH_BASE_ADD+0x10000;
		}
    }
	return result;
}

/*****************************************************************************
*
*写目前流量的地址
*
*****************************************************************************/
void write_flow_add()
{
	int flag;
	unsigned long address;
	unsigned short temp;
	unsigned short* tempp;
	volatile unsigned short *fb  = (unsigned short *) na_cfi_flash_0_base;
	#ifdef nasys_main_flash
	  if (-1 == (int)fb)
	    fb = nasys_main_flash;
	#endif

	if(now_flow_add==0)
	    nr_flash_erase_sector((unsigned short*)na_cfi_flash_0_base,(unsigned short*)(FLASH_BASE_ADD+0x80000));

	address=read_last_flow_add();
    unlock_bypass_mode(fb);
    temp=(unsigned short)(address>>16);
    flag = nr_flash_write_bypass((unsigned short*)na_cfi_flash_0_base,(unsigned
short*)(FLASH_BASE_ADD+0x80000+now_flow_add*4),temp);
    temp=(unsigned short)address;
    flag = nr_flash_write_bypass((unsigned short*)na_cfi_flash_0_base,(unsigned
short*)(FLASH_BASE_ADD+0x80002+now_flow_add*4),temp);
    reset_bypass_mode(fb);

	if(now_flow_add==0)
	    nr_flash_erase_sector((unsigned short*)na_cfi_flash_0_base,(unsigned short*)(FLASH_BASE_ADD+0x90000));

	address=read_last_flow_add();
    unlock_bypass_mode(fb);
    temp=(unsigned short)(address>>16);
    flag = nr_flash_write_bypass((unsigned short*)na_cfi_flash_0_base,(unsigned
short*)(FLASH_BASE_ADD+0x90000+now_flow_add*4),temp);
    temp=(unsigned short)address;
    flag = nr_flash_write_bypass((unsigned short*)na_cfi_flash_0_base,(unsigned
short*)(FLASH_BASE_ADD+0x90002+now_flow_add*4),temp);
    reset_bypass_mode(fb);

    now_flow_add++;
}

/*****************************************************************************
*
*复制目前流量的地址
*
*****************************************************************************/
void copy_flow_add()
{
	int flag;
	unsigned short* temp1;
	unsigned short* temp2;
	unsigned short* add1;
	unsigned short* add2;
	int i;
	int flow_num=0;

	volatile unsigned short *fb  = (unsigned short *) na_cfi_flash_0_base;
	#ifdef nasys_main_flash
	  if (-1 == (int)fb)
	    fb = nasys_main_flash;
	#endif

	add1=(unsigned short*)(FLASH_BASE_ADD+0x80000);
	add2=(unsigned short*)(FLASH_BASE_ADD+0x90000);
	temp1=add1;
	temp2=add2;
	for(i=0; i<10; i++)
	{
		if(*add1==0xffff && *add2!=0xffff)

⌨️ 快捷键说明

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