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

📄 ethernet_test.c

📁 ADSP 地层驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
			buf->pNext = NULL;
			if (event == ADI_ETHER_EVENT_FRAME_RCVD) 
			{
				DecrInt(&CurRcve);
				DumpBuffer(buf,0);
				reuse = 1;
				// get the status word and see if the reported received length is correct
				int Status = buf->StatusWord;
				
				if ((Status&0x3000) == 0x3000) 
				{
					// valid frame received
					int nobytes;
					unsigned char lst,nxt;
					
					IncrInt(&NoRcvd);
					frm = (ADI_ETHER_FRAME_BUFFER *)buf->Data;
					nobytes = frm->NoBytes-4; // 4 bytes of CRC
					if (nobytes>128) reuse=0;
					CurRxStat.nframes++;
					CurRxStat.nbytes += nobytes;
	#ifdef FILL_AND_CHECK				
					if (nobytes==60) 
					{
						// short frame padded out
						unsigned short int lt;
						lt = (frm->LTfield[0]<<8)+frm->LTfield[1];
						nobytes = lt+sizeof(ADI_ETHER_FRAME_BUFFER)-2;
					}
					
#ifdef CHECK_FRAMES			
					if (1)
					{
						TEMPLATE_INFO xx;
						TEMPLATE_INFO *tm = &xx;
						int *fsize = (int *)frm->Data;
						xx.lnth = FrameSize;
						xx.pattern = 0x5a;
#ifdef BOARD_TO_BOARD						
						SizeOK[*fsize] = true;
#endif						
						
#else
					if ((0<=nbuf->template_id) && (nbuf->template_id<=9)) 
					{
						TEMPLATE_INFO *tm = GetTemplate(nbuf->template_id);
						
#endif
												
						if (nobytes == sizeof(ADI_ETHER_FRAME_BUFFER)-2+sizeof(int)+tm->lnth) 
						{
							ok = 1;
							ch = nbuf->data;
							i = 0;
							while (i<tm->lnth) 
							{
								if (*ch++ != tm->pattern) 
								{
									ok = 0;
									break;
								}
								i++;
							}
							if (ok ==0) 
							{
								IncrIgnored();
							}
						} 
						else 
						{
							// the no. of bytes is incorrect
							IncrIgnored();
							
						}
					} 
					else 
					{
						// ignore the frame
						IncrIgnored();
					}
					
#endif			
				} 
				else 
				{
					IncrInt(&NoRcvdErrs);
				}
				FreeBuffer(nbuf);
				IssueRead(handle);
#ifdef CHECK_FRAMES				
				FrameRcvd=true;
#endif	
				
			} 
			else 
			{
				if (event == ADI_ETHER_EVENT_FRAME_XMIT) 
				{
					// get the status word and see if the reported received length is correct
					int Status = buf->StatusWord;
#ifdef CHECK_FRAMES
					FrameXmit = true;
#endif										
					
					DecrInt(&CurXmit);
					DumpBuffer(buf,1);
					
					if ((Status&0x3) == 0x3) {
						// valid frame transmitted
						int nobytes = buf->ProcessedElementCount;
						frm = (ADI_ETHER_FRAME_BUFFER *)buf->Data;
						if (frm->NoBytes != nobytes-2) 
						{
							// count mismatch
							//printf("xmit count mismatch frm=%d, proc=%d\n",frm->NoBytes,nobytes);
							DEBUG_PRINT("Transmit Count Mismatch");
						} 
						else 
						{
							NoXmit++;
							CurTxStat.nframes++;
							CurTxStat.nbytes += nobytes;
						}
					} 
					else 
					{
						NoXmitErrs++;
					}
					FreeBuffer(nbuf);
#ifndef CHECK_FRAMES				
					IssueWrite(handle);
#endif					
				} 
				else 
				{
					// it must be an interrupt event
					IncrInt(&NoInts);
					
				}
			}
			nbuf = nxt_buffer;
		}
	} 
	else 
	{
		// ethernet event
		;
	}
	adi_int_ExitCriticalRegion(xit);
}


