Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 5 > I2C in PSoC creator 2.0

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



I2C in PSoC creator 2.0
Moderator:
ANCY

Post Reply
Follow this topic



I2C in PSoC creator 2.0

mike.hersh posted on 07 Apr 2012 12:13 AM PST
Top Contributor
21 Forum Posts

 Hi,

I'm trying to communicate with a sensor using I2C interface and I use the I2C Master (UDB) component in the PSoC creator 2.0 to do so.The reset is connected to '0' and the SDA, SCL connected to the sensor (slave) with pull-ups resistors. I used many times before I2C interface so I very familiar with the protocol. the problem is that this time I get a complete module with functions that from the datasheet I just can't understand how to make a simple reading from the slave's registers:

I need to access to the sensor's address and then access its registers using another address and then read the data and save it.

The protocol of the sensor interface is simple and standard:

Master|S|AD+W|        |RA|        |S|AD+R|        |          |NACK|P|

Slave  |  |            |ACK|      |ACK|  |           |ACK|DATA|           |  |

If someone can give me a simple code example for how to use the component's functions to make this all work I'll be very grateful...

Thanks,

Michael.




Re: I2C in PSoC creator 2.0

Bob Marlowe posted on 07 Apr 2012 01:28 AM PST
Top Contributor
1768 Forum Posts

I do not have a code-example at hand, but from your protocoll I can see  (|AD-W| , |AD+R|) that you might try to modify the sllave's address to indicate a read or write operation. This is handled within the master-software, you just specify the slave's base address where you want to read from or write to.

You'll easily find an example when you right-click un your I2C-module in the schematic or in the component catalog and then click on "Find example Project" which will bring up a dialog where you select your device (PSoC 5). The example given is for an EZI2c-slave, but that will not matter for the master and for the addressing.

Hope that helps

Bob



Re: I2C in PSoC creator 2.0

mike.hersh posted on 07 Apr 2012 04:27 AM PST
Top Contributor
21 Forum Posts

Thanks Bob for the reply,

I looked at that example before but I don't see how to address the registers after I address the device. Unlike the EzI2C In my case I use a device with registers... I'ts possible to invent the wheel all over again by writing the codes from scratch but I think that must be an easy way to do that using the I2C component functions which I just don't see/understand them from the datasheet or the EzI2C example...



Re: I2C in PSoC creator 2.0

H L posted on 07 Apr 2012 05:14 AM PST
Top Contributor
679 Forum Posts

 It may be better if you can give us the part number or the specification of the slave devices? 



Re: I2C in PSoC creator 2.0

mike.hersh posted on 07 Apr 2012 05:41 AM PST
Top Contributor
21 Forum Posts

The slave's datasheet: http://invensense.com/mems/gyro/documents/PS-MPU-6000A.pdf see pages 36 to 39

In my case the slave's address is: 0x68

The slave's register map: invensense.com/mems/gyro/documents/RM-MPU-6000A.pdf

I need code example for reading/writing one of the data registers.

Thanks,

Michael H.



Re: I2C in PSoC creator 2.0

Bob Marlowe posted on 07 Apr 2012 06:59 AM PST
Top Contributor
1768 Forum Posts

 

I do not have an example-code right at hand, but it is a two-liner.

Have a look at the datasheet of the I2C Master/Multimaster/Slave module, page 19.

Use the

I2C_MasterReadBuf(SlaveAdr,&DataBuffer,2, I2C_MODE_COMPLETE_XFER);

That's all.

 Bob

 

I2C_MasterWriteBuf(SlaveAdr,&RegisterNumber,1, I2C_MODE_NO_STOP);



Re: I2C in PSoC creator 2.0

Bob Marlowe posted on 07 Apr 2012 07:01 AM PST
Top Contributor
1768 Forum Posts

The lines of the above code have gotten mangled, I suppose you can order them correctly again (Write first, then read).

Bob



Re: I2C in PSoC creator 2.0

mike.hersh posted on 07 Apr 2012 11:38 AM PST
Top Contributor
21 Forum Posts

Thanks Bob for the support,

After alot of trying (and failing) the following code works:

int32 i;

uint8 wrData[1], rdData[2]; // Put in the wrData[0] array the register address

volatile uint8 status;

 

I2C_Start();

status = I2C_MasterSendStart(MPU6050_ADD, I2C_WRITE_XFER_MODE);

if(status == I2C_MSTR_NO_ERROR) /* Check if transfer completed without errors */

{

/* Send array of 1 bytes */

for(i=0; i<1; i++)

{

status = I2C_MasterWriteByte(wrData[i]);

if(status != I2C_MSTR_NO_ERROR) break;

}

}

I2C_MasterSendStop(); /* Send Stop */

status = I2C_MasterSendStart(MPU6050_ADD, I2C_READ_XFER_MODE);

if(status == I2C_MSTR_NO_ERROR) /* Check if transfer completed without errors */

{

/* Read array of 2 bytes */

for(i=0; i<2; i++)

{

if(i < 1) rdData[i] = I2C_MasterReadByte(I2C_ACK_DATA);

else rdData[i] = I2C_MasterReadByte(I2C_NAK_DATA);

}

}

I2C_MasterSendStop(); /* Send Stop */

I2C_Stop();



Re: I2C in PSoC creator 2.0

Bob Marlowe posted on 07 Apr 2012 12:40 PM PST
Top Contributor
1768 Forum Posts

Well, looks a bit more than my twoliner... But fine that you've got it and thank you to share your code with us!

Bob






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20