📄 rf232.c
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 EXAMPLE PROGRAM *
* *** + + *** RF232 (RS232 <-> RF <-> RS232) *
* *** +++ *** Wireless null-modem cable *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* This program demonstrates the use of the SPP library, timers, and the *
* serial port. The program is mainly interrupt driven. *
* *
* A pair of CC1010EBs running this program will function as a wireless *
* null-modem cable, between serial port 0 on each board. *
* *
* *
* +----+ +--------+ +--------+ +----+ *
* | | UART0 RX | | | | UART0 RX | | *
* | |----------->| | RF/SPP with ACK | |<-----------| | *
* | PC | | CC1010 |<--------------->| CC1010 | | PC | *
* | | UART0 TX | | Half duplex | | UART0 TX | | *
* | |<-----------| | | |----------->| | *
* +----+ +--------+ +--------+ +----+ *
* *
* 38400 baud RF @ 19200 baud 38400 baud *
* *
* *
* FEATURES: *
* - Max RS232 baud rate: 38400 (no parity bit) *
* - Max RF baud rate: 19200/NRZ *
* - Half-duplex *
* - Hardware flow control (CTS only) *
* - Automatic retransmission and transmission crash recovery through *
* randomized timeouts *
* - Serial input/output while the transceiver is active (through use of *
* interrupts) *
* - Automatic recalibration of the transceiver (every 2 minutes) *
* *
* EXPECTED PERFORMANCE (serial port data transfer): *
* - 1k Xmodem: >7000 bps with RF@9600 and UART@38400 *
* - 1k Xmodem: >11500 bps with RF@19200 and UART@38400 *
* *
* LED INDICATORS: *
* - Red: Waiting for retransmission *
* - Yellow: Transmitting *
* - Green: Toggled for each received packet *
* - Blue: Hardware flow control activated (CTS) *
* *
* HOW IT WORKS: *
* The main loop takes care of RF transmission and reception. *
* The UART0 ISR takes care of UART transmission and reception. *
* *
* RF->UART: *
* When an RF packet has been received, the RF RX and UART TX buffer *
* are switched, and the packet data is transmitted over the UART *
* (after making sure that it's ready). *
* *
* UART->RF: *
* When the first byte is received, a start RF countdown begins. To *
* avoid buffer overruns the serial port flow control is enabled *
* after TX_BUFFER_LIMIT bytes. The RF TX buffer and the UART RX *
* buffers are switched, and the RF transmission begins. In the mean *
* while the UART is still ready for receptions (into the other *
* buffer). *
*****************************************************************************
* Author: JOL *
*****************************************************************************
* Revision history: *
* 1.0 2002/07/18 First public release *
* *
* $Log: rf232.c,v $
* Revision 1.5 2003/04/03 12:27:36 tos
* Bugfix:
* Disabled timer 3 interrupt in UART0_ISR (Caused the program to hang on TX_BUFFER_LIMIT)
* Removed SPP timer function calls after recalibration (sppSetupRF).
*
* Revision 1.4 2003/02/03 08:25:33 tos
* Correction of UART flow contol: activate flow control to avoid buffer overrun.
*
* Revision 1.3 2003/01/30 07:29:19 tos
* Corrected recalibration procedure: re-initialization of function pointers (Call Back).
*
* Revision 1.2 2002/11/20 14:10:00 kht
* Added startup macros
* Moved call to sppSetupRF to be called before setting callbacks, this was
* required due to change in SPP library
*
* Revision 1.1 2002/10/14 10:55:46 tos
* Initial version in CVS.
*
* *
****************************************************************************/
#include <chipcon/reg1010.h>
#include <chipcon/cc1010eb.h>
#include <chipcon/hal.h>
#include <chipcon/cul.h>
#include <stdlib.h> // srand(), rand()
// MACRO: Switch two values without a third temporary variable
#define SWITCH(a,b) do { b^=a; a^=b; b^=a; } while (0)
// Buffer sizes
#define RX_BUFFER_LENGTH 255
#define TX_BUFFER_LENGTH 255
// Address definitions
#define DESTINATION_ADDRESS 255
#define MY_ADDRESS 255
// UART0 flow control
#define UART0_FLOW_CONTROL (P1_5)
#define STOP 1
#define READY 0
#define TX_BUFFER_LIMIT 150
// Data buffers
byte xdata pRXBuffer[2][RX_BUFFER_LENGTH]; byte rxDataLen[2]; // RF RX data buffer, valid length (set by RF RX)
byte xdata pTXBuffer[2][TX_BUFFER_LENGTH]; byte txDataLen[2]; // RF TX data buffer, valid length (set by UART RX)
byte xdata uartRXBuffer, uartTXBuffer, uartRXPos, uartTXPos, rfRXBuffer, rfTXBuffer;
// Transmission request flag
byte xdata txRequest;
#define TX_REQUEST_ON 1
#define TX_REQUEST_OFF 0
// Transmission timeout
word xdata startTXTimeout;
#define START_TX_TIMEOUT 7
#define SHORT_TX_TIMEOUT 2
// Recalibration request flag
byte xdata recalibRequest;
#define RECALIB_REQUEST_ON 1
#define RECALIB_REQUEST_OFF 0
// Recalibration timeout
word xdata recalibTimeout;
#define RECALIB_TIMEOUT 12000 // Every 2 minutes
// Reception timeout
#define NORMAL_TIMEOUT 100
#define RETRY_TX_TIMEOUT (1 + (rand() & 0x001F))
// Used to detect and ignore retransmissions (SPP_SEQUENCE_BIT)
byte xdata lastRXflags;
// SPP variables
SPP_SETTINGS xdata sppSettings;
SPP_RX_INFO xdata RXI;
SPP_TX_INFO xdata TXI;
// Function prototypes
void startTX (void);
void startRecalib (void);
//----------------------------------------------------------------------------
// MAIN PROGRAM
//----------------------------------------------------------------------------
void main() {
#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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -