|
|
|
|
@ -1,10 +1,182 @@
|
|
|
|
|
# Introduction
|
|
|
|
|
How to compile
|
|
|
|
|
# GPIO Command-Line Tool in C
|
|
|
|
|
|
|
|
|
|
A simple command-line tool written in C to control Raspberry Pi GPIO pins using the Linux GPIO `sysfs` interface.
|
|
|
|
|
|
|
|
|
|
The program supports configuring a GPIO as an **input** or **output**, writing a digital value, and reading the state of an input.
|
|
|
|
|
|
|
|
|
|
## Compile
|
|
|
|
|
|
|
|
|
|
Compile the program using GCC:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
gcc main.c -o gpio
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
The general command syntax is:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio <gpio> <direction> [value]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### GPIO as Output
|
|
|
|
|
|
|
|
|
|
Configure GPIO 17 as an output and set it HIGH:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio 17 out 1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Set GPIO 17 LOW:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio 17 out 0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
When `out` is selected, the value argument is required:
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
./gpio <gpio> out <0|1>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
For example, the following command is invalid:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio 17 out
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
because the output value is missing.
|
|
|
|
|
|
|
|
|
|
### GPIO as Input
|
|
|
|
|
|
|
|
|
|
Configure GPIO 17 as an input and read its current digital state:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
gcc main.c -o hello
|
|
|
|
|
./gpio 17 in
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The program reads the GPIO `value` file and displays either:
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
or:
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# Issues
|
|
|
|
|
No additional value argument is required for an input.
|
|
|
|
|
|
|
|
|
|
## Program Flow
|
|
|
|
|
|
|
|
|
|
The program follows this sequence:
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
Command-line arguments
|
|
|
|
|
│
|
|
|
|
|
▼
|
|
|
|
|
Validate direction
|
|
|
|
|
│
|
|
|
|
|
├── out → verify value argument
|
|
|
|
|
│
|
|
|
|
|
└── in → no value required
|
|
|
|
|
│
|
|
|
|
|
▼
|
|
|
|
|
Export GPIO
|
|
|
|
|
│
|
|
|
|
|
▼
|
|
|
|
|
Set GPIO direction
|
|
|
|
|
│
|
|
|
|
|
├── out → gpio_write()
|
|
|
|
|
│
|
|
|
|
|
└── in → gpio_read()
|
|
|
|
|
│
|
|
|
|
|
▼
|
|
|
|
|
Unexport GPIO
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
|
|
The program is divided into the following functions:
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
int gpio_export(const char *gpio);
|
|
|
|
|
int gpio_direction(const char *gpio, const char *direction);
|
|
|
|
|
int gpio_write(const char *gpio, const char *value);
|
|
|
|
|
int gpio_read(const char *gpio);
|
|
|
|
|
int gpio_unexport(const char *gpio);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Their responsibilities are:
|
|
|
|
|
|
|
|
|
|
- `gpio_export()` — exports the selected GPIO.
|
|
|
|
|
- `gpio_direction()` — configures the GPIO as `in` or `out`.
|
|
|
|
|
- `gpio_write()` — writes `0` or `1` to an output.
|
|
|
|
|
- `gpio_read()` — reads the digital state of an input.
|
|
|
|
|
- `gpio_unexport()` — releases the GPIO after the operation.
|
|
|
|
|
|
|
|
|
|
## GPIO sysfs Files
|
|
|
|
|
|
|
|
|
|
For GPIO 17, the program accesses files such as:
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
/sys/class/gpio/chip0/gpio17/direction
|
|
|
|
|
/sys/class/gpio/chip0/gpio17/value
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `direction` file determines whether the GPIO is configured as an input or output.
|
|
|
|
|
|
|
|
|
|
The `value` file is used to read or write the digital state of the GPIO.
|
|
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
|
|
Output:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio 17 out 1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Input:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio 17 in
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Invalid output command:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio 17 out
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Expected error:
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
Error: GPIO out option requires a value.
|
|
|
|
|
Usage: ./gpio <gpio> out <0|1>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Current Features
|
|
|
|
|
|
|
|
|
|
- GPIO export and unexport
|
|
|
|
|
- Input/output direction configuration
|
|
|
|
|
- Digital output (`0` or `1`)
|
|
|
|
|
- Digital input reading
|
|
|
|
|
- Command-line argument validation
|
|
|
|
|
- Invalid direction detection
|
|
|
|
|
|
|
|
|
|
## Future Work
|
|
|
|
|
|
|
|
|
|
A future version will include a `blink` operation:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
./gpio 17 blink 500
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
where `500` represents the blinking interval in milliseconds.
|
|
|
|
|
|
|
|
|
|
This version now reflects the structure of your current C implementation, including the separation between argument validation and GPIO operations.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- [ ] Error catcher
|
|
|
|
|
|