int GetRtcSecs(void)
{
	unsigned int clk = *pRTC_STAT;
	return (((clk>>12)&0xf)*3600) + (((clk>>6)&0x3f)*60) + (clk&0x3f);
}

/************************************************************************
 *
 * Setup BF533 with best possible SCLK,CCLK settings. 
 * Note: One has to be careful moving this code to other processors as 
 * it my break the part if configured wrongly. 
 *
 **/
#ifdef __ADSPBF533__
void setup_bf533_using_ssl(void)
{
	// initialize power management module
	ADI_PWR_COMMAND_PAIR ezkit[] = 
	{	
		{ ADI_PWR_CMD_SET_EZKIT, (void*)ADI_PWR_EZKIT_BF533_600MHZ },	
		//{ ADI_PWR_CMD_SET_PC133_COMPLIANCE, FALSE},
		{ ADI_PWR_CMD_END, 0}
	};
	adi_pwr_Init(ezkit);
	
	// initialize EBIU module
	//adi_ebiu_Init(sdram_config,0);
	
	// set max speed
	adi_pwr_SetFreq(0,0,ADI_PWR_DF_NONE);
	u32 fcclk,fsclk,fvco;
	adi_pwr_GetFreq(&fcclk,&fsclk,&fvco);
	printf("BF533 board running at %dMHz CCLK and %dMHz SCLK\n",fcclk,fsclk);
}
#endif /* __ADSPBF533__ */

/************************************************************************
 *
 * Initialize the System Services Libraries and LAN91c111 driver. Done only
 * once.
 *
 **/
static int InitSSLAndDriver(void *arg)
{
	ADI_DEV_MANAGER_HANDLE devmgr_handle;
	ADI_DCB_HANDLE	dcbmgr_handle;
	unsigned int result,mask;
	u32 response_count;
	int i,recv_data=0;
	ADI_ETHER_MEM_SIZES msizes;
	ADI_ETHER_BUFFER *buf;
	long long int last_irl=0;
	static int critical_handle;
	int linkUp=false;
	int checkCount = 0;

    // If system is already initialzied we will return.	
    if(g_SystemInitialized)
   	     return 0;

   // setup_bf533_using_ssl();	
	heap_ndx = heap_install((void *)16,0x20000,127);

	AllocateBuffers(heap_ndx);

	// initialize the interrupt manager
	// 
	result = adi_int_Init(intmgr_storage,sizeof(intmgr_storage),&response_count,&critical_handle);
	 
	if(result != ADI_DEV_RESULT_SUCCESS)
	{
		DEBUG_PRINT("Failed to Initilize device manager\n");
		return -1; 
	}
	// initialize the device manager
	//
	result = adi_dev_Init(devmgr_storage,sizeof(devmgr_storage),&response_count,&devmgr_handle,&critical_handle);
	if(result != ADI_DEV_RESULT_SUCCESS)
	{
		DEBUG_PRINT("Failed to Initilize device manager\n");
		return -1;
	}
	
	result =  adi_dcb_Init(dcbmgr_storage, sizeof(dcbmgr_storage), &response_count,&critical_handle);

	if(result != ADI_DCB_RESULT_SUCCESS)
	{
		DEBUG_PRINT("Failed to open the landevice \n");
		return -1;
	}
	
	result = adi_dcb_Open(ik_ivg14, dcbqueue_storage, sizeof(dcbqueue_storage), &response_count, &dcbmgr_handle);
	

 	// open lan-device
	//
	result = adi_dev_Open(devmgr_handle,&ADI_ETHER_USBLAN_Entrypoint,0,&lan_handle,&lan_handle,
				ADI_DEV_DIRECTION_BIDIRECTIONAL,NULL,dcbmgr_handle,LanCallback);

	if(result != ADI_DEV_RESULT_SUCCESS)
	{
		DEBUG_PRINT("Failed to open the landevice \n");
		return -1;
	}
	// get memory siea
	mask = ADI_ETHER_CMD_MEM_SIZES;
	result = adi_dev_Control(lan_handle,ADI_ETHER_CMD_MEM_SIZES,&msizes);
	if (result == ADI_DEV_RESULT_SUCCESS) 
	{
		
		result = adi_dev_Control(lan_handle,ADI_ETHER_CMD_SUPPLY_MEM,&memtable);
		if (result == ADI_DEV_RESULT_SUCCESS) 
		{
			DriverHandle = lan_handle;
			result = adi_dev_Control(lan_handle,ADI_DEV_CMD_TABLE,&setup);
			if (result == ADI_DEV_RESULT_SUCCESS) 
			{			
				// provide the driver with some buffers before starting the device
				while ((FreeBuf) && (FreeBuf->next)) 
				{
					IssueRead(lan_handle);
				}
				// now we start the controller
				result = adi_dev_Control(lan_handle,ADI_ETHER_CMD_START,NULL);
				if (result == ADI_DEV_RESULT_SUCCESS) 
				{
					adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_PHY_REGS,phyregs);
					do 
					{
					
					   // check if link is already up		
					    if (((phyregs[1] & 0x4)==0x4))
					    {
                            linkUp = true;
					        break;
					    }
						// read the PHY registers again
					   adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_PHY_REGS,phyregs);
					checkCount++;
					}while(checkCount < 10);

				     if(linkUp == true)
				     {
				        DEBUG_PRINT("Link Established");
				     }
				     else
				     {
				   	  	return -1;
				     }
				} 
				else 
				{
					DEBUG_PRINT("adi_dev_Control for CMD_START returned Error");
				}
			} 
			else 
			{
				DEBUG_PRINT("adi_dev_Control for CMD_TABLE returned Error");
			}
		}
	} 
	else 
	{
		DEBUG_PRINT("adi_dev_Control returned Error");
	}

 g_SystemInitialized = true;
 return 1; 
}

