打印
[STM32F1]

关于SI4432双工通信时冲突

[复制链接]
2926|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
学生0时代|  楼主 | 2014-8-27 14:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在使用si4432时,单工可以很好实现,一个收另一个发,没有问题。现在要做双工通信,默认情况为接收状态,接收结果显示在串;串口触发SI4432发送。但是现在出现一个问题是:当发送的时候,在发送方串口会同时显示上次接受的数据的第一个数据并和上次接收数据个数一样。我一步一步调试发现两个中断位都响应了,但是我在发送后的中断函数最后默认了接收模式啊。。。。。。。。请大神给点指示,如何调试


void Si4432_ISR(void)
{
        uint8_t status1 = 0;
        uint8_t status2 = 0;
        uint8_t SI4432_RxLenth = 0;
       
        statebuf[2] = SPI_Read(0x02);
//  unprint((char*)statebuf,1);       
        delay_ms(1);
        status1 = SPI_Read(0x03);                //read the Interrupt Status1 register
        status2 = SPI_Read(0x04);                //read the Interrupt Status2 register, just for clear the pending bits?
       
  statebuf[3] = SPI_Read(0x02);
//  unprint((char*)statebuf,1);       
         if ( (status1 & 0x02) == 0x02 ) /*Valid Packet Received*/
        {
                rx_flag |= 2;
                SI4432_RxLenth = SPI_Read(0x4B);                                                       
                for (SI4432_RxCount=0;SI4432_RxCount < SI4432_RxLenth;SI4432_RxCount++)
                        {
                                SI4432_RxBUFF[SI4432_RxCount] = SPI_Read(0x7F);       
                        }
               
//                SPI_RW_Reg(0x08, 0x02);       
//                SPI_RW_Reg(0x08, 0x00);
//                SPI_RW_Reg(0x07, 0x05); //enble rx
                        statebuf[4] = SPI_Read(0x02);
//      unprint((char*)statebuf,1);       
        }
        if(  (status1 & 0x04) == 0x04 ) /* packet sent interrupt*/
        {
                tx_ok = 1;
                statebuf[5] = SPI_Read(0x02);
//    unprint((char*)statebuf,1);
        }       

        statebuf[6] = SPI_Read(0x02);
//  unprint((char*)statebuf,1);       
        SPI_RW_Reg(0x08, 0x03);       
        SPI_RW_Reg(0x08, 0x00);       
       
        SPI_RW_Reg(0x07,0x01);
        delay_ms(10);
       
        SPI_RW_Reg(0x05, 0x03); /* enable rx interrupt*/
        SPI_RW_Reg(0x06, 0x00);        
        SPI_RW_Reg(0x07, 0x05); //enble rx
        status1 = SPI_Read(0x03);                //read the Interrupt Status1 register
        status2 = SPI_Read(0x04);                //read the Interrupt Status2 register, just for clear the pending bits?
  statebuf[7] = SPI_Read(0x02);
//  unprint((char*)statebuf,1);
}

void Si4432Send(uint8_t* buf, uint8_t len)
{
        if(tx_ok)
        {
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x5);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x6);       
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x7);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x8);               
               
                SPI_RW_Reg(0x07, 0x01);        //ready mode
                delay_ms(1);                // delay for stable
               
                SPI_RW_Reg(0x3e, len);  // SET transimit length
                while(len--)
                SPI_RW_Reg(0x7F, *buf++);
               
                statebuf[9] = SPI_Read(0x02);
//unprint((char*)statebuf,1);               
                SPI_RW_Reg(0x05, 0x04);        //enable trasimition complete interrupt
                SPI_RW_Reg(0x06, 0x00);
                SPI_Read(0x03);                        //clear interrupt flags
                SPI_Read(0x04);
                SPI_RW_Reg(0x07, 0x09);        // transmition enable
       
    statebuf[10] = SPI_Read(0x02);
//unprint((char*)statebuf,1);
                tx_ok = 0;
        }
}

