I2C Read Enhancements

- Enable I2C implicit I2C retries on transaction errors.
  Disabled by ONLP_I2C_F_DISABLE_READ_RETRIES.
This commit is contained in:
Jeffrey Townsend
2017-05-26 00:42:21 +00:00
parent 20af5f254c
commit a43cd8a9a3
6 changed files with 41 additions and 11 deletions

View File

@@ -51,6 +51,10 @@ cdefs: &cdefs
- ONLPLIB_CONFIG_I2C_BLOCK_SIZE:
doc: "Maximum read and write block size."
default: 32
- ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT:
doc: "The number of I2C read retry attempts (if enabled)."
default: 16
- ONLPLIB_CONFIG_I2C_USE_CUSTOM_HEADER:
doc: "Include the custom i2c header (include/linux/i2c-devices.h) to avoid conflicts with the kernel and i2c-dev packages."
default: 1

View File

@@ -71,6 +71,11 @@
*/
#define ONLP_I2C_F_USE_SMBUS_BLOCK_READ 0x40
/**
* Do not retry reads on I2C transaction failures.
*/
#define ONLP_I2C_F_DISABLE_READ_RETRIES 0x80
/**
* @brief Open and prepare for reading or writing.
* @param bus The i2c bus number.

View File

@@ -135,6 +135,16 @@
#define ONLPLIB_CONFIG_I2C_BLOCK_SIZE 32
#endif
/**
* ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT
*
* The number of I2C read retry attempts (if enabled). */
#ifndef ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT
#define ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT 16
#endif
/**
* ONLPLIB_CONFIG_I2C_USE_CUSTOM_HEADER
*

View File

@@ -105,16 +105,22 @@ onlp_i2c_block_read(int bus, uint8_t addr, uint8_t offset, int size,
int count = size;
uint8_t* p = rdata;
while(count > 0) {
int rv;
int rsize = (count >= ONLPLIB_CONFIG_I2C_BLOCK_SIZE) ? ONLPLIB_CONFIG_I2C_BLOCK_SIZE : count;
if(flags & ONLP_I2C_F_USE_SMBUS_BLOCK_READ) {
rv = i2c_smbus_read_block_data(fd, offset, p);
} else {
rv = i2c_smbus_read_i2c_block_data(fd,
offset,
rsize,
p);
offset += rsize;
int retries = (flags & ONLP_I2C_F_DISABLE_READ_RETRIES) ? 1 : ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT;
int rv = -1;
while(retries-- && rv < 0) {
if(flags & ONLP_I2C_F_USE_SMBUS_BLOCK_READ) {
rv = i2c_smbus_read_block_data(fd, offset, p);
} else {
rv = i2c_smbus_read_i2c_block_data(fd,
offset,
rsize,
p);
}
if(rv >= 0) {
offset += rsize;
}
}
if(rv != rsize) {
@@ -150,7 +156,7 @@ onlp_i2c_read(int bus, uint8_t addr, uint8_t offset, int size,
for(i = 0; i < size; i++) {
int rv = -1;
int retries = 3;
int retries = (flags & ONLP_I2C_F_DISABLE_READ_RETRIES) ? 1: ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT;
while(retries-- && rv < 0) {
rv = i2c_smbus_read_byte_data(fd, offset+i);

View File

@@ -75,6 +75,11 @@ onlplib_config_settings_t onlplib_config_settings[] =
#else
{ ONLPLIB_CONFIG_I2C_BLOCK_SIZE(__onlplib_config_STRINGIFY_NAME), "__undefined__" },
#endif
#ifdef ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT
{ __onlplib_config_STRINGIFY_NAME(ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT), __onlplib_config_STRINGIFY_VALUE(ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT) },
#else
{ ONLPLIB_CONFIG_I2C_READ_RETRY_COUNT(__onlplib_config_STRINGIFY_NAME), "__undefined__" },
#endif
#ifdef ONLPLIB_CONFIG_I2C_USE_CUSTOM_HEADER
{ __onlplib_config_STRINGIFY_NAME(ONLPLIB_CONFIG_I2C_USE_CUSTOM_HEADER), __onlplib_config_STRINGIFY_VALUE(ONLPLIB_CONFIG_I2C_USE_CUSTOM_HEADER) },
#else

View File

@@ -3,7 +3,7 @@
#
# Inclusive Makefile for the onlplib module.
#
# Autogenerated 2016-12-15 17:09:12.738344
# Autogenerated 2017-05-26 00:39:15.535760
#
###############################################################################
onlplib_BASEDIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))