summaryrefslogtreecommitdiff
path: root/i2c.c
blob: 4f1e7cfa976e1d1c4d4a1c5ee9564e93685b50d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "globals.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <glib.h>


#define I2C_BUS "/dev/i2c-3"


void I2C_Write(gulong address, guchar 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) {
		g_print_debug("ERROR: could not open I2C bus %s\n", I2C_BUS);
		return;
	}

	ioctl(device, I2C_SLAVE, address);
	ioctl(device, I2C_SMBUS, &argswrite);

	close(device);
}


guchar I2C_Read(gulong 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 0;
	}

	ioctl(device, I2C_SLAVE, address);
	ioctl(device, I2C_SMBUS, &argsread);

	close(device);

	return data.byte;
}