int main(void)
{
        RCC_Configuration();
        GPIO_Configuration();
        NVIC_Configuration();
        USART2_Configuration();
        JrInit();
//        printf("wholly shit");
        uprintf("si4432 poc");
        /*software reset*/
        SPI_RW_Reg(0x07, 0x80);
        delay_ms(100);
//        while(GPIO_ReadInputDataBit(TIP_JR_IRQ_PORT,TIP_JR_IRQ_PIN) == SET);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x3);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x4);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x5);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x6);       
        delay_ms(100);

        SPI_RW_Reg(0x75, 0x53);  
        SPI_RW_Reg(0x76, 0x64);  //
        SPI_RW_Reg(0x77, 0x00);

        SI4432_RxBUFF[rx_count++] = SPI_Read(0x75);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x76);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x77);

        SPI_RW_Reg(0x2a, 0x14);
        SPI_RW_Reg(0x6e, 0x09);
        SPI_RW_Reg(0x6f, 0xd5);
        SPI_RW_Reg(0x70, 0x2c);       
       
//SpiWriteRegister(0x72, 0x48);        //(9.6kbps)       
    SPI_RW_Reg(0x72, 0x38);        // frequency bias (1.2kbps)       
//(9.6 kbps, deviation: 45 kHz, channel filter BW: 102.2 kHz
        SPI_RW_Reg(0x1C, 0x1b);                                                        //write 0x1E to the IF Filter Bandwidth register               
        SPI_RW_Reg(0x20, 0x83);                                                        //write 0xD0 to the Clock Recovery Oversampling Ratio register               
        SPI_RW_Reg(0x21, 0xc0);                                                        //write 0x00 to the Clock Recovery Offset 2 register               
        SPI_RW_Reg(0x22, 0x13);                                                        //write 0x9D to the Clock Recovery Offset 1 register               
        SPI_RW_Reg(0x23, 0xa9);                                                        //write 0x49 to the Clock Recovery Offset 0 register               
        SPI_RW_Reg(0x24, 0x00);                                                        //write 0x00 to the Clock Recovery Timing Loop Gain 1 register               
        SPI_RW_Reg(0x25, 0x03);                                                        //write 0x24 to the Clock Recovery Timing Loop Gain 0 register               
        SPI_RW_Reg(0x1D, 0x40);                                                        //write 0x40 to the AFC Loop Gearshift Override register               
        SPI_RW_Reg(0x1E, 0x0A);                                                        //write 0x0A to the AFC Timing Control register               
        SPI_RW_Reg(0x2A, 0x14);                                                        //write 0x20 to the AFC Limiter register                               

        /* preamble & synchronous words configuration*/
        SPI_RW_Reg(0x34, 0X0A);                                        //  5 bytes of preamble
        SPI_RW_Reg(0x35, 0x2A);                                        // preamble check enable
        SPI_RW_Reg(0x33, 0x02);                                        // Transmitted Synchronization Word 3 first, followed by sync word 2
        SPI_RW_Reg(0x36, 0x2d);                                        //Synchronization Word 0x2dd4
        SPI_RW_Reg(0x37, 0xd4);
        SPI_RW_Reg(0x30, 0x8D);                                        //enalbe PH+FIFO MODE, MSB, CCITT CRC
    SPI_RW_Reg(0x32, 0x00 );                                // disable frame header
        SPI_RW_Reg(0x71, 0x63);                                        // tranismit without CLK, FIFO, FSK MODE
       
        /*GPIO configuration*/
        SPI_RW_Reg(0x0b, 0x12); //  
        SPI_RW_Reg(0x0c, 0x15); //

        /* others configuration */
        SPI_RW_Reg(0x09, 0xD7);  // loading capacity
        SPI_RW_Reg(0x69, 0x60);  //AGC overload

        /* tx power*/
        SPI_RW_Reg(0x6d, 0x1e);

        /* enable rx manually*/
        SPI_RW_Reg(0x07, 0x05);

        /* enable rx interrupt*/
        SPI_RW_Reg(0x05, 0x03);
        SPI_RW_Reg(0x06, 0x00);

        /* clear interrupt*/
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x03);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x04);       

        statebuf[0] = SPI_Read(0x02);
