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

📄 packettransceiver.c

📁 chipcon公司无线芯片cc1100的从机通信程序 具有一定的参考价值
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                CHIPCON CC1010 Wireless audio project         *
 *      ***   + +   ***                                                      *
 *      ***   +++   ***        PacketTransceiver for the slave 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:20:10  tos
 * Synchronisation with library update (reentry issue).
 *
 * Revision 1.1  2003/08/04 12:33:15  tos
 * Initial version in CVS.
 *
 *                                                                           *
 *                                                                           *
 ****************************************************************************/

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

#define WAIT 1
#define DO_NOT_WAIT 0

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

// Function prototypes
void InitializeDisassembler();
void InitializeAssembler();
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)
{
    // Reset the PacketAssembler (for correct timing)
    InitializeAssembler();

    // 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 the packet
    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)
{
    // Save copies of packetsize
    word packetsizecopy1 = packetsize;
    word packetsizecopy2 = packetsize;

    // Save copies of buffer address
    byte xdata *bufferaddresscopy1 = bufferaddress;
    byte xdata *bufferaddresscopy2 = bufferaddress;

    // Define flag for reception timeout indication
    bit timeoutflag = 0;

    // 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)
    {
        if(INT_GETFLAG(INUM_TIMER2))
        {
            // Set timeout flag
            timeoutflag = 1;

            break;

        }// End if

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

            break;

        }// End if

    }// End while
    
    /* Fill buffer with NULL-samples after timeout, and wait
    for the next packet */
    if (timeoutflag)
    {   
        // Fill buffers with NULL-samples
        ResetReceiveBuffers();

        // Wait for synch byte
        while (!RF_BYTE_RECEIVED());

        // Clear interrupt flag
        INT_SETFLAG(INUM_RF, INT_CLR);

    }// End if

    // Receive and buffer a packet after synch byte is received
    while(packetsizecopy1--)
        RF_WAIT_AND_RECEIVE_BYTE(*bufferaddresscopy1++);

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

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

    // 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;                                                           *
*   None.                                                                 *
**************************************************************************/
void AverageFilterUpdate(byte numpreamblebitsense,
    RF_RXTXPAIR_SETTINGS* RF_SETTINGS)
{
    // Prepare for reception
    halRFSetRxTxOff(RF_RX, RF_SETTINGS, &RF_CALDATA);

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

    // Start reception
    RF_START_RX();

    // Wait for synch byte (which generate an interrupt)
    while (!RF_BYTE_RECEIVED());

    // Clear interrupt flag
    INT_SETFLAG(INUM_RF, INT_CLR);

    // Lock average filter
    RF_LOCK_AVERAGE_FILTER(TRUE);

    // Disable the preamble detector
    RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF);

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

}// End function

⌨️ 快捷键说明

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