/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : stm32f407vet6_board.c
  * @brief          : STM32F407VET6 板卡配置实现
  ******************************************************************************
  */
/* USER CODE END Header */

#include "bsp_board.h"
#include "bsp_config.h"
#include "bsp_eth.h"
#include "hal_gpio.h"
#include "hal_uart.h"
#include "hal_spi.h"
#include "hal_eth.h"

/**
 * @brief 默认 LED 初始化函数
 * @param config: LED 配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_led_init(const void* config) {
    const bsp_led_config_t* led_config = (const bsp_led_config_t*)config;
    hal_gpio_config_t gpio_config = {
        .port = led_config->port,
        .pin = led_config->pin,
        .mode = led_config->mode,
        .speed = led_config->speed,
        .pull = led_config->pull
    };
    return hal_gpio_configure_pin(&gpio_config);
}

/**
 * @brief 默认按键初始化函数
 * @param config: 按键配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_button_init(const void* config) {
    const bsp_buttons_config_t* buttons_config = (const bsp_buttons_config_t*)config;
    uint8_t i;
    hal_ret_t status = HAL_RET_OK;
    
    for (i = 0; i < buttons_config->count; i++) {
        const bsp_button_config_t* button_config = &buttons_config->buttons[i];
        hal_gpio_config_t gpio_config = {
            .port = button_config->port,
            .pin = button_config->pin,
            .mode = button_config->mode,
            .speed = button_config->speed,
            .pull = button_config->pull
        };
        status = hal_gpio_configure_pin(&gpio_config);
        if (status != HAL_RET_OK) {
            break;
        }
    }
    
    return status;
}

/**
 * @brief 默认 UART 初始化函数
 * @param config: UART 配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_uart_init(const void* config) {
    const bsp_uart_config_t* uart_config = (const bsp_uart_config_t*)config;
    hal_uart_config_t uart_cfg = {
        .instance = (hal_uart_instance_t)uart_config->instance,
        .baudrate = uart_config->baudrate,
        .parity = uart_config->parity,
        .stopbits = uart_config->stopbits,
        .databits = uart_config->databits
    };
    return hal_uart_config(&uart_cfg);
}

/**
 * @brief 默认 SPI 初始化函数
 * @param config: SPI 配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_spi_init(const void* config) {
    const bsp_spi_config_t* spi_config = (const bsp_spi_config_t*)config;
    hal_spi_config_t spi_cfg = {
        .instance = spi_config->instance,
        .mode = spi_config->mode,
        .baudrate = spi_config->baudrate,
        .polarity = spi_config->polarity,
        .phase = spi_config->phase,
        .databits = spi_config->databits
    };
    return hal_spi_init(spi_config->instance, &spi_cfg);
}

/**
 * @brief 默认 I2C 初始化函数
 * @param config: I2C 配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_i2c_init(const void* config) {
    (void)config;
    /* TODO: 实现默认 I2C 初始化 */
    return HAL_RET_OK;
}

/**
 * @brief 默认 CAN 初始化函数
 * @param config: CAN 配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_can_init(const void* config) {
    (void)config;
    /* TODO: 实现默认 CAN 初始化 */
    return HAL_RET_OK;
}

/**
 * @brief 默认 ADC 初始化函数
 * @param config: ADC 配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_adc_init(const void* config) {
    (void)config;
    /* TODO: 实现默认 ADC 初始化 */
    return HAL_RET_OK;
}

/**
 * @brief 默认 W25QXX 初始化函数
 * @param config: W25QXX 配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_w25qxx_init(const void* config) {
    const bsp_w25qxx_config_t* w25qxx_config = (const bsp_w25qxx_config_t*)config;
    hal_ret_t status = HAL_RET_OK;
    
    /* 初始化 CS 引脚 */
    hal_gpio_config_t gpio_config = {
        .port = w25qxx_config->cs_port,
        .pin = w25qxx_config->cs_pin,
        .mode = HAL_GPIO_MODE_OUTPUT_PP,
        .speed = HAL_GPIO_SPEED_HIGH,
        .pull = HAL_GPIO_PULL_NO
    };
    status = hal_gpio_configure_pin(&gpio_config);
    if (status != HAL_RET_OK) {
        return status;
    }
    
    /* 初始时取消选择芯片 */
    status = hal_gpio_write_pin(w25qxx_config->cs_port, w25qxx_config->cs_pin, HAL_GPIO_PIN_SET);
    if (status != HAL_RET_OK) {
        return status;
    }
    
    /* SPI 实例现在只是一个索引，实际的 SPI 初始化在别处处理 */
    return status;
}

