⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packettransceiver.c

📁 chipcon公司无线芯片cc1100的主机通信程序 具有一定的参考价值
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                CHIPCON CC1010 Wireless headset project       *
 *      ***   + +   ***                                                      *
 *      ***   +++   ***        PacketTransceiver for the master unit         *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * This source file is part of a software project for Full Duplex,           *
 * single-chip, wireless intercom, written for the CC1010 chip               *
 * (RF-transceiver chip with integrated 8051 micro-controller).              *
 *****************************************************************************
 * Author:              OAE                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: PacketTransceiver.c,v $
 * Revision 1.2  2003/08/18 12:19:33  tos
 * Synchronisation with library update (reentry issue).
 *
 * Revision 1.1  2003/08/04 12:34:27  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include <chipcon/reg1010.h>
#include <chipcon/cc1010eb.h>
#include <chipcon/hal.h>

// Define ocnstants
#define WAIT 1
#define DO_NOT_WAIT 0

// External variables
extern RF_RXTXPAIR_CALDATA xdata RF_CALDATA;
extern byte xdata DES_KEY[7];

// Prototypes
void InitializeDisassembler();
byte* DES(byte options, byte xdata* buffer, byte xdata* key,
    word length, bit wait);
void ResetReceiveBuffers();
ulong ConfigureTimer23(byte options, ulong period, word clkFreq);

/**************************************************************************
* PacketSend() - Routine for RF-transmission of a single packet           *
***************************************************************************
* Description:                                                            *
*   Used to send a packet using the chosen RF configuration. The RF       *
*   calibration data must be available.                                   *
***************************************************************************
* Input arguments:                                                        *
*   byte numpreambles: The number of preamble bytes (alternating ones and *
*       zeros) to be sent prior to the synchronization byte.              *
*   byte xdata *bufferaddress: A pointer to the location (in XRAM) where  *
*       the first byte of the packet, which is to be sent, is stored.     *
*   word packetsize: Defines the number of bytes to be sent.              *
*   RF_RXTXPAIR_SETTINGS* RF_SETTINGS: A pointer to the structure holding *
*       the chosen RF RX/TX parameters.                                   *
* Return value;                                                           *
*   None.                                                                 *
**************************************************************************/
void PacketSend(byte numpreambles, byte xdata *bufferaddress,
    word packetsize, RF_RXTXPAIR_SETTINGS* RF_SETTINGS)
{
    // Encrypt the buffered packet before transmission
    DES(DES_SINGLE_DES | DES_ENCRYPT | DES_CFB_MODE,
        bufferaddress, &DES_KEY, packetsize, DO_NOT_WAIT);

    // Prepare for transmission
    halRFSetRxTxOff(RF_TX, RF_SETTINGS, &RF_CALDATA);

    // Load buffer (RBUF) with the preamble byte
    RF_SEND_BYTE(RF_PREAMBLE_BYTE);

    // Activate the digital modem and PA
    RF_START_TX();

    // Send remaining preambles
    while(--numpreambles)
        RF_WAIT_AND_SEND_BYTE(RF_PREAMBLE_BYTE);

    // Send synchronising byte
    RF_WAIT_AND_SEND_BYTE(RF_SUITABLE_SYNC_BYTE);

    // Send buffered data
    while(packetsize--)
        RF_WAIT_AND_SEND_BYTE(*bufferaddress++);

    // Send two extra bytes to empty buffer (RFBUF)
    RF_WAIT_AND_SEND_BYTE(0);
    RF_WAIT_AND_SEND_BYTE(0);

    // End transmission
    halRFSetRxTxOff(RF_OFF, NULL, NULL);

}// End function


