24L01收发一体多线程测试demo

一、<

/h2>

1.初始化

	NRF24L01_Init();
	if(NRF24L01_Check()==0)
	{
	
		nRF24L01_data_init();
		main_data_1.NRF24L01_statue=1;
	}
	else
	{
	 
		main_data_1.NRF24L01_statue=0;
	}

2.发送线程

void NRF24L01_SEND_task(void *pvParameters)
{
	//u8 send_time=0;
	u8 send_data[32]={0};
	u8 rec_data[32]={0};
	u8 i=0;
	u8 t_d=0;
	u8 rec_time=0;
	u8 SEND_statue=0;
	BaseType_t statue;
	//BaseType_t err=pdFALSE;
	printf("Free heap: %d bytes
", xPortGetFreeHeapSize());			/*′òó?ê£óà????′óD?*/	
	while(1)
	{
		  if(main_data_1.NRF24L01_statue==1)
			{

				send_data[0]=0xbb;
				send_data[1]=0xbf;
				send_data[2]=0x01;
				send_data[3]=0x1b;
				for(i=4;i<31;i++)
				{
				  send_data[i]=0x00;
				}
				send_data[31]=0x96 ;
				for(i=0;i<32;i++)
					{
						printf(" %x ", send_data[i]);
					}
					printf("
");
					xSemaphoreTake(BinarySemaphore,portMAX_DELAY); //??è?D?o?á?£??àμè
					nRF24L01_TxPacket(send_data);
					xSemaphoreGive(BinarySemaphore ); //·¢?í

				
			}		
		vTaskDelay(10);
	}

}

3.接收线程

void NRF24L01_REC_task(void *pvParameters)
{
	BaseType_t err=pdFALSE;
	u8 statue=0;
	u8 RxBuf[32]={0};
	u8 rec_time=0;
	
	while(1)
	{
		if(main_data_1.NRF24L01_statue==1)
		{

			xSemaphoreTake(BinarySemaphore,portMAX_DELAY); //??è?D?o?á?£??àμè
			//delay_us(800);
			SetRX_Mode();
			statue=nRF24L01_RxPacket(RxBuf);
			xSemaphoreGive(BinarySemaphore ); //·¢?í
			if(statue==0)
				{
						main_data_1.not_rec_time=0;
						LED3(1);
				}
		}	
	  vTaskDelay(1);
	}
}

4. 24L01.C

#include "24l01.h"


SPI_HandleTypeDef hspi2;
extern QueueHandle_t   Message_Queue;		//???¢?óáD

void MX_SPI2_Init(void)
{

  hspi2.Instance = SPI2;//SPI1
  hspi2.Init.Mode = SPI_MODE_MASTER;//éè??SPI1¤×÷?£ê?£?éè???a?÷?£ê?
  hspi2.Init.Direction = SPI_DIRECTION_2LINES;//éè??SPIμ¥?ò?ò?????òμ?êy?Y?£ê?:SPIéè???a?????£ê?
  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;//éè??SPIμ?êy?Y′óD?:SPI·¢?í?óê?8?????á11
  hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; //′?DDí?2?ê±?óμ????D×′ì??a??μ???
  hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; //′?DDí?2?ê±?óμ?μú?t??ì?±???£¨é?éy?ò???μ£?êy?Y±?2é?ù
  hspi2.Init.NSS = SPI_NSS_SOFT;//NSSD?o?óéó2?t£¨NSS1ü??£??1ê?èí?t£¨ê1ó?SSI??£?1üàí:?ú2?NSSD?o?óDSSI??????
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;//?¨ò?2¨ì??ê?¤·??μμ??μ:2¨ì??ê?¤·??μ?μ?a256
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;//???¨êy?Y′?ê?′óMSB???1ê?LSB???aê?:êy?Y′?ê?′óMSB???aê?
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;//1?±?TI?£ê?
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;//1?±?ó2?tCRCD£?é
  hspi2.Init.CRCPolynomial = 7; //CRC?μ????μ??à??ê?
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    Error_Handler();
  }


	__HAL_SPI_ENABLE(&hspi2);//ê1?üSPI1
	
	 SPI2_ReadWriteByte(0Xff);   
	
}

//SPI2μ×2??y?ˉ£?ê±?óê1?ü£?òy??????
//′?oˉêy?á±?HAL_SPI_Init()μ÷ó?
//hspi:SPI??±ú
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(spiHandle->Instance==SPI2)
  {
		__HAL_RCC_SPI2_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**SPI2 GPIO Configuration    
    PB13     ------> SPI2_SCK
    PB14     ------> SPI2_MISO
    PB15     ------> SPI2_MOSI 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_14;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* SPI2 interrupt Init */
    HAL_NVIC_SetPriority(SPI2_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(SPI2_IRQn);
  }
}

//SPI?ù?èéè??oˉêy
//SPI?ù?è=fAPB1/·??μ?μêy
//@ref SPI_BaudRate_Prescaler:SPI_BAUDRATEPRESCALER_2~SPI_BAUDRATEPRESCALER_2 256
//fAPB1ê±?óò?°??a42Mhz£o
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
    assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//?D??óDD§D?
    __HAL_SPI_DISABLE(&hspi2);            //1?±?SPI
    hspi2.Instance->CR1&=0XFFC7;          //??3-5??á?£?ó?à′éè??2¨ì??ê
    hspi2.Instance->CR1|=SPI_BaudRatePrescaler;//éè??SPI?ù?è
    __HAL_SPI_ENABLE(&hspi2);             //ê1?üSPI
    
}

