📄 iictest.c
字号:
/*
* Copyright (c) 1995-2000 by TriMedia Technologies.
*
* +------------------------------------------------------------------+
* | This software is furnished under a license and may only be used |
* | and copied in accordance with the terms and conditions of such |
* | a license and with the inclusion of this copyright notice. This |
* | software or any other copies of this software may not be provided|
* | or otherwise made available to any other person. The ownership |
* | and title of this software is not transferred. |
* | |
* | The information in this software is subject to change without |
* | any prior notice and should not be construed as a commitment by |
* | TriMedia Technologies. |
* | |
* | this code and information is provided "as is" without any |
* | warranty of any kind, either expressed or implied, including but |
* | not limited to the implied warranties of merchantability and/or |
* | fitness for any particular purpose. |
* +------------------------------------------------------------------+
*
* Module name : iictest.c 1.33
*
* Last update : 12:06:13 - 99/11/11
*
* Description :
*
* A test and demonstration for the TriMedia IIC library.
* This program can be used to test IIC access on TM1000.
* Available targets on the board include:
* an 8 bit expander port, U17, at address 0x70
* The EEPROM, at address 0xA0
* In addition, the 7111 could be used.
* The 7125 requires clock to operate, and it is not enabled here.
*
* Or a user can hook the IIC interface to another system using J6.
*
*
*
*/
#include <tmlib/dprintf.h> /* for DP debugging */
#include <stdio.h>
#include <ops/custom_defs.h>
#include <tm1/mmio.h>
#include <tm1/tmIIC.h>
#include <tm1/tmBoard.h>
#include <tm1/tmProcessor.h>
#include <tm1/tmHelp.h>
#define PROGRAM_VERSION 2.30
#define EEPROM_ADDRESS 0xA1
#define IIC_EEPROM_SIZE 32
/* private functions */
static void iicError2String(int error, char *str);
static int checkSanity(void);
void
printhelp(void)
{
printf("\nTM IIC Test program: Available Commands:\n");
printf("\tr address: single byte iic read\n");
printf("\tw address data: single byte iic write\n");
printf("\tsr address subaddress: single byte subaddress read\n");
printf("\tsw address subaddress data: single byte subaddress write\n");
printf("\te: Read EEPROM\n");
printf("\tmr address count: multi byte iic read\n");
printf("\tmw address [data] count: multi byte iic write\n");
printf("\tme (count): Read EEPROM in multibyte mode\n");
printf("\tf: Fill EEPROM with default values\n");
printf("\tps: Check processor speed\n");
printf("\tq: quit\n\n");
}
void dumpEEPROM(void);
void dumpProcSpeed(void);
/* To test completion functions */
void testComp(iicRequest_t * req)
{
DP(("Completed request %x (a: %x, c: %d) with error %x\n",
req, req->address, req->byteCount, req->errorCode));
}
/****************************************/
int main()
{
char ins[80];
unsigned int d, address;
unsigned int data;
int retval;
int ic, ic1;
int i, subaddress;
char errstr[25];
iicCapabilities_t *piicCap;
DP_START(64*1024, Null);
DP(("iictest: TM-1 IIC test program\n"));
printf("\niictest: TM-1 IIC Test program, v%2.2f.\n", PROGRAM_VERSION);
iicGetCapabilities(&piicCap);
printf("Using IIC library version %d.%d.%d.\n",
piicCap->version.majorVersion,
piicCap->version.minorVersion,
piicCap->version.buildVersion);
checkSanity(); /* report if board is not responding */
DP(("Running on ")); tmHelpReportSystem(Null);
printf("Running on "); tmHelpReportSystem(stdout);
dumpEEPROM();
dumpProcSpeed();
#ifdef __TCS_nohost__
exit(0);
#endif
printhelp();
while (1) {
printf("e, r, w, sr, sw, me, mr, mw, f, ps, q:\n");
gets(ins);
switch (*ins) {
case 'r': /* read one byte, using the iicReadReg function */
DP(("\n%s\n", ins));
sscanf(ins, "%c %x", &ic, &address);
printf("read from 0x%x\n", address);
retval = iicReadReg(address, -1, &data);
if (retval) {
iicError2String(retval, errstr);
printf("IIC Read returned %s error and data "
"0x%X\n", errstr, data);
}
else
printf("Data 0x%X read from address 0x%X\n", data, address);
break;
case 'w': /* write one byte using the iicWriteReg function */
DP(("\n%s\n", ins));
retval = sscanf(ins, "%c %x %x", &ic, &address, &d);
data = (unsigned int) d;
retval = iicWriteReg(address, -1, d);
if (retval) {
iicError2String(retval, errstr);
printf("iicWriteReg to address 0x%X returned %s "
"error\n", address, errstr);
}
else
printf("0x%X written to address 0x%X\n", data, address);
break;
case 's': /* subaddresss: The IIC bus supports a "subaddress" mode. */
{
DP(("\n%s\n", ins));
switch (ins[1]) {
case 'r': /* subaddress read. iicReadReg supports this. */
sscanf(ins, "%c%c %x %x", &ic, &ic1, &address, &subaddress);
retval = iicReadReg(address, subaddress, &data);
if (retval) {
iicError2String(retval, errstr);
printf("IIC Read from 0x%X, 0x%X returned "
"%s error and data 0x%X\n",
address, subaddress, errstr, data);
}
else
printf("Data 0x%X read from address 0x%X, "
"0x%X\n", data, address, subaddress);
break;
case 'w': /* subaddress write. iicWriteReg supports this. */
retval = sscanf(ins, "%c%c %x %x %x", &ic, &ic1, &address, &subaddress, &d);
data = (unsigned int) d;
retval = iicWriteReg(address, subaddress, d);
if (retval) {
iicError2String(retval, errstr);
printf("iicWriteReg to address 0x%x, 0x%x "
"returned %s error\n",
address, subaddress, errstr);
}
else
printf("0x%X written to address 0x%X, "
"0x%X\n", data, address, subaddress);
break;
}
break;
}
case 'm': /* multibyte read and write */
{
char ic2;
DP(("\n%s\n", ins));
switch (ins[1]) {
case 'r': /* multibyte read, using iicDispatch() */
{
iicRequest_t req;
Int instance, address, count;
UInt8 xdata[256];
for (i = 0; i < 255; i++)
xdata[i] = 0;
sscanf(ins, "%c%c %x %d", &ic, &ic2, &address, &count );
req.address = address;
req.byteCount = count;
req.direction = IIC_READ;
req.type = IIC_SIMPLE;
req.data = xdata;
req.mode = IIC_Synchronous_By_Polling;
req.completion_function = testComp;
iicOpen(&instance);
retval = iicDispatch(instance, &req);
if (retval) {
char errstr[80];
iicError2String(retval, errstr);
printf("Error %s \n", errstr);
}
else {
printf("Read: \n");
for (i = 0; i < req.byteCount; i++) {
printf("0x%02X ", xdata[i]);
if (7 == (i % 8))
printf("\n");
}
printf("\n");
}
iicClose(instance);
break;
}
case 'e': /* multibyte read of the EEPROM, using iicDispatch() */
{
iicRequest_t req;
Int instance, count, address, c;
UInt8 xdata[256];
count = sscanf(ins, "%c%c %d %x",
&ic, &ic2, &c,
&address);
req.byteCount = c;
if (count < 3)
req.byteCount = 16;
if (count < 4)
address = 0;
count = req.byteCount;
iicOpen(&instance);
/* write out the address */
xdata[0] = address;
req.direction = IIC_WRITE;
req.type = IIC_SIMPLE;
req.data = xdata;
req.mode = IIC_Synchronous_By_Polling;
req.completion_function = Null;
req.byteCount = 1;
req.address = EEPROM_ADDRESS;
retval = iicDispatch(instance, &req);
if (retval) {
char errstr[80];
iicError2String(retval, errstr);
printf("Error %s writing subaddress\n", errstr);
}
else
printf("%x bytes written to address %x, "
"%x\n", req.byteCount, req.address, address);
/* microsleep(20000); */
for (i = 0; i < 255; i++)
xdata[i] = 0;
/* read the data */
req.direction = IIC_READ;
req.type = IIC_SIMPLE;
req.data = xdata;
req.mode = IIC_Synchronous_By_Polling;
req.address = EEPROM_ADDRESS;
req.byteCount = count;
req.completion_function = Null;
retval = iicDispatch(instance, &req);
if (retval) {
char errstr[80];
iicError2String(retval, errstr);
printf("Error %s \n", errstr);
}
else {
printf("Read from EEPROM starting "
"at %d: \n", address);
for (i = 0; i < req.byteCount; i++) {
printf("0x%02X ", xdata[i]);
if (7 == (i % 8))
printf("\n");
}
printf("\n");
}
iicClose(instance);
break;
}
case 'w': /* multibyte write, using iicDispatch() */
{
iicRequest_t req;
Int instance, d, count, address, c;
UInt8 xdata[256];
/* mw address data count */
count = sscanf(ins, "%c%c %x %x %d",
&ic, &ic2, &address, &d, &c);
req.address = address;
req.byteCount = c;
if (count < 5) { /* no data supplied, just count */
for (i = 0; i < 255; i++)
xdata[i] = i;
req.byteCount = d;
}
else {
for (i = 0; i < 255; i++)
xdata[i] = d;
}
req.direction = IIC_WRITE;
req.type = IIC_SIMPLE;
req.data = xdata;
req.mode = IIC_Synchronous_By_Polling;
req.completion_function = Null;
iicOpen(&instance);
retval = iicDispatch(instance, &req);
iicClose(instance);
if (retval) {
char errstr[80];
iicError2String(retval, errstr);
printf("Error %s \n", errstr);
}
else
printf("%x bytes of %x written to address %x\n",
req.byteCount, d, req.address);
break;
}
}
break;
}
case 'p': /* report and change processor/ memory speed controls */
dumpProcSpeed();
break;
case 'e': /* Dump the EEPROM header in verbose mode. */
dumpEEPROM();
break;
case 'f': /* fill the eeprom with default values */
{
printf("Warning! This will overwrite your EEPROM "
"with default values.\n");
printf("This is OK, if you have a standare IREF with "
"2x clock.\n");
printf("If this is OK, press Y and return.\n");
gets(ins);
if ((*ins != 'y') && (*ins != 'Y'))
break;
printf("Filling EEPROM with standard values \n");
iicWriteReg(EEPROM_ADDRESS, 0, 0xc8);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 1, 0);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 2, 1);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 3, 0x11);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 4, 0x31);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 5, 0x0);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 6, 0x4c);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 7, 0x44);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 8, 0x0);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 9, 0x0);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 10, 0x0);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 11, 0x0);
microsleep(2000);
iicWriteReg(EEPROM_ADDRESS, 12, 0x0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -