How to Write more than 1 byte command in I2C

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

cross mob
luonc_4149601
Level 1
Level 1

Greetings to everyone.

Like the title of the discussion says, i have to write a 16 bit command in a sensor using I2C interface.

The structure of the message should be (according to sensor datasheet):

S+Slave_addr+W+ACK+8bit Command(MSB)+ACK+8bit Command(LSB)+ACK

I've tried this code:

Error myI2C_Write(uint8_t SlaveAddr, uint16_t command){

    Error error;

    static cy_en_scb_i2c_status_t tmp;

    tmp=I2C_MasterSendStart(SlaveAddr,CY_SCB_I2C_WRITE_XFER,0);

    if (tmp!=CY_SCB_I2C_SUCCESS){

        error=ERROR_ALL;

        return error;

    }

    tmp=I2C_MasterWriteByte(command>>8,0);

    if (tmp!=CY_SCB_I2C_SUCCESS){

        error=ERROR_ALL;

        return error;

    }

    uint8_t cmd2=command&0xFF;

    tmp=I2C_MasterWriteByte(cmd2,0);

   

    if (tmp!=CY_SCB_I2C_SUCCESS){

        error=ERROR_ALL;

        return error;

    }

   

    tmp=I2C_MasterSendStop(0);

    CyDelayUs(2000);

    if (tmp!=CY_SCB_I2C_SUCCESS){

        error=ERROR_ALL;

        return error;

    }

   

   

    return error=ERROR_NONE;

};

But after first command byte succeded, the second one return with "CY_SCB_I2C_MASTER_MANUAL_NAK"

I've also tried the high-level function "Cy_SCB_I2C_MasterWrite(I2C_HW,&masterTransferCfg,&I2C_context)"

with:

uint8_t buffer[2];

    buffer[0]=0x36;

    buffer[1]=0x15;

   

   

    masterTransferCfg.buffer=buffer;

    masterTransferCfg.bufferSize=2U;

    error=Cy_SCB_I2C_MasterWrite(I2C_HW,&masterTransferCfg,&I2C_context);

but this function doesn't seem to work at all.

In my debug panel i always see that the command is only 0x36 and not both.

Any suggestion?

Thanks in advance

Luca

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
ShanmathiN_06
Employee
Employee
100 replies posted 50 replies posted 25 replies posted

Hi Luca,

I modified the CE220818 to send out 2 bytes of data to an I2C slave. Please check the attached project, emulated slave logs and I2C bus scope shots.

You could use that as a reference. If you still face any issues with the attached project, please send the scope shots of your I2C bus (SDA and SCL).

Thanks,
Shanmathi

View solution in original post

0 Likes
5 Replies
lock attach
Attachments are accessible only for community members.
ShanmathiN_06
Employee
Employee
100 replies posted 50 replies posted 25 replies posted

Hi Luca,

I modified the CE220818 to send out 2 bytes of data to an I2C slave. Please check the attached project, emulated slave logs and I2C bus scope shots.

You could use that as a reference. If you still face any issues with the attached project, please send the scope shots of your I2C bus (SDA and SCL).

Thanks,
Shanmathi

0 Likes

Hi, thanks for the reply.

Sadly this code seems to fail in this point:

            if ((0u == (MASTER_ERROR_MASK & masterStatus)) &&

                (TX_PACKET_SIZE == Cy_SCB_I2C_MasterGetTransferCount(I2C_HW, &I2C_context)))

            {

                status = TRANSFER_CMPLT;

            }

in particular this condition:  (0u == (MASTER_ERROR_MASK & masterStatus)

is always false, and i can't understand why.

I've tried to monitoring the output of both scl and sda lines to see if they were behaving correctly and as far as i can understand it seems that they are working well.

Any further suggestion?

Thanks in advance

0 Likes

Hi,

Can you check what is the master status, when the IF condition is executed?

Also, pls share the I2C bus wave forms when the master status has errors.

Thanks,
Shanmathi

0 Likes

The master status is:

0x00220000 and that doesn't seem to be one of the macros defined in the API documentation

This is a shot taken to an oscilloscope of the 2 waveforms. The upper one is the SDA and the other is the SCL line.

I hope this is what you were asking for.

I think that the waveform of the SCL is odd, it should be high in normal state, according to the documentation od the I2C protocol. I'm missing something?

Thanks for your time and patience.

Luca

IMG_20190408_130707.jpg

0 Likes

I've solved.

The problem was i had to connect my device through different pull-up resistors and also there was a missing line in the code: mI2C_Start();

Thanks for your time and help.

0 Likes