diff --git a/README.md b/README.md index 43f6c4e..97a8cee 100644 --- a/README.md +++ b/README.md @@ -38,12 +38,10 @@ Will set GPIO21 pin (HEADER 40) to High #Size Difference with Pointers, don't forget that in an MCU envoiroment it will also execute faster. ``` -With Pointers -text data bss dec hex filename -3812 340 16 4168 1048 ked_executable +Size +text data bss dec hex filename +3680 332 16 4028 fbc ked_executable -Without Pointer -text data bss dec hex filename -3692 332 16 4040 fc8 ked_executable ``` +``` diff --git a/examples/pin_control.c b/examples/pin_control.c index 92b3ed8..1e9d21e 100644 --- a/examples/pin_control.c +++ b/examples/pin_control.c @@ -55,9 +55,10 @@ int main(int argc, char* argv[]) { .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; + gpio_error_t err = ked_gpio_init(&gpio); + if (err != T_GPIO_ERR_OK) { + ked_gpio_handle_error(err); + return 1; } switch (action) { diff --git a/peripherals/gpio/gpio.c b/peripherals/gpio/gpio.c index 3e9b0f7..5e0be79 100644 --- a/peripherals/gpio/gpio.c +++ b/peripherals/gpio/gpio.c @@ -10,6 +10,9 @@ /** * @brief Internal Linux GPIO context. + * + * Holds the active chip and line handles. + * This implementation assumes one active GPIO at a time (can be extended to multiple if needed). */ typedef struct { struct gpiod_chip* chip; @@ -18,34 +21,82 @@ typedef struct { static gpio_linux_ctx_t gpio_ctx; +/* === Centralized Error Handling === */ + +/** + * @brief Centralized error handler for GPIO-related failures. + * + * Logs the error to stderr and can be extended to trigger hooks or diagnostics. + * + * @param err The GPIO error code encountered. + */ +void ked_gpio_handle_error(gpio_error_t err) { + fprintf(stderr, "[GPIO ERROR] Code: %d\n", err); + // Future: report to diagnostic logger or panic handler +} + +/* === GPIO Operation Implementations === */ + +/** + * @brief Set the output level of the GPIO line. + * + * @param value 1 = HIGH, 0 = LOW + */ void ked_gpio_set(gpio_t* gpio, uint8_t value) { - gpiod_line_set_value(gpio_ctx.line, value); + if (gpio_ctx.line) { + gpiod_line_set_value(gpio_ctx.line, value); + } else { + ked_gpio_handle_error(T_GPIO_ERR_NOT_AVAILABLE); + } } +/** + * @brief Read the current logic level of the GPIO line. + * + * @return Logic level (1 = HIGH, 0 = LOW) + */ uint8_t ked_gpio_read(gpio_t* gpio) { + if (!gpio_ctx.line) { + ked_gpio_handle_error(T_GPIO_ERR_NOT_AVAILABLE); + return 0; + } return (uint8_t)gpiod_line_get_value(gpio_ctx.line); } +/** + * @brief Toggle the current logic level of the GPIO line. + */ void ked_gpio_toggle(gpio_t* gpio) { + if (!gpio_ctx.line) { + ked_gpio_handle_error(T_GPIO_ERR_NOT_AVAILABLE); + return; + } uint8_t current = (uint8_t)gpiod_line_get_value(gpio_ctx.line); gpiod_line_set_value(gpio_ctx.line, !current); } -void gpio_linux_deinit(gpio_t* gpio) { - if (gpio_ctx.line) { - gpiod_line_release(gpio_ctx.line); - gpio_ctx.line = NULL; - } - if (gpio_ctx.chip) { - gpiod_chip_close(gpio_ctx.chip); - gpio_ctx.chip = NULL; - } -} -void ked_gpio_handle_error(gpio_error_t err) { - fprintf(stderr, "[GPIO ERROR] Code: %d\n", err); +/** + * @brief Update GPIO configuration dynamically. + * + * Currently not supported on Linux – placeholder only. + */ +gpio_error_t ked_gpio_update(gpio_t* gpio) { + // TODO: implement runtime reconfiguration if needed + return T_GPIO_ERR_UNSUPPORTED_MODE; } +/* === Public API Implementation === */ + +/** + * @brief Initialize the GPIO using the given Linux gpio_t config and callback struct. + * + * This sets direction, bias, and binds callbacks to the gpio_fn_ptr_t table. + * + * @param gpio Pointer to GPIO configuration structure + * @param callbacks Optional pointer to gpio_fn_ptr_t to receive platform-specific handlers + * @return T_GPIO_ERR_OK on success, or appropriate error code + */ gpio_error_t ked_gpio_init(const gpio_t* gpio) { gpio_ctx.chip = gpiod_chip_open(gpio->chipname); if (!gpio_ctx.chip) { @@ -85,20 +136,16 @@ gpio_error_t ked_gpio_init(const gpio_t* gpio) { } -//3812 340 16 4168 1048 ked_executable -//3816 340 16 4172 104c ked_executable -//3692 332 16 4040 fc8 ked_executable - +/** + * @brief Deinitialize and release resources for the GPIO. + */ void ked_gpio_deinit(gpio_t* gpio) { -/* - if (gpio && gpio->deinit) { - gpio->deinit(); - gpio->status = T_GPIO_STATUS_NOT_INIT; - }*/ -} - -gpio_error_t ked_gpio_update(gpio_t* gpio) { - ked_gpio_deinit(gpio); - return ked_gpio_init(gpio); + if (gpio_ctx.line) { + gpiod_line_release(gpio_ctx.line); + gpio_ctx.line = NULL; + } + if (gpio_ctx.chip) { + gpiod_chip_close(gpio_ctx.chip); + gpio_ctx.chip = NULL; + } } -