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

cross mob

I2C Master Implementation in PSoC® 5LP using Low-level APIs - KBA 223957

I2C Master Implementation in PSoC® 5LP using Low-level APIs - KBA 223957

Community-Team
Employee
Employee
50 questions asked 10 questions asked 5 questions asked

Version: **

Question:

How do I implement I2C Master in PSoC® 5LP using low-level APIs?

Answer:

In use cases of communication with I2C slave memory devices, use the following code snippets:

1.  Write Transaction

For example:

Slave address of the memory device: 0x08

Address of the memory location: 0x00

Number of bytes to be transmitted: 5

Data to be transmitted: 1, 2, 3, 4, 5

#define SLAVE_ADDRESS (0x08u)

#define SUB_ADDRESS_LOCATION (0x00u)

#define SUB_ADDRESS_LENGTH (0x01u)

#define WRITE_DATA_LENGTH (5u)

#define WRITE_DATA_BUFFER_LENGTH (WRITE_DATA_LENGTH + SUB_ADDRESS_LENGTH)

uint8 i = 0;

    uint8 status = 0;

/* Load the write buffer with the sub-address location and data to be transmitted */

uint8 write_data_buffer[WRITE_DATA_BUFFER_LENGTH] = { SUB_ADDRESS_LOCATION , 1 , 2 , 3 , 4 , 5 };

/* Send start condition and slave address, followed by write bit */

      status = I2C_MasterSendStart(SLAVE_ADDRESS, I2C_WRITE_XFER_MODE);     

     /* Check if the master status is error free */

   

     if(status == I2C_MSTR_NO_ERROR)

     {

        for(i=0; i<WRITE_DATA_BUFFER_LENGTH; i++)

        {

/* Transmit data to be written to the slave that was loaded into the write buffer initially */

           

            status = I2C_MasterWriteByte(write_data_buffer);

            /* Check if the master status is error free */

           

            if(status != I2C_MSTR_NO_ERROR)

            {

                break;

            }

        }

}

/* Send stop condition after the transaction is completed */

        

I2C_MasterSendStop();

  2. Read Transaction

For example:

Slave address of the memory device: 0x08

Address of the memory location: 0x00

Number of bytes to be read: 5

#define SLAVE_ADDRESS (0x08u)

#define SUB_ADDRESS_LOCATION (0x00u)

#define SUB_ADDRESS_LENGTH (0x01u)

#define READ_DATA_LENGTH (5u)

uint8 i = 0;

    uint8 status = 0;

/* Load the write buffer with the sub-address location */

uint8 write_data_buffer[SUB_ADDRESS_LENGTH] = { SUB_ADDRESS_LOCATION };

/* Initialize the read buffer */

uint8 read_data_buffer[READ_DATA_LENGTH];

/* Send start condition and slave address, followed by write bit */

     status = I2C_MasterSendStart(SLAVE_ADDRESS, I2C_WRITE_XFER_MODE);     

          /* Check if the master status is error free */

           

     if(status == I2C_MSTR_NO_ERROR)

     {

        for(i=0; i< SUB_ADDRESS_LENGTH; i++)

        {

      

            /* Send the sub-address */

          

            status = I2C_MasterWriteByte(write_data_buffer);

            /* Check if the master status is error free */

          

            if(status != I2C_MSTR_NO_ERROR)

            {

                break;

            }

        }

       /* Send start condition and slave address, followed by read bit */

       

      status = I2C_MasterSendRestart(SLAVE_ADDRESS, I2C_READ_XFER_MODE);

          

     /* Check if the master status is error free */

   

       if(status == I2C_MSTR_NO_ERROR)

     {

            for(i=0; i<(READ_DATA_LENGTH-1); i++)

            {

               

            /* Load the read buffer with the data read from slave, followed by ACK */

           

read_data_buffer = I2C_MasterReadByte(I2C_ACK_DATA);

            }

            /* Load the read buffer with the last data read from slave, followed by NACK */

           

read_data_buffer = I2C_MasterReadByte(I2C_NAK_DATA);

        }

}

     /* Send stop condition after the transaction is completed */

   

I2C_MasterSendStop();

Refer to the I2C Component Datasheet for details on each API.

Note High-level APIs such as WriteBuf() and ReadBuf(), should not be used in conjunction with the low-level APIs.

0 Likes
3529 Views
Contributors