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 mraa_iio_channel* chan;
94 dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
96 while ((ent = readdir(dir)) != NULL) {
97 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
98 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
99 fd = open(buf, O_RDONLY);
101 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
104 chan_num = ((int) strtol(readbuf, NULL, 10));
105 chan = &dev->channels[chan_num];
106 chan->index = chan_num;
109 buf[(strlen(buf)-5)] = '\0';
110 char* str = strdup(buf);
111 // grab the type of the buffer
112 snprintf(buf, MAX_SIZE, "%stype", str);
113 fd = open(buf, O_RDONLY);
115 read(fd, readbuf, 31 * sizeof(char));
116 ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf,
117 &signchar, &chan->bits_used,
118 &padint, &chan->shift);
119 chan->bytes = padint / 8;
120 if (curr_bytes % chan->bytes == 0) {
121 chan->location = curr_bytes;
123 chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
125 curr_bytes = chan->location + chan->bytes;
126 // probably should be 5?
131 return MRAA_IO_SETUP_FAILURE;
133 chan->signedd = (signchar == 's');
134 chan->lendian = (shortbuf == 'l');
135 if (chan->bits_used == 64) {
138 chan->mask = (1 << chan->bits_used) - 1;
142 // grab the enable flag of channel
143 snprintf(buf, MAX_SIZE, "%sen", str);
144 fd = open(buf, O_RDONLY);
146 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
147 syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
150 chan->enabled = (int) strtol(readbuf, NULL, 10);
158 dev->datasize = curr_bytes;
164 mraa_iio_get_device_name(mraa_iio_context dev)
170 mraa_iio_get_device_num_by_name(const char* name)
174 if (plat_iio == NULL) {
175 syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
180 syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
184 for (i = 0; i < plat_iio->iio_device_count; i++) {
186 device = &plat_iio->iio_devices[i];
187 // we want to check for exact match
188 if (strncmp(device->name, name, strlen(device->name)+1) == 0) {
197 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
200 snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
201 int fd = open(buf, O_RDONLY);
203 int len = read(fd, &buf, 64);
204 *data = strtol(buf, NULL, 10);
207 return MRAA_ERROR_UNSPECIFIED;
211 mraa_iio_write(mraa_iio_context dev, const char* attr_chan)
213 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
217 mraa_iio_wait_event(int fd, char* data)
222 return MRAA_ERROR_INVALID_RESOURCE;
228 // Wait for it forever or until pthread_cancel
229 // poll is a cancelable point like sleep()
230 int x = poll(&pfd, 1, -1);
232 memset(data, 0, 100);
239 mraa_iio_trigger_handler(void* arg)
241 mraa_iio_context dev = (mraa_iio_context) arg;
242 char data[MAX_SIZE*100];
245 if (mraa_iio_wait_event(dev->fp, &data[0]) == MRAA_SUCCESS) {
246 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
248 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
250 // we must have got an error code so die nicely
251 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
258 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
261 if (dev->thread_id != 0) {
262 return MRAA_ERROR_NO_RESOURCES;
265 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
266 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
268 return MRAA_ERROR_INVALID_RESOURCE;
272 pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
278 mraa_iio_get_event_data(mraa_iio_context dev)
280 const struct dirent *ent;
289 char shortbuf, signchar;
290 memset(buf, 0, MAX_SIZE);
291 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
294 while ((ent = readdir(dir)) != NULL) {
295 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
299 dev->event_num = event_num;
300 mraa_iio_event* event;
301 dev->events = calloc(event_num, sizeof(mraa_iio_event));
302 if ( dev->events == NULL)
305 return MRAA_ERROR_UNSPECIFIED;
309 while ((ent = readdir(dir)) != NULL)
311 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
312 event = &dev->events[event_num];
313 event->name = strdup(ent->d_name);
314 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
315 fd = open(buf, O_RDONLY);
317 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
322 event->enabled = ((int) strtol(readbuf, NULL, 10));
323 //Todo, read other event info.
333 mraa_iio_event_read(mraa_iio_context dev, const char* attribute, float* data)
336 snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
337 int fd = open(buf, O_RDONLY);
339 int len = read(fd, &buf, 64);
340 *data = strtol(buf, NULL, 10);
343 return MRAA_ERROR_UNSPECIFIED;
347 mraa_iio_event_write(mraa_iio_context dev, const char* attribute, const char* data)
351 snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
352 int fd = open(buf, O_WRONLY);
354 int len = write(fd, data, ( strlen(data) +1 ));
357 return MRAA_ERROR_UNSPECIFIED;
361 mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data)
366 return MRAA_ERROR_INVALID_RESOURCE;
372 // Wait for it forever or until pthread_cancel
373 // poll is a cancelable point like sleep()
374 int x = poll(&pfd, 1, -1);
376 read(fd, data, sizeof(struct iio_event_data));
382 mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
389 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
391 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
394 if (ret == -1 || event_fd == -1)
395 return MRAA_ERROR_UNSPECIFIED;
397 ret = read(event_fd, data, sizeof(struct iio_event_data));
404 mraa_iio_event_handler(void* arg)
406 struct iio_event_data data;
407 mraa_iio_context dev = (mraa_iio_context) arg;
410 if (mraa_iio_event_poll_nonblock(dev->fp_event, &data) == MRAA_SUCCESS) {
411 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
412 dev->isr_event(&data);
413 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
415 // we must have got an error code so die nicely
416 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
423 mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
427 if (dev->thread_id != 0) {
428 return MRAA_ERROR_NO_RESOURCES;
431 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
432 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
434 return MRAA_ERROR_INVALID_RESOURCE;
436 ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
439 if (ret == -1 || dev->fp_event == -1)
441 return MRAA_ERROR_UNSPECIFIED;
444 dev->isr_event = fptr;
445 pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
451 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)
453 *chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
454 *modifier= IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
455 *type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
456 *direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
457 *channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
458 *channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
459 *different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
463 // does stop make any sense on iio devices?
465 mraa_iio_stop(mraa_iio_context dev)