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

#include "hal.h"
#include "hal_spi.h"
#include "hal_stm32f4_spi.h"

/**
 * @brief Initialize SPI hardware
 * @param instance SPI instance identifier
 * @param config SPI configuration structure
 * @return true if initialization is successful, false otherwise
 */
bool hal_spi_init(hal_spi_instance_t instance, const hal_spi_config_t *config) {
    /* Call architecture specific SPI initialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    return hal_stm32f1_spi_init(instance, config);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    return hal_stm32f4_spi_init(instance, config);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    return hal_stm32f7_spi_init(instance, config);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    return hal_stm32l4_spi_init(instance, config);
#else
#error "Unsupported HAL architecture"
    return false;
#endif
}

/**
 * @brief Deinitialize SPI hardware
 * @param instance SPI instance identifier
 * @return true if deinitialization is successful, false otherwise
 */
bool hal_spi_deinit(hal_spi_instance_t instance) {
    /* Call architecture specific SPI deinitialization */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    return hal_stm32f1_spi_deinit(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    return hal_stm32f4_spi_deinit(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    return hal_stm32f7_spi_deinit(instance);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    return hal_stm32l4_spi_deinit(instance);
#else
#error "Unsupported HAL architecture"
    return false;
#endif
}

/**
 * @brief SPI transmit function
 * @param instance SPI instance identifier
 * @param p_data Pointer to data buffer to transmit
 * @param size Size of data to transmit
 * @return true if transmission is successful, false otherwise
 */
bool hal_spi_transmit(hal_spi_instance_t instance, const uint8_t *p_data, uint16_t size) {
    /* Call architecture specific SPI transmit implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    return hal_stm32f1_spi_transmit(instance, p_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    return hal_stm32f4_spi_transmit(instance, p_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    return hal_stm32f7_spi_transmit(instance, p_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    return hal_stm32l4_spi_transmit(instance, p_data, size);
#else
#error "Unsupported HAL architecture"
    return false;
#endif
}

/**
 * @brief SPI receive function
 * @param instance SPI instance identifier
 * @param p_data Pointer to data buffer to receive
 * @param size Size of data to receive
 * @return true if reception is successful, false otherwise
 */
bool hal_spi_receive(hal_spi_instance_t instance, uint8_t *p_data, uint16_t size) {
    /* Call architecture specific SPI receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    return hal_stm32f1_spi_receive(instance, p_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    return hal_stm32f4_spi_receive(instance, p_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    return hal_stm32f7_spi_receive(instance, p_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    return hal_stm32l4_spi_receive(instance, p_data, size);
#else
#error "Unsupported HAL architecture"
    return false;
#endif
}

/**
 * @brief SPI transmit and receive function
 * @param instance SPI instance identifier
 * @param p_tx_data Pointer to data buffer to transmit
 * @param p_rx_data Pointer to data buffer to receive
 * @param size Size of data to transmit/receive
 * @return true if transmission and reception are successful, false otherwise
 */
bool hal_spi_transmit_receive(hal_spi_instance_t instance, const uint8_t *p_tx_data, uint8_t *p_rx_data, uint16_t size) {
    /* Call architecture specific SPI transmit and receive implementation */
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
    return hal_stm32f1_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
    return hal_stm32f4_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
    return hal_stm32f7_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
    return hal_stm32l4_spi_transmit_receive(instance, p_tx_data, p_rx_data, size);
#else
#error "Unsupported HAL architecture"
    return false;
#endif
}