//  unprint((char*)statebuf, 1);
        for(;;)
        {
                if(rx_flag & 1)
                {
                        rx_flag ^= 1;
                        delay_ms(2);
                        Si4432Send(uart_rx_buf, uart_rx_count);
                        uart_rx_count = 0;
                 statebuf[14] = SPI_Read(0x02);
//     unprint((char*)statebuf, 1);
                }
                if(rx_flag & 2)
                {
                        rx_flag ^= 2;
                        delay_ms(2);
                        unprint((char*)SI4432_RxBUFF, SI4432_RxCount);
                        SI4432_RxCount=0;
                       
                        statebuf[15] = SPI_Read(0x02);
//      unprint((char*)statebuf, 1);                       
                }
                delay_ms(5);
        }
}
沙发
mmuuss586| | 2014-8-27 20:07 | 只看该作者
没搞过这个,自己再好好查查,哪里逻辑不太合理;

使用特权

评论回复
板凳
学生0时代|  楼主 | 2014-8-27 21:14 | 只看该作者
mmuuss586 发表于 2014-8-27 20:07
没搞过这个,自己再好好查查,哪里逻辑不太合理;

网上关于双工通信的资源不多,真心不好调试

使用特权

评论回复
地板
lijiajialjj| | 2014-8-28 10:25 | 只看该作者
好吧 我接受还没调通   发送有信号出来  接受不到  能把代码给我看看么

使用特权

评论回复
5
学生0时代|  楼主 | 2014-8-28 13:03 | 只看该作者
lijiajialjj 发表于 2014-8-28 10:25
好吧 我接受还没调通   发送有信号出来  接受不到  能把代码给我看看么

这个是单向的,测试一切OK





/*******************************************************************************
* Copyright (C), 2000-2014, Avisar Electronic Technology Co., Ltd.
* Filename: TIP_Si4432.c
* Author: Anson Chan
* Version:
* Date: 2014-8-3     
* Description:         
*                  
*                  
* Revision history:        
*                  
*    1. Date: 2014-8-3
*       Author: Anson Chan
*       Change: Build
*    2.
* Attention:         
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "TIP_Si4432.h"
#include "delay.h"
#include "led.h"

u8 SI4432_RxBUFF[100];
u8 rx_count =0 ;
u8 tx_ok =1 ;
/* Private constants -----------------------------------------------------------*/
/* Private typedef -------------------------------------------------------------*/
/* Private macro --------------------------------------------------------------*/
#if 0
#define NRF_SPI                         SPI1
#define TIP_NRF_SPI_GRP     GPIOA
#define TIP_NRF_NSS_PIN     GPIO_Pin_3
#define TIP_NRF_SCK_PIN     GPIO_Pin_5
#define TIP_NRF_MISO_PIN    GPIO_Pin_6
#define TIP_NRF_MOSI_PIN    GPIO_Pin_7

#define TIP_NRF_CE_GRP      GPIOB
#define TIP_NRF_CE_PIN      GPIO_Pin_12

#define TIP_NRF_IRQ_GRP     GPIOB
#define TIP_NRF_IRQ_PIN     GPIO_Pin_3
#define TIP_NRF_IRQ_PORT_SRC    GPIO_PortSourceGPIOB
#define TIP_NRF_IRQ_PIN_SRC     GPIO_PinSource3
#define TIP_NRF_IRQ_EXTI_LINE   EXTI_Line3

#define Select_NRF()     GPIO_ResetBits(TIP_NRF_SPI_GRP, TIP_NRF_NSS_PIN)
#define NotSelect_NRF()    GPIO_SetBits(TIP_NRF_SPI_GRP, TIP_NRF_NSS_PIN)
#endif /* 0 */

/* Private variable -------------------------------------------------------------*/
/* Private functions prototype ----------------------------------------------------*/
/* Private functions ------------------------------------------------------------*/



