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

📄 i2c_test.c

📁 I2C testing sample code
💻 C
字号:
//****************************************************************************
// 
// Faraday Technology Coporation 
// Copyright (C) Microsoft Corporation. All rights reserved.
//                                
//*--------------------------------------------------------------------------*
//* Name:I2C_test.c                                                          *
//* Description: I2C test functions                                          *
//* Author: Shawn, Hsiao                                                     *
//* Date: 2006/08/07                                                         *
//* Version:0.1	                                                             *
//* Note: modified from old burn-In                                          *
//****************************************************************************

//============================================================================
//        Include
//============================================================================

#include <stdio.h>
#include "I2C_test.h"
#include "fLib.h"
#include "I2C.h"			

//============================================================================
//        Definition
//============================================================================

#define I2C_DEBUG	// must define, and do not know why...

//============================================================================
//        Global Variables
//============================================================================

volatile BOOL DT_LOCK;
volatile BOOL DR_LOCK;
volatile BOOL I2C_LOCK;

UINT32 I2C_TEST_RESULT;

//============================================================================
//        Functions
//============================================================================

extern void DelayTime1(UINT32);
extern unsigned int PMU_get_pclk(void);
extern UINT32 Esc_Key;
extern INT32 fLib_printf(const char *f, ...);

extern void API_SYS_set_MUX_I2C(void);

/* I2C test main
*/
int I2CTest (void)
{
	UINT32 i;
	int ret = 0;
	
	I2C_TEST_RESULT=  TRUE;

	printf("\rBegin I2C Test... \n");
	
	// set MUX to I2C
	API_SYS_set_MUX_I2C();

	// set delay timer
	fLib_Timer_Init(1,1000,Timer_Tick_Dispatcher);

	EnableIRQ();			//It enables ARM's IRQ in the Boot.s

	/*Initiated I2C
	*/
	//setting PCLK clock  cycles
	fLib_I2C_SetTGSR(I2C_GSR_Value,I2C_TSR_Value);
	//set clock divider
	fLib_I2C_SetClockdiv(PMU_get_pclk()/50/2/1000);

	/* set interrupt
	*/
	fLib_ConnectInt(FIE702x_IRQ_I2C1, I2C_Isr);
	fLib_SetIntTrig(FIE702x_IRQ_I2C1,EDGE,H_ACTIVE);
	fLib_EnableInt(FIE702x_IRQ_I2C1);

	/* byte write test
	*/
	// pattern 1
	i = ByteWriteTest1();
	if(i == FALSE)
	{
		I2C_TEST_RESULT=FALSE;
		printf("Fail!\n");
		ret = -1;
	}

	// pattern 2
	i = ByteWriteTest2();
	if(i==FALSE)
	{
		I2C_TEST_RESULT=FALSE;
		printf("Fail!\n");
		ret = -1;
	}

	/* page write test
	*/
	// pattern 1
	i = PageWriteTest1();
	if(i == FALSE)
	{
		I2C_TEST_RESULT=FALSE;
		printf("Fail!\n");
		ret = -1;
	}

	// pattern 2
	i = PageWriteTest2();
	if(i == FALSE)
	{
		I2C_TEST_RESULT=FALSE;
		printf("Fail!\n");
		ret = -1;
	}

	printf("End I2C Test!\n");

	fLib_CloseInt(FIE702x_IRQ_I2C1);

	DisableIRQ();			//It disables ARM's IRQ in the Boot.s
	fLib_Timer_Close(1);
	
	return ret;
}

/* I2C ISR
*/
void I2C_Isr(void)
{
	UINT32 status;

	fLib_ClearInt(FIE702x_IRQ_I2C1);

	status = fLib_I2C_ReadStatus();

	if(status & I2C_DT)
	{
		DT_LOCK = FALSE;
		if(!(status & I2C_BUSY))
			I2C_LOCK = FALSE;
	}
	else
	{
		if(status & I2C_DR)
			DR_LOCK = FALSE;
	}
}

