1081c8d21943eb3a17134421c9cebed32f925258
[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   CHANNR    = 0x00;   // Channel number.
30   DEVIATN   = 0x00;   // Modem deviation setting (when FSK modulation is enabled).
31   FREND1    = 0x56;   // Front end RX configuration.
32   FREND0    = 0x10;   // Front end RX configuration.
33   MCSM0     = 0x14;   // Main Radio Control State Machine configuration.
34   FOCCFG    = 0x16;   // Frequency Offset Compensation Configuration.
35   BSCFG     = 0x6C;   // Bit synchronization Configuration.
36   AGCCTRL2  = 0x03;   // AGC control.
37   AGCCTRL1  = 0x40;   // AGC control.
38   AGCCTRL0  = 0x91;   // AGC control.
39   FSCAL3    = 0xE9;   // Frequency synthesizer calibration.
40   FSCAL2    = 0x2a;   // Frequency synthesizer calibration.
41   FSCAL1    = 0x00;   // Frequency synthesizer calibration.
42   FSCAL0    = 0x1f;   // Frequency synthesizer calibration
43   
44   TEST2     = 0x88;   // Various test settings.
45   TEST1     = 0x31;   // Various test settings.
46   TEST0     = 0x09;   // Various test settings.
47   
48   //FE is too high
49   PA_TABLE0 = 0xFF;   // PA output power setting.
50   PKTCTRL1  = 0x04;   // Packet automation control.
51   PKTCTRL0  = 0x22;   // Packet automation control.
52   ADDR      = 0x00;   // Device address.
53   PKTLEN    = 0xFF;   // Packet length.
54
55   /* Settings not from SmartRF® Studio. Setting both sync word registers to
56    * 0xAA = 0b10101010, i.e., the same as the preamble pattern. Not necessary,
57    * but gives control of what the radio attempts to transmit.
58    */
59   SYNC1     = 0xAA;
60   SYNC0     = 0xAA;
61
62   /* Put radio in TX. 
63   RFST      = RFST_STX;
64   while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
65   */
66
67   
68 #define RFON RFST = RFST_SIDLE; RFST = RFST_STX; while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
69 #define RFOFF RFST = RFST_SIDLE; //while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_IDLE);
70   //RFON;
71   //while(1);  
72   
73   
74 }
75
76
77 void sleepMillis(int ms) {
78         int j;
79         while (--ms > 0) { 
80                 for (j=0; j<1200;j++); // about 1 millisecond
81         };
82 }
83
84 //! Wait for a packet to come, then immediately return.
85 void rxwait(){
86   int i=0;
87
88   //Disable interrupts.
89   RFTXRXIE=0;
90   
91   //idle a bit.
92   RFST=RFST_SIDLE;
93   while(MARCSTATE!=MARC_STATE_IDLE);
94   
95   sleepMillis(10);
96   //Begin to receive.
97   RFST=RFST_SRX;
98   while(MARCSTATE!=MARC_STATE_RX);
99   //while(MARCSTATE!=MARC_STATE_RX_OVERFLOW);
100   
101   
102   
103   //Incoming!
104   /*
105   //Fixed length
106   packet[i++]=PKTLEN;
107   while(i<PKTLEN){
108     while(!RFTXRXIF); //Wait for byte to be ready.
109     RFTXRXIF=0;      //Clear the flag.
110     
111     packet[i++]=RFD; //Grab the next byte.
112   }
113   */
114   //sleepMillis(10);
115   //RFST = RFST_SIDLE; //End receive.  
116 }
117
118 //! Reflexively jam on the present channel by responding to a signal with a carrier wave.
119 void main(){
120   unsigned char threshold=packet[0], i=0, rssi=0;;
121   
122   PKTCTRL0=0x02;
123   //Disable interrupts.
124   RFTXRXIE=0;
125   
126   //carrier();
127   
128   //idle a bit.
129   //RFST=RFST_SIDLE;
130   //while(MARCSTATE!=MARC_STATE_IDLE);
131
132   while(1){
133     
134     rxwait();
135     
136     //idle a bit.
137     RFST=RFST_SIDLE;
138     while(MARCSTATE!=MARC_STATE_IDLE);
139     //HALT;
140     
141     RFOFF;
142     
143     //SYNC1=0xAA;
144     //SYNC0=0xAA;
145     
146     //Transmit carrier for 10ms
147     /*
148       RFST=RFST_STX;
149       while(MARCSTATE!=MARC_STATE_TX);
150       sleepMillis(20);
151     */
152     
153     //RFON;
154     sleepMillis(200);
155     
156     //Carrier will clear when the loop continue,
157     //but we can HALT to give the host a chance to take over.
158     HALT;
159   }  
160   RFST = RFST_SIDLE; //End transmit.
161   
162   HALT;
163 }