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