- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to understand more about I2C and the use of it in PSoC.
At this moment i am working with a MPU-6050. There is a library for this component and it works all fine.
But..... its to easy for me to just use a library, copy/past it and ready.
At the moment i have code that works with the I2C_MasterReadByte() function.
Is there a way to use the I2C_MasterReadBuf() function?
And is there a way to shorten this piece of code?
Best Regards,
Frank
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You need to use I2C_MasterWriteBuf() at first
The high-level APIs must be used in this way:
Writing to slave Count bytes
I2C_MasterWriteBuf(SlaveAddress,DataPtr,Count,I2C_MODE_COMPLETE_XFER);
Reading from Slave sending register/command byte first:
I2C_MasterWriteBuf(SlaveAddress,&RegAddress,1,I2C_MODE_NO_STOP);
I2C_MasterReadBuf(SlaveAddress,DataPtr,Count,I2C_MODE_REPEAT_START);
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try to remove the line I2C_MasterClearStatus();
Ensure that global interrupts are enabled
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My I2C interface works (usually). 😉
Can you upload your complete project so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Bob,
So after reading some stuff on the internet i found out that the I2C uses its own interrupt. I use some interrupts myself in the application. It looks like the I2C component does'nt like that at all. Yep and after checking the interrupts priority i found out they where all on the same(1). I changed that in the right one for the I2C_I2C_IRQ to priority (0). And the other onces in order from priority, its logical.
Despite it did'nt work direct. I also had to add I2C_MasterClearWriteBuf() and I2C_MasterClearReadBuf(). After that it worked all perfectly.
So to use the I2C_MasterWriteBuf() and I2C_MasterReadBuf() you have to do it in following order.
Look after your priority from the interrupt when you use other interrupts in your application.
And than code like:
wBuf[1];
rBuf[14]; //Number of bytes you want to read
wBuf[0] = cmdAdress;
I2C_MasterClearWriteBuf();
I2C_MasterWriteBuf(devAdress,wBuf,1,I2C_MODE_NO_STOP);
while (0u == (I2C_MasterStatus() & I2C_MSTAT_WR_CMPLT)) //Waits until master completes write transfer
if (0u == (I2C_MSTAT_ERR_XFER & I2C_MasterStatus())) //Check transfer status
{
if(I2C_MasterGetWriteBufSize() == 1) //Check if all bytes was written
{
//What ever you want to do after writing the byte
}
}
else
{
//What ever you want to do when something wrong
}
I2C_MasterClearReadBuf();
I2C_MasterReadBuf(devAdress,rBuf,14,I2C_MODE_REPEAT_START);
while (0u == (I2C_MasterStatus() & I2C_MSTAT_RD_CMPLT)) //Waits until master complete read transfer
if (0u == (I2C_MSTAT_ERR_XFER & I2C_MasterStatus())) //Displays transfer status
{
if(I2C_MasterGetReadBufSize() == 14) // Check if all bytes was written
{
//What ever you want to do after reading all the bytes
}
}
else
{
//What ever you want to do when something wrong
}
Thanks Bob for the hints.
Happy coding,
Frank