void JrGpioInit(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        /* ENABLE GPIO clock*/
        TIP_JR_SPI_GPIO_CLK_INIT(TIP_JR_SPI_GPIO_CLK, ENABLE);
        TIP_JR_IRQ_GPIO_CLK_INIT(TIP_JR_IRQ_GPIO_CLK, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
       
        GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE);
       
        /* SPI configuration: SCK, MISO and MOSI */
        GPIO_InitStructure.GPIO_Pin = TIP_JR_SCK_PIN| TIP_JR_MISO_PIN | TIP_JR_MOSI_PIN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(TIP_JR_SPI_PORT, &GPIO_InitStructure);

        /*NSS configuration*/
        GPIO_InitStructure.GPIO_Pin = TIP_JR_NSS_PIN;                                                         
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;          
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  
        GPIO_Init(TIP_JR_SPI_PORT, &GPIO_InitStructure);
        GPIO_SetBits(TIP_JR_SPI_PORT, TIP_JR_NSS_PIN);

       

        /* IRQ configuration*/
        GPIO_InitStructure.GPIO_Pin = TIP_JR_IRQ_PIN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_Init(TIP_JR_IRQ_PORT, &GPIO_InitStructure);
        GPIO_EXTILineConfig(TIP_JR_IRQ_PORT_SRC/*GPIO_PortSourceGPIOB*/, TIP_JR_IRQ_PIN_SRC);

}

void JrNvicInit(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        EXTI_InitTypeDef JR_EXTI_InitStructure;

        /* NVIC channel & piority configuration*/
//NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

#if 1
        NVIC_InitStructure.NVIC_IRQChannel = TIP_JR_IRQ_CHN;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);                                                                                        
       
//        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);
       
        JR_EXTI_InitStructure.EXTI_Line = TIP_JR_IRQ_EXTI_LINE;
        JR_EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        JR_EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
        JR_EXTI_InitStructure.EXTI_LineCmd = ENABLE;
        EXTI_Init(&JR_EXTI_InitStructure);

#else
        NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;                                       
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;       
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;                               
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                        
        NVIC_Init(&NVIC_InitStructure);                                                                                        

        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);//NRF24L01 IRQ  PB4

        JR_EXTI_InitStructure.EXTI_Line = EXTI_Line4;                                                       
        JR_EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                   
        JR_EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;        
        JR_EXTI_InitStructure.EXTI_LineCmd = ENABLE;                                                               
        EXTI_Init(&JR_EXTI_InitStructure);
#endif /* 0 */

}

#if 0
void NRF_SPI_Init(void)
{
        SPI_InitTypeDef  SPI_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        EXTI_InitTypeDef EXTI_InitStructure;

//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO  , ENABLE);  
//        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1  , ENABLE);

        TIP_JR_SPI_GPIO_CLK_INIT(TIP_JR_SPI_GPIO_CLK, ENABLE);
        TIP_JR_IRQ_GPIO_CLK_INIT(TIP_JR_IRQ_GPIO_CLK, ENABLE);
        RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
        TIP_JR_SPI_CLK_INIT(TIP_JR_SPI_CLK, ENABLE);


#ifdef 0
        /********************************NRF24L01 ¹Ü½Å²¿·Ö********************************/
                /* ÅäÖà SPI2 Òý½Å: SCK, MISO and MOSI */
        GPIO_InitStructure.GPIO_Pin = TIP_NRF_SCK_PIN| TIP_NRF_MISO_PIN | TIP_NRF_MOSI_PIN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         //¸´Óù¦ÄÜ£¨ÍÆÍ죩Êä³ö  SPI1
        GPIO_Init(TIP_NRF_SPI_GRP, &GPIO_InitStructure);

        //NSS,PA4
        GPIO_InitStructure.GPIO_Pin = TIP_NRF_NSS_PIN;                                                          
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;           
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  
        GPIO_Init(TIP_NRF_SPI_GRP, &GPIO_InitStructure);   

        /* ÅäÖÃSPI NRF24L01+Ƭѡ CE  PB12 */
        GPIO_InitStructure.GPIO_Pin = TIP_NRF_CE_PIN;                                                          
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;           
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  
        GPIO_Init(TIP_NRF_CE_GRP, &GPIO_InitStructure);

        /********************************NRF24L01 external interrupt********************************/
        /* ÅäÖÃNRF24L01+ ÖжÏÐźŲúÉúIRQÁ¬½Óµ½  PB4 */
        GPIO_InitStructure.GPIO_Pin = TIP_NRF_IRQ_PIN;                                 //NRF24L01 IRQ
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                   //ÉÏÀ­ÊäÈëģʽ
        GPIO_Init(TIP_NRF_IRQ_GRP, &GPIO_InitStructure);
#endif /* 0 */
        JrGpioInit();

#if 1
//        //ÉèÖÃÖжÏÏòÁ¿±íµÄ´æ·ÅλÖú͵ØÖ·
//        //NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
        NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;                                       
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;       
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;                               
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                               
        NVIC_Init(&NVIC_InitStructure);                                                                                            
       
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource3);//NRF24L01 IRQ  PB4
       
        EXTI_InitStructure.EXTI_Line = EXTI_Line3;                                                          
        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                          
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;               
        EXTI_InitStructure.EXTI_LineCmd = ENABLE;                                                                  
        EXTI_Init(&EXTI_InitStructure);
