Quantcast
Channel: Infineon Forums
Viewing all articles
Browse latest Browse all 9892

I2C configuration code

$
0
0
Hello,

What I understand from the Infineon Library for I2C_MASTER_Transmit and I2C_MASTER_Received is I should provide the address of the slave and the function will add the last bit for write or read.
I am using the adafruit BMP280 and the SDO pin is connected to Vddio (so X is a 1), and the slave address is 0x77.
SO in write mode it is 0xEE and in read mode 0xEF.

I checked it with an Arduino, the sensor is working well.

I tried another program using the library functions, because according to this post ( https://www.infineonforums.com/threa...-loops-forever) , there is a bug in I2C_MASTER_Transmit().

The following code is working, I obtain 0x58 in RBUF.




Code:


#include <DAVE.h>                //Declarations from DAVE Code Generation (includes SFR declaration)

/**

 * @brief main() - Application entry point
 *
 * <b>Details of function</b><br>
 * This routine is the application entry point. It is invoked by the device startup code. It is responsible for
 * invoking the APP initialization dispatcher routine - DAVE_Init() and hosting the place-holder for user application
 * code.
 */


uint8_t data_received = 0;
int main(void)
{
  DAVE_STATUS_t status;

  status = DAVE_Init();          /* Initialization of DAVE APPs  */

  if(status == DAVE_STATUS_FAILURE)
  {
    /* Placeholder for error handler code. The while loop below can be replaced with an user error handler. */
    XMC_DEBUG("DAVE APPs initialization failed\n");

    while(1U)
    {

    }
  }

  //-------------------------------------------------------
 // reinitialize PSR register
  USIC1_CH1->PSR = 0;

  /* Placeholder for user application code. The while loop below can be replaced with user application code. */
  while(1U)
  {
        uint8_t SLAVE_ADDRESS = 0xEE;
        // 0x77 : BMP address
        // 0xEE : Write BMP address
        // 0xEF : Read BMP address
        I2C_MASTER_SendStart(&I2C_MASTER_0, SLAVE_ADDRESS, XMC_I2C_CH_CMD_WRITE);
        while(I2C_MASTER_GetFlagStatus(&I2C_MASTER_0, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U)
        {
        // wait for ACKS
        }
        I2C_MASTER_ClearFlag(&I2C_MASTER_0,XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED);

        // write to address 0xD0
        I2C_MASTER_TransmitByte(&I2C_MASTER_0, 0xD0);
        while(I2C_MASTER_GetFlagStatus(&I2C_MASTER_0, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U)
        {
        // wait for ACKS
        }
        I2C_MASTER_ClearFlag(&I2C_MASTER_0,XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED);

        // read
        SLAVE_ADDRESS = 0xEF;
        I2C_MASTER_SendRepeatedStart(&I2C_MASTER_0, SLAVE_ADDRESS, XMC_I2C_CH_CMD_READ);
        while(I2C_MASTER_GetFlagStatus(&I2C_MASTER_0, XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED) == 0U)
        {
        // wait for ACKS
        }
        I2C_MASTER_ClearFlag(&I2C_MASTER_0,XMC_I2C_CH_STATUS_FLAG_ACK_RECEIVED);

        data_received = I2C_MASTER_GetReceivedByte(&I2C_MASTER_0);
        // send NOACKM and then Stop condition
        XMC_I2C_CH_MasterReceiveNack(&I2C_MASTER_0);
        I2C_MASTER_SendStop(&I2C_MASTER_0);


  }
}


Viewing all articles
Browse latest Browse all 9892

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>