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