//SPI1 ?áD′ò???×??ú
//TxData:òaD′è?μ?×??ú
//·μ???μ:?áè?μ?μ?×??ú
u8 SPI2_ReadWriteByte(u8 TxData)
{
    u8 Rxdata;
    HAL_SPI_TransmitReceive(&hspi2,&TxData,&Rxdata,1, 1000);       
		return Rxdata;          		    //·μ??ê?μ?μ?êy?Y		
}


/**********************************************************************************/
/***********************************24l01***********************************************/
/**********************************************************************************/

const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x00,0x00,0x00,0x00,0x01}; //·¢?íμ??·
const u8 RX_ADDRESS[RX_ADR_WIDTH]={0x00,0x00,0x00,0x00,0x01}; //·¢?íμ??·



//????NRF24L01DT??SPI1?y?ˉ
void NRF24L01_SPI_Init(void)
{
    __HAL_SPI_DISABLE(&hspi2);               //?è1?±?SPI2
    hspi2.Init.CLKPolarity=SPI_POLARITY_LOW; //′?DDí?2?ê±?óμ????D×′ì??aμíμ???
    hspi2.Init.CLKPhase=SPI_PHASE_1EDGE;     //′?DDí?2?ê±?óμ?μú1??ì?±???£¨é?éy?ò???μ£?êy?Y±?2é?ù
    HAL_SPI_Init(&hspi2);
    __HAL_SPI_ENABLE(&hspi2);                //ê1?üSPI2
}


//3?ê??ˉ24L01μ?IO?ú
void NRF24L01_Init(void)
{
    GPIO_InitTypeDef GPIO_Initure;
    __HAL_RCC_GPIOA_CLK_ENABLE();			//?a??GPIOBê±?ó
    __HAL_RCC_GPIOB_CLK_ENABLE();			//?a??GPIOGê±?ó
    
	//CSN
    //GPIO_Initure.Pin=GPIO_PIN_12; 	
		GPIO_Initure.Pin=GPIO_PIN_4; 		
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;  //í?íìê?3?
    GPIO_Initure.Pull=GPIO_PULLUP;          //é?à-
    GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;     //???ù
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);     //3?ê??ˉ

	//CE
    //GPIO_Initure.Pin=GPIO_PIN_8; 		
		GPIO_Initure.Pin=GPIO_PIN_3;	
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;  //í?íìê?3?
    GPIO_Initure.Pull=GPIO_PULLUP;          //é?à-
    GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;     //???ù
    HAL_GPIO_Init(GPIOB,&GPIO_Initure);     //3?ê??ˉ
	
	//IRQ
	//GPIO_Initure.Pin=GPIO_PIN_9;		
	GPIO_Initure.Pin=GPIO_PIN_5;		
	GPIO_Initure.Mode=GPIO_MODE_INPUT;      //ê?è?
	HAL_GPIO_Init(GPIOB,&GPIO_Initure);     //3?ê??ˉ
	
	
    
	MX_SPI2_Init();    		                //3?ê??ˉSPI1
  //NRF24L01_SPI_Init();                    //????NRFμ?ì?μ?DT??SPIμ?éè??	
	NRF24L01_CE(0); 			                //ê1?ü24L01
	NRF24L01_CSN(1);			                //SPI????è???	 		 	 
	
	
}


