#include "Simulated_SPI_1_Master.h"
#include "Port_Cfg.h"
#include "Port.h"
#include "Dio.h"

SimSPIConfigStruct  SimSPIConfig_1;

/*时钟输出高*/
void SGMCD1020Q_Clock_Output_High(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_00, STD_HIGH);
}
/*时钟输出低*/
void SGMCD1020Q_Clock_Output_Low(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_00, STD_LOW);
}
/*时钟翻转*/
void SGMCD1020Q_Clock_Output_Flip(void)
{
  Dio_FlipChannel(DioConf_DioChannel_P02_00);
}

/*MOSI输出高*/
void SGMCD1020Q_MOSI_Output_High(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_05, STD_HIGH);
}
/*MOSI输出低*/
void SGMCD1020Q_MOSI_Output_Low(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_05, STD_LOW);
}

/*CS1输出高*/
void SGMCD1020Q_CS1_Output_High(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_02, STD_HIGH);
}
/*CS1输出低*/
void SGMCD1020Q_CS1_Output_Low(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_02, STD_LOW);
}

/*CS2输出高*/
void SGMCD1020Q_CS2_Output_High(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_06, STD_HIGH);
}
/*CS2输出低*/
void SGMCD1020Q_CS2_Output_Low(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P02_06, STD_LOW);  
}

/*CS3输出高*/
void SGMCD1020Q_CS3_Output_High(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P06_03, STD_HIGH);
}
/*CS3输出低*/
void SGMCD1020Q_CS3_Output_Low(void)
{
  Dio_WriteChannel(DioConf_DioChannel_P06_03, STD_LOW);  
}
/*MISO*/
/*Dio_ReadChannel(Port_Config_PortGroup02_PortPin03);*/

/******************************************************************************
��������SPI_Master_1_Start
��  �ܣ���ʼ��������SPI����ģʽ
��  ����Mode��SPI����ģʽ��         SIM_SPI_MODE_0
                                    SIM_SPI_MODE_1
                                    SIM_SPI_MODE_2
                                    SIM_SPI_MODE_3
                                    
        FirstBit�����ȷ��͵�����λ��SIM_SPI_MSB_FIRST
                                    SIM_SPI_LSB_FIRST
����ֵ����
******************************************************************************/
void Sim_SPI_Master_1_Start(uint8_t Mode, uint8_t FirstBit)
{
  switch (Mode)
  {
    case SIM_SPI_MODE_0 : SimSPIConfig_1.CPOL = 0;
                          SimSPIConfig_1.CPHA = 0;
                          break;
                          
    case SIM_SPI_MODE_1 : SimSPIConfig_1.CPOL = 0;
                          SimSPIConfig_1.CPHA = 1;
                          break;
                          
    case SIM_SPI_MODE_2 : SimSPIConfig_1.CPOL = 1;
                          SimSPIConfig_1.CPHA = 0;
                          break;
                          
    case SIM_SPI_MODE_3 : SimSPIConfig_1.CPOL = 1;
                          SimSPIConfig_1.CPHA = 1;
                          break;
                          
    default             : SimSPIConfig_1.CPOL = 0;
                          SimSPIConfig_1.CPHA = 0;
                          break;                          
  }
  SimSPIConfig_1.FirstBit = FirstBit;
    
  // SPI_SCLK = SimSPIConfig_1.CPOL;
  Dio_WriteChannel(DioConf_DioChannel_P02_00, SimSPIConfig_1.CPOL);
  
  SGMCD1020Q_MOSI_Output_Low();
}

/******************************************************************************
��������Sim_SPI_Master_1_Byte_Write
��  �ܣ���SPI������дһ�ֽ�
��  ����Data:Ҫд�������
����ֵ����
******************************************************************************/
void Sim_SPI_Master_1_Byte_Write(uint8_t Data)
{
  uint8_t BitPos;
  
  if (SimSPIConfig_1.FirstBit == SIM_SPI_MSB_FIRST)
  {
    for (BitPos = 0x80; BitPos >= 0x01; BitPos >>= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
    }
  }
  else
  {
    for (BitPos = 0x01; BitPos <= 0x80; BitPos <<= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
    }
  }
}

  uint8_t BitPosfsfds;
/******************************************************************************
��������Sim_SPI_Master_1_Byte_Read
��  �ܣ���SPI�����϶�ȡһ�ֽ�����
��  ����TxData���ڶ�ȡ��ͬʱд�������
����ֵ������������
******************************************************************************/
uint8_t Sim_SPI_Master_1_Byte_Read(uint8_t TxData)
{
  uint8_t RxData;
  uint8_t BitPos;
  RxData = 0x00;
  // BitPosfsfds=Dio_ReadChannel(Port_Config_PortGroup2_PortPin3);

  if (SimSPIConfig_1.FirstBit == SIM_SPI_MSB_FIRST)
  {
    for (BitPos = 0x80; BitPos >= 0x01; BitPos >>= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      else
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
    }
  }
  else
  {
    for (BitPos = 0x01; BitPos <= 0x80; BitPos <<= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      else
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
    }
  }

  return RxData;
}

/******************************************************************************
��������Sim_SPI_Master_1_Word_Write
��  �ܣ���SPI������дһ����(16bit)������
��  ����Data:Ҫд�������
����ֵ����
******************************************************************************/
void Sim_SPI_Master_1_Word_Write(uint16_t Data)
{
  uint16_t BitPos;
  
  if (SimSPIConfig_1.FirstBit == SIM_SPI_MSB_FIRST)
  {
    for (BitPos = 0x8000; BitPos >= 0x0001; BitPos >>= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
    }
  }
  else
  {
    for (BitPos = 0x0001; BitPos <= 0x8000; BitPos <<= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (Data & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
    }
  }
}

/******************************************************************************
��������Sim_SPI_Master_1_Word_Read
��  �ܣ���SPI�����϶�ȡһ����(16bit)������
��  ����TxData���ڶ�ȡ��ͬʱд�������
����ֵ������������
******************************************************************************/
uint16_t Sim_SPI_Master_1_Word_Read(uint16_t TxData)
{
  uint16_t RxData;
  uint16_t BitPos;
  
  RxData = 0x00;

  if (SimSPIConfig_1.FirstBit == SIM_SPI_MSB_FIRST)
  {
    for (BitPos = 0x8000; BitPos >= 0x0001; BitPos >>= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      else
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
    }
  }
  else
  {
    for (BitPos = 0x0001; BitPos <= 0x8000; BitPos <<= 1)
    {
      if (SimSPIConfig_1.CPHA == 0)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        if (TxData & BitPos)
          SGMCD1020Q_MOSI_Output_High();
        else
          SGMCD1020Q_MOSI_Output_Low();
        
        NOP();
        NOP();
        NOP();
      }
      else
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
      
      NOP();
      SGMCD1020Q_Clock_Output_Flip();
      
      if (SimSPIConfig_1.CPHA)
      {
        NOP();
        NOP();
        NOP();
        
        if (Dio_ReadChannel(Port_Config_PortGroup2_PortPin3))
          RxData |= BitPos;
      }
    }
  }

  return RxData;
}
