Hardware Reference
In-Depth Information
87 /
88
Read 16- bits of data from peripheral :
89
/
90 int
91 i2c_read16(int addr,int reg) {
92 struct i2c_rdwr_ioctl_data msgset;
93 struct i2c_msg iomsgs[2];
94 unsigned char buf[1], rbuf [2];
95 int rc;
96
97 buf[0] = (unsigned char)reg;
98
99 iomsgs[0].addr = iomsgs[1].addr = (unsigned)addr;
100 iomsgs[0].flags = 0; /
Write
/
101 iomsgs[0].buf = buf;
102 iomsgs[0].len = 1;
103
104 iomsgs[1].flags = I2C_M_RD;
105 iomsgs[1].buf = rbuf; /
Read
/
106 iomsgs[1].len = 2;
107
108 msgset.msgs = iomsgs;
109 msgset.nmsgs = 2;
110
111 if ( (rc = ioctl(i2c_fd,I2C_RDWR,&msgset)) < 0 )
112 return -1;
113 return (rbuf[0] << 8) | rbuf[1];
114 }
115
116 /
117
Open I2C bus and check capabilities :
118
/
119 static void
120 i2c_init(const char
node) {
121 int rc;
122
123 i2c_fd = open(node,O_RDWR); /
Open driver /dev/i2s-1
/
124 if ( i2c_fd < 0 ) {
125 perror("Opening /dev/i2s-1");
126 puts("Check that the i2c dev & i2c-bcm2708 kernel
modules "
127 "are loaded.");
128 abort();
129 }
130
 
Search WWH ::




Custom Search