Hardware Reference
In-Depth Information
131 /
∗
132
Make sure the driver supports plain I2C I/O:
∗
133
/
134 rc = ioctl(i2c_fd,I2C_FUNCS,&i2c_funcs);
135 assert(rc >= 0) ;
136 assert(i2c_funcs & I2C_FUNC_I2C);
137 }
138
139 /
∗
∗
140
Close the I2C driver :
∗
141
/
142 static void
143 i2c_close(void) {
144 close(i2c_fd);
145 i2c_fd = -1;
146 }
147
148 /
∗
End i2c_funcs.c
/
∗
∗
Module sysgpio.c
The
sysgpio.c
module performs some grunt work in making the
/sys/class/gpio17/
value
node available and configuring it. This node is opened for reading, so that
poll(2)
can be called upon it.
The interesting code in this module is found in lines 89 to 106, where
gpio_poll()
is
defined. The file descriptor passed to it as
fd
is the
/sys/class/gpio17/value
file that is
•
Configured as input
•
The
poll(2)
system call in line 99 blocks the execution of the program until the
input (
GPIO 17
) changes from a high state to a low state. This is connected to the
MCP23017
INT A
pin, so it can tell us when its GPIO extender input(s) have changed.
The
poll(2)
system call can return an error if the program has handled a signal. The
error returned will be
EINTR
when this happens (as discussed in Chapter 9 of
Raspberry
Pi Hardware Reference
[Apress, 2014], section “Error EINTR”). If the program detects that
^C
has been pressed (
is_signaled
is true), then it exits, returning
-1
, to allow the main
program to exit.
A value of
rc=1
is returned if
/sys/class/gpio17/value
has a changed value to be
read. Before returning from
gpio_poll()
, a
read(2)
of any unread data is performed.
This is necessary so that the next call to
poll(2)
will block until new data is available.
Triggered on the falling edge (high-to-low transition)