You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.0 KiB
81 lines
2.0 KiB
/**
|
|
* @file gpio_control.c
|
|
* @brief CLI utility to control GPIO pins using KED GPIO API on Linux.
|
|
*/
|
|
|
|
#include "../ked/peripherals/gpio/gpio.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
/**
|
|
* @brief Print usage message for the CLI tool.
|
|
*/
|
|
void print_usage(const char* prog_name) {
|
|
printf("Usage: %s -pin <number> -set|-reset|-toggle|-read\n", prog_name);
|
|
}
|
|
|
|
/**
|
|
* @brief Entry point for GPIO control CLI.
|
|
*/
|
|
int main(int argc, char* argv[]) {
|
|
if (argc < 4) {
|
|
print_usage(argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
uint32_t pin = 0;
|
|
uint8_t action = 0xFF; // 0=set, 1=reset, 2=toggle, 3=read
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
if (strcmp(argv[i], "-pin") == 0 && i + 1 < argc) {
|
|
pin = atoi(argv[++i]);
|
|
} else if (strcmp(argv[i], "-set") == 0) {
|
|
action = 0;
|
|
} else if (strcmp(argv[i], "-reset") == 0) {
|
|
action = 1;
|
|
} else if (strcmp(argv[i], "-toggle") == 0) {
|
|
action = 2;
|
|
} else if (strcmp(argv[i], "-read") == 0) {
|
|
action = 3;
|
|
}
|
|
}
|
|
|
|
if (pin == 0 || action == 0xFF) {
|
|
print_usage(argv[0]);
|
|
return 2;
|
|
}
|
|
|
|
gpio_t gpio = {
|
|
.chipname = "/dev/gpiochip0",
|
|
.line_offset = pin,
|
|
.direction = (action == 3) ? T_GPIO_DIRECTION_INPUT : T_GPIO_DIRECTION_OUTPUT,
|
|
.bias = T_GPIO_BIAS_DEFAULT,
|
|
.mode = T_GPIO_MODE_DEFAULT,
|
|
.status = T_GPIO_STATUS_NOT_INIT
|
|
};
|
|
|
|
if (ked_gpio_init(&gpio) != T_GPIO_ERR_OK) {
|
|
fprintf(stderr, "Failed to initialize GPIO%u\n", pin);
|
|
return 3;
|
|
}
|
|
|
|
switch (action) {
|
|
case 0:
|
|
ked_gpio_set(&gpio, 1);
|
|
break;
|
|
case 1:
|
|
ked_gpio_set(&gpio, 0);
|
|
break;
|
|
case 2:
|
|
ked_gpio_toggle(&gpio);
|
|
break;
|
|
case 3:
|
|
printf("GPIO%u value: %d\n", pin, ked_gpio_read(&gpio));
|
|
break;
|
|
}
|
|
|
|
ked_gpio_deinit(&gpio);
|
|
return 0;
|
|
}
|