Skip to content

Commit

Permalink
gpio: Add mraa_get_gpiochip_base
Browse files Browse the repository at this point in the history
This allows to retrieve the GPIO base using a stable device path as
found in sysfs. We cannot rely on the absolute GPIO numbers because
Linux prefers dynamic numbering. So, different probe ordering or
additional GPIO chips can ruin our day if we just use hard-coded
numbers.

This is a building block to enable upstream kernels on Galileo Gen2
and IOT2000.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
  • Loading branch information
jan-kiszka committed Jun 2, 2017
1 parent 79da382 commit 034d787
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/mraa_internal.h
Expand Up @@ -179,6 +179,14 @@ uint32_t mraa_add_from_lockfile(const char* imraa_lock_file);
mraa_result_t imraa_init();
#endif

/**
* helper function to find the gpiochip base corresponding to a device path
*
* @param device_path sysfs path to device
* @return GPIO base number or -1
*/
int mraa_get_gpiochip_base(const char* device_path);

#ifdef __cplusplus
}
#endif
26 changes: 26 additions & 0 deletions src/gpio/gpio.c
Expand Up @@ -40,6 +40,32 @@
#define MAX_SIZE 64
#define POLL_TIMEOUT

int mraa_get_gpiochip_base(const char* device_path)
{
char *base_file;
char buf[1024];
FILE* fh;
int ret;

snprintf(buf, sizeof(buf) - 1, "%s/gpio/gpiochip*/base", device_path);

This comment has been minimized.

Copy link
@andy-shev

andy-shev Apr 26, 2019

It should be derived other way around, i.e. via /sys/class/gpio or /dev/gpio (later can be done using https://github.com/brgl/libgpiod).

This comment has been minimized.

Copy link
@jan-kiszka

jan-kiszka Apr 27, 2019

Author Collaborator

None of them gives you information about which gpipchip is what. And that's the whole purpose of this function: linking a gpiochip instance to its (stable) topological address.

This comment has been minimized.

Copy link
@andy-shev

andy-shev May 6, 2019

I think I stand corrected. I commented on the other patch with ideas how it can be achieved.

base_file = mraa_file_unglob(buf);
if (!base_file)
return -1;

fh = fopen(base_file, "r");
if (!fh)
return -1;

ret = fread(buf, 1, sizeof(buf) - 1, fh);
fclose(fh);

if (ret < 0)
return -1;

buf[ret] = 0;
return strtol(buf, NULL, 10);
}

static mraa_result_t
mraa_gpio_get_valfp(mraa_gpio_context dev)
{
Expand Down

0 comments on commit 034d787

Please sign in to comment.