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_get_device_num_by_name(const char* name)
171 if (plat_iio == NULL) {
172 syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
177 syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
181 for (i = 0; i < plat_iio->iio_device_count; i++) {
183 device = &plat_iio->iio_devices[i];
184 // we want to check for exact match
185 if (strncmp(device->name, name, strlen(device->name)+1) == 0) {
194 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
197 snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
198 int fd = open(buf, O_RDONLY);
200 int len = read(fd, &buf, 64);
201 *data = strtol(buf, NULL, 10);
204 return MRAA_ERROR_UNSPECIFIED;
208 mraa_iio_write(mraa_iio_context dev, const char* attr_chan)
210 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
214 mraa_iio_wait_event(int fd, char* data)
219 return MRAA_ERROR_INVALID_RESOURCE;
225 // Wait for it forever or until pthread_cancel
226 // poll is a cancelable point like sleep()
227 int x = poll(&pfd, 1, -1);
229 memset(data, 0, 100);
236 mraa_iio_trigger_handler(void* arg)
238 mraa_iio_context dev = (mraa_iio_context) arg;
239 char data[MAX_SIZE*100];
242 if (mraa_iio_wait_event(dev->fp, &data[0]) == MRAA_SUCCESS) {
243 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
245 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
247 // we must have got an error code so die nicely
248 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
255 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
258 if (dev->thread_id != 0) {
259 return MRAA_ERROR_NO_RESOURCES;
262 sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
263 dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
265 return MRAA_ERROR_INVALID_RESOURCE;
269 pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
275 // does stop make any sense on iio devices?
277 mraa_iio_stop(mraa_iio_context dev)