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_MOUNTING_MATRIX "mounting_matrix"
36 #define IIO_SLASH_DEV "/dev/" IIO_DEVICE
37 #define IIO_SYSFS_DEVICE "/sys/bus/iio/devices/" IIO_DEVICE
38 #define IIO_EVENTS "events"
39 #define IIO_CONFIGFS_TRIGGER "/sys/kernel/config/iio/triggers/"
42 mraa_iio_init(int device)
44 if (device > plat_iio->iio_device_count) {
48 mraa_iio_get_channel_data(&plat_iio->iio_devices[device]);
49 mraa_iio_get_event_data(&plat_iio->iio_devices[device]);
51 return &plat_iio->iio_devices[device];
55 mraa_iio_read_size(mraa_iio_context dev)
61 mraa_iio_get_channels(mraa_iio_context dev)
67 mraa_iio_get_channel_count(mraa_iio_context dev)
73 mraa_iio_get_channel_data(mraa_iio_context dev)
75 const struct dirent* ent;
84 char shortbuf, signchar;
88 memset(buf, 0, MAX_SIZE);
89 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM, dev->num);
92 while ((ent = readdir(dir)) != NULL) {
93 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
98 dev->chan_num = chan_num;
99 // no need proceed if no channel found
102 mraa_iio_channel* chan;
103 dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
105 while ((ent = readdir(dir)) != NULL) {
106 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
107 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
108 fd = open(buf, O_RDONLY);
110 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
113 chan_num = ((int) strtol(readbuf, NULL, 10));
114 chan = &dev->channels[chan_num];
115 chan->index = chan_num;
118 buf[(strlen(buf) - 5)] = '\0';
119 char* str = strdup(buf);
120 // grab the type of the buffer
121 snprintf(buf, MAX_SIZE, "%stype", str);
122 fd = open(buf, O_RDONLY);
124 read(fd, readbuf, 31 * sizeof(char));
125 ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf, &signchar, &chan->bits_used,
126 &padint, &chan->shift);
127 chan->bytes = padint / 8;
128 if (curr_bytes % chan->bytes == 0) {
129 chan->location = curr_bytes;
131 chan->location = curr_bytes - curr_bytes % chan->bytes + chan->bytes;
133 curr_bytes = chan->location + chan->bytes;
134 // probably should be 5?
139 return MRAA_IO_SETUP_FAILURE;
141 chan->signedd = (signchar == 's');
142 chan->lendian = (shortbuf == 'l');
143 if (chan->bits_used == 64) {
146 chan->mask = (1 << chan->bits_used) - 1;
150 // grab the enable flag of channel
151 snprintf(buf, MAX_SIZE, "%sen", str);
152 fd = open(buf, O_RDONLY);
154 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
155 syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
158 chan->enabled = (int) strtol(readbuf, NULL, 10);
159 // only calculate enable buffer size for trigger buffer extract data
161 dev->datasize += chan->bytes;
175 mraa_iio_get_device_name(mraa_iio_context dev)
181 mraa_iio_get_device_num_by_name(const char* name)
185 if (plat_iio == NULL) {
186 syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
191 syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
195 for (i = 0; i < plat_iio->iio_device_count; i++) {
197 device = &plat_iio->iio_devices[i];
198 // we want to check for exact match
199 if (strncmp(device->name, name, strlen(device->name) + 1) == 0) {
208 mraa_iio_read_float(mraa_iio_context dev, const char* filename, float* data)
211 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
212 FILE* fp = fopen(buf, "r");
214 fscanf(fp, "%f\n", data);
218 return MRAA_ERROR_UNSPECIFIED;
222 mraa_iio_read_integer(mraa_iio_context dev, const char* filename, int* data)
225 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
226 FILE* fp = fopen(buf, "r");
228 fscanf(fp, "%d\n", data);
232 return MRAA_ERROR_UNSPECIFIED;
236 mraa_iio_read_string(mraa_iio_context dev, const char* filename, char* data)
239 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
240 FILE* fp = fopen(buf, "r");
242 fscanf(fp, "%s\n", data);
246 return MRAA_ERROR_UNSPECIFIED;
250 mraa_iio_write_float(mraa_iio_context dev, const char* filename, const float data)
253 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
254 FILE* fp = fopen(buf, "w");
256 fprintf(fp, "%f", data);
260 return MRAA_ERROR_UNSPECIFIED;
264 mraa_iio_write_integer(mraa_iio_context dev, const char* filename, const int data)
267 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
268 FILE* fp = fopen(buf, "w");
270 fprintf(fp, "%d", data);
274 return MRAA_ERROR_UNSPECIFIED;
278 mraa_iio_write_string(mraa_iio_context dev, const char* filename, const char* data)
281 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
282 FILE* fp = fopen(buf, "w");
284 fprintf(fp, "%s", data);
288 return MRAA_ERROR_UNSPECIFIED;
292 mraa_iio_wait_event(int fd, char* data, int* read_size)
297 return MRAA_ERROR_INVALID_RESOURCE;
303 // Wait for it forever or until pthread_cancel
304 // poll is a cancelable point like sleep()
305 int x = poll(&pfd, 1, -1);
307 memset(data, 0, 100);
308 *read_size = read(fd, data, 100);
314 mraa_iio_trigger_handler(void* arg)
316 mraa_iio_context dev = (mraa_iio_context) arg;
318 char data[MAX_SIZE * 100];
322 if (mraa_iio_wait_event(dev->fp, &data[0], &read_size) == MRAA_SUCCESS) {
323 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
324 // only can process if readsize >= enabled channel's datasize
325 for (i = 0; i < (read_size / dev->datasize); i++) {
328 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
330 // we must have got an error code so die nicely
331 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
338 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
341 if (dev->thread_id != 0) {
342 return MRAA_ERROR_NO_RESOURCES;
345 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
346 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
348 return MRAA_ERROR_INVALID_RESOURCE;
352 pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
358 mraa_iio_get_event_data(mraa_iio_context dev)
360 const struct dirent* ent;
369 char shortbuf, signchar;
370 memset(buf, 0, MAX_SIZE);
371 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
374 while ((ent = readdir(dir)) != NULL) {
375 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
379 dev->event_num = event_num;
380 // no need proceed if no event found
383 mraa_iio_event* event;
384 dev->events = calloc(event_num, sizeof(mraa_iio_event));
385 if (dev->events == NULL) {
387 return MRAA_ERROR_UNSPECIFIED;
391 while ((ent = readdir(dir)) != NULL) {
392 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
393 event = &dev->events[event_num];
394 event->name = strdup(ent->d_name);
395 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
396 fd = open(buf, O_RDONLY);
398 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
403 event->enabled = ((int) strtol(readbuf, NULL, 10));
404 // Todo, read other event info.
414 mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data)
419 return MRAA_ERROR_INVALID_RESOURCE;
425 // Wait for it forever or until pthread_cancel
426 // poll is a cancelable point like sleep()
427 int x = poll(&pfd, 1, -1);
429 read(fd, data, sizeof(struct iio_event_data));
435 mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
442 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
444 ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
447 if (ret == -1 || event_fd == -1)
448 return MRAA_ERROR_UNSPECIFIED;
450 ret = read(event_fd, data, sizeof(struct iio_event_data));
457 mraa_iio_event_handler(void* arg)
459 struct iio_event_data data;
460 mraa_iio_context dev = (mraa_iio_context) arg;
463 if (mraa_iio_event_poll_nonblock(dev->fp_event, &data) == MRAA_SUCCESS) {
464 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
465 dev->isr_event(&data);
466 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
468 // we must have got an error code so die nicely
469 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
476 mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
480 if (dev->thread_id != 0) {
481 return MRAA_ERROR_NO_RESOURCES;
484 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
485 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
487 return MRAA_ERROR_INVALID_RESOURCE;
489 ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
492 if (ret == -1 || dev->fp_event == -1) {
493 return MRAA_ERROR_UNSPECIFIED;
496 dev->isr_event = fptr;
497 pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
503 mraa_iio_event_extract_event(struct iio_event_data* event,
512 *chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
513 *modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
514 *type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
515 *direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
516 *channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
517 *channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
518 *different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
523 mraa_iio_get_mounting_matrix(mraa_iio_context dev, float mm[9])
528 memset(buf, 0, MAX_SIZE);
529 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_MOUNTING_MATRIX, dev->num);
530 fp = fopen(buf, "r");
532 fscanf(fp, "%f %f %f\n%f %f %f\n%f %f %f\n", &mm[0], &mm[1], &mm[2], &mm[3], &mm[4], &mm[5],
533 &mm[6], &mm[7], &mm[8]);
537 return MRAA_ERROR_UNSPECIFIED;
541 mraa_iio_create_trigger(mraa_iio_context dev, const char* trigger)
543 struct stat configfs_status;
544 struct stat trigger_status;
547 if (stat(IIO_CONFIGFS_TRIGGER, &configfs_status) == 0) {
548 memset(buf, 0, MAX_SIZE);
549 snprintf(buf, MAX_SIZE, IIO_CONFIGFS_TRIGGER "%s", trigger);
550 if (stat(buf, &trigger_status) != 0) {
551 if (mkdir(buf, configfs_status.st_mode) == 0)
554 // trigger folder already created
559 return MRAA_ERROR_UNSPECIFIED;
563 // does stop make any sense on iio devices?
565 mraa_iio_stop(mraa_iio_context dev)