Modified to be as identical to the linux_test implementation with pointers in order to have a good benchmark

linux_test_nopointer
Kynsight 2 months ago
parent 23cd672505
commit 1fa5b939ae

@ -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. #Size Difference with Pointers, don't forget that in an MCU envoiroment it will also execute faster.
``` ```
With Pointers Size
text data bss dec hex filename text data bss dec hex filename
3812 340 16 4168 1048 ked_executable 3680 332 16 4028 fbc ked_executable
Without Pointer ```
text data bss dec hex filename
3692 332 16 4040 fc8 ked_executable ```

@ -55,9 +55,10 @@ int main(int argc, char* argv[]) {
.status = T_GPIO_STATUS_NOT_INIT .status = T_GPIO_STATUS_NOT_INIT
}; };
if (ked_gpio_init(&gpio) != T_GPIO_ERR_OK) { gpio_error_t err = ked_gpio_init(&gpio);
fprintf(stderr, "Failed to initialize GPIO%u\n", pin); if (err != T_GPIO_ERR_OK) {
return 3; ked_gpio_handle_error(err);
return 1;
} }
switch (action) { switch (action) {

@ -10,6 +10,9 @@
/** /**
* @brief Internal Linux GPIO context. * @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 { typedef struct {
struct gpiod_chip* chip; struct gpiod_chip* chip;
@ -18,34 +21,82 @@ typedef struct {
static gpio_linux_ctx_t gpio_ctx; 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) { void ked_gpio_set(gpio_t* gpio, uint8_t value) {
if (gpio_ctx.line) {
gpiod_line_set_value(gpio_ctx.line, value); 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) { 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); 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) { 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); uint8_t current = (uint8_t)gpiod_line_get_value(gpio_ctx.line);
gpiod_line_set_value(gpio_ctx.line, !current); 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_error_t ked_gpio_init(const gpio_t* gpio) {
gpio_ctx.chip = gpiod_chip_open(gpio->chipname); gpio_ctx.chip = gpiod_chip_open(gpio->chipname);
if (!gpio_ctx.chip) { 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 * @brief Deinitialize and release resources for the GPIO.
//3692 332 16 4040 fc8 ked_executable */
void ked_gpio_deinit(gpio_t* gpio) { void ked_gpio_deinit(gpio_t* gpio) {
/* if (gpio_ctx.line) {
if (gpio && gpio->deinit) { gpiod_line_release(gpio_ctx.line);
gpio->deinit(); gpio_ctx.line = NULL;
gpio->status = T_GPIO_STATUS_NOT_INIT; }
}*/ if (gpio_ctx.chip) {
gpiod_chip_close(gpio_ctx.chip);
gpio_ctx.chip = NULL;
} }
gpio_error_t ked_gpio_update(gpio_t* gpio) {
ked_gpio_deinit(gpio);
return ked_gpio_init(gpio);
} }

Loading…
Cancel
Save