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