source: ResearchApps/MAC/Debug_PseudoMACs/OFDM_Reflector/ofdm_reflector.c

Last change on this file was 2082, checked in by murphpo, 11 years ago

Updated OFDM reflector with channel changing via push button

File size: 7.9 KB
Line 
1
2#include "xparameters.h"
3#include "warpmac.h"
4#include "warpphy.h"
5#include "ofdm_reflector.h"
6#include "stdio.h"
7#include "warp_hw_ver.h"
8
9///Macframe struct to represent received wireless packets
10Macframe rxFrame;
11
12///Indices of Rx/Tx packet buffers in the PHY
13unsigned char pktBufInd_rx;
14unsigned char pktBufInd_tx;
15
16unsigned short myID;
17unsigned short seqNum;
18
19unsigned char cur_chan;
20unsigned char cur_ant;
21
22///@brief Callback for the reception of data frames from the higher network layer
23///
24///This function is called by the WARPMAC framework when a new payload is available
25/// for wireless transmission. New payloads can either arrive via Ethernet (for
26/// externally generated traffic) or via dummy packet mode (for internally generated
27/// traffic). Before calling this function, WARPMAC has already copied the payload
28/// to the PHY packet buffer previously specified by warpmac_setPHYTxBuffer().
29///@param length Length, in bytes, of received Ethernet frame
30///@param payload Memory address of first byte in the payload
31void dataFromNetworkLayer_callback(Xuint32 length, char* payload)
32{
33
34//WORKSHOP PSEUDOCODE:
35//1) Instantiate a "Macframe" struct to hold a packet header
36//2) Setup the wireless packet header by setting fields in the Macframe.header struct:
37//     2.1) length (set to length argument passed to this function)
38//     2.2) fullRate (set to HDR_FULLRATE_QPSK or HDR_FULLRATE_QAM_16 for QPSK/16QAM payload modulation)
39//     2.3) codeRate (set to HDR_CODE_RATE_12 or HDR_CODE_RATE_34 for 1/2 or 3/4 rate payload coding)
40//5) Copy the header over the the PHY Tx buffer using the "warpmac_prepPhyForXmit" function.
41//6) Initiate the PHY transmission using the "warpmac_startPhyXmit" function.
42//7) Wait for the PHY transmitter to finish and re-enable the receiver by using the "warpmac_finishPhyXmit" function.
43
44//Note: You may be wondering why the act of transmitting is broken up into three function calls (5,6,7 of pseudocode above).
45//In this simple exercise, it seems a bit unnecessary. However, the reason for this division will become clear in later labs.
46
47/**********************USER CODE STARTS HERE***************************/
48    //Buffer for holding a packet-to-xmit
49    Macframe txFrame;
50    seqNum++;
51
52    //Set the length field in the header
53    txFrame.header.length = length;
54    txFrame.header.seqNum = seqNum;
55
56    warpmac_leftHex(seqNum);
57
58    //Set the modulation scheme for the packet's full-rate symbols
59    txFrame.header.fullRate = HDR_FULLRATE_QPSK;
60    //txFrame.header.fullRate = HDR_FULLRATE_QAM_16;
61
62    //Set the payload coding rate
63    //txFrame.header.codeRate = HDR_CODE_RATE_NONE;
64    //txFrame.header.codeRate = HDR_CODE_RATE_12;
65    //txFrame.header.codeRate = HDR_CODE_RATE_23;
66    txFrame.header.codeRate = HDR_CODE_RATE_34;
67
68    //Copy the header over to packet buffer 1
69    warpmac_prepPhyForXmit(&txFrame, pktBufInd_tx);
70
71    //Send packet buffer pktBuf_tx
72    warpmac_startPhyXmit(pktBufInd_tx);
73
74    //Wait for it to finish and enable the receiver
75    warpmac_finishPhyXmit();
76/**********************USER CODE ENDS HERE***************************/
77    return;
78}
79
80///@brief Callback for the reception of bad wireless headers
81void phyRx_badHeader_callback()
82{
83    //Blink the bottom user LEDs on reception of a bad header
84    warpmac_incrementLEDLow();
85   
86    return;
87}
88
89///@brief Callback for the reception of good wireless headers
90///
91///The WARPMAC framework calls this function after the PHY receives an error-free wireless header
92/// If the packet also has a payload, this function must poll the PHY until the payload status (good/bad)
93/// is determined.
94///If the payload is error-free, it is transmitted over Ethernet, completing the wired-wireless-wired bridge
95///@param packet Pointer to received Macframe
96int phyRx_goodHeader_callback(Macframe* packet)
97{
98
99    //Initialize the Rx pkt state variable
100    unsigned char state = PHYRXSTATUS_INCOMPLETE;
101
102    //Poll the PHY; blocks until the PHY declares the payload good or bad
103    state = warpmac_finishPhyRecv();
104
105    if(state & PHYRXSTATUS_GOOD)
106    {
107        //If the received packet has no errors, send it (minus the wireless header) via Ethernet
108
109        warpmac_rightHex(packet->header.seqNum);
110
111        if(myID == 0) {
112            //Re-transmit the received packet with no changes
113            warpmac_startPhyXmit(pktBufInd_rx);
114        }
115
116        //Toggle the top user LEDs
117        warpmac_incrementLEDHigh();
118    }
119
120    if(state & PHYRXSTATUS_BAD)
121    {
122        //If the received packet has errors, drop it (i.e. don't send it via Ethernet)
123
124        //Toggle the bottom user LEDs
125        warpmac_incrementLEDLow();
126    }
127
128    //Return 0, indicating this function did not clear the PHY status bits; WARPMAC will handle this
129    return 0;
130}
131
132void middleButton() {
133    if(cur_chan >= 14) {
134        cur_chan = 1;
135    } else {
136        cur_chan++;
137    }
138    xil_printf("Set Channel: %d\n", cur_chan);
139
140    userio_write_leds_green(USERIO_BASEADDR, cur_chan & 0xF);
141    warpphy_setChannel(GHZ_2, cur_chan);
142
143    return;
144}
145
146void upButton() {
147    if(cur_ant == 0) {
148        cur_ant = 1;
149        warpphy_setAntennaMode(TX_ANTMODE_SISO_ANTB, RX_ANTMODE_SISO_ANTB);
150    } else {
151        cur_ant = 0;
152        warpphy_setAntennaMode(TX_ANTMODE_SISO_ANTA, RX_ANTMODE_SISO_ANTA);
153    }
154
155    return;
156}
157
158///@brief Main function
159///
160///This function configures & initializes WARPMAC, assigns user-level callbacks then loops forever.
161int main()
162{
163    print("\fReference Design v18 OFDM Reflector FMC Radios\r\n");
164
165    cur_chan = 1;
166    cur_ant = 0;
167
168    userio_write_leds_green(USERIO_BASEADDR, cur_chan & 0xF);
169
170    //Assign Tx/Rx to packet buffers in the PHY
171    pktBufInd_rx = 1;
172    pktBufInd_tx = 2;
173
174    //Initialize the framework
175    // This function sets safe defaults for many parameters in the MAC/PHY frameworks
176    // Many of these can be changed with other warpmac_ and warpphy_ calls
177    //  or by customizing the warpmac.c/warpphy.c source
178    warpmac_init();
179
180    //Choose the antnenna mode
181    warpphy_setAntennaMode(TX_ANTMODE_SISO_ANTA, RX_ANTMODE_SISO_ANTA);
182
183    //Rx buffer is where the PHY will write received payloads,
184    // and the EMAC will read packets for transmission via Ethernet
185    warpmac_setRxBuffers(&rxFrame, pktBufInd_rx);
186
187    //EMACRxBuffer is where incoming Ethernet packets will be written
188    warpmac_setEMACRxBuffer(pktBufInd_tx);
189
190    //PHYTxBuffer is the buffer from which the PHY will read packets for wireless transmission
191    warpmac_setPHYTxBuffer(pktBufInd_tx);
192
193#ifdef WARP_HW_VER_v3
194    //Set the OFDM Rx detection thresholds
195    warpphy_setCarrierSenseThresh(4000); //Carrier sense thresh (in [0,16368])
196    warpphy_setEnergyDetThresh(6500);       //Min RSSI (in [0,16368])
197    warpphy_setAutoCorrDetParams(50, 20);   //Min auto-correlation (UFix8_7) and min energy (UFix16_8)
198    warpphy_setLongCorrThresh(10000);       //Min cross-correlation (in [0,45e3])
199
200    //Set the default Tx gain (in [0,63])
201    warpphy_setTxPower(45);
202#else
203    //Set the OFDM Rx detection thresholds (copied from OFDM ref des v17 for now)
204    warpphy_setCarrierSenseThresh(12000); //Carrier sense thresh (in [0,16368])
205    warpphy_setEnergyDetThresh(7000);       //Min RSSI (in [0,16368])
206    warpphy_setAutoCorrDetParams(90, 20);   //Min auto-correlation (UFix8_7) and min energy (UFix16_8)
207    warpphy_setLongCorrThresh(8000);        //Min cross-correlation (in [0,45e3])
208
209    //Set the default Tx gain (in [0,63])
210    warpphy_setTxPower(55);
211#endif
212
213    warpphy_setChannel(GHZ_2, cur_chan);
214
215    //Assign the user-level callbacks
216    warpmac_setCallback(EVENT_DATAFROMNETWORK, (void *)dataFromNetworkLayer_callback);
217    warpmac_setCallback(EVENT_PHYGOODHEADER, (void *)phyRx_goodHeader_callback);
218    warpmac_setCallback(EVENT_PHYBADHEADER, (void *)phyRx_badHeader_callback);
219    warpmac_setCallback(EVENT_MIDDLEBUTTON, (void *)middleButton);
220    warpmac_setCallback(EVENT_UPBUTTON, (void *)upButton);
221   
222
223    myID = (unsigned short int)warpmac_getMyId();
224    seqNum = 0;
225
226    //Enable calls to our dataFromnetworkLayer callback (to start receiving Ethernet payloads)
227    warpmac_enableDataFromNetwork();
228
229    //Enable dummy packet mode (Ethernet will be ignored, packets will be generated/transmitted locally)
230    warpmac_setDummyPacketMode(myID);
231    warpmac_startPacketGeneration(1400, 750000);
232
233    //Poll the timer, PHY and user I/O forever; actual processing will happen via callbacks above
234    while(1)
235    {
236        warpmac_pollPeripherals();
237    }
238
239    return 0;
240}
Note: See TracBrowser for help on using the repository browser.