#include "Pro_interface.h"
#include "pro_data.h"
#include "pro_hal.h"
#include "pro_phy.h"
#include "bsp.h"

volatile u8 TranFinish_flag,Timeout;

//初始化24L01的IO口
void NRF_GPIO_Init(void)
{ 	
	GPIO_InitTypeDef GPIO_InitStructure;
	SPI_InitTypeDef  SPI_InitStructure;

	RCC_APB2PeriphClockCmd(NRF_CE_GPIO_CLK|NRF_CSN_GPIO_CLK | NRF_SPI_MOSI_GPIO_CLK | NRF_SPI_MISO_GPIO_CLK |
							NRF_SPI_SCK_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
	//RCC_APB2PeriphClockCmd(NRF_SPI_CLK, ENABLE);//SPI1时钟使能
	RCC_APB1PeriphClockCmd(NRF_SPI_CLK, ENABLE);//SPI2时钟使能

	GPIO_InitStructure.GPIO_Pin = NRF_SPI_SCK_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(NRF_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = NRF_SPI_MISO_PIN;
	GPIO_Init(NRF_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = NRF_SPI_MOSI_PIN;
	GPIO_Init(NRF_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = NRF_CSN_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(NRF_CSN_GPIO_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = NRF_CE_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(NRF_CE_GPIO_PORT, &GPIO_InitStructure);

	SPI_Cmd(NRF_SPI, DISABLE);
	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_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(NRF_SPI, &SPI_InitStructure);

	SPI_Cmd(NRF_SPI, ENABLE);
	SPI_SetSpeed(SPI_BaudRatePrescaler_8);//spi速度为9Mhz（24L01的最大SPI时钟为10Mhz）
	
	NRF_CE=0;
	NRF_CSN=1;//SPI片选取消	
}

void NRF_EXTIX_Init(void)
{ 
	EXTI_InitTypeDef EXTI_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	 
	RCC_APB2PeriphClockCmd(NRF_IRQ_GPIO_CLK|RCC_APB2Periph_AFIO, ENABLE);

	GPIO_InitStructure.GPIO_Pin = NRF_IRQ_PIN;
	//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//设置成上拉输入
	GPIO_Init(NRF_IRQ_GPIO_PORT, &GPIO_InitStructure);
	GPIO_EXTILineConfig(NRF_IRQ_EXTI_PORT_SOURCE, NRF_IRQ_EXTI_PIN_SOURCE);

	EXTI_InitStructure.EXTI_Line = NRF_IRQ_EXTI_LINE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = NRF_IRQ_EXTI_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure); 
}

//SPI 速度设置函数
//SpeedSet:
//SPI_BaudRatePrescaler_2   2分频   
//SPI_BaudRatePrescaler_8   8分频   
//SPI_BaudRatePrescaler_16  16分频  
//SPI_BaudRatePrescaler_256 256分频 
void SPI_SetSpeed(const u8 SPI_BaudRatePrescaler)
{
	assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
	NRF_SPI->CR1&=0XFFC7;
	NRF_SPI->CR1|=SPI_BaudRatePrescaler;//设置NRF_SPI速度 
	SPI_Cmd(NRF_SPI,ENABLE); 
} 

//SPIx 读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
u8 SPI_ReadWriteByte(const u8 TxData)
{		
	u8 retry=0;
	
	while(SPI_I2S_GetFlagStatus(NRF_SPI, SPI_I2S_FLAG_TXE) == RESET)//检查指定的SPI标志位设置与否:发送缓存空标志位
	{
		retry++;
		if(retry>200)
		{
			return 0;
		}
	}			  
	SPI_I2S_SendData(NRF_SPI, TxData);//通过外设SPIx发送一个数据
	retry=0;

	while(SPI_I2S_GetFlagStatus(NRF_SPI, SPI_I2S_FLAG_RXNE) == RESET)//检查指定的SPI标志位设置与否:接受缓存非空标志位
	{
		retry++;
		if(retry>200)
		{
			return 0;
		}
	}
	return SPI_I2S_ReceiveData(NRF_SPI);//返回通过SPIx最近接收的数据
}

//读取SPI寄存器值
u8 NRF_ReadReg(const u8 reg)
{
	u8 reg_val;
	
 	NRF_CSN = 0;//使能SPI传输
	SPI_ReadWriteByte(R_REGISTER|reg);//发送寄存器号
	reg_val = SPI_ReadWriteByte(0XFF);//读取寄存器内容
	NRF_CSN = 1;//禁止SPI传输
	return(reg_val);//返回状态值
}

//SPI写寄存器
u8 NRF_WriteReg(const u8 reg,const  u8 value)
{
	u8 status;
	
	NRF_CSN = 0;//使能SPI传输
	status = SPI_ReadWriteByte(W_REGISTER|reg);//发送寄存器号
	SPI_ReadWriteByte(value);//写入寄存器的值
	NRF_CSN = 1;//禁止SPI传输
	return(status);//返回状态值
}

//在指定位置读出指定长度的数据
//返回读到的状态寄存器值 
u8 NRF_ReadBuf(const u8 reg, u8 *pBuf, const u8 len)
{
	u8 status,i;
	
	NRF_CSN = 0;//使能SPI传输
	status=SPI_ReadWriteByte(R_REGISTER|reg);//发送寄存器值(位置),并读取状态值
 	for(i=0; i<len; i++)
	{
		pBuf[i] = SPI_ReadWriteByte(0XFF);//读出数据
	}
	NRF_CSN = 1;//关闭SPI传输
	return status;//返回读到的状态值
}

//在指定位置写指定长度的数据
//返回读到的状态寄存器值
u8 NRF_WriteBuf(const u8 reg,const u8 *pBuf,const u8 len)
{
	u8 status,i;
	
 	NRF_CSN = 0;//使能SPI传输
	status = SPI_ReadWriteByte(W_REGISTER|reg);//发送寄存器值(位置),并读取状态值
	for(i=0; i<len; i++)
	{
		SPI_ReadWriteByte(*pBuf++);//写入数据
	}
	NRF_CSN = 1;//关闭SPI传输
	return status;//返回读到的状态值
}

void NRF_InterruptHandler(void)
{
	u8 status;
	
	status = NRF_ReadReg(STATUS);//读状态寄存器,判断是哪种中断状态
	if(status & RX_DR)//接收到数据
	{
		NRF_RecvData();
	}
	if(status & TX_DS)//发送完成
	{
		TranFinish_flag=TRUE;
	}
	if(status & MAX_RT)//最大重发次数
	{
		Timeout=TRUE;
		NRF_ReadReg(FLUSH_TX);//清发送FIFO
	}
	NRF_WriteReg(STATUS, status);//清除状态标志
}

//外部中断服务程序
void EXTI9_5_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line8) != RESET)//RF有中断了
	{
		NRF_InterruptHandler();//中断处理
		EXTI_ClearITPendingBit(EXTI_Line8);
	}
}
