I2C Help

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2C Help

6,011 Views
airswit
Contributor III
Hi everyone,

I am starting my first experience with I2C! I am trying to communicate with a MAXIM keypad controller, and am using the following function to send an array of 4 bytes:


void I2CsendString(uint8 *data, uint16 length, uint8 address, uint8 id)
{
uint16 i;
MCF_I2C_I2CR |= MCF_I2C_I2CR_MTX; //become master

MCF_I2C_I2CR |= MCF_I2C_I2CR_MSTA; //send start bit
MCF_I2C_I2DR = id; //send slave addr
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF )); //wait till xfer is done
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF; //clear flag
MCF_I2C_I2DR = address; //send register addr
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF )); //wait till xfer is done
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF; //clear flag

for(i = 0 ; i length ; i++)
{
MCF_I2C_I2DR = data[i]; //send data byte addr
while( !(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF )); //wait till xfer is done
MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF; //clear flag

/
}
return;
}


now, for some reason, when this function gets called, it never returns! when i run the debugger, i see the stack having the function that called this one, then a memory offset from IPSBAR, which is to a set of memory that is cleared! it seems to get through the first few tranfers, at least (slave address, device register address), i think! can anyone see any holes in my code? By the way, the device wants to see a transfer like this:

[start][slave addr][ACK][control byte][ACK][N data bytes][ack after each byte][stop]

I am running the bus at slower than 400kHz, which is the upper limit of the device, and am using the correct address, as per the datasheet.

I don't have interrupts enabled for this, or anything like that (though there are other interrupts running on the device).



...SPI is so much easier!!!!


Thanks in advance,
Trevor

--update--
i programmed the code to FLASH, and now it doesn't run off into space, but it doesn't look like the controller is getting initialized properly. can anyone see where an error might be?
If you could look at the datasheet, maybe, that would be great (this is a MAX7348 device). you don't have to, but any suggestions on my code would be awesome too!

/update

Message Edited by airswit on 05-13-200601:00 AM

Labels (1)
0 Kudos
Reply
4 Replies

710 Views
DrSeuss
Contributor I
Below are some sample calls to the attached driver. The delays in the code are to make scope debugging easier.
 
 
 
 /* Enable the I2C signals */
 MCF_GPIO_PASPAR |= ( MCF_GPIO_PASPAR_SDA_SDA
        | MCF_GPIO_PASPAR_SCL_SCL);
       
 I2Cinit();
    
 printf(" initialization start!\n\n");
 I2CsendByte(0x10,0x10,slave_addr);
/* Wait for a bit */
i=50000;
while(i--);
 I2CsendByte(0x08,0x11,slave_addr);
 printf(" initialization complete!\n\n");
 for (i=0;i<8;i++)
 {
    printf("data = 0x%02x\n",I2CreceiveByte(i,slave_addr));  
 }
0 Kudos
Reply

710 Views
TechCom
Contributor I

Now,I use M52233 Demo Board + 24LC256.
I use Coldfire Lite for web server.
Can use this code for read/write 24LC256.?
I try many time.
It printf to terminal

 

 initialization start!
 initialization complete!
data = 0xFF

data = 0xFF

data = 0xFF

data = 0xFF

data = 0xFF

data = 0xFF

data = 0xFF

data = 0xFF


0 Kudos
Reply

710 Views
mjbcswitzerland
Specialist V

Hi Trevor

I didn't check your code (since I haven't actually used the IIC controller on the Coldfire yet) but do have a suggestion as to what you could quickly check.

How have you defined the status registers (such as MCF_I2C_I2SR) which you are polling to see when each step has completed? Check that they are indeed defined as "volatile".

The CW will typically optimise the code (when volatile is not used) to read the location once and use the same value for subsequent 'polling'. It may then get stuck in a polling loop. Typically it is obvious that this is happening when a routine like that gets stuck when run at full speed but it walks through with the debugger. as the flags are set fast enough at each step.

Maybe it's as simple as that? Otherwise I hope you can solve it - IIC is not that complicated if you are only doing single master transferes and is more convenient that SPI for multiple slaves - so weather it out - it will be worth it.

Regards

Mark Butcher
www.mjbc.ch

 

0 Kudos
Reply

710 Views
DavidS
NXP Employee
NXP Employee

Hi Trevor,

Which ColdFire device?

I'm not an i2c expert (or even novice) but can point you to some example code for i2c that is on the Freescale web site along with appnotes:

https://www.freescale.com/webapp/Download?colCode=MPC860COD12&appType=license&location=null&srch=1 

http://www.freescale.com/webapp/sps/site/overview.jsp?code=8BITAPPNOTEIIC&srch=1

http://www.freescale.com/webapp/TransformXMLServlet?XmlId=FAQ-3256&XslId=SingleFaqDisplay.xsl&XslId2...

Regards, DavidS

0 Kudos
Reply