From 428e1cde1fb3aa00734742b0491dd3a616c71c61 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 31 Dec 1999 19:32:00 -0500 Subject: Added I2C read/write support, on cape bus for now --- CMakeLists.txt | 2 +- i2c.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ i2c.h | 2 ++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 i2c.c create mode 100644 i2c.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fa99325..7b01906 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ find_package(Glib) find_package(GIO) include_directories(${GLIB_PKG_INCLUDE_DIRS}) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") -add_executable(instr-daemon instr-daemon.c signalobject.c response.c) +add_executable(instr-daemon instr-daemon.c signalobject.c response.c i2c.c) add_executable(instr-client instr-client.c) target_link_libraries(instr-daemon gio-2.0 gobject-2.0 glib-2.0) diff --git a/i2c.c b/i2c.c new file mode 100644 index 0000000..02e5812 --- /dev/null +++ b/i2c.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include + + +#define I2C_BUS "/dev/i2c-3" + + +int I2C_Write(int address, int value) { + + struct i2c_smbus_ioctl_data argswrite; + + argswrite.read_write = I2C_SMBUS_WRITE; + argswrite.size = I2C_SMBUS_BYTE; + argswrite.data = NULL; + argswrite.command = value; + + int device = open(I2C_BUS, O_RDWR); + + if (device == -1) return device; + + ioctl(device, I2C_SLAVE, address); + ioctl(device, I2C_SMBUS, &argswrite); + + close(device); +} + + +int I2C_Read(int address) { + + union i2c_smbus_data data; + struct i2c_smbus_ioctl_data argsread; + + argsread.read_write = I2C_SMBUS_READ; + argsread.size = I2C_SMBUS_BYTE; + argsread.data = &data; + argsread.command = 0; + + int device = open(I2C_BUS, O_RDWR); + + if (device == -1) return device; + + ioctl(device, I2C_SLAVE, address); + ioctl(device, I2C_SMBUS, &argsread); + + close(device); + + return data.byte; +} + diff --git a/i2c.h b/i2c.h new file mode 100644 index 0000000..5382c4e --- /dev/null +++ b/i2c.h @@ -0,0 +1,2 @@ +int I2C_Write(int address, int value); +int I2C_Read(int address); -- cgit