#else
        JrNvicInit();          

#endif /* 0 */


        NotSelect_NRF();
        SPI_Cmd(NRF_SPI, DISABLE);  
        /* SPI1 ÅäÖÃ */
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;          //È«Ë«¹¤       
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;                                                                                          //Ö÷ģʽ
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;                                                                          //8λ
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;                                                                                                          //ʱÖÓ¼«ÐÔ ¿ÕÏÐ״̬ʱ£¬SCK±£³ÖµÍµçƽ
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;                                                                                          //ʱÖÓÏàλ Êý¾Ý²ÉÑù´ÓµÚÒ»¸öʱÖÓ±ßÑØ¿ªÊ¼
        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                                                                                                          //Èí¼þ²úÉúNSS
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;   //²¨ÌØÂÊ¿ØÖÆ SYSCLK/16
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;                                                                          //Êý¾Ý¸ßλÔÚÇ°
        SPI_InitStructure.SPI_CRCPolynomial = 7;                                                                                          //CRC¶àÏîʽ¼Ä´æÆ÷³õʼֵΪ7
        SPI_Init(NRF_SPI, &SPI_InitStructure);  
        SPI_SSOutputCmd(NRF_SPI, ENABLE);
        SPI_Cmd(NRF_SPI, ENABLE);

}
#endif /* 0 */


