cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC™ 4

FlHe_4723206
New Contributor II

uint8 writeRegister8(uint8 i2c_address, uint8 reg, uint8 data){

        if(!I2C_MasterSendStart(i2c_address,0)){

            I2C_MasterWriteByte(reg);

            I2C_MasterWriteByte(data);

            return I2C_MasterSendStop();

        }else{

            I2C_MasterSendStop();

            return 1;

        }

    }else{

        return 99u; //placeholder

    }

}

with the loop in my main code being:

    for(uint8 i=0; i<BUF_LENGTH; i++){

        status = writeRegister8(i2c_address, PWM_REGISTER+i,gamma32[brightness]);

    }

I want to replace the loop+function call with I2C_MasterWriteBuf function. However, I2C_MasterWriteBuf does not have a register field. I have tried all sorts of combinations of writing register address first as a byte, restarting the bus, stopping and starting again, writing the buffer directly after setting the register address... Nothing works in terms of that the slave device never gets the right values.

Basic question is: How do I use

I2C_MasterWriteBuf

in an intended way when I need to set a register to write to first?

0 Likes
1 Solution
BragadeeshV
Moderator
Moderator

Hi flhe_4723206​,

Instead of having separate variables for register and data, you can have a single structure or single array to hold both these values. You can pass this array/ structure containing both these values as parameter to the I2C_MasterWriteBuf . See attached file for reference.

Let us know if this solves your problem.

Regards,

Bragadeesh

Regards,
Bragadeesh

View solution in original post

4 Replies
MotooTanaka
Esteemed Contributor

Hi,

I wonder if i2c function i2c_writeRegs() in my sample below can be a hint for you.

MCU Tester, a Swiss Army Knife for PSoC (CY8CKIT-044 version)

moto

FlHe_4723206
New Contributor II

Thank you, that is a very interesting project and I'm sure I will dive into it.

BragadeeshV
Moderator
Moderator

Hi flhe_4723206​,

Instead of having separate variables for register and data, you can have a single structure or single array to hold both these values. You can pass this array/ structure containing both these values as parameter to the I2C_MasterWriteBuf . See attached file for reference.

Let us know if this solves your problem.

Regards,

Bragadeesh

Regards,
Bragadeesh

View solution in original post

FlHe_4723206
New Contributor II

Hi BragadeeshV_41

Thanks a lot for your hint! After I adapted your code to my function it worked immediately and without any need for further tweaks. Have a good day!