c7896226758a82b60f845168a95fe87c2e5a633a
[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, unecessary
95     //RFST=RFST_SFSTXON;
96     //while(MARCSTATE!=MARC_STATE_FSTXON);
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(200);
119     //sleepMillis(100);
120     //sleepMillis(50);
121     sleepMillis(25);
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     /*
148     if(PKTCTRL0&1){
149       //auto length
150       while(i<len+3){ //len+3 if status is appended.
151         while(!RFTXRXIF); //Wait for byte to be ready.
152         RFTXRXIF=0;      //Clear the flag.
153         
154         packet[i++]=RFD; //Grab the next byte.
155         len=packet[0];   //First byte of the packet is the length.
156       }
157     }else{
158     */
159       //Fixed length
160       packet[i++]=PKTLEN;
161       while(i<3){ //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