void JrSpiInit(void)
{
        SPI_InitTypeDef  SPI_InitStructure;       

        /* ENABLE SPI clock*/
        TIP_JR_SPI_CLK_INIT(TIP_JR_SPI_CLK, ENABLE);

        /* SPI configuration*/
//        NotSelect_NRF();
        SPI_Cmd(TIP_JR_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(TIP_JR_SPI, &SPI_InitStructure);       
        SPI_Cmd(TIP_JR_SPI, ENABLE);        
}

void JrInit(void)
{
        JrGpioInit();
        JrSpiInit();
}

/****************************************************************************
* Ãû    ³Æ£ºunsigned char SPI_NRF_SendByte(unsigned char byte)
* ¹¦    ÄÜ£ºÍ¨¹ýSPI2 ·¢ËÍÒ»¸ö×Ö½ÚµÄÊý¾Ý¡£
* Èë¿Ú²ÎÊý£ºbyte£º         ·¢Ë͵ÄÊý¾Ý
* ³ö¿Ú²ÎÊý£º½ÓÊÕµ½µÄ×Ö½Ú
* ˵    Ã÷£º
* µ÷Ó÷½·¨£ºSPI_NRF_SendByte(data1);
****************************************************************************/  
unsigned char SPI_NRF_SendByte(unsigned char byte)
{
  /* Ñ­»·¼ì²â·¢ËÍ»º³åÇøÊÇ·ñÊÇ¿Õ */
  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

  /* ͨ¹ýSPI1ÍâÉè·¢³öÊý¾Ý */
  SPI_I2S_SendData(SPI1, byte);

  /* µÈ´ý½ÓÊÕÊý¾Ý£¬Ñ­»·¼ì²é½ÓÊÕÊý¾Ý»º³åÇø */
  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

  /* ·µ»Ø¶Á³öµÄÊý¾Ý */
  return SPI_I2S_ReceiveData(SPI1);
}


/****************************************************************************
* Ãû    ³Æ£ºunsigned char SPI_RW_Reg(unsigned char data1,unsigned char data2)
* ¹¦    ÄÜ£ºÍ¨¹ýSPI2 ½«µ¥×Ö½ÚдÈëµ½NRF24L01+Ö¸¶¨µÄ¼Ä´æÆ÷Àï¡£
* Èë¿Ú²ÎÊý£ºdata1£º         NRF24L01¼Ä´æÆ÷
                        data2:             µ¥×Ö½ÚÊý¾Ý
* ³ö¿Ú²ÎÊý£º½ÓÊÕµ½µÄ×Ö½Ú
* ˵    Ã÷£º
* µ÷Ó÷½·¨£ºSPI_RW_Reg(WRITE_REG1 + EN_AA, 0x3f);  
****************************************************************************/  
unsigned char SPI_RW_Reg(unsigned char data1,unsigned char data2)
{
  unsigned int Data = 0;   
  Select_NRF();                                                     //Ñ¡ÔñNRF24L01Ƭѡ
#ifdef _TIP_NRF_H_
        data1 |= 0x20;
#else
#ifdef _TIP_SI4432_H_
        data1 |= 0x80;
#endif
#endif
  Data=SPI_NRF_SendByte(data1);                          //Ö¸¶¨NRF24L01¼Ä´æÆ÷
  SPI_NRF_SendByte(data2);                                 //дÈëÊý¾Ý
  NotSelect_NRF();                                                  //½ûÖ¹NRF24L01Ƭѡ
  return(Data);                                                         //·µ»ØNRF24L01 д¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
}  


/****************************************************************************
* Ãû    ³Æ£ºunsigned char SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
* ¹¦    ÄÜ£ºÍ¨¹ýSPI1 ½«Êý×éÀïµÄÊý¾ÝдÈëµ½NRF24L01+Ö¸¶¨µÄ¼Ä´æÆ÷Àï¡£
* Èë¿Ú²ÎÊý£ºreg£º         NRF24L01¼Ä´æÆ÷
                        pBuf:         Êý×é
                    bytes£º        дÈëµÄ×Ö½ÚÊý
* ³ö¿Ú²ÎÊý£º½ÓÊÕµ½µÄ×Ö½Ú
* ˵    Ã÷£º
* µ÷Ó÷½·¨£ºSPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH);
****************************************************************************/  
unsigned char SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
        unsigned char status,byte_ctr;
#ifdef _TIP_NRF_H_
                reg |= 0x20;
#else
#ifdef _TIP_SI4432_H_
                reg |= 0x80;
#endif
#endif

  Select_NRF();                                             //Ñ¡ÔñNRF24L01Ƭѡ
  status=SPI_NRF_SendByte(reg);                                  //Ö¸¶¨NRF24L01¼Ä´æÆ÷
         
  for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) //дÈëÖ¸¶¨³¤¶ÈµÄÊý¾Ý
  {
                SPI_NRF_SendByte(*pBuf++);         
        }       
  NotSelect_NRF();                                          //½ûÖ¹NRF24L01Ƭѡ
  return(status);                                                    //·µ»ØNRF24L01 д¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
}


/****************************************************************************
* Ãû    ³Æ£ºunsigned char SPI_Read(BYTE reg)
* ¹¦    ÄÜ£ºÍ¨¹ýSPI1 ½«NRF24L01+Ö¸¶¨µÄ¼Ä´æÆ÷Àï¶Á³öÒ»¸ö×Ö½Ú¡£
* Èë¿Ú²ÎÊý£ºreg£º         NRF24L01¼Ä´æÆ÷                
* ³ö¿Ú²ÎÊý£ºÖ¸¶¨NRF24L01¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
* ˵    Ã÷£º
* µ÷Ó÷½·¨£ºstatus=SPI_Read(READ_REG1+STATUS);
****************************************************************************/  
unsigned char SPI_Read(BYTE reg)
{
  unsigned char Data;
  Select_NRF();                                                    //Ñ¡ÔñNRF24L01Ƭѡ
  delay_ms(100);
  SPI_NRF_SendByte(reg);                                    //Ö¸¶¨NRF24L01¼Ä´æÆ÷
  Data=SPI_NRF_SendByte(0);                                //¶Á³öÊý¾Ý   Data=SPI_NRF_SendByte(0);       
  NotSelect_NRF();                                             //½ûÖ¹NRF24L01Ƭѡ
  return (Data);
}

u16 GetFrequency(void)
{
  return 0;
}

