#include "BSP.H"

/********************************** 功能说明 ***********************************
 *	访问 SPI 总线( SPI1x )
 *******************************************************************************/
// #ifdef	SimulationSPI/*TODEL*/
#define PinBB(_Port, _Num) (*(__IO int32_t *)(PERIPH_BB_BASE + ((uint32_t) & (_Port)-PERIPH_BASE) * 32u + (_Num)*4u))
#define Pin_SPIxSCK        PinBB(GPIOB->ODR, 13U)
#define Pin_SPIxMISO       PinBB(GPIOB->IDR, 14U)
#define Pin_SPIxMOSI       PinBB(GPIOB->ODR, 15U)
uint8_t bus_SPIxShift(uint8_t OutByte)
{
    uint8_t i;

    for (i = 8u; i != 0u; --i) {

        delay_us(30);
        if (OutByte & 0x80u) {
            Pin_SPIxMOSI = 1;
        } else {
            Pin_SPIxMOSI = 0;
        }
        delay_us(30);
        Pin_SPIxSCK = 0;
        delay_us(30);
        OutByte <<= 1;
        if (Pin_SPIxMISO) {
            OutByte |= 0x01u;
        } else {
            OutByte &= 0xFEu;
        }
        delay_us(30);
        Pin_SPIxSCK = 1;
    }
    /*TODEL*/
    Pin_SPIxMOSI = 0; // 1
    return OutByte;
}
void bus_SPIxPortInit(void)
{
    SET_BIT(RCC->APB2ENR, RCC_APB2ENR_AFIOEN);
    MODIFY_REG(AFIO->MAPR, AFIO_MAPR_SWJ_CFG, AFIO_MAPR_SWJ_CFG_JTAGDISABLE);
    SET_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPBEN);
    Pin_SPIxSCK = 1;
    MODIFY_REG(GPIOB->CRH, 0xFFF00000u, 0x74700000u);
}

/// @brief SPI2初始化 CS5552
/// @param      None
/// @retval     None
void SPI2_Configuration(void)
{
    // GPIO_InitTypeDef GPIO_InitStructure;
    // SPI_InitTypeDef SPI_InitStructure;

    // // 使能SPI2时钟
    // RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

    // // 使能GPIOB时钟
    // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    // // 配置SPI2引脚为复用推挽输出
    // GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    // GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_OD;
    // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    // GPIO_Init(GPIOB, &GPIO_InitStructure);

    // // 配置SPI2为主机模式
    // SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
    // SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
    // SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
    // SPI_InitStructure.SPI_CPOL              = SPI_CPOL_High;
    // SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;
    // SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
    // SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
    // SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
    // SPI_InitStructure.SPI_CRCPolynomial     = 7;
    // SPI_Init(SPI2, &SPI_InitStructure);

    // // 使能SPI2
    // SPI_Cmd(SPI2, ENABLE);

    bus_SPIxPortInit();
}
// 定义SPI读取函数
uint8_t SPI2_ReadWrite(uint8_t data)
{
    // // 等待发送缓冲区为空
    // while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
    //     ;

    // // 通过SPI发送数据
    // SPI_I2S_SendData(SPI2, data);

    // // 等待接收缓冲区非空
    // while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
    //     ;

    // // 从SPI接收数据
    // return SPI_I2S_ReceiveData(SPI2);
    return bus_SPIxShift(data);
}


#define Pin_SPIxSCK1        PinBB(GPIOA->ODR, 5U)
#define Pin_SPIxMISO1       PinBB(GPIOA->IDR, 6U)
#define Pin_SPIxMOSI1       PinBB(GPIOA->ODR, 7U)
void SPI1_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;

    // 使能SPI1时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

    // 使能GPIOA时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    // 配置SPI1引脚为复用推挽输出
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 配置SPI1为主机模式
    SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL              = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
    SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
//    SPI_InitStructure.SPI_CRCPolynomial     = 7;
    SPI_Init(SPI1, &SPI_InitStructure);

    // 使能SPI1
    SPI_Cmd(SPI1, ENABLE);



//    SET_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPAEN);
//    Pin_SPIxSCK1 = 1;
//    MODIFY_REG(GPIOA->CRL, 0xFFF00000u, 0x34300000u);

}
uint8_t bus_SPIxShift1(uint8_t OutByte)
{
    uint8_t i;

    for (i = 8u; i != 0u; --i) {

        delay_us(30);
        if (OutByte & 0x80u) {
            Pin_SPIxMOSI1 = 1;
        } else {
            Pin_SPIxMOSI1 = 0;
        }
        delay_us(30);
        Pin_SPIxSCK1 = 0;
        delay_us(30);
        OutByte <<= 1;
        if (Pin_SPIxMISO1) {
            OutByte |= 0x01u;
        } else {
            OutByte &= 0xFEu;
        }
        delay_us(30);
        Pin_SPIxSCK1 = 1;
    }
    /*TODEL*/
    Pin_SPIxMOSI1 = 0; // 1
    return OutByte;
}

// 定义SPI读取函数
uint8_t SPI1_ReadWrite(uint8_t data)
{
	uint16_t retry = 0;
    // 等待发送缓冲区为空
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
		{
			retry++;
			if(retry>=0XFFFE)
				return 0;  //超时退出
		}
		retry = 0;
		while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET)
		{
			retry++;
			if(retry>=0XFFFE)
				return 0;  //超时退出
		}

    // 通过SPI发送数据
    SPI_I2S_SendData(SPI1, data);
		retry = 0;
    // 等待接收缓冲区非空
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
		{
			retry++;
			if(retry>=0XFFFE)
				return 0;  //超时退出
		}

    // 从SPI接收数据
    return SPI_I2S_ReceiveData(SPI1);
	
//	return bus_SPIxShift1(data);
}