/**
 * @brief 默认以太网初始化函数
 * @param config: 以太网配置结构体
 * @retval HAL 状态码
 */
static hal_ret_t default_eth_init(const void* config) {
    const bsp_eth_config_t* eth_config = (const bsp_eth_config_t*)config;
    return bsp_eth_init(eth_config);
}

/**
 * @brief STM32F407VET6 板卡按键配置
 */
static const bsp_button_config_t stm32f407vet6_buttons[] = {
    /* KEY0 - PE4, 低电平有效 */
    {
        .port = HAL_GPIO_PORT_E,
        .pin = HAL_GPIO_PIN_4,
        .mode = HAL_GPIO_MODE_INPUT,
        .speed = HAL_GPIO_SPEED_LOW,
        .pull = HAL_GPIO_PULL_UP,
        .active_high = 0
    },
    /* KEY1 - PE3, 低电平有效 */
    {
        .port = HAL_GPIO_PORT_E,
        .pin = HAL_GPIO_PIN_3,
        .mode = HAL_GPIO_MODE_INPUT,
        .speed = HAL_GPIO_SPEED_LOW,
        .pull = HAL_GPIO_PULL_UP,
        .active_high = 0
    },
    /* WKUP - PA0, 高电平有效 */
    {
        .port = HAL_GPIO_PORT_A,
        .pin = HAL_GPIO_PIN_0,
        .mode = HAL_GPIO_MODE_INPUT,
        .speed = HAL_GPIO_SPEED_LOW,
        .pull = HAL_GPIO_PULL_DOWN,
        .active_high = 1
    }
};

/**
 * @brief STM32F407VET6 按键配置
 */
static const bsp_buttons_config_t stm32f407vet6_buttons_config = {
    .enable = 1,
    .count = sizeof(stm32f407vet6_buttons) / sizeof(bsp_button_config_t),
    .buttons = stm32f407vet6_buttons
};

/**
 * @brief STM32F407VET6 板卡 UART 配置
 */
static const bsp_uart_config_t stm32f407vet6_uarts[] = {
    /* USART1 - PA9(TX), PA10(RX) */
    {
        .enable = 1,
        .instance = BSP_UART_INSTANCE_1,
        .baudrate = 115200,
        .parity = HAL_UART_PARITY_NONE,
        .stopbits = HAL_UART_STOPBITS_1,
        .databits = HAL_UART_DATABITS_8,
        .tx_port = HAL_GPIO_PORT_A,  /* USART1_TX - PA9 */
        .tx_pin = HAL_GPIO_PIN_9,
        .rx_port = HAL_GPIO_PORT_A,  /* USART1_RX - PA10 */
        .rx_pin = HAL_GPIO_PIN_10
    }
};

/**
 * @brief STM32F407VET6 板卡 SPI 配置
 */
static const bsp_spi_config_t stm32f407vet6_spis[] = {
    /* SPI1 - PA5(SCK), PA6(MISO), PA7(MOSI) */
    {
        .enable = 1,
        .instance = HAL_SPI_INSTANCE_1,
        .mode = HAL_SPI_MODE_MASTER,
        .baudrate = 1000000, /* 1 MHz */
        .polarity = HAL_SPI_POLARITY_LOW,
        .phase = HAL_SPI_PHASE_1EDGE,
        .databits = HAL_SPI_DATABITS_8,
        .sck_port = HAL_GPIO_PORT_A,
        .sck_pin = HAL_GPIO_PIN_5,
        .miso_port = HAL_GPIO_PORT_A,
        .miso_pin = HAL_GPIO_PIN_6,
        .mosi_port = HAL_GPIO_PORT_A,
        .mosi_pin = HAL_GPIO_PIN_7
    }
};

