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