//?ì2a24L01ê?·?′??ú
//·μ???μ:0£?3é1|;1£?꧰ü	
u8 NRF24L01_Check(void)
{
	u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
	u8 i;
	SPI2_SetSpeed(SPI_BAUDRATEPRESCALER_8);   //  spi?ù?è?a10.5Mhz£¨£¨24L01μ?×?′óSPIê±?ó?a10Mhz,?aà?′óò?μ???1??μ£?    	 
	NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);//D′è?5??×??úμ?μ??·.	
	NRF24L01_Read_Buf(TX_ADDR,buf,5); //?á3?D′è?μ?μ??·  
	for(i=0;i<5;i++)if(buf[i]!=0XA5)break;	 							   
	if(i!=5)
	{
	  return 1;//?ì2a24L01′í?ó	
	}	
	init_NRF24L01();
	return 0;		 //?ì2aμ?24L01
}	 	 


//SPID′??′??÷
//reg:???¨??′??÷μ??·
//value:D′è?μ??μ
u8 NRF24L01_Write_Reg(u8 reg,u8 value)
{
	u8 status;	
   	NRF24L01_CSN(0);                 //ê1?üSPI′?ê?
  	status =SPI2_ReadWriteByte(reg);//·¢?í??′??÷o? 
  	SPI2_ReadWriteByte(value);      //D′è???′??÷μ??μ
  	NRF24L01_CSN(1);                 //???1SPI′?ê?	   
  	return(status);       		    //·μ??×′ì??μ
}


//?áè?SPI??′??÷?μ
//reg:òa?áμ???′??÷
u8 NRF24L01_Read_Reg(u8 reg)
{
	  u8 reg_val;	    
   	NRF24L01_CSN(0);             //ê1?üSPI′?ê?		
  	SPI2_ReadWriteByte(reg);    //·¢?í??′??÷o?
  	reg_val=SPI2_ReadWriteByte(0XFF);//?áè???′??÷?úèY
  	NRF24L01_CSN(1);             //???1SPI′?ê?		    
  	return(reg_val);            //·μ??×′ì??μ
}	

//?ú???¨?????á3????¨3¤?èμ?êy?Y
//reg:??′??÷(????)
//*pBuf:êy?Y????
//len:êy?Y3¤?è
//·μ???μ,′?′??áμ?μ?×′ì???′??÷?μ 
u8 NRF24L01_Read_Buf(u8 reg,u8 *pBuf,u8 len)
{
	u8 status,u8_ctr;	       
  	NRF24L01_CSN(0);            //ê1?üSPI′?ê?
  	status=SPI2_ReadWriteByte(reg);//·¢?í??′??÷?μ(????),2¢?áè?×′ì??μ   	   
	for(u8_ctr=0;u8_ctr<len;u8_ctr++)pBuf[u8_ctr]=SPI2_ReadWriteByte(0XFF);//?á3?êy?Y
  	NRF24L01_CSN(1);            //1?±?SPI′?ê?
  	return status;             //·μ???áμ?μ?×′ì??μ
}

//?ú???¨????D′???¨3¤?èμ?êy?Y
//reg:??′??÷(????)
//*pBuf:êy?Y????
//len:êy?Y3¤?è
//·μ???μ,′?′??áμ?μ?×′ì???′??÷?μ
u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 len)
{
	u8 status,u8_ctr;	    
	NRF24L01_CSN(0);             //ê1?üSPI′?ê?
  	status = SPI2_ReadWriteByte(reg);//·¢?í??′??÷?μ(????),2¢?áè?×′ì??μ
  	for(u8_ctr=0; u8_ctr<len; u8_ctr++)SPI2_ReadWriteByte(*pBuf++); //D′è?êy?Y	 
  	NRF24L01_CSN(1);             //1?±?SPI′?ê?
  	return status;              //·μ???áμ?μ?×′ì??μ
}				   

