/********************************************************************************
 * @file    SPI_FIFOSlave.c
 * @author  SOC AE Team
 * @version V1.1
 * @date    2025-06-27
 * @brief		SPI0作为从机，SPI1作为主机，SPI0使用FIFO收发数据。（主从机模式请在宏定义进行选择，SPI_Mode_=0为主机代码，SPI_Mode_=1为从机代码）
 *
 *******************************************************************************/
#include "sc32_conf.h"
#include "Function_Init.h"

/* 请选择主从机代码 */
#define SPI_Mode_ 0

#if (SPI_Mode_ == 1)
uint16_t FIFOSlave_SPI0_SendData[16] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xff};
#elif (SPI_Mode_ == 0)
static volatile FlagStatus SPI1_Flag = RESET;
uint16_t FIFOSlave_SPI1_SendData[16] = {0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x2f};
#endif

void SPI_FIFOSlave_Function(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_Init(GPIOB, &GPIO_InitStruct);
	SPI_InitTypeDef SPI_InitStruct; //定义SPI初始化结构体变量
	
#if (SPI_Mode_ == 1)
	volatile uint16_t FIFOSlave_SPI0_ReceiveData[16];

  /* 使能SPI0的时钟 */
  RCC_APB0Cmd(ENABLE);  //SPI0外设挂载在APB0时钟上
#if defined(SC32f12xx)||defined(SC32f10xx)
  RCC_APB0PeriphClockCmd(RCC_APB0Periph_SPI0, ENABLE);

#endif
	
  /*  配置SPI0为从机 */
	SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;	//SPI第一沿采集数据
	SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;	//SPI空闲电平为低电平
	SPI_InitStruct.SPI_DataSize = SPI_DataSize_16B;	//数据长度为16位
	SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;	//数值传输为高位在前
	SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;	//SPI工作在从机模式
	SPI_InitStruct.SPI_Prescaler = SPI_Prescaler_1024;	//SPI传输频率为APB0时钟1024分频	
	SPI_Init(SPI0,&SPI_InitStruct);		

	/* 使能SPI0 */
	SPI_Cmd(SPI0,ENABLE);
	
	//PB_OT(11) ;
	
#elif (SPI_Mode_ == 0)
  volatile uint16_t FIFOSlave_SPI1_ReceiveData[16];
	volatile uint16_t SPI1_Num = 0;
	
#if defined(SC32f12xx)
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_Init(GPIOA, &GPIO_InitStruct);
	PA_BIT(1) = 1;
#elif defined(SC32f10xx)
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_Init(GPIOA, &GPIO_InitStruct);
	PA_BIT(13) = 1;
#endif
  /* 使能SPI1的时钟 */
   RCC_APB1Cmd(ENABLE);//SPI1外设挂载在APB1时钟上 
#if defined(SC32f12xx)
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI1, ENABLE);
#elif defined(SC32f10xx)
   RCC_APB1Cmd(ENABLE);//SPI1外设挂载在APB1时钟上
#endif
	
  /* 配置SPI1为主机 */
	SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;	//SPI第一沿采集数据
	SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;	//SPI空闲电平为低电平
	SPI_InitStruct.SPI_DataSize = SPI_DataSize_16B;	//数据长度为16位
	SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;	//数值传输为高位在前
	SPI_InitStruct.SPI_Mode = SPI_Mode_Master;	//SPI工作在主机模式
	SPI_InitStruct.SPI_Prescaler = SPI_Prescaler_1024;	//SPI传输频率为APB0时钟1024分频
	SPI_Init(SPI1,&SPI_InitStruct);		
	
#if defined(SC32f12xx)
  /* 使能SPI1中断 */
  SPI_ITConfig(SPI1, SPI_IT_INTEN, ENABLE);
  NVIC_EnableIRQ(SPI1_2_IRQn);
#elif defined(SC32f10xx)
  /* 使能SPI1中断 */
  SPI_ITConfig(SPI1, SPI_IT_INTEN, ENABLE);
  NVIC_EnableIRQ(SPI1_IRQn);

#endif

  /* 使能SPI1 */
	SPI_Cmd(SPI1,ENABLE);
#endif
	
	
	while(1)
	{
#if (SPI_Mode_ == 1)

   	SPI_SendDataFIFO(SPI0,(uint32_t *)FIFOSlave_SPI0_SendData,8);//写入前8个数据

		while(SPI_GetFlagStatus(SPI0,SPI_Flag_RXFIF) == RESET); //等待接收FIFO已满
		SPI_ReceiveDataFIFO(SPI0,(uint32_t *)FIFOSlave_SPI0_ReceiveData,8); //读取8个数据
		
		SPI_SendDataFIFO(SPI0,(uint32_t *)(FIFOSlave_SPI0_SendData+8),8);//写入后8个数据
		
		while(SPI_GetFlagStatus(SPI0,SPI_Flag_RXFIF) == RESET); //等待接收FIFO已满
		SPI_ReceiveDataFIFO(SPI0,(uint32_t *)(FIFOSlave_SPI0_ReceiveData+8),8); //读取后8个数据
	
		for(int i=0 ;i <= 15 ;i++)	printf("SPI0从机接收到的数据为“0x%x”\r\n",FIFOSlave_SPI0_ReceiveData[i]);
		printf("\r\n");
#elif (SPI_Mode_ == 0)
			
		
		
		/* SPI1没有FIFO，需要手动发送/接收数据 */
			for(SPI1_Num=0; SPI1_Num <= 15;SPI1_Num ++)
		{
    
     SPI_SendData(SPI1,FIFOSlave_SPI1_SendData[SPI1_Num]);
		 while(SPI1_Flag == RESET);
		 SPI1_Flag = RESET;
	   FIFOSlave_SPI1_ReceiveData[SPI1_Num] = SPI_ReceiveData(SPI1);//SPI1接收数据
		  PB_OT(11) ;
				
		}

		
		for(int i=0 ;i <= 15 ;i++)	printf("SPI1主机接收到的数据为“0x%x”！\r\n",FIFOSlave_SPI1_ReceiveData[i]);
		printf("SPI1主机接收到的数据为OK！\r\n");
		/* 清除标志位，为下一次通信做准备 */
		SPI1_Num=0;
		SPI1_Flag = RESET;
#endif
		
	}
}


void SPI_FIFOSlave_SPI1Handler(void)
{

#if (SPI_Mode_ == 0)
#if defined(SC32f10xx)||defined(SC32f12xx)
	if(SPI_GetFlagStatus(SPI1,SPI_Flag_SPIF))
	{
		SPI_ClearFlag(SPI1,SPI_Flag_SPIF);
		SPI1_Flag = SET;
	}
	
#endif
#endif

}