/* byte write test pattern 1
*/
UINT32 ByteWriteTest1(void)
{
	UINT32 i, rdata;
	
	for(i = 0; i < 0x100; i ++)
	{
		// wtite byte
		ByteWrite(i,i);

		DelayTime1(12); //delay 10ms for write to device

		rdata = RandomRead(i);

		// verify byte
		if(rdata != i)
		{	// error message
			printf("Read data no match!, write=%d read=%d\n", i, rdata);
			printf("Write Byte and Verify Loop Test Fail!\n");
			return FALSE;
		}

		DelayTime1(10);

		if(Esc_Key) 
			return FALSE;
	}
	
	printf("Write Byte and Verify Loop Test Success!\n");
	
	return TRUE;
}

/* byte write test pattern 2
*/
UINT32 ByteWriteTest2()
{
	UINT32 i, rdata;

	for(i = 0; i < 0x100; i ++)
	{
		// write byte
		ByteWrite(i,i);
		DelayTime1(12); //delay 10ms for write to device
	}

	for(i = 0; i < 0x100; i ++)
	{
		rdata = RandomRead(i);
		
		// verify byte
		if(rdata != i)
		{	// error message
			printf("Read data no match!\n");
			printf("Write All Bytes and Verify Test Fail\n");
			return FALSE;
		}
		
		DelayTime1(10);

		if(Esc_Key) 
			return FALSE;
	}

	printf("Write All Bytes and Verify Test Success!\n");

	return TRUE;
}

/* page write test pattern 1
*/
UINT32 PageWriteTest1()
{
	UINT32 i , j, rdata;

	/*write 8 and read 8
	*/
	for(i = 0;i < 0x100; i += 8)
	{
		// page write
		PageWrite(i);
		DelayTime1(12); //delay 10ms for write to device

		for(j = i; j < i + 8; j ++)
		{
			// verify 
			rdata = RandomRead(j);

			if(rdata != j)
			{	// error message
				printf("Read data no match! write=%d read=%d\n",j,rdata);
				printf("Write Page and Verify Loop Test Fail!\n");
				return FALSE;
			}

			DelayTime1(10); //delay 10ms for write to device

		}//end for j

		if(Esc_Key) 
			return FALSE;
	} //end for i

	printf("Write Page and Verify Loop Test Success!\n");

	return TRUE;
}

/* page write test pattern 2
*/
UINT32 PageWriteTest2()
{
	UINT32 i, rdata;

	/* write all and read all
	*/
	for(i = 0;i < 0x100; i += 8)
	{
		// page write
		PageWrite(i);
		DelayTime1(12); //delay 10ms for write to device
	}

	for(i = 0; i < 0x100; i ++)
	{
		// verify
		rdata = RandomRead(i);

		if(rdata != i)
		{
			printf("Read data no match! write=%d read=%d\n",i,rdata);
			printf("Write All Pages and Verify Test Fail!\n");
			return FALSE;
		}

		DelayTime1(10); //delay 10ms for write to device

		if(Esc_Key) 
			return FALSE;
	}

	printf("Write All Pages and Verify Test Success!\n");

	return TRUE;
}

/* byte write
*/
void ByteWrite(UINT32 waddr,UINT32 wdata)
{
	/* give decive ID
	*/
	// write slave address to device
  fLib_I2C_WriteData(WRITE_DEVICE);

	//set control to start
	DT_LOCK = TRUE;
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN|I2C_START);

  // checking status
  while(DT_LOCK == TRUE)
	;

	/* give address
	*/
  // write address to device
	fLib_I2C_WriteData(waddr);

#ifdef I2C_DEBUG
	fLib_printf("Write address %x\r", waddr);
#endif

  // set control to start
	DT_LOCK = TRUE;
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN);

	// checking status
  while(DT_LOCK == TRUE)
  ;

	/* give data
	*/
  // write data to device
	fLib_I2C_WriteData(wdata);

