- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
------------------------------------------------------------------------------------------
Solved! Go to Solution.
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Glad to have helped you a bit (or even a byte)
Bob