From 8c9bddf9985e5d1cb32a9edd611aac520474f098 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 1 Feb 2016 08:57:22 -0600 Subject: [PATCH] I2C: Eliminate the I2C_WRITE and I2C_READ macros --- TODO | 10 ++-- configs | 2 +- drivers/audio/wm8904.c | 11 +++- drivers/ioexpander/pca9555.c | 4 +- drivers/leds/pca9635pw.c | 21 ++++---- drivers/mtd/at24xx.c | 64 ++++++++++++++++++++--- drivers/power/bq2425x.c | 57 +++++++++++++++++++-- drivers/power/max1704x.c | 56 +++++++++++++++++--- drivers/sensors/Kconfig | 36 +++++++++++++ drivers/sensors/as5048b.c | 59 ++++++++++++++++++--- drivers/sensors/bmp180.c | 68 +++++++++++++++++++++---- drivers/sensors/lm75.c | 87 ++++++++++++++++++++++++------- drivers/sensors/lm92.c | 93 ++++++++++++++++++++++++++-------- drivers/sensors/lsm9ds1.c | 68 ++++++++++++++++++++++--- drivers/sensors/mb7040.c | 71 +++++++++++++++++++++++--- drivers/sensors/mcp9844.c | 71 +++++++++++++++++++++++--- drivers/sensors/ms58xx.c | 80 ++++++++++++++++++++++++----- drivers/timers/ds3231.c | 5 +- drivers/timers/pcf85263.c | 5 +- drivers/video/ov2640.c | 57 ++++++++++++++++++--- include/nuttx/i2c/i2c_master.h | 67 +++++------------------- 21 files changed, 802 insertions(+), 190 deletions(-) diff --git a/TODO b/TODO index 9d7c25dfad..854b3996e4 100644 --- a/TODO +++ b/TODO @@ -1614,10 +1614,9 @@ o Other drivers (drivers/) in a multi-tasking I2C environment: - I2C_SETFREQUENCY: Frequency setting can be overwritten by other I2C usage. - - I2C_SETADDRESS used with I2C_READ and I2C_WRITE: - Similarly, address can and will be changed by other I2C usage. - NOTE also that I2C_SETADDRESS also sets the address width (either - 7 or 10 bits). + - I2C_SETADDRESS: The I2C address can and will be changed by other + I2C usage. NOTE also that I2C_SETADDRESS also sets the address width + (either 7 or 10 bits). - I2C_TRANSFER: This is the only interface that is properly self contained and protected from most mult-tasking issues. But even this interface can suffer if there are differing frequency settings. @@ -1626,8 +1625,7 @@ o Other drivers (drivers/) are two ways to fix theses problems: (1) Add a locking method such as is provided with the SPI interface, or (2) make each interface self-contained and atomic: Remove the I2C_FREQUENCY and I2C_ADDRESS - methods; Add frequency to all interfaces and add the address to - I2C_READ and I2C_WRITE. + methods; Add frequency to the I2C_TRANSFER message structure. o Linux/Cywgin simulation (arch/sim) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/configs b/configs index ee7d12e263..7286dea76c 160000 --- a/configs +++ b/configs @@ -1 +1 @@ -Subproject commit ee7d12e2635c22a7adedf923c6564c29ec057272 +Subproject commit 7286dea76c9a19895a14d02b593c4c6356461a9d diff --git a/drivers/audio/wm8904.c b/drivers/audio/wm8904.c index 0742db5072..6b298b079c 100644 --- a/drivers/audio/wm8904.c +++ b/drivers/audio/wm8904.c @@ -332,8 +332,15 @@ uint16_t wm8904_readreg(FAR struct wm8904_dev_s *priv, uint8_t regaddr) static void wm8904_writereg(FAR struct wm8904_dev_s *priv, uint8_t regaddr, uint16_t regval) { + struct i2c_config_s config; int retries; + /* Setup up the I2C configuration */ + + config.frequency = priv->lower->frequency; + config.address = priv->lower->address; + config.addrlen = 7; + /* Try up to three times to read the register */ for (retries = 1; retries <= 3; retries++) @@ -351,13 +358,13 @@ static void wm8904_writereg(FAR struct wm8904_dev_s *priv, uint8_t regaddr, * completed. */ - ret = I2C_WRITE(priv->i2c, data, 3); + ret = i2c_write(priv->i2c, &config, data, 3); if (ret < 0) { #ifdef CONFIG_I2C_RESET /* Perhaps the I2C bus is locked up? Try to shake the bus free */ - auddbg("WARNING: I2C_TRANSFER failed: %d ... Resetting\n", ret); + auddbg("WARNING: i2c_write failed: %d ... Resetting\n", ret); ret = up_i2creset(priv->i2c); if (ret < 0) diff --git a/drivers/ioexpander/pca9555.c b/drivers/ioexpander/pca9555.c index a2607a8db9..7aafe3d9ae 100644 --- a/drivers/ioexpander/pca9555.c +++ b/drivers/ioexpander/pca9555.c @@ -128,10 +128,10 @@ static const struct ioexpander_ops_s g_pca9555_ops = ****************************************************************************/ /**************************************************************************** - * Name: pca9555_writeread + * Name: pca9555_write * * Description: - * Write to then read from the I2C device. + * Write to the I2C device. * ****************************************************************************/ diff --git a/drivers/leds/pca9635pw.c b/drivers/leds/pca9635pw.c index d07b10baab..b4a8419061 100644 --- a/drivers/leds/pca9635pw.c +++ b/drivers/leds/pca9635pw.c @@ -103,6 +103,8 @@ static int pca9635pw_i2c_write_byte(FAR struct pca9635pw_dev_s *priv, uint8_t const reg_addr, uint8_t const reg_val) { + struct i2c_config_s config; + dbg("pca9635pw_i2c_write_byte\n"); /* assemble the 2 byte message comprised of reg_addr and reg_val */ @@ -113,25 +115,22 @@ static int pca9635pw_i2c_write_byte(FAR struct pca9635pw_dev_s *priv, buffer[0] = reg_addr; buffer[1] = reg_val; - /* Write the register address followed by the data (no RESTART) */ + /* Setup up the I2C configuration */ - uint8_t const NUMBER_OF_I2C_ADDRESS_BITS = 7; + config.frequency = I2C_BUS_FREQ_HZ; + config.address = priv->i2c_addr; + config.addrlen = 7; + + /* Write the register address followed by the data (no RESTART) */ dbg("i2c addr: 0x%02X reg addr: 0x%02X value: 0x%02X\n", priv->i2c_addr, buffer[0], buffer[1]); - int ret = I2C_SETADDRESS(priv->i2c, priv->i2c_addr, - NUMBER_OF_I2C_ADDRESS_BITS); - if (ret != OK) - { - dbg("I2C_SETADDRESS returned error code %d\n", ret); - return ret; - } - ret = I2C_WRITE(priv->i2c, buffer, BUFFER_SIZE); + ret = i2c_write(priv->i2c, &config, buffer, BUFFER_SIZE); if (ret != OK) { - dbg("I2C_WRITE returned error code %d\n", ret); + dbg("i2c_write returned error code %d\n", ret); return ret; } diff --git a/drivers/mtd/at24xx.c b/drivers/mtd/at24xx.c index ef6abe5322..5cfe754dd4 100644 --- a/drivers/mtd/at24xx.c +++ b/drivers/mtd/at24xx.c @@ -4,9 +4,9 @@ * * Copyright (C) 2011 Li Zhuoyi. All rights reserved. * Author: Li Zhuoyi - * History: 0.1 2011-08-20 initial version * - * 2011-11-1 Added support for larger MTD block sizes: Hal Glenn + * Copyright (C) 2013, 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Derived from drivers/mtd/m25px.c * @@ -192,6 +192,54 @@ static struct at24c_dev_s g_at24c; * Private Functions ************************************************************************************/ +/**************************************************************************** + * Name: at24c_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int at24c_i2c_write(FAR struct at24c_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_AT24XX_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->dev, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: at24c_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int at24c_i2c_read(FAR struct at24c_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_AT24XX_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->dev, &config, buffer, buflen); +} + +/************************************************************************************ + * Name: at24c_eraseall + ************************************************************************************/ + static int at24c_eraseall(FAR struct at24c_dev_s *priv) { int startblock = 0; @@ -212,12 +260,12 @@ static int at24c_eraseall(FAR struct at24c_dev_s *priv) I2C_SETADDRESS(priv->dev, priv->addr | ((offset >> 8) & 0x07), 7); #endif - while (I2C_WRITE(priv->dev, buf, AT24XX_ADDRSIZE) < 0) + while (at24c_i2c_write(priv, buf, AT24XX_ADDRSIZE) < 0) { usleep(1000); } - I2C_WRITE(priv->dev, buf, AT24XX_PAGESIZE + AT24XX_ADDRSIZE); + at24c_i2c_write(priv, buf, AT24XX_PAGESIZE + AT24XX_ADDRSIZE); } return OK; @@ -273,7 +321,7 @@ static ssize_t at24c_read_internal(FAR struct at24c_dev_s *priv, off_t offset, I2C_SETADDRESS(priv->dev, address | ((offset >> 8) & 0x07), 7); #endif - while (I2C_WRITE(priv->dev, buf, AT24XX_ADDRSIZE) < 0) + while (at24c_i2c_write(priv, buf, AT24XX_ADDRSIZE) < 0) { fvdbg("wait\n"); usleep(1000); @@ -281,7 +329,7 @@ static ssize_t at24c_read_internal(FAR struct at24c_dev_s *priv, off_t offset, /* Then transfer the following bytes */ - I2C_READ(priv->dev, buffer, nbytes); + at24c_i2c_read(priv, buffer, nbytes); return nbytes; } @@ -388,7 +436,7 @@ static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock, size_t I2C_SETADDRESS(priv->dev, priv->addr | ((offset >> 8) & 0x07), 7); #endif - while (I2C_WRITE(priv->dev, buf, AT24XX_ADDRSIZE) < 0) + while (at24c_i2c_write(priv, buf, AT24XX_ADDRSIZE) < 0) { fvdbg("wait\n"); usleep(1000); @@ -396,7 +444,7 @@ static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock, size_t memcpy(&buf[AT24XX_ADDRSIZE], buffer, priv->pagesize); - I2C_WRITE(priv->dev, buf, priv->pagesize + AT24XX_ADDRSIZE); + at24c_i2c_write(priv, buf, priv->pagesize + AT24XX_ADDRSIZE); startblock++; buffer += priv->pagesize; } diff --git a/drivers/power/bq2425x.c b/drivers/power/bq2425x.c index ad73f9f8ca..3a00dc6f1b 100644 --- a/drivers/power/bq2425x.c +++ b/drivers/power/bq2425x.c @@ -5,6 +5,9 @@ * Copyright (C) 2015 Alan Carvalho de Assis. All rights reserved. * Author: Alan Carvalho de Assis * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -144,6 +147,50 @@ static const struct battery_charger_operations_s g_bq2425xops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: bq2425x_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int bq2425x_i2c_write(FAR struct bq2425x_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->frequency; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: bq2425x_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int bq2425x_i2c_read(FAR struct bq2425x_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->frequency; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: bq2425x_getreg8 * @@ -167,19 +214,19 @@ static int bq2425x_getreg8(FAR struct bq2425x_dev_s *priv, uint8_t regaddr, /* Write the register address */ - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = bq2425x_i2c_write(priv, ®addr, 1); if (ret < 0) { - batdbg("I2C_WRITE failed: %d\n", ret); + batdbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 8-bits from the register */ - ret = I2C_READ(priv->i2c, &val, 1); + ret = bq2425x_i2c_read(priv, &val, 1); if (ret < 0) { - batdbg("I2C_READ failed: %d\n", ret); + batdbg("i2c_read failed: %d\n", ret); return ret; } @@ -217,7 +264,7 @@ static int bq2425x_putreg8(FAR struct bq2425x_dev_s *priv, uint8_t regaddr, /* Write the register address followed by the data (no RESTART) */ - return I2C_WRITE(priv->i2c, buffer, 2); + return bq2425x_i2c_write(priv, buffer, 2); } /**************************************************************************** diff --git a/drivers/power/max1704x.c b/drivers/power/max1704x.c index ff358bda17..5ced5cfc09 100644 --- a/drivers/power/max1704x.c +++ b/drivers/power/max1704x.c @@ -2,7 +2,7 @@ * drivers/power/max1704x.c * Lower half driver for MAX1704x battery fuel gauge * - * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2012, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -229,6 +229,50 @@ static const struct battery_gauge_operations_s g_max1704xops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: max1704x_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int max1704x_i2c_write(FAR struct max1704x_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->frequency; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: max1704x_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int max1704x_i2c_read(FAR struct max1704x_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->frequency; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: max1704x_getreg16 * @@ -252,19 +296,19 @@ static int max1704x_getreg16(FAR struct max1704x_dev_s *priv, uint8_t regaddr, /* Write the register address */ - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = max1704x_i2c_write(priv, ®addr, 1); if (ret < 0) { - batdbg("I2C_WRITE failed: %d\n", ret); + batdbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 16-bits from the register */ - ret = I2C_READ(priv->i2c, buffer, 2); + ret = max1704x_i2c_read(priv, buffer, 2); if (ret < 0) { - batdbg("I2C_READ failed: %d\n", ret); + batdbg("i2c_read failed: %d\n", ret); return ret; } @@ -302,7 +346,7 @@ static int max1704x_putreg16(FAR struct max1704x_dev_s *priv, uint8_t regaddr, /* Write the register address followed by the data (no RESTART) */ - return I2C_WRITE(priv->i2c, buffer, 3); + return max1704x_i2c_write(priv, buffer, 3); } /**************************************************************************** diff --git a/drivers/sensors/Kconfig b/drivers/sensors/Kconfig index dc57cc69ba..46f0e3b2c9 100644 --- a/drivers/sensors/Kconfig +++ b/drivers/sensors/Kconfig @@ -31,6 +31,12 @@ config SN_LSM9DS1 ---help--- Enable driver support for the STMicro LSM9DS1. +config LSM9DS1_I2C_FREQUENCY + bool "LSM9DS1 I2C frequency" + default 400000 + range 1 400000 + depends on SN_LSM9DS1 + config MB7040 bool "MaxBotix MB7040 Sonar support" default n @@ -38,6 +44,12 @@ config MB7040 ---help--- Enable driver support for the MaxBotix MB7040 sonar. +config MB7040_I2C_FREQUENCY + bool "MB7040 I2C frequency" + default 400000 + range 1 400000 + depends on MB7040 + config MCP9844 bool "MCP9844 Temperature Sensor" default n @@ -45,6 +57,12 @@ config MCP9844 ---help--- Enable driver support for the MCP9844 I2C Temperature sensor. +config MCP9844_I2C_FREQUENCY + bool "MCP9844 I2C frequency" + default 400000 + range 1 400000 + depends on MCP9844 + config MS58XX bool "MEAS MS58XX Altimeter support" default n @@ -52,6 +70,12 @@ config MS58XX ---help--- Enable driver support for MEAS MS58XX altimeters. +config MS58XX_I2C_FREQUENCY + bool "MS58XX I2C frequency" + default 400000 + range 1 400000 + depends on MS58XX + config MS58XX_VDD int "MEAS MS58XX VDD" default 30 @@ -134,6 +158,12 @@ config LM75 This should also work with compatible temperature sensors such as the TI TMP100/101. +config LM75_I2C_FREQUENCY + bool "LM75 I2C frequency" + default 100000 + range 1 100000 + depends on I2C_LM75 + config LM92 bool "TI LM92 Temperature Sensor support" default n @@ -141,6 +171,12 @@ config LM92 ---help--- Enable driver support for the TI LM92 Temperature Sensor. +config LM92_I2C_FREQUENCY + bool "LM92 I2C frequency" + default 400000 + range 1 400000 + depends on LM92 + config QENCODER bool "Qencoder" default n diff --git a/drivers/sensors/as5048b.c b/drivers/sensors/as5048b.c index 7ee52e1f0f..154f82bd63 100644 --- a/drivers/sensors/as5048b.c +++ b/drivers/sensors/as5048b.c @@ -5,6 +5,9 @@ * Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved. * Author: Paul Alexander Patience * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -111,6 +114,50 @@ static const struct qe_ops_s g_qeops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: as5048b_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int as5048b_i2c_write(FAR struct as5048b_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->frequency; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: as5048b_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int as5048b_i2c_read(FAR struct as5048b_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->frequency; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: as5048b_readu8 * @@ -127,19 +174,19 @@ static int as5048b_readu8(FAR struct as5048b_dev_s *priv, uint8_t regaddr, /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); + ret = as5048b_i2c_write(priv, ®addr, sizeof(regaddr)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 8 bits from the register */ - ret = I2C_READ(priv->i2c, regval, sizeof(*regval)); + ret = as5048b_i2c_read(priv, regval, sizeof(*regval)); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -209,10 +256,10 @@ static int as5048b_writeu8(FAR struct as5048b_dev_s *priv, uint8_t regaddr, /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, buffer, sizeof(buffer)); + ret = as5048b_i2c_write(priv, buffer, sizeof(buffer)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); } return ret; diff --git a/drivers/sensors/bmp180.c b/drivers/sensors/bmp180.c index ce16dd94e4..97fda6221a 100644 --- a/drivers/sensors/bmp180.c +++ b/drivers/sensors/bmp180.c @@ -3,10 +3,12 @@ * drivers/sensors/bmp180.c * Character driver for the Freescale BMP1801 Barometer Sensor * - * Copyright (C) 2015 Gregory Nutt. All rights reserved. * Copyright (C) 2015 Alan Carvalho de Assis * Author: Alan Carvalho de Assis * + * Copyright (C) 2015-2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -171,6 +173,50 @@ static const struct file_operations g_bmp180fops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: bmp180_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int bmp180_i2c_write(FAR struct bmp180_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->freq; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: bmp180_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int bmp180_i2c_read(FAR struct bmp180_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = priv->freq; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: bmp180_getreg8 * @@ -186,19 +232,19 @@ static uint8_t bmp180_getreg8(FAR struct bmp180_dev_s *priv, uint8_t regaddr) /* Restart the register */ - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = bmp180_i2c_write(priv, ®addr, 1); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart the register */ - ret = I2C_READ(priv->i2c, ®val, 1); + ret = bmp180_i2c_read(priv, ®val, 1); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -221,19 +267,19 @@ static uint16_t bmp180_getreg16(FAR struct bmp180_dev_s *priv, uint8_t regaddr) /* Register to read */ - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = bmp180_i2c_write(priv, ®addr, 1); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Read register */ - ret = I2C_READ(priv->i2c, (uint8_t *)®val, 2); + ret = bmp180_i2c_read(priv, (uint8_t *)®val, 2); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -266,10 +312,10 @@ static void bmp180_putreg8(FAR struct bmp180_dev_s *priv, uint8_t regaddr, /* Restart the register */ - ret = I2C_WRITE(priv->i2c, (uint8_t *) &data, 2); + ret = bmp180_i2c_write(priv, (uint8_t *) &data, 2); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return; } diff --git a/drivers/sensors/lm75.c b/drivers/sensors/lm75.c index 609377f69e..f70c61bfd1 100644 --- a/drivers/sensors/lm75.c +++ b/drivers/sensors/lm75.c @@ -2,7 +2,7 @@ * drivers/sensors/lm75.c * Character driver for the STMicro LM-75 Temperature Sensor * - * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2013, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -56,6 +56,10 @@ * Pre-processor Definitions ****************************************************************************/ +#ifndef CONFIG_LM75_I2C_FREQUENCY +# define CONFIG_LM75_I2C_FREQUENCY 100000 +#endif + /* Centigrade to Fahrenheit conversion: F = 9*C/5 + 32 */ #define B16_9DIV5 (9 * 65536 / 5) @@ -77,13 +81,17 @@ struct lm75_dev_s ****************************************************************************/ /* I2C Helpers */ -static int lm75_readb16(FAR struct lm75_dev_s *priv, uint8_t regaddr, - FAR b16_t *regvalue); -static int lm75_writeb16(FAR struct lm75_dev_s *priv, uint8_t regaddr, - b16_t regval); -static int lm75_readtemp(FAR struct lm75_dev_s *priv, FAR b16_t *temp); -static int lm75_readconf(FAR struct lm75_dev_s *priv, FAR uint8_t *conf); -static int lm75_writeconf(FAR struct lm75_dev_s *priv, uint8_t conf); +static int lm75_i2c_write(FAR struct lm75_dev_s *priv, + FAR const uint8_t *buffer, int buflen); +static int lm75_i2c_read(FAR struct lm75_dev_s *priv, + FAR uint8_t *buffer, int buflen); +static int lm75_readb16(FAR struct lm75_dev_s *priv, uint8_t regaddr, + FAR b16_t *regvalue); +static int lm75_writeb16(FAR struct lm75_dev_s *priv, uint8_t regaddr, + b16_t regval); +static int lm75_readtemp(FAR struct lm75_dev_s *priv, FAR b16_t *temp); +static int lm75_readconf(FAR struct lm75_dev_s *priv, FAR uint8_t *conf); +static int lm75_writeconf(FAR struct lm75_dev_s *priv, uint8_t conf); /* Character driver methods */ @@ -115,6 +123,51 @@ static const struct file_operations g_lm75fops = /**************************************************************************** * Private Functions ****************************************************************************/ + +/**************************************************************************** + * Name: lm75_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int lm75_i2c_write(FAR struct lm75_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_LM75_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: lm75_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int lm75_i2c_read(FAR struct lm75_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_LM75_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: lm75_readb16 * @@ -132,19 +185,19 @@ static int lm75_readb16(FAR struct lm75_dev_s *priv, uint8_t regaddr, /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = lm75_i2c_write(priv, ®addr, 1); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 16-bits from the register (discarding 7) */ - ret = I2C_READ(priv->i2c, buffer, 2); + ret = lm75_i2c_read(priv, buffer, 2); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -185,7 +238,7 @@ static int lm75_writeb16(FAR struct lm75_dev_s *priv, uint8_t regaddr, /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - return I2C_WRITE(priv->i2c, buffer, 3); + return lm75_i2c_write(priv, buffer, 3); } /**************************************************************************** @@ -244,16 +297,16 @@ static int lm75_readconf(FAR struct lm75_dev_s *priv, FAR uint8_t *conf) I2C_SETADDRESS(priv->i2c, priv->addr, 7); buffer = LM75_CONF_REG; - ret = I2C_WRITE(priv->i2c, &buffer, 1); + ret = lm75_i2c_write(priv, &buffer, 1); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 8-bits from the register */ - ret = I2C_READ(priv->i2c, conf, 1); + ret = lm75_i2c_read(priv, conf, 1); sndbg("conf: %02x ret: %d\n", *conf, ret); return ret; } @@ -280,7 +333,7 @@ static int lm75_writeconf(FAR struct lm75_dev_s *priv, uint8_t conf) /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - return I2C_WRITE(priv->i2c, buffer, 2); + return lm75_i2c_write(priv, buffer, 2); } /**************************************************************************** diff --git a/drivers/sensors/lm92.c b/drivers/sensors/lm92.c index 432445e30c..e168e90a51 100644 --- a/drivers/sensors/lm92.c +++ b/drivers/sensors/lm92.c @@ -58,6 +58,10 @@ * Pre-processor Definitions ****************************************************************************/ +#ifndef CONFIG_LM92_I2C_FREQUENCY +# define CONFIG_LM92_I2C_FREQUENCY 400000 +#endif + /* Centigrade to Fahrenheit conversion: F = 9*C/5 + 32 */ #define B16_9DIV5 (9 * 65536 / 5) @@ -79,13 +83,17 @@ struct lm92_dev_s ****************************************************************************/ /* I2C Helpers */ -static int lm92_readb16(FAR struct lm92_dev_s *priv, uint8_t regaddr, - FAR b16_t *regvalue); -static int lm92_writeb16(FAR struct lm92_dev_s *priv, uint8_t regaddr, - b16_t regval); -static int lm92_readtemp(FAR struct lm92_dev_s *priv, FAR b16_t *temp); -static int lm92_readconf(FAR struct lm92_dev_s *priv, FAR uint8_t *conf); -static int lm92_writeconf(FAR struct lm92_dev_s *priv, uint8_t conf); +static int lm92_i2c_write(FAR struct lm92_dev_s *priv, + FAR const uint8_t *buffer, int buflen); +static int lm92_i2c_read(FAR struct lm92_dev_s *priv, + FAR uint8_t *buffer, int buflen); +static int lm92_readb16(FAR struct lm92_dev_s *priv, uint8_t regaddr, + FAR b16_t *regvalue); +static int lm92_writeb16(FAR struct lm92_dev_s *priv, uint8_t regaddr, + b16_t regval); +static int lm92_readtemp(FAR struct lm92_dev_s *priv, FAR b16_t *temp); +static int lm92_readconf(FAR struct lm92_dev_s *priv, FAR uint8_t *conf); +static int lm92_writeconf(FAR struct lm92_dev_s *priv, uint8_t conf); /* Character driver methods */ @@ -117,6 +125,51 @@ static const struct file_operations g_lm92fops = /**************************************************************************** * Private Functions ****************************************************************************/ + +/**************************************************************************** + * Name: lm92_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int lm92_i2c_write(FAR struct lm92_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_LM92_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: lm92_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int lm92_i2c_read(FAR struct lm92_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_LM92_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: lm92_readb16 * @@ -135,19 +188,19 @@ static int lm92_readb16(FAR struct lm92_dev_s *priv, uint8_t regaddr, /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = lm92_i2c_write(priv, ®addr, 1); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 16 bits from the register (discarding 3) */ - ret = I2C_READ(priv->i2c, buffer, 2); + ret = lm92_i2c_read(priv, buffer, 2); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -188,7 +241,7 @@ static int lm92_writeb16(FAR struct lm92_dev_s *priv, uint8_t regaddr, /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - return I2C_WRITE(priv->i2c, buffer, 3); + return lm92_i2c_write(priv, buffer, 3); } /**************************************************************************** @@ -247,16 +300,16 @@ static int lm92_readconf(FAR struct lm92_dev_s *priv, FAR uint8_t *conf) I2C_SETADDRESS(priv->i2c, priv->addr, 7); buffer = LM92_CONF_REG; - ret = I2C_WRITE(priv->i2c, &buffer, 1); + ret = lm92_i2c_write(priv, &buffer, 1); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 8 bits from the register */ - ret = I2C_READ(priv->i2c, conf, 1); + ret = lm92_i2c_read(priv, conf, 1); sndbg("conf: %02x ret: %d\n", *conf, ret); return ret; } @@ -283,7 +336,7 @@ static int lm92_writeconf(FAR struct lm92_dev_s *priv, uint8_t conf) /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - return I2C_WRITE(priv->i2c, buffer, 2); + return lm92_i2c_write(priv, buffer, 2); } /**************************************************************************** @@ -305,19 +358,19 @@ static int lm92_readid(FAR struct lm92_dev_s *priv, FAR uint16_t *id) I2C_SETADDRESS(priv->i2c, priv->addr, 7); regaddr = LM92_ID_REG; - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = lm92_i2c_write(priv, ®addr, 1); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 16 bits from the register */ - ret = I2C_READ(priv->i2c, buffer, 2); + ret = lm92_i2c_read(priv, buffer, 2); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } diff --git a/drivers/sensors/lsm9ds1.c b/drivers/sensors/lsm9ds1.c index bdde5cdc35..3977cafeb1 100644 --- a/drivers/sensors/lsm9ds1.c +++ b/drivers/sensors/lsm9ds1.c @@ -4,6 +4,9 @@ * Copyright (C) 2016 Omni Hoverboards Inc. All rights reserved. * Author: Paul Alexander Patience * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -53,6 +56,11 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ + +#ifndef CONFIG_LSM9DS1_I2C_FREQUENCY +# define CONFIG_LSM9DS1_I2C_FREQUENCY 400000 +#endif + /* Register Addresses *******************************************************/ /* Accelerometer and gyroscope registers */ @@ -514,6 +522,10 @@ struct lsm9ds1_dev_s ****************************************************************************/ /* I2C Helpers */ +static int lsm9ds1_i2c_write(FAR struct lsm9ds1_dev_s *priv, + FAR const uint8_t *buffer, int buflen); +static int lsm9ds1_i2c_read(FAR struct lsm9ds1_dev_s *priv, + FAR uint8_t *buffer, int buflen); static int lsm9ds1_readreg8(FAR struct lsm9ds1_dev_s *priv, uint8_t regaddr, FAR uint8_t *regval); static int lsm9ds1_writereg8(FAR struct lsm9ds1_dev_s *priv, uint8_t regaddr, @@ -622,6 +634,50 @@ static const struct lsm9ds1_ops_s g_lsm9ds1mag_ops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: lsm9ds1_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int lsm9ds1_i2c_write(FAR struct lsm9ds1_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_LSM9DS1_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: lsm9ds1_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int lsm9ds1_i2c_read(FAR struct lsm9ds1_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_LSM9DS1_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: lsm9ds1_readreg8 * @@ -643,19 +699,19 @@ static int lsm9ds1_readreg8(FAR struct lsm9ds1_dev_s *priv, uint8_t regaddr, /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); + ret = lsm9ds1_i2c_write(priv, ®addr, sizeof(regaddr)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 8 bits from the register */ - ret = I2C_READ(priv->i2c, regval, sizeof(*regval)); + ret = lsm9ds1_i2c_read(priv, regval, sizeof(*regval)); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -689,10 +745,10 @@ static int lsm9ds1_writereg8(FAR struct lsm9ds1_dev_s *priv, uint8_t regaddr, /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, buffer, sizeof(buffer)); + ret = lsm9ds1_i2c_write(priv, buffer, sizeof(buffer)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } diff --git a/drivers/sensors/mb7040.c b/drivers/sensors/mb7040.c index 3741761ca2..5840204e0a 100644 --- a/drivers/sensors/mb7040.c +++ b/drivers/sensors/mb7040.c @@ -5,6 +5,9 @@ * Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved. * Author: Paul Alexander Patience * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -51,6 +54,14 @@ #if defined(CONFIG_I2C) && defined(CONFIG_MB7040) +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_MB7040_I2C_FREQUENCY +# define CONFIG_MB7040_I2C_FREQUENCY 400000 +#endif + /**************************************************************************** * Private Types ****************************************************************************/ @@ -66,6 +77,10 @@ struct mb7040_dev_s ****************************************************************************/ /* I2C Helpers */ +static int mb7040_i2c_write(FAR struct mb7040_dev_s *priv, + FAR const uint8_t *buffer, int buflen); +static int mb7040_i2c_read(FAR struct mb7040_dev_s *priv, + FAR uint8_t *buffer, int buflen); static int mb7040_measurerange(FAR struct mb7040_dev_s *priv); static int mb7040_readrange(FAR struct mb7040_dev_s *priv, FAR uint16_t *range); @@ -106,6 +121,50 @@ static const struct file_operations g_fops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: mb7040_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int mb7040_i2c_write(FAR struct mb7040_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_MB7040_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: mb7040_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int mb7040_i2c_read(FAR struct mb7040_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_MB7040_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: mb7040_measurerange * @@ -125,10 +184,10 @@ static int mb7040_measurerange(FAR struct mb7040_dev_s *priv) /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); + ret = mb7040_i2c_write(priv, ®addr, sizeof(regaddr)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); } return ret; @@ -151,10 +210,10 @@ static int mb7040_readrange(FAR struct mb7040_dev_s *priv, /* Read two bytes */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_READ(priv->i2c, buffer, sizeof(buffer)); + ret = mb7040_i2c_read(priv, buffer, sizeof(buffer)); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -192,10 +251,10 @@ static int mb7040_changeaddr(FAR struct mb7040_dev_s *priv, uint8_t addr) /* Write the register address followed by the data (no RESTART) */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, buffer, sizeof(buffer)); + ret = mb7040_i2c_write(priv, buffer, sizeof(buffer)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } diff --git a/drivers/sensors/mcp9844.c b/drivers/sensors/mcp9844.c index b8e5c90851..1c4b044e5d 100644 --- a/drivers/sensors/mcp9844.c +++ b/drivers/sensors/mcp9844.c @@ -5,6 +5,9 @@ * Copyright (C) 2015 DS-Automotion GmbH. All rights reserved. * Author: Alexander Entinger * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -51,7 +54,15 @@ #if defined(CONFIG_I2C) && defined(CONFIG_MCP9844) /**************************************************************************** - * Private + * Pre-process Definitions + ****************************************************************************/ + +#ifndef CONFIG_MCP9844_I2C_FREQUENCY +# define CONFIG_MCP9844_I2C_FREQUENCY 400000 +#endif + +/**************************************************************************** + * Private Types ****************************************************************************/ struct mcp9844_dev_s @@ -66,6 +77,10 @@ struct mcp9844_dev_s /* I2C helper functions */ +static int mcp9844_i2c_write(FAR struct mcp9844_dev_s *priv, + FAR const uint8_t *buffer, int buflen); +static int mcp9844_i2c_read(FAR struct mcp9844_dev_s *priv, + FAR uint8_t *buffer, int buflen); static int mcp9844_read_u16(FAR struct mcp9844_dev_s *priv, uint8_t const regaddr, FAR uint16_t *value); static int mcp9844_write_u16(FAR struct mcp9844_dev_s *priv, @@ -103,6 +118,50 @@ static const struct file_operations g_mcp9844_fops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: mcp9844_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int mcp9844_i2c_write(FAR struct mcp9844_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_MCP9844_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: mcp9844_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int mcp9844_i2c_read(FAR struct mcp9844_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_MCP9844_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: mcp9844_read_u16 * @@ -120,10 +179,10 @@ static int mcp9844_read_u16(FAR struct mcp9844_dev_s *priv, I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, 1); + ret = mcp9844_i2c_write(priv, ®addr, 1); if (ret < 0) { - sndbg ("I2C_WRITE failed: %d\n", ret); + sndbg ("i2c_write failed: %d\n", ret); return ret; } @@ -131,10 +190,10 @@ static int mcp9844_read_u16(FAR struct mcp9844_dev_s *priv, uint8_t const BUFFER_SIZE = 2; uint8_t buffer[BUFFER_SIZE]; - ret = I2C_READ(priv->i2c, buffer, BUFFER_SIZE); + ret = mcp9844_i2c_read(priv, buffer, BUFFER_SIZE); if (ret < 0) { - sndbg ("I2C_READ failed: %d\n", ret); + sndbg ("i2c_read failed: %d\n", ret); return ret; } @@ -173,7 +232,7 @@ static int mcp9844_write_u16(FAR struct mcp9844_dev_s *priv, I2C_SETADDRESS(priv->i2c, priv->addr, 7); - return I2C_WRITE(priv->i2c, buffer, BUFFER_SIZE); + return mcp9844_i2c_write(priv, buffer, BUFFER_SIZE); } /**************************************************************************** diff --git a/drivers/sensors/ms58xx.c b/drivers/sensors/ms58xx.c index 36c6ac07ef..a6e880d59e 100644 --- a/drivers/sensors/ms58xx.c +++ b/drivers/sensors/ms58xx.c @@ -6,6 +6,9 @@ * Author: Paul Alexander Patience * Updated by: Karim Keddam * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -56,6 +59,11 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ + +#ifndef CONFIG_MS58XX_I2C_FREQUENCY +# define CONFIG_MS58XX_I2C_FREQUENCY 400000 +#endif + /* Register Definitions *****************************************************/ /* Register Addresses */ @@ -135,6 +143,10 @@ static uint8_t ms58xx_crc(FAR uint16_t *src, uint8_t crcIndex); /* I2C Helpers */ +static int ms58xx_i2c_write(FAR struct ms58xx_dev_s *priv, + FAR const uint8_t *buffer, int buflen); +static int ms58xx_i2c_read(FAR struct ms58xx_dev_s *priv, + FAR uint8_t *buffer, int buflen); static int ms58xx_readu16(FAR struct ms58xx_dev_s *priv, uint8_t regaddr, FAR uint16_t *regval); static int ms58xx_readadc(FAR struct ms58xx_dev_s *priv, FAR uint32_t *adc); @@ -228,6 +240,50 @@ static uint8_t ms58xx_crc(FAR uint16_t *src, uint8_t crcIndex) return (n_rem ^ 0x00); } +/**************************************************************************** + * Name: ms58xx_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int ms58xx_i2c_write(FAR struct ms58xx_dev_s *priv, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_MS58XX_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_write(priv->i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: ms58xx_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int ms58xx_i2c_read(FAR struct ms58xx_dev_s *priv, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_MS58XX_I2C_FREQUENCY; + config.address = priv->addr; + config.addrlen = 7; + + return i2c_read(priv->i2c, &config, buffer, buflen); +} + /**************************************************************************** * Name: ms58xx_readu16 * @@ -247,19 +303,19 @@ static int ms58xx_readu16(FAR struct ms58xx_dev_s *priv, uint8_t regaddr, /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); + ret = ms58xx_i2c_write(priv, ®addr, sizeof(regaddr)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 16 bits from the register */ - ret = I2C_READ(priv->i2c, buffer, sizeof(buffer)); + ret = ms58xx_i2c_read(priv, buffer, sizeof(buffer)); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -288,19 +344,19 @@ static int ms58xx_readadc(FAR struct ms58xx_dev_s *priv, FAR uint32_t *adc) /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); + ret = ms58xx_i2c_write(priv, ®addr, sizeof(regaddr)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } /* Restart and read 24 bits from the register */ - ret = I2C_READ(priv->i2c, buffer, sizeof(buffer)); + ret = ms58xx_i2c_read(priv, buffer, sizeof(buffer)); if (ret < 0) { - sndbg("I2C_READ failed: %d\n", ret); + sndbg("i2c_read failed: %d\n", ret); return ret; } @@ -496,10 +552,10 @@ static int ms58xx_reset(FAR struct ms58xx_dev_s *priv) /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); + ret = ms58xx_i2c_write(priv, ®addr, sizeof(regaddr)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); return ret; } @@ -533,10 +589,10 @@ static int ms58xx_convert(FAR struct ms58xx_dev_s *priv, uint8_t regaddr, /* Write the register address */ I2C_SETADDRESS(priv->i2c, priv->addr, 7); - ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr)); + ret = ms58xx_i2c_write(priv, ®addr, sizeof(regaddr)); if (ret < 0) { - sndbg("I2C_WRITE failed: %d\n", ret); + sndbg("i2c_write failed: %d\n", ret); } /* Wait for the conversion to end */ diff --git a/drivers/timers/ds3231.c b/drivers/timers/ds3231.c index bff34b437a..32f87863a9 100644 --- a/drivers/timers/ds3231.c +++ b/drivers/timers/ds3231.c @@ -547,9 +547,8 @@ int up_rtc_settime(FAR const struct timespec *tp) I2C_SETFREQUENCY(g_ds3231.i2c, CONFIG_DS3231_I2C_FREQUENCY); - /* Perform the transfer (This could be done with I2C_READ). This transfer - * will be repeated if the seconds count rolls over to a smaller value - * while writing. + /* Perform the transfer. This transfer will be repeated if the seconds + * count rolls over to a smaller value while writing. */ do diff --git a/drivers/timers/pcf85263.c b/drivers/timers/pcf85263.c index d6318820ef..201179199a 100644 --- a/drivers/timers/pcf85263.c +++ b/drivers/timers/pcf85263.c @@ -511,9 +511,8 @@ int up_rtc_settime(FAR const struct timespec *tp) I2C_SETFREQUENCY(g_pcf85263.i2c, CONFIG_PCF85263_I2C_FREQUENCY); - /* Perform the transfer (This could be done with I2C_READ). This transfer - * will be repeated if the seconds count rolls over to a smaller value - * while writing. + /* Perform the transfer. This transfer will be repeated if the seconds + * count rolls over to a smaller value while writing. */ do diff --git a/drivers/video/ov2640.c b/drivers/video/ov2640.c index 67322ff3b7..04c431c24a 100644 --- a/drivers/video/ov2640.c +++ b/drivers/video/ov2640.c @@ -670,6 +670,51 @@ static const struct ovr2640_reg_s g_ov2640_jpeg_uxga_resolution[] = /**************************************************************************** * Private Functions ****************************************************************************/ + +/**************************************************************************** + * Name: ov2640_i2c_write + * + * Description: + * Write to the I2C device. + * + ****************************************************************************/ + +static int ov2640_i2c_write(FAR struct i2c_master_s *i2c, + FAR const uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_OV2640_FREQUENCY; + config.address = CONFIG_OV2640_I2CADDR; + config.addrlen = 7; + + return i2c_write(i2c, &config, buffer, buflen); +} + +/**************************************************************************** + * Name: ov2640_i2c_read + * + * Description: + * Read from the I2C device. + * + ****************************************************************************/ + +static int ov2640_i2c_read(FAR struct i2c_master_s *i2c, + FAR uint8_t *buffer, int buflen) +{ + struct i2c_config_s config; + + /* Set up the configuration and perform the write-read operation */ + + config.frequency = CONFIG_OV2640_FREQUENCY; + config.address = CONFIG_OV2640_I2CADDR; + config.addrlen = 7; + + return i2c_read(i2c, &config, buffer, buflen); +} + /**************************************************************************** * Function: ov2640_putreg * @@ -704,10 +749,10 @@ static int ov2640_putreg(FAR struct i2c_master_s *i2c, uint8_t regaddr, /* And do it */ - ret = I2C_WRITE(i2c, buffer, 2); + ret = ov2640_i2c_write(i2c, buffer, 2); if (ret < 0) { - gdbg("ERROR: I2C_WRITE failed: %d\n", ret); + gdbg("ERROR: i2c_write failed: %d\n", ret); return ret; } @@ -738,19 +783,19 @@ static uint8_t ov2640_getreg(FAR struct i2c_master_s *i2c, uint8_t regaddr) /* Write the register address */ - ret = I2C_WRITE(i2c, ®addr, 1); + ret = ov2640_i2c_write(i2c, ®addr, 1); if (ret < 0) { - gdbg("ERROR: I2C_WRITE failed: %d\n", ret); + gdbg("ERROR: i2c_write failed: %d\n", ret); return 0; } /* Restart and read 8-bits from the register */ - ret = I2C_READ(i2c, ®val, 1); + ret = ov2640_i2c_read(i2c, ®val, 1); if (ret < 0) { - gdbg("ERROR: I2C_READ failed: %d\n", ret); + gdbg("ERROR: i2c_read failed: %d\n", ret); return 0; } #ifdef CONFIG_OV2640_REGDEBUG diff --git a/include/nuttx/i2c/i2c_master.h b/include/nuttx/i2c/i2c_master.h index a8b91f8226..9ff3357a30 100644 --- a/include/nuttx/i2c/i2c_master.h +++ b/include/nuttx/i2c/i2c_master.h @@ -117,48 +117,6 @@ #define I2C_SETADDRESS(d,a,n) ((d)->ops->setaddress(d,a,n)) -/**************************************************************************** - * Name: I2C_WRITE - * - * Description: - * Send a block of data on I2C using the previously selected I2C - * frequency and slave address. Each write operational will be an 'atomic' - * operation in the sense that any other I2C actions will be serialized - * and pend until this write completes. Required. - * - * Input Parameters: - * dev - Device-specific state data - * buffer - A pointer to the read-only buffer of data to be written to device - * buflen - The number of bytes to send from the buffer - * - * Returned Value: - * 0: success, <0: A negated errno - * - ****************************************************************************/ - -#define I2C_WRITE(d,b,l) ((d)->ops->write(d,b,l)) - -/**************************************************************************** - * Name: I2C_READ - * - * Description: - * Receive a block of data from I2C using the previously selected I2C - * frequency and slave address. Each read operational will be an 'atomic' - * operation in the sense that any other I2C actions will be serialized - * and pend until this read completes. Required. - * - * Input Parameters: - * dev - Device-specific state data - * buffer - A pointer to a buffer of data to receive the data from the device - * buflen - The requested number of bytes to be read - * - * Returned Value: - * 0: success, <0: A negated errno - * - ****************************************************************************/ - -#define I2C_READ(d,b,l) ((d)->ops->read(d,b,l)) - /**************************************************************************** * Name: I2C_TRANSFER * @@ -166,12 +124,15 @@ * Perform a sequence of I2C transfers, each transfer is started with a * START and the final transfer is completed with a STOP. Each sequence * will be an 'atomic' operation in the sense that any other I2C actions - * will be serialized and pend until this read completes. Optional. + * will be serialized and pend until this sequence of transfers completes. + * Required. * * Input Parameters: - * dev - Device-specific state data - * msgs - A pointer to a set of message descriptors - * msgcount - The number of transfers to perform + * dev + + - Device-specific state data + * msgs - A pointer to a set of message descriptors + * count - The number of transfers to perform * * Returned Value: * The number of transfers completed @@ -191,13 +152,13 @@ struct i2c_msg_s; struct i2c_ops_s { uint32_t (*setfrequency)(FAR struct i2c_master_s *dev, uint32_t frequency); - int (*setaddress)(FAR struct i2c_master_s *dev, int addr, int nbits); - int (*write)(FAR struct i2c_master_s *dev, FAR const uint8_t *buffer, - int buflen); - int (*read)(FAR struct i2c_master_s *dev, FAR uint8_t *buffer, - int buflen); - int (*transfer)(FAR struct i2c_master_s *dev, FAR struct i2c_msg_s *msgs, - int count); + int (*setaddress)(FAR struct i2c_master_s *dev, int addr, int nbits); + int (*write)(FAR struct i2c_master_s *dev, FAR const uint8_t *buffer, + int buflen); + int (*read)(FAR struct i2c_master_s *dev, FAR uint8_t *buffer, + int buflen); + int (*transfer)(FAR struct i2c_master_s *dev, FAR struct i2c_msg_s *msgs, + int count); }; /* This structure contains the full state of I2C as needed for a specific