/**************************************************************************
* PacketReceive() - Routine for RF-reception of a single packet           *
***************************************************************************
* Description:                                                            *
*   Used to receive a packet using the chosen RF configuration. The RF    *
*   calibration data must be available.                                   *
***************************************************************************
* Input arguments:                                                        *
*   byte numpreamblebitsense: The number of preamble bits (alternating    *
*       ones and zeros) that must be received before a synchronization    *
*       byte is accepted.                                                 *
*   byte xdata *bufferaddress: A pointer to the location (in XRAM) where  *
*       the first byte of the packet, which is to be received, is stored. *
*   word packetsize: Defines the number of bytes to be received.          *
*   RF_RXTXPAIR_SETTINGS* RF_SETTINGS: A pointer to the structure holding *
*       the chosen RF RX/TX parameters.                                   *
* Return value;                                                           *
*   None.                                                                 *
**************************************************************************/
void PacketReceive(byte numpreamblebitsense, byte xdata * bufferaddress,
    word packetsize, RF_RXTXPAIR_SETTINGS* RF_SETTINGS)
{
    // Define flag for reception timeout indication
    bit timeoutflag = 0;

    // Copy the packetsize
    word packetsizecopy = packetsize;

    // Save copy of buffer address
    byte xdata *bufferaddresscopy = bufferaddress;

    // Prepare for reception
    halRFSetRxTxOff(RF_RX, RF_SETTINGS, &RF_CALDATA);

    // Configure Timer 2 for timeout detection
    halConfigTimer23(TIMER2|TIMER23_NO_INT_TIMER, 6000, CC1010EB_CLKFREQ);
    INT_SETFLAG(INUM_TIMER2, INT_CLR);
    TIMER2_RUN(TRUE);

    // Set the number of preamble bits
    RF_SET_PREAMBLE_COUNT(numpreamblebitsense);

    // Start reception
    RF_START_RX();

    // Wait for synch or timeout
    while (TRUE)
    {
        // Check if timeout has occurred
        if(INT_GETFLAG(INUM_TIMER2))
        {
            // Toggle yellow led to indicate no valid reception
            YLED = !YLED;

            // Set timeout flag
            timeoutflag = 1;

            break;

        }// End if

        // Check if sync byte is received
        if(RF_BYTE_RECEIVED())
        {
            // Clear interrupt flag
            INT_SETFLAG(INUM_RF, INT_CLR);

            break;

        }// End if

    }// End while

    // Receive data when no timeout occured
    if (!timeoutflag)
    {
        // Receive and buffer the packet
        while(packetsize--)
            RF_WAIT_AND_RECEIVE_BYTE(*bufferaddress++);

        // Perform DES-decryption on the received packet
        DES(DES_SINGLE_DES | DES_DECRYPT | DES_CFB_MODE,
            bufferaddresscopy, &DES_KEY, packetsizecopy, WAIT);

        // Reset the PacketDisassembler (ensures correct timing)
        InitializeDisassembler();

    }// End if
    
    // Fill buffer with NULL-samples when timeout occured
    else
    {
        ResetReceiveBuffers();

    }// End else

    // Stop timer used for timeout detection
    TIMER2_RUN(FALSE);

    // Disable the preamble detector
    RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF);

    // End reception
    halRFSetRxTxOff(RF_OFF, NULL, NULL);

}// End function


/**************************************************************************
* AverageFilterUpdate() - Routine for average filter update               *
***************************************************************************
* Description:                                                            *
*   Used to receive a number of preambles (alternating ones and zeros) in *
*   order to update the average filter of the RF-receiver. The average    *
*   filter is free running during update, and locked after completion.    *
*   The RF calibration data must be available.                            *
***************************************************************************
* Input arguments:                                                        *
*   byte numpreamblebitsense: The number of preamble bits (alternating    *
*       ones and zeros) that must be received before a synchronization    *
*       byte is accepted.                                                 *
*   RF_RXTXPAIR_SETTINGS* RF_SETTINGS: A pointer to the structure holding *
*       the chosen RF RX/TX parameters.                                   *
* Return value;                                                           *
*   bit: High after timeout.                                              *
**************************************************************************/
bit AverageFilterUpdate(byte numpreamblebitsense,
    RF_RXTXPAIR_SETTINGS* RF_SETTINGS)
{
    // Define flag for reception timeout indication
    bit timeoutflag = 0;

    // Prepare for reception
    halRFSetRxTxOff(RF_RX, RF_SETTINGS, &RF_CALDATA);

    // Set the number of preamble bits
    RF_SET_PREAMBLE_COUNT(numpreamblebitsense);

    // Configure Timer 2 for timeout detection
    halConfigTimer23(TIMER2|TIMER23_NO_INT_TIMER, 10000, CC1010EB_CLKFREQ);
    INT_SETFLAG(INUM_TIMER2, INT_CLR);
    TIMER2_RUN(TRUE);

    // Start reception
    RF_START_RX();

    // Wait for sync or timeout
    while (TRUE)
    {
        // Check if timeout has occurred
        if(INT_GETFLAG(INUM_TIMER2))
        {
            // Set timeout flag
            timeoutflag = 1;

            break;

        }// End if

        // Check if synch byte received
        if(RF_BYTE_RECEIVED())
        {
            // Clear interrupt flag
            INT_SETFLAG(INUM_RF, INT_CLR);
            
            break;

        }// End if

    }// End while

    // Lock average filter
    RF_LOCK_AVERAGE_FILTER(TRUE);

    // Stop timer used for timeout detection
    TIMER2_RUN(FALSE);

    // Disable the preamble detector
    RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF);

    // End reception
    halRFSetRxTxOff(RF_OFF, NULL, NULL);

    // Return timeout status
    return timeoutflag;

}// End function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -