summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt2
-rw-r--r--i2c.c53
-rw-r--r--i2c.h2
3 files changed, 56 insertions, 1 deletions
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 <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
+
+
+#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);