cancel
Showing results for 
Search instead for 
Did you mean: 

USB Superspeed Peripherals

HaHu_4486526
New Contributor

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;
}

 

pastedImage_0.png

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.

pastedImage_1.png

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.

0 Likes
1 Solution
JayakrishnaT_76
Moderator
Moderator

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

Best Regards,
Jayakrishna

View solution in original post

0 Likes
2 Replies
JayakrishnaT_76
Moderator
Moderator

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

Best Regards,
Jayakrishna
0 Likes
JayakrishnaT_76
Moderator
Moderator

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

Best Regards,
Jayakrishna

View solution in original post

0 Likes