From: Brendan Le Foll Date: Mon, 28 Sep 2015 15:26:53 +0000 (+0100) Subject: iio: initial API and enumeration of devices X-Git-Tag: v0.9.0~62 X-Git-Url: http://review.tizen.org/git/?p=contrib%2Fmraa.git;a=commitdiff_plain;h=d920d136eadc16af4ab34031e72d05c05cfcff40 iio: initial API and enumeration of devices Signed-off-by: Brendan Le Foll --- diff --git a/api/mraa/iio.h b/api/mraa/iio.h index c20cb8e..5bfb9ab 100644 --- a/api/mraa/iio.h +++ b/api/mraa/iio.h @@ -59,7 +59,16 @@ mraa_iio_context mraa_iio_init(int device); /** */ -mraa_result_t mraa_iio_read(mraa_iio_context dev, uint32_t *data, int length); +int mraa_iio_get_channel_count(mraa_iio_context dev); + +/** + */ +mraa_result_t mraa_iio_read(mraa_iio_context dev, int channel, const char* attribute, float* data); + +/** + * + */ +mraa_result_t mraa_iio_write(mraa_iio_context dev, int channel, const char* attribute); /** * De-inits an mraa_iio_context device diff --git a/include/mraa_internal_types.h b/include/mraa_internal_types.h index af7fd02..b5a495c 100644 --- a/include/mraa_internal_types.h +++ b/include/mraa_internal_types.h @@ -134,6 +134,7 @@ struct _uart { */ struct _iio { int num; /**< IIO device number */ + char* name; /**< IIO device name */ }; /** @@ -276,6 +277,8 @@ typedef struct _board_t { mraa_pininfo_t* pins; /**< Pointer to pin array */ mraa_adv_func_t* adv_func; /**< Pointer to advanced function disptach table */ struct _board_t* sub_platform; /**< Pointer to sub platform */ + struct _iio* iio_devices; /**< Pointer to IIO devices */ + uint8_t iio_device_count; /**< IIO device count */ /*@}*/ } mraa_board_t; diff --git a/src/iio/iio.c b/src/iio/iio.c index d5d3c3b..982a5dd 100644 --- a/src/iio/iio.c +++ b/src/iio/iio.c @@ -28,20 +28,36 @@ mraa_iio_context mraa_iio_init(int device) { - mraa_iio_context dev = (mraa_iio_context) calloc(1, sizeof(struct _iio)); - if (dev == NULL) { - syslog(LOG_CRIT, "iio: Failed to allocate memory for context"); + if (device > plat->iio_device_count) { return NULL; } + return &plat->iio_devices[device]; +} - return dev; +int +mraa_iio_get_channel_count(mraa_iio_context dev) +{ + return 1; } +mraa_result_t +mraa_iio_read(mraa_iio_context dev, int channel, const char* attribute, float* data) +{ + char buf[64]; + snprintf(buf, 64, "/sys/bus/iio/devices/iio:device%d/in_voltage%d_%s", dev->num, channel, attribute); + int fd = open(buf, O_RDONLY); + if (fd != -1) { + int len = read(fd, &buf, 64); + *data = strtol(buf, NULL, 10); + return MRAA_SUCCESS; + } + return MRAA_ERROR_UNSPECIFIED; +} mraa_result_t -mraa_iio_read(mraa_iio_context dev, uint32_t* data, int length) +mraa_iio_write(mraa_iio_context dev, int channel, const char* attribute) { - return 0; + return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED; } mraa_result_t diff --git a/src/mraa.c b/src/mraa.c index 9daa0f6..6e2b6be 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -52,6 +52,9 @@ mraa_board_t* plat = NULL; static char platform_name[MAX_PLATFORM_NAME_LENGTH]; mraa_adv_func_t* advance_func; +static int num_i2c_devices = 0; +static int num_iio_devices = 0; + const char* mraa_get_version() { @@ -70,6 +73,17 @@ mraa_set_log_level(int level) return MRAA_ERROR_INVALID_PARAMETER; } +static int +mraa_count_iio_devices(const char* path, const struct stat* sb, int flag, struct FTW* ftwb) +{ + switch (sb->st_mode & S_IFMT) { + case S_IFLNK: + num_iio_devices++; + break; + } + return 0; +} + #if (defined SWIGPYTHON) || (defined SWIG) mraa_result_t #else @@ -142,6 +156,30 @@ mraa_init() } #endif + // Now detect IIO devices, linux only + // find how many i2c buses we have if we haven't already + if (num_iio_devices == 0) { + if (nftw("/sys/bus/iio/devices", &mraa_count_iio_devices, 20, FTW_PHYS) == -1) { + return MRAA_ERROR_UNSPECIFIED; + } + } + char name[64], filepath[64]; + int fd, len, i; + plat->iio_device_count = num_iio_devices; + plat->iio_devices = calloc(num_iio_devices, sizeof(struct _iio)); + struct _iio* device; + for (i=0; i < num_iio_devices; i++) { + device = &plat->iio_devices[i]; + device->num = i; + snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d", i); + fd = open(filepath, O_RDONLY); + if (fd != -1) { + len = read(fd, &name, 64); + device->name = malloc((sizeof(char) * len) + sizeof(char)); + strncpy(device->name, name, len); + } + } + syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type()); return MRAA_SUCCESS; } @@ -625,10 +663,8 @@ mraa_link_targets(const char* filename, const char* targetname) } } -static int num_i2c_devices = 0; - static int -mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb) +mraa_count_i2c_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb) { switch (sb->st_mode & S_IFMT) { case S_IFLNK: @@ -654,7 +690,7 @@ mraa_find_i2c_bus(const char* devname, int startfrom) // find how many i2c buses we have if we haven't already if (num_i2c_devices == 0) { - if (nftw("/sys/class/i2c-dev/", &mraa_count_files, 20, FTW_PHYS) == -1) { + if (nftw("/sys/class/i2c-dev/", &mraa_count_i2c_files, 20, FTW_PHYS) == -1) { return -1; } } @@ -724,3 +760,22 @@ mraa_get_sub_platform_index(int pin_or_bus) { return pin_or_bus & (~MRAA_SUB_PLATFORM_MASK); } + +int +mraa_get_iio_device_count() +{ + return plat->iio_device_count; +} + +int +mraa_find_iio_device(const char* devicename) +{ + int i = 0; + for (i; i < plat->iio_device_count; i++) { +#if 0 + if (!strcmp() { + } +#endif + } + return 0; +}