📄 i2ctest.c
字号:
/* i2c.c
*
*---------------------------------------------------------------------------
*
* I N T E L P R O P R I E T A R Y
*
* COPYRIGHT (c) 2002 BY INTEL CORPORATION. ALL RIGHTS
* RESERVED. NO PART OF THIS PROGRAM OR PUBLICATION MAY
* BE REPRODUCED, TRANSMITTED, TRANSCRIBED, STORED IN A
* RETRIEVAL SYSTEM, OR TRANSLATED INTO ANY LANGUAGE OR COMPUTER
* LANGUAGE IN ANY FORM OR BY ANY MEANS, ELECTRONIC, MECHANICAL,
* MAGNETIC, OPTICAL, CHEMICAL, MANUAL, OR OTHERWISE, WITHOUT
* THE PRIOR WRITTEN PERMISSION OF :
*
* INTEL CORPORATION
*
* 2200 MISSION COLLEGE BLVD
*
* SANTA CLARA, CALIFORNIA 95052-8119
*
*---------------------------------------------------------------------------
*
*
* system: IXP2400
* subsystem: DIAG
* author: ccm, Jan 31, 02
* revisions:
*
*
* --------------------------------------------------------------------------
*/
#include "misc_func.h"
#include "diagstruct.h"
#include "i2c_test.h"
#include "i2c.h"
#include "diag_utils.h"
#include "led.h"
#include "uart.h"
#include "syslog.h"
// static function prototypes
static void I2C_Help(void);
static void I2C_Pattern_Test(void);
static void I2C_Write_Test(unsigned char addr, unsigned int data);
static void I2C_Page_Write_Test(unsigned char start_addr);
// Redefine SLAVE_ADDR to A4 (Media Card I2C)
#define SLAVE_ADDRESS 0xa4
//
// Function: I2C_Help
//
static void I2C_Help(void)
{
eprintf("> t i2c <option>, where\n");
eprintf("<option> are:\n");
eprintf("m - I2C pattern test\n");
eprintf("w - I2C write test <h:addr> <h:data>\n");
eprintf("p - I2C page write test <h:addr>\n");
eprintf("a - All I2C tests <h:addr> <h:data>\n");
eprintf("h - I2C help message\n");
}
//
// Function: I2C_Test
//
void I2C_Test(void)
{
unsigned char opt;
unsigned int addr, no_of_loops, i;
register PDiagCommon acL = (PDiagCommon) ACADDRESS;
opt = acL->argv[2][0];
addr = acL->hexArg[3];
no_of_loops = 1;//acL->decArg[0];
if(acL->HostType != MASTER)
{
eprintf("I2C Test is only available on the Master NPU.\n");
return;
}
switch(opt)
{
case 'm': for(i = 0; i < no_of_loops; i++)
I2C_Pattern_Test();
break;
case 'w': if(acL->argc < 5)
{
eprintf("***** ERROR *****: Insufficient arguments.");
eprintf("Please type \"t i2c h\" for help.\n");
}
else
{
for(i = 0; i < no_of_loops; i++)
I2C_Write_Test(addr, acL->hexArg[4]);
}
break;
case 'p': if(acL->argc < 4)
{
eprintf("***** ERROR *****: Insufficient arguments.");
eprintf("Please type \"t i2c h\" for help.\n");
}
else
{
for(i = 0; i < no_of_loops; i++)
I2C_Page_Write_Test(addr);
}
break;
case 'a': if(acL->argc < 5)
{
eprintf("***** ERROR *****: Insufficient arguments.");
eprintf("Please type \"t i2c h\" for help.\n");
}
else
{
I2C_Pattern_Test();
I2C_Write_Test(addr, acL->hexArg[4]);
I2C_Page_Write_Test(addr);
}
break;
case 'h':
default: I2C_Help();
}
}
//
// Function: I2C_Pattern_Test
//
static void I2C_Pattern_Test(void)
{
unsigned int addr;
int j, failure = 0;
unsigned char pattern_w, pattern_r[16], answer;
unsigned int Test_Pattern[4] = {0xA5,0x5A,0x00,0xFF};
eprintf("**** WARNING ****: The I2C Pattern Test reduces ");
eprintf("the write life of the I2C by writing to the I2C.\n");
eprintf("Do you want to continue? ('y' to continue)\n");
answer = GetChar();
if(answer == 'y')
{
for(addr = Test_Start_Addr; addr<=Test_End_Addr; addr++)
for(j = 0, pattern_w = Test_Pattern[j]; j<4; j++)
{
i2c_write((unsigned char)SLAVE_ADDRESS, (unsigned char)addr, pattern_w);
i2c_read(SLAVE_ADDRESS, addr, pattern_r);
if(pattern_w != pattern_r[0])
{
eprintf("Writing pattern failed at addr: %d\n", addr);
failure++;
}
}
eprintf("I2C Pattern Test... ");
if(failure == 0)
{
eprintf("PASS\n");
Set_LED("Im P");
slowport_code[0] = SP_I2C;
slowport_code[1] = SP_PASS;
dump_slowport(slowport_code, 2, SP_NON_FATAL);
}
else
{
eprintf("I2C PATTERN WRITE TEST FAIL!!!\n");
Set_LED("Im F");
slowport_code[0] = SP_I2C;
slowport_code[1] = SP_FAIL;
dump_slowport(slowport_code, 2, SP_NON_FATAL);
// prepare structure for dumping
syslog.type = TYPE_ERROR;
syslog.source_comp = MASTER_DIAG;
strcpy(syslog.desc, "I2C Pattern Test FAIL");
// write to syslog
syslog_dump(&syslog, sizeof(SYSLOG_DATA));
}
}
return;
}
//
// Function: I2C_Write_Test
//
static void I2C_Write_Test(unsigned char addr, unsigned int data)
{
unsigned char data_r[16] = "", answer;
eprintf("**** WARNING ****: The I2C Pattern Test reduces ");
eprintf("the write life of the I2C by writing to the I2C.\n");
eprintf("Do you want to continue? ('y' to continue)\n");
answer = GetChar();
if(answer == 'y')
{
i2c_read(SLAVE_ADDRESS, 0x0, data_r);
if(data_r[0] != 0xFF) // Config data is programmed, do not access config data
{
// If addr falls within config data area, do not allow write
if( (addr <= RESERVED_HIGH_ADDR) || (addr >= EEPROM_SIZE) )
{
eprintf("The address used falls in reserved space.\n");
eprintf("Please use addresses from %08x to %08x\n",
RESERVED_HIGH_ADDR, (EEPROM_SIZE-1));
return;
}
}
i2c_write(SLAVE_ADDRESS, addr, (unsigned char)data);
i2c_read(SLAVE_ADDRESS, addr, data_r);
eprintf("I2C Write Test... ");
if(data == data_r[0])
{
eprintf("PASS\n");
Set_LED("Iw P");
slowport_code[0] = SP_I2C;
slowport_code[1] = SP_PASS;
dump_slowport(slowport_code, 2, SP_NON_FATAL);
}
else
{
eprintf("I2C WRITE TEST FAIL!!!\n");
Set_LED("Iw F");
slowport_code[0] = SP_I2C;
slowport_code[1] = SP_FAIL;
dump_slowport(slowport_code, 2, SP_NON_FATAL);
// prepare structure for dumping
syslog.type = TYPE_ERROR;
syslog.source_comp = MASTER_DIAG; // Determine master/slave
strcpy(syslog.desc, "I2C Write Test FAIL");
// write to syslog
syslog_dump(&syslog, sizeof(SYSLOG_DATA));
}
}
return;
}
//
// Function: I2C_Page_Write_Test
//
static void I2C_Page_Write_Test(unsigned char start_addr)
{
int i;
unsigned char data_w[32] = "", data_r[32] = "", answer;
eprintf("**** WARNING ****: The I2C Page Write Test reduces ");
eprintf("the write life of the I2C by writing to the I2C.\n");
eprintf("Do you want to continue? ('y' to continue)\n");
answer = GetChar();
if(answer == 'y')
{
// Check if first byte is 0xFF (blank), if so, then test the entire I2C.
// If not don't write to address location 0-127.
i2c_read(SLAVE_ADDRESS, 0x0, data_r);
if(data_r[0] != 0xFF) // Config data is programmed, do not access config data
{
if( (start_addr < RESERVED_HIGH_ADDR) || (start_addr > 239) ) // 255 - 16 = 239
{
eprintf("The address used falls in reserved space.\n");
eprintf("Please use addresses from %08x to %08x\n",
(RESERVED_HIGH_ADDR+1), (EEPROM_SIZE-1));
return;
}
}
for(i = 0; i < 16; i++)
data_w[i] = start_addr + i;
// The offset address is 16 byte-aligned and therefore
// the lower 3 bits are masked off
i2c_page_write(SLAVE_ADDRESS, (start_addr & 0xF0), data_w, 16);
i2c_seq_read(SLAVE_ADDRESS, (start_addr & 0xF0), data_r, 16);
eprintf("I2C Page Write Test... ");
if(strcmp(data_w, data_r) == 0)
{
eprintf("PASS\n");
Set_LED("Ip P");
slowport_code[0] = SP_I2C;
slowport_code[1] = SP_PASS;
dump_slowport(slowport_code, 2, SP_NON_FATAL);
}
else
{
eprintf("I2C PAGE WRITE TEST FAIL!!!\n");
Set_LED("Ip F");
slowport_code[0] = SP_I2C;
slowport_code[1] = SP_FAIL;
dump_slowport(slowport_code, 2, SP_NON_FATAL);
// prepare structure for dumping
syslog.type = TYPE_ERROR;
syslog.source_comp = MASTER_DIAG;
strcpy(syslog.desc, "I2C Page Write Test FAIL");
// write to syslog
syslog_dump(&syslog, sizeof(SYSLOG_DATA));
}
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -