Justin_B
Justin_B

Reputation: 31

Linux IIO accelerometer motion dectection events

I have a MMA8563 accelerometer in an imx6ul board and am using the mma8452.c Linux IIO driver. I can access x, y, z raw values fine through /sys/bus/iio/devices/iio:device0.

However, I am unsure of how to use IIO events. I have configured motion detection in the accelerometer using the following commands:

# echo 15 > events/in_accel_mag_rising_value
# echo 1 > events/in_accel_mag_rising_period
# echo 1 > events/in_accel_x_mag_rising_en

I have placed debug print statements in the driver code and can see that the interrupt is being triggered when I move the device. I see that the driver then calls the iio_push_event() function. I can also see that the interrupt number in /proc/interrupts increments each time I move the device.

From user space, how can I get access to this interrupt or event? I would like to be able to tell when the device is in motion.

The driver creates a buffer and a char dev in /dev/iio:device0 but this seems to only populate when I enable either x, y, or z in the scan_elements directory.

Upvotes: 3

Views: 1044

Answers (1)

Tan En De
Tan En De

Reputation: 361

Applications can read events via a special file descriptor obtained from ioctl() requesting IIO_GET_EVENT_FD_IOCTL on the file descriptor of /dev/iio:devicex, something like this:

struct iio_event_data event;
int fd, event_fd;

fd = open("/dev/iio:devicex", 0);
ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
read(event_fd, &event, sizeof(event)); 
/* Instead of read(), may also use select() or poll() etc. */

Reference:
linux/tools/iio/iio_event_monitor.c

Upvotes: 0

Related Questions