source: ReferenceDesigns/w3_802.11/c/wlan_mac_low_nomac/wlan_mac_nomac.c

Last change on this file was 6319, checked in by chunter, 5 years ago

1.8.0 release wlan-mac-se

File size: 13.1 KB
Line 
1/** @file wlan_mac_nomac.c
2 *  @brief Simple MAC that does nothing but transmit and receive
3 *
4 *  @copyright Copyright 2014-2019, Mango Communications. All rights reserved.
5 *          Distributed under the Mango Communications Reference Design License
6 *              See LICENSE.txt included in the design archive or
7 *              at http://mangocomm.com/802.11/license
8 *
9 *  This file is part of the Mango 802.11 Reference Design (https://mangocomm.com/802.11)
10 */
11
12/***************************** Include Files *********************************/
13
14// Xilinx SDK includes
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include "xio.h"
19#include "xil_cache.h"
20
21// WLAN includes
22#include "wlan_platform_common.h"
23#include "wlan_platform_low.h"
24#include "wlan_mac_low.h"
25#include "wlan_mac_pkt_buf_util.h"
26#include "wlan_mac_802_11_defs.h"
27#include "wlan_phy_util.h"
28#include "wlan_platform_low.h"
29#include "wlan_mac_nomac.h"
30#include "xparameters.h"
31#include "wlan_mac_common.h"
32#include "wlan_mac_mailbox_util.h"
33
34// WLAN Exp includes
35#include "wlan_exp.h"
36
37
38/*************************** Constant Definitions ****************************/
39#define WLAN_EXP_TYPE_DESIGN_80211_CPU_LOW WLAN_EXP_LOW_SW_ID_NOMAC
40
41#define DEFAULT_TX_ANTENNA_MODE TX_ANTMODE_SISO_ANTA
42
43
44/*********************** Global Variable Definitions *************************/
45
46
47/*************************** Variable Definitions ****************************/
48static u8 eeprom_addr[MAC_ADDR_LEN];
49
50// Common Platform Device Info
51platform_common_dev_info_t platform_common_dev_info;
52
53
54/*************************** Functions Prototypes ****************************/
55
56void process_low_param(u8 mode, u32* payload);
57
58
59/******************************** Functions **********************************/
60
61int main() {
62    // Call the platform-supplied cpu_init() first to setup any
63    //  processor-specific settings to enable sane execution
64    //  of the platform and framework code below
65    wlan_platform_cpu_low_init();
66
67    wlan_mac_hw_info_t* hw_info;
68
69    xil_printf("\f");
70    xil_printf("----- Mango 802.11 Reference Design -----\n");
71    xil_printf("----- v1.8.0 ----------------------------\n");
72    xil_printf("----- wlan_mac_nomac --------------------\n");
73    xil_printf("Compiled %s %s\n\n", __DATE__, __TIME__);
74
75    xil_printf("Note: this UART is currently printing from CPU_LOW. To view prints from\n");
76    xil_printf("and interact with CPU_HIGH, raise the right-most User I/O DIP switch bit.\n");
77    xil_printf("This switch can be toggled live while the design is running.\n\n");
78    xil_printf("------------------------\n");
79
80    wlan_mac_common_malloc_init();
81
82    // Initialize the Low Framework
83    wlan_mac_low_init(WLAN_EXP_TYPE_DESIGN_80211_CPU_LOW, __DATE__, __TIME__);
84
85    // Get the device info
86    platform_common_dev_info = wlan_platform_common_get_dev_info();
87
88    // Get the node's HW address
89    hw_info = get_mac_hw_info();
90    memcpy(eeprom_addr, hw_info->hw_addr_wlan, MAC_ADDR_LEN);
91
92    // Set up the TX / RX callbacks
93    wlan_mac_low_set_frame_rx_callback((void*)frame_receive);
94    wlan_mac_low_set_ipc_low_param_callback((void*)process_low_param);
95    wlan_mac_low_set_handle_tx_pkt_buf_ready((void*)handle_tx_pkt_buf_ready);
96    // wlan_mac_low_set_sample_rate_change_callback() not used at this time.
97
98    // Finish Low Framework initialization
99    wlan_mac_low_init_finish();
100
101    // Set the MAC HW:
102    //     - Ignore carrier sensing
103    //     - Ignore NAV
104    //
105    REG_SET_BITS(WLAN_MAC_REG_CONTROL, (WLAN_MAC_CTRL_MASK_CCA_IGNORE_PHY_CS | WLAN_MAC_CTRL_MASK_CCA_IGNORE_NAV));
106
107    // Print NOMAC information to the terminal
108    xil_printf("------------------------\n");
109    xil_printf("WLAN MAC NOMAC boot complete: \n");
110    xil_printf("  Serial Number     : W3-a-%05d\n", hw_info->serial_number);
111    xil_printf("  Wireless MAC Addr : %02x:%02x:%02x:%02x:%02x:%02x\n\n", eeprom_addr[0], eeprom_addr[1], eeprom_addr[2], eeprom_addr[3], eeprom_addr[4], eeprom_addr[5]);
112
113    while(1){
114        // Poll PHY RX start
115        wlan_mac_low_poll_frame_rx();
116
117        // Poll IPC rx
118        wlan_mac_low_poll_ipc_rx();
119    }
120
121    return 0;
122}
123
124
125
126/*****************************************************************************/
127/**
128 * @brief Handles reception of a wireless packet
129 *
130 * This function is called after a good SIGNAL field is detected by either PHY (OFDM or DSSS)
131 *
132 * It is the responsibility of this function to wait until a sufficient number of bytes have been received
133 * before it can start to process those bytes. When this function is called the eventual checksum status is
134 * unknown. In NOMAC, this function doesn't need to do any kind of filtering or operations like transmitting
135 * an acknowledgment.  This should be modified to fit the user's needs.
136 *
137 * NOTE: The timing of this function is critical for correct operation of the 802.11. It is not
138 *     safe to add large delays to this function (e.g. xil_printf or wlan_usleep)
139 *
140 * @param   rx_pkt_buf       - Index of the Rx packet buffer containing the newly recevied packet
141 * @param   phy_details      - Pointer to phy_rx_details struct containing PHY mode, MCS, and Length
142 * @return  u32              - Bit mask of flags indicating various results of the reception
143 *
144 * @note    Default NOMAC implementation always returns 0
145 */
146u32 frame_receive(u8 rx_pkt_buf, phy_rx_details_t* phy_details){
147
148    void* pkt_buf_addr = (void*) CALC_PKT_BUF_ADDR(platform_common_dev_info.rx_pkt_buf_baseaddr, rx_pkt_buf);
149    rx_frame_info_t* rx_frame_info = (rx_frame_info_t*) pkt_buf_addr;
150
151    // Wait for the Rx PHY to finish receiving this packet
152    if(wlan_mac_hw_rx_finish() == 1){
153        //FCS was good
154        rx_frame_info->flags |= RX_FRAME_INFO_FLAGS_FCS_GOOD;
155    } else {
156        //FCS was bad
157        rx_frame_info->flags &= ~RX_FRAME_INFO_FLAGS_FCS_GOOD;
158    }
159
160    // Increment the LEDs based on the FCS status
161    if(rx_frame_info->flags & RX_FRAME_INFO_FLAGS_FCS_GOOD){
162        wlan_platform_low_userio_disp_status(USERIO_DISP_STATUS_GOOD_FCS_EVENT);
163    } else {
164        wlan_platform_low_userio_disp_status(USERIO_DISP_STATUS_BAD_FCS_EVENT);
165    }
166
167    rx_frame_info->rx_pkt_buf_state = RX_PKT_BUF_READY;
168    if (unlock_rx_pkt_buf(rx_pkt_buf) != PKT_BUF_MUTEX_SUCCESS) {
169        xil_printf("Error: unable to unlock RX pkt_buf %d\n", rx_pkt_buf);
170        wlan_mac_low_send_exception(WLAN_ERROR_CODE_CPU_LOW_RX_MUTEX);
171    } else {
172        wlan_mac_low_frame_ipc_send();
173
174        // Find a free packet buffer and begin receiving packets there (blocks until free buf is found)
175        wlan_mac_low_lock_empty_rx_pkt_buf();
176    }
177
178
179    return 0;
180}
181
182int handle_tx_pkt_buf_ready(u8 pkt_buf){
183    if( wlan_mac_low_prepare_frame_transmit(pkt_buf) == 0 ){
184        frame_transmit(pkt_buf);
185        wlan_mac_low_finish_frame_transmit(pkt_buf);
186        return WLAN_SUCCESS;
187    } else {
188        return WLAN_FAILURE;
189    }
190}
191
192/*****************************************************************************/
193/**
194 * @brief Handles transmission of a wireless packet
195 *
196 * This function is called to transmit a new packet via the PHY. While the code does utilize the wlan_mac_dcf_hw core,
197 * it bypasses any of the DCF-specific state in order to directly transmit the frame. This function should be called
198 * once per packet and will return immediately following that transmission. It will not perform any DCF-like retransmissions.
199 *
200 * This function is called once per IPC_MBOX_TX_MPDU_READY message from CPU High. The IPC_MBOX_TX_MPDU_DONE message will be
201 * sent back to CPU High when this function returns.
202 *
203 * @param   pkt_buf          - Index of the Tx packet buffer containing the packet to transmit
204 * @return  int              - Transmission result
205 */
206int frame_transmit(u8 pkt_buf) {
207
208    u32 mac_hw_status;
209    u32 mac_tx_ctrl_status;
210    u8 tx_gain;
211    wlan_mac_low_tx_details_t  __attribute__ ((aligned (4))) low_tx_details;
212
213    tx_frame_info_t* tx_frame_info = (tx_frame_info_t*) (CALC_PKT_BUF_ADDR(platform_common_dev_info.tx_pkt_buf_baseaddr, pkt_buf));
214    u8 mpdu_tx_ant_mask = 0;
215
216    // Extract waveform params from the tx_frame_info
217    u8  mcs = tx_frame_info->params.phy.mcs;
218    u8  phy_mode = (tx_frame_info->params.phy.phy_mode & (PHY_MODE_HTMF | PHY_MODE_NONHT));
219    u16 length = tx_frame_info->length;
220
221    // Write the PHY premable (SIGNAL or L-SIG/HT-SIG) to the packet buffer
222    write_phy_preamble(pkt_buf, phy_mode, mcs, length);
223
224    // Set the antenna mode
225    switch(tx_frame_info->params.phy.antenna_mode) {
226        case TX_ANTMODE_SISO_ANTA:  mpdu_tx_ant_mask |= 0x1;  break;
227        case TX_ANTMODE_SISO_ANTB:  mpdu_tx_ant_mask |= 0x2;  break;
228        case TX_ANTMODE_SISO_ANTC:  mpdu_tx_ant_mask |= 0x4;  break;
229        case TX_ANTMODE_SISO_ANTD:  mpdu_tx_ant_mask |= 0x8;  break;
230        default:                    mpdu_tx_ant_mask  = 0x1;  break;      // Default to RF_A
231    }
232
233    // Fill in the number of attempts to transmit the packet
234    tx_frame_info->num_tx_attempts = 1;
235
236    // Update tx_frame_info with current PHY sampling rate
237    tx_frame_info->phy_samp_rate = (u8)wlan_mac_low_get_phy_samp_rate();
238
239    // Convert the requested Tx power (dBm) to a Tx gain setting for the radio
240    tx_gain = wlan_platform_tx_power_to_gain_target(tx_frame_info->params.phy.power);
241
242    // Set the MAC HW control parameters
243    //  args: (pktBuf, antMask, preTx_backoff_slots, preWait_postRxTimer1, preWait_postTxTimer1, postWait_postTxTimer2, phy_mode)
244    wlan_mac_tx_ctrl_A_params(pkt_buf, mpdu_tx_ant_mask, 0, 0, 0, 0, phy_mode);
245
246    // Set Tx Gains - use same gain for all RF interfaces
247    wlan_mac_tx_ctrl_A_gains(tx_gain, tx_gain, tx_gain, tx_gain);
248
249    // Before we mess with any PHY state, we need to make sure it isn't actively
250    //  transmitting. For example, it may be sending an ACK when we get to this part of the code
251    while (wlan_mac_get_status() & WLAN_MAC_STATUS_MASK_TX_PHY_ACTIVE) {}
252
253    // Submit the MPDU for transmission - this starts the MAC hardware's MPDU Tx state machine
254    wlan_mac_tx_ctrl_A_start(1);
255    wlan_mac_tx_ctrl_A_start(0);
256
257    // Fill in the Tx low details
258    low_tx_details.tx_details_type = tx_frame_info->tx_details_type;
259    low_tx_details.phy_params_mpdu.mcs = mcs;
260    low_tx_details.phy_params_mpdu.phy_mode = phy_mode;
261    low_tx_details.phy_params_mpdu.power = tx_frame_info->params.phy.power;
262    low_tx_details.phy_params_mpdu.antenna_mode = tx_frame_info->params.phy.antenna_mode;
263    low_tx_details.chan_num = wlan_mac_low_get_active_channel();
264    low_tx_details.num_slots = 0;
265    low_tx_details.cw = 0;
266    low_tx_details.attempt_number = 1;
267
268    // Wait for the PHY Tx to finish
269    do{
270        // Get the MAC HW status
271        mac_hw_status = wlan_mac_get_status();
272        mac_tx_ctrl_status = wlan_mac_get_tx_ctrl_status();
273
274        // If the MAC HW is done, fill in the remaining Tx low details and return
275        if (mac_tx_ctrl_status & WLAN_MAC_TXCTRL_STATUS_MASK_TX_A_DONE) {
276
277            low_tx_details.tx_start_timestamp_mpdu = wlan_mac_low_get_tx_start_timestamp();
278            low_tx_details.tx_start_timestamp_frac_mpdu = wlan_mac_low_get_tx_start_timestamp_frac();
279
280            // Send IPC message containing the details about this low-level transmission
281            wlan_mac_low_send_low_tx_details(pkt_buf, &low_tx_details);
282
283
284            // Set return value based on Tx A result
285            //  This is easy for NoMAC - all transmissions are immediately successful
286            switch (mac_tx_ctrl_status & WLAN_MAC_TXCTRL_STATUS_MASK_TX_A_RESULT) {
287                case WLAN_MAC_TXCTRL_STATUS_TX_A_RESULT_NONE:
288                default:
289                    return TX_FRAME_INFO_RESULT_SUCCESS;
290                break;
291            }
292        }
293    } while (mac_hw_status & WLAN_MAC_STATUS_MASK_TX_A_PENDING);
294
295
296    // NoMAC Tx is always "successful"
297    return TX_FRAME_INFO_RESULT_SUCCESS;
298}
299
300
301
302/*****************************************************************************/
303/**
304 * @brief Process NOMAC Low Parameters
305 *
306 * This method is part of the IPC_MBOX_LOW_PARAM parameter processing in the low framework.  It
307 * will process NOMAC specific low parameters.
308 *
309 * @param   mode             - Mode to process parameter:  IPC_REG_WRITE_MODE or IPC_REG_READ_MODE
310 * @param   payload          - Pointer to parameter and arguments
311 * @return  none
312 */
313void process_low_param(u8 mode, u32* payload) {
314
315    switch(mode){
316        case IPC_REG_WRITE_MODE:
317            switch(payload[0]){
318
319#if 0
320                //---------------------------------------------------------------------
321                case <Parameter #define in wlan_mac_nomac.h>: {
322                    // Implementation of parameter write
323                }
324                break;
325#endif
326
327                //---------------------------------------------------------------------
328                default: {}
329                break;
330            }
331        break;
332
333        case IPC_REG_READ_MODE: {
334            // Not supported.  See comment in wlan_mac_low.c for IPC_REG_READ_MODE mode.
335        }
336        break;
337
338        default: {
339            xil_printf("Unknown mode 0x%08x\n", mode);
340        }
341        break;
342    }
343
344    return;
345}
Note: See TracBrowser for help on using the repository browser.