u32 GetAddress(void)
{
  return 0;
}
void Si4432Init(u16 frequency,u32 address)
{
        u8 Fr0 =0;
        u8 Fr1 =0;
        u8 addr3 =0,addr2 =0,addr1 =0;
       
        JrInit();
        LED_GPIO_Config();
        LED1( ON );
       

        Select_NRF();
        delay_ms(100);
        NotSelect_NRF();
        delay_ms(100);

        /*software reset*/
        SPI_RW_Reg(0x07, 0x80);
        delay_ms(100); //at least 15ms

        SPI_Read(0x3);
  SPI_Read(0x4);        //clear receive and send interrupt
        delay_ms(100);
       
  JrNvicInit();
       
        addr3 = address >>16;
        addr2 =(address >>8)&0xFF;
        addr1 = address &0xFF;
        if((frequency >0)&&(frequency <=930))
        {
                        if(frequency <480)
                        {
                                 Fr0 =frequency/10-24+64;
                                 Fr1 =((frequency%10)*6400)>>8;
                        }
                        else
                        {
                                Fr0 =(frequency-480)/20+64;
                                Fr1 =((frequency%20)*6400)>>8;
                }
  }
        else
                return;
       
        SPI_RW_Reg(0x75, Fr0);  
        SPI_RW_Reg(0x76, Fr1);  
        SPI_RW_Reg(0x77, 0x00); //init 433MHz

        SPI_Read(0x75);
        SPI_Read(0x76);
        SPI_Read(0x77);

        SPI_RW_Reg(0x2a, 0x14);
        SPI_RW_Reg(0x6e, 0x4e);
        SPI_RW_Reg(0x6f, 0xa5);
        SPI_RW_Reg(0x70, 0x2c);         //9.6kbps
       
//SpiWriteRegister(0x72, 0x38);        //(1.2kbps)       
    SPI_RW_Reg(0x72, 0x48);        // frequency bias (9.6kbps)       
//(9.6 kbps, deviation: 45 kHz, channel filter BW: 102.2 kHz
        SPI_RW_Reg(0x1C, 0x1e);                                                        //write 0x1E to the IF Filter Bandwidth register               
        SPI_RW_Reg(0x20, 0xd0);                                                        //write 0xD0 to the Clock Recovery Oversampling Ratio register               
        SPI_RW_Reg(0x21, 0x00);                                                        //write 0x00 to the Clock Recovery Offset 2 register               
        SPI_RW_Reg(0x22, 0x9d);                                                        //write 0x9D to the Clock Recovery Offset 1 register               
        SPI_RW_Reg(0x23, 0x49);                                                        //write 0x49 to the Clock Recovery Offset 0 register               
        SPI_RW_Reg(0x24, 0x00);                                                        //write 0x00 to the Clock Recovery Timing Loop Gain 1 register               
        SPI_RW_Reg(0x25, 0x24);                                                        //write 0x24 to the Clock Recovery Timing Loop Gain 0 register               
        SPI_RW_Reg(0x1D, 0x40);                                                        //write 0x40 to the AFC Loop Gearshift Override register               
        SPI_RW_Reg(0x1E, 0x0A);                                                        //write 0x0A to the AFC Timing Control register               
        SPI_RW_Reg(0x2A, 0x20);                                                        //write 0x20 to the AFC Limiter register                               

        /* preamble & synchronous words configuration*/
        SPI_RW_Reg(0x34, 0X0A);                                        //  5 bytes of preamble
        SPI_RW_Reg(0x35, 0x2A);                                        // preamble check enable
        SPI_RW_Reg(0x33, 0x04);                                        // Transmitted Synchronization Word 3 first, followed by sync word 2,end by sync word 1
        SPI_RW_Reg(0x36, addr3);                                        //Synchronization Word 0x2dd401
        SPI_RW_Reg(0x37, addr2);
        SPI_RW_Reg(0x38, addr1);        //this word can change for address
        SPI_RW_Reg(0x30, 0x8D);                                        //enalbe PH+FIFO MODE, MSB, CCITT CRC
    SPI_RW_Reg(0x32, 0x00 );                                // disable frame header
        SPI_RW_Reg(0x71, 0x63);                                        // tranismit without CLK, FIFO, FSK MODE
       
        /*GPIO configuration*/
        SPI_RW_Reg(0x0b, 0x12); //  
        SPI_RW_Reg(0x0c, 0x15); //

        /* others configuration */
        SPI_RW_Reg(0x09, 0xD7);  // loading capacity
        SPI_RW_Reg(0x69, 0x60);  //AGC overload

        /* tx power*/
        SPI_RW_Reg(0x6d, 0x1e);

        /* enable rx manually*/
        SPI_RW_Reg(0x07, 0x05);

        /* enable rx interrupt*/
        SPI_RW_Reg(0x05, 0x03);
        SPI_RW_Reg(0x06, 0x00);

        /* clear interrupt*/
        SPI_Read(0x03);
        SPI_Read(0x04);       

}

