- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi:
i want to read and write a 8bits byteaddr I2C device by USB3014, i can write the right slave addr and sub-addr, but i find wirte data is fail.
this is my code:
i want to wirte the data 0x9F to a slave device(slave addr is 0x42 and sbu addr is 0x03).
CyFxUsbI2cTransfer (0x03,0x42,0x01,0x9F,0x00);
CyFxUsbI2cTransfer (
uint8_t byteAddress,
uint8_t devAddr,
uint16_t byteCount,
uint8_t *buffer,
CyBool_t isRead)
{
CyU3PI2cPreamble_t preamble;
uint16_t pageCount = (byteCount / glI2cPageSize);
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
uint16_t resCount = glI2cPageSize;
if (byteCount == 0)
{
return CY_U3P_SUCCESS;
}
if ((byteCount % glI2cPageSize) != 0)
{
pageCount ++;
resCount = byteCount % glI2cPageSize;
}
while (pageCount != 0)
{
if (isRead)
{
/* Update the preamble information. */
preamble.length = 3;
preamble.buffer[0] = devAddr;
preamble.buffer[1] = byteAddress;
preamble.buffer[2] = (devAddr | 0x01);
preamble.ctrlMask = 0x0002;
status = CyU3PI2cReceiveBytes (&preamble, buffer, 0x01, 0);
CyU3PDebugPrint (2, "I2C_read_data 0x%x \r\n", buffer);
CyU3PDebugPrint (2, "I2C_read 0x%x \r\n", status);
if (status != CY_U3P_SUCCESS)
{
return status;
}
}
else /* Write */
{
/* Update the preamble information. */
preamble.length = 2;
preamble.buffer[0] = devAddr;
preamble.buffer[1] = byteAddress;
preamble.ctrlMask = 0x0000;
//status = CyU3PI2cTransmitBytes (&preamble, buffer, (pageCount == 1) ? resCount : glI2cPageSize, 0);
status = CyU3PI2cTransmitBytes (&preamble, buffer, 0x01, 0);
CyU3PDebugPrint (2, "I2C_transmit_data 0x%x \r\n", buffer);
CyU3PDebugPrint (2, "I2C_transmit 0x%x \r\n", status);
if (status != CY_U3P_SUCCESS)
{
return status;
}
/* Wait for the write to complete. */
preamble.length = 1;
status = CyU3PI2cWaitForAck(&preamble, 200);
CyU3PDebugPrint (2, "I2C_ACK 0x%x \r\n", status);
if (status != CY_U3P_SUCCESS)
{
return status;
}
}
/* An additional delay seems to be required after receiving an ACK. */
CyU3PThreadSleep (1);
/* Update the parameters */
//byteAddress += glI2cPageSize;
//buffer += glI2cPageSize;
pageCount --;
}
return CY_U3P_SUCCESS;
}
i can read from wave that the slave addr and sub addr are alright but the data is error.
the log from UART says the state of I2C write is OK.
can you tell me where is my code wrong or can you give a good example for read and write a 8bits byteaddr I2C device?
thanks a lot.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
In addition to my previous response, the 4th argument of the function CyFxUsbI2cTransfer needs to be a pointer. So, please try storing the data into a buffer and pass a pointer to that buffer as the fourth argument of the function CyFxUsbI2cTransfer. This can be done as shown below.
uint8_t dat = 0x9F;
uint8_t* pdat = &dat;
CyFxUsbI2cTransfer (0x03,0x42,0x01,pdat,0x00);
Please try this and check whether you are facing the same issue or not.
Best Regards,
Jayakrishna
Jayakrishna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
The code seems to be fine. Please try reading the data that was transmitted to the byte address 0x03 and let me know the data that you get. As you might be knowing, this can be done by calling the function CyFxUsbI2cTransfer with the isRead parameter set to CyTrue. Then a pointer *buffer will be returned which contains the data that is read. Please try this and let me know whether data received is correct or not.
Best Regards,
Jayakrishna
Jayakrishna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
In addition to my previous response, the 4th argument of the function CyFxUsbI2cTransfer needs to be a pointer. So, please try storing the data into a buffer and pass a pointer to that buffer as the fourth argument of the function CyFxUsbI2cTransfer. This can be done as shown below.
uint8_t dat = 0x9F;
uint8_t* pdat = &dat;
CyFxUsbI2cTransfer (0x03,0x42,0x01,pdat,0x00);
Please try this and check whether you are facing the same issue or not.
Best Regards,
Jayakrishna
Jayakrishna