si4438 cca 侦听

我们两清 提交于 2020-03-04 19:12:44

/* set GPIO0 for RSSI interrupt / CCA */
txbuf[0] = CMD_GPIO_PIN_CFG;
txbuf[1] = 27; /* GPIO[0] = 27: CCA or 37: CCA_LATCH */
txbuf[2] = 0; /* GPIO[1] = DONOTHING */
txbuf[3] = 0; /* GPIO[2] = DONOTHING */
txbuf[4] = 0; /* GPIO[3] = DONOTHING */
txbuf[5] = 0; /* NIRQ = behavior not modified */
txbuf[6] = 0; /* SDO = behavior not modified*/
txbuf[7] = 0; /* GEN_CONFIG = GPIO outputs will have the highest drive strength*/
si446x_SendCommand(8, txbuf);
si446x_WaitForCTS();

 

/* set RSSI threshold */
txbuf[0] = CMD_SET_PROPERTY;
txbuf[1] = PROP_MODEM_GROUP;
txbuf[2] = 0x03; /* num of property */
txbuf[3] = PROP_MODEM_RSSI_THRESH;/* start address */
txbuf[4] = 30; /* MODEM_RSSI_THRESH: if < 30, CCA GPIO0 becomes low */
txbuf[5] = 0x0c; /* MODEM_RSSI_JUMP_THRESH: default */
txbuf[6] = 0x22; /* MODEM_RSSI_CONTROL: check thresh after latch, latch RSSI when sync word is detected */
si446x_SendCommand(5, txbuf);
si446x_WaitForCTS();

 

  1. #include <reg51.h>  
  2. #include <string.h>  
  3. #include "radio.h"  
  4. #include"spi.h"  
  5. #include"uart.h"  
  6. #define  U8 unsigned char  
  7. #define SEND_OUT_MESSAGE_WITH_ACK      "NEED_ACK"  
  8. #define SEND_OUT_MESSAGE_NO_ACK        "NO_ACK"  
  9. #define SEND_OUT_ACK                   "ACK!"  
  10.   
  11.   
  12. #define SEND_OUT_MESSAGE_WITH_ACK_LEN   8  
  13. #define SEND_OUT_MESSAGE_NO_ACK_LEN     6  
  14. #define SEND_OUT_ACK_LEN                4  
  15. #define SNED_OUT_MSG_MAX_LEN            8       //It is the maxium length of the send out packet.  
  16.   
  17.   
  18. extern idata U8 ItStatus1,ItStatus2;  
  19. void delay_ms(unsigned int ms)  
  20. {  
  21.         unsigned int i;  
  22.         unsigned char j;  
  23.         for(i=0;i<ms;i++)  
  24.         {  
  25.                 for(j=0;j<200;j++);  
  26.                 for(j=0;j<102;j++);  
  27.         }   
  28. }  
  29. void Init_Device(void)  
  30. {  
  31. //   Reset_Sources_Init();  
  32. //   PCA_Init();  
  33.   //  Port_IO_Init();   // Oscillator_Init();  
  34.   // Timer0_Init();  
  35.     SPI_Init();  
  36.         EA = 1; //Enable Gloable interruption.  
  37.   
  38.   
  39.         //启动射频模块,模块的SDN引脚拉低后必须延时至少30ms,实际上可以直接把SDN引脚接地,这样就不用在程序中初始化了;  
  40.         RF_SDN=0;  
  41.         delay_ms(30);  
  42.         delay_ms(30);  
  43. //        RF_SDN = 0;  
  44. //        DelayMs(30);  
  45. //        NSEL = 1;  
  46. //        SCLK = 0;  
  47.   
  48.   
  49.            SpiWriteRegister(0x0B, 0xCA);  
  50.            SpiWriteRegister(0x0C, 0xCA);  
  51.            SpiWriteRegister(0x0D, 0xCA);  
  52.   
  53.   
  54.         //1.Blink the LED to show that the initialization is finished.  
  55.         //2.Wait for more than 16ms to wait for the radio chip to work correctly.  
  56. //        TurnOnAllLEDs();  
  57.   
  58.   
  59. //        DelayMs(20);  
  60.         delay_ms(20);  
  61. //        TurnOffAllLEDs();  
  62. }  
  63.   
  64.   
  65. void main(void)  
  66. {  
  67.         U8 length,temp8, *str, sendLen;  
  68.         U8 payload[10];  
  69.   
  70.   
  71.         //Initialize the MCU:   
  72.         UART_Init();   
  73.         Init_Device();  
  74.         UART_Send_Str("MCU初始化完毕....\n");  
  75.         //读取中断状态寄存器清除中断标志以便释放NIRQ引脚  
  76.         //如果中断来了,那么NIRQ引脚会被拉低;如果这时候中断状态寄存器0x03和0x04被读取,那么NIRQ引脚会被释放恢复高电平  
  77.         ItStatus1 = SpiReadRegister(0x03);                                                                                                        //read the Interrupt Status1 register  
  78.         ItStatus2 = SpiReadRegister(0x04);                                                                                                        //read the Interrupt Status2 register  
  79.           
  80.         //SW reset,软件复位这个模块主控芯片     
  81.     SpiWriteRegister(0x07, 0x80);                                                                                                                //write 0x80 to the Operating & Function Control1 register   
  82. //        DelayMs(20);  
  83.         delay_ms(20);  
  84.         delay_ms(20);  
  85.         //wait for POR interrupt from the radio (while the nIRQ pin is high)  
  86.         //wait for chip ready interrupt from the radio (while the nIRQ pin is high)  
  87.         //等待软复位完成,当软复位完成后有中断发生。客户也可以在这里直接延时至少20ms而不用等待中断来;等待至少20ms后复位完成,这时候必须读中断状态寄存器释放中断引脚  
  88.         while ( NIRQ == 1);    
  89.         //read interrupt status registers to clear the interrupt flags and release NIRQ pin  
  90.         ItStatus1 = SpiReadRegister(0x03);                                                                                                        //read the Interrupt Status1 register  
  91.         ItStatus2 = SpiReadRegister(0x04);                                                                                                        //read the Interrupt Status2 register  
  92.           
  93.         //根据不同的射频参数初始化射频模块;  
  94.     RF_init();  
  95.         UART_Send_Str("RF芯片si4432初始化完毕....\n");  
  96.         //Enable two interrupts:   
  97.         // a) one which shows that a valid packet received: 'ipkval'  
  98.         // b) second shows if the packet received with incorrect CRC: 'icrcerror'  
  99.         //设置中断使能寄存器,这里设置为只有当有效的数据包被接收或者接收到的数据包数据CRC校验出错才来中断;具体设置参考0x05和0x06寄存器   
  100.         SpiWriteRegister(0x05, 0x03);                                                                                                                        //write 0x03 to the Interrupt Enable 1 register  
  101.         SpiWriteRegister(0x06, 0x00);                                                                                                                        //write 0x00 to the Interrupt Enable 2 register  
  102.   
  103.   
  104.         //output dummy data.  
  105.         PB1_TX = 1;  
  106.         PB2_TX = 1;  
  107.   
  108.   
  109.         str = SEND_OUT_MESSAGE_WITH_ACK;  
  110.         sendLen = SEND_OUT_MESSAGE_WITH_ACK_LEN;  
  111.   
  112.   
  113.         //设置模块处于接收状态,当没有按键按下的时候一直处于接收状态,等待接收数据  
  114.         RFSetRxMode();  
  115.         UART_Send_Str("模块处于接收状态....\n");  
  116.         /*MAIN Loop*/  
  117.         while(1)  
  118.         {  
  119.   
  120.   
  121.                 //当按键被按下就有一个数据包被发出;  
  122.             if(PB1_TX == 0)  
  123.                 {  
  124.                         while( PB1_TX == 0 );  
  125.                         UART_Send_Str("按键按下,开始发送....\n");  
  126.                         RFFIFOSendData(sendLen, str);  
  127.                         //after packet transmission set the interrupt enable bits according receiving mode  
  128.                         //Enable two interrupts:   
  129.                         // a) one which shows that a valid packet received: 'ipkval'  
  130.                         // b) second shows if the packet received with incorrect CRC: 'icrcerror'   
  131.                         //设置中断使能寄存器,这里设置为只有当有效的数据包被接收或者接收到的数据包数据CRC校验出错才来中断;具体设置参考0x05和0x06寄存器   
  132.                         SpiWriteRegister(0x05, 0x03);                                                                                                 //write 0x03 to the Interrupt Enable 1 register  
  133.                         SpiWriteRegister(0x06, 0x00);                                                                                                 //write 0x00 to the Interrupt Enable 2 register  
  134.                         //发射完毕后设置模块让它又工作在接收状态下;  
  135.                         RFSetRxMode();  
  136.                         UART_Send_Str("发送完毕,恢复到接收状态....\n");  
  137.                 }  
  138.   
  139.   
  140.   
  141.   
  142.                 //check whether interrupt occured  
  143.                 //查询中断是否到来,如果中断来了,根据我们前面中断使能寄存器的设置,说明有效数据包已经收到,或者收到的数据包CRC校验出错;  
  144.                 //如果客户采用中断触发的方式,那么服务程序必须包括这些内容。在中断服务程序中必须判断被使能的那些中断的状态位是否被置位,然后根据不同的  
  145.                 //状态位进行处理  
  146.                 if( NIRQ == 0 )  
  147.                 {  
  148.                   
  149.                   //设置模块处于空闲模式,处理收到的数据包,不继续接收数据  
  150.                         RFSetIdleMode();  
  151.                  UART_Send_Str("中断来了....\n");  
  152.                         /*CRC Error interrupt occured*/  
  153.                         //判断是否由于CRC校验出错引发的中断;在RFSetIdleMode中已经读出了中断状态寄存器的值  
  154.                         if( (ItStatus1 & 0x01) == 0x01 )  
  155.                         {  
  156.                                 //reset the RX FIFO  
  157.                                 //如果是CRC校验出错,那么接收FIFO复位;  
  158.                              SpiWriteRegister(0x08, 0x02);                                                                                                //write 0x02 to the Operating Function Control 2 register  
  159.                     SpiWriteRegister(0x08, 0x00);                                                                                                //write 0x00 to the Operating Function Control 2 register  
  160.                                 //blink all LEDs to show the error  
  161.                                 //闪灯提示,客户移植代码的时候这个闪灯操作根据不同客户不同操作  
  162.                         //        TurnOnAllLEDs();  
  163.                         //        DelayMs(2);  
  164.                                 UART_Send_Str("CRC校验出错中断....\n");  
  165.                         //        TurnOffAllLEDs();  
  166.                         }  
  167.   
  168.   
  169.                         /*packet received interrupt occured*/  
  170.                         //判断是否是数据包已经被正确接收。  
  171.                         if( (ItStatus1 & 0x02) == 0x02 )  
  172.                         {  
  173.                                 //Read the length of the received payload  
  174.                                 //数据包已经被正确接收,读取收到的数据包长度  
  175.                                 length = SpiReadRegister(0x4B);                                                                                        //read the Received Packet Length register  
  176.                                 //根据长度判断相应的操作。客户可以不做这些,而直接从FIFO读出收到的数据;  
  177.                                 //check whether the received payload is not longer than the allocated buffer in the MCU  
  178.                                 if(length <= SNED_OUT_MSG_MAX_LEN)  
  179.                                 {  
  180.                                         //Get the reeived payload from the RX FIFO  
  181.                                         //直接从FIFO中读取收到的数据。客户只要读出FIFO的数据就算收到数据。  
  182.                                         for(temp8=0;temp8 < length;temp8++)  
  183.                                         {  
  184.                                                 payload[temp8] = SpiReadRegister(0x7F);                                                //read the FIFO Access register  
  185.                                         }  
  186.                                   
  187.                                         for(temp8=0;temp8 < length;temp8++)  
  188.                                         {  
  189.                                                 UART_Send_Byte(payload[temp8]);        //向串口发送接收到的数据  
  190.                                         }  
  191.                          UART_Send_Str("向串口发送接收到的数据....\n");  
  192.                                         //check whether the acknowledgement packet received  
  193.                                         //判断是否是预先设定的数据;一般应用情况客户可以不理会这个,这个判断只是我们demo板的应用。客户只要读出FIFO的数据就算收到数据;  
  194.                                         if(( length == SEND_OUT_ACK_LEN ) || (length == SEND_OUT_MESSAGE_NO_ACK_LEN))  
  195.                             {  
  196.                                     if((memcmp(&payload[0], SEND_OUT_ACK, SEND_OUT_ACK_LEN - 1) == 0 )  
  197.                                                 || (memcmp(&payload[0], SEND_OUT_MESSAGE_NO_ACK, SEND_OUT_MESSAGE_NO_ACK_LEN - 1) == 0))  
  198.                                                    {  
  199.                                           //blink LED2 to show that ACK received  
  200.                                              //        RxLEDOn();  
  201.                                                         //Show the Rx LED for about 10ms  
  202.                                                         UART_Send_Str("数据包已经被正确接收....\n");  
  203.                                                 //        DelayMs(2);  
  204.                                                      //        TurnOffAllLEDs();  
  205.                                                 }  
  206.                                         }  
  207.   
  208.   
  209.                                         //check whether an expected packet received, this should be acknowledged  
  210.                                         //判断是否是需要发送回答信号的数据包。这个只是根据收到的数据做的不同操作,在客户那里可能是根据收到的数据做的特定的操作,例如控制某个开关  
  211.                                         if( length == SEND_OUT_MESSAGE_WITH_ACK_LEN )   
  212.                             {  
  213.                                                 //必须发送应答信号,启动发送  
  214.                                     if( memcmp(&payload[0], SEND_OUT_MESSAGE_WITH_ACK, SEND_OUT_MESSAGE_WITH_ACK_LEN - 1) == 0 )  
  215.                                            {  
  216.                                          //blink LED2 to show that the packet received  
  217.                                                      //RxLEDOn();  
  218.                                                         //Show the Rx LED for about 10ms  
  219.                                                 //        DelayMs(2);  
  220.                                                         UART_Send_Str("发送应答....\n");  
  221.                                                      //        TurnOffAllLEDs();  
  222.   
  223.   
  224.                                                         //发送应答信号。  
  225.                                                         /*send back an acknowledgement*/  
  226.                                                         RFFIFOSendData(SEND_OUT_ACK_LEN, SEND_OUT_ACK);  
  227.   
  228.   
  229.                                                         //after packet transmission set the interrupt enable bits according receiving mode  
  230.                                                         //Enable two interrupts:   
  231.                                                         // a) one which shows that a valid packet received: 'ipkval'  
  232.                                                         // b) second shows if the packet received with incorrect CRC: 'icrcerror'   
  233.                                                         SpiWriteRegister(0x05, 0x03);                                                                 //write 0x03 to the Interrupt Enable 1 register  
  234.                                                         SpiWriteRegister(0x06, 0x00);                                                                 //write 0x00 to the Interrupt Enable 2 register  
  235.                                                         //read interrupt status registers to release all pending interrupts  
  236.                                                         ItStatus1 = SpiReadRegister(0x03);                                                        //read the Interrupt Status1 register  
  237.                                                         ItStatus2 = SpiReadRegister(0x04);                                                        //read the Interrupt Status2 register  
  238.                                                 }  
  239.                                         }  
  240.                                 }  
  241.                        }  
  242.                         //reset the RX FIFO  
  243.                        SpiWriteRegister(0x08, 0x02);                                                                                                        //write 0x02 to the Operating Function Control 2 register  
  244.                 SpiWriteRegister(0x08, 0x00);                                                                                                        //write 0x00 to the Operating Function Control 2 register  
  245.                         RFSetRxMode();  
  246.                 }           
  247.         }  
  248. }</pre>  
  249. <p>  
  250.        
  251. </p>  
  252. <br>  
  253.   
  254. <p>  
  255.     <br>  
  256. </p>  

 
 
