PSoC6 Master Write I2C Use High-Level Functions

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
PascalS
Level 4
Level 4
25 replies posted 10 replies posted 10 questions asked

Hello,

I'm using a CYBLE-416045-02 Controller and want to read out some registers from an external I2C sensor.

I followed Infineon's instructions how to use the High-Level I2C Functions: 

https://infineon.github.io/psoc6pdl/pdl_api_reference_manual/html/group__group__scb__i2c.html

 

My question is, how and where to configure the register address I want to read out?

 

 

int8_t I2C_readData(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
    int8_t result = 0;
    uint8_t readBuffer [5UL];
    cy_stc_scb_i2c_master_xfer_config_t transfer;

    transfer.slaveAddress = dev_addr;
    transfer.buffer       = data;
    transfer.bufferSize   = len;
    transfer.xferPending  = false; /* Generate Stop condition the end of transaction */
    /* Initiate read transaction.
    * The ReStart condition is generated to begin this transaction because
    * previous transaction was completed without Stop. */
    (void) Cy_SCB_I2C_MasterRead(I2C_0_HW, &transfer, &I2C_0_context);
    /* Wait for transaction completion */
    while (0UL != (CY_SCB_I2C_MASTER_BUSY & Cy_SCB_I2C_MasterGetStatus(I2C_0_HW, &I2C_0_context)))
    {
    }
    return result;
}

 

 

So the device address (dev_addr) seems to be the transfer.slaveAddress but there is no element for the register address.

 

Best Regards!

0 Likes
1 Solution
AlenAn14
Moderator
Moderator
Moderator
500 replies posted 100 solutions authored 250 replies posted

Hi @PascalS ,

In order to perform a read operation , you must first write to the slave the register address to read from without initiating a stop condition (transfer.xferPending = true) at the end of the write operation followed by a read operation with a stop condition (transfer.xferPending = false) after the read is done.
The following is a 1 byte read operation in I2C:

 

void i2c_readByte(uint8_t address,uint8_t registerAddress,uint8_t* readData)
{
   cy_stc_scb_i2c_master_xfer_config_t transfer;
   uint8_t readByte = 0;
   uint8_t writeBuffer[1UL] = {registerAddress};

   /* Configure write transaction */
   transfer.slaveAddress = address;
   transfer.buffer = writeBuffer;
   transfer.bufferSize = 1; // Read only a single byte
   transfer.xferPending = true; /* Do not generate Stop condition at the end of transaction */

   /* Initiate write transaction.
   * The Start condition is generated to begin this transaction.
   */
   Cy_SCB_I2C_MasterWrite(SCB3, &transfer, &cy_stc_scb_i2c_context);

   /* Wait for transaction completion */
   while (0UL != (CY_SCB_I2C_MASTER_BUSY & Cy_SCB_I2C_MasterGetStatus(SCB3, &cy_stc_scb_i2c_context)))
   {
   }
   
   /* Configure read transaction */
   transfer.buffer = readData;
   transfer.bufferSize = 1;
   transfer.xferPending = false; /* Generate Stop condition the end of transaction */

   /* Initiate read transaction.
   * The ReStart condition is generated to begin this transaction because
   * previous transaction was completed without Stop.
   */
   Cy_SCB_I2C_MasterRead(SCB3, &transfer, &cy_stc_scb_i2c_context);
   
   /* Wait for transaction completion */
   while (0UL != (CY_SCB_I2C_MASTER_BUSY & Cy_SCB_I2C_MasterGetStatus(SCB3, &cy_stc_scb_i2c_context)))
   {
   }
}

 

 

Hope this helps.

Regards
Alen

View solution in original post

2 Replies
AlenAn14
Moderator
Moderator
Moderator
500 replies posted 100 solutions authored 250 replies posted

Hi @PascalS ,

In order to perform a read operation , you must first write to the slave the register address to read from without initiating a stop condition (transfer.xferPending = true) at the end of the write operation followed by a read operation with a stop condition (transfer.xferPending = false) after the read is done.
The following is a 1 byte read operation in I2C:

 

void i2c_readByte(uint8_t address,uint8_t registerAddress,uint8_t* readData)
{
   cy_stc_scb_i2c_master_xfer_config_t transfer;
   uint8_t readByte = 0;
   uint8_t writeBuffer[1UL] = {registerAddress};

   /* Configure write transaction */
   transfer.slaveAddress = address;
   transfer.buffer = writeBuffer;
   transfer.bufferSize = 1; // Read only a single byte
   transfer.xferPending = true; /* Do not generate Stop condition at the end of transaction */

   /* Initiate write transaction.
   * The Start condition is generated to begin this transaction.
   */
   Cy_SCB_I2C_MasterWrite(SCB3, &transfer, &cy_stc_scb_i2c_context);

   /* Wait for transaction completion */
   while (0UL != (CY_SCB_I2C_MASTER_BUSY & Cy_SCB_I2C_MasterGetStatus(SCB3, &cy_stc_scb_i2c_context)))
   {
   }
   
   /* Configure read transaction */
   transfer.buffer = readData;
   transfer.bufferSize = 1;
   transfer.xferPending = false; /* Generate Stop condition the end of transaction */

   /* Initiate read transaction.
   * The ReStart condition is generated to begin this transaction because
   * previous transaction was completed without Stop.
   */
   Cy_SCB_I2C_MasterRead(SCB3, &transfer, &cy_stc_scb_i2c_context);
   
   /* Wait for transaction completion */
   while (0UL != (CY_SCB_I2C_MASTER_BUSY & Cy_SCB_I2C_MasterGetStatus(SCB3, &cy_stc_scb_i2c_context)))
   {
   }
}

 

 

Hope this helps.

Regards
Alen

AlenAn14
Moderator
Moderator
Moderator
500 replies posted 100 solutions authored 250 replies posted

Hi @PascalS c,

Glad your issue is resolved.
Please feel free to post any queries or issues you may face on our products in the community and we will be happy to help.

Warm Regards
Alen

0 Likes