Reception working in reflexframe.c.
[goodfet] / shellcode / chipcon / cc1110 / reflex.c
1 #include <cc1110.h>
2 #include "cc1110-ext.h"
3
4 char __xdata at 0xfe00 packet[256] ;
5
6 void carrier(){
7   // Set the system clock source to HS XOSC and max CPU speed,
8   // ref. [clk]=>[clk_xosc.c]
9   SLEEP &= ~SLEEP_OSC_PD;
10   while( !(SLEEP & SLEEP_XOSC_S) );
11   CLKCON = (CLKCON & ~(CLKCON_CLKSPD | CLKCON_OSC)) | CLKSPD_DIV_1;
12   while (CLKCON & CLKCON_OSC);
13   SLEEP |= SLEEP_OSC_PD;
14
15
16   /* Setup radio with settings from SmartRF® Studio. The default settings are
17    * used, except that "unmodulated" is chosen in the "Simple RX tab". This
18    * results in an umodulated carrier with a frequency of approx. 2.433 GHz.
19    */
20   FSCTRL1   = 0x0A;   // Frequency synthesizer control.
21   FSCTRL0   = 0x00;   // Frequency synthesizer control.
22   
23   
24     
25   MDMCFG4   = 0x86;   // Modem configuration.
26   MDMCFG3   = 0x83;   // Modem configuration.
27   MDMCFG2   = 0x30;   // Modem configuration.
28   MDMCFG1   = 0x22;   // Modem configuration.
29   MDMCFG0   = 0xF8;   // Modem configuration.
30   CHANNR    = 0x00;   // Channel number.
31   DEVIATN   = 0x00;   // Modem deviation setting (when FSK modulation is enabled).
32   FREND1    = 0x56;   // Front end RX configuration.
33   FREND0    = 0x10;   // Front end RX configuration.
34   MCSM0     = 0x14;   // Main Radio Control State Machine configuration.
35   FOCCFG    = 0x16;   // Frequency Offset Compensation Configuration.
36   BSCFG     = 0x6C;   // Bit synchronization Configuration.
37   AGCCTRL2  = 0x03;   // AGC control.
38   AGCCTRL1  = 0x40;   // AGC control.
39   AGCCTRL0  = 0x91;   // AGC control.
40   FSCAL3    = 0xE9;   // Frequency synthesizer calibration.
41   FSCAL2    = 0x2a;   // Frequency synthesizer calibration.
42   FSCAL1    = 0x00;   // Frequency synthesizer calibration.
43   FSCAL0    = 0x1f;   // Frequency synthesizer calibration
44   
45   TEST2     = 0x88;   // Various test settings.
46   TEST1     = 0x31;   // Various test settings.
47   TEST0     = 0x09;   // Various test settings.
48   
49   //FE is too high
50   PA_TABLE0 = 0xFF;   // PA output power setting.
51   PKTCTRL1  = 0x04;   // Packet automation control.
52   PKTCTRL0  = 0x22;   // Packet automation control.
53   ADDR      = 0x00;   // Device address.
54   PKTLEN    = 0xFF;   // Packet length.
55
56   /* Settings not from SmartRF® Studio. Setting both sync word registers to
57    * 0xAA = 0b10101010, i.e., the same as the preamble pattern. Not necessary,
58    * but gives control of what the radio attempts to transmit.
59    */
60   SYNC1     = 0xAA;
61   SYNC0     = 0xAA;
62
63   /* Put radio in TX. 
64   RFST      = RFST_STX;
65   while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
66   */
67
68   
69 #define RFON RFST = RFST_SIDLE; RFST = RFST_STX; while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
70 #define RFOFF RFST = RFST_SIDLE; //while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_IDLE);
71   //RFON;
72   //while(1);  
73   
74   
75 }
76
77
78 void sleepMillis(int ms) {
79   int j,k;
80   //k=1000;
81   //while(--k>0)
82   while (--ms > 0) { 
83     for (j=0; j<1200;j++); // about 1 millisecond
84   };
85 }
86
87 //! Reflexively jam on the present channel by responding to a signal with a carrier wave.
88 void main(){
89   unsigned char threshold=packet[0], i=0, rssi=0;;
90   //Disable interrupts.
91   RFTXRXIE=0;
92
93     
94   carrier();
95   sleepMillis(10);
96   //RFON;
97   
98   while(1){
99     
100     
101     //RFOFF;
102     
103
104     //Wait for the transmission.
105     RFST=RFST_SRX;
106     while(MARCSTATE!=MARC_STATE_RX);
107     rssi=0;
108     
109     
110     //Wait for RSSI to settle.
111     sleepMillis(100);
112     //RFOFF;
113     
114     
115     //Delay until the RSSI is above the threshold.
116     while(rssi<threshold){
117       sleepMillis(1);
118       rssi=RSSI^0x80;
119       packet[0]=rssi;
120       HALT;
121     }
122     
123     RFON;
124     HALT;
125     while(1);
126     
127     
128     HALT;
129     //tx carrier for 10 ms
130     
131     RFON;
132     while(1);
133     RFOFF;
134     sleepMillis(1000);
135     
136     
137     while(1);
138     //Carrier will clear when the loop continue,
139     //but we can HALT to give the host a chance to take over.
140     //HALT;
141   }  
142   //RFST = RFST_SIDLE; //End transmit.
143   
144   HALT;
145 }