b4f6765a46af98bafabdd73b5f6e282c6062e624
[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   
44   MDMCFG4   = 0x86;   // Modem configuration.
45   MDMCFG3   = 0x83;   // Modem configuration.
46   MDMCFG2   = 0x30;   // Modem configuration.
47   MDMCFG1   = 0x22;   // Modem configuration.
48   MDMCFG0   = 0xF8;   // Modem configuration.
49   
50   /* Settings not from SmartRF® Studio. Setting both sync word registers to
51    * 0xAA = 0b10101010, i.e., the same as the preamble pattern. Not necessary,
52    * but gives control of what the radio attempts to transmit.
53    */
54   
55   //These sync values are better for jamming, but they break reception.
56   //SYNC1     = 0xAA;
57   //SYNC0     = 0xAA;
58   
59 #define RFON RFST = RFST_SIDLE; RFST = RFST_STX; while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
60 #define RFOFF RFST = RFST_SIDLE; //while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_IDLE);
61   //RFON;
62   //while(1);  
63   
64   
65 }
66
67
68 void sleepMillis(int ms) {
69         int j;
70         while (--ms > 0) { 
71                 for (j=0; j<1200;j++); // about 1 millisecond
72         };
73 }
74
75
76 //! Reflexively jam on the present channel by responding to a signal with a carrier wave.
77 void main(){
78   unsigned char threshold=packet[0], i=0, rssi=0;;
79   
80   //Disable interrupts.
81   RFTXRXIE=0;
82   
83   save_settings();
84   
85   //carrier();
86   
87   //idle a bit.
88   RFST=RFST_SIDLE;
89   while(MARCSTATE!=MARC_STATE_IDLE);
90
91   while(1){
92     restore_settings();
93     //idle a bit.
94     RFST=RFST_SFSTXON;
95     while(MARCSTATE!=MARC_STATE_FSTXON);
96     
97     
98     sleepMillis(5);
99     rxwait();
100     
101     //idle w/ oscillator
102     RFST=RFST_SFSTXON;
103     while(MARCSTATE!=MARC_STATE_FSTXON);
104     
105     //HALT;
106     //sleepMillis(500);
107     //HALT;
108     
109     //RFOFF;
110     
111     //SYNC1=0xAA;
112     //SYNC0=0xAA;
113     
114     //Transmit carrier for 10ms
115     carrier();
116     RFON;
117     
118     //sleepMillis(2000);
119     
120     sleepMillis(500);
121     HALT;
122   }
123 }
124
125 //! Receives a packet out of the radio from 0xFE00.
126 void rxwait(){
127   unsigned char len=16, i=0;
128   
129   do{
130     //1-out the buffer.
131     for(i=0;i<64;i++)
132       packet[i]=0xFF;
133     i=0;
134     
135     //Disable interrupts.
136     RFTXRXIE=0;
137     
138     //idle a bit.
139     RFST=RFST_SIDLE;
140     while(MARCSTATE!=MARC_STATE_IDLE);
141     
142     //Begin to receive.
143     RFST=RFST_SRX;
144     while(MARCSTATE!=MARC_STATE_RX);
145     
146     if(PKTCTRL0&1){
147       //auto length
148       while(i<len+3){ //len+3 if status is appended.
149         while(!RFTXRXIF); //Wait for byte to be ready.
150         RFTXRXIF=0;      //Clear the flag.
151         
152         packet[i++]=RFD; //Grab the next byte.
153         len=packet[0];   //First byte of the packet is the length.
154       }
155     }else{
156       //Fixed length
157       packet[i++]=PKTLEN;
158       while(i<PKTLEN){
159         while(!RFTXRXIF); //Wait for byte to be ready.
160         RFTXRXIF=0;      //Clear the flag.
161         
162         packet[i++]=RFD; //Grab the next byte.
163       }
164     }
165     RFST = RFST_SIDLE; //End receive.
166     
167     //This while loop can be used for filtering.  Unused for now.
168   }while(0); //packet[0]==(char) 0x0f || packet[1]==(char) 0xFF || packet[9]==(char) 0x03);
169 }
170
171