//???ˉNRF24L01·¢?íò?′?êy?Y
//txbuf:′y·¢?íêy?Yê×μ??·
//·μ???μ:·¢?ííê3é×′??
//u8 NRF24L01_TxPacket(u8 *txbuf)
//{
//	u8 sta;
// 	//SPI2_SetSpeed(SPI_BAUDRATEPRESCALER_8); //spi?ù?è?a6.75Mhz£¨24L01μ?×?′óSPIê±?ó?a10Mhz£?   
//	NRF24L01_CE(0);
//  	NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//D′êy?Yμ?TX BUF  32??×??ú
// 	NRF24L01_CE(1);                         //???ˉ·¢?í	   
//	while(NRF24L01_IRQ!=0);                 //μè′y·¢?ííê3é
//	sta=NRF24L01_Read_Reg(STATUS);          //?áè?×′ì???′??÷μ??μ	   
//	printf("sta=%x  
",sta);
//	NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //??3yTX_DS?òMAX_RT?D??±ê??
//	if(sta&MAX_TX)                          //′?μ?×?′ó??·¢′?êy
//	{
//		NRF24L01_Write_Reg(FLUSH_TX,0xff);  //??3yTX FIFO??′??÷ 
//		return MAX_TX; 
//	}
//	if(sta&TX_OK)                           //·¢?ííê3é
//	{
//		return TX_OK;
//	}
//	return 0xff;//?????-òò·¢?í꧰ü
//}

//???ˉNRF24L01·¢?íò?′?êy?Y
//txbuf:′y·¢?íêy?Yê×μ??·
//·μ???μ:0£??óê?íê3é£?????£?′í?ó′ú??
//u8 NRF24L01_RxPacket(u8 *rxbuf)
//{
//	u8 sta;	
//	u8 i=0;
//	//SPI2_SetSpeed(SPI_BAUDRATEPRESCALER_8); //spi?ù?è?a6.75Mhz£¨24L01μ?×?′óSPIê±?ó?a10Mhz£?   
//	sta=NRF24L01_Read_Reg(STATUS);          //?áè?×′ì???′??÷μ??μ    	 
//	NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //??3yTX_DS?òMAX_RT?D??±ê??
//	if(sta&RX_OK)//?óê?μ?êy?Y
//	{
//			if(sta&0x02)
//			{
//				printf("êy?Yà′×?í¨μà1");
//			}
//			else if(sta&0x04)
//			{
//				printf("êy?Yà′×?í¨μà2");
//			}
//			else if(sta&0x06)
//			{
//				printf("êy?Yà′×?í¨μà3");
//			}
//			else if(sta&0x08)
//			{
//				printf("êy?Yà′×?í¨μà4");
//			}
//			else if(sta&0x0a)
//			{
//				printf("êy?Yà′×?í¨μà5");
//			}
//			else 
//			{
//				//printf("êy?Yà′×?í¨μà0");
//			}
//			NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//?áè?êy?Y
//			for(i=0;i<32;i++)
//			{
//			  printf(" %x ", rxbuf[i]);
//			}
//			printf("
");
//			NRF24L01_Write_Reg(FLUSH_RX,0xff);  //??3yRX FIFO??′??÷ 
//			return 0;
//		}	   
//	return 1;//??ê?μ?è?o?êy?Y
//}					    


//??oˉêy3?ê??ˉNRF24L01μ?RX?£ê?
//éè??RXμ??·,D′RXêy?Y?í?è,????RF?μμà,2¨ì??êoíLNA HCURR
//μ±CE±???oó,?′??è?RX?£ê?,2¢?éò??óê?êy?Yá?		   
void NRF24L01_RX_Mode(void)
{
		NRF24L01_CE(0);	  
  	NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH);//D′RX?úμ?μ??·
	  
  	NRF24L01_Write_Reg(NRF_WRITE_REG+EN_AA,0x01);       //ê1?üí¨μà0μ?×??ˉó|′e    
  	NRF24L01_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01);   //ê1?üí¨μà0μ??óê?μ??·  	 
  	NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,40);	        //éè??RFí¨D??μ?ê		  
  	NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//????í¨μà0μ?óDD§êy?Y?í?è 	    
  	NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f);    //éè??TX·¢é?2?êy,0db??ò?,2Mbps,μí??éù??ò??a??   
  	NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG, 0x0f);     //?????ù±?1¤×÷?£ê?μ?2?êy;PWR_UP,EN_CRC,16BIT_CRC,?óê??£ê? 
  	NRF24L01_CE(1); //CE?a??,??è??óê??£ê? 
}						 

