#include "hdl_spi.h"

void hdl_spi1_init(void)
{
	stc_spi_init_t stcSpiInit;
    stc_gpio_init_t stcGpioInit;
	
	LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_SRAM);

	/* 配置SPI引脚 */
	(void)GPIO_StructInit(&stcGpioInit);
    stcGpioInit.u16PinDrv = PIN_HIGH_DRV;

    stcGpioInit.u16PinDrv       = PIN_HIGH_DRV;
 
    (void)GPIO_Init(SPI1_SCLK_PORT, SPI1_SCLK_PIN,  &stcGpioInit);
    (void)GPIO_Init(SPI1_MOSI_PORT, SPI1_MOSI_PIN, &stcGpioInit);
    (void)GPIO_Init(SPI1_MISO_PORT, SPI1_MISO_PIN, &stcGpioInit);

	GPIO_SetFunc(SPI1_SCLK_PORT,    SPI1_SCLK_PIN,  SPI1_SCLK_FUNC);
    GPIO_SetFunc(SPI1_MOSI_PORT,    SPI1_MOSI_PIN, SPI1_MOSI_FUNC);
    GPIO_SetFunc(SPI1_MISO_PORT,    SPI1_MISO_PIN, SPI1_MISO_FUNC);
	
	/* 配置SPI参数 */
	SPI_StructInit(&stcSpiInit);

    FCG_Fcg1PeriphClockCmd(SPI1_CLOCK, ENABLE);
    
    stcSpiInit.u32WireMode          = SPI_3_WIRE;			//3线SPI
    stcSpiInit.u32TransMode         = SPI_FULL_DUPLEX;		//全双工
    stcSpiInit.u32MasterSlave       = SPI_MASTER;			//主机
	stcSpiInit.u32ModeFaultDetect   = SPI_MD_FAULT_DETECT_DISABLE;	// 禁用模式故障检测
    stcSpiInit.u32Parity            = SPI_PARITY_INVD;		//无奇偶校验
    stcSpiInit.u32SpiMode           = SPI_MD_3;				//SPI模式3
    stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV64;	//预分频
    stcSpiInit.u32DataBits          = SPI_DATA_SIZE_8BIT;	//数据宽度
    stcSpiInit.u32FirstBit          = SPI_FIRST_MSB;		//MSB高位有效	
    (void)SPI_Init(SPI1_INSTANCE, &stcSpiInit);
    SPI_Cmd(SPI1_INSTANCE, ENABLE);
	
	LL_PERIPH_WP(LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_SRAM);
	
}

uint8_t hdl_spi1_txrx(uint8_t w_data)
{
	uint8_t r_data = 0;
	uint16_t retry = 0;
	
	while (RESET == SPI_GetStatus(SPI1_INSTANCE, SPI_FLAG_TX_BUF_EMPTY))
	{
		retry++;
		if(retry > 200)
		{
			return 0;
		}
	}
    SPI_WriteData(SPI1_INSTANCE, (uint32_t)w_data);
	
    while (RESET == SPI_GetStatus(SPI1_INSTANCE, SPI_FLAG_RX_BUF_FULL))
	{
		retry++;
		if(retry > 200)
		{
			return 0;
		}
	}
    r_data = SPI_ReadData(SPI1_INSTANCE);
		
    return r_data;
}


void hdl_spi2_init(void)
{
	stc_spi_init_t stcSpiInit;
    stc_gpio_init_t stcGpioInit;
	
	LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_SRAM);

	/* 配置SPI引脚 */
	(void)GPIO_StructInit(&stcGpioInit);
    stcGpioInit.u16PinDrv = PIN_HIGH_DRV;

    stcGpioInit.u16PinDrv       = PIN_HIGH_DRV;
 
    (void)GPIO_Init(SPI2_SCLK_PORT, SPI2_SCLK_PIN,  &stcGpioInit);
    (void)GPIO_Init(SPI2_MOSI_PORT, SPI2_MOSI_PIN, &stcGpioInit);
    (void)GPIO_Init(SPI2_MISO_PORT, SPI2_MISO_PIN, &stcGpioInit);

	GPIO_SetFunc(SPI2_SCLK_PORT,    SPI2_SCLK_PIN,  SPI2_SCLK_FUNC);
    GPIO_SetFunc(SPI2_MOSI_PORT,    SPI2_MOSI_PIN, SPI2_MOSI_FUNC);
    GPIO_SetFunc(SPI2_MISO_PORT,    SPI2_MISO_PIN, SPI2_MISO_FUNC);
	
	/* 配置SPI参数 */
	SPI_StructInit(&stcSpiInit);

    FCG_Fcg1PeriphClockCmd(SPI2_CLOCK, ENABLE);
    
    stcSpiInit.u32WireMode          = SPI_3_WIRE;			//3线SPI
    stcSpiInit.u32TransMode         = SPI_FULL_DUPLEX;		//全双工
    stcSpiInit.u32MasterSlave       = SPI_MASTER;			//主机
	stcSpiInit.u32ModeFaultDetect   = SPI_MD_FAULT_DETECT_DISABLE;	// 禁用模式故障检测
    stcSpiInit.u32Parity            = SPI_PARITY_INVD;		//无奇偶校验
    stcSpiInit.u32SpiMode           = SPI_MD_3;				//SPI模式3
    stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV64;	//预分频
    stcSpiInit.u32DataBits          = SPI_DATA_SIZE_8BIT;	//数据宽度
    stcSpiInit.u32FirstBit          = SPI_FIRST_MSB;		//MSB高位有效	
    (void)SPI_Init(SPI2_INSTANCE, &stcSpiInit);
    SPI_Cmd(SPI2_INSTANCE, ENABLE);
	
	LL_PERIPH_WP(LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_SRAM);
	
}

