Compare commits
4 Commits
e2c5431bfa
...
1.0
Author | SHA1 | Date | |
---|---|---|---|
|
42c902ea52 | ||
|
8cb066d3dc | ||
|
7da732b5e1 | ||
|
9d22af507d |
100
README.md
Normal file
100
README.md
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
# GPIO_SYS
|
||||||
|
|
||||||
|
Gpio driver using sys file systeme.g.:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
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
|
||||||
|
|
||||||
|
```shell
|
||||||
|
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.:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
PREFIX=/usr/local/ make install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Uninstallation
|
||||||
|
|
||||||
|
```shell
|
||||||
|
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.:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
PREFIX=/usr/local/ make install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```C
|
||||||
|
#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. |
|
||||||
|
| distclean | Remove all directories created while any build step. |
|
||||||
|
| 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). |
|
@@ -9,7 +9,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <gpio_sys.h>
|
#include <gpio.h>
|
||||||
|
|
||||||
#define IN 0
|
#define IN 0
|
||||||
#define OUT 1
|
#define OUT 1
|
||||||
@@ -22,6 +22,7 @@ static int gpio_export(int pin);
|
|||||||
static int gpio_unexport(int pin);
|
static int gpio_unexport(int pin);
|
||||||
static int gpio_set_direction(int pin, int dir);
|
static int gpio_set_direction(int pin, int dir);
|
||||||
|
|
||||||
|
// cppcheck-suppress unusedFunction
|
||||||
int gpio_open(const struct gpio_sys *gpio)
|
int gpio_open(const struct gpio_sys *gpio)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
@@ -41,6 +42,7 @@ int gpio_open(const struct gpio_sys *gpio)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cppcheck-suppress unusedFunction
|
||||||
int gpio_close(const struct gpio_sys *gpio)
|
int gpio_close(const struct gpio_sys *gpio)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
@@ -58,6 +60,7 @@ int gpio_close(const struct gpio_sys *gpio)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cppcheck-suppress unusedFunction
|
||||||
int gpio_direction(struct gpio_sys *gpio, int direction)
|
int gpio_direction(struct gpio_sys *gpio, int direction)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
@@ -76,6 +79,7 @@ int gpio_direction(struct gpio_sys *gpio, int direction)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cppcheck-suppress unusedFunction
|
||||||
int gpio_read(const struct gpio_sys *gpio)
|
int gpio_read(const struct gpio_sys *gpio)
|
||||||
{
|
{
|
||||||
char path[VALUE_MAX];
|
char path[VALUE_MAX];
|
||||||
@@ -99,6 +103,7 @@ int gpio_read(const struct gpio_sys *gpio)
|
|||||||
return(atoi(value_str));
|
return(atoi(value_str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cppcheck-suppress unusedFunction
|
||||||
int gpio_write(const struct gpio_sys *gpio, int value)
|
int gpio_write(const struct gpio_sys *gpio, int value)
|
||||||
{
|
{
|
||||||
static const char s_values_str[] = "01";
|
static const char s_values_str[] = "01";
|
||||||
|
Reference in New Issue
Block a user