Trying to figure out the I2C example for the I2C_LDD component

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

Trying to figure out the I2C example for the I2C_LDD component

1,668 Views
arbj
Contributor I

Hi,

I am trying to figure out this line in the example given below from the I2C_LDD help page. I am refering to the Master mode, without interrupt mode example. (I have extracted only the relevant portion of the code)

the variable is initialised as follows

uint8_t OutData[4] = {0x00U, 0x01U, 0x02U, 0x03U}; /* Initialization of output data buffer */

  Error = I2C2_MasterSendBlock(MyI2CPtr, OutData, 4U, LDD_I2C_SEND_STOP); /* Send OutData (4 bytes) on the I2C bus and generates a stop condition to end transmission */

while (!DataTransmittedFlg) { /* Wait until OutData are transmitted */

  I2C2_Main(MyI2CPtr);

  }

The contents of the Events.c file is as follows

extern volatile bool DataTransmittedFlg;

void I2C3_OnSlaveBlockSent(LDD_TUserData *UserDataPtr)

{

  DataTransmittedFlg = TRUE; /* Set DataTransmittedFlg flag */

}

extern volatile bool DataReceivedFlg;

void I2C3_OnSlaveBlockReceived(LDD_TUserData *UserDataPtr)

{

  DataReceivedFlg = TRUE; /* Set DataReceivedFlg flag */

}

I have the following queries...

1.The text bold is what I am unable to understand, is 0x00U, 0x01U... the register addresses or data for the I2C slave device ??

2. what is 4U meant for ??

3. The while loop never quits, as the BlockSentEvent is not fired and DataTransmittedFlg remains FALSE.

I am trying to connect to a MCP23016 (address=1) with a MKL26Z64VFT4 mcu (via I2C1). For this I would like to send the following parameters.

Mode=Master

Slave Address=1

Register Address=0

GPIO1=0xFE

GPIO2=0x00

So how can do this using the I2C_LDD device driver ?? In my test program I am doing this (again only relevant portions are shown, if required I could post more info)

OutData[0]=0;          //GP0 address

OutData[1]=0xFF;     //GP0 set as input

OutData[2]=0;          //GP2 set as output

Error = I2C2_MasterSendBlock(MyI2CPtr, OutData,3U, LDD_I2C_SEND_STOP);

while (!DataTransmittedFlg) { /* Wait until OutData are transmitted */

  I2C2_Main(MyI2CPtr);

  }

With a scope I can see the SDA pin pulsing, but no pulsing on SCL line is observed. Also the while loop is never quit as the BlockSentEvent does not get fired.

thanks

a

0 Kudos
7 Replies

1,029 Views
BlackNight
NXP Employee
NXP Employee

To answer that particular question:

>>2. what is 4U meant for ??

'U' stands for 'unsigned'. If you would write just '4', it would be a (signed) int according to the the C standard. Writing it as '4U' makes it an unsigned int. Same as you would write '(unsigned int)4'.

I hope this helps,

Erich

PS: The OnSent/OnReceived events get called from the interrupts. If you would like to have a look at a tutorial using the I2C_LDD in interrupt mode: http://mcuoneclipse.com/2012/09/21/tutorial-accelerating-the-kl25z-freedom-board/

0 Kudos

1,029 Views
arbj
Contributor I

Hi,

Thanks for the reply. I have read most of your tutorials on I2C.

I am using a flag on the event handler to indicate when the block transmission has ended. As per the example code in the help page.

However in my case the event does not get triggered for some reason. Is this because the master device (MKL26Z64VFT4) is expecting an ACK from the slave? The MCP 23016 does not return an ACK after receiving the command and data bytes. So in this situation how to ensure block has been sent ?

thanks

a

0 Kudos

1,029 Views
marek_neuzil
NXP Employee
NXP Employee

Hi Arun,

I have checked the MCP23016 datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/20090C.pdf) and the device shall provide ACK after each data byte, see the details in the datasheet. Use an oscilloscope to verify signals on I2C pins. There can also a problem with communication speed (select lower I2C clock frequency).

Best Regards,

Marek Neuzil

0 Kudos

1,029 Views
DavidS
NXP Employee
NXP Employee

Hi Arun,

A colleague and I recently used the same tool set (KDS_2.0.0/KSDK_1.2.0) to get access to the Freedom FRDM-K22F accelerometer/magnitometer FXOS8700CQ device via I2C using PE.

Attached is that project for reference.

The I2C slave address is buried down in the function call and not passed via high level API.

Hope it helps.

Regards,

David

0 Kudos

1,029 Views
arbj
Contributor I

Hi,

Thanks for the help and the attachment, I can now understand some parts of the example. But it is still not clear how to set the R/W bit for the MCP23016, this bit is set or cleared immediately after the slave address is sent.

Any help on that ?? Meanwhile I am looking in the attachment where the slave address is defined..

thanks

a

0 Kudos

1,029 Views
DavidS
NXP Employee
NXP Employee

Hi Arun,

That bit is set at lower level of driver based upon the function call used.

Generically:

W/R bit clear for this function call:  I2C2_MasterSendBlock()

W/R bit set for this function call: I2C2_MasterReceiveBlock()

Note that the lower level driver is automatically handling the I2C Device Address you entered into PE.

Regards,

David

0 Kudos

1,029 Views
arbj
Contributor I

Hi David,

Thanks for the help. I got that part now..

But I cant figure out why the MasterBlockSent event is not getting triggered. I have tried the example given in the component usage help file, but the event does not get triggered, I tried both with interrupts and the polling method but the event is not triggered in either.

Any tips on this ??

thanks

a

0 Kudos