2 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3 * Copyright (c) 2015 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "mraa_internal.h"
33 #define IIO_DEVICE "iio:device"
34 #define IIO_SCAN_ELEM "scan_elements"
35 #define IIO_SLASH_DEV "/dev/" IIO_DEVICE
36 #define IIO_SYSFS_DEVICE "/sys/bus/iio/devices/" IIO_DEVICE
37 #define IIO_EVENTS "events"
40 mraa_iio_init(int device)
42 if (device > plat_iio->iio_device_count) {
46 mraa_iio_get_channel_data(&plat_iio->iio_devices[device]);
47 mraa_iio_get_event_data(&plat_iio->iio_devices[device]);
49 return &plat_iio->iio_devices[device];
53 mraa_iio_read_size(mraa_iio_context dev)
59 mraa_iio_get_channels(mraa_iio_context dev)
65 mraa_iio_get_channel_count(mraa_iio_context dev)
71 mraa_iio_get_channel_data(mraa_iio_context dev)
73 const struct dirent* ent;
82 char shortbuf, signchar;
86 memset(buf, 0, MAX_SIZE);
87 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM, dev->num);
90 while ((ent = readdir(dir)) != NULL) {
91 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
96 dev->chan_num = chan_num;
97 // no need proceed if no channel found
100 mraa_iio_channel* chan;
101 dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
103 while ((ent = readdir(dir)) != NULL) {
104 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
105 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
106 fd = open(buf, O_RDONLY);
108 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
111 chan_num = ((int) strtol(readbuf, NULL, 10));
112 chan = &dev->channels[chan_num];
113 chan->index = chan_num;
116 buf[(strlen(buf) - 5)] = '\0';
117 char* str = strdup(buf);
118 // grab the type of the buffer
119 snprintf(buf, MAX_SIZE, "%stype", str);
120 fd = open(buf, O_RDONLY);
122 read(fd, readbuf, 31 * sizeof(char));
123 ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf, &signchar, &chan->bits_used,
124 &padint, &chan->shift);
125 chan->bytes = padint / 8;
126 if (curr_bytes % chan->bytes == 0) {
127 chan->location = curr_bytes;
129 chan->location = curr_bytes - curr_bytes % chan->bytes + chan->bytes;
131 curr_bytes = chan->location + chan->bytes;
132 // probably should be 5?
137 return MRAA_IO_SETUP_FAILURE;
139 chan->signedd = (signchar == 's');
140 chan->lendian = (shortbuf == 'l');
141 if (chan->bits_used == 64) {
144 chan->mask = (1 << chan->bits_used) - 1;
148 // grab the enable flag of channel
149 snprintf(buf, MAX_SIZE, "%sen", str);
150 fd = open(buf, O_RDONLY);
152 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
153 syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
156 chan->enabled = (int) strtol(readbuf, NULL, 10);
157 // only calculate enable buffer size for trigger buffer extract data
159 dev->datasize += chan->bytes;
173 mraa_iio_get_device_name(mraa_iio_context dev)
179 mraa_iio_get_device_num_by_name(const char* name)
183 if (plat_iio == NULL) {
184 syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
189 syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
193 for (i = 0; i < plat_iio->iio_device_count; i++) {
195 device = &plat_iio->iio_devices[i];
196 // we want to check for exact match
197 if (strncmp(device->name, name, strlen(device->name) + 1) == 0) {
206 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
209 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
210 int fd = open(buf, O_RDONLY);
212 int len = read(fd, &buf, MAX_SIZE);
213 *data = strtol(buf, NULL, 10);
216 return MRAA_ERROR_UNSPECIFIED;
220 mraa_iio_write(mraa_iio_context dev, const char* attr_chan, const char* data)
223 snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
224 int fd = open(buf, O_WRONLY);
226 write(fd, data, (strlen(data) + 1));
229 return MRAA_ERROR_UNSPECIFIED;
233 mraa_iio_wait_event(int fd, char* data, int* read_size)
238 return MRAA_ERROR_INVALID_RESOURCE;
244 // Wait for it forever or until pthread_cancel
245 // poll is a cancelable point like sleep()
246 int x = poll(&pfd, 1, -1);
248 memset(data, 0, 100);
249 *read_size = read(fd, data, 100);
255 mraa_iio_trigger_handler(void* arg)
257 mraa_iio_context dev = (mraa_iio_context) arg;
259 char data[MAX_SIZE * 100];
263 if (mraa_iio_wait_event(dev->fp, &data[0], &read_size) == MRAA_SUCCESS) {
264 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
265 // only can process if readsize >= enabled channel's datasize
266 for (i = 0; i < (read_size / dev->datasize); i++) {
269 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
271 // we must have got an error code so die nicely
272 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
279 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
282 if (dev->thread_id != 0) {
283 return MRAA_ERROR_NO_RESOURCES;
286 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
287 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
289 return MRAA_ERROR_INVALID_RESOURCE;
293 pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
299 mraa_iio_get_event_data(mraa_iio_context dev)
301 const struct dirent* ent;
310 char shortbuf, signchar;
311 memset(buf, 0, MAX_SIZE);
312 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
315 while ((ent = readdir(dir)) != NULL) {
316 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
320 dev->event_num = event_num;
321 // no need proceed if no event found
324 mraa_iio_event* event;
325 dev->events = calloc(event_num, sizeof(mraa_iio_event));
326 if (dev->events == NULL) {
328 return MRAA_ERROR_UNSPECIFIED;
332 while ((ent = readdir(dir)) != NULL) {
333 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
334 event = &dev->events[event_num];
335 event->name = strdup(ent->d_name);
336 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
337 fd = open(buf, O_RDONLY);
339 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
344 event->enabled = ((int) strtol(readbuf, NULL, 10));
345 // Todo, read other event info.
355 mraa_iio_event_read(mraa_iio_context dev, const char* attribute, float* data)
358 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
359 int fd = open(buf, O_RDONLY);
361 int len = read(fd, &buf, MAX_SIZE);
362 *data = strtol(buf, NULL, 10);
365 return MRAA_ERROR_UNSPECIFIED;
369 mraa_iio_event_write(mraa_iio_context dev, const char* attribute, const char* data)
373 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
374 int fd = open(buf, O_WRONLY);
376 int len = write(fd, data, (strlen(data) + 1));
379 return MRAA_ERROR_UNSPECIFIED;
383 mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data)
388 return MRAA_ERROR_INVALID_RESOURCE;
394 // Wait for it forever or until pthread_cancel
395 // poll is a cancelable point like sleep()
396 int x = poll(&pfd, 1, -1);
398 read(fd, data, sizeof(struct iio_event_data));
404 mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
411 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
413 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
416 if (ret == -1 || event_fd == -1)
417 return MRAA_ERROR_UNSPECIFIED;
419 ret = read(event_fd, data, sizeof(struct iio_event_data));
426 mraa_iio_event_handler(void* arg)
428 struct iio_event_data data;
429 mraa_iio_context dev = (mraa_iio_context) arg;
432 if (mraa_iio_event_poll_nonblock(dev->fp_event, &data) == MRAA_SUCCESS) {
433 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
434 dev->isr_event(&data);
435 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
437 // we must have got an error code so die nicely
438 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
445 mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
449 if (dev->thread_id != 0) {
450 return MRAA_ERROR_NO_RESOURCES;
453 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
454 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
456 return MRAA_ERROR_INVALID_RESOURCE;
458 ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
461 if (ret == -1 || dev->fp_event == -1) {
462 return MRAA_ERROR_UNSPECIFIED;
465 dev->isr_event = fptr;
466 pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
472 mraa_iio_event_extract_event(struct iio_event_data* event,
481 *chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
482 *modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
483 *type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
484 *direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
485 *channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
486 *channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
487 *different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
491 // does stop make any sense on iio devices?
493 mraa_iio_stop(mraa_iio_context dev)