/**
 * @brief STM32F407VET6 板卡 I2C 配置
 */
static const bsp_i2c_config_t stm32f407vet6_i2cs[] = {
    /* I2C1 - PB6(SCL), PB7(SDA) */
    {
        .enable = 0, /* 默认禁用 */
        .instance = HAL_I2C_INSTANCE_1,
        .speed = HAL_I2C_SPEED_STANDARD,
        .address_mode = HAL_I2C_ADDRESS_7BIT,
        .dutycycle = HAL_I2C_DUTYCYCLE_2,
        .own_address1 = 0x00,
        .scl_port = HAL_GPIO_PORT_B,
        .scl_pin = HAL_GPIO_PIN_6,
        .sda_port = HAL_GPIO_PORT_B,
        .sda_pin = HAL_GPIO_PIN_7
    }
};

/**
 * @brief STM32F407VET6 板卡 CAN 配置
 */
static const bsp_can_config_t stm32f407vet6_cans[] = {
    /* CAN1 - PA11(RX), PA12(TX) */
    {
        .enable = 0, /* 默认禁用 */
        .instance = HAL_CAN_INSTANCE_1,
        .mode = HAL_CAN_MODE_NORMAL,
        .prescaler = 6, /* 168MHz / 6 = 28MHz */
        .sync_jump_width = 1,
        .time_segment1 = 13,
        .time_segment2 = 2,
        .rx_port = HAL_GPIO_PORT_A,
        .rx_pin = HAL_GPIO_PIN_11,
        .tx_port = HAL_GPIO_PORT_A,
        .tx_pin = HAL_GPIO_PIN_12
    }
};

/**
 * @brief STM32F407VET6 板卡 ADC 通道配置
 */
static const bsp_adc_channel_config_t stm32f407vet6_adc_channels[] = {
    /* ADC1 Channel 0 - PA0 */
    {
        .enable = 0, /* 默认禁用 */
        .channel = HAL_ADC_CHANNEL_0,
        .sampletime = HAL_ADC_SAMPLETIME_56CYCLES,
        .rank = 1
    }
};

/**
 * @brief STM32F407VET6 板卡 ADC 配置
 */
static const bsp_adc_config_t stm32f407vet6_adcs[] = {
    /* ADC1 */
    {
        .enable = 0, /* 默认禁用 */
        .instance = HAL_ADC_INSTANCE_1,
        .resolution = HAL_ADC_RESOLUTION_12B,
        .scan_conversion_mode = 0,
        .continuous_conversion_mode = 0,
        .channel_count = sizeof(stm32f407vet6_adc_channels) / sizeof(bsp_adc_channel_config_t),
        .channels = stm32f407vet6_adc_channels
    }
};

/**
 * @brief STM32F407VET6 板卡 LED 配置
 */
static const bsp_led_config_t stm32f407vet6_leds[] = {
    /* LED0 - PA6 */
    {
        .enable = 1,
        .port = HAL_GPIO_PORT_A,
        .pin = HAL_GPIO_PIN_6,
        .mode = HAL_GPIO_MODE_OUTPUT_PP,
        .speed = HAL_GPIO_SPEED_MEDIUM,
        .pull = HAL_GPIO_PULL_NO
    },
    /* LED1 - PA7 */
    {
        .enable = 1,
        .port = HAL_GPIO_PORT_A,
        .pin = HAL_GPIO_PIN_7,
        .mode = HAL_GPIO_MODE_OUTPUT_PP,
        .speed = HAL_GPIO_SPEED_MEDIUM,
        .pull = HAL_GPIO_PULL_NO
    }
};

/**
 * @brief STM32F407VET6 板卡配置
 */
