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
38 mraa_iio_init(int device)
40 if (device > plat_iio->iio_device_count) {
44 mraa_iio_get_channel_data(&plat_iio->iio_devices[device]);
45 return &plat_iio->iio_devices[device];
49 mraa_iio_read_size(mraa_iio_context dev)
55 mraa_iio_get_channels(mraa_iio_context dev)
61 mraa_iio_get_channel_count(mraa_iio_context dev)
67 mraa_iio_get_channel_data(mraa_iio_context dev)
69 const struct dirent *ent;
78 char shortbuf, signchar;
79 memset(buf, 0, MAX_SIZE);
80 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM, dev->num);
83 while ((ent = readdir(dir)) != NULL) {
84 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
89 dev->chan_num = chan_num;
90 mraa_iio_channel* chan;
91 dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
93 while ((ent = readdir(dir)) != NULL) {
94 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
95 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
96 fd = open(buf, O_RDONLY);
98 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
101 chan_num = ((int) strtol(readbuf, NULL, 10));
102 chan = &dev->channels[chan_num];
103 chan->index = chan_num;
106 buf[(strlen(buf)-5)] = '\0';
107 char* str = strdup(buf);
108 // grab the type of the buffer
109 snprintf(buf, MAX_SIZE, "%stype", str);
110 fd = open(buf, O_RDONLY);
112 read(fd, readbuf, 31 * sizeof(char));
113 ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf,
114 &signchar, &chan->bits_used,
115 &padint, &chan->shift);
116 chan->bytes = padint / 8;
117 if (curr_bytes % chan->bytes == 0) {
118 chan->location = curr_bytes;
120 chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
122 curr_bytes = chan->location + chan->bytes;
123 // probably should be 5?
128 return MRAA_IO_SETUP_FAILURE;
130 chan->signedd = (signchar == 's');
131 chan->lendian = (shortbuf == 'l');
132 if (chan->bits_used == 64) {
135 chan->mask = (1 << chan->bits_used) - 1;
139 // grab the enable flag of channel
140 snprintf(buf, MAX_SIZE, "%sen", str);
141 fd = open(buf, O_RDONLY);
143 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
144 syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
147 chan->enabled = (int) strtol(readbuf, NULL, 10);
155 dev->datasize = curr_bytes;
161 mraa_iio_get_device_name(mraa_iio_context dev)
167 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
170 snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
171 int fd = open(buf, O_RDONLY);
173 int len = read(fd, &buf, 64);
174 *data = strtol(buf, NULL, 10);
177 return MRAA_ERROR_UNSPECIFIED;
181 mraa_iio_write(mraa_iio_context dev, const char* attr_chan)
183 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
187 mraa_iio_wait_event(int fd, char* data)
192 return MRAA_ERROR_INVALID_RESOURCE;
198 // Wait for it forever or until pthread_cancel
199 // poll is a cancelable point like sleep()
200 int x = poll(&pfd, 1, -1);
202 memset(data, 0, 100);
209 mraa_iio_trigger_handler(void* arg)
211 mraa_iio_context dev = (mraa_iio_context) arg;
212 char data[MAX_SIZE*100];
215 if (mraa_iio_wait_event(dev->fp, &data[0]) == MRAA_SUCCESS) {
216 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
218 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
220 // we must have got an error code so die nicely
221 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
228 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
231 if (dev->thread_id != 0) {
232 return MRAA_ERROR_NO_RESOURCES;
235 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
236 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
238 return MRAA_ERROR_INVALID_RESOURCE;
242 pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
248 // does stop make any sense on iio devices?
250 mraa_iio_stop(mraa_iio_context dev)