/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : bsp_init.c
  * @brief          : Board support package initialization source file
  ******************************************************************************
  */
/* USER CODE END Header */

#include "bsp_init.h"
#include "bsp_board.h"
#include "bsp_board_manager.h"
#include "hal.h"
#include "hal_gpio.h"
#include "hal_uart.h"

/**
 * @brief Initialize board support package
 */
void bsp_init(void) {
    /* Get board configuration */
    const bsp_board_config_t* board_config = bsp_board_get_config();
    
    /* Initialize HAL layer */
    hal_init();
    
    /* Initialize peripherals based on configuration */
    
    /* Initialize LED */
    if (board_config->led_init != NULL) {
        board_config->led_init(&board_config->led);
    } else {
        /* Use default initialization if no function provided */
        hal_gpio_config_t gpio_config = {
            .port = board_config->led.port,
            .pin = board_config->led.pin,
            .mode = board_config->led.mode,
            .speed = board_config->led.speed,
            .pull = board_config->led.pull
        };
        hal_gpio_configure_pin(&gpio_config);
    }
    
    /* Initialize Buttons */
    if (board_config->button_init != NULL) {
        board_config->button_init(&board_config->buttons);
    }
    
    /* Initialize UART */
    if (board_config->uart_init != NULL) {
        board_config->uart_init(&board_config->uart);
    }
    
    /* Initialize W25QXX if available */
    if (board_config->w25qxx_init != NULL) {
        board_config->w25qxx_init(&board_config->w25qxx);
    }
}

/**
 * @brief Initialize board GPIO using configuration
 */
void bsp_gpio_init(void) {
    /* Get board configuration */
    const bsp_board_config_t* board_config = bsp_board_get_config();
    uint8_t i;
    
    /* Initialize LED GPIO */
    hal_gpio_config_t gpio_config = {
        .port = board_config->led.port,
        .pin = board_config->led.pin,
        .mode = board_config->led.mode,
        .speed = board_config->led.speed,
        .pull = board_config->led.pull
    };
    hal_gpio_configure_pin(&gpio_config);
    
    /* Initialize Buttons GPIO */
    for (i = 0; i < board_config->buttons.count; i++) {
        const bsp_button_config_t* button = &board_config->buttons.buttons[i];
        gpio_config.port = button->port;
        gpio_config.pin = button->pin;
        gpio_config.mode = button->mode;
        gpio_config.speed = button->speed;
        gpio_config.pull = button->pull;
        hal_gpio_configure_pin(&gpio_config);
    }
}

/**
 * @brief Get board name
 * @retval Board name string
 */
const char* bsp_get_board_name(void) {
    return bsp_board_get_name();
}

/**
 * @brief Get current board configuration
 * @retval Pointer to board configuration structure
 */
const bsp_board_config_t* bsp_get_board_config(void) {
    return bsp_board_get_config();
}
