Hardware Reference
In-Depth Information
The C API for these I2C functions are described in Chapter 12 of Raspberry Pi
Hardware Reference (Apress, 2014).
1 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2
i2c_funcs.c : I2C Access Functions
3
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
4
5 static int i2c_fd = -1; /
Device node : /dev/i2c-1
/
6 static unsigned long i2c_funcs = 0; /
Support flags
/
7
8 /
9
Write 8 bits to I2C bus peripheral:
10
/
11 int
12 i2c_write8(int addr,int reg,int byte) {
13 struct i2c_rdwr_ioctl_data msgset;
14 struct i2c_msg iomsgs[1];
15 unsigned char buf[2];
16 int rc;
17
18 buf[0] = (unsigned char)reg; /
MCP23017 register no.
/
19 buf[1] = (unsigned char)byte; /
Byte to write to register
/
20
21 iomsgs[0].addr = (unsigned)addr;
22 iomsgs[0].flags = 0; /
Write
/
23 iomsgs[0].buf = buf;
24 iomsgs[0].len = 2;
25
26 msgset.msgs = iomsgs;
27 msgset.nmsgs = 1;
28
29 rc = ioctl(i2c_fd,I2C_RDWR,&msgset);
30 return rc < 0 ? -1 : 0;
31 }
32
33 /
34
Write 16 bits to Peripheral at address :
35
/
36 int
37 i2c_write16(int addr, int reg, int value) {
38 struct i2c_rdwr_ioctl_data msgset;
39 struct i2c_msg iomsgs[1];
40 unsigned char buf[3];
41 int rc;
42
 
Search WWH ::




Custom Search