/************************************************************************
 *
 * Initialize Real Time Clock (RTC). RTC peripheral is used to keep track
 * of time.
 *
 **/
static int InitRealTimeClock(unsigned int inSeconds)
{
 unsigned long clock,min;
 int StartMin,EndMin;

		clock = *pRTC_STAT;
		StartMin = (clock>>6)&0x3ff;
		EndMin = StartMin+5;

		*pRTC_PREN = 1;	/* enable prescaler and the 1Khz clock */
		while ((*pRTC_ISTAT&0x8000)==0);	/* wait for writes to complete */

		while ((*pRTC_ISTAT&4)==0);  // wait for seconds event
		*pRTC_STAT = 0;
		while ((*pRTC_ISTAT&0x8000)==0);	/* wait for writes to complete */

		clock = *pRTC_STAT;
		StartMin = (clock>>6)&0x3ff;
		EndMin = StartMin+5;
		return 1;
}

/************************************************************************
 *
 *  Main test routine will get launched from the POST framework.
 *
 */
int TEST_ETHERNET(void)
{
	int rtcsecs;
	unsigned long clock,min;
	int StartMin,EndMin;
	ADI_ETHER_STATISTICS_COUNTS cts;
	bool ret=false;

	if((InitSSLAndDriver(0)) == -1)
	{
	    DEBUG_PRINT("Unable to Initialize System. Check System Settings");
	    return 0;
	}
	// check if link is up  or not.
	else
	{
	  	adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_PHY_REGS,phyregs);
		if (!((phyregs[1] & 0x4)==0x4)) 
		{
		    // User might have pulled out the plug orsomething happend
		    // we can not proceed.
			DEBUG_PRINT("System Settings Changed, unable to proceed.");
			return 0;
		}
	}
	
	InitRealTimeClock(30);
	
