/********************************************************************************
 * @file    SPI_Communication.c
 * @author  SOC AE Team
 * @version V1.1
 * @date    2025-06-27
 * @brief   SPI0作为主机，SPI1作为从机，两者互相通信。（主从机模式请在宏定义进行选择，SPI_Mode_=0为主机代码，SPI_Mode_=1为从机代码）
 *
 *******************************************************************************/
#include "sc32_conf.h"
#include "Function_Init.h"

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

#if (SPI_Mode_ == 0)
static volatile FlagStatus SPI0_Flag = RESET;
const uint8_t Master_SendData[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
#elif (SPI_Mode_ == 1)
static volatile FlagStatus SPI1_Flag = RESET;
const uint8_t Slave_SendData[8] = {0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15};
#endif

void SPI_Communication_Function(void)
{
  SPI_InitTypeDef SPI_InitStruct; //定义SPI初始化结构体变量
 
#if (SPI_Mode_ == 0)	
  volatile uint8_t Master_ReceiveData[8];
  volatile uint8_t SPI0_Data, SPI0_Num = 0;;
  GPIO_InitTypeDef GPIO_InitStruct; //定义GPIO初始化结构体变量
  /* 使能SPI0的时钟 */
  RCC_APB0Cmd(ENABLE);  //SPI0外设挂载在APB0时钟上
#if defined(SC32f12xx)||defined(SC32f10xx)
  RCC_APB0PeriphClockCmd(RCC_APB0Periph_SPI0, ENABLE);
#endif
#if defined(SC32f12xx)
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_Init(GPIOC, &GPIO_InitStruct);
	PC_BIT(2) = 1;
#elif defined(SC32f10xx)
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_Init(GPIOB, &GPIO_InitStruct);
	PB_BIT(1) = 1;

#endif

  /*  配置SPI0为主机 */
  SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;	//SPI第一沿采集数据
  SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;	//SPI空闲电平为高电平
  SPI_InitStruct.SPI_DataSize = SPI_DataSize_8B;	//数据长度为8位
  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(SPI0, &SPI_InitStruct);


#if defined(SC32f12xx) ||defined(SC32f10xx)
  /* 使能SPI0中断 */
  SPI_ITConfig(SPI0, SPI_IT_INTEN, ENABLE);
  NVIC_EnableIRQ(SPI0_IRQn);
#endif
  /* 使能SPI0 */
  SPI_Cmd(SPI0, ENABLE);
  
#elif (SPI_Mode_ == 1)
  volatile uint8_t Slave_ReceiveData[8];
  volatile uint8_t SPI1_Data, SPI1_Num = 0;

  /* 使能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_High;	//SPI空闲电平为高电平
  SPI_InitStruct.SPI_DataSize = SPI_DataSize_8B;	//数据长度为8位
  SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;	//数值传输为高位在前
  SPI_InitStruct.SPI_Prescaler = SPI_Prescaler_1024;	//SPI传输频率为APB1时钟1024分频
  SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;	//SPI工作在从机模式
  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_ == 0)
    GPIO_SetBits( GPIOC, GPIO_Pin_2);
  //  SPI0发送第一个数据
    SPI_SendData(SPI0,Master_SendData[SPI0_Num]);
    
    while(SPI0_Num < 7)
    {
      
      while(SPI0_Flag == RESET);
      SPI0_Flag = RESET;
      Master_ReceiveData[SPI0_Num] = SPI_ReceiveData(SPI0);//主机SPI0接收数据
      SPI0_Num++;
      SPI_SendData(SPI0, Master_SendData[SPI0_Num]);	//主机SPI0发送		
      
    }
    
      while(SPI0_Flag == RESET);
    Master_ReceiveData[SPI0_Num] = SPI_ReceiveData(SPI0);	//SPI0接收最后一个数据
    for(int i=0 ;i <= 7 ;i++)	printf("SPI0主机接收到的数据为“0x%x”\r\n",Master_ReceiveData[i]);
   for(int time=1000;time>0;time--)
   {
      for(int time1 = 1000;time1>0;time1--);
   };
    /* 清除标志位，为下一次通信做准备 */
   
    SPI0_Flag = RESET;
     SPI0_Num = 0;

#elif (SPI_Mode_ == 1)
   
    //SPI1发送第一个数据
    SPI_SendData(SPI1,Slave_SendData[SPI1_Num]);
    
    while(SPI1_Num < 7)
    {
      while(SPI1_Flag == RESET);	  
      SPI1_Flag = RESET;      
      Slave_ReceiveData[SPI1_Num] = SPI_ReceiveData(SPI1);//从机SPI1接收数据
      SPI1_Num ++;
      SPI_SendData(SPI1, Slave_SendData[SPI1_Num]);	//从机SPI1发送数据
    }
    
    while(SPI1_Flag == RESET);
    Slave_ReceiveData[SPI1_Num] = SPI_ReceiveData(SPI1);	//SPI1接收最后一个数据
   for(int i=0 ;i <= 7 ;i++)	printf("SPI1从机接收到的数据为“0x%x”\r\n",Slave_ReceiveData[i]);
    
    /* 清除标志位，为下一次通信做准备 */
    SPI1_Num = 0;
    SPI1_Flag = RESET;	
#endif	
    
  }
}
#if defined(SC32f12xx) ||defined(SC32f10xx)
void SPI_Communication_SPI0Handler(void)
{
#if (SPI_Mode_ == 0)
  if(SPI_GetFlagStatus(SPI0, SPI_Flag_SPIF))
  {
    
    SPI_ClearFlag(SPI0, SPI_Flag_SPIF);
    SPI0_Flag = SET;
  }
#endif

}

#endif
#if defined(SC32f12xx)
void SPI_Communication_SPI1_2_Handler(void)
{
 
#if (SPI_Mode_ == 1)
  if(SPI_GetFlagStatus(SPI1, SPI_Flag_SPIF))
  {
    SPI_ClearFlag(SPI1, SPI_Flag_SPIF);
    SPI1_Flag = SET;
  }
#endif
}
#elif defined(SC32f10xx)
void SPI_Communication_SPI1_Handler(void)
{
 
#if (SPI_Mode_ == 1)
  if(SPI_GetFlagStatus(SPI1, SPI_Flag_SPIF))
  {
    SPI_ClearFlag(SPI1, SPI_Flag_SPIF);
    SPI1_Flag = SET;
  }
#endif
}

#endif