//??oˉêy3?ê??ˉNRF24L01μ?TX?£ê?
//éè??TXμ??·,D′TXêy?Y?í?è,éè??RX×??ˉó|′eμ?μ??·,ì?3?TX·¢?íêy?Y,????RF?μμà,2¨ì??êoíLNA HCURR
//PWR_UP,CRCê1?ü
//μ±CE±???oó,?′??è?RX?£ê?,2¢?éò??óê?êy?Yá?		   
//CE?a??′óóú10us,?ò???ˉ·¢?í.	 
void NRF24L01_TX_Mode(void)
{														 
	NRF24L01_CE(0);	    
	NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);//D′TX?úμ?μ??· 
	NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH); //éè??TX?úμ?μ??·,?÷òa?aá?ê1?üACK	  

	NRF24L01_Write_Reg(NRF_WRITE_REG+EN_AA,0x01);     //ê1?üí¨μà0μ?×??ˉó|′e    
	NRF24L01_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01); //ê1?üí¨μà0μ??óê?μ??·  
	NRF24L01_Write_Reg(NRF_WRITE_REG+SETUP_RETR,0x1a);//éè??×??ˉ??·¢????ê±??:500us + 86us;×?′ó×??ˉ??·¢′?êy:10′?
	NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,40);       //éè??RFí¨μà?a40
	NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f);  //éè??TX·¢é?2?êy,0db??ò?,2Mbps,μí??éù??ò??a??   
	NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e);    //?????ù±?1¤×÷?£ê?μ?2?êy;PWR_UP,EN_CRC,16BIT_CRC,?óê??£ê?,?a???ùóD?D??
	NRF24L01_CE(1);//CE?a??,10usoó???ˉ·¢?í
}

//void rec_24l01()
//{
//	u8 tmp_buf[33];	
//	u8 t=0;
//	u8 i=0;
//	
//	
//  while(NRF24L01_Check())
//	{
//		printf("NRF24L01 Error  
");
//	  vTaskDelay(1000);
//	}
//	NRF24L01_RX_Mode();	
//	while(1)
//		{	  		    		    				 
//			if(NRF24L01_RxPacket(tmp_buf)==0)//ò?μ??óê?μ?D??¢,?ò??ê?3?à′.
//			{
//				printf("NRF24L01 rec_data: 
");
//				for(i=0;i<32;i++)
//				{
//				  printf("  %x  ",tmp_buf[i]);  
//				}
//				printf("
"); 
				tmp_buf[32]=0;//?óè?×?·?′??áê?·?
				printf("NRF24L01 rec_data:  %s  
",tmp_buf);   
//			}
//			
//			t++;
//			if(t==100)//′ó??1s?ó??±?ò?′?×′ì?
//			{
//				t=0;
//				LED_TEST_TOG;
//			} 
//			vTaskDelay(10);			
//		}
//}


*************************************************ê?·¢ò?ì?///********************************************************************///
*************************************************ê?·¢ò?ì?///********************************************************************///
*************************************************ê?·¢ò?ì?///********************************************************************///
*************************************************ê?·¢ò?ì?///********************************************************************///



//********************
//*NRF24L013?ê??ˉ
//
void init_NRF24L01(void)
{
	NRF24L01_CE(0);
	
	NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);//D′TX?úμ?μ??· 
	NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH); //éè??TX?úμ?μ??·,?÷òa?aá?ê1?üACK
	
	NRF24L01_Write_Reg(NRF_WRITE_REG + EN_AA, 0x01);      //ê1?üí¨μà0μ?×??ˉó|′e    
	NRF24L01_Write_Reg(NRF_WRITE_REG + EN_RXADDR, 0x01);  //ê1?üí¨μà0μ??óê?μ??·  
	NRF24L01_Write_Reg(NRF_WRITE_REG+SETUP_RETR,0x1a);//éè??×??ˉ??·¢????ê±??:500us + 86us;×?′ó×??ˉ??·¢′?êy:10′?
	NRF24L01_Write_Reg(NRF_WRITE_REG + RF_CH, 40);       //éè??RFí¨μà?a40
	NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//????í¨μà0μ?óDD§êy?Y?í?è 	
	NRF24L01_Write_Reg(NRF_WRITE_REG + RF_SETUP, 0x0e);   	//éè??TX·¢é?2?êy,0db??ò?,2Mbps,μí??éù??ò??a??   
	//NRF24L01_Write_Reg(NRF_WRITE_REG + RF_SETUP, 0x06);   	//éè??TX·¢é?2?êy,0db??ò?,1Mbps,μí??éù??ò??a??   
}