#ifdef CHECK_FRAMES
#ifdef SENDING
	for (FrameSize=1;FrameSize<=1496; FrameSize++) 
	{
		FrameRcvd = false;
		FrameXmit = false;
		while (IssueWrite(lan_handle)==0);
		while (!FrameXmit);	// wait for the frame to hav egone
#ifndef BOARD_TO_BOARD		
		SizeOK[FrameSize] = true;
		while (!FrameRcvd) 
		{
			adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_STATISTICS,&cts);
			if (cts.cEMAC_RX_CNT_IRL>last_irl) 
			{
				last_irl = cts.cEMAC_RX_CNT_IRL;
				SizeOK[FrameSize] = false;
				break;
			}
			if (cts.cEMAC_RX_CNT_ALLF>=cts.cEMAC_TX_CNT_OK) break;
		}
#endif		
	}	
	for (FrameSize=1;FrameSize<=1496; FrameSize++) 
	{
		if (!SizeOK[FrameSize]) 
		{
			//printf("Size %d failed\n",FrameSize);
		}
	}
#else
	// receiving board to board
	adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_STATISTICS,&cts);
	while (cts.cEMAC_RX_CNT_ALLF < 1496) 
	{
		adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_STATISTICS,&cts);
	}
	for (FrameSize=1;FrameSize<=1496; FrameSize++) 
	{
		if (!SizeOK[FrameSize]) 
		{
			//printf("Size %d failed\n",FrameSize);
		}
	}
#endif	
	{
		unsigned long  int *c = (unsigned  long int *)&cts;
		
		adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_STATISTICS,&cts);
		for (i=0; i<sizeof(cts)/sizeof(long long int);i++) 
		{					
			printf("0x%8.8x%8.8x  %s\n",(unsigned int)c[2*i+1],c[2*i],CtDesc[i] );
		}
	}
#else	
	// provide the driver with some frames to transmit 
	IssueWrite(lan_handle);
	
	rtcsecs= GetRtcSecs();
	while ((NoRcvd<NO_FRAMES) || (rtcsecs<30)) 	
	{
		clock = *pRTC_STAT;
		min = (clock>>6)&0x3f;
		StartMin = (clock>>6)&0x3ff;
		if (NoRcvd>=NO_FRAMES) break;
		if (min != CurMinute) 
		{
			// update the stats
			int ndx = CurMinute % 10;
			
			CurMinute = min;
			AvgRxStat[ndx].nbytes = CurRxStat.nbytes/60;
			AvgRxStat[ndx].nframes = CurRxStat.nframes/60;
			
			AvgTxStat[ndx].nbytes = CurTxStat.nbytes/60;
			AvgTxStat[ndx].nframes = CurTxStat.nframes/60;
			
			CurMinute = min;
			
			if (DumpStats) 
			{
				int i;
				printf("     Rx kfr/sec  Rx kbytes/sec     Tx kfr/sec  Tx kbytes/sec\n");
				for (i=0;i<10;i++) 
				{
					printf("%15u%15u%15u%15u\n",
					AvgRxStat[i].nframes/1024,AvgRxStat[i].nbytes/1024,
					AvgTxStat[i].nframes/1024,AvgTxStat[i].nbytes/1024);
				}
				printf("\n\n");
				DumpStats = 0;
				
			}
			

		}
		IssueRead(lan_handle);
		adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_STATISTICS,&cts);
		adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_PHY_REGS,phyregs);
		
		rtcsecs= GetRtcSecs();
		
	}
	rtcsecs= GetRtcSecs();
	DumpStats = 0;
			
	// check on success
	if ((NoXmit<NO_FRAMES/2) || (NoRcvd<NO_FRAMES/2)) 
	{
		DEBUG_PRINT("FAIL");
		ret = false;
	} 
	else 
	{
		unsigned long  int *c = (unsigned  long int *)&cts;
		
		//printf("OK  Sent=%u (0x%4.4x), Rcvd=%u (0x%4.4x)\n",NoXmit,NoXmit,NoRcvd,NoRcvd);
		DEBUG_PRINT("PASS");
		adi_dev_Control(lan_handle,ADI_ETHER_CMD_GET_STATISTICS,&cts);
		ret = true;
	}
#endif
	return ret;
}

⌨️ 快捷键说明

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