Go to file
Thomas Klaehn 7da732b5e1 Add Readme
2019-07-29 14:00:17 +02:00
.vscode create library 2019-07-25 10:26:27 +02:00
inc rename gop_sys.h to gpio.h 2019-07-29 13:59:54 +02:00
src gpio_sys: simplify api. 2019-07-29 12:23:49 +02:00
test Initial commit 2019-07-25 08:45:43 +01:00
.gitignore Initial commit 2019-07-25 08:45:43 +01:00
Makefile Make: add distclean target. 2019-07-29 13:38:32 +02:00
README.md Add Readme 2019-07-29 14:00:17 +02:00

GPIO_SYS

Gpio driver using sys file systeme.g.:

echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction
echo "1"> /sys/class/gpio/gpio4/value
echo "4" > /sys/class/gpio/unexport

Installation

make install

Installs libgpio_sys.a and libgpio_sys.so in the directory $(PREFIX)/lib/. It also installs the include header file gpio.h in the directory $(PREFIX)/include/gpio_sys/. The default value for PREFIX is: PREFIX=usr. To use another location for installation changing the value of the PREFIX variable is needed. E.g.:

PREFIX=/usr/local/ make install

Uninstallation

make install

Removes the files installed with the install make target.

NOTE: If PREFIX variable was changed during installation process the same value needs to be set for uninstallation. E.g.:

PREFIX=/usr/local/ make install

Example

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <syslog.h>
    #include <stdbool.h>
    #include <gpio_sys/gpio.h>


    static const struct gpio_sys gpio_4 = {
        .pin = 4,
        .direction = 0, // out
    };

    int main(void)
    {
        int res, cnt;
        int value;

        openlog("gpio_sys", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);

        res = gpio_open(&gpio_4);
        if(res != EXIT_SUCCESS) {
            syslog(LOG_ERR, "Unable to open gpio %d\n", gpio_4.pin);
            return res;
        }

        gpio_write(&gpio_4, 1);
        for(cnt = 0; cnt < 10; cnt++) {
            if(cnt % 2) {
                gpio_write(&gpio_4, 1);
            } else {
                gpio_write(&gpio_4, 0);
            }
            value = gpio_read(&gpio_4);
            if(value < 0) {
                syslog(LOG_ERR, "Unable to read from gpio %d\n", gpio_4.pin);
                return EXIT_FAILURE;
            } else {
                printf("Gpio 4: %d\n", value);
            }
            sleep(1);
        }
        gpio_close(&gpio_4);

        return EXIT_SUCCESS;
    }

Additional make targets

Target Meaning
all Build the code, assemble the static and shared library.
install Install the libraries and necessary include header files.
uninstall Uninstall the libraries and necessary include header files.
clean Clean up build artifacts.
build_unit_test Build the unit tests.
exec_unit_test Execute the unit tests.
coverage Determine code coverage of the unit tests.
check Static code analysis (cppcheck).