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

📄 chatmain.c

📁 基于cc1010的设计实例
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***                      CC1010 CHAT                     *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * This program demonstrates the use of the SPP library, random number       *
 * generation, timers, the serial port (with use of escape codes), how to    *
 * assign SPP addresses and maintain a client table, pinging (broadcasted to *
 * all clients at regular intervals), and DES encryption.                    *
 *                                                                           *
 * INSTRUCTIONS:                                                             *
 * Use up to 16 evaluation boards, and connect each of them to a terminal    *
 * window via serial port 0 (38400). Create a server and 1-15 clients.       *
 * Please follow the instructions displayed at startup.                      *  
 *                                                                           *
 * HOW IT WORKS:                                                             *
 * The server and the clients share the same code. The SPP address (255 for  *
 * servers, 254 for unregistered clients and 1-15 for clients) is used to    *
 * distinguish between them.                                                 *
 *                                                                           *
 * The chat server maintains and distributes a client table. All clients     *
 * have a copy of this table, which contain nick names, addresses, keys,     *
 * etc. The server also transmits a ping message at regular intervals (about * 
 * every second). If no ping or other server messages have been received     *
 * during the last 20 seconds, the client will automatically disconnect      *
 * (passively). It will also be disconnected (actively) if messages from the *
 * server don't get through. To avoid jamming of server messages, the server *
 * has 50 TX attempts available, while the clients only have two.            *
 *                                                                           *
 * The text input is handled by the UART0 ISR. When ENTER is pressed, a flag *
 * is set to indicate that there is message ready for transmission.          *
 *                                                                           *
 * The main program loop will receive and process incoming messages. When    *
 * the TX flag is on, it will transmit, both broadcasted and private         *
 * messages. Key exchange is performed if necessary.                         *
 *                                                                           *
 * The printf/putchar functions are used to print text to the chat window.   *
 * This requires switching between interrupt-driven and polled I/O.          *
 *                                                                           *
 * LEDs:                                                                     *
 *     RED:    Retransmission                                                *
 *     YELLOW: Transmission                                                  *
 *     GREEN:  Processing received message						             *
 *     BLUE:   Toggled for each received ping                     		     *
 *                                                                           *
 * NOTE: This program requires HyperTerminal 5.0 or later (bug fix).         *
 *****************************************************************************
 * Author:              JOL                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 * 1.0  2002/08/13      First public release                                 *
 *                                                                           *
 * $Log: chatMain.c,v $
 * Revision 1.4  2003/04/03 12:31:04  tos
 * Modified for compatibility with the new version of CUL
 *
 * Revision 1.3  2002/11/19 13:48:47  kht
 * Added startup macros
 *
 * Revision 1.2  2002/10/30 14:53:15  kht
 * Changed the tempBroadcast example so that it can run on a stand-alone EM module.
 * The example now stores the unit name in Flash memory.
 *
 * Revision 1.1  2002/10/14 10:02:28  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include "chat.h"


// Data buffers
byte xdata pMessageTXBuffer[BUFFER_LENGTH] 			_at_ 0x0000; // Transmitted messages
byte xdata pAdminTXBuffer[BUFFER_LENGTH] 			_at_ 0x0100; // Transmitted administration messages
byte xdata pRXBuffer[BUFFER_LENGTH] 				_at_ 0x0200; // Received messages 
byte xdata pKeyBuffer[CI_KEY_LENGTH * MAX_CLIENTS] 	_at_ 0x0300; // DES keys

// The current input position (equals the length of the typed string)
byte xdata inPos;

// The ping countdown variable (clients only!)
word xdata noPingTicks;

// Disables message decryption (for demonstration purposes)
byte xdata noDecryption;

// The client info table
CLIENT_INFO xdata CI[MAX_CLIENTS];
CLIENT_INFO xdata *pMyCI;
byte xdata clientCount;

// SPP data
SPP_SETTINGS xdata sppSettings;
SPP_RX_INFO xdata RXI;
SPP_TX_INFO xdata TXI;

// Transmission request flag (transmit asap)
byte xdata txRequest;
#define TX_REQUEST_ON  1
#define TX_REQUEST_OFF 0

// Private (encrypted) messages
byte xdata privateAddress;	
byte xdata isPrivateMode;


