#include "bsp.h"

static 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);
	
	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_CE = 0;
	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_CE = 0;
 	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_Config(const u8 *addr,const u8 channel)
{
	NRF_GPIO_Init();
	NRF_EXTIX_Init();
	TranFinish_flag=0;
	Timeout=0;
	NRF_CE = 0;
	NRF_PowerDown();//掉电模式
	NRF_WriteBuf(RX_ADDR_P0, addr, MAC_LEN);//写通道0,RX地址
	NRF_WriteReg(EN_AA, 0x01);//使能通道0自动应答
	NRF_WriteReg(EN_RXADDR, 0x3f);//使能所有通道的接收地址 
	NRF_WriteReg(SETUP_RETR, 0x71);//等待2000us,重发1次(250Kbps=2000us)
	NRF_WriteReg(SETUP_AW, 0x03);//地址宽度5
	NRF_WriteReg(RF_SETUP, 0x27);//设置TX发射参数,0db增益,250Kbps,低噪声增益开启
	NRF_WriteReg(DYNPD, 0x3f);//所有通道使能动态长度
	NRF_WriteReg(FEATRUE, 0x06);//使能动态长度,使能数据应答
	NRF_WriteReg(STATUS, IRQ_ALL);//清除状态标志
	NRF_ReadReg(FLUSH_RX);//清接收FIFO
	NRF_ReadReg(FLUSH_TX);//清发送FIFO
	NRF_DelayUs(2000);//掉电模式->接收模式前必须经过1.5ms的待机模式
	NRF_RxMode(addr, channel);//初始化为接收模式
	NRF_DelayUs(150);//待机模式->发送/接收模式130us
}

void NRF_SetPipe1To4Mac(const u8 *addr)
{
	NRF_CE = 0;
	NRF_WriteBuf(RX_ADDR_P1, addr, MAC_LEN);//写通道1,RX地址
	NRF_WriteReg(RX_ADDR_P2, addr[0]-1);//写通道2,RX地址
	NRF_WriteReg(RX_ADDR_P3, addr[0]-2);//写通道3,RX地址
	NRF_WriteReg(RX_ADDR_P4, addr[0]-3);//写通道4,RX地址
	NRF_WriteReg(CONFIG, 0x0B);//接收模式
	NRF_CE = 1;
}

u8 NRF_SetRfPower(const u8 level)//(0-3)
{
	u8 state,power;
	if(level<4)
	{
		NRF_CE = 0;
		power=level;
		state=NRF_ReadReg(RF_SETUP);//读参数
		state&=0XF9;//清power位(B11111001)
		power&=0x03;//设置power(B00000011)
		NRF_WriteReg(RF_SETUP, state|(power<<1));//设置power位
		return state;
	}
	return 0;
}

u8 NRF_ReadChannel(void)
{
	return NRF_ReadReg(RF_CH);//读频道
}

void NRF_SetChannel(const u8 channel)
{
	if(channel<126)
	{
		NRF_CE = 0;
		NRF_WriteReg(RF_CH, channel);//设置RF通信频率
	}
}

//检测当前频点的外部环境信号强度
u8 NRF_DisturbanceDetect(void)
{
	u8 state;
	NRF_CE = 0;
	NRF_DelayUs(1);
	NRF_WriteReg(CONFIG, 0x0B);//RCRC:8,接收模式
	NRF_CE = 1;
	NRF_DelayUs(200);//待机模式->接收模式(130us)+40us
	state = NRF_ReadReg(CD);
	return state;
}

//发送数据
u8 NRF_SendPayload(const u8 ack,const u8 *addr,const u8 channel,const u8 *buf)
{
	u16 i=1;
	u8 k;
	printf("Send_Addr=");
	for(k=0;k<MAC_LEN;k++)
	{
		printf("%02x ",addr[k]);
	}
	printf("\r\n");
	NRF_CE = 0;
	NRF_WriteBuf(TX_ADDR, addr, MAC_LEN);//写TX节点地址
	if(ack)
	{
		NRF_WriteBuf(RX_ADDR_P0, addr, MAC_LEN);//写通道0,RX地址
	}
	NRF_WriteReg(RF_CH, channel);//设置RF通信频率
	NRF_WriteReg(STATUS, IRQ_ALL);//清除状态标志
	NRF_ReadReg(FLUSH_RX);//清接收FIFO
	NRF_ReadReg(FLUSH_TX);//清发送FIFO
	NRF_WriteBuf(W_TX_PAYLOAD, buf, PAYLOAD_LEN);
	NRF_WriteReg(CONFIG, 0x0A);//RX_DR,TX_DS,MAX_RT中断使能,CRC:8,POWER_UP,发送模式
	NRF_CE = 1;//设置CE为高启动发射,CE高电平持续时间最小为10us
	NRF_DelayUs(10);
	while(i++)
	{
		if(TranFinish_flag)
		{
			TranFinish_flag = 0;
			return TRUE;
		}
		if(Timeout)
		{
			Timeout = 0;
			NRF_ReadReg(FLUSH_TX);//清发送FIFO
			return FALSE;
		}
	}
	NRF_WriteReg(STATUS, IRQ_ALL);//清除状态标志
	NRF_ReadReg(FLUSH_TX);//清发送FIFO
	return FALSE;
}

//接收模式
void NRF_RxMode(const u8 *addr,const u8 channel)
{
	NRF_CE = 0;
	NRF_WriteBuf(RX_ADDR_P0, addr, MAC_LEN);//写RX节点地址
	NRF_WriteReg(RF_CH, channel);//设置RF通信频率
	NRF_ReadReg(FLUSH_RX);//清接收FIFO
	NRF_WriteReg(CONFIG, 0x0B);//RX_DR,TX_DS,MAX_RT中断使能,CRC:8,POWER_UP,接收模式
	NRF_CE = 1;//CE为高,进入接收模式
}

//void NRF_RecvData(void)
//{
//	Payload tmp;
//	u8 status,pipe_num;
//	NRF_CE = 0;
//	MemSet((BYTE *)&tmp,0,sizeof(Payload));
//	status=NRF_ReadReg(STATUS);//读取status寄存器的值
//	pipe_num=(status&RX_P_NO_MASK)>>1;//读取通道
//	NRF_ReadBuf(R_RX_PAYLOAD,(u8 *)&tmp,PAYLOAD_LEN);//保存接收到的数据
////	if(RecvHandler)//回调
////	{
////		RecvHandler(tmp.header.data,tmp.header.pack_type,
////					tmp.header.ID,tmp.buf,pipe_num);
////	}
//	NRF_RxMode(NodeStatus.node_mac,NodeStatus.channel);
//}

void NRF_PowerDown(void)
{
	NRF_CE = 0;
	NRF_WriteReg(CONFIG, 0x79);//RX_DR,TX_DS,MAX_RT中断屏蔽,CRC:8,POWER_DOWN,接收模式
}

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

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

void EXTI9_5Close(void)
{
	EXTI_InitTypeDef EXTI_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	EXTI_InitStructure.EXTI_Line = NRF_IRQ_EXTI_LINE;
	EXTI_InitStructure.EXTI_LineCmd = DISABLE;
	EXTI_Init(&EXTI_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = NRF_IRQ_EXTI_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	NVIC_Init(&NVIC_InitStructure);

    EXTI_ClearITPendingBit(EXTI_Line8);
	//OFF_exti(8);
}

void NRF_DelayUs(u16 i)
{
	TIM_DelayUs(i);//1~65535us延时	
}

//void NRF_DelayUs(u16 i)
//{
//	u16 k,j;
//	for(k=0;k<i;k++)
//		for(j=0;j<10;j++);
//}

