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"
32 #define IIO_DEVICE "iio:device"
33 #define IIO_SCAN_ELEM "scan_elements"
34 #define IIO_SLASH_DEV "/dev/"IIO_DEVICE
35 #define IIO_SYSFS_DEVICE "/sys/bus/iio/devices/"IIO_DEVICE
36 #define IIO_EVENTS "events"
39 mraa_iio_init(int device)
41 if (device > plat_iio->iio_device_count) {
45 mraa_iio_get_channel_data(&plat_iio->iio_devices[device]);
46 mraa_iio_get_event_data(&plat_iio->iio_devices[device]);
48 return &plat_iio->iio_devices[device];
52 mraa_iio_read_size(mraa_iio_context dev)
58 mraa_iio_get_channels(mraa_iio_context dev)
64 mraa_iio_get_channel_count(mraa_iio_context dev)
70 mraa_iio_get_channel_data(mraa_iio_context dev)
72 const struct dirent *ent;
81 char shortbuf, signchar;
82 memset(buf, 0, MAX_SIZE);
83 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM, dev->num);
86 while ((ent = readdir(dir)) != NULL) {
87 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
92 dev->chan_num = chan_num;
93 //no need proceed if no channel found
96 mraa_iio_channel* chan;
97 dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
99 while ((ent = readdir(dir)) != NULL) {
100 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
101 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
102 fd = open(buf, O_RDONLY);
104 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
107 chan_num = ((int) strtol(readbuf, NULL, 10));
108 chan = &dev->channels[chan_num];
109 chan->index = chan_num;
112 buf[(strlen(buf)-5)] = '\0';
113 char* str = strdup(buf);
114 // grab the type of the buffer
115 snprintf(buf, MAX_SIZE, "%stype", str);
116 fd = open(buf, O_RDONLY);
118 read(fd, readbuf, 31 * sizeof(char));
119 ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf,
120 &signchar, &chan->bits_used,
121 &padint, &chan->shift);
122 chan->bytes = padint / 8;
123 if (curr_bytes % chan->bytes == 0) {
124 chan->location = curr_bytes;
126 chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
128 curr_bytes = chan->location + chan->bytes;
129 // probably should be 5?
134 return MRAA_IO_SETUP_FAILURE;
136 chan->signedd = (signchar == 's');
137 chan->lendian = (shortbuf == 'l');
138 if (chan->bits_used == 64) {
141 chan->mask = (1 << chan->bits_used) - 1;
145 // grab the enable flag of channel
146 snprintf(buf, MAX_SIZE, "%sen", str);
147 fd = open(buf, O_RDONLY);
149 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
150 syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
153 chan->enabled = (int) strtol(readbuf, NULL, 10);
161 dev->datasize = curr_bytes;
167 mraa_iio_get_device_name(mraa_iio_context dev)
173 mraa_iio_get_device_num_by_name(const char* name)
177 if (plat_iio == NULL) {
178 syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
183 syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
187 for (i = 0; i < plat_iio->iio_device_count; i++) {
189 device = &plat_iio->iio_devices[i];
190 // we want to check for exact match
191 if (strncmp(device->name, name, strlen(device->name)+1) == 0) {
200 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
203 snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
204 int fd = open(buf, O_RDONLY);
206 int len = read(fd, &buf, 64);
207 *data = strtol(buf, NULL, 10);
210 return MRAA_ERROR_UNSPECIFIED;
214 mraa_iio_write(mraa_iio_context dev, const char* attr_chan, const char* data)
217 snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
218 int fd = open(buf, O_WRONLY);
220 write(fd, data, (strlen(data)+1));
223 return MRAA_ERROR_UNSPECIFIED;
227 mraa_iio_wait_event(int fd, char* data)
232 return MRAA_ERROR_INVALID_RESOURCE;
238 // Wait for it forever or until pthread_cancel
239 // poll is a cancelable point like sleep()
240 int x = poll(&pfd, 1, -1);
242 memset(data, 0, 100);
249 mraa_iio_trigger_handler(void* arg)
251 mraa_iio_context dev = (mraa_iio_context) arg;
252 char data[MAX_SIZE*100];
255 if (mraa_iio_wait_event(dev->fp, &data[0]) == MRAA_SUCCESS) {
256 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
258 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
260 // we must have got an error code so die nicely
261 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
268 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
271 if (dev->thread_id != 0) {
272 return MRAA_ERROR_NO_RESOURCES;
275 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
276 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
278 return MRAA_ERROR_INVALID_RESOURCE;
282 pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
288 mraa_iio_get_event_data(mraa_iio_context dev)
290 const struct dirent *ent;
299 char shortbuf, signchar;
300 memset(buf, 0, MAX_SIZE);
301 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
304 while ((ent = readdir(dir)) != NULL) {
305 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
309 dev->event_num = event_num;
310 //no need proceed if no event found
313 mraa_iio_event* event;
314 dev->events = calloc(event_num, sizeof(mraa_iio_event));
315 if ( dev->events == NULL)
318 return MRAA_ERROR_UNSPECIFIED;
322 while ((ent = readdir(dir)) != NULL)
324 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
325 event = &dev->events[event_num];
326 event->name = strdup(ent->d_name);
327 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
328 fd = open(buf, O_RDONLY);
330 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
335 event->enabled = ((int) strtol(readbuf, NULL, 10));
336 //Todo, read other event info.
346 mraa_iio_event_read(mraa_iio_context dev, const char* attribute, float* data)
349 snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
350 int fd = open(buf, O_RDONLY);
352 int len = read(fd, &buf, 64);
353 *data = strtol(buf, NULL, 10);
356 return MRAA_ERROR_UNSPECIFIED;
360 mraa_iio_event_write(mraa_iio_context dev, const char* attribute, const char* data)
364 snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
365 int fd = open(buf, O_WRONLY);
367 int len = write(fd, data, ( strlen(data) +1 ));
370 return MRAA_ERROR_UNSPECIFIED;
374 mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data)
379 return MRAA_ERROR_INVALID_RESOURCE;
385 // Wait for it forever or until pthread_cancel
386 // poll is a cancelable point like sleep()
387 int x = poll(&pfd, 1, -1);
389 read(fd, data, sizeof(struct iio_event_data));
395 mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
402 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
404 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
407 if (ret == -1 || event_fd == -1)
408 return MRAA_ERROR_UNSPECIFIED;
410 ret = read(event_fd, data, sizeof(struct iio_event_data));
417 mraa_iio_event_handler(void* arg)
419 struct iio_event_data data;
420 mraa_iio_context dev = (mraa_iio_context) arg;
423 if (mraa_iio_event_poll_nonblock(dev->fp_event, &data) == MRAA_SUCCESS) {
424 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
425 dev->isr_event(&data);
426 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
428 // we must have got an error code so die nicely
429 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
436 mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
440 if (dev->thread_id != 0) {
441 return MRAA_ERROR_NO_RESOURCES;
444 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
445 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
447 return MRAA_ERROR_INVALID_RESOURCE;
449 ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
452 if (ret == -1 || dev->fp_event == -1)
454 return MRAA_ERROR_UNSPECIFIED;
457 dev->isr_event = fptr;
458 pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
464 mraa_iio_event_extract_event(struct iio_event_data* event, int* chan_type, int* modifier, int* type, int* direction, int* channel, int* channel2, int* different)
466 *chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
467 *modifier= IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
468 *type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
469 *direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
470 *channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
471 *channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
472 *different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
476 // does stop make any sense on iio devices?
478 mraa_iio_stop(mraa_iio_context dev)