Help using I2C with PE

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

Help using I2C with PE

1,131 Views
alexismaslyczyk
Contributor II

Hi everyone,

I'm trying to use I2C with my IMU MPU6050.

Im' using my MKV58F1M0VLQ22 with KDS 3.1.0 with Processor Expert.

I added my I2C component and configure a master with my SDA SCL pins.

Firstly I would like to receive data from my IMU.

I have to start the I2C first so I think I have to use the function:

i2c_status_t I2C_DRV_MasterInit(uint32_t instance, i2c_master_state_t * master);

But I'm lost, what is the instance and what is The pointer to the I2C master driver state structure.?

 

To get the accelero the register is 0x3B

The adress of my IMU is 0x68

and the size of data is 6 octets

 

To do that I should use

i2c_status_t I2C_DRV_MasterReceiveDataBlocking(uint32_t instance,                                                const i2c_device_t * device,                                                const uint8_t * cmdBuff,                                                uint32_t cmdSize,                                                uint8_t * rxBuff,                                                uint32_t rxSize,                                                uint32_t timeout_ms);

 

but it's the same, what is the instance and the pointer?

cmdBuff is the register 0x3B? so size is 2 ?

rx buff is the adresse of my variable okay and rxSize is 6 I'm right?

And the tileout is a a time defined by me, what is it in general?

 

Thank you  very much

Labels (1)
Tags (3)
0 Kudos
4 Replies

706 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Regarding your question about the api function parameters declaration, pls refer to IIC driver description in chaper 28.7 I2C HAL driver in the Kinetis_SDK v1.3 API Reference Manual.pdf. I copy it here.

We have also example for the master mode of IIC. refer to the example in SDK:

D:\Freescale\KSDK_1.3.0\examples\frdmk64f\driver_examples\i2c\i2c_blocking\master\kds

Foe the parameter of api function:

  1. i2c_status_t I2C_DRV_MasterReceiveDataBlocking(uint32_t instance, 
  2. const i2c_device_t * device, 
  3. const uint8_t * cmdBuff, 
  4.                                                uint32_t cmdSize, 
  5.                                                uint8_t * rxBuff, 
  6.                                                uint32_t rxSize, 
  7.                                                uint32_t timeout_ms); 

The instance is the index of IIC module, for example, the processor has 3 IIC module: IIC0/IIC1/IIC2, if you use IIC0 module, the instance is 0, if you use IIC1 module, the instance is 1.

The device is a structure which is declared for example as:

i2c_device_t device =

    {

      .address = 0x68U, //slave IIC device address

      .baudRate_kbps = 400   // 400 Kbps

    };

Based on your parameter:

To get the accelero the register is 0x3B

The adress of my IMU is 0x68

and the size of data is 6 octets

I think you can declare the parameters:

#define IIC_INSTANCE 0 //if you use IIC0 module

uint8_t cmd=0x3b; //register address

uint8_t count=6;

uint8_t buffer[10];

I2C_DRV_MasterReceiveDataBlocking(IIC_INSTANCE, &device,&cmd,1,&buffer[0],count,1000);

As you know, when you read data from IIC slave, this is the sequence:

send:slave address|write,send: register address,send:repeat start,send:slave address|read,receive data....

pastedImage_0.png

Hope it can help you.

BR

XiangJun Rong

0 Kudos

706 Views
alexismaslyczyk
Contributor II

Okay thank you for your help,

just a quick question, uint8_t buffer[10]; why 10 if my size data is 6?

And what do you mean by:

As you know, when you read data from IIC slave, this is the sequence:

send:slave address|write,send: register address,send:repeat start,send:slave address|read,receive data....

My data will be stored in my buffer directly right?

Thank you very much

0 Kudos

706 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Alex

Q1:just a quick question, uint8_t buffer[10]; why 10 if my size data is 6?

>>>>>>It is okay to define  uint8_t buffer[6]; , I just wrote it randomly.

Q2:And what do you mean by:

As you know, when you read data from IIC slave, this is the sequence:

send:slave address|write,send: register address,send:repeat start,send:slave address|read,receive data....

>>>>>I mean the master IIC module data output sequence after you call the I2C_DRV_MasterReceiveDataBlocking(IIC_INSTANCE, &device,&cmd,1,&buffer[0],count,1000) function.  After the above function is called, the IIC module firstly outputs SLAVE DEVICE ADDRESS(0x68) and WRITE bit, then the IIC module receives ACK bit. Then the IIC module outputs register address 0x3B and receive an ACK bit. Then the IIC module generates repeated START signal and outputs SLAVE DEVICE ADDRESS(0x68) and READ bit, then the IIC module receives one BYTE and generates an ACK bit, receives one BYTE and generates an ACK bit,....

Of course, you data will be saved in the buffer[] array after you calling the function.

BR

XiangJun Rong

0 Kudos

706 Views
alexismaslyczyk
Contributor II

Thank you xiang, bit I'm still not able to receiv something from my IMU.

Firstly I defined my I2C:

pastedImage_1.png

with my SDL/SDA pins:

pastedImage_0.png

In my code i defined:

#define I2C_INSTANCE 1    //I2C1 means thats the instance is 1 right?

i2c_device_t device =

     {

       .address = 0x68U, //slave IIC device address

       .baudRate_kbps = 400   // 400 Kbps

     };

i2c_master_state_t master;

uint8_t cmd=0x75; //register address

uint8_t buffer[14];

PE_low_level_init();

hardware_init();

OSA_Init();

I2C_DRV_MasterInit(I2C_INSTANCE,&master); // init l'I2C

OSA_TimeDelay(100); //wait 10ms

/****** NO I WANT READ THE REGISTER 0x75 which should send 0x71:*****/

I2C_DRV_MasterReceiveDataBlocking(I2C_INSTANCE, &device,&cmd,1,&buffer[0],1,1000);

if (buffer[0]== 0x71)

    {

debug_printf("MPU is online...\n\r");

}else{

debug_printf("Problem...\n\r");

}

But buffer[0] is never equal to 0x71

Do you have any idea?

I don't underdtand if I'm supposed to :

I2C_DRV_MasterInit(I2C_INSTANCE,&master); // init l'I2C

?

Thank you very much

0 Kudos