/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : hal_gpio.c
  * @brief          : GPIO hardware abstraction layer source file
  ******************************************************************************
  */
/* USER CODE END Header */

#include "hal.h"
#include "hal_gpio.h"
#include <stddef.h>

/**
 * @brief Initialize GPIO hardware
 * @retval HAL status code
 */
hal_ret_t hal_gpio_init(void) {
    /* Call architecture specific GPIO initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    hal_stm32f1_gpio_init();
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    hal_stm32f4_gpio_init();
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    hal_stm32f7_gpio_init();
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    hal_stm32l4_gpio_init();
#else
#error "Unsupported HAL architecture"
    return HAL_RET_ERROR;
#endif
    return HAL_RET_OK;
}

/**
 * @brief Configure GPIO pin
 * @param config: GPIO configuration structure
 * @retval HAL status code
 */
hal_ret_t hal_gpio_configure_pin(const hal_gpio_config_t *config) {
    if (config == NULL) {
        return HAL_RET_INVALID_PARAM;
    }
    
    /* Call architecture specific GPIO configuration */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    hal_stm32f1_gpio_configure_pin(config);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    hal_stm32f4_gpio_configure_pin(config);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    hal_stm32f7_gpio_configure_pin(config);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    hal_stm32l4_gpio_configure_pin(config);
#else
#error "Unsupported HAL architecture"
    return HAL_RET_ERROR;
#endif
    return HAL_RET_OK;
}

/**
 * @brief Write GPIO pin state
 * @param port: GPIO port
 * @param pin: GPIO pin
 * @param state: GPIO pin state
 * @retval HAL status code
 */
hal_ret_t hal_gpio_write_pin(hal_gpio_port_t port, hal_gpio_pin_t pin, hal_gpio_pin_state_t state) {
    /* Validate parameters */
    if (port >= HAL_GPIO_PORT_K + 1 || pin >= HAL_GPIO_PIN_ALL) {
        return HAL_RET_INVALID_PARAM;
    }
    
    /* Call architecture specific GPIO write implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    hal_stm32f1_gpio_write_pin(port, pin, state);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    hal_stm32f4_gpio_write_pin(port, pin, state);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    hal_stm32f7_gpio_write_pin(port, pin, state);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    hal_stm32l4_gpio_write_pin(port, pin, state);
#else
#error "Unsupported HAL architecture"
    return HAL_RET_ERROR;
#endif
    return HAL_RET_OK;
}

/**
 * @brief Toggle GPIO pin state
 * @param port: GPIO port
 * @param pin: GPIO pin
 * @retval HAL status code
 */
hal_ret_t hal_gpio_toggle_pin(hal_gpio_port_t port, hal_gpio_pin_t pin) {
    /* Validate parameters */
    if (port >= HAL_GPIO_PORT_K + 1 || pin >= HAL_GPIO_PIN_ALL) {
        return HAL_RET_INVALID_PARAM;
    }
    
    /* Call architecture specific GPIO toggle implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    hal_stm32f1_gpio_toggle_pin(port, pin);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    hal_stm32f4_gpio_toggle_pin(port, pin);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    hal_stm32f7_gpio_toggle_pin(port, pin);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    hal_stm32l4_gpio_toggle_pin(port, pin);
#else
#error "Unsupported HAL architecture"
    return HAL_RET_ERROR;
#endif
    return HAL_RET_OK;
}

/**
 * @brief Read GPIO pin state
 * @param port: GPIO port
 * @param pin: GPIO pin
 * @retval GPIO pin state
 */
hal_gpio_pin_state_t hal_gpio_read_pin(hal_gpio_port_t port, hal_gpio_pin_t pin) {
    /* Call architecture specific GPIO read implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    return hal_stm32f1_gpio_read_pin(port, pin);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    return hal_stm32f4_gpio_read_pin(port, pin);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    return hal_stm32f7_gpio_read_pin(port, pin);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    return hal_stm32l4_gpio_read_pin(port, pin);
#else
#error "Unsupported HAL architecture"
    return HAL_GPIO_PIN_RESET;
#endif
}
