forked from eclipse/mraa
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
79da382
commit 034d787
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jan-kiszka
Author
Collaborator
|
||
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) | ||
{ | ||
|
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).