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