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

📄 link2.c

📁 低成本、低功耗的RF芯片CC1100
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************************************
 *                                                                                                     *
 *        **********                                                                                   *
 *       ************                                                                                  *
 *      ***        ***                                                                                 *
 *      ***   +++   ***                                                                                *
 *      ***   + +   ***     This program lets the user test a simple rf link                           *
 *      ***   +                                                                                        *
 *      ***   + +   ***                                                                                *
 *      ***   +++   ***     Link2.c                                                                    *
 *      ***        ***                                                                                 *
 *       ************                                                                                  *
 *        **********                                                                                   *
 *                                                                                                     *
 *******************************************************************************************************
 * Compiler:                Keil C51 V7.50                                                             *
 * Target platform:         Chipcon CCxxx0 (Silabs F320)                                               *
 * Author:                  SNA                                                                        *
 *******************************************************************************************************
 * Revision history:        See end of file                                                            *
 ******************************************************************************************************/
#include <Chipcon\srf04\regssrf04.h>
#include <Chipcon\srf04\halsrf04.h>
#include <Chipcon\srf04\ebsrf04.h>
#include <Link2.h>
#include <stdlib.h>
#include <intrins.h>




//-------------------------------------------------------------------------------------------------------
// DESCRIPTION:
//      This program demonstrates how it is possible to transmit and receive packets that are longer 
//      than the FIFO size without reading the status bytes. The radios use variable packet mode,
//      CRC enabled and append status. NO FILTERING SHOULD BE USED as one should not read any
//      status registers (See the CC1100 or 2500 Errata Note) to determine if the FIFO 
//      has been flushed or not. 
//      If other data rates than 250 kbps should be used, the halWait(70) in the EXTERNAL0_ISR(void)
//      needs to be changed to:
//      halWait(n);, where n = 2*8*(1/data rate) + a couple of us margin
//      For better understanding on how this program works, please see the 
//      CC1100/CC1150DK & CC2500/CC2550DK Development Kit Examples and Libraries User Manual   
//
//      Parameters          | Values 
//      ------------------------------------------------------
//      Packet Length       | 10, 30, 50, 70, ...., 230, 250
//      Number of Packets   | 100, 200, 300, ......, 900, 1000
//      Radio Mode          | Rx, Tx
//
//      RX UNIT: 
//                  * Set radio mode to RX. 
//                  * Move joystick down until the message 
//                    "Press S1 to start" is showed on the LCD display  
//                  * Press S1
//                  
//                  The LCD display will show number of packets received with CRC OK
//
//      TX UNIT:
//                  * Set packet length and number of packets to transmit
//                  * Set radio mode to TX
//                  * Press S1 to Start
//
//                  The LCD will show number of packets transmitted
//
//                  After all the packets have been transmitted, S1 can be pressed again to transmit 
//                  or the joystick can be used to change packet length and number of packets 
//                  to transmit 
//
//  
//      IMPORTANT NOTICE!
//      When compiling this example you will get the following warnings:
//
//  *** WARNING L15: MULTIPLE CALL TO FUNCTION
//      NAME:    _HALSPIREADBURSTREG/SPIREADBURSTREG
//      CALLER1: EXTERNAL1_ISR/LINK2
//      CALLER2: EXTERNAL0_ISR/LINK2
//  *** WARNING L15: MULTIPLE CALL TO FUNCTION
//      NAME:    _HALSPIWRITEBURSTREG/SPIWRITEBURSTREG
//      CALLER1: EXTERNAL1_ISR/LINK2
//      CALLER2: ?C_C51STARTUP
//
//      These warnings are generated because the halSpiReadBurstReg and halSpiWriteBurstReg functions 
//      are not reentrant. The code is, however, written so that these interrupts will never occur while 
//      in the process of accessing the SPI interface, so these warnings can be ignored.   
//-------------------------------------------------------------------------------------------------------




//-------------------------------------------------------------------------------------------------------
// Global Variables

BYTE xdata txBuffer[258];   // 1 length byte + 255 bytes payload + 2 status bytes
BYTE xdata rxBuffer[258];   // 1 length byte + 255 bytes payload + 2 status bytes

UINT8 xdata joystickPosition = JOYSTICK_CENTER;
UINT8 xdata prevJoystickPosition = JOYSTICK_CENTER;

UINT8 state = SETUP;    // Variable used to hold the next state in the state machine in the main loop

TX_DATA xdata txData = {
    0,          // bytesLeft
    0,          // iterations 
    FALSE,      // writeRemainingDataFlag
    FALSE,      // packetSentFlag
    txBuffer,   // *pBufferIndex 
    0           // packetsSent
};  

RX_DATA xdata rxData = {
    0,          // bytesLeft
    FALSE,      // packetReceivedFlag
    SYNC,       // syncOrEndOfPacket
    rxBuffer,   // *pBufferIndex
    0,          // lengthByte
    FALSE,      // crcOK
    0           // packetsReceived
};
//-------------------------------------------------------------------------------------------------------




