From 7c4f9eb8e921fe6fc973056e6bbd1652e45b1047 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 2 Feb 2016 10:27:50 -0600 Subject: [PATCH] I2CTOOL: Now uses the I2C driver instead of direct calls into the OS --- system/i2c/Kconfig | 9 +-- system/i2c/Makefile | 7 +- system/i2c/README.txt | 29 ++----- system/i2c/i2c_bus.c | 39 ++-------- system/i2c/i2c_dev.c | 41 +++------- system/i2c/i2c_devif.c | 171 +++++++++++++++++++++++++++++++++++++++++ system/i2c/i2c_get.c | 46 +++-------- system/i2c/i2c_main.c | 8 -- system/i2c/i2c_set.c | 46 +++-------- system/i2c/i2c_verf.c | 38 ++------- system/i2c/i2ctool.h | 18 +++-- 11 files changed, 242 insertions(+), 210 deletions(-) create mode 100644 system/i2c/i2c_devif.c diff --git a/system/i2c/Kconfig b/system/i2c/Kconfig index 34ebb509a..6671626bd 100644 --- a/system/i2c/Kconfig +++ b/system/i2c/Kconfig @@ -8,6 +8,7 @@ menuconfig SYSTEM_I2CTOOL bool "I2C tool" default n depends on I2C + select I2C_DRIVER ---help--- Enable support for the I2C tool. @@ -16,27 +17,23 @@ if SYSTEM_I2CTOOL config I2CTOOL_MINBUS int "Minimum bus number" default 0 - depends on SYSTEM_I2CTOOL ---help--- Smallest bus index supported by the hardware (default 0). config I2CTOOL_MAXBUS int "Maximum bus number" - depends on SYSTEM_I2CTOOL default 3 ---help--- Largest bus index supported by the hardware (default 3) config I2CTOOL_MINADDR hex "Minimum I2C address" - depends on SYSTEM_I2CTOOL default 0x03 ---help--- Minium 7-bit device address (default: 0x03) config I2CTOOL_MAXADDR hex "Maximum I2C address" - depends on SYSTEM_I2CTOOL default 0x77 ---help--- Largest 7-bit device address (default: 0x77) @@ -44,15 +41,13 @@ config I2CTOOL_MAXADDR config I2CTOOL_MAXREGADDR hex "Maximum I2C register address" default 0xff - depends on SYSTEM_I2CTOOL ---help--- Largest I2C register address (default: 0xff) config I2CTOOL_DEFFREQ int "Default I2C frequency" default 400000 - depends on SYSTEM_I2CTOOL ---help--- Default I2C frequency (default: 400000) -endif +endif # SYSTEM_I2CTOOL diff --git a/system/i2c/Makefile b/system/i2c/Makefile index cab06eda2..c9160c9c8 100644 --- a/system/i2c/Makefile +++ b/system/i2c/Makefile @@ -1,7 +1,7 @@ ############################################################################ # apps/system/i2c/Makefile # -# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. +# Copyright (C) 2011-2012, 2016 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -39,8 +39,9 @@ include $(APPDIR)/Make.defs # I2C tool -ASRCS = -CSRCS = i2c_bus.c i2c_common.c i2c_dev.c i2c_get.c i2c_set.c i2c_verf.c +ASRCS = +CSRCS = i2c_bus.c i2c_common.c i2c_dev.c i2c_get.c i2c_set.c i2c_verf.c +CSRCS += i2c_devif.c MAINSRC = i2c_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) diff --git a/system/i2c/README.txt b/system/i2c/README.txt index 99219bbae..b51011b42 100644 --- a/system/i2c/README.txt +++ b/system/i2c/README.txt @@ -29,17 +29,6 @@ CONTENTS System Requirements =================== -I2C Driver ----------- -In order to use the I2C driver, you system -- in particular, your I2C driver -- -must meet certain requirements: - -1. It support calling up_i2cinitialize() numerous times, resetting the I2C - hardware on each (initial) time. up_i2cuninitialize() will be called after - each call to up_i2cinitialize() to free any resources and disable the I2C. -2. up_i2cinitialize must accept any interface number without crashing. It - must simply return NULL if the device is not supported. - The I2C tool is designed to be implemented as a NuttShell (NSH) add-on. Read the apps/nshlib/README.txt file for information about add-ons. @@ -350,18 +339,16 @@ The I2C tools requires the following in your NuttX configuration: CONFIG_SYSTEM_I2C=y -2. Device-specific I2C support must be enabled. The I2C tool will call the - platform-specific function up_i2cinitialize() to get instances of the - I2C interface and the platform-specific function up_i2cuninitialize() - to discard instances of the I2C interface. +2. Device-specific I2C driver support must be enabled: - NOTE 1: The I2C interface is defined in include/nuttx/i2c/i2c_master.h. + CONFIG_I2C_DRIVER=y - NOTE 2: This I2C tool uses direct I2C device interfaces. As such, it - relies on internal OS interfaces that are not normally available to a - user-space program. As a result, the I2C tool cannot be used if a - NuttX is built as a protected, supervisor kernel (CONFIG_BUILD_PROTECTED - or CONFIG_BUILD_KERNEL). + The I2C tool will then use the I2C character driver to access the I2C + bus. These devices will reside at /dev/i2cN where N is the I2C bus + number. + + NOTE 1: The I2C driver ioctl interface is defined in + include/nuttx/i2c/i2c_master.h. I2C Tool Configuration Options ------------------------------ diff --git a/system/i2c/i2c_bus.c b/system/i2c/i2c_bus.c index 735bfab18..e2e972875 100644 --- a/system/i2c/i2c_bus.c +++ b/system/i2c/i2c_bus.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/system/i2c/i2c_bus.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -43,30 +43,6 @@ #include "i2ctool.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -77,21 +53,18 @@ int i2ccmd_bus(FAR struct i2ctool_s *i2ctool, int argc, char **argv) { - FAR struct i2c_master_s *dev; - int i; + int bus; i2ctool_printf(i2ctool, " BUS EXISTS?\n"); - for (i = CONFIG_I2CTOOL_MINBUS; i <= CONFIG_I2CTOOL_MAXBUS; i++) + for (bus = CONFIG_I2CTOOL_MINBUS; bus <= CONFIG_I2CTOOL_MAXBUS; bus++) { - dev = up_i2cinitialize(i); - if (dev) + if (i2cdev_exists(bus)) { - i2ctool_printf(i2ctool, "Bus %d: YES\n", i); - (void)up_i2cuninitialize(dev); + i2ctool_printf(i2ctool, "Bus %d: YES\n", bus); } else { - i2ctool_printf(i2ctool, "Bus %d: NO\n", i); + i2ctool_printf(i2ctool, "Bus %d: NO\n", bus); } } diff --git a/system/i2c/i2c_dev.c b/system/i2c/i2c_dev.c index 645ca1740..284fc8ba8 100644 --- a/system/i2c/i2c_dev.c +++ b/system/i2c/i2c_dev.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/system/i2c/i2c_dev.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -45,30 +45,6 @@ #include "i2ctool.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -79,7 +55,6 @@ int i2ccmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv) { - FAR struct i2c_master_s *dev; struct i2c_msg_s msg[2]; FAR char *ptr; union @@ -95,6 +70,7 @@ int i2ccmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv) int nargs; int argndx; int ret; + int fd; int i; int j; @@ -150,8 +126,8 @@ int i2ccmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv) /* Get a handle to the I2C bus */ - dev = up_i2cinitialize(i2ctool->bus); - if (!dev) + fd = i2cdev_open(i2ctool, i2ctool->bus); + if (fd < 0) { i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); return ERROR; @@ -200,15 +176,15 @@ int i2ccmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv) if (i2ctool->start) { - ret = I2C_TRANSFER(dev, &msg[0], 1); + ret = i2cdev_transfer(i2ctool, fd, &msg[0], 1); if (ret == OK) { - ret = I2C_TRANSFER(dev, &msg[1], 1); + ret = i2cdev_transfer(i2ctool, fd, &msg[1], 1); } } else { - ret = I2C_TRANSFER(dev, msg, 2); + ret = i2cdev_transfer(i2ctool, fd, msg, 2); } if (ret == OK) @@ -220,10 +196,11 @@ int i2ccmd_dev(FAR struct i2ctool_s *i2ctool, int argc, char **argv) i2ctool_printf(i2ctool, "-- "); } } + i2ctool_printf(i2ctool, "\n"); i2ctool_flush(i2ctool); } - (void)up_i2cuninitialize(dev); + (void)close(fd); return OK; } diff --git a/system/i2c/i2c_devif.c b/system/i2c/i2c_devif.c new file mode 100644 index 000000000..fb5384be2 --- /dev/null +++ b/system/i2c/i2c_devif.c @@ -0,0 +1,171 @@ +/**************************************************************************** + * apps/system/i2c/i2c_devif.c + * + * 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: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include + +#include "i2ctool.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Device naming */ + +#define DEVNAME_FMT "/dev/i2c%d" +#define DEVNAME_FMTLEN (8 + 3 + 1) + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static char g_devname[DEVNAME_FMTLEN]; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: i2cdev_path + ****************************************************************************/ + +FAR char *i2cdev_path(int bus) +{ + snprintf(g_devname, DEVNAME_FMTLEN, DEVNAME_FMT, bus); + return g_devname; +} + +/**************************************************************************** + * Name: i2cdev_exists + ****************************************************************************/ + +bool i2cdev_exists(int bus) +{ + struct stat buf; + FAR char *devpath; + int ret; + + /* Get the device path */ + + devpath = i2cdev_path(bus); + + /* Check if something exists at that path */ + + ret = stat(devpath, &buf); + if (ret >= 0) + { + /* Return TRUE only if it is a character driver */ + + if (S_ISCHR(buf.st_mode)) + { + return true; + } + } + + return false; +} + +/**************************************************************************** + * Name: i2cdev_open + ****************************************************************************/ + +int i2cdev_open(FAR struct i2ctool_s *i2ctool, int bus) +{ + FAR char *devpath; + int fd; + + /* Get the device path */ + + devpath = i2cdev_path(bus); + + /* Open the file for read-only access (we need only IOCTLs) */ + + fd = open(devpath, O_RDONLY); + if (fd < 0) + { + int errcode = errno; + + /* We failed to open the driver */ + + i2ctool_printf(i2ctool, "ERROR: Failed to open %s: %d\n", + devpath, errcode); + return ERROR; + } + + return fd; +} + +/**************************************************************************** + * Name: i2cdev_transfer + ****************************************************************************/ + +int i2cdev_transfer(FAR struct i2ctool_s *i2ctool, int fd, + FAR struct i2c_msg_s *msgv, int msgc) +{ + struct i2c_transfer_s xfer; + int ret; + + /* Set up the IOCTL argument */ + + xfer.msgv = msgv; + xfer.msgc = msgc; + + /* Perform the IOCTL */ + + ret = ioctl(fd, I2CIOC_TRANSFER, (unsigned long)((uintptr_t)&xfer)); + if (ret < 0) + { + int errcode = errno; + + /* We failed to open the driver */ + + i2ctool_printf(i2ctool, "ERROR: ioctl(I2CIOC_TRANSFER) failed: %d\n", + errcode); + return ERROR; + } + + return OK; +} \ No newline at end of file diff --git a/system/i2c/i2c_get.c b/system/i2c/i2c_get.c index 55edc17d4..70777abde 100644 --- a/system/i2c/i2c_get.c +++ b/system/i2c/i2c_get.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/system/i2c/i2c_get.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -45,30 +45,6 @@ #include "i2ctool.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -79,7 +55,6 @@ int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) { - FAR struct i2c_master_s *dev; FAR char *ptr; uint16_t result; uint8_t regaddr; @@ -87,6 +62,7 @@ int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) int nargs; int argndx; int ret; + int fd; int i; /* Parse any command line arguments */ @@ -137,8 +113,8 @@ int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) /* Get a handle to the I2C bus */ - dev = up_i2cinitialize(i2ctool->bus); - if (!dev) + fd = i2cdev_open(i2ctool, i2ctool->bus); + if (fd < 0) { i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); return ERROR; @@ -153,7 +129,7 @@ int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) { /* Read from the I2C bus */ - ret = i2ctool_get(i2ctool, dev, regaddr, &result); + ret = i2ctool_get(i2ctool, fd, regaddr, &result); /* Display the result */ @@ -185,7 +161,7 @@ int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) } } - (void)up_i2cuninitialize(dev); + (void)close(fd); return ret; } @@ -193,8 +169,8 @@ int i2ccmd_get(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) * Name: i2ctool_get ****************************************************************************/ -int i2ctool_get(FAR struct i2ctool_s *i2ctool, FAR struct i2c_master_s *dev, - uint8_t regaddr, uint16_t *result) +int i2ctool_get(FAR struct i2ctool_s *i2ctool, int fd, uint8_t regaddr, + FAR uint16_t *result) { struct i2c_msg_s msg[2]; union @@ -229,15 +205,15 @@ int i2ctool_get(FAR struct i2ctool_s *i2ctool, FAR struct i2c_master_s *dev, if (i2ctool->start) { - ret = I2C_TRANSFER(dev, &msg[0], 1); + ret = i2cdev_transfer(i2ctool, fd, &msg[0], 1); if (ret== OK) { - ret = I2C_TRANSFER(dev, &msg[1], 1); + ret = i2cdev_transfer(i2ctool, fd, &msg[1], 1); } } else { - ret = I2C_TRANSFER(dev, msg, 2); + ret = i2cdev_transfer(i2ctool, fd, msg, 2); } /* Return the result of the read operation */ diff --git a/system/i2c/i2c_main.c b/system/i2c/i2c_main.c index 2c7f50727..b9c9fa259 100644 --- a/system/i2c/i2c_main.c +++ b/system/i2c/i2c_main.c @@ -52,14 +52,6 @@ #include "i2ctool.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ diff --git a/system/i2c/i2c_set.c b/system/i2c/i2c_set.c index 81fbbb200..b5cad99b1 100644 --- a/system/i2c/i2c_set.c +++ b/system/i2c/i2c_set.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/system/i2c/i2c_set.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -45,30 +45,6 @@ #include "i2ctool.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -79,7 +55,6 @@ int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) { - FAR struct i2c_master_s *dev; FAR char *ptr; uint8_t regaddr; long value; @@ -87,6 +62,7 @@ int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) int nargs; int argndx; int ret; + int fd; int i; /* Parse any command line arguments */ @@ -165,8 +141,8 @@ int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) /* Get a handle to the I2C bus */ - dev = up_i2cinitialize(i2ctool->bus); - if (!dev) + fd = i2cdev_open(i2ctool, i2ctool->bus); + if (fd < 0) { i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); return ERROR; @@ -181,7 +157,7 @@ int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) { /* Write to the I2C bus */ - ret = i2ctool_set(i2ctool, dev, regaddr, (uint16_t)value); + ret = i2ctool_set(i2ctool, fd, regaddr, (uint16_t)value); /* Display the result */ @@ -212,7 +188,7 @@ int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) } } - (void)up_i2cuninitialize(dev); + (void)close(fd); return ret; } @@ -220,8 +196,8 @@ int i2ccmd_set(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) * Name: i2ctool_set ****************************************************************************/ -int i2ctool_set(FAR struct i2ctool_s *i2ctool, FAR struct i2c_master_s *dev, - uint8_t regaddr, uint16_t value) +int i2ctool_set(FAR struct i2ctool_s *i2ctool, int fd, uint8_t regaddr, + uint16_t value) { struct i2c_msg_s msg[2]; union @@ -257,15 +233,15 @@ int i2ctool_set(FAR struct i2ctool_s *i2ctool, FAR struct i2c_master_s *dev, if (i2ctool->start) { - ret = I2C_TRANSFER(dev, &msg[0], 1); + ret = i2cdev_transfer(i2ctool, fd, &msg[0], 1); if (ret == OK) { - ret = I2C_TRANSFER(dev, &msg[1], 1); + ret = i2cdev_transfer(i2ctool, fd, &msg[1], 1); } } else { - ret = I2C_TRANSFER(dev, msg, 2); + ret = i2cdev_transfer(i2ctool, fd, msg, 2); } return ret; diff --git a/system/i2c/i2c_verf.c b/system/i2c/i2c_verf.c index 66dc0faf1..d5bd8339a 100644 --- a/system/i2c/i2c_verf.c +++ b/system/i2c/i2c_verf.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/system/i2c/i2c_verf.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -45,30 +45,6 @@ #include "i2ctool.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -79,7 +55,6 @@ int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) { - FAR struct i2c_master_s *dev; FAR char *ptr; uint16_t rdvalue; uint8_t regaddr; @@ -89,6 +64,7 @@ int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) int nargs; int argndx; int ret; + int fd; int i; /* Parse any command line arguments */ @@ -167,8 +143,8 @@ int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) /* Get a handle to the I2C bus */ - dev = up_i2cinitialize(i2ctool->bus); - if (!dev) + fd = i2cdev_open(i2ctool, i2ctool->bus); + if (fd < 0) { i2ctool_printf(i2ctool, "Failed to get bus %d\n", i2ctool->bus); return ERROR; @@ -192,12 +168,12 @@ int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) /* Write to the I2C bus */ - ret = i2ctool_set(i2ctool, dev, regaddr, (uint16_t)wrvalue); + ret = i2ctool_set(i2ctool, fd, regaddr, (uint16_t)wrvalue); if (ret == OK) { /* Read the value back from the I2C bus */ - ret = i2ctool_get(i2ctool, dev, regaddr, &rdvalue); + ret = i2ctool_get(i2ctool, fd, regaddr, &rdvalue); } /* Display the result */ @@ -239,6 +215,6 @@ int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv) } } - (void)up_i2cuninitialize(dev); + (void)close(fd); return ret; } diff --git a/system/i2c/i2ctool.h b/system/i2c/i2ctool.h index c9dac3184..da3997178 100644 --- a/system/i2c/i2ctool.h +++ b/system/i2c/i2ctool.h @@ -1,7 +1,7 @@ /**************************************************************************** * apps/system/i2c/i2ctool.h * - * Copyright (C) 2011, 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2014, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -192,10 +192,10 @@ int i2ccmd_verf(FAR struct i2ctool_s *i2ctool, int argc, FAR char **argv); /* I2C access functions */ -int i2ctool_get(FAR struct i2ctool_s *i2ctool, FAR struct i2c_master_s *dev, - uint8_t addr, uint16_t *result); -int i2ctool_set(FAR struct i2ctool_s *i2ctool, FAR struct i2c_master_s *dev, - uint8_t regaddr, uint16_t value); +int i2ctool_get(FAR struct i2ctool_s *i2ctool, int fd, uint8_t addr, + FAR uint16_t *result); +int i2ctool_set(FAR struct i2ctool_s *i2ctool, int fd, uint8_t regaddr, + uint16_t value); /* Common logic */ @@ -204,4 +204,12 @@ int arg_string(FAR char **arg, FAR char **value); int arg_decimal(FAR char **arg, FAR long *value); int arg_hex(FAR char **arg, FAR long *value); +/* Driver access utilities */ + +FAR char *i2cdev_path(int bus); +bool i2cdev_exists(int bus); +int i2cdev_open(FAR struct i2ctool_s *i2ctool, int bus); +int i2cdev_transfer(FAR struct i2ctool_s *i2ctool, int fd, + FAR struct i2c_msg_s *msgv, int msgc); + #endif /* __APPS_SYSTEM_I2C_I2CTOOLS_H */