/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : hal_stm32f4_uart.c
  * @brief          : STM32F4 specific UART HAL implementation
  ******************************************************************************
  */
/* USER CODE END Header */

#include "hal.h"
#include "hal_uart.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_uart.h"

/* UART handle */
static UART_HandleTypeDef huart1;

/**
 * @brief STM32F4 specific UART initialization
 */
void hal_stm32f4_uart_init(void) {
    /* UART initialization is handled by HAL_Init() */
    
    /* Configure UART GPIO pins */
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    
    /* Enable UART clock */
    __HAL_RCC_USART1_CLK_ENABLE();
    
    /* Enable GPIO clocks */
    __HAL_RCC_GPIOA_CLK_ENABLE();
    
    /* Configure USART1 Tx (PA9) as alternate function push-pull */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    /* Configure USART1 Rx (PA10) as alternate function push-pull */
    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    /* Initialize UART */
    huart1.Instance = USART1;
    huart1.Init.BaudRate = 115200;
    huart1.Init.WordLength = UART_WORDLENGTH_8B;
    huart1.Init.StopBits = UART_STOPBITS_1;
    huart1.Init.Parity = UART_PARITY_NONE;
    huart1.Init.Mode = UART_MODE_TX_RX;
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    
    if (HAL_UART_Init(&huart1) != HAL_OK) {
        /* Initialization Error */
        while (1);
    }
}

/**
 * @brief STM32F4 specific UART configuration
 */
void hal_stm32f4_uart_config(const hal_uart_config_t *config) {
    if (config == NULL) {
        return;
    }
    
    /* Update UART configuration */
    huart1.Init.BaudRate = config->baudrate;
    
    /* Convert parity */
    switch (config->parity) {
        case HAL_UART_PARITY_NONE:
            huart1.Init.Parity = UART_PARITY_NONE;
            break;
        case HAL_UART_PARITY_ODD:
            huart1.Init.Parity = UART_PARITY_ODD;
            break;
        case HAL_UART_PARITY_EVEN:
            huart1.Init.Parity = UART_PARITY_EVEN;
            break;
        default:
            huart1.Init.Parity = UART_PARITY_NONE;
            break;
    }
    
    /* Convert stop bits */
    huart1.Init.StopBits = (config->stopbits == HAL_UART_STOPBITS_1) ? 
                           UART_STOPBITS_1 : UART_STOPBITS_2;
    
    /* Convert data bits */
    huart1.Init.WordLength = (config->databits == HAL_UART_DATABITS_8) ? 
                            UART_WORDLENGTH_8B : UART_WORDLENGTH_9B;
    
    /* Re-initialize UART */
    if (HAL_UART_Init(&huart1) != HAL_OK) {
        /* Initialization Error */
        while (1);
    }
}

/**
 * @brief STM32F4 specific UART send implementation
 */
void hal_stm32f4_uart_send(hal_uart_instance_t instance, const uint8_t *data, size_t length) {
    if (data == NULL || length == 0) {
        return;
    }
    
    /* Currently only supports USART1 */
    if (instance == HAL_UART_INSTANCE_1) {
        HAL_UART_Transmit(&huart1, (uint8_t *)data, length, HAL_MAX_DELAY);
    }
}

/**
 * @brief STM32F4 specific UART receive implementation
 */
size_t hal_stm32f4_uart_receive(hal_uart_instance_t instance, uint8_t *data, size_t length) {
    if (data == NULL || length == 0) {
        return 0;
    }
    
    /* Currently only supports USART1 */
    if (instance == HAL_UART_INSTANCE_1) {
        HAL_StatusTypeDef status = HAL_UART_Receive(&huart1, data, length, HAL_MAX_DELAY);
        
        if (status == HAL_OK) {
            return length;
        }
    }
    
    return 0;
}

/**
 * @brief STM32F4 specific UART TX ready check
 */
uint8_t hal_stm32f4_uart_is_tx_ready(hal_uart_instance_t instance) {
    /* Currently only supports USART1 */
    if (instance == HAL_UART_INSTANCE_1) {
        return (HAL_UART_GetState(&huart1) == HAL_UART_STATE_READY) ? 1 : 0;
    }
    return 0;
}

/**
 * @brief STM32F4 specific UART RX ready check
 */
uint8_t hal_stm32f4_uart_is_rx_ready(hal_uart_instance_t instance) {
    /* Currently only supports USART1 */
    if (instance == HAL_UART_INSTANCE_1) {
        return (HAL_UART_GetState(&huart1) == HAL_UART_STATE_READY) ? 1 : 0;
    }
    return 0;
}
