Problem using I2C under MQX

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

Problem using I2C under MQX

Jump to solution
2,265 Views
MPotts
Contributor III

Hi,

 

I am trying to use I2C under MQX on an MCF52259 design. We have tested I2C under Processor Expert so know the hardware works. The I2C documentation and example are not very clear but I am doing the following:

 

To install/initialize I2C:

 

    MCF52XX_I2C_INIT_STRUCT i2c0_init = {
        0,
        BSP_I2C0_MODE,
        BSP_I2C0_ADDRESS,
        BSP_I2C0_BAUD_RATE,
        BSP_I2C0_INT_LEVEL,
        BSP_I2C0_INT_SUBLEVEL,
        BSP_I2C0_TX_BUFFER_SIZE,
        BSP_I2C0_RX_BUFFER_SIZE
    };


    rc = _mcf52xx_i2c_polled_install("i2c0:", &i2c0_init);

This returns 0xA00 which I do not know whether is correct.

 

To read an 8-byte buffer from I2C:

 

    if (NULL != (fd = fopen ("i2c0:", NULL))) goto fail;
    param = I2C_ADDR;
    if (I2C_OK != ioctl(fd, IO_IOCTL_I2C_SET_DESTINATION_ADDRESS, &param)) goto fail;
    param = sizeof(buff);
    if (I2C_OK != ioctl(fd, IO_IOCTL_I2C_SET_RX_REQUEST, &param)) goto fail;
    fread(&buff, 1, sizeof(buff), fd);
    if (I2C_OK != ioctl(fd, IO_IOCTL_FLUSH_OUTPUT, &buff)) goto fail;

     fclose(fd);

This executes without indicating failure but reads incorrect data. If I change the destination I2C address it still reads the same data, so something is wrong!

 

Mark

0 Kudos
1 Solution
809 Views
MPotts
Contributor III

Hi Nick,

 

You were correct that the I2C device address needed to be 7-bit (shifted 1-bit) then everything worked.

 

Thanks for your help.

Mark

View solution in original post

0 Kudos
7 Replies
809 Views
anthony_huereca
NXP Employee
NXP Employee

Mark,

  Try taking a look at the I2C example in some lab projects done for a seminar last year. Specifically you want to look at the Sensor Web lab for the I2C code reading data from the TWR-Sensor board.

 

  You can also look at the TWR-K60N512 Quick Start Demo for another example of using the MQX I2C driver to read accelerometer data. That project isn't set up for the MCF52259 that you're using, but the use of the I2C driver is the same. The pertinent code is in Accel_Task.c

 

That should get you on the right track!

0 Kudos
809 Views
MPotts
Contributor III

I mean

    if (NULL == (fd = fopen ("i2c0:", NULL))) goto fail;

 

0 Kudos
809 Views
OldNick
Contributor IV
Mark,
I noticed you didn't include the STOP command.  In the MQX example code it goes immediately after the flush.
   ioctl (fd, IO_IOCTL_I2C_STOP, NULL);
does that help?
0 Kudos
809 Views
MPotts
Contributor III

Hi Nick,

 

I added that but still get the same incorrect data. I am asking for 8 bytes but get back only 4 (0,0,0,1). The data appears after IO_IOCTL_FLUSH_OUTPUT. The same data appears even if I change the I2C destination address!

 

Mark

0 Kudos
809 Views
OldNick
Contributor IV

Mark,

lots of things that can go wrong.:robotsad:

 

Obvious things to check...

1. Is the

rc = _mcf52xx_i2c_polled_install("i2c0:", &i2c0_init);

in the bsp or your code, or both? In my MQX build, you configure i2c device x as present (user_config.h) and the BSP loads it for you. (If you remember to rebuild it!)

 

2. All the "set" IO requests have a corresponding "get",  you could put those in just to check the setup of the driver.

 

3. And finally, are you using 7-bit slave addressing?

porting old code, I had to define my slave devices as 

#define I2C_SLAVE_ADDRESS    (0x88 >> 1) //i2c slave address in Kinetis is 7-bit!!!

 

Which I only found out when I put a scope probe on the SDA and SCL lines.

 

Let me know?

 

0 Kudos
810 Views
MPotts
Contributor III

Hi Nick,

 

You were correct that the I2C device address needed to be 7-bit (shifted 1-bit) then everything worked.

 

Thanks for your help.

Mark

0 Kudos
809 Views
MPotts
Contributor III

Hi Nick,

 

You were right about _mcf52xx_i2c_polled_install("i2c0:", &i2c0_init) being called twice. I removed the redundant call but am still getting the same results. I will try the other checks you describe and see if I can resolve the problem.

 

Mark

0 Kudos