const bsp_board_config_t stm32f407vet6_board_config = {
    .version = BSP_CONFIG_VERSION,
    .name = "STM32F407VET6",
    .description = "STM32F407VET6 开发板",
    .id = {
        .vendor_id = 0x0483,      /* STMicroelectronics */
        .product_id = 0x374B,      /* STM32F4 系列 */
        .serial_number = 0x00000001 /* 默认序列号 */
    },
    
    /* 板卡特性 */
    .features = (
        BSP_BOARD_FEATURE_LED | 
        BSP_BOARD_FEATURE_BUTTON | 
        BSP_BOARD_FEATURE_UART | 
        BSP_BOARD_FEATURE_SPI | 
        BSP_BOARD_FEATURE_I2C | 
        BSP_BOARD_FEATURE_CAN | 
        BSP_BOARD_FEATURE_ADC | 
        BSP_BOARD_FEATURE_W25QXX |
        BSP_BOARD_FEATURE_ETH
    ),
    
    /* 硬件信息 */
    .hw_info = {
        .clock_speed = 168000000,  /* 168 MHz */
        .flash_size = 512 * 1024,  /* 512 KB */
        .ram_size = 192 * 1024,     /* 192 KB */
        .eeprom_size = 0,           /* 无内部 EEPROM */
        .sram_size = 64 * 1024,     /* 64 KB SRAM */
        .cpu_core = 0x0F,           /* Cortex-M4 */
        .cpu_bits = 32              /* 32位 CPU */
    },
    
    /* 外设配置 */
    .leds = {
        .enable = 1,
        .count = sizeof(stm32f407vet6_leds) / sizeof(bsp_led_config_t),
        .leds = stm32f407vet6_leds
    },
    .buttons = stm32f407vet6_buttons_config,
    .w25qxx = {
        .enable = 1,
        .cs_port = HAL_GPIO_PORT_B,
        .cs_pin = HAL_GPIO_PIN_0,
        .spi_instance = 0  /* 使用 SPI 实例 0 */
    },
    .eth = {
        .enable = 1,
        .instance = HAL_ETH_INSTANCE_1,
        .mode = HAL_ETH_MODE_FULLDUPLEX,
        .speed = HAL_ETH_SPEED_100MBPS,
        .phy_addr = LAN8720_PHY_ADDR,
        .mac_addr = {
            .byte = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
        },
        .auto_negotiation = 1,
        .interrupt_enable = 1
    },
    
    /* 外设数组 */
    .periphs = {
        /* UART 配置 */
        .uart_count = 1,
        .uarts = stm32f407vet6_uarts,
        
        /* SPI 配置 */
        .spi_count = 1,
        .spis = stm32f407vet6_spis,
        
        /* I2C 配置 */
        .i2c_count = 1,
        .i2cs = stm32f407vet6_i2cs,
        
        /* CAN 配置 */
        .can_count = 1,
        .cans = stm32f407vet6_cans,
        
        /* ADC 配置 */
        .adc_count = 1,
        .adcs = stm32f407vet6_adcs
    },
    
    /* 初始化函数指针 */
    .init_funcs = {
        .led_init = default_led_init,
        .button_init = default_button_init,
        .uart_init = default_uart_init,
        .spi_init = default_spi_init,
        .i2c_init = default_i2c_init,
        .can_init = default_can_init,
        .adc_init = default_adc_init,
        .w25qxx_init = default_w25qxx_init,
        .eth_init = default_eth_init
    },
    
    /* 额外的板卡特定配置 */
    .custom_config = NULL,
    .custom_config_size = 0,
    
    /* 板卡版本信息 */
    .major_rev = 1,
    .minor_rev = 0,
    .patch_rev = 0,
    .revision_desc = "初始版本"
};

/**
 * @brief 获取板卡名称
 * @retval 板卡名称字符串
 */
const char* bsp_board_get_name(void) {
    return stm32f407vet6_board_config.name;
}

/**
 * @brief 获取板卡配置
 * @retval 指向板卡配置结构体的指针
 */
const bsp_board_config_t* bsp_board_get_config(void) {
    return &stm32f407vet6_board_config;
}