//----------------------------------------------------------------------------
//	CLIENT MAIN PROGRAM: setup -> login -> main loop
//----------------------------------------------------------------------------
void main (void) {

	// Local variables
	byte temp, n;
	CLIENT_INFO xdata *pCI;

#ifdef FREQ868
	// X-tal frequency: 14.745600 MHz
	// RF frequency A: 868.277200 MHz	Rx
	// RF frequency B: 868.277200 MHz	Tx
	// RX Mode: Low side LO
	// Frequency separation: 64 kHz
	// Data rate: 19.2 kBaud
	// Data Format: NRZ
	// RF output power: 4 dBm
	// IF/RSSI: RSSI Enabled
	
    RF_RXTXPAIR_SETTINGS code RF_SETTINGS = {
        0xA3, 0x27, 0x11,    // Modem 0, 1 and 2
        0x75, 0xA0, 0x00,    // Freq A
        0x58, 0x32, 0x8D,    // Freq B
        0x01, 0xAB,          // FSEP 1 and 0
        0x40,                // PLL_RX
        0x30,                // PLL_TX
        0x6C,                // CURRENT_RX
        0xF3,                // CURRENT_TX
        0x32,                // FREND
        0xFF,                // PA_POW
        0x00,                // MATCH
        0x00,                // PRESCALER
    };
#endif

#ifdef FREQ915
    // X-tal frequency: 14.745600 MHz
    // RF frequency A: 915.027455 MHz	Rx
    // RF frequency B: 915.027455 MHz	Tx
    // RX Mode: Low side LO
    // Frequency separation: 64 kHz
    // Data rate: 19.2 kBaud
    // Data Format: NRZ
    // RF output power: 4 dBm
    // IF/RSSI: RSSI Enabled

    RF_RXTXPAIR_SETTINGS code RF_SETTINGS = {
        0xA3, 0x27, 0x11,    // Modem 0, 1 and 2
        0xAA, 0x80, 0x00,    // Freq A
        0x5C, 0xF4, 0x02,    // Freq B
        0x01, 0xAB,          // FSEP 1 and 0
        0x58,                // PLL_RX
        0x30,                // PLL_TX
        0x6C,                // CURRENT_RX
        0xF3,                // CURRENT_TX
        0x32,                // FREND
        0xFF,                // PA_POW
        0x00,                // MATCH
        0x00,                // PRESCALER
    };
#endif

#ifdef FREQ433
    // X-tal frequency: 14.745600 MHz
    // RF frequency A: 433.302000 MHz	Rx
    // RF frequency B: 433.302000 MHz	Tx
    // RX Mode: Low side LO
    // 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;

    // Startup macros for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);

	// Initialize peripherals
	WDT_ENABLE(FALSE);
	RLED_OE(TRUE);
	YLED_OE(TRUE);
	GLED_OE(TRUE);
	BLED_OE(TRUE);

	// Initialize the client info table
	initClientInfo();

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

    // Setup UART0 for polled I/O 
    UART0_SETUP(38400, CLK_FREQ, UART_NO_PARITY | UART_RX_TX | UART_POLLED);

	// Initialize the login screen
	VT100_INIT_LOGIN_SCREEN();
	printf("********************************************************************************");
	printf("                                   CC1010 CHAT                                  ");
	printf("********************************************************************************");
	printf("INSTRUCTIONS:");
	printf("\n  - Broadcasted messages are not encrypted");
	printf("\n  - To send a broadcasted message, type: [message][ENTER]");
	printf("\n  - The maximum length of a message is one line");
	printf("\n  - The DELETE key and the arrow keys are not available"); 
	printf("\n");
	printf("\nSECURITY:");
	printf("\n  - Private messages are encrypted with HW single DES (Dark yellow background)");
	printf("\n  - To send a private message, type [client index (HEX) + '<'][message][RETURN]");
	printf("\n  - To enable/disable message decryption, type ['<']\n");
	printf("********************************************************************************");
	printf("\n");

	// Ask the user for a nick name
	printf("Enter your nick name (%d chars max): ", (int) (NAME_LENGTH - 1)); // Remember the null terminator
	memset(pMyCI->name, '\0', sizeof(pMyCI->name));	
	gets(pMyCI->name, NAME_LENGTH);

	// Ask the user to choose between server and client	
	printf("\nPress '1' to become a client or '2' to become a server: ");  
	while(TRUE) {
		if ((temp = getchar()) == '2') {
	
			// Defualt local settings for servers
			sppSettings.myAddress = pMyCI->address = SERVER_ADDRESS;
			sppSettings.rxTimeout = LONG_RX_TIMEOUT;
			sppSettings.txAckTimeout = 2;
			sppSettings.txAttempts = 50;
			sppSettings.txPreambleByteCount = 7;

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

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

			printf("\nSERVER: Creating chat room...");
			halWait(255, CLK_FREQ);
			break;

		} else if (temp == '1') {
	
			// Defualt local settings for clients (the address is temporary)
			sppSettings.myAddress = pMyCI->address = NEW_CLIENT_ADDRESS;
			sppSettings.rxTimeout = LONG_RX_TIMEOUT;
			sppSettings.txAckTimeout = 2;
			sppSettings.txAttempts = 2;
			sppSettings.txPreambleByteCount = 7;

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

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

			printf("\nCLIENT: Joining chat room...");
			halWait(255, CLK_FREQ);

⌨️ 快捷键说明

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