/***********************************************************************************************************/
//·¢?í  tx_buf  
//*******************************************************************************************************
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
	u8 sta=0;
	
	NRF24L01_CE(0);
	
	NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH); //éè??TX?úμ?μ??·,?÷òa?aá?ê1?üACK
	NRF24L01_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);//D′êy?Yμ?TX BUF  32??×??ú
	NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e);    //?????ù±?1¤×÷?£ê?μ?2?êy;PWR_UP,EN_CRC,16BIT_CRC,?óê??£ê?,?a???ùóD?D??
	NRF24L01_CE(1);
	while(NRF24L01_IRQ!=0);                 //μè′y·¢?ííê3é
	
	//delay_us(100);
//	sta=NRF24L01_Read_Reg(STATUS);          //?áè?×′ì???′??÷μ??μ	   
//	printf("sta=%x  
",sta);
//	NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //??3yTX_DS?òMAX_RT?D??±ê??
//	if(sta&MAX_TX)                          //′?μ?×?′ó??·¢′?êy
//	{
//		NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,0xff);  //??3yTX FIFO??′??÷ 
//		 
//	}
//	if(sta&TX_OK)                           //·¢?ííê3é
//	{
//		
//	}
	
}

void SetRX_Mode(void)
{
	NRF24L01_CE(0);
	NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG, 0x0f);     //?????ù±?1¤×÷?£ê?μ?2?êy;PWR_UP,EN_CRC,16BIT_CRC,?óê??£ê? 
	NRF24L01_CE(1);
	delay_us(130);
}

u8 nRF24L01_RxPacket(u8* rx_buf)
{
	u8 sum=0;
	u8 sta=0;
	u8 i=0;
	unsigned char revale=0;
	BaseType_t statue;
	u8 nRF24L01_RxPacket_data[32]={0};
	//static u8 rx_data[32]={0};
	//memset(rx_data,0,32);
	//memcpy(rx_data,rx_buf,0);
	
	sta=NRF24L01_Read_Reg(STATUS);          //?áè?×′ì???′??÷μ??μ    	 
	if(sta&RX_OK)//?óê?μ?êy?Y
	{
			if(sta&0x02)
			{
				//printf("êy?Yà′×?í¨μà1 
");
			}
			else if(sta&0x04)
			{
				//printf("êy?Yà′×?í¨μà2 
");
			}
			else if(sta&0x06)
			{
				//printf("êy?Yà′×?í¨μà3 
");
			}
			else if(sta&0x08)
			{
				//printf("êy?Yà′×?í¨μà4 
");
			}
			else if(sta&0x0a)
			{
				//printf("êy?Yà′×?í¨μà5 
");
			}
			else 
			{
				printf("êy?Yà′×?í¨μà0 
");
			}
			NRF24L01_Read_Buf(RD_RX_PLOAD,rx_buf,RX_PLOAD_WIDTH);//?áè?êy?Y
			memcpy(nRF24L01_RxPacket_data,rx_buf,32);
			
			NRF24L01_Write_Reg(FLUSH_RX,0xff);  //??3yRX FIFO??′??÷ 
			NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //??3yTX_DS?òMAX_RT?D??±ê??			
			
			if(rx_buf[0]==0xbb&&rx_buf[1]==0xbf)
				{
					for(i=0;i<31;i++)
					{
						sum+=rx_buf[i];
					}
					if(rx_buf[31]==sum)
					{
						//main_data_1.not_rec_time=0;
						
//						statue=xQueueSend(Message_Queue,rx_buf,0);
//						if(statue != pdTRUE)
//						{
//							printf("Queue Send Failed!
");
//						}
						dear_data(rx_buf,32);
						return 0;
					}
				}
				else
				{
				  return 1;
				}
				
			
		}	
	 else if(sta&TX_OK)
	 {
	   //printf("·¢?ííê3é  
");
		 NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //??3yTX_DS?òMAX_RT?D??±ê??
		 return 1;
	 }
	 else if(sta&0x10)
	 {
	   //printf("′?μ?×??à??·¢?D??′?êy 
");
		 NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //??3yTX_DS?òMAX_RT?D??±ê??
		 return 1;
	 }
//	 else if(sta==0x00)
//	 {
//		 //printf("Free heap: %d bytes
", xPortGetFreeHeapSize());			/*′òó?ê£óà????′óD?*/	
//	   NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,0xff); //??3yTX_DS?òMAX_RT?D??±ê??
//	 }
	return 1;//??ê?μ?è?o?êy?Y
}

5. 24L01.h

#ifndef __24L01_H
#define __24L01_H

#include "main.h"


typedef unsigned char u8; 
typedef unsigned short u16;
typedef unsigned int u32;


extern SPI_HandleTypeDef hspi2;

void MX_SPI2_Init(void);

void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler);
u8 SPI2_ReadWriteByte(u8 TxData);