void Si4432Send(uint8_t* buf, uint8_t len)
{
        if(tx_ok)
        {
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x5);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x6);       
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x7);
        SI4432_RxBUFF[rx_count++] = SPI_Read(0x8);               
               
                SPI_RW_Reg(0x07, 0x01);        //ready mode
                delay_ms(1);                // delay for stable
               
                SPI_RW_Reg(0x3e, len);  // SET transimit length
                while(len--)
                SPI_RW_Reg(0x7F, *buf++);
               
                SPI_RW_Reg(0x05, 0x04);        //enable trasimition complete interrupt
                SPI_RW_Reg(0x06, 0x00);
                SPI_Read(0x03);                        //clear interrupt flags
                SPI_Read(0x04);
                SPI_RW_Reg(0x07, 0x09);        // transmition enable

                tx_ok = 0;
        }
}



void Si4432Suspend(void)
{
SPI_RW_Reg(0x07, 0x00); //wait mode
}

void Si4432ErrHandle(void)
{
        SPI_RW_Reg(0x08, 0x03);        //clear rx/tx fifo
  SPI_Read(0x03);                            //clear interrupt flags
        SPI_Read(0x04);
}



void Si4432_ISR(void)
{

        uint8_t status1 = 0;
        uint8_t status2 = 0;
        uint8_t SI4432_RxLenth = 0;
        uint8_t SI4432_RxCount = 0;
        delay_ms(1);
        status1 = SPI_Read(0x03);                //read the Interrupt Status1 register
        status2 = SPI_Read(0x04);                //read the Interrupt Status2 register, just for clear the pending bits?

       
        if ( (status1 & 0x02) == 0x02 ) /*Valid Packet Received*/
        {
                SI4432_RxLenth = SPI_Read(0x4B);                                                       
                for (SI4432_RxCount=0;SI4432_RxCount < SI4432_RxLenth;SI4432_RxCount++)
                        {
                                SI4432_RxBUFF[SI4432_RxCount] = SPI_Read(0x7F);       
                        }
                SI4432_RxCount=0;
                SPI_RW_Reg(0x08, 0x02);       
                SPI_RW_Reg(0x08, 0x00);
                SPI_RW_Reg(0x07, 0x05); //enble rx
                        GPIO_WriteBit(GPIOC, GPIO_Pin_13,
                             (BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13))));
        }
        if(  (status1 & 0x04) == 0x04 ) /* packet sent interrupt*/
        {
                tx_ok = 1;
                GPIO_WriteBit(GPIOC, GPIO_Pin_13,
                             (BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13))));
        }
        status1 = SPI_Read(0x03);                //read the Interrupt Status1 register
        status2 = SPI_Read(0x04);                //read the Interrupt Status2 register, just for clear the pending bits?

}

extern void Si4432_ISR();
void TIP_JR_IRQHANDLER(void)
{
        if(EXTI_GetITStatus(TIP_JR_IRQ_EXTI_LINE) != RESET )
        {
                if( GPIO_ReadInputDataBit( TIP_JR_IRQ_PORT, TIP_JR_IRQ_PIN) == RESET)
                {
                        Si4432_ISR();
                }
                EXTI_ClearITPendingBit(TIP_JR_IRQ_EXTI_LINE);
        }
}

使用特权

评论回复
6
zh113214| | 2014-8-28 21:55 | 只看该作者
,俺也看的不明白,都不知这是在什么地方用的呢

使用特权

评论回复
7
lqsgg| | 2019-5-15 16:21 | 只看该作者
谢谢分享!学习学习!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

34

帖子

0

粉丝