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

📄 rf232.c

📁 基于cc1010的设计实例
💻 C
📖 第 1 页 / 共 2 页
字号:
    // Frequency separation: 64 kHz
    // Data rate: 19.2 kBaud
    // Data Format: NRZ
    // RF output power: 10 dBm
    // IF/RSSI: RSSI Enabled

    RF_RXTXPAIR_SETTINGS code RF_SETTINGS = {
        0xA3, 0x2F, 0x0E,    // Modem 0, 1 and 2
        0x58, 0x00, 0x00,    // Freq A
        0x41, 0xFC, 0x9C,    // Freq B
        0x02, 0x80,          // FSEP 1 and 0
        0x60,                // PLL_RX
        0x48,                // PLL_TX
        0x44,                // CURRENT_RX
        0x81,                // CURRENT_TX
        0x0A,                // FREND
        0xFF,                // PA_POW
        0xC0,                // MATCH
        0x00,                // PRESCALER
    };
#endif

    // Calibration data
    RF_RXTXPAIR_CALDATA xdata RF_CALDATA;

    // Initialize peripherals
    WDT_ENABLE(FALSE);
    RLED_OE(TRUE); RLED = LED_OFF;
    YLED_OE(TRUE); YLED = LED_OFF;
    GLED_OE(TRUE); GLED = LED_ON;
    BLED_OE(TRUE); BLED = LED_OFF;

    // Set optimum settings for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);

    // Local settings
    sppSettings.myAddress = MY_ADDRESS;
    sppSettings.rxTimeout = NORMAL_TIMEOUT;
    sppSettings.txAckTimeout = 2;
    sppSettings.txAttempts = 2;
    sppSettings.txPreambleByteCount = 7;

    // Setup serial port flow control
    PORTDIRBIT(1, 5, POUT);
    UART0_FLOW_CONTROL = READY;

    // Initialize the buffer state variables
    uartRXBuffer = 1;
    uartTXBuffer = 1;
    uartRXPos = 0;
    uartTXPos = 0;
    rfRXBuffer = 0;
    rfTXBuffer = 0;

    // RF setup
    sppSetupRF(&RF_SETTINGS, &RF_CALDATA, TRUE);

    // Initialize the SPP timer
    sppStartTimer(CC1010EB_CLKFREQ);
    SPP_INIT_TIMEOUTS();

    // Initialize the start TX timeout
    txRequest = TX_REQUEST_OFF;
    startTXTimeout = 0;
    sppSetTimerCB (SPP_CUSTOM_0_TIMER, startTX, &startTXTimeout);

    // Initialize the recalibration timeout
    recalibRequest = RECALIB_REQUEST_OFF;
    recalibTimeout = RECALIB_TIMEOUT;
    sppSetTimerCB (SPP_CUSTOM_1_TIMER, startRecalib, &recalibTimeout);

    // Initialize the RF transmit structure
    TXI.destination = DESTINATION_ADDRESS;
    TXI.pDataBuffer = pTXBuffer[rfTXBuffer];
    TXI.flags = SPP_ACK_REQ;
    TXI.status = SPP_TX_FINISHED; // Nothing has gone wrong yet...

    // Initialize the RF receive structure
    RXI.maxDataLen = RX_BUFFER_LENGTH;
    RXI.pDataBuffer = pRXBuffer[rfRXBuffer];
    lastRXflags = TXI.flags | SPP_SEQUENCE_BIT;

    // Initialize the random number generator (used to generate random timeouts)
    srand(0x1234);


    // Serial port 0 setup
    UART0_SETUP(38400, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_RX_TX | UART_ISR);

    // Run forever
    while (TRUE) {
        
        // Receive a packet
        if (sppReceive(&RXI) == SPP_RX_STARTED) {

            // Wait for RF RX to complete
            do {
                if (txRequest && (TXI.status == SPP_TX_FINISHED) && (RXI.status == SPP_RX_WAITING)) {
                    sppReset();
                }
            } while (SPP_STATUS() != SPP_IDLE_MODE); 

            // Check the results (ignore retransmitted messages)
            if (RXI.status == SPP_RX_FINISHED) {
                if (SPP_SEQUENCE_BIT & (lastRXflags ^ RXI.flags)) {

                    // Wait for UART TX to complete 
                    while (uartRXPos != rxDataLen[uartRXBuffer]);
                                                                            //
                    // Only process packets with payload data               //
                    if ((rxDataLen[rfRXBuffer] = RXI.dataLen) != 0) {       //
                                                                            //
                        // Switch buffers and initiate UART0 transmission	// No UART TX interrupts in this section
                        GLED = ~GLED;                                       //
                        SWITCH(uartRXBuffer, rfRXBuffer);                   //
                        RXI.pDataBuffer = pRXBuffer[rfRXBuffer];            //
                        uartRXPos = 0;                                      //
                        UART0_SEND(pRXBuffer[uartRXBuffer][uartRXPos]);
                    }
                }
                lastRXflags = RXI.flags;
            }
        }

        // Transmit packet, if requested
        if (txRequest) {
            if (sppSend(&TXI) == SPP_TX_STARTED) {

                // Wait for the transmission to finish
                YLED = LED_ON;
                while (SPP_STATUS() != SPP_IDLE_MODE);
                YLED = LED_OFF;

                // Check out the results
                if (TXI.status == SPP_TX_FINISHED) {
                    sppSettings.rxTimeout = NORMAL_TIMEOUT;
                    RLED = LED_OFF;
                    txRequest = TX_REQUEST_OFF;
                } else {
                    RLED = LED_ON;
                    sppSettings.rxTimeout = RETRY_TX_TIMEOUT;
                }
            }
        }

        // Recalibrate the transceiver if requested to
        if (recalibRequest) {
            sppSetupRF(&RF_SETTINGS, &RF_CALDATA, TRUE);
        }
    }
} // main