#ifdef I2C_DEBUG
	fLib_printf("Write data %x\r", wdata);
#endif

  // set control to stop
	DT_LOCK = TRUE;

	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN|I2C_STOP);

  // waiting start to stop
#ifdef I2C_DEBUG
	fLib_printf("Waiting to stop...\r");
#endif

	// checking status
	while(DT_LOCK == TRUE)
	;
}

/* page write
*/
void PageWrite(UINT32 addr)
{
	UINT32 i;
	UINT32 waddr;

	/* give decive ID
	*/
 	// write slave address to device
	fLib_I2C_WriteData(WRITE_DEVICE);

	// set control to start
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN|I2C_START);

	//checking status
	while(DT_LOCK==TRUE)
	;

	/* give address
	*/
	// write address to device
	waddr = 0;
	fLib_I2C_WriteData(waddr);

	DelayTime1(10);

#ifdef I2C_DEBUG
	fLib_printf("Write address %x\r", waddr);
#endif

	/* give data
	*/
	// set control to start
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN);

	//checking status
	while(DT_LOCK==TRUE)
	;

	for(i = addr; i < addr + 8; i ++)
	{
		// write data to device
		fLib_I2C_WriteData(i);

		DelayTime1(10);

#ifdef I2C_DEBUG
		fLib_printf("Write data %x\r", i);
#endif

		// set control to start
		fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN);

	 	// clear status
		while(DT_LOCK==TRUE)
		;
	}

	// set control to stop
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN|I2C_STOP);

	// waiting start to stop
#ifdef I2C_DEBUG
	fLib_printf("Waiting to stop...\r");
#endif

	while(I2C_LOCK==TRUE)
	;
}

/* random read
*/
UINT32 RandomRead(UINT32 raddr)
{
	UINT32 rdata;

	/* give decive ID
	*/
	// write slave address to device
	fLib_I2C_WriteData(WRITE_DEVICE);

	// set control to start
	DT_LOCK = TRUE;
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN|I2C_START);

	// checking status
	while(DT_LOCK==TRUE)
  ;

	/* give address
	*/
	// write address to device
	fLib_I2C_WriteData(raddr);
  	
#ifdef I2C_DEBUG
	fLib_printf("Write address %x\r",raddr);
#endif

	// set control to start
	DT_LOCK = TRUE;
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN);

 	// checking status
	while(DT_LOCK==TRUE)
	;
  
 	/* read data from rom
 	*/
 	// write slave address to device
	// waddr=READ_DEVICE;
	fLib_I2C_WriteData(READ_DEVICE);

	//set control to start
	DT_LOCK = TRUE;
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN|I2C_START);

	// checking status
	while(DT_LOCK == TRUE)
  ;
  
	// read data
	// set control to stop
	DR_LOCK = TRUE;
	fLib_I2C_IOCtrl(I2C_ENABLE|I2C_TBEN|I2C_STOP|I2C_ACKNAK);

 	// checking status
	while(DR_LOCK == TRUE)
  ;
  
	rdata = fLib_I2C_ReadData();
#ifdef I2C_DEBUG
	fLib_printf("Read data=%x\r",rdata);
#endif

	DelayTime1(10);

	return(rdata);
}

extern void i2c_rw_func(void);

/* I2C Test Main
*/
void I2C_Test_Main()
{
	int sel;
	
gain:	
	printf("\n\n[I2C Test]\n");
	printf("(1) I2C Test      (2) I2C Read/Write         \n");
	printf("(3) exit\n");
	
	scanf("%d", &sel);
	
	switch(sel)
	{
	case 1:	// I2C Test
		I2CTest();
		goto gain;
		break;
	case 2:	// I2C Read/Write
		i2c_rw_func();
		goto gain;
		break;
	case 3:	// exit
		break;	
	default:
		goto gain;
		break;	
	}
}

⌨️ 快捷键说明

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