/**********************************************************************************/
/***********************************24l01***********************************************/
/**********************************************************************************/

//NRF24L01??′??÷2ù×÷?üá?
#define NRF_READ_REG    0x00  //?á??????′??÷,μí5???a??′??÷μ??·
#define NRF_WRITE_REG   0x20  //D′??????′??÷,μí5???a??′??÷μ??·
#define RD_RX_PLOAD     0x61  //?áRXóDD§êy?Y,1~32×??ú
#define WR_TX_PLOAD     0xA0  //D′TXóDD§êy?Y,1~32×??ú
#define FLUSH_TX        0xE1  //??3yTX FIFO??′??÷.·¢é??£ê???ó?
#define FLUSH_RX        0xE2  //??3yRX FIFO??′??÷.?óê??£ê???ó?
#define REUSE_TX_PL     0xE3  //??D?ê1ó?é?ò?°üêy?Y,CE?a??,êy?Y°ü±?2???·¢?í.
#define NOP             0xFF  //??2ù×÷,?éò?ó?à′?á×′ì???′??÷	 
//SPI(NRF24L01)??′??÷μ??·
#define CONFIG          0x00  //??????′??÷μ??·;bit0:1?óê??£ê?,0·¢é??£ê?;bit1:μ?????;bit2:CRC?£ê?;bit3:CRCê1?ü;
                              //bit4:?D??MAX_RT(′?μ?×?′ó??·¢′?êy?D??)ê1?ü;bit5:?D??TX_DSê1?ü;bit6:?D??RX_DRê1?ü
#define EN_AA           0x01  //ê1?ü×??ˉó|′e1|?ü  bit0~5,??ó|í¨μà0~5
#define EN_RXADDR       0x02  //?óê?μ??·?êDí,bit0~5,??ó|í¨μà0~5
#define SETUP_AW        0x03  //éè??μ??·?í?è(?ùóDêy?Yí¨μà):bit1,0:00,3×??ú;01,4×??ú;02,5×??ú;
#define SETUP_RETR      0x04  //?¨á¢×??ˉ??·¢;bit3:0,×??ˉ??·¢??êy?÷;bit7:4,×??ˉ??·¢?óê± 250*x+86us
#define RF_CH           0x05  //RFí¨μà,bit6:0,1¤×÷í¨μà?μ?ê;
#define RF_SETUP        0x06  //RF??′??÷;bit3:′?ê??ù?ê(0:1Mbps,1:2Mbps);bit2:1,·¢é?1|?ê;bit0:μí??éù·?′ó?÷??ò?
#define STATUS          0x07  //×′ì???′??÷;bit0:TX FIFO?ú±ê??;bit3:1,?óê?êy?Yí¨μào?(×?′ó:6);bit4,′?μ?×??à′???·¢
                              //bit5:êy?Y·¢?ííê3é?D??;bit6:?óê?êy?Y?D??;
#define MAX_TX  		0x10  //′?μ?×?′ó·¢?í′?êy?D??
#define TX_OK   		0x20  //TX·¢?ííê3é?D??
#define RX_OK   		0x40  //?óê?μ?êy?Y?D??