uint8_t hdl_spi2_txrx(uint8_t w_data)
{
	uint8_t r_data = 0;
	uint16_t retry = 0;
	
	while (RESET == SPI_GetStatus(SPI2_INSTANCE, SPI_FLAG_TX_BUF_EMPTY))
	{
		retry++;
		if(retry > 200)
		{
			return 0;
		}
	}
    SPI_WriteData(SPI2_INSTANCE, (uint32_t)w_data);
	
    while (RESET == SPI_GetStatus(SPI2_INSTANCE, SPI_FLAG_RX_BUF_FULL))
	{
		retry++;
		if(retry > 200)
		{
			return 0;
		}
	}
    r_data = SPI_ReadData(SPI2_INSTANCE);
		
    return r_data;
}



void hdl_spi3_init(void)
{
	stc_spi_init_t stcSpiInit;
    stc_gpio_init_t stcGpioInit;
	
	LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_SRAM);

	/* 配置SPI引脚 */
	(void)GPIO_StructInit(&stcGpioInit);
    stcGpioInit.u16PinDrv = PIN_HIGH_DRV;

    stcGpioInit.u16PinDrv       = PIN_HIGH_DRV;
 
    (void)GPIO_Init(SPI3_SCLK_PORT, SPI3_SCLK_PIN,  &stcGpioInit);
    (void)GPIO_Init(SPI3_MOSI_PORT, SPI3_MOSI_PIN, &stcGpioInit);
    (void)GPIO_Init(SPI3_MISO_PORT, SPI3_MISO_PIN, &stcGpioInit);

	GPIO_SetFunc(SPI3_SCLK_PORT,    SPI3_SCLK_PIN,  SPI3_SCLK_FUNC);
    GPIO_SetFunc(SPI3_MOSI_PORT,    SPI3_MOSI_PIN, SPI3_MOSI_FUNC);
    GPIO_SetFunc(SPI3_MISO_PORT,    SPI3_MISO_PIN, SPI3_MISO_FUNC);
	
	/* 配置SPI参数 */
	SPI_StructInit(&stcSpiInit);

    FCG_Fcg1PeriphClockCmd(SPI3_CLOCK, ENABLE);
    
    stcSpiInit.u32WireMode          = SPI_3_WIRE;			//3线SPI
    stcSpiInit.u32TransMode         = SPI_FULL_DUPLEX;		//全双工
    stcSpiInit.u32MasterSlave       = SPI_MASTER;			//主机
	stcSpiInit.u32ModeFaultDetect   = SPI_MD_FAULT_DETECT_DISABLE;	// 禁用模式故障检测
    stcSpiInit.u32Parity            = SPI_PARITY_INVD;		//无奇偶校验
    stcSpiInit.u32SpiMode           = SPI_MD_3;				//SPI模式3
    stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV64;	//预分频
    stcSpiInit.u32DataBits          = SPI_DATA_SIZE_8BIT;	//数据宽度
    stcSpiInit.u32FirstBit          = SPI_FIRST_MSB;		//MSB高位有效	
    (void)SPI_Init(SPI3_INSTANCE, &stcSpiInit);
    SPI_Cmd(SPI3_INSTANCE, ENABLE);
	
	LL_PERIPH_WP(LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_SRAM);
	
}

uint8_t hdl_spi3_txrx(uint8_t w_data)
{
	uint8_t r_data = 0;
	uint16_t retry = 0;
	
	while (RESET == SPI_GetStatus(SPI3_INSTANCE, SPI_FLAG_TX_BUF_EMPTY))
	{
		retry++;
		if(retry > 200)
		{
			return 0;
		}
	}
    SPI_WriteData(SPI3_INSTANCE, (uint32_t)w_data);
	
    while (RESET == SPI_GetStatus(SPI3_INSTANCE, SPI_FLAG_RX_BUF_FULL))
	{
		retry++;
		if(retry > 200)
		{
			return 0;
		}
	}
    r_data = SPI_ReadData(SPI3_INSTANCE);
		
    return r_data;
}