//----------------------------------------------------------------------------
//  Start transmission (called by TIMER3_ISR(), in sppTimer.c)
//----------------------------------------------------------------------------
void startTX() {

    if (!txRequest) {
        SWITCH(uartTXBuffer, rfTXBuffer);
        TXI.pDataBuffer = pTXBuffer[rfTXBuffer];
        TXI.dataLen = uartTXPos;
        uartTXPos = 0;
        UART0_FLOW_CONTROL = READY; BLED = LED_OFF;
        txRequest = TX_REQUEST_ON;
    } else {
        startTXTimeout = SHORT_TX_TIMEOUT;
    }
} // startTX





//----------------------------------------------------------------------------
//  Recalibrate the transceiver (called by TIMER3_ISR(), in sppTimer.c)
//----------------------------------------------------------------------------
void startRecalib (void) {
    recalibRequest = RECALIB_REQUEST_ON;
    recalibTimeout = RECALIB_TIMEOUT;
} // recalibrateRF



//----------------------------------------------------------------------------
//  UART 0 interrupt service routine:
//----------------------------------------------------------------------------
void UART0_ISR () interrupt INUM_UART0 {
                  

    // Byte sent (RF RX)
    if (TI_0) {
        INT_SETFLAG (INUM_UART0_TX, INT_CLR);

        // Push bytes until the buffer is empty
        if (++uartRXPos < rxDataLen[uartRXBuffer]) {
            UART0_SEND(pRXBuffer[uartRXBuffer][uartRXPos]);
        } else {
            uartRXPos = rxDataLen[uartRXBuffer];
        }
    }

    // Byte received (RF TX)
    if (RI_0) {
        INT_SETFLAG (INUM_UART0_RX, INT_CLR);
        if (uartTXPos < TX_BUFFER_LENGTH) {
            pTXBuffer[uartTXBuffer][uartTXPos++] = UART0_RECEIVE();

            // Schedule a start TX callback when the first data byte arrives through the UART
            INT_ENABLE (INUM_TIMER3, INT_OFF);
            if (!startTXTimeout) {
                startTXTimeout = START_TX_TIMEOUT;
            } else {
                if (uartTXPos == TX_BUFFER_LIMIT) {
                    // Prevent buffer overrun by turning CTS off (RTS at our end of the cable)
                    UART0_FLOW_CONTROL = STOP; BLED = LED_ON;
                    startTXTimeout = SHORT_TX_TIMEOUT;
                }
            }
            INT_ENABLE (INUM_TIMER3, INT_ON);
        }
    }

} // UART0_ISR

⌨️ 快捷键说明

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