source: ResearchApps/MAC/NOMAC/noMac.c

Last change on this file was 1817, checked in by murphpo, 10 years ago
File size: 8.1 KB
RevLine 
[954]1/*! \file noMac.c
[988]2 \brief No Medium Access Control Workshop MAC.
[1453]3
[1816]4 @version 18.0
[1578]5 @author Chris Hunter & Patrick Murphy
[1453]6
[988]7 The easiest MAC. Forwards all traffic from
[1712]8 Ethernet to the radio medium and vice versa.
[954]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.
[1453]13
[989]14 Note to workshop users: any names in quotes
15 are names that can be searched for in the
16 WARP API.
[1453]17
[988]18*/
[770]19
[954]20
[1329]21#include "xparameters.h"
[770]22#include "warpmac.h"
23#include "warpphy.h"
[1109]24#include "noMac.h"
[1712]25#include "stdio.h"
[1817]26#include "warp_hw_ver.h"
[770]27
[1634]28///Macframe struct to represent received wireless packets
[1578]29Macframe rxFrame;
[770]30
[1634]31///Indices of Rx/Tx packet buffers in the PHY
[1630]32unsigned char pktBufInd_rx;
33unsigned char pktBufInd_tx;
[1396]34
[1453]35///@brief Callback for the reception of data frames from the higher network layer
[954]36///
[1634]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().
[954]42///@param length Length, in bytes, of received Ethernet frame
[1634]43///@param payload Memory address of first byte in the payload
[1329]44void dataFromNetworkLayer_callback(Xuint32 length, char* payload)
45{
[988]46
[1453]47//WORKSHOP PSEUDOCODE:
[1634]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.
[989]56
[1120]57//Note: You may be wondering why the act of transmitting is broken up into three function calls (5,6,7 of pseudocode above).
[1109]58//In this simple exercise, it seems a bit unnecessary. However, the reason for this division will become clear in later labs.
[989]59
[1634]60/**********************USER CODE STARTS HERE***************************/
[988]61    //Buffer for holding a packet-to-xmit
[1578]62    Macframe txFrame;
[1630]63
[954]64    //Set the length field in the header
[1578]65    txFrame.header.length = length;
[1630]66
[954]67    //Set the modulation scheme for the packet's full-rate symbols
[1634]68    txFrame.header.fullRate = HDR_FULLRATE_QPSK;
[1660]69    //txFrame.header.fullRate = HDR_FULLRATE_QAM_16;
[1630]70
[1120]71    //Set the payload coding rate
[1660]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;
[1630]76
[954]77    //Copy the header over to packet buffer 1
[1630]78    warpmac_prepPhyForXmit(&txFrame, pktBufInd_tx);
79
[1578]80    //Send packet buffer pktBuf_tx
[1630]81    warpmac_startPhyXmit(pktBufInd_tx);
82
[954]83    //Wait for it to finish and enable the receiver
84    warpmac_finishPhyXmit();
[1634]85/**********************USER CODE ENDS HERE***************************/
[1630]86    return;
[770]87}
88
[954]89///@brief Callback for the reception of bad wireless headers
[1329]90void phyRx_badHeader_callback()
91{
[1630]92    //Blink the bottom user LEDs on reception of a bad header
[770]93    warpmac_incrementLEDLow();
[1630]94   
95    return;
[770]96}
97
[954]98///@brief Callback for the reception of good wireless headers
99///
[1634]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
[954]104///@param packet Pointer to received Macframe
[1578]105int phyRx_goodHeader_callback(Macframe* packet)
[1329]106{
[989]107
[1578]108    //Initialize the Rx pkt state variable
[1453]109    unsigned char state = PHYRXSTATUS_INCOMPLETE;
110
[1578]111    //Poll the PHY; blocks until the PHY declares the payload good or bad
[1329]112    state = warpmac_finishPhyRecv();
[1453]113
[1329]114    if(state & PHYRXSTATUS_GOOD)
115    {
[1578]116        //If the received packet has no errors, send it (minus the wireless header) via Ethernet
117       
[954]118        //Starts the DMA transfer of the payload into the EMAC
[1630]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));
[1329]121
[954]122        //Waits until the DMA transfer is complete, then starts the EMAC
[1396]123        warpmac_startPktToNetwork(packet->header.length);
[1578]124
125        //Toggle the top user LEDs
126        warpmac_incrementLEDHigh();
[954]127    }
[1453]128
[1329]129    if(state & PHYRXSTATUS_BAD)
130    {
[1578]131        //If the received packet has errors, drop it (i.e. don't send it via Ethernet)
132
[1329]133        //Toggle the bottom user LEDs
[954]134        warpmac_incrementLEDLow();
135    }
[1578]136
137    //Return 0, indicating this function did not clear the PHY status bits; WARPMAC will handle this
138    return 0;
[770]139}
140
[1617]141void rightButton() {
[1634]142    //The WARPMAC framework will call this fuction when the RIGHT button is pushed
143    return;
[1617]144}
145
146void upButton() {
[1634]147    //The WARPMAC framework will call this fuction when the UP button is pushed
148    return;
[1617]149}
150
[954]151///@brief Main function
152///
[1634]153///This function configures & initializes WARPMAC, assigns user-level callbacks then loops forever.
[1329]154int main()
[1453]155{
[1817]156    print("\fReference Design v18 NOMAC\r\n");
[1329]157
[1578]158    //Assign Tx/Rx to packet buffers in the PHY
[1630]159    pktBufInd_rx = 1;
160    pktBufInd_tx = 2;
[1453]161
[770]162    //Initialize the framework
[1329]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
[770]166    warpmac_init();
[1453]167
[1578]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);
[1330]172
[1630]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);
[1453]176
[1630]177    //EMACRxBuffer is where incoming Ethernet packets will be written
178    warpmac_setEMACRxBuffer(pktBufInd_tx);
[1329]179
[1630]180    //PHYTxBuffer is the buffer from which the PHY will read packets for wireless transmission
181    warpmac_setPHYTxBuffer(pktBufInd_tx);
182
[1816]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])
[1634]195    warpphy_setEnergyDetThresh(7000);       //Min RSSI (in [0,16368])
[1816]196    warpphy_setAutoCorrDetParams(90, 20);   //Min auto-correlation (UFix8_7) and min energy (UFix16_8)
[1636]197    warpphy_setLongCorrThresh(8000);        //Min cross-correlation (in [0,45e3])
[1816]198
199    //Set the default Tx gain (in [0,63])
200    warpphy_setTxPower(55);
201#endif
202
[1817]203    warpphy_setChannel(GHZ_2, 11);
204
[1634]205    //Assign the user-level callbacks
[1396]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);
[1617]209    warpmac_setCallback(EVENT_RIGHTBUTTON, (void *)rightButton);
210    warpmac_setCallback(EVENT_UPBUTTON, (void *)upButton);
[1630]211   
[1110]212
[1634]213    //Enable calls to our dataFromnetworkLayer callback (to start receiving Ethernet payloads)
[1329]214    warpmac_enableDataFromNetwork();
[1630]215
216    //Enable dummy packet mode (Ethernet will be ignored, packets will be generated/transmitted locally)
[1816]217    warpmac_setDummyPacketMode(0);//warpmac_getMyId());
[1712]218    warpmac_startPacketGeneration(1400, 500000);
[1630]219
220    //Poll the timer, PHY and user I/O forever; actual processing will happen via callbacks above
[1329]221    while(1)
222    {
223        warpmac_pollPeripherals();
[770]224    }
[1453]225
[1712]226    return 0;
[770]227}
Note: See TracBrowser for help on using the repository browser.