#define OBSERVE_TX      0x08  //·¢?í?ì2a??′??÷,bit7:4,êy?Y°ü?aê§??êy?÷;bit3:0,??·¢??êy?÷
#define CD              0x09  //??2¨?ì2a??′??÷,bit0,??2¨?ì2a;
#define RX_ADDR_P0      0x0A  //êy?Yí¨μà0?óê?μ??·,×?′ó3¤?è5??×??ú,μí×??ú?ú?°
#define RX_ADDR_P1      0x0B  //êy?Yí¨μà1?óê?μ??·,×?′ó3¤?è5??×??ú,μí×??ú?ú?°
#define RX_ADDR_P2      0x0C  //êy?Yí¨μà2?óê?μ??·,×?μí×??ú?ééè??,??×??ú,±?D?í?RX_ADDR_P1[39:8]?àμè;
#define RX_ADDR_P3      0x0D  //êy?Yí¨μà3?óê?μ??·,×?μí×??ú?ééè??,??×??ú,±?D?í?RX_ADDR_P1[39:8]?àμè;
#define RX_ADDR_P4      0x0E  //êy?Yí¨μà4?óê?μ??·,×?μí×??ú?ééè??,??×??ú,±?D?í?RX_ADDR_P1[39:8]?àμè;
#define RX_ADDR_P5      0x0F  //êy?Yí¨μà5?óê?μ??·,×?μí×??ú?ééè??,??×??ú,±?D?í?RX_ADDR_P1[39:8]?àμè;
#define TX_ADDR         0x10  //·¢?íμ??·(μí×??ú?ú?°),ShockBurstTM?£ê???,RX_ADDR_P0ó?′?μ??·?àμè
#define RX_PW_P0        0x11  //?óê?êy?Yí¨μà0óDD§êy?Y?í?è(1~32×??ú),éè???a0?ò·?·¨
#define RX_PW_P1        0x12  //?óê?êy?Yí¨μà1óDD§êy?Y?í?è(1~32×??ú),éè???a0?ò·?·¨
#define RX_PW_P2        0x13  //?óê?êy?Yí¨μà2óDD§êy?Y?í?è(1~32×??ú),éè???a0?ò·?·¨
#define RX_PW_P3        0x14  //?óê?êy?Yí¨μà3óDD§êy?Y?í?è(1~32×??ú),éè???a0?ò·?·¨
#define RX_PW_P4        0x15  //?óê?êy?Yí¨μà4óDD§êy?Y?í?è(1~32×??ú),éè???a0?ò·?·¨
#define RX_PW_P5        0x16  //?óê?êy?Yí¨μà5óDD§êy?Y?í?è(1~32×??ú),éè???a0?ò·?·¨
#define NRF_FIFO_STATUS 0x17  //FIFO×′ì???′??÷;bit0,RX FIFO??′??÷??±ê??;bit1,RX FIFO?ú±ê??;bit2,3,±£á?
                              //bit4,TX FIFO??±ê??;bit5,TX FIFO?ú±ê??;bit6,1,?-?··¢?íé?ò?êy?Y°ü.0,2??-?·;
//
//24L012ù×÷??
//#define NRF24L01_CE(n)  n==1? HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET): HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET)
//#define NRF24L01_CSN(n)  n==1? HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET): HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET)
//#define NRF24L01_IRQ  HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_9)

#define NRF24L01_CE(n)  n==1? HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_SET): HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_RESET)
#define NRF24L01_CSN(n)  n==1? HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_SET): HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,GPIO_PIN_RESET)
#define NRF24L01_IRQ  HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_5)


//#define NRF24L01_CE     PGout(8)  //24L01????D?o?
//#define NRF24L01_CSN    PGout(7)  //SPI????D?o?	   
//#define NRF24L01_IRQ    PGin(6)   //IRQ?÷?úêy?Yê?è?
//24L01·¢?í?óê?êy?Y?í?è?¨ò?
#define TX_ADR_WIDTH    5   	//5×??úμ?μ??·?í?è
#define RX_ADR_WIDTH    5   	//5×??úμ?μ??·?í?è
#define TX_PLOAD_WIDTH  32  	//32×??úμ?ó??§êy?Y?í?è
#define RX_PLOAD_WIDTH  32  	//32×??úμ?ó??§êy?Y?í?è


void NRF24L01_Init(void);//3?ê??ˉ
void NRF24L01_RX_Mode(void);//?????a?óê??£ê?
void NRF24L01_TX_Mode(void);//?????a·¢?í?£ê?
u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 u8s);//D′êy?Y??
u8 NRF24L01_Read_Buf(u8 reg, u8 *pBuf, u8 u8s);//?áêy?Y??		  
u8 NRF24L01_Read_Reg(u8 reg);			//?á??′??÷
u8 NRF24L01_Write_Reg(u8 reg, u8 value);//D′??′??÷
u8 NRF24L01_Check(void);//?ì2é24L01ê?·?′??ú
u8 NRF24L01_TxPacket(u8 *txbuf);//·¢?íò???°üμ?êy?Y
//u8 NRF24L01_RxPacket(u8 *rxbuf);//?óê?ò???°üμ?êy?Y
void rec_24l01(void);


/**********************************************************************************/

void init_NRF24L01(void);
void nRF24L01_TxPacket(unsigned char * tx_buf);
void SetRX_Mode(void);
u8 nRF24L01_RxPacket(u8* rx_buf);



#endif