Interrupt free I2C functions ...

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

cross mob
JEv_295166
Level 3
Level 3
First like received

Greetings.

   

We are testing PSOC I2C interaction with some 3rd party devices and getting odd results using the high-level MasterXXX functions. To try and isolate issues and ensure fewer unintended consequences I've written I2C read and write functions that follow the following patterns. They appear to work more consistenly but we are still seeing some odd errors. Would anyone care to critique and/or comment? I would be much obliged!

   

TVMIA

   

Jerry

   

------------------------------------------------------------------------------------------

   

// N.B. all function return values are checked by a general purpose error flag decoder if return value is not I2C_MSTR_NO_ERROR

   

// N.B I2C_Master interrupts are all disabled and cleared at startup

   

    I2C_DisableInt();
    I2C_ClearPendingInt();

   

// I2CWrite pattern

   

I2C_MasterSendStart(device_address, I2C_WRITE_XFER_MODE); // send start write mode

   

I2C_MasterWriteByte(register_address); // register address on the device is 8 bits

   

I2C_MasterWriteByte(data[0..N-1]); // write N bytes to the bus

   

I2C_MasterSendStop();  // send stop

   

// I2CRead pattern

   

I2C_MasterSendStart(device_address, I2C_WRITE_XFER_MODE); // we need to write the register address before we read

   

I2C_MasterWriteByte(register_address); // write it

   

I2C_MasterSendRestart(device_address, I2C_READ_XFER_MODE); // switch transfer mode as we now wish to read

   

data[0..n-1] = I2C_MasterReadByte(I2C_NAK_DATA); // read N bytes

   

I2C_MasterSendStop();  // and then stop the I2C transaction.

   

------------------------------------------------------------------------------------------

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The only error I can see within your code snippets is:

   

data[0..n-1] = I2C_MasterReadByte(I2C_NAK_DATA); // read N bytes

   

only the last byte red  has to be NAKed, all others [0..n-2] must be ACKed.

   

 

   

Bob

View solution in original post

0 Likes
3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The only error I can see within your code snippets is:

   

data[0..n-1] = I2C_MasterReadByte(I2C_NAK_DATA); // read N bytes

   

only the last byte red  has to be NAKed, all others [0..n-2] must be ACKed.

   

 

   

Bob

0 Likes

Hello Bob,

   

Extremely helpful thank you. Naturally we are working with single data bytes so would not have got bitten by the ACK issue until later.

   

Best wishes for the season.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Glad to have helped you a bit (or even a byte)

   

 

   

Bob

0 Likes