//-------------------------------------------------------------------------------------------------------
//  void main(void)
//
//  DESCRIPTION:
//      This function takes care of all the MCU initialization and radio configuration. The main loop
//      is implemented as a state machine and a state diagram can be found in the
//      CC1100/CC1150DK & CC2500/CC2550DK Development Kit Examples and Libraries User Manual     
//----------------------------------------------------------------------------------------------------
void main (void) {

    #ifdef STAND_ALONE
    
		// Select the Internal Oscillator as Multiplier input source and disable the watchdog timer
    	// SYSCLK = 4X Clock Multiplier / 2     
     	CLOCK_INIT();
    #endif

	// Set up the crossbar and I/O ports to communicate with the SmartRF04EB peripherals
    IO_PORT_INIT();

    // Initialize the LCD display. The SMBus uses timer 0 to generate SCL
    ebLcdInit();

    // Initialize the ADC converter
    ebAdcInit(ADC_JOY);

    SPI_INIT(SCLK_6_MHZ);
   
    POWER_UP_RESET_CCxxx0();

    halRfWriteRfSettings(&rfSettings);

    halSpiWriteReg(CCxxx0_FIFOTHR, 0x0E);   // FIFO_THR = 14
                                            // 5 bytes in TX FIFO (59 available spaces)
                                            // 60 bytes in the RX FIFO
                                            // If this threshold is changed, AVAILABLE_BYTES_IN_TX_FIFO
                                            // and BYTES_RX_FIFO (Link2.h) must be updated

    halSpiWriteReg(CCxxx0_PATABLE, paTable);
	
    handleAndDisplayMenu();

    INT_PRIORITY(INUM_EXTERNAL1, INT_HIGH); 
    
    // Infinite loop
    while (TRUE) {
        switch (state) {
            
            //----------------------------------------------------------------------------------------
            case SETUP:
            //----------------------------------------------------------------------------------------
            
                // This wait is implemented to give the LCD display enought time to be update
                // between each iteration
                halWait(250);

                joystickPosition = ebGetJoystickPosition();

                if (prevJoystickPosition != joystickPosition)
                    parseMenu(joystickPosition);

                prevJoystickPosition = joystickPosition;

                if ((index == START) && (ebButtonPushed())) {
                    
                    ENABLE_GLOBAL_INT(INT_OFF);

                    if (menuData.radioMode == RX) {
                        state = RX_START;
                        ebLcdUpdate("Waiting for", "data");

                        // Associated to the RX FIFO: Asserts when RX FIFO is filled above RXFIFO_THR.
                        // De-asserts when RX FIFO is drained below RXFIFO_THR.
                        halSpiWriteReg(CCxxx0_IOCFG2, 0x00);

                        SETUP_GDO2_INT(EDGE, HIGH);     // Enables external interrupt on rising edge
                        SETUP_GDO0_INT(EDGE, HIGH);
                    } else {
                        state = TX_START;
                        
                        // Associated to the TX FIFO: Asserts when the TX FIFO is filled above TXFIFO_THR.
                        // De-asserts when the TX FIFO is below TXFIFO_THR.
                        halSpiWriteReg(CCxxx0_IOCFG2, 0x02);
          
                        SETUP_GDO2_INT(EDGE, LOW);      // Enables external interrupt on falling edge
                        SETUP_GDO0_INT(EDGE, LOW);
                    }
                    _nop_(); // If this instruction is removed, IE0 might not be cleared
                    INT_SETFLAG(INUM_EXTERNAL0, INT_CLR);   // Clears the interrupt flag
                    INT_SETFLAG(INUM_EXTERNAL1, INT_CLR);   // Clears the interrupt flag
                    ENABLE_GLOBAL_INT(INT_ON);
                }
                break;            
                
            //----------------------------------------------------------------------------------------
            case TX_START:
            //----------------------------------------------------------------------------------------              

                // Create data packet
                createDataPacket();

                halWait(25000); // Delay to allow for the LCD display to be updated for every packet 
				
                // The entire packet can be written at once
				if (menuData.packetLength < FIFO_SIZE) {
                    halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, (menuData.packetLength + 1));
                    halSpiStrobe(CCxxx0_STX);

                    INT_ENABLE(INUM_EXTERNAL1, INT_OFF);

                } else { // The TX FIFO needs to be re-filled several times
                    halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, FIFO_SIZE); // Fill up the TX FIFO
                    halSpiStrobe(CCxxx0_STX);
                    txData.bytesLeft = menuData.packetLength + 1 - FIFO_SIZE;
                    txData.pBufferIndex = txBuffer + FIFO_SIZE;
                    txData.iterations = (txData.bytesLeft / AVAILABLE_BYTES_IN_TX_FIFO);
                    if (!txData.iterations)
                        txData.writeRemainingDataFlag = TRUE; 
                
                    // Enable external interrupt 1 (the TX FIFO will need to be re-filled in this ISR)              
                    ENABLE_GLOBAL_INT(INT_OFF);
                    INT_ENABLE(INUM_EXTERNAL1, INT_ON);
                    INT_SETFLAG(INUM_EXTERNAL1, INT_CLR);
                    ENABLE_GLOBAL_INT(INT_ON);
                }            
                state = TX_WAIT;

            //----------------------------------------------------------------------------------------
            case TX_WAIT:
            //----------------------------------------------------------------------------------------

⌨️ 快捷键说明

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