院士
2013-12-25 20:51:19    评分
2楼
没有使用贴代码功能?

 
高工
2013-12-25 22:06:48    评分
3楼
修饰一下,帅多了、

 
菜鸟
2013-12-27 11:44:02    评分
4楼
4432应该是兼容的吧? Si4438与Si446x收发器产品引脚兼容以及软件兼容哦(是针对全球部署智能电表和智能电网产品而设计的sub-GHz ISM频段产品)。

 
菜鸟
2014-01-02 14:34:25    评分
5楼

多谢版主帮修饰,确实好看多了!


回LS的: 并不完全兼容,下面是我请世强的杨工提供的采用Si4438的部分代码,大家可以参考下,完整的代码可以找深圳世强的FAE杨工要。大家可以对比研究下。

 

 

  1. #include "compiler_defs.h"  
  2. #include "c8051f960_defs.h"  
  3. #include "hardware_defs.h"  
  4. #include "control_IO.h"  
  5. #include "spi.h"  
  6. #include "Si446x_B0_defs.h"  
  7. #include "ezrp_next_api.h"  
  8. #include "modem_params.h"  
  9. // Define capacitor bank value  
  10. #define CAP_BANK_VALUE    0x48    // Capacitor bank value for adjusting the XTAL frequency  
  11.                                   // Note that it may varies on different test cards  
  12. /*------------------------------------------------------------------------*/  
  13. /*            GLOBAL variables                        */  
  14. /*------------------------------------------------------------------------*/  
  15. // Set up modem parameters database; data is retrieved from modem_params.h header file which is  
  16. // automatically generated by the WDS (Wireless Development Suite)  
  17. SEG_CODE U8 ModemTrx1[] = {7, MODEM_MOD_TYPE_7};  
  18. SEG_CODE U8 ModemTrx2[] = {5, MODEM_CLKGEN_BAND_5};  
  19. SEG_CODE U8 ModemTrx3[] = {11, SYNTH_PFDCP_CPFF_11};  
  20. SEG_CODE U8 ModemTrx4[] = {12, FREQ_CONTROL_INTE_12};  
  21. SEG_CODE U8 ModemRx1[] = {11, MODEM_MDM_CTRL_11};  
  22. SEG_CODE U8 ModemRx2[] = {14, MODEM_BCR_OSR_1_14};  
  23. SEG_CODE U8 ModemRx3[] = {12, MODEM_AFC_GEAR_12};  
  24. SEG_CODE U8 ModemRx4[] = {5, MODEM_AGC_CONTRL_5};  
  25. SEG_CODE U8 ModemRx4_1[] = {7, MODEM_AGC_WINDOW_SIZE_7};  
  26. SEG_CODE U8 ModemRx5[] = {9, MODEM_FSK4_GAIN1_9};  
  27. SEG_CODE U8 ModemRx6[] = {8, MODEM_OOK_PDTC_8};  
  28. SEG_CODE U8 ModemRx7[] = {8, MODEM_RAW_SEARCH_8};  
  29. SEG_CODE U8 ModemRx8[] = {6, MODEM_ANT_DIV_MODE_6};  
  30. SEG_CODE U8 ModemRx9[] = {13, MODEM_CHFLT_RX1_CHFLT_COE13_7_0_13};  
  31. SEG_CODE U8 ModemRx10[] = {13, MODEM_CHFLT_RX1_CHFLT_COE4_7_0_13};  
  32. SEG_CODE U8 ModemRx11[] = {13, MODEM_CHFLT_RX2_CHFLT_COE13_7_0_13};  
  33. SEG_CODE U8 ModemRx12[] = {13, MODEM_CHFLT_RX2_CHFLT_COE4_7_0_13};  
  34. /*------------------------------------------------------------------------*/  
  35. /*            LOCAL function prototypes                         */  
  36. /*------------------------------------------------------------------------*/  
  37. void MCU_Init(void);  
  38. /*------------------------------------------------------------------------*/  
  39. /*            MAIN function                             */  
  40. /*------------------------------------------------------------------------*/  
  41. void main(void)  
  42. {  
  43.   SEGMENT_VARIABLE(wDelay, U16, SEG_XDATA);  
  44.   SEGMENT_VARIABLE(bButtonNumber, U8, SEG_XDATA);  
  45.   BIT fValidPacket;  
  46.   //initialize the MCU peripherals  
  47.   MCU_Init();  
  48.   InitIO();  
  49.   // Reset the radio  
  50.   EZRP_PWRDN = 1;  
  51.   // Wait ~300us (SDN pulse width)  
  52.   for(wDelay=0; wDelay<330; wDelay++);  
  53.   // Wake up the chip from SDN  
  54.   EZRP_PWRDN = 0;  
  55.   // Wait for POR (power on reset); ~5ms  
  56.   for(wDelay=0; wDelay<5500; wDelay++);  
  57.   // Start the radio  
  58.   abApi_Write[0] = CMD_POWER_UP;          // Use API command to power up the radio IC  
  59.   abApi_Write[1] = 0x01;              // Write global control registers  
  60.   abApi_Write[2] = 0x00;              // Write global control registers  
  61.   bApi_SendCommand(3,abApi_Write);        // Send command to the radio IC  
  62.   // Wait for boot  
  63.   if (vApi_WaitforCTS())                // Wait for CTS  
  64.   {  
  65.     while (1) {}    // Stop if radio power-up error  
  66.   }  
  67.   // Read ITs, clear pending ones  
  68.   abApi_Write[0] = CMD_GET_INT_STATUS;      // Use interrupt status command  
  69.   abApi_Write[1] = 0;               // Clear PH_CLR_PEND  
  70.   abApi_Write[2] = 0;               // Clear MODEM_CLR_PEND  
  71.   abApi_Write[3] = 0;               // Clear CHIP_CLR_PEND  
  72.   bApi_SendCommand(4,abApi_Write);        // Send command to the radio IC  
  73.   bApi_GetResponse(8, abApi_Read );         // Make sure that CTS is ready then get the response  
  74.   // Set TRX parameters of the radio IC; data retrieved from the WDS-generated modem_params.h header file  
  75.   bApi_SendCommand(ModemTrx1[0],&ModemTrx1[1]);       // Send API command to the radio IC  
  76.   vApi_WaitforCTS();                    // Wait for CTS  
  77.   bApi_SendCommand(ModemTrx2[0],&ModemTrx2[1]);  
  78.   vApi_WaitforCTS();  
  79.   bApi_SendCommand(ModemTrx3[0],&ModemTrx3[1]);  
  80.   vApi_WaitforCTS();  
  81.   bApi_SendCommand(ModemTrx4[0],&ModemTrx4[1]);  
  82.   vApi_WaitforCTS();  
  83.   // Set Rx parameters of the radio IC  
  84.   bApi_SendCommand(ModemRx1[0],&ModemRx1[1]);       // Send API command to the radio IC  
  85.   vApi_WaitforCTS();                    // Wait for CTS  
  86.   bApi_SendCommand(ModemRx2[0],&ModemRx2[1]);  
  87.   vApi_WaitforCTS();  
  88.   bApi_SendCommand(ModemRx3[0],&ModemRx3[1]);  
  89.   vApi_WaitforCTS();  
  90.   bApi_SendCommand(ModemRx4[0],&ModemRx4[1]);  
  91.   vApi_WaitforCTS();  
  92.   bApi_SendCommand(ModemRx4_1[0],&ModemRx4_1[1]);  
  93.   vApi_WaitforCTS();  
  94.   bApi_SendCommand(ModemRx5[0],&ModemRx5[1]);  
  95.   vApi_WaitforCTS();  
  96.   bApi_SendCommand(ModemRx6[0],&ModemRx6[1]);  
  97.   vApi_WaitforCTS();  
  98.   bApi_SendCommand(ModemRx7[0],&ModemRx7[1]);  
  99.   vApi_WaitforCTS();  
  100.   bApi_SendCommand(ModemRx8[0],&ModemRx8[1]);  
  101.   vApi_WaitforCTS();  
  102.   bApi_SendCommand(ModemRx9[0],&ModemRx9[1]);  
  103.   vApi_WaitforCTS();  
  104.   bApi_SendCommand(ModemRx10[0],&ModemRx10[1]);  
  105.   vApi_WaitforCTS();  
  106.   bApi_SendCommand(ModemRx11[0],&ModemRx11[1]);  
  107.   vApi_WaitforCTS();  
  108.   bApi_SendCommand(ModemRx12[0],&ModemRx12[1]);  
  109.   vApi_WaitforCTS();  
  110.   // Enable packet received and CRC error interrupt only  
  111.   abApi_Write[0] = CMD_SET_PROPERTY;      // Use property command  
  112.   abApi_Write[1] = PROP_INT_CTL_GROUP;    // Select property group  
  113.   abApi_Write[2] = 4;               // Number of properties to be written  
  114.   abApi_Write[3] = PROP_INT_CTL_ENABLE;   // Specify property  
  115.   abApi_Write[4] = 0x01;                   // INT_CTL: PH interrupt enabled  
  116.   abApi_Write[5] = 0x18;              // INT_CTL_PH: PH PACKET_RX & CRC2_ERR interrupt enabled  
  117.   abApi_Write[6] = 0x00;              // INT_CTL_MODEM: -  
  118.   abApi_Write[7] = 0x00;              // INT_CTL_CHIP_EN: -  
  119.   bApi_SendCommand(8,abApi_Write);        // Send API command to the radio IC  
  120.   vApi_WaitforCTS();                // Wait for CTS  
  121.   // Configure Fast response registers  
  122.   abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command  
  123.   abApi_Write[1] = PROP_FRR_CTL_GROUP;      // Select property group  
  124.   abApi_Write[2] = 4;               // Number of properties to be written  
  125.   abApi_Write[3] = PROP_FRR_CTL_A_MODE;     // Specify property (1st)  
  126.   abApi_Write[4] = 0x04;              // FRR A: PH IT pending  
  127.   abApi_Write[5] = 0x06;              // FRR B: Modem IT pending  
  128.   abApi_Write[6] = 0x0A;              // FRR C: Latched RSSI  
  129.   abApi_Write[7] = 0x00;              // FRR D: disabled  
  130.   bApi_SendCommand(8,abApi_Write);        // Send API command to the radio IC  
  131.   vApi_WaitforCTS();                // Wait for CTS  
  132.   //Set packet content  
  133.   //Set preamble length  
  134.   abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command  
  135.   abApi_Write[1] = PROP_PREAMBLE_GROUP;     // Select property group  
  136.   abApi_Write[2] = 1;               // Number of properties to be written  
  137.   abApi_Write[3] = PROP_PREAMBLE_CONFIG_STD_1;  // Specify property  
  138.   abApi_Write[4] = 20;              // 20 bits preamble detection threshold  
  139.   bApi_SendCommand(5,abApi_Write);        // Send API command to the radio IC  
  140.   vApi_WaitforCTS();                // Wait for CTS  
  141.   // Set preamble pattern  
  142.   abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command  
  143.   abApi_Write[1] = PROP_PREAMBLE_GROUP;     // Select property group  
  144.   abApi_Write[2] = 1;               // Number of properties to be written  
  145.   abApi_Write[3] = PROP_PREAMBLE_CONFIG;      // Specify property  
  146.   abApi_Write[4] = 0x31;              // Use `1010` pattern, length defined in bytes  
  147.   bApi_SendCommand(5,abApi_Write);        // Send API command to the radio IC  
  148.   vApi_WaitforCTS();                // Wait for CTS  
  149.   // Set sync word  
  150.   abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command  
  151.   abApi_Write[1] = PROP_SYNC_GROUP;       // Select property group  
  152.   abApi_Write[2] = 3;               // Number of properties to be written  
  153.   abApi_Write[3] = PROP_SYNC_CONFIG;        // Specify property  
  154.   abApi_Write[4] = 0x01;              // SYNC_CONFIG: 2 bytes sync word  
  155.   abApi_Write[5] = 0xB4;              // SYNC_BITS_31_24: 1st sync byte: 0x2D; NOTE: LSB transmitted first!  
  156.   abApi_Write[6] = 0x2B;              // SYNC_BITS_23_16: 2nd sync byte: 0xD4; NOTE: LSB transmitted first!  
  157.   bApi_SendCommand(7,abApi_Write);        // Send command to the radio IC  
  158.   vApi_WaitforCTS();                // Wait for CTS  
  159.   // General packet config (set bit order)  
  160.   abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command  
  161.   abApi_Write[1] = PROP_PKT_GROUP;        // Select property group  
  162.   abApi_Write[2] = 1;               // Number of properties to be written  
  163.   abApi_Write[3] = PROP_PKT_CONFIG1;        // Specify property  
  164.   abApi_Write[4] = 0x00;              // Payload data goes MSB first  
  165.   bApi_SendCommand(5,abApi_Write);        // Send command to the radio IC  
  166.   vApi_WaitforCTS();                // Wait for CTS  
  167.   // Set RSSI latch to sync word  
  168.   abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command  
  169.   abApi_Write[1] = PROP_MODEM_GROUP;        // Select property group  
  170.   abApi_Write[2] = 1;               // Number of properties to be written  
  171.   abApi_Write[3] = PROP_MODEM_RSSI_CONTROL;   // Specify property  
  172.   abApi_Write[4] = 0x12;              // RSSI average over 4 bits, latch at sync detect  
  173.   bApi_SendCommand(5,abApi_Write);        // Send API command to the radio IC  
  174.   vApi_WaitforCTS();                // Wait for CTS  
  175.   // Configure the GPIOs  
  176.   abApi_Write[0] = CMD_GPIO_PIN_CFG;        // Use GPIO pin configuration command  
  177. #ifdef ONE_SMA_WITH_RF_SWITCH  
  178.   // If RF switch is used  
  179.   // Select Tx state to GPIO2, Rx state to GPIO0  
  180.   abApi_Write[1] = 0x21;              // Configure GPIO0 as Rx state  
  181.   abApi_Write[2] = 0x13;              // Configure GPIO1 as Tx data  
  182.   abApi_Write[3] = 0x20;              // Configure GPIO2 as Tx state  
  183.   abApi_Write[4] = 0x10;              // Configure GPIO3 as Tx data CLK  
  184. #else  
  185.   abApi_Write[1] = 0x10;              // Configure GPIO0 as Tx data CLK  
  186.   abApi_Write[2] = 0x13;              // Configure GPIO1 as Tx data  
  187.   abApi_Write[3] = 0x20;              // Configure GPIO2 as Tx state  
  188.   abApi_Write[4] = 0x21;              // Configure GPIO3 as Rx state  
  189. #endif  
  190.   bApi_SendCommand(5,abApi_Write);        // Send command to the radio IC  
  191.   vApi_WaitforCTS();  
  192.   // Adjust XTAL clock frequency  
  193.   abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command  
  194.   abApi_Write[1] = PROP_GLOBAL_GROUP;       // Select property group  
  195.   abApi_Write[2] = 1;               // Number of properties to be written  
  196.   abApi_Write[3] = PROP_GLOBAL_XO_TUNE;     // Specify property  
  197.   abApi_Write[4] = CAP_BANK_VALUE;        // Set cap bank value to adjust XTAL clock frequency  
  198.   bApi_SendCommand(5,abApi_Write);        // Send command to the radio IC  
  199.   vApi_WaitforCTS();                // Wait for CTS  
  200.   // Read ITs, clear pending ones  
  201.   abApi_Write[0] = CMD_GET_INT_STATUS;  // Use interrupt status command  
  202.   abApi_Write[1] = 0;           // Clear PH_CLR_PEND  
  203.   abApi_Write[2] = 0;           // Clear MODEM_CLR_PEND  
  204.   abApi_Write[3] = 0;           // Clear CHIP_CLR_PEND  
  205.   bApi_SendCommand(4,abApi_Write);    // Send command to the radio IC  
  206.   bApi_GetResponse(8,abApi_Read);     // Get the response  
  207.   // Start Rx  
  208.   abApi_Write[0] = CMD_START_RX;      // Use start Rx command  
  209.   abApi_Write[1] = 0;           // Set channel number  
  210.   abApi_Write[2] = 0x00;          // Start Rx immediately  
  211.   abApi_Write[3] = 0x00;          // 8 bytes to receive  
  212.   abApi_Write[4] = 0x08;          // 8 bytes to receive  
  213.   abApi_Write[5] = 0x00;          // No change if Rx timeout  
  214.   abApi_Write[6] = 0x03;          // Ready state after Rx  
  215.   abApi_Write[7] = 0x03;          // Ready state if Rx invalid  
  216.   bApi_SendCommand(8,abApi_Write);    // Send API command to the radio IC  
  217.   vApi_WaitforCTS();            // Wait for CTS  
  218.   // Turn off LEDs  
  219.   ClearLed(1);  
  220.   ClearLed(2);  
  221.   ClearLed(3);  
  222.   ClearLed(4);  
  223.   while(1)  
  224.   {  
  225.     if(EZRP_NIRQ == 0)  
  226.     {  
  227.       // Read PH IT registers to see the cause for the IT  
  228.       abApi_Write[0] = CMD_GET_PH_STATUS;   // Use packet handler status command  
  229.       abApi_Write[1] = 0x00;          // Dummy byte for a proper CTS response  
  230.       bApi_SendCommand(2,abApi_Write);    // Send command to the radio IC  
  231.       bApi_GetResponse(1,abApi_Read);     // Make sure that CTS is ready then get the response  
  232.       if((abApi_Read[0] & 0x10) == 0x10)    // Check if packet received  
  233.       {// Packet received  
  234.         // Get RSSI  
  235.         bApi_GetFastResponseRegister(CMD_FAST_RESPONSE_REG_C,1,abApi_Read);  
  236.         // Read the FIFO  
  237.         bApi_ReadRxDataBuffer(8,abApi_Read);  
  238.         fValidPacket = 0;  
  239.         // Check the packet content  
  240.         if ((abApi_Read[0]=='B') && (abApi_Read[1]=='U') && (abApi_Read[2]=='T') && (abApi_Read[3]=='T') && (abApi_Read[4]=='O') && (abApi_Read[5]=='N'))  
  241.         {  
  242.           bButtonNumber = abApi_Read[6] & 0x07; // Get button info  
  243.           if((bButtonNumber > 0) && (bButtonNumber < 5))  
  244.           {  
  245.             SetLed(bButtonNumber);          // Turn on the appropriate LED  
  246.             for(wDelay=0; wDelay<30000; wDelay++);  // Wait to show LED  
  247.             ClearLed(bButtonNumber);        // Turn off the corresponding LED  
  248.             fValidPacket = 1;  
  249.           }  
  250.         }  
  251.         // Packet content is not what was expected  
  252.         if(fValidPacket == 0)  
  253.         {  
  254.           SetLed(1);                // Blink all LEDs  
  255.           SetLed(2);  
  256.           SetLed(3);  
  257.           SetLed(4);  
  258.           for(wDelay=0; wDelay<30000; wDelay++);  
  259.           ClearLed(1);  
  260.           ClearLed(2);  
  261.           ClearLed(3);  
  262.           ClearLed(4);  
  263.         }  
  264.       }  
  265.       // Read ITs, clear pending ones  
  266.       abApi_Write[0] = CMD_GET_INT_STATUS;  // Use interrupt status command  
  267.       abApi_Write[1] = 0;           // Clear PH_CLR_PEND  
  268.       abApi_Write[2] = 0;           // Clear MODEM_CLR_PEND  
  269.       abApi_Write[3] = 0;           // Clear CHIP_CLR_PEND  
  270.       bApi_SendCommand(4,abApi_Write);    // Send command to the radio IC  
  271.       bApi_GetResponse(8,abApi_Read);     // Get the response  
  272.       // Start Rx  
  273.       abApi_Write[0] = CMD_START_RX;      // Use start Rx command  
  274.       abApi_Write[1] = 0;           // Set channel number  
  275.       abApi_Write[2] = 0x00;          // Start Rx immediately  
  276.       abApi_Write[3] = 0x00;          // 8 bytes to receive  
  277.       abApi_Write[4] = 0x08;          // 8 bytes to receive  
  278.       abApi_Write[5] = 0x00;          // No change if Rx timeout  
  279.       abApi_Write[6] = 0x03;          // Ready state after Rx  
  280.       abApi_Write[7] = 0x03;          // Ready if Rx invalid  
  281.       bApi_SendCommand(8,abApi_Write);    // Send API command to the radio IC  
  282.       vApi_WaitforCTS();            // Wait for CTS  
  283.     }  
  284.   }  
  285. }  

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!