how to use 24c02 by i2c inside of tms320 28035?
i composed a writting function and a reading function by i2c.
it is ok, if i write 1 byte data in it, then read it out .
now, i write 10 bytes data, then read it out putting them in the size 10 array g[10]. the first data in the g[0] is always wrong.
which good brother can solve it for me, please?
and i put my program here, please exchange Filename Extensions into .c (not .txt)
thanks
#include "DSP28x_Project.h"
struct text {
char id;
char howmany;//the number is pluged by 1(address)+how many data
char info[3];// max is only 4
};
void w(struct text *temp);
void r(struct text *temp);
struct text o={0x50},i={0x50};//={0x51,3,0,1,2},i;
char g[10];//={0};
int f[5];
interrupt void cpu_timer0_isr(void);
void main()
{
InitSysCtrl();
InitPieCtrl();
InitPieVectTable();
EALLOW;
PieVectTable.TINT0 = &cpu_timer0_isr;
InitCpuTimers();
ConfigCpuTimer(&CpuTimer0, 60, 5000000);
CpuTimer0Regs.TCR.all = 0x4001;
IER |= M_INT1;
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
EINT;
ERTM;
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0; // Enable pull-up for GPIO28 (SDAA)
GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0; // Enable pull-up for GPIO29 (SCLA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // Asynch input GPIO28 (SDAA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO29 = 3; // Asynch input GPIO29 (SCLA)
GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 2; // Configure GPIO28 for SDAA operation
GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 2; // Configure GPIO29 for SCLA operation
//-----------------------------------------------------
/*
// Initialize I2C
I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
// I2CCLK = SYSCLK/(I2CPSC+1)
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk
#endif
#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x00; // Enable SCD & ARDY interrupts
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
*/
// EALLOW;
// I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
// while(1)
{
o.id=0x50;// it is an account named by device
o.howmany=3;// put the number of byte including how many byte you wanna send and how many byte of address..
o.info[0]=0x0;//address inside of eeprom
o.info[1]=0x1;
o.info[2]=0x2;
w(&o);
// DELAY_US(10000);//required by eeprom.
o.howmany=3;// put the number of byte including how many byte you wanna send and how many byte of address..
o.info[0]=0x2;//address inside of eeprom
o.info[1]=0x3;
o.info[2]=0x4;
w(&o);
// DELAY_US(10000);//required by eeprom.
// o.id=0x50;
o.howmany=3;
o.info[0]=0x4;//address inside of eeprom
o.info[1]=0x5;
o.info[2]=0x6;
w(&o);
// DELAY_US(10000);//required by eeprom.
// o.id=0x50;// it is an account named by device
o.howmany=3;// put the number of byte including how many byte you wanna send and how many byte of address..
o.info[0]=0x6;//address inside of eeprom
o.info[1]=0x7;
o.info[2]=0x8;
w(&o);
DELAY_US(10000);//required by eeprom.
// o.id=0x50;
o.howmany=3;
o.info[0]=0x8;//address inside of eeprom
o.info[1]=0x9;
o.info[2]=0xa;
w(&o);
// DELAY_US(10000);//required by eeprom.
// o.id=0x50;
// o.howmany=3;
// o.info[0]=0xa;//address inside of eeprom
// o.info[1]=0x19;
// o.info[2]=0x1a;
// w(&o);
// DELAY_US(10000);//required by eeprom.
// i.id=0x50;
/// i.info[0]=0x0;//<----------===== if you exchange these two address, it is in right orer.
/// i.howmany=2;// |
/// r(&i);//
// g[0]=i.info[1];// it is | weild if you monitor this variable g.
// g[1]=i.info[2];// |
/// DELAY_US(3000);// |
// i.id=0x50;// |
// i.info[0]=0x1;//<---|
// i.howmany=2;
/// r(&i);
/// g[0]=i.info[1];
/// g[1]=i.info[2];
/// DELAY_US(3000);//
/// f[0]=((i.info[2]<<8) + i.info[1]);
// i.id=0x50;
i.info[0]=0x0;//<----------===== if you exchange these two address, it is in right orer.
i.howmany=2;// |
r(&i);// |
// g[2]=i.info[1];// it is | weild if you monitor this variable g.
// g[1]=i.info[2];// |
// DELAY_US(3000);// |
// i.id=0x50;// |
// i.info[0]=0x3;//<---|
// i.howmany=2;
r(&i);
g[0]=i.info[1];
g[1]=i.info[2];
// DELAY_US(10000);//
f[0]=((i.info[2]<<8) + i.info[1]);
// i.id=0x50;
i.info[0]=0x2;//<----------===== if you exchange these two address, it is in right orer.
i.howmany=2;// |
r(&i);// |
// g[4]=i.info[1];// it is | weild if you monitor this variable g.
// g[5]=i.info[2];// |
// DELAY_US(3000);// |
// i.id=0x50;// |
// i.info[0]=0x5;//<---|
// i.howmany=2;
r(&i);
g[2]=i.info[1];
g[3]=i.info[2];
DELAY_US(10000);//
f[1]=((i.info[2]<<8) + i.info[1]);
// i.id=0x50;
i.info[0]=0x4;//<----------===== if you exchange these two address, it is in right orer.
i.howmany=2;// |
r(&i);// |
// g[6]=i.info[1];// it is | weild if you monitor this variable g.
// g[7]=i.info[2];// |
// DELAY_US(3000);// |
// i.id=0x50;// |
// i.info[0]=0x7;//<---|
// i.howmany=2;
r(&i);
g[4]=i.info[1];
g[5]=i.info[2];
// DELAY_US(3000);//
f[2]=((i.info[2]<<8) + i.info[1]);
// i.id=0x50;
i.info[0]=0x6;//<----------===== if you exchange these two address, it is in right orer.
i.howmany=2;// |
r(&i);// |
// g[6]=i.info[1];// it is | weild if you monitor this variable g.
// g[7]=i.info[2];// |
// DELAY_US(3000);// |
// i.id=0x50;// |
// i.info[0]=0x7;//<---|
// i.howmany=2;
r(&i);
g[6]=i.info[1];
g[7]=i.info[2];
DELAY_US(10000);//
f[3]=((i.info[2]<<8) + i.info[1]);
i.id=0x50;
i.info[0]=0x8;//<----------===== if you exchange these two address, it is in right orer.
i.howmany=2;// |
r(&i);//
// DELAY_US(10000);
// g[0]=i.info[1];
// g[1]=i.info[2];
// i.id=0x50;
// i.info[0]=0xee;//<----------===== if you exchange these two address, it is in right orer.
// i.howmany=2;//
r(&i);
g[8]=i.info[1];// it is | weild if you monitor this variable g.
g[9]=i.info[2];// |
// DELAY_US(10000);// |
f[4]=((i.info[2]<<8) + i.info[1]);
/// i.id=0x50;// |
/// i.info[0]=0xcc;//<---|
/// i.howmany=2;
/// r(&i);
/// g[2]=i.info[1];
/// g[3]=i.info[2];
/// DELAY_US(10000);
// i.id=0x50;// |
// i.info[0]=0x4;//<---|
// i.howmany=2;
// r(&i);
// DELAY_US(10000);
// g[2]=i.info[2];
// g[3]=i.info[2];
}
}
void w(struct text *temp)
{
// usage: o.id=0x50; <--- at first, give account of the device.
// o.howmany=3; <--- 1+how many byte you wanna put into memory. the max is two.
// o.info[0]=0x28; <--- put the address inside of memory in the o.info[0]
// o.info[1]=0x9; <--- put the data you wanna put into the memory in o.info[0] and o.info[1]
// o.info[2]=0xa;
char s;
if ((I2caRegs.I2CMDR.bit.STP == 1) || (I2caRegs.I2CSTR.bit.BB == 1))
{
return ;
}
I2caRegs.I2CSAR = temp->id; // Setup slave address
I2caRegs.I2CCNT = temp->howmany+1; // plus 1 because of id.
// for (i=0; i<msg->NumOfBytes-2; i++)
for (s=0; s < temp->howmany+1; s++)
{
I2caRegs.I2CDXR = *(temp->info+s);
}
// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20;
DELAY_US(3000);
}
void r(struct text *temp)
{
// usage: i.id=0x50; <--- at first, give account of the device.
// i.info[0]=0x28; <--- put the address inside of memory into info[0] where you wanna start fetching
// i.howmany=2; <--- how many data byte you wanna fatch. the max munber is two.
// r(&i); <--- the two byte data will be put in the i.info[1] and i.info[2]. and the i.info[0] will be put address that you started fetching.
char s;
if ((I2caRegs.I2CMDR.bit.STP == 1) || (I2caRegs.I2CSTR.bit.BB == 1))
{
return ;
}
I2caRegs.I2CSAR = temp->id;
I2caRegs.I2CDXR = temp->info[0];//info[0] is the address inside of eeprom.
I2caRegs.I2CCNT = 1;
I2caRegs.I2CMDR.all = 0x2620; // Send data to setup EEPROM address
while(I2caRegs.I2CSTR.bit.ARDY !=1);
I2caRegs.I2CSTR.bit.ARDY = 1;
I2caRegs.I2CCNT = temp->howmany; // Setup how many bytes to expect
I2caRegs.I2CMDR.all = 0x2C20; // Send restart as master receiver
for(s=0; s < temp->howmany; s++)
{
temp->info[s+1] = I2caRegs.I2CDRR;
}
while(I2caRegs.I2CSTR.bit.SCD !=1);
I2caRegs.I2CSTR.bit.SCD = 1;
DELAY_US(3000);
}
interrupt void cpu_timer0_isr(void)
{
// o.id=0x51;// it is an account named by device
// o.howmany=3;// put the number of byte including how many byte you wanna send and how many byte of address..
// o.info[0]+=1;//address inside of eeprom
// o.info[1]+=1;
// o.info[2]+=1;
//w(&o);
// Acknowledge this interrupt to receive more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}