[3681] | 1 | /** @file wlan_mac_low.c |
---|
[2720] | 2 | * @brief Low-level WLAN MAC High Framework |
---|
| 3 | * |
---|
| 4 | * This contains the low-level code for accessing the WLAN MAC Low Framework. |
---|
| 5 | * |
---|
[5402] | 6 | * @copyright Copyright 2014-2016, Mango Communications. All rights reserved. |
---|
[2720] | 7 | * Distributed under the Mango Communications Reference Design License |
---|
[4844] | 8 | * See LICENSE.txt included in the design archive or |
---|
| 9 | * at http://mangocomm.com/802.11/license |
---|
[2720] | 10 | * |
---|
| 11 | * @author Chris Hunter (chunter [at] mangocomm.com) |
---|
| 12 | * @author Patrick Murphy (murphpo [at] mangocomm.com) |
---|
| 13 | * @author Erik Welsh (welsh [at] mangocomm.com) |
---|
| 14 | */ |
---|
[4844] | 15 | /***************************** Include Files *********************************/ |
---|
[2720] | 16 | |
---|
[4844] | 17 | // Xilinx / Standard library includes |
---|
| 18 | #include <xparameters.h> |
---|
| 19 | #include <xil_io.h> |
---|
| 20 | #include <xio.h> |
---|
| 21 | #include <stdlib.h> |
---|
[4593] | 22 | #include <string.h> |
---|
| 23 | |
---|
[4844] | 24 | // WARP Includes |
---|
[2720] | 25 | #include "w3_userio.h" |
---|
| 26 | #include "w3_ad_controller.h" |
---|
| 27 | #include "w3_clock_controller.h" |
---|
| 28 | #include "w3_iic_eeprom.h" |
---|
| 29 | #include "radio_controller.h" |
---|
| 30 | |
---|
[4844] | 31 | // WLAN includes |
---|
[5153] | 32 | #include "wlan_mac_time_util.h" |
---|
[5224] | 33 | #include "wlan_mac_mailbox_util.h" |
---|
[2720] | 34 | #include "wlan_mac_802_11_defs.h" |
---|
| 35 | #include "wlan_phy_util.h" |
---|
[4844] | 36 | #include "wlan_mac_low.h" |
---|
[2720] | 37 | |
---|
[4844] | 38 | // WLAN Exp includes |
---|
[2720] | 39 | #include "wlan_exp.h" |
---|
| 40 | |
---|
| 41 | |
---|
[4844] | 42 | /*************************** Constant Definitions ****************************/ |
---|
| 43 | |
---|
[4954] | 44 | #define DBG_PRINT 0 |
---|
[4844] | 45 | |
---|
| 46 | // Power / RSSI conversion |
---|
[4954] | 47 | #define POW_LOOKUP_SHIFT 3 // Shift from 10 bit RSSI to 7 bit for lookup |
---|
[4844] | 48 | |
---|
| 49 | |
---|
| 50 | /*********************** Global Variable Definitions *************************/ |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | /*************************** Functions Prototypes ****************************/ |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | /*************************** Variable Definitions ****************************/ |
---|
[5368] | 57 | volatile static phy_samp_rate_t gl_phy_samp_rate; ///< Current PHY sampling rate |
---|
[5221] | 58 | volatile static u32 mac_param_chan; ///< Current channel of the lower-level MAC |
---|
| 59 | volatile static u8 mac_param_band; ///< Current band of the lower-level MAC |
---|
[5385] | 60 | volatile static u8 mac_param_dsss_en; ///< Enable / Disable DSSS when possible |
---|
[5221] | 61 | volatile static s8 mac_param_ctrl_tx_pow; ///< Current transmit power (dBm) for control packets |
---|
| 62 | volatile static u32 mac_param_rx_filter; ///< Current filter applied to packet receptions |
---|
| 63 | volatile static u8 rx_pkt_buf; ///< Current receive buffer of the lower-level MAC |
---|
[3587] | 64 | |
---|
[5221] | 65 | static u32 cpu_low_status; ///< Status flags that are reported to upper-level MAC |
---|
[5418] | 66 | static u32 cpu_low_type; ///< wlan_exp CPU_LOW type that is reported to upperp-level MAC |
---|
[2720] | 67 | |
---|
[5418] | 68 | static wlan_ipc_msg_t ipc_msg_from_high; ///< Buffer for incoming IPC messages |
---|
| 69 | static u32 ipc_msg_from_high_payload[MAILBOX_BUFFER_MAX_NUM_WORDS]; ///< Buffer for payload of incoming IPC messages |
---|
[4844] | 70 | |
---|
[5221] | 71 | volatile static u8 allow_new_mpdu_tx; ///< Toggle for allowing new MPDU Tx requests to be processed |
---|
| 72 | volatile static s8 pkt_buf_pending_tx; ///< Internal state variable for knowing if an MPDU Tx request is pending |
---|
[4875] | 73 | |
---|
[2730] | 74 | // Callback function pointers |
---|
[4031] | 75 | static function_ptr_t frame_rx_callback; ///< User callback frame receptions |
---|
| 76 | static function_ptr_t frame_tx_callback; ///< User callback frame transmissions |
---|
[2730] | 77 | |
---|
[5220] | 78 | static function_ptr_t beacon_txrx_config_callback; |
---|
[5258] | 79 | static function_ptr_t mactime_change_callback; |
---|
[5368] | 80 | static function_ptr_t sample_rate_change_callback; |
---|
[5139] | 81 | |
---|
[4031] | 82 | static function_ptr_t ipc_low_param_callback; ///< User callback for IPC_MBOX_LOW_PARAM ipc calls |
---|
[3604] | 83 | |
---|
[5101] | 84 | // Unique transmit sequence number |
---|
| 85 | volatile static u64 unique_seq; |
---|
| 86 | |
---|
[4844] | 87 | // NOTE: this statically allocated space should be larger than the maximum number of attempts |
---|
| 88 | // dot11ShortRetryLimit+dot11LongRetryLimit-1 |
---|
[5235] | 89 | static wlan_mac_low_tx_details_t low_tx_details[50]; //TODO make a #define |
---|
[4505] | 90 | |
---|
[4844] | 91 | // Constant LUTs for MCS |
---|
[5268] | 92 | const static u16 mcs_to_n_dbps_nonht_lut[WLAN_MAC_NUM_MCS] = {24, 36, 48, 72, 96, 144, 192, 216}; |
---|
| 93 | const static u16 mcs_to_n_dbps_htmf_lut[WLAN_MAC_NUM_MCS] = {26, 52, 78, 104, 156, 208, 234, 260}; |
---|
[4583] | 94 | |
---|
[4844] | 95 | /******************************** Functions **********************************/ |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | /*****************************************************************************/ |
---|
[2730] | 99 | /** |
---|
| 100 | * @brief Initialize MAC Low Framework |
---|
| 101 | * |
---|
| 102 | * This function initializes the MAC Low Framework by setting |
---|
| 103 | * up the hardware and other subsystems in the framework. |
---|
| 104 | * |
---|
[4844] | 105 | * @param type - Lower-level MAC type |
---|
| 106 | * @return int - Initialization status (0 = success) |
---|
[2730] | 107 | */ |
---|
| 108 | int wlan_mac_low_init(u32 type){ |
---|
[5401] | 109 | u32 status; |
---|
| 110 | rx_frame_info_t* rx_frame_info; |
---|
| 111 | tx_frame_info_t* tx_frame_info; |
---|
| 112 | u32 i; |
---|
[2730] | 113 | |
---|
[5385] | 114 | mac_param_dsss_en = 1; |
---|
[4844] | 115 | mac_param_band = RC_24GHZ; |
---|
| 116 | mac_param_ctrl_tx_pow = 10; |
---|
| 117 | cpu_low_status = 0; |
---|
[5418] | 118 | cpu_low_type = type; |
---|
[2730] | 119 | |
---|
[5101] | 120 | unique_seq = 0; |
---|
| 121 | |
---|
[4844] | 122 | mac_param_rx_filter = (RX_FILTER_FCS_ALL | RX_FILTER_HDR_ALL); |
---|
[3243] | 123 | |
---|
[5221] | 124 | frame_rx_callback = (function_ptr_t) wlan_null_callback; |
---|
| 125 | frame_tx_callback = (function_ptr_t) wlan_null_callback; |
---|
| 126 | ipc_low_param_callback = (function_ptr_t) wlan_null_callback; |
---|
| 127 | beacon_txrx_config_callback = (function_ptr_t) wlan_null_callback; |
---|
[5258] | 128 | mactime_change_callback = (function_ptr_t) wlan_null_callback; |
---|
[5368] | 129 | sample_rate_change_callback = (function_ptr_t) wlan_null_callback; |
---|
[4875] | 130 | allow_new_mpdu_tx = 1; |
---|
| 131 | pkt_buf_pending_tx = -1; // -1 is an invalid pkt_buf index |
---|
| 132 | |
---|
[4844] | 133 | status = w3_node_init(); |
---|
[2730] | 134 | |
---|
[4844] | 135 | if(status != 0) { |
---|
| 136 | xil_printf("Error in w3_node_init()! Exiting\n"); |
---|
| 137 | return -1; |
---|
| 138 | } |
---|
[2730] | 139 | |
---|
[5235] | 140 | // Initialize mailbox |
---|
| 141 | init_mailbox(); |
---|
[2730] | 142 | |
---|
[5235] | 143 | // Initialize packet buffers |
---|
| 144 | init_pkt_buf(); |
---|
| 145 | |
---|
[5391] | 146 | // *************************************************** |
---|
| 147 | // Initialize Transmit Packet Buffers |
---|
| 148 | // *************************************************** |
---|
| 149 | for(i = 0; i < NUM_TX_PKT_BUFS; i++){ |
---|
[5401] | 150 | tx_frame_info = (tx_frame_info_t*)TX_PKT_BUF_TO_ADDR(i); |
---|
[5391] | 151 | switch(i){ |
---|
| 152 | case TX_PKT_BUF_MPDU_1: |
---|
| 153 | case TX_PKT_BUF_MPDU_2: |
---|
| 154 | case TX_PKT_BUF_MPDU_3: |
---|
[5401] | 155 | switch(tx_frame_info->tx_pkt_buf_state){ |
---|
[5400] | 156 | case TX_PKT_BUF_UNINITIALIZED: |
---|
| 157 | case TX_PKT_BUF_HIGH_CTRL: |
---|
[5391] | 158 | // CPU High will initialize |
---|
| 159 | break; |
---|
| 160 | break; |
---|
[5400] | 161 | case TX_PKT_BUF_READY: |
---|
| 162 | case TX_PKT_BUF_DONE: |
---|
[5391] | 163 | // CPU Low rebooted after finishing old Tx |
---|
| 164 | // No way to know if CPU Low sent TX_DONE(p) message - must reset p.state here |
---|
| 165 | // Two potential races: |
---|
[5406] | 166 | // -CPU High just rebooted and will also attempt setting p.state=TX_PKT_BUF_HIGH_CTRL |
---|
| 167 | // No problem if both CPUs set state to TX_PKT_BUF_HIGH_CTRL |
---|
[5391] | 168 | // -CPU High did not reboot and will attempt tx_done_handler(p) |
---|
[5406] | 169 | // If p.state=TX_PKT_BUF_HIGH_CTRL when tx_done_handler(p) runs, CPU High will fail gracefully |
---|
| 170 | // If p.state set to TX_PKT_BUF_HIGH_CTRL during tx_done_handler(p), CPU High will succeed normally |
---|
[5400] | 171 | case TX_PKT_BUF_LOW_CTRL: |
---|
[5391] | 172 | // CPU Low rebooted after CPU High submitted packet for Tx |
---|
| 173 | // Release lock and reset state |
---|
[5406] | 174 | // CPU High will find this TX_PKT_BUF_HIGH_CTRL buffer in next ping/pong update |
---|
[5407] | 175 | default: |
---|
| 176 | // Something went wrong if tx_pkt_buf_state is something |
---|
| 177 | // other than one of the tx_pkt_buf_state_t enums. We'll |
---|
| 178 | // attempt to resolve the problem by explicitly setting |
---|
| 179 | // the state. |
---|
[5401] | 180 | tx_frame_info->tx_pkt_buf_state = TX_PKT_BUF_HIGH_CTRL; |
---|
[5391] | 181 | unlock_tx_pkt_buf(i); |
---|
| 182 | break; |
---|
| 183 | } |
---|
| 184 | break; |
---|
| 185 | case TX_PKT_BUF_BEACON: |
---|
| 186 | unlock_tx_pkt_buf(i); |
---|
| 187 | break; |
---|
| 188 | case TX_PKT_BUF_RTS: |
---|
| 189 | case TX_PKT_BUF_ACK_CTS: |
---|
| 190 | force_lock_tx_pkt_buf(i); |
---|
[5401] | 191 | tx_frame_info->tx_pkt_buf_state = TX_PKT_BUF_LOW_CTRL; |
---|
[5391] | 192 | default: |
---|
| 193 | break; |
---|
| 194 | } |
---|
| 195 | } |
---|
[5400] | 196 | // *************************************************** |
---|
| 197 | // Initialize Receive Packet Buffers |
---|
| 198 | // *************************************************** |
---|
| 199 | for(i = 0; i < NUM_RX_PKT_BUFS; i++){ |
---|
[5401] | 200 | rx_frame_info = (rx_frame_info_t*)RX_PKT_BUF_TO_ADDR(i); |
---|
| 201 | switch(rx_frame_info->rx_pkt_buf_state){ |
---|
[5400] | 202 | case RX_PKT_BUF_UNINITIALIZED: |
---|
| 203 | case RX_PKT_BUF_LOW_CTRL: |
---|
[5407] | 204 | default: |
---|
| 205 | // Something went wrong if rx_pkt_buf_state is something |
---|
| 206 | // other than one of the rx_pkt_buf_state_t enums. We'll |
---|
| 207 | // attempt to resolve the problem by explicitly setting |
---|
| 208 | // the state. |
---|
[5400] | 209 | force_lock_rx_pkt_buf(i); |
---|
[5401] | 210 | rx_frame_info->rx_pkt_buf_state = RX_PKT_BUF_LOW_CTRL; |
---|
[5400] | 211 | break; |
---|
| 212 | case RX_PKT_BUF_HIGH_CTRL: |
---|
| 213 | case RX_PKT_BUF_READY: |
---|
| 214 | // CPU Low rebooted after submitting packet for de-encapsulation/logging |
---|
| 215 | // Will be handled by CPU High, either because CPU High is about |
---|
| 216 | // to de-encapsulate/log p or just rebooted and will clean up |
---|
| 217 | break; |
---|
| 218 | } |
---|
| 219 | } |
---|
[5391] | 220 | |
---|
[4844] | 221 | // Create IPC message to receive into |
---|
| 222 | ipc_msg_from_high.payload_ptr = &(ipc_msg_from_high_payload[0]); |
---|
[2730] | 223 | |
---|
[5400] | 224 | // Point the PHY to an empty Rx Pkt Buffer |
---|
| 225 | wlan_mac_low_lock_empty_rx_pkt_buf(); |
---|
[2730] | 226 | |
---|
[4844] | 227 | // Move the PHY's starting address into the packet buffers by PHY_XX_PKT_BUF_PHY_HDR_OFFSET. |
---|
| 228 | // This accounts for the metadata located at the front of every packet buffer (Xx_mpdu_info) |
---|
| 229 | wlan_phy_rx_pkt_buf_phy_hdr_offset(PHY_RX_PKT_BUF_PHY_HDR_OFFSET); |
---|
| 230 | wlan_phy_tx_pkt_buf_phy_hdr_offset(PHY_TX_PKT_BUF_PHY_HDR_OFFSET); |
---|
[2730] | 231 | |
---|
[5370] | 232 | wlan_mac_reset(1); |
---|
[4844] | 233 | wlan_radio_init(); |
---|
| 234 | wlan_phy_init(); |
---|
[5179] | 235 | wlan_mac_hw_init(); |
---|
[5370] | 236 | wlan_mac_reset(0); |
---|
[2730] | 237 | |
---|
[4844] | 238 | // Initialize the HW info structure |
---|
[5418] | 239 | init_mac_hw_info(); |
---|
[2730] | 240 | |
---|
[5221] | 241 | // Set the NAV ignore addr to this HW address |
---|
| 242 | wlan_mac_low_set_nav_check_addr(get_mac_hw_addr_wlan()); |
---|
| 243 | |
---|
[4844] | 244 | return 0; |
---|
[2730] | 245 | } |
---|
| 246 | |
---|
[4425] | 247 | |
---|
| 248 | |
---|
[4844] | 249 | /*****************************************************************************/ |
---|
| 250 | /** |
---|
| 251 | * @brief Finish Initializing MAC Low Framework |
---|
| 252 | * |
---|
| 253 | * This function finishes the initialization and notifies the upper-level |
---|
| 254 | * MAC that it has finished booting. |
---|
| 255 | * |
---|
| 256 | * @param None |
---|
| 257 | * @return None |
---|
| 258 | */ |
---|
| 259 | void wlan_mac_low_init_finish(){ |
---|
[4425] | 260 | |
---|
[5368] | 261 | //Set the default PHY sample rate to 20 MSps |
---|
| 262 | set_phy_samp_rate(PHY_20M); |
---|
| 263 | |
---|
[4844] | 264 | // Update the CPU Low status |
---|
| 265 | cpu_low_status |= CPU_STATUS_INITIALIZED; |
---|
[4527] | 266 | |
---|
[5421] | 267 | wlan_mac_low_send_status(CPU_STATUS_REASON_BOOTED); |
---|
[4436] | 268 | |
---|
[4844] | 269 | } |
---|
[4425] | 270 | |
---|
[5421] | 271 | void wlan_mac_low_send_status(u8 cpu_status_reason){ |
---|
[5418] | 272 | wlan_ipc_msg_t ipc_msg_to_high; |
---|
| 273 | u32 ipc_msg_to_high_payload[2]; |
---|
[4425] | 274 | |
---|
[5418] | 275 | // Send a message to other processor to say that this processor is initialized and ready |
---|
| 276 | ipc_msg_to_high.msg_id = IPC_MBOX_MSG_ID(IPC_MBOX_CPU_STATUS); |
---|
[5421] | 277 | ipc_msg_to_high.arg0 = cpu_status_reason; |
---|
[5418] | 278 | ipc_msg_to_high.num_payload_words = 2; |
---|
| 279 | ipc_msg_to_high.payload_ptr = &(ipc_msg_to_high_payload[0]); |
---|
| 280 | ipc_msg_to_high_payload[0] = cpu_low_status; |
---|
| 281 | ipc_msg_to_high_payload[1] = cpu_low_type; |
---|
| 282 | |
---|
| 283 | write_mailbox_msg(&ipc_msg_to_high); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | |
---|
[5368] | 287 | /*****************************************************************************/ |
---|
| 288 | /** |
---|
[5369] | 289 | * @brief Set the PHY Sampling Rate |
---|
[5368] | 290 | * |
---|
[5369] | 291 | * This function should be called to switch the PHY sampling rate between |
---|
| 292 | * 10/20/40 MSps. |
---|
[5368] | 293 | * |
---|
[5369] | 294 | * @param phy_samp_rate_t phy_samp_rate |
---|
[5368] | 295 | * @return None |
---|
| 296 | */ |
---|
| 297 | void set_phy_samp_rate(phy_samp_rate_t phy_samp_rate){ |
---|
[5384] | 298 | |
---|
| 299 | // Check sample rate argument |
---|
| 300 | // - Must be in [PHY_10M, PHY_20M, PHY_40M] |
---|
| 301 | if (!((phy_samp_rate == PHY_10M) || (phy_samp_rate == PHY_20M) || (phy_samp_rate == PHY_40M))) { |
---|
| 302 | return; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | // Set global sample rate variable |
---|
[5368] | 306 | gl_phy_samp_rate = phy_samp_rate; |
---|
[4425] | 307 | |
---|
[5370] | 308 | // Assert PHY Tx/Rx and MAC Resets |
---|
| 309 | REG_SET_BITS(WLAN_RX_REG_CTRL, WLAN_RX_REG_CTRL_RESET); |
---|
| 310 | REG_SET_BITS(WLAN_TX_REG_CFG, WLAN_TX_REG_CFG_RESET); |
---|
| 311 | wlan_mac_reset(1); |
---|
| 312 | |
---|
[5368] | 313 | // DSSS Rx only supported at 20Msps |
---|
| 314 | switch(phy_samp_rate){ |
---|
| 315 | case PHY_10M: |
---|
| 316 | case PHY_40M: |
---|
[5384] | 317 | // Always disable DSSS when PHY sample rate is not 20 MSps |
---|
[5370] | 318 | wlan_phy_DSSS_rx_disable(); |
---|
[5368] | 319 | break; |
---|
| 320 | case PHY_20M: |
---|
[5385] | 321 | // Enable DSSS if global variable indicates it should be enabled and RF band allows it |
---|
| 322 | if ((mac_param_dsss_en) && (mac_param_band == RC_24GHZ)) { |
---|
[5384] | 323 | wlan_phy_DSSS_rx_enable(); |
---|
| 324 | } |
---|
[5368] | 325 | break; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | // Configure auto-correlation packet detection |
---|
| 329 | // wlan_phy_rx_pktDet_autoCorr_ofdm_cfg(corr_thresh, energy_thresh, min_dur, post_wait) |
---|
[5384] | 330 | switch(phy_samp_rate){ |
---|
| 331 | case PHY_40M: |
---|
| 332 | //TODO: The 2 value is suspiciously low |
---|
| 333 | wlan_phy_rx_pktDet_autoCorr_ofdm_cfg(200, 2, 15, 0x3F); |
---|
[5368] | 334 | break; |
---|
[5384] | 335 | case PHY_10M: |
---|
| 336 | case PHY_20M: |
---|
| 337 | wlan_phy_rx_pktDet_autoCorr_ofdm_cfg(200, 9, 4, 0x3F); |
---|
| 338 | break; |
---|
| 339 | } |
---|
[5368] | 340 | |
---|
[5384] | 341 | // Set post Rx extension |
---|
| 342 | // Number of sample periods post-Rx the PHY waits before asserting Rx END - must be long enough for worst-case |
---|
| 343 | // decoding latency and should result in RX_END asserting 6 usec after the last sample was received |
---|
| 344 | switch(phy_samp_rate){ |
---|
| 345 | case PHY_40M: |
---|
| 346 | // 6us Extension |
---|
| 347 | wlan_phy_rx_set_extension(6*40); |
---|
| 348 | break; |
---|
| 349 | case PHY_20M: |
---|
| 350 | // 6us Extension |
---|
| 351 | wlan_phy_rx_set_extension(6*20); |
---|
| 352 | break; |
---|
| 353 | case PHY_10M: |
---|
| 354 | // 6us Extension |
---|
| 355 | wlan_phy_rx_set_extension(6*10); |
---|
| 356 | break; |
---|
| 357 | } |
---|
[5368] | 358 | |
---|
[5384] | 359 | // Set Tx duration extension, in units of sample periods |
---|
| 360 | switch(phy_samp_rate){ |
---|
[5368] | 361 | case PHY_40M: |
---|
| 362 | // 364 20MHz sample periods. |
---|
| 363 | // The extra 3 usec properly delays the assertion of TX END to match the assertion of RX END at the receiving node. |
---|
| 364 | wlan_phy_tx_set_extension(364); |
---|
| 365 | |
---|
| 366 | // Set extension from last samp output to RF Tx -> Rx transition |
---|
| 367 | // This delay allows the Tx pipeline to finish driving samples into DACs |
---|
| 368 | // and for DAC->RF frontend to finish output Tx waveform |
---|
| 369 | wlan_phy_tx_set_txen_extension(100); |
---|
| 370 | |
---|
| 371 | // Set extension from RF Rx -> Tx to un-blocking Rx samples |
---|
| 372 | wlan_phy_tx_set_rx_invalid_extension(300); |
---|
| 373 | break; |
---|
| 374 | case PHY_20M: |
---|
| 375 | // 182 20MHz sample periods. |
---|
| 376 | // The extra 3 usec properly delays the assertion of TX END to match the assertion of RX END at the receiving node. |
---|
| 377 | wlan_phy_tx_set_extension(182); |
---|
| 378 | |
---|
| 379 | // Set extension from last samp output to RF Tx -> Rx transition |
---|
| 380 | // This delay allows the Tx pipeline to finish driving samples into DACs |
---|
| 381 | // and for DAC->RF frontend to finish output Tx waveform |
---|
| 382 | wlan_phy_tx_set_txen_extension(50); |
---|
| 383 | |
---|
| 384 | // Set extension from RF Rx -> Tx to un-blocking Rx samples |
---|
| 385 | wlan_phy_tx_set_rx_invalid_extension(150); |
---|
| 386 | break; |
---|
| 387 | case PHY_10M: |
---|
| 388 | wlan_phy_tx_set_extension(91); |
---|
| 389 | |
---|
| 390 | // Set extension from last samp output to RF Tx -> Rx transition |
---|
| 391 | // This delay allows the Tx pipeline to finish driving samples into DACs |
---|
| 392 | // and for DAC->RF frontend to finish output Tx waveform |
---|
| 393 | wlan_phy_tx_set_txen_extension(25); |
---|
| 394 | |
---|
| 395 | // Set extension from RF Rx -> Tx to un-blocking Rx samples |
---|
| 396 | wlan_phy_tx_set_rx_invalid_extension(75); |
---|
| 397 | break; |
---|
[5384] | 398 | } |
---|
[5368] | 399 | |
---|
[5485] | 400 | // Set RF interface clocking and interp/decimation filters |
---|
[5368] | 401 | switch(phy_samp_rate){ |
---|
| 402 | case PHY_40M: |
---|
[5485] | 403 | // Set ADC_CLK=DAC_CLK=40MHz, interp_rate=decim_rate=1 |
---|
[5368] | 404 | clk_config_dividers(CLK_BASEADDR, 2, (CLK_SAMP_OUTSEL_AD_RFA | CLK_SAMP_OUTSEL_AD_RFB)); |
---|
| 405 | ad_config_filters(AD_BASEADDR, AD_ALL_RF, 1, 1); |
---|
| 406 | ad_spi_write(AD_BASEADDR, (AD_ALL_RF), 0x32, 0x2F); |
---|
| 407 | ad_spi_write(AD_BASEADDR, (AD_ALL_RF), 0x33, 0x08); |
---|
| 408 | break; |
---|
| 409 | case PHY_20M: |
---|
[5485] | 410 | // Set ADC_CLK=DAC_CLK=40MHz, interp_rate=decim_rate=2 |
---|
[5368] | 411 | clk_config_dividers(CLK_BASEADDR, 2, (CLK_SAMP_OUTSEL_AD_RFA | CLK_SAMP_OUTSEL_AD_RFB)); |
---|
| 412 | ad_config_filters(AD_BASEADDR, AD_ALL_RF, 2, 2); |
---|
| 413 | ad_spi_write(AD_BASEADDR, (AD_ALL_RF), 0x32, 0x27); |
---|
| 414 | ad_spi_write(AD_BASEADDR, (AD_ALL_RF), 0x33, 0x08); |
---|
| 415 | break; |
---|
| 416 | case PHY_10M: |
---|
[5485] | 417 | // Set ADC_CLK=DAC_CLK=20MHz, interp_rate=decim_rate=2 |
---|
[5368] | 418 | clk_config_dividers(CLK_BASEADDR, 4, (CLK_SAMP_OUTSEL_AD_RFA | CLK_SAMP_OUTSEL_AD_RFB)); |
---|
| 419 | ad_config_filters(AD_BASEADDR, AD_ALL_RF, 2, 2); |
---|
| 420 | ad_spi_write(AD_BASEADDR, (AD_ALL_RF), 0x32, 0x27); |
---|
| 421 | ad_spi_write(AD_BASEADDR, (AD_ALL_RF), 0x33, 0x08); |
---|
| 422 | break; |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | switch(phy_samp_rate){ |
---|
| 426 | case PHY_40M: |
---|
| 427 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_RXLPF_BW, 3); |
---|
| 428 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXLPF_BW, 3); |
---|
| 429 | break; |
---|
| 430 | case PHY_10M: |
---|
| 431 | case PHY_20M: |
---|
| 432 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_RXLPF_BW, 1); |
---|
| 433 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXLPF_BW, 1); |
---|
| 434 | break; |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | // AGC timing: capt_rssi_1, capt_rssi_2, capt_v_db, agc_done |
---|
| 438 | switch(phy_samp_rate){ |
---|
| 439 | case PHY_40M: |
---|
| 440 | wlan_agc_set_AGC_timing(10, 30, 90, 96); |
---|
| 441 | break; |
---|
| 442 | case PHY_10M: |
---|
| 443 | case PHY_20M: |
---|
| 444 | wlan_agc_set_AGC_timing(1, 30, 90, 96); |
---|
| 445 | break; |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | // Call user callback so it can deal with any changes that need to happen due to a change in sampling rate |
---|
| 449 | sample_rate_change_callback(gl_phy_samp_rate); |
---|
[5370] | 450 | |
---|
| 451 | // Deassert PHY Tx/Rx and MAC Resets |
---|
| 452 | REG_CLEAR_BITS(WLAN_RX_REG_CTRL, WLAN_RX_REG_CTRL_RESET); |
---|
| 453 | REG_CLEAR_BITS(WLAN_TX_REG_CFG, WLAN_TX_REG_CFG_RESET); |
---|
| 454 | wlan_mac_reset(0); |
---|
[5385] | 455 | |
---|
| 456 | // Let PHY Tx take control of radio TXEN/RXEN |
---|
| 457 | REG_CLEAR_BITS(WLAN_TX_REG_CFG, WLAN_TX_REG_CFG_SET_RC_RXEN); |
---|
| 458 | REG_SET_BITS(WLAN_TX_REG_CFG, WLAN_TX_REG_CFG_SET_RC_RXEN); |
---|
[5368] | 459 | } |
---|
| 460 | |
---|
| 461 | |
---|
[4844] | 462 | /*****************************************************************************/ |
---|
| 463 | /** |
---|
| 464 | * @brief Initialize the DCF Hardware Core |
---|
| 465 | * |
---|
| 466 | * This function initializes the DCF hardware core. |
---|
| 467 | * |
---|
| 468 | * @param None |
---|
| 469 | * @return None |
---|
| 470 | */ |
---|
[5179] | 471 | void wlan_mac_hw_init(){ |
---|
[4844] | 472 | // Enable blocking of the Rx PHY following good-FCS receptions and bad-FCS receptions |
---|
| 473 | // BLOCK_RX_ON_VALID_RXEND will block the Rx PHY on all RX_END events following valid RX_START events |
---|
| 474 | // This allows the wlan_exp framework to count and log bad FCS receptions |
---|
| 475 | // |
---|
[5253] | 476 | REG_SET_BITS(WLAN_MAC_REG_CONTROL, WLAN_MAC_CTRL_MASK_BLOCK_RX_ON_TX); |
---|
[4425] | 477 | |
---|
[4844] | 478 | // Enable the NAV counter |
---|
| 479 | REG_CLEAR_BITS(WLAN_MAC_REG_CONTROL, (WLAN_MAC_CTRL_MASK_DISABLE_NAV)); |
---|
[4425] | 480 | |
---|
[5370] | 481 | // Set sane defaults for MAC timing values. These will be overwritten by |
---|
| 482 | // low-level applications that need to specify these times (e.g. the DCF) |
---|
| 483 | wlan_mac_set_slot(9*10); |
---|
| 484 | wlan_mac_set_DIFS(28*10); |
---|
| 485 | wlan_mac_set_TxDIFS((28*10) - (TX_PHY_DLY_100NSEC)); |
---|
| 486 | wlan_mac_postTx_timer1_en(0); |
---|
| 487 | wlan_mac_postRx_timer2_en(0); |
---|
| 488 | wlan_mac_set_NAV_adj(0*10); |
---|
| 489 | wlan_mac_set_EIFS(88*10); |
---|
[4425] | 490 | |
---|
[5179] | 491 | // Set the TU target to 2^32-1 (max value) and hold TU_LATCH in reset |
---|
| 492 | // MAC Low application should re-enabled if needed |
---|
| 493 | wlan_mac_set_tu_target(0xFFFFFFFF); |
---|
| 494 | wlan_mac_reset_tu_target_latch(1); |
---|
| 495 | |
---|
[4844] | 496 | // Clear any stale Rx events |
---|
[5253] | 497 | wlan_mac_hw_clear_rx_started(); |
---|
[4844] | 498 | } |
---|
[4425] | 499 | |
---|
| 500 | |
---|
| 501 | |
---|
[4844] | 502 | /*****************************************************************************/ |
---|
[2730] | 503 | /** |
---|
| 504 | * @brief Send Exception to Upper-Level MAC |
---|
| 505 | * |
---|
| 506 | * This function generates an IPC message for the upper-level MAC |
---|
| 507 | * to tell it that something has gone wrong |
---|
| 508 | * |
---|
| 509 | * @param u32 reason |
---|
| 510 | * - reason code for the exception |
---|
| 511 | * @return None |
---|
| 512 | */ |
---|
| 513 | inline void wlan_mac_low_send_exception(u32 reason){ |
---|
[5224] | 514 | wlan_ipc_msg_t ipc_msg_to_high; |
---|
[4844] | 515 | u32 ipc_msg_to_high_payload[2]; |
---|
[2730] | 516 | |
---|
[4844] | 517 | // Update CPU Low status |
---|
| 518 | cpu_low_status |= CPU_STATUS_EXCEPTION; |
---|
[2730] | 519 | |
---|
[4844] | 520 | // Send an exception to CPU_HIGH along with a reason |
---|
| 521 | ipc_msg_to_high.msg_id = IPC_MBOX_MSG_ID(IPC_MBOX_CPU_STATUS); |
---|
[5421] | 522 | ipc_msg_to_high.arg0 = (u8)CPU_STATUS_REASON_EXCEPTION; |
---|
[4844] | 523 | ipc_msg_to_high.num_payload_words = 2; |
---|
| 524 | ipc_msg_to_high.payload_ptr = &(ipc_msg_to_high_payload[0]); |
---|
| 525 | ipc_msg_to_high_payload[0] = cpu_low_status; |
---|
| 526 | ipc_msg_to_high_payload[1] = reason; |
---|
[2730] | 527 | |
---|
[5224] | 528 | write_mailbox_msg(&ipc_msg_to_high); |
---|
[4659] | 529 | |
---|
[4844] | 530 | // Set the Hex display with the reason code and flash the LEDs |
---|
[5221] | 531 | cpu_error_halt(reason); |
---|
[4659] | 532 | } |
---|
| 533 | |
---|
[4844] | 534 | /*****************************************************************************/ |
---|
| 535 | /** |
---|
| 536 | * @brief Poll the Receive Frame Start |
---|
| 537 | * |
---|
| 538 | * This function will poll the hardware to see if the PHY is currently receiving or |
---|
| 539 | * has finished receiving a packet. This will then dispatch the current RX packet |
---|
[5253] | 540 | * buffer and PHY details to the frame_rx_callback(). The callback is responsible for |
---|
| 541 | * updating the current Rx packet buffer, typically required if the received packet |
---|
| 542 | * is passed to CPU High for further processing. |
---|
[4844] | 543 | * |
---|
| 544 | * @param None |
---|
| 545 | * @return u32 - Status (See MAC Polling defines in wlan_mac_low.h) |
---|
| 546 | */ |
---|
| 547 | inline u32 wlan_mac_low_poll_frame_rx(){ |
---|
[5253] | 548 | phy_rx_details_t phy_details; |
---|
[4659] | 549 | |
---|
[5253] | 550 | volatile u32 mac_hw_status; |
---|
| 551 | volatile u32 phy_hdr_params; |
---|
[4659] | 552 | |
---|
[5262] | 553 | int i = 0; |
---|
| 554 | |
---|
[5253] | 555 | u32 return_status = 0; |
---|
| 556 | |
---|
[4844] | 557 | // Read the MAC/PHY status |
---|
| 558 | mac_hw_status = wlan_mac_get_status(); |
---|
[4659] | 559 | |
---|
[5253] | 560 | // Check if PHY has started a new reception |
---|
| 561 | if(mac_hw_status & WLAN_MAC_STATUS_MASK_RX_PHY_STARTED) { |
---|
[4659] | 562 | |
---|
[5253] | 563 | // Check whether this is an OFDM or DSSS Rx |
---|
| 564 | if(wlan_mac_get_rx_phy_sel() == WLAN_MAC_PHY_RX_PHY_HDR_PHY_SEL_DSSS) { |
---|
[4844] | 565 | // DSSS Rx - PHY Rx length is already valid, other params unused for DSSS |
---|
[5241] | 566 | phy_details.phy_mode = PHY_MODE_DSSS; |
---|
[5253] | 567 | phy_details.N_DBPS = 0; |
---|
[4844] | 568 | |
---|
| 569 | // Strip off extra pre-MAC-header bytes used in DSSS frames; this adjustment allows the next |
---|
| 570 | // function to treat OFDM and DSSS payloads the same |
---|
| 571 | phy_details.length = wlan_mac_get_rx_phy_length() - 5; |
---|
[5273] | 572 | phy_details.mcs = 0; |
---|
[4844] | 573 | |
---|
| 574 | // Call the user callback to handle this Rx, capture return value |
---|
[5253] | 575 | return_status |= POLL_MAC_STATUS_RECEIVED_PKT; |
---|
| 576 | return_status |= frame_rx_callback(rx_pkt_buf, &phy_details); |
---|
[4844] | 577 | |
---|
| 578 | } else { |
---|
[5253] | 579 | // OFDM Rx - must wait for valid PHY header |
---|
| 580 | // Order of operations is critical here |
---|
| 581 | // 1) Read status first |
---|
| 582 | // 2) Read PHY header register second |
---|
| 583 | // 3) Check for complete PHY header - continue if complete |
---|
| 584 | // 4) Else check for early PHY reset - quit if reset |
---|
[5262] | 585 | |
---|
| 586 | while (1) { |
---|
[5253] | 587 | mac_hw_status = wlan_mac_get_status(); |
---|
| 588 | phy_hdr_params = wlan_mac_get_rx_phy_hdr_params(); |
---|
[4844] | 589 | |
---|
[5262] | 590 | if(i++ > 1000000) {xil_printf("Stuck in OFDM Rx PHY hdr check: 0x%08x 0x%08x\n", mac_hw_status, phy_hdr_params);} |
---|
| 591 | |
---|
| 592 | if(phy_hdr_params & WLAN_MAC_PHY_RX_PHY_HDR_READY) { |
---|
| 593 | // Rx PHY received enough bytes to decode PHY header |
---|
| 594 | // Exit loop and check PHY header params |
---|
| 595 | break; |
---|
| 596 | } |
---|
| 597 | if((mac_hw_status & WLAN_MAC_STATUS_MASK_RX_PHY_ACTIVE) == 0) { |
---|
| 598 | // Rx PHY went idle before asserting RX_PHY_HDR_READY |
---|
| 599 | // This only happens if the PHY is reset externally, possible if MAC starts a Tx during Rx |
---|
| 600 | // Only option is to reset RX_STARTED and wait for next Rx |
---|
| 601 | |
---|
[5263] | 602 | // There is a 1-cycle race in this case, because RX_END asserts 1 cycle before RX_PHY_HDR_READY in the |
---|
| 603 | // case of an invalid HT-SIG. The invalid HT-SIG generates an RX_END_ERROR which causes |
---|
| 604 | // RX_END to assert. The simple workaround used below is to re-read phy_hdr_params one last time |
---|
| 605 | // before concluding that the Rx PHY was reset unexpectedly |
---|
[5262] | 606 | break; |
---|
| 607 | } |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | // Re-read phy_hdr_params to resolve 1-cycle ambiguity in case of HT-SIG error |
---|
| 611 | phy_hdr_params = wlan_mac_get_rx_phy_hdr_params(); |
---|
| 612 | |
---|
[5253] | 613 | // Decide how to handle this waveform |
---|
[5262] | 614 | if(phy_hdr_params & WLAN_MAC_PHY_RX_PHY_HDR_READY) { |
---|
| 615 | // Received PHY header - decide whether to call MAC callback |
---|
[5383] | 616 | if( (phy_hdr_params & WLAN_MAC_PHY_RX_PHY_HDR_MASK_UNSUPPORTED) || |
---|
| 617 | (wlan_mac_get_rx_phy_mode() > 0x2) ) { |
---|
[5253] | 618 | // Valid HT-SIG but unsupported waveform |
---|
| 619 | // Rx PHY will hold ACTIVE until last samp but will not write payload |
---|
[5262] | 620 | // HT-SIG fields (MCS, length) can be safely read here if desired |
---|
[5383] | 621 | // Or detected VHT waveform (not supported), did not attempt decoding VHT-SIG |
---|
[5262] | 622 | //xil_printf("Quitting - WLAN_MAC_PHY_RX_PHY_HDR_MASK_UNSUPPORTED (MCS = %d, Length = %d)", wlan_mac_get_rx_phy_mcs(), wlan_mac_get_rx_phy_length()); |
---|
[4844] | 623 | |
---|
[5253] | 624 | } else if(phy_hdr_params & WLAN_MAC_PHY_RX_PHY_HDR_MASK_RX_ERROR) { |
---|
| 625 | // Invalid HT-SIG (CRC error, invalid RESERVED or TAIL bits, invalid LENGTH, etc) |
---|
| 626 | // Rx PHY has already released ACTIVE and will not write payload |
---|
[5262] | 627 | // HT-SIG fields (MCS, length) should not be trusted in this case |
---|
[5253] | 628 | //xil_printf("Quitting - WLAN_MAC_PHY_RX_PHY_HDR_MASK_RX_ERROR"); |
---|
[5177] | 629 | |
---|
[5253] | 630 | } else { |
---|
[5262] | 631 | // NONHT waveform or HTMF waveform with supported HT-SIG - PHY will write payload |
---|
| 632 | // Call lower MAC Rx callback |
---|
| 633 | // Callback can safely return anytime (before or after RX_END) |
---|
[4844] | 634 | |
---|
[5253] | 635 | phy_details.phy_mode = wlan_mac_get_rx_phy_mode(); |
---|
| 636 | phy_details.length = wlan_mac_get_rx_phy_length(); |
---|
| 637 | phy_details.mcs = wlan_mac_get_rx_phy_mcs(); |
---|
[5268] | 638 | phy_details.N_DBPS = wlan_mac_low_mcs_to_n_dbps(phy_details.mcs, phy_details.phy_mode); |
---|
[4844] | 639 | |
---|
[5253] | 640 | return_status |= POLL_MAC_STATUS_RECEIVED_PKT; |
---|
| 641 | return_status |= frame_rx_callback(rx_pkt_buf, &phy_details); |
---|
| 642 | } |
---|
[4844] | 643 | } else { |
---|
[5253] | 644 | // PHY went idle before PHY_HDR_DONE, probably due to external reset |
---|
[5263] | 645 | // The Rx PHY can be reset from software (only used in wlan_phy_init()) or hardware. |
---|
| 646 | // The hardware reset is asserted by the MAC core during Tx. Asserting Tx during Rx is |
---|
| 647 | // impossible with the normal DCF code, as packet det is treated as a busy medium. With a |
---|
| 648 | // custom MAC implementation that allows Tx during Rx this code block will catch the |
---|
| 649 | // unexpected reset events. |
---|
[4844] | 650 | |
---|
[5263] | 651 | // PHY header cannot be trusted in this case - do nothing and return |
---|
| 652 | |
---|
[5253] | 653 | }//END if(PHY_HDR_DONE) |
---|
| 654 | } //END if(OFDM Rx) |
---|
[4844] | 655 | |
---|
[5253] | 656 | // Clear the MAC status register RX_STARTED bit |
---|
[5262] | 657 | // By this point the framework and MAC are done with the current waveform, however it was handled. |
---|
| 658 | // The RX_STARTED bit is cleared in every case. It is important to only call clear_rx_started() after |
---|
[5253] | 659 | // the RX_STARTED latch has asserted. Calling it any other time creates a race that can miss Rx events |
---|
| 660 | wlan_mac_hw_clear_rx_started(); |
---|
[4844] | 661 | |
---|
[5253] | 662 | } //END if(PHY_RX_STARTED) |
---|
[4844] | 663 | |
---|
| 664 | return return_status; |
---|
[4659] | 665 | } |
---|
| 666 | |
---|
[4844] | 667 | /*****************************************************************************/ |
---|
[2730] | 668 | /** |
---|
| 669 | * @brief Poll for IPC Receptions |
---|
| 670 | * |
---|
[4844] | 671 | * This function is a non-blocking poll for IPC receptions from the upper-level MAC. |
---|
[2730] | 672 | * |
---|
[4844] | 673 | * @param None |
---|
| 674 | * @return None |
---|
[2730] | 675 | */ |
---|
| 676 | inline void wlan_mac_low_poll_ipc_rx(){ |
---|
[4844] | 677 | // Poll mailbox read msg |
---|
[5224] | 678 | if (read_mailbox_msg(&ipc_msg_from_high) == IPC_MBOX_SUCCESS) { |
---|
[4844] | 679 | wlan_mac_low_process_ipc_msg(&ipc_msg_from_high); |
---|
| 680 | } |
---|
[2730] | 681 | } |
---|
| 682 | |
---|
[4844] | 683 | |
---|
| 684 | |
---|
| 685 | /*****************************************************************************/ |
---|
[2730] | 686 | /** |
---|
| 687 | * @brief Process IPC Reception |
---|
| 688 | * |
---|
| 689 | * This is an internal function to the WLAN MAC Low framework to process |
---|
| 690 | * received IPC messages and call the appropriate callback. |
---|
| 691 | * |
---|
[4844] | 692 | * @param None |
---|
| 693 | * @return None |
---|
[2730] | 694 | */ |
---|
[5224] | 695 | void wlan_mac_low_process_ipc_msg(wlan_ipc_msg_t * msg){ |
---|
| 696 | wlan_ipc_msg_t ipc_msg_to_high; |
---|
[3889] | 697 | |
---|
[4844] | 698 | switch(IPC_MBOX_MSG_ID_TO_MSG(msg->msg_id)){ |
---|
[4740] | 699 | |
---|
[5139] | 700 | //--------------------------------------------------------------------- |
---|
[5258] | 701 | case IPC_MBOX_SET_MAC_TIME: |
---|
| 702 | switch(msg->arg0){ |
---|
| 703 | default: |
---|
| 704 | case 0: |
---|
| 705 | //Payload is an absolute MAC time that must be applied |
---|
| 706 | set_mac_time_usec( *(u64*)(msg->payload_ptr) ); |
---|
| 707 | mactime_change_callback( (*(s64*)(msg->payload_ptr))-((s64)get_mac_time_usec()) ); |
---|
| 708 | break; |
---|
| 709 | case 1: |
---|
| 710 | //Payload is a MAC time delta that must be applied |
---|
| 711 | apply_mac_time_delta_usec( *(s64*)(msg->payload_ptr)); |
---|
| 712 | mactime_change_callback( *(s64*)(msg->payload_ptr)); |
---|
| 713 | break; |
---|
| 714 | } |
---|
| 715 | break; |
---|
| 716 | |
---|
| 717 | //--------------------------------------------------------------------- |
---|
[5220] | 718 | case IPC_MBOX_TXRX_BEACON_CONFIGURE: { |
---|
| 719 | beacon_txrx_config_callback(msg->payload_ptr); |
---|
[5219] | 720 | } |
---|
| 721 | break; |
---|
| 722 | |
---|
[5139] | 723 | //--------------------------------------------------------------------- |
---|
[4959] | 724 | case IPC_MBOX_CPU_STATUS: { |
---|
[5421] | 725 | if(msg->arg0 == (u8)CPU_STATUS_REASON_BOOTED){ |
---|
| 726 | // If CPU_HIGH just booted, we should re-inform it of our CPU status |
---|
| 727 | wlan_mac_low_send_status(CPU_STATUS_REASON_RESPONSE); |
---|
| 728 | } |
---|
[4959] | 729 | } |
---|
[4844] | 730 | break; |
---|
[4816] | 731 | |
---|
[4844] | 732 | //--------------------------------------------------------------------- |
---|
[4959] | 733 | case IPC_MBOX_MEM_READ_WRITE: { |
---|
[4844] | 734 | switch(msg->arg0){ |
---|
[4959] | 735 | case IPC_REG_WRITE_MODE: { |
---|
[5224] | 736 | u32 * payload_to_write = (u32*)((u8*)ipc_msg_from_high_payload + sizeof(ipc_reg_read_write_t)); |
---|
[4816] | 737 | |
---|
[4844] | 738 | // IMPORTANT: this memcpy assumes the payload provided by CPU high is ready as-is |
---|
| 739 | // Any byte swapping (i.e. for payloads that arrive over Ethernet) *must* be performed |
---|
| 740 | // before the payload is passed to this function |
---|
[5224] | 741 | memcpy((u8*)(((ipc_reg_read_write_t*)ipc_msg_from_high_payload)->baseaddr), |
---|
[4844] | 742 | (u8*)payload_to_write, |
---|
[5224] | 743 | (sizeof(u32) * ((ipc_reg_read_write_t*)ipc_msg_from_high_payload)->num_words)); |
---|
[4959] | 744 | } |
---|
[4844] | 745 | break; |
---|
[4816] | 746 | |
---|
[4959] | 747 | case IPC_REG_READ_MODE: { |
---|
[4844] | 748 | /* |
---|
| 749 | xil_printf("\nCPU Low Read:\n"); |
---|
| 750 | xil_printf(" Addr: 0x%08x\n", (u32*)((ipc_reg_read_write*)ipc_msg_from_high_payload)->baseaddr); |
---|
| 751 | xil_printf(" N Wrds: %d\n", ((ipc_reg_read_write*)ipc_msg_from_high_payload)->num_words); |
---|
[4816] | 752 | |
---|
[4844] | 753 | xil_printf("Mem[0x%08x] = 0x%08x\n", |
---|
| 754 | (u32*)((ipc_reg_read_write*)ipc_msg_from_high_payload)->baseaddr, |
---|
| 755 | Xil_In32((u32*)((ipc_reg_read_write*)ipc_msg_from_high_payload)->baseaddr)); |
---|
| 756 | */ |
---|
| 757 | ipc_msg_to_high.msg_id = IPC_MBOX_MSG_ID(IPC_MBOX_MEM_READ_WRITE); |
---|
[5224] | 758 | ipc_msg_to_high.num_payload_words = ((ipc_reg_read_write_t*)ipc_msg_from_high_payload)->num_words; |
---|
| 759 | ipc_msg_to_high.payload_ptr = (u32*)((ipc_reg_read_write_t*)ipc_msg_from_high_payload)->baseaddr; |
---|
[3889] | 760 | |
---|
[5224] | 761 | write_mailbox_msg(&ipc_msg_to_high); |
---|
[4959] | 762 | } |
---|
[4844] | 763 | break; |
---|
| 764 | } |
---|
[4959] | 765 | } |
---|
[4844] | 766 | break; |
---|
[3889] | 767 | |
---|
[4844] | 768 | //--------------------------------------------------------------------- |
---|
[4959] | 769 | case IPC_MBOX_LOW_PARAM: { |
---|
[4844] | 770 | switch(msg->arg0){ |
---|
[4959] | 771 | case IPC_REG_WRITE_MODE: { |
---|
[4844] | 772 | switch(ipc_msg_from_high_payload[0]){ |
---|
[4959] | 773 | case LOW_PARAM_BB_GAIN: { |
---|
[4844] | 774 | if(ipc_msg_from_high_payload[1] <= 3){ |
---|
| 775 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXGAIN_BB, ipc_msg_from_high_payload[1]); |
---|
| 776 | } |
---|
[4959] | 777 | } |
---|
[4844] | 778 | break; |
---|
[3889] | 779 | |
---|
[4959] | 780 | case LOW_PARAM_LINEARITY_PA: { |
---|
[4844] | 781 | if(ipc_msg_from_high_payload[1] <= 3){ |
---|
| 782 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXLINEARITY_PADRIVER, ipc_msg_from_high_payload[1]); |
---|
| 783 | } |
---|
[4959] | 784 | } |
---|
[4844] | 785 | break; |
---|
[3564] | 786 | |
---|
[4959] | 787 | case LOW_PARAM_LINEARITY_VGA: { |
---|
[4844] | 788 | if(ipc_msg_from_high_payload[1] <= 3){ |
---|
| 789 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXLINEARITY_VGA, ipc_msg_from_high_payload[1]); |
---|
| 790 | } |
---|
[4959] | 791 | } |
---|
[4844] | 792 | break; |
---|
[3412] | 793 | |
---|
[4959] | 794 | case LOW_PARAM_LINEARITY_UPCONV: { |
---|
[4844] | 795 | if(ipc_msg_from_high_payload[1] <= 3){ |
---|
| 796 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXLINEARITY_UPCONV, ipc_msg_from_high_payload[1]); |
---|
| 797 | } |
---|
[4959] | 798 | } |
---|
[4844] | 799 | break; |
---|
[3412] | 800 | |
---|
[4959] | 801 | case LOW_PARAM_AD_SCALING: { |
---|
[4844] | 802 | ad_spi_write(AD_BASEADDR, AD_ALL_RF, 0x36, (0x1F & ipc_msg_from_high_payload[1])); |
---|
| 803 | ad_spi_write(AD_BASEADDR, AD_ALL_RF, 0x37, (0x1F & ipc_msg_from_high_payload[2])); |
---|
| 804 | ad_spi_write(AD_BASEADDR, AD_ALL_RF, 0x35, (0x1F & ipc_msg_from_high_payload[3])); |
---|
[4959] | 805 | } |
---|
[4844] | 806 | break; |
---|
[3412] | 807 | |
---|
[4959] | 808 | case LOW_PARAM_PKT_DET_MIN_POWER: { |
---|
[4844] | 809 | if( ipc_msg_from_high_payload[1]&0xFF000000 ){ |
---|
| 810 | wlan_phy_enable_req_both_pkt_det(); |
---|
| 811 | //The value sent from wlan_exp will be unsigned with 0 representing PKT_DET_MIN_POWER_MIN |
---|
| 812 | wlan_mac_low_set_pkt_det_min_power((ipc_msg_from_high_payload[1]&0x000000FF) - PKT_DET_MIN_POWER_MIN); |
---|
[4167] | 813 | |
---|
[4844] | 814 | } else { |
---|
| 815 | wlan_phy_disable_req_both_pkt_det(); |
---|
| 816 | } |
---|
[4959] | 817 | } |
---|
[4844] | 818 | break; |
---|
[4167] | 819 | |
---|
[5366] | 820 | case LOW_PARAM_PHY_SAMPLE_RATE: { |
---|
[5384] | 821 | set_phy_samp_rate(ipc_msg_from_high_payload[1]); |
---|
[5366] | 822 | } |
---|
| 823 | break; |
---|
| 824 | |
---|
[4959] | 825 | default: { |
---|
[4844] | 826 | ipc_low_param_callback(IPC_REG_WRITE_MODE, ipc_msg_from_high_payload); |
---|
[4959] | 827 | } |
---|
[4844] | 828 | break; |
---|
| 829 | } |
---|
[4959] | 830 | } |
---|
[4844] | 831 | break; |
---|
[4167] | 832 | |
---|
[4959] | 833 | case IPC_REG_READ_MODE: { |
---|
[4844] | 834 | // Read Mode is not supported |
---|
| 835 | // |
---|
| 836 | // NOTE: This is due to the fact that IPC messages in CPU low can take an infinitely long amount of |
---|
| 837 | // to return given that the sending and receiving of wireless data takes precedent. Therefore, |
---|
| 838 | // it is not good to try to return values from CPU low since there is no guarantee when the values |
---|
| 839 | // will be available. |
---|
| 840 | // |
---|
[4959] | 841 | u32 ret_val = 0; |
---|
[4167] | 842 | |
---|
[4844] | 843 | ipc_msg_to_high.msg_id = IPC_MBOX_MSG_ID(IPC_MBOX_LOW_PARAM); |
---|
| 844 | ipc_msg_to_high.num_payload_words = 0; |
---|
[4959] | 845 | ipc_msg_to_high.payload_ptr = (u32 *)&ret_val; |
---|
[4167] | 846 | |
---|
[5224] | 847 | write_mailbox_msg(&ipc_msg_to_high); |
---|
[4959] | 848 | } |
---|
[4844] | 849 | break; |
---|
| 850 | } |
---|
[4959] | 851 | } |
---|
[4844] | 852 | break; |
---|
[4169] | 853 | |
---|
[4844] | 854 | //--------------------------------------------------------------------- |
---|
[4959] | 855 | case IPC_MBOX_CONFIG_CHANNEL: { |
---|
[5298] | 856 | wlan_mac_low_set_radio_channel(ipc_msg_from_high_payload[0]); |
---|
[4959] | 857 | } |
---|
[4844] | 858 | break; |
---|
[3412] | 859 | |
---|
[4844] | 860 | //--------------------------------------------------------------------- |
---|
[4959] | 861 | case IPC_MBOX_LOW_RANDOM_SEED: { |
---|
[4844] | 862 | srand(ipc_msg_from_high_payload[0]); |
---|
[4959] | 863 | } |
---|
[4844] | 864 | break; |
---|
[3681] | 865 | |
---|
[4844] | 866 | //--------------------------------------------------------------------- |
---|
[4959] | 867 | case IPC_MBOX_CONFIG_TX_CTRL_POW: { |
---|
[4844] | 868 | mac_param_ctrl_tx_pow = (s8)ipc_msg_from_high_payload[0]; |
---|
[4959] | 869 | } |
---|
[4844] | 870 | break; |
---|
[3681] | 871 | |
---|
[4844] | 872 | //--------------------------------------------------------------------- |
---|
[4959] | 873 | case IPC_MBOX_CONFIG_RX_FILTER: { |
---|
| 874 | u32 filter_mode_hi = (u32)ipc_msg_from_high_payload[0]; |
---|
| 875 | u32 filter_mode_lo = 0; |
---|
[3681] | 876 | |
---|
[4959] | 877 | if((filter_mode_hi & RX_FILTER_FCS_MASK) == RX_FILTER_FCS_NOCHANGE){ |
---|
| 878 | filter_mode_lo |= (mac_param_rx_filter & RX_FILTER_FCS_MASK); |
---|
[4844] | 879 | } else { |
---|
[4959] | 880 | filter_mode_lo |= (filter_mode_hi & RX_FILTER_FCS_MASK); |
---|
[4844] | 881 | } |
---|
[3889] | 882 | |
---|
[4959] | 883 | if((filter_mode_hi & RX_FILTER_HDR_NOCHANGE) == RX_FILTER_HDR_NOCHANGE){ |
---|
| 884 | filter_mode_lo |= (mac_param_rx_filter & RX_FILTER_HDR_NOCHANGE); |
---|
[4844] | 885 | } else { |
---|
[4959] | 886 | filter_mode_lo |= (filter_mode_hi & RX_FILTER_HDR_NOCHANGE); |
---|
[4844] | 887 | } |
---|
[3889] | 888 | |
---|
[4959] | 889 | mac_param_rx_filter = filter_mode_lo; |
---|
| 890 | } |
---|
[4844] | 891 | break; |
---|
[3681] | 892 | |
---|
[4844] | 893 | //--------------------------------------------------------------------- |
---|
[4959] | 894 | case IPC_MBOX_CONFIG_RX_ANT_MODE: { |
---|
[4844] | 895 | wlan_rx_config_ant_mode(ipc_msg_from_high_payload[0]); |
---|
[4959] | 896 | } |
---|
[4844] | 897 | break; |
---|
[3210] | 898 | |
---|
[4844] | 899 | //--------------------------------------------------------------------- |
---|
[5417] | 900 | case IPC_MBOX_CONFIG_DSSS_EN: { |
---|
[5418] | 901 | if (ipc_msg_from_high_payload[0] == 1) { |
---|
| 902 | // xil_printf("Enabling DSSS\n"); |
---|
| 903 | wlan_mac_low_DSSS_rx_enable(); |
---|
| 904 | } else { |
---|
| 905 | // xil_printf("Disabling DSSS\n"); |
---|
| 906 | wlan_mac_low_DSSS_rx_disable(); |
---|
| 907 | } |
---|
[4959] | 908 | } |
---|
[4844] | 909 | break; |
---|
[3243] | 910 | |
---|
[4844] | 911 | //--------------------------------------------------------------------- |
---|
[4959] | 912 | case IPC_MBOX_TX_MPDU_READY: { |
---|
[5394] | 913 | u8 tx_pkt_buf; |
---|
| 914 | tx_pkt_buf = msg->arg0; |
---|
| 915 | if(tx_pkt_buf < NUM_TX_PKT_BUFS){ |
---|
| 916 | // Message is an indication that a Tx Pkt Buf needs processing |
---|
| 917 | if(allow_new_mpdu_tx){ |
---|
| 918 | wlan_mac_low_proc_pkt_buf( tx_pkt_buf ); |
---|
| 919 | } else { |
---|
| 920 | pkt_buf_pending_tx = tx_pkt_buf; |
---|
| 921 | } |
---|
| 922 | } |
---|
[4959] | 923 | } |
---|
[4875] | 924 | break; |
---|
| 925 | } |
---|
| 926 | } |
---|
[3149] | 927 | |
---|
[4959] | 928 | |
---|
| 929 | |
---|
[4875] | 930 | /*****************************************************************************/ |
---|
| 931 | /** |
---|
| 932 | * @brief Disable New MPDU Transmissions |
---|
| 933 | * |
---|
| 934 | * This function will prevent any future MPDU transmission requests from |
---|
| 935 | * being accepted. |
---|
| 936 | * |
---|
| 937 | * @param None |
---|
| 938 | * @return None |
---|
| 939 | * |
---|
| 940 | */ |
---|
| 941 | void wlan_mac_low_disable_new_mpdu_tx(){ |
---|
| 942 | allow_new_mpdu_tx = 0; |
---|
| 943 | } |
---|
[2730] | 944 | |
---|
[4959] | 945 | |
---|
| 946 | |
---|
[4875] | 947 | /*****************************************************************************/ |
---|
| 948 | /** |
---|
| 949 | * @brief Enable New MPDU Transmissions |
---|
| 950 | * |
---|
| 951 | * This function will allow future MPDU transmission requests from |
---|
| 952 | * being accepted. |
---|
| 953 | * |
---|
| 954 | * @param None |
---|
| 955 | * @return None |
---|
| 956 | * |
---|
| 957 | */ |
---|
| 958 | void wlan_mac_low_enable_new_mpdu_tx(){ |
---|
| 959 | if(allow_new_mpdu_tx == 0){ |
---|
| 960 | allow_new_mpdu_tx = 1; |
---|
| 961 | if(pkt_buf_pending_tx != -1){ |
---|
| 962 | wlan_mac_low_proc_pkt_buf(pkt_buf_pending_tx); |
---|
| 963 | pkt_buf_pending_tx = -1; |
---|
| 964 | } |
---|
| 965 | } |
---|
| 966 | } |
---|
[3555] | 967 | |
---|
[4959] | 968 | |
---|
| 969 | |
---|
[4875] | 970 | /*****************************************************************************/ |
---|
| 971 | /** |
---|
[5298] | 972 | * @brief Set the radio channel |
---|
| 973 | * |
---|
| 974 | * This function will set the radio channel for CPU LOW |
---|
| 975 | * |
---|
| 976 | * @param channel - Radio channel |
---|
| 977 | * @return None |
---|
| 978 | * |
---|
| 979 | */ |
---|
| 980 | void wlan_mac_low_set_radio_channel(u32 channel){ |
---|
| 981 | |
---|
| 982 | mac_param_chan = channel; |
---|
| 983 | |
---|
| 984 | if (wlan_verify_channel(mac_param_chan) == XST_SUCCESS) { |
---|
| 985 | if(mac_param_chan <= 14){ |
---|
| 986 | mac_param_band = RC_24GHZ; |
---|
[5358] | 987 | |
---|
[5385] | 988 | // Enable DSSS if global variable indicates it should be enabled and PHY sample rate allows it |
---|
| 989 | if ((mac_param_dsss_en) && (gl_phy_samp_rate == PHY_20M)) { |
---|
[5358] | 990 | wlan_phy_DSSS_rx_enable(); |
---|
| 991 | } |
---|
[5298] | 992 | } else { |
---|
| 993 | mac_param_band = RC_5GHZ; |
---|
[5358] | 994 | |
---|
| 995 | // Always disable DSSS when in the 5 GHZ band |
---|
| 996 | wlan_phy_DSSS_rx_disable(); |
---|
[5298] | 997 | } |
---|
| 998 | |
---|
[5432] | 999 | if(channel >= 36){ |
---|
| 1000 | // Adjust Tx baseband gain when switching to 5GHz channels; this adjustment makes |
---|
| 1001 | // the actual Tx power set via the Tx VGA more accurate |
---|
| 1002 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXGAIN_BB, 3); |
---|
| 1003 | } else { |
---|
| 1004 | radio_controller_setRadioParam(RC_BASEADDR, RC_ALL_RF, RC_PARAMID_TXGAIN_BB, 1); |
---|
| 1005 | } |
---|
| 1006 | |
---|
[5298] | 1007 | radio_controller_setCenterFrequency(RC_BASEADDR, (RC_ALL_RF), mac_param_band, wlan_mac_low_wlan_chan_to_rc_chan(mac_param_chan)); |
---|
| 1008 | wlan_mac_reset_NAV_counter(); |
---|
| 1009 | |
---|
| 1010 | } else { |
---|
| 1011 | xil_printf("Invalid channel selection %d\n", mac_param_chan); |
---|
| 1012 | } |
---|
| 1013 | } |
---|
| 1014 | |
---|
| 1015 | |
---|
| 1016 | |
---|
| 1017 | /*****************************************************************************/ |
---|
| 1018 | /** |
---|
[5358] | 1019 | * @brief Enable / Disable DSSS RX |
---|
| 1020 | * |
---|
[5384] | 1021 | * DSSS RX must be disabled when in the 5 GHz band or when the PHY sample rate |
---|
| 1022 | * is not 20 MSps. However, the low framework will maintain what the state |
---|
| 1023 | * should be when in the 2.4 GHz band and the PHY sample rate is 20 MSps |
---|
[5358] | 1024 | * |
---|
| 1025 | * @param None |
---|
| 1026 | * @return None |
---|
| 1027 | * |
---|
| 1028 | */ |
---|
| 1029 | void wlan_mac_low_DSSS_rx_enable() { |
---|
[5385] | 1030 | mac_param_dsss_en = 1; |
---|
[5358] | 1031 | |
---|
[5384] | 1032 | // Only enable DSSS if in 2.4 GHz band and phy sample rate is 20 |
---|
| 1033 | if ((mac_param_band == RC_24GHZ) && (gl_phy_samp_rate == PHY_20M)) { |
---|
[5358] | 1034 | wlan_phy_DSSS_rx_enable(); |
---|
| 1035 | } |
---|
| 1036 | } |
---|
| 1037 | |
---|
| 1038 | |
---|
| 1039 | void wlan_mac_low_DSSS_rx_disable() { |
---|
[5385] | 1040 | mac_param_dsss_en = 0; |
---|
[5358] | 1041 | wlan_phy_DSSS_rx_disable(); |
---|
| 1042 | } |
---|
| 1043 | |
---|
| 1044 | |
---|
| 1045 | |
---|
| 1046 | /*****************************************************************************/ |
---|
| 1047 | /** |
---|
[4875] | 1048 | * @brief Process a Tx Packet Buffer for Transmission |
---|
| 1049 | * |
---|
| 1050 | * This function can be called directly via the IPC_MBOX_TX_MPDU_READY message |
---|
| 1051 | * or asynchronously if allow_new_mpdu_tx is enabled. |
---|
| 1052 | * |
---|
| 1053 | * @param tx_pkt_buf - u16 packet buffer index |
---|
| 1054 | * @return None |
---|
| 1055 | * |
---|
| 1056 | */ |
---|
| 1057 | void wlan_mac_low_proc_pkt_buf(u16 tx_pkt_buf){ |
---|
| 1058 | u32 status; |
---|
[5401] | 1059 | tx_frame_info_t * tx_frame_info; |
---|
[4875] | 1060 | mac_header_80211 * tx_80211_header; |
---|
[5235] | 1061 | u32 is_locked, owner; |
---|
[4875] | 1062 | u32 low_tx_details_size; |
---|
[5224] | 1063 | wlan_ipc_msg_t ipc_msg_to_high; |
---|
[5221] | 1064 | ltg_packet_id_t* pkt_id; |
---|
[4821] | 1065 | |
---|
[5391] | 1066 | if(tx_pkt_buf >= NUM_TX_PKT_BUFS){ |
---|
| 1067 | xil_printf("Error: Tx Pkt Buf index exceeds NUM_TX_PKT_BUFS\n"); |
---|
| 1068 | return; |
---|
| 1069 | } |
---|
[2730] | 1070 | |
---|
[5401] | 1071 | tx_frame_info = (tx_frame_info_t*)TX_PKT_BUF_TO_ADDR(tx_pkt_buf); |
---|
[2730] | 1072 | |
---|
[5401] | 1073 | switch(tx_frame_info->tx_pkt_buf_state){ |
---|
[5391] | 1074 | // ---- Normal Tx process - buffer contains packet ready for Tx ---- |
---|
[5400] | 1075 | case TX_PKT_BUF_READY: |
---|
[5391] | 1076 | if(lock_tx_pkt_buf(tx_pkt_buf) != PKT_BUF_MUTEX_SUCCESS){ |
---|
| 1077 | wlan_printf(PL_ERROR, "Error: unable to lock TX pkt_buf %d\n", tx_pkt_buf); |
---|
[2730] | 1078 | |
---|
[5391] | 1079 | get_tx_pkt_buf_status(tx_pkt_buf, &is_locked, &owner); |
---|
[2730] | 1080 | |
---|
[5391] | 1081 | wlan_printf(PL_ERROR, " TX pkt_buf %d status: isLocked = %d, owner = %d\n", tx_pkt_buf, is_locked, owner); |
---|
[5401] | 1082 | tx_frame_info->tx_pkt_buf_state = TX_PKT_BUF_HIGH_CTRL; |
---|
[2730] | 1083 | |
---|
[5391] | 1084 | } else { |
---|
[5126] | 1085 | |
---|
[5401] | 1086 | tx_frame_info->tx_pkt_buf_state = TX_PKT_BUF_LOW_CTRL; |
---|
[2730] | 1087 | |
---|
[5401] | 1088 | tx_frame_info->delay_accept = (u32)(get_mac_time_usec() - tx_frame_info->timestamp_create); |
---|
[2730] | 1089 | |
---|
[5391] | 1090 | // Get pointer to start of MAC header in packet buffer |
---|
| 1091 | tx_80211_header = (mac_header_80211*)(TX_PKT_BUF_TO_ADDR(tx_pkt_buf)+PHY_TX_PKT_BUF_MPDU_OFFSET); |
---|
[2730] | 1092 | |
---|
[5391] | 1093 | // Insert sequence number here |
---|
| 1094 | tx_80211_header->sequence_control = ((tx_80211_header->sequence_control) & 0xF) | ( (unique_seq&0xFFF)<<4 ); |
---|
[5101] | 1095 | |
---|
[5401] | 1096 | if((tx_frame_info->flags) & TX_MPDU_FLAGS_FILL_UNIQ_SEQ){ |
---|
[5391] | 1097 | // Fill unique sequence number into LTG payload |
---|
| 1098 | pkt_id = (ltg_packet_id_t*)((u8*)tx_80211_header + sizeof(mac_header_80211)); |
---|
| 1099 | pkt_id->unique_seq = unique_seq; |
---|
| 1100 | } |
---|
[5101] | 1101 | |
---|
[5391] | 1102 | //Increment the global unique sequence number |
---|
| 1103 | unique_seq++; |
---|
[3889] | 1104 | |
---|
[5391] | 1105 | // Submit the MPDU for transmission - this callback will return only when the MPDU Tx is |
---|
| 1106 | // complete (after all re-transmissions, ACK Rx, timeouts, etc.) |
---|
| 1107 | // |
---|
| 1108 | // If a frame_tx_callback is not provided, the wlan_null_callback will always |
---|
| 1109 | // return 0 (ie TX_MPDU_RESULT_SUCCESS). |
---|
| 1110 | // |
---|
| 1111 | status = frame_tx_callback(tx_pkt_buf, low_tx_details); |
---|
[3271] | 1112 | |
---|
[5391] | 1113 | //Record the total time this MPDU spent in the Tx state machine |
---|
[5401] | 1114 | tx_frame_info->delay_done = (u32)(get_mac_time_usec() - (tx_frame_info->timestamp_create + (u64)(tx_frame_info->delay_accept))); |
---|
[2730] | 1115 | |
---|
[5401] | 1116 | low_tx_details_size = (tx_frame_info->num_tx_attempts)*sizeof(wlan_mac_low_tx_details_t); |
---|
[2822] | 1117 | |
---|
[5391] | 1118 | if(status == TX_MPDU_RESULT_SUCCESS){ |
---|
[5401] | 1119 | tx_frame_info->tx_result = TX_MPDU_RESULT_SUCCESS; |
---|
[5391] | 1120 | } else { |
---|
[5401] | 1121 | tx_frame_info->tx_result = TX_MPDU_RESULT_FAILURE; |
---|
[5391] | 1122 | } |
---|
[5126] | 1123 | |
---|
[5401] | 1124 | tx_frame_info->tx_pkt_buf_state = TX_PKT_BUF_DONE; |
---|
[3981] | 1125 | |
---|
[5391] | 1126 | //Revert the state of the packet buffer and return control to CPU High |
---|
| 1127 | if(unlock_tx_pkt_buf(tx_pkt_buf) != PKT_BUF_MUTEX_SUCCESS){ |
---|
| 1128 | wlan_printf(PL_ERROR, "Error: unable to unlock TX pkt_buf %d\n", tx_pkt_buf); |
---|
| 1129 | wlan_mac_low_send_exception(WLAN_ERROR_CODE_CPU_LOW_TX_MUTEX); |
---|
[5401] | 1130 | tx_frame_info->tx_pkt_buf_state = TX_PKT_BUF_HIGH_CTRL; |
---|
[5391] | 1131 | } else { |
---|
| 1132 | ipc_msg_to_high.msg_id = IPC_MBOX_MSG_ID(IPC_MBOX_TX_MPDU_DONE); |
---|
[2730] | 1133 | |
---|
[5391] | 1134 | //Add the per-Tx-event details to the IPC message so CPU High can add them to the log as TX_LOW entries |
---|
| 1135 | if(low_tx_details != NULL){ |
---|
| 1136 | ipc_msg_to_high.payload_ptr = (u32*)low_tx_details; |
---|
| 1137 | |
---|
| 1138 | //Make sure we don't overfill the IPC mailbox with TX_LOW data; truncate the Tx details if necessary |
---|
| 1139 | if(low_tx_details_size < (MAILBOX_BUFFER_MAX_NUM_WORDS << 2)){ |
---|
| 1140 | ipc_msg_to_high.num_payload_words = ( low_tx_details_size ) >> 2; // # of u32 words |
---|
| 1141 | } else { |
---|
| 1142 | ipc_msg_to_high.num_payload_words = (((MAILBOX_BUFFER_MAX_NUM_WORDS << 2) / sizeof(wlan_mac_low_tx_details_t)) * sizeof(wlan_mac_low_tx_details_t)) >> 2; // # of u32 words |
---|
| 1143 | } |
---|
| 1144 | } else { |
---|
| 1145 | ipc_msg_to_high.num_payload_words = 0; |
---|
| 1146 | ipc_msg_to_high.payload_ptr = NULL; |
---|
| 1147 | } |
---|
| 1148 | ipc_msg_to_high.arg0 = tx_pkt_buf; |
---|
| 1149 | write_mailbox_msg(&ipc_msg_to_high); |
---|
[4875] | 1150 | } |
---|
| 1151 | } |
---|
[5391] | 1152 | break; |
---|
| 1153 | // ---- Something went wrong - TX_READY message didn't match state of pkt buf ---- |
---|
[5400] | 1154 | default: |
---|
| 1155 | case TX_PKT_BUF_LOW_CTRL: |
---|
| 1156 | // CPU Low responsible for any LOW_CTRL buffers |
---|
[5391] | 1157 | // Don't transmit - just clean up and return |
---|
[5401] | 1158 | tx_frame_info->tx_pkt_buf_state = TX_PKT_BUF_HIGH_CTRL; |
---|
[5400] | 1159 | case TX_PKT_BUF_UNINITIALIZED: |
---|
| 1160 | case TX_PKT_BUF_DONE: |
---|
| 1161 | case TX_PKT_BUF_HIGH_CTRL: |
---|
[5391] | 1162 | // CPU High will handle it eventually |
---|
| 1163 | // Ensure CPU Low doesn't own lock then return |
---|
| 1164 | unlock_tx_pkt_buf(tx_pkt_buf); |
---|
| 1165 | break; |
---|
[4875] | 1166 | } |
---|
| 1167 | } |
---|
[3787] | 1168 | |
---|
[4959] | 1169 | |
---|
| 1170 | |
---|
[4844] | 1171 | /*****************************************************************************/ |
---|
| 1172 | /** |
---|
| 1173 | * @brief Notify upper-level MAC of frame reception |
---|
| 1174 | * |
---|
| 1175 | * Sends an IPC message to the upper-level MAC to notify it that a frame has been |
---|
| 1176 | * received and is ready to be processed |
---|
| 1177 | * |
---|
| 1178 | * @param None |
---|
| 1179 | * @return None |
---|
| 1180 | * |
---|
| 1181 | * @note This function assumes it is called in the same context where rx_pkt_buf is still valid. |
---|
| 1182 | */ |
---|
| 1183 | void wlan_mac_low_frame_ipc_send(){ |
---|
[5224] | 1184 | wlan_ipc_msg_t ipc_msg_to_high; |
---|
[2890] | 1185 | |
---|
[4844] | 1186 | ipc_msg_to_high.msg_id = IPC_MBOX_MSG_ID(IPC_MBOX_RX_MPDU_READY); |
---|
| 1187 | ipc_msg_to_high.num_payload_words = 0; |
---|
| 1188 | ipc_msg_to_high.arg0 = rx_pkt_buf; |
---|
[3981] | 1189 | |
---|
[5224] | 1190 | write_mailbox_msg(&ipc_msg_to_high); |
---|
[4844] | 1191 | } |
---|
[3981] | 1192 | |
---|
| 1193 | |
---|
[2890] | 1194 | |
---|
[4844] | 1195 | /*****************************************************************************/ |
---|
| 1196 | /** |
---|
| 1197 | * @brief Set Frame Reception Callback |
---|
| 1198 | * |
---|
| 1199 | * Tells the framework which function should be called when the PHY begins processing a frame reception |
---|
| 1200 | * |
---|
| 1201 | * @param callback - Pointer to callback function |
---|
| 1202 | * @return None |
---|
| 1203 | */ |
---|
| 1204 | inline void wlan_mac_low_set_frame_rx_callback(function_ptr_t callback){ |
---|
| 1205 | frame_rx_callback = callback; |
---|
| 1206 | } |
---|
[4505] | 1207 | |
---|
[5368] | 1208 | inline void wlan_mac_low_set_sample_rate_change_callback(function_ptr_t callback){ |
---|
| 1209 | sample_rate_change_callback = callback; |
---|
| 1210 | } |
---|
| 1211 | |
---|
[5220] | 1212 | inline void wlan_mac_low_set_beacon_txrx_config_callback(function_ptr_t callback){ |
---|
| 1213 | beacon_txrx_config_callback = callback; |
---|
[5139] | 1214 | } |
---|
[2730] | 1215 | |
---|
[5258] | 1216 | inline void wlan_mac_low_set_mactime_change_callback(function_ptr_t callback){ |
---|
| 1217 | mactime_change_callback = callback; |
---|
| 1218 | } |
---|
[2993] | 1219 | |
---|
[5258] | 1220 | |
---|
[4844] | 1221 | /*****************************************************************************/ |
---|
[2730] | 1222 | /** |
---|
[4844] | 1223 | * @brief Set Frame Transmission Callback |
---|
[2730] | 1224 | * |
---|
[4844] | 1225 | * Tells the framework which function should be called when an MPDU is passed down from the |
---|
| 1226 | * upper-level MAC for wireless transmission |
---|
[2730] | 1227 | * |
---|
[4844] | 1228 | * @param callback - Pointer to callback function |
---|
| 1229 | * @return None |
---|
[2730] | 1230 | */ |
---|
[4844] | 1231 | inline void wlan_mac_low_set_frame_tx_callback(function_ptr_t callback){ |
---|
| 1232 | frame_tx_callback = callback; |
---|
| 1233 | } |
---|
[2720] | 1234 | |
---|
[4740] | 1235 | |
---|
[2720] | 1236 | |
---|
[4844] | 1237 | /*****************************************************************************/ |
---|
[2730] | 1238 | /** |
---|
[4844] | 1239 | * @brief Set IPC_MBOX_LOW_PARAM Callback |
---|
[2730] | 1240 | * |
---|
[4844] | 1241 | * Tells the framework which function should be called when an ipc message is received for |
---|
| 1242 | * the IPC_MBOX_LOW_PARAM command. |
---|
[2730] | 1243 | * |
---|
[4844] | 1244 | * @param callback - Pointer to callback function |
---|
| 1245 | * @return None |
---|
[2730] | 1246 | */ |
---|
[4844] | 1247 | void wlan_mac_low_set_ipc_low_param_callback(function_ptr_t callback){ |
---|
| 1248 | ipc_low_param_callback = callback; |
---|
| 1249 | } |
---|
[2730] | 1250 | |
---|
| 1251 | |
---|
| 1252 | |
---|
[4844] | 1253 | /*****************************************************************************/ |
---|
[2730] | 1254 | /** |
---|
[4844] | 1255 | * @brief Various Getter Methods |
---|
[2730] | 1256 | * |
---|
[4844] | 1257 | * These functions will get parameters from the low framework. |
---|
[2730] | 1258 | * |
---|
[4844] | 1259 | * @param None |
---|
| 1260 | * @return (see individual function) |
---|
[2730] | 1261 | */ |
---|
[3243] | 1262 | inline u32 wlan_mac_low_get_active_channel(){ |
---|
[4844] | 1263 | return mac_param_chan; |
---|
[2730] | 1264 | } |
---|
| 1265 | |
---|
[4844] | 1266 | |
---|
[3243] | 1267 | inline s8 wlan_mac_low_get_current_ctrl_tx_pow(){ |
---|
[4844] | 1268 | return mac_param_ctrl_tx_pow; |
---|
[3210] | 1269 | } |
---|
[2730] | 1270 | |
---|
[4844] | 1271 | |
---|
[3243] | 1272 | inline u32 wlan_mac_low_get_current_rx_filter(){ |
---|
[4844] | 1273 | return mac_param_rx_filter; |
---|
[3243] | 1274 | } |
---|
[3210] | 1275 | |
---|
[3243] | 1276 | |
---|
[5384] | 1277 | inline phy_samp_rate_t wlan_mac_low_get_phy_samp_rate() { |
---|
| 1278 | return gl_phy_samp_rate; |
---|
| 1279 | } |
---|
[4959] | 1280 | |
---|
[5384] | 1281 | |
---|
| 1282 | |
---|
[4959] | 1283 | /*****************************************************************************/ |
---|
[2730] | 1284 | /** |
---|
[4844] | 1285 | * @brief Get the Rx Start Microsecond Timestamp |
---|
[2730] | 1286 | * |
---|
[4844] | 1287 | * This function returns the Rx start timestamp of the system |
---|
[2730] | 1288 | * |
---|
[4844] | 1289 | * @param None |
---|
| 1290 | * @return u64 - microsecond timestamp |
---|
[2730] | 1291 | */ |
---|
[4844] | 1292 | inline u64 wlan_mac_low_get_rx_start_timestamp() { |
---|
| 1293 | u32 timestamp_high_u32; |
---|
| 1294 | u32 timestamp_low_u32; |
---|
| 1295 | u64 timestamp_u64; |
---|
[4203] | 1296 | |
---|
[4844] | 1297 | // RX_START timestamp is captured once per reception - no race condition between 32-bit reads |
---|
| 1298 | timestamp_high_u32 = Xil_In32(WLAN_MAC_REG_RX_TIMESTAMP_MSB); |
---|
| 1299 | timestamp_low_u32 = Xil_In32(WLAN_MAC_REG_RX_TIMESTAMP_LSB); |
---|
| 1300 | timestamp_u64 = (((u64)timestamp_high_u32)<<32) + ((u64)timestamp_low_u32); |
---|
[4203] | 1301 | |
---|
[4844] | 1302 | return timestamp_u64; |
---|
| 1303 | } |
---|
[4203] | 1304 | |
---|
[4227] | 1305 | |
---|
[4959] | 1306 | |
---|
| 1307 | /*****************************************************************************/ |
---|
[4844] | 1308 | /** |
---|
| 1309 | * @brief Get the Tx Start Microsecond Timestamp |
---|
| 1310 | * |
---|
| 1311 | * This function returns the Tx start timestamp of the system |
---|
| 1312 | * |
---|
| 1313 | * @param None |
---|
| 1314 | * @return u64 - microsecond timestamp |
---|
| 1315 | */ |
---|
| 1316 | inline u64 wlan_mac_low_get_tx_start_timestamp() { |
---|
[4227] | 1317 | |
---|
[4844] | 1318 | u32 timestamp_high_u32; |
---|
| 1319 | u32 timestamp_low_u32; |
---|
| 1320 | u64 timestamp_u64; |
---|
[4227] | 1321 | |
---|
[4844] | 1322 | // TX_START timestamp is captured once per transmission - no race condition between 32-bit reads |
---|
| 1323 | timestamp_high_u32 = Xil_In32(WLAN_MAC_REG_TX_TIMESTAMP_MSB); |
---|
| 1324 | timestamp_low_u32 = Xil_In32(WLAN_MAC_REG_TX_TIMESTAMP_LSB); |
---|
| 1325 | timestamp_u64 = (((u64)timestamp_high_u32)<<32) + ((u64)timestamp_low_u32); |
---|
[4227] | 1326 | |
---|
[4844] | 1327 | return timestamp_u64; |
---|
| 1328 | } |
---|
[4227] | 1329 | |
---|
| 1330 | |
---|
| 1331 | |
---|
[4844] | 1332 | /*****************************************************************************/ |
---|
| 1333 | /** |
---|
| 1334 | * @brief Various Setter Methods |
---|
| 1335 | * |
---|
| 1336 | * These functions will set parameters in the low framework. |
---|
| 1337 | * |
---|
| 1338 | * @param (see individual function) |
---|
| 1339 | * @return None |
---|
| 1340 | */ |
---|
| 1341 | void wlan_mac_low_set_nav_check_addr(u8* addr) { |
---|
| 1342 | Xil_Out32(WLAN_MAC_REG_NAV_CHECK_ADDR_1, *((u32*)&(addr[0])) ); |
---|
| 1343 | Xil_Out32(WLAN_MAC_REG_NAV_CHECK_ADDR_2, *((u32*)&(addr[4])) ); |
---|
[4275] | 1344 | } |
---|
[4227] | 1345 | |
---|
[4275] | 1346 | |
---|
| 1347 | |
---|
[4844] | 1348 | /*****************************************************************************/ |
---|
| 1349 | /** |
---|
| 1350 | * @brief Calculates Rx Power (in dBm) |
---|
| 1351 | * |
---|
| 1352 | * This function calculates receive power for a given band, RSSI and LNA gain. This |
---|
| 1353 | * provides a reasonable estimate of Rx power, accurate to a few dB for standard waveforms. |
---|
| 1354 | * |
---|
| 1355 | * This function does not use the VGA gain setting or I/Q magnitudes. The PHY should use these |
---|
| 1356 | * to refine its own power measurement if needed. |
---|
| 1357 | * |
---|
| 1358 | * NOTE: These lookup tables were developed as part of the RF characterization. See: |
---|
| 1359 | * http://warpproject.org/trac/wiki/802.11/Benchmarks/Rx_Char |
---|
| 1360 | * |
---|
| 1361 | * |
---|
| 1362 | * @param rssi - RSSI value from RF frontend |
---|
| 1363 | * @param lna_gain - Value of LNA gain stage in RF frontend |
---|
| 1364 | * @return int - Power in dBm |
---|
| 1365 | */ |
---|
| 1366 | const s8 pow_lookup_B24[128] = {-90, -90, -89, -88, -88, -87, -87, -86, -86, -85, -84, -84, -83, -83, -82, -82, |
---|
| 1367 | -81, -81, -80, -79, -79, -78, -78, -77, -77, -76, -75, -75, -74, -74, -73, -73, |
---|
| 1368 | -72, -72, -71, -70, -70, -69, -69, -68, -68, -67, -66, -66, -65, -65, -64, -64, |
---|
| 1369 | -63, -63, -62, -61, -61, -60, -60, -59, -59, -58, -58, -57, -56, -56, -55, -55, |
---|
| 1370 | -54, -54, -53, -52, -52, -51, -51, -50, -50, -49, -49, -48, -47, -47, -46, -46, |
---|
| 1371 | -45, -45, -44, -43, -43, -42, -42, -41, -41, -40, -40, -39, -38, -38, -37, -37, |
---|
| 1372 | -36, -36, -35, -34, -34, -33, -33, -32, -32, -31, -31, -30, -29, -29, -28, -28, |
---|
| 1373 | -27, -27, -26, -26, -25, -24, -24, -23, -23, -22, -22, -21, -20, -20, -19, -19}; |
---|
[4275] | 1374 | |
---|
[4844] | 1375 | const s8 pow_lookup_B5[128] = {-97, -97, -96, -96, -95, -94, -94, -93, -93, -92, -92, -91, -90, -90, -89, -89, |
---|
| 1376 | -88, -88, -87, -87, -86, -85, -85, -84, -84, -83, -83, -82, -81, -81, -80, -80, |
---|
| 1377 | -79, -79, -78, -78, -77, -76, -76, -75, -75, -74, -74, -73, -72, -72, -71, -71, |
---|
| 1378 | -70, -70, -69, -69, -68, -67, -67, -66, -66, -65, -65, -64, -63, -63, -62, -62, |
---|
| 1379 | -61, -61, -60, -60, -59, -58, -58, -57, -57, -56, -56, -55, -54, -54, -53, -53, |
---|
| 1380 | -52, -52, -51, -51, -50, -49, -49, -48, -48, -47, -47, -46, -45, -45, -44, -44, |
---|
| 1381 | -43, -43, -42, -42, -41, -40, -40, -39, -39, -38, -38, -37, -36, -36, -35, -35, |
---|
| 1382 | -34, -34, -33, -32, -32, -31, -31, -30, -30, -29, -29, -28, -27, -27, -26, -26}; |
---|
[4227] | 1383 | |
---|
| 1384 | |
---|
[2730] | 1385 | inline int wlan_mac_low_calculate_rx_power(u16 rssi, u8 lna_gain){ |
---|
[3996] | 1386 | |
---|
[4844] | 1387 | u8 band; |
---|
| 1388 | int power = -100; |
---|
| 1389 | u16 adj_rssi = 0; |
---|
[2730] | 1390 | |
---|
[4844] | 1391 | band = mac_param_band; |
---|
[2730] | 1392 | |
---|
[4844] | 1393 | if(band == RC_24GHZ){ |
---|
| 1394 | switch(lna_gain){ |
---|
| 1395 | case 0: |
---|
| 1396 | case 1: |
---|
| 1397 | // Low LNA Gain State |
---|
| 1398 | adj_rssi = rssi + (440 << PHY_RX_RSSI_SUM_LEN_BITS); |
---|
| 1399 | break; |
---|
[3996] | 1400 | |
---|
[4844] | 1401 | case 2: |
---|
| 1402 | // Medium LNA Gain State |
---|
| 1403 | adj_rssi = rssi + (220 << PHY_RX_RSSI_SUM_LEN_BITS); |
---|
| 1404 | break; |
---|
[2730] | 1405 | |
---|
[4844] | 1406 | case 3: |
---|
| 1407 | // High LNA Gain State |
---|
| 1408 | adj_rssi = rssi; |
---|
| 1409 | break; |
---|
| 1410 | } |
---|
[2730] | 1411 | |
---|
[4844] | 1412 | power = pow_lookup_B24[(adj_rssi >> (PHY_RX_RSSI_SUM_LEN_BITS+POW_LOOKUP_SHIFT))]; |
---|
[4203] | 1413 | |
---|
[4844] | 1414 | } else if(band == RC_5GHZ){ |
---|
| 1415 | switch(lna_gain){ |
---|
| 1416 | case 0: |
---|
| 1417 | case 1: |
---|
| 1418 | // Low LNA Gain State |
---|
| 1419 | adj_rssi = rssi + (540 << PHY_RX_RSSI_SUM_LEN_BITS); |
---|
| 1420 | break; |
---|
[4203] | 1421 | |
---|
[4844] | 1422 | case 2: |
---|
| 1423 | // Medium LNA Gain State |
---|
| 1424 | adj_rssi = rssi + (280 << PHY_RX_RSSI_SUM_LEN_BITS); |
---|
| 1425 | break; |
---|
[4203] | 1426 | |
---|
[4844] | 1427 | case 3: |
---|
| 1428 | // High LNA Gain State |
---|
| 1429 | adj_rssi = rssi; |
---|
| 1430 | break; |
---|
| 1431 | } |
---|
[4203] | 1432 | |
---|
[4844] | 1433 | power = pow_lookup_B5[(adj_rssi >> (PHY_RX_RSSI_SUM_LEN_BITS+POW_LOOKUP_SHIFT))]; |
---|
| 1434 | } |
---|
[2730] | 1435 | |
---|
[4844] | 1436 | return power; |
---|
| 1437 | } |
---|
[3996] | 1438 | |
---|
[4203] | 1439 | |
---|
[3996] | 1440 | |
---|
[4844] | 1441 | /*****************************************************************************/ |
---|
[2730] | 1442 | /** |
---|
[4844] | 1443 | * @brief Calculates RSSI from Rx power (in dBm) |
---|
[2730] | 1444 | * |
---|
[4844] | 1445 | * This function calculates receive power for a given band, RSSI and LNA gain. This |
---|
| 1446 | * provides a reasonable estimate of Rx power, accurate to a few dB for standard waveforms. |
---|
[2730] | 1447 | * |
---|
[4844] | 1448 | * This function does not use the VGA gain setting or I/Q magnitudes. The PHY should use these |
---|
| 1449 | * to refine its own power measurement if needed. |
---|
[2730] | 1450 | * |
---|
[4844] | 1451 | * NOTE: These lookup tables were developed as part of the RF characterization. See: |
---|
| 1452 | * http://warpproject.org/trac/wiki/802.11/Benchmarks/Rx_Char |
---|
[2730] | 1453 | * |
---|
| 1454 | * |
---|
[4844] | 1455 | * @param rx_pow - Receive power in dBm |
---|
| 1456 | * @return u16 - RSSI value |
---|
[2730] | 1457 | * |
---|
[4844] | 1458 | * @note rx_pow must be in the range [PKT_DET_MIN_POWER_MIN, PKT_DET_MIN_POWER_MAX] inclusive |
---|
[2730] | 1459 | */ |
---|
[4844] | 1460 | const u16 rssi_lookup_B24[61] = { 1, 16, 24, 40, 56, 72, 80, 96, 112, 128, 144, 152, 168, 184, 200, 208, |
---|
| 1461 | 224, 240, 256, 272, 280, 296, 312, 328, 336, 352, 368, 384, 400, 408, 424, 440, |
---|
| 1462 | 456, 472, 480, 496, 512, 528, 536, 552, 568, 584, 600, 608, 624, 640, 656, 664, |
---|
| 1463 | 680, 696, 712, 728, 736, 752, 768, 784, 792, 808, 824, 840, 856}; |
---|
[2730] | 1464 | |
---|
[4844] | 1465 | const u16 rssi_lookup_B5[61] = { 96, 112, 128, 144, 160, 168, 184, 200, 216, 224, 240, 256, 272, 288, 296, 312, |
---|
| 1466 | 328, 344, 352, 368, 384, 400, 416, 424, 440, 456, 472, 480, 496, 512, 528, 544, |
---|
| 1467 | 552, 568, 584, 600, 608, 624, 640, 656, 672, 680, 696, 712, 728, 736, 752, 768, |
---|
| 1468 | 784, 800, 808, 824, 840, 856, 864, 880, 896, 912, 920, 936, 952}; |
---|
[3604] | 1469 | |
---|
[3271] | 1470 | |
---|
[4844] | 1471 | int wlan_mac_low_rx_power_to_rssi(s8 rx_pow){ |
---|
| 1472 | u8 band; |
---|
| 1473 | u16 rssi_val = 0; |
---|
[2730] | 1474 | |
---|
[4844] | 1475 | band = mac_param_band; |
---|
[2730] | 1476 | |
---|
[4844] | 1477 | if ((rx_pow <= PKT_DET_MIN_POWER_MAX) && (rx_pow >= PKT_DET_MIN_POWER_MIN)) { |
---|
| 1478 | if(band == RC_24GHZ){ |
---|
| 1479 | rssi_val = rssi_lookup_B24[rx_pow-PKT_DET_MIN_POWER_MIN]; |
---|
| 1480 | } else if(band == RC_5GHZ){ |
---|
| 1481 | rssi_val = rssi_lookup_B5[rx_pow-PKT_DET_MIN_POWER_MIN]; |
---|
| 1482 | } |
---|
[4768] | 1483 | |
---|
[4844] | 1484 | return rssi_val; |
---|
[2730] | 1485 | |
---|
[4844] | 1486 | } else { |
---|
| 1487 | return -1; |
---|
| 1488 | } |
---|
| 1489 | } |
---|
[2730] | 1490 | |
---|
[3271] | 1491 | |
---|
[2730] | 1492 | |
---|
[4844] | 1493 | /*****************************************************************************/ |
---|
[2730] | 1494 | /** |
---|
[4844] | 1495 | * @brief Set the minimum power for packet detection |
---|
[2730] | 1496 | * |
---|
[4844] | 1497 | * @param rx_pow - Receive power in dBm |
---|
| 1498 | * @return int - Status: 0 - Success; -1 - Failure |
---|
[2730] | 1499 | */ |
---|
[4844] | 1500 | int wlan_mac_low_set_pkt_det_min_power(s8 rx_pow){ |
---|
| 1501 | int rssi_val; |
---|
[3271] | 1502 | |
---|
[4844] | 1503 | rssi_val = wlan_mac_low_rx_power_to_rssi(rx_pow); |
---|
[3271] | 1504 | |
---|
[4844] | 1505 | if(rssi_val != -1){ |
---|
| 1506 | wlan_phy_rx_pktDet_RSSI_cfg( (PHY_RX_RSSI_SUM_LEN-1), (rssi_val << PHY_RX_RSSI_SUM_LEN_BITS), 1); |
---|
[3271] | 1507 | |
---|
[4844] | 1508 | return 0; |
---|
| 1509 | } else { |
---|
| 1510 | return -1; |
---|
| 1511 | } |
---|
| 1512 | } |
---|
[3271] | 1513 | |
---|
| 1514 | |
---|
| 1515 | |
---|
[4844] | 1516 | /*****************************************************************************/ |
---|
[2730] | 1517 | /** |
---|
[4844] | 1518 | * @brief Search for and Lock Empty Packet Buffer (Blocking) |
---|
[2730] | 1519 | * |
---|
[4953] | 1520 | * This is a blocking function for finding and locking an empty rx packet buffer. The low framework |
---|
| 1521 | * calls this function after passing a new wireless reception up to CPU High for processing. CPU High |
---|
| 1522 | * must unlock Rx packet buffers after processing the received packet. This function loops over all Rx |
---|
| 1523 | * packet buffers until it finds one that has been unlocked by CPU High. |
---|
[2730] | 1524 | * |
---|
[4953] | 1525 | * By design this function prints a message if it fails to unlock the oldest packet buffer. When this |
---|
| 1526 | * occurs it indicates that CPU Low has outrun CPU High, a situation that leads to dropped wireless |
---|
| 1527 | * receptions with high probability. The node recovers gracefully from this condition and will |
---|
| 1528 | * continue processing new Rx events after CPU High catches up. But seeing this message in the UART |
---|
| 1529 | * for CPU Low is a strong indicator the CPU High code is not keeping up with wireless receptions. |
---|
| 1530 | * |
---|
[4844] | 1531 | * @param None |
---|
| 1532 | * @return None |
---|
| 1533 | * |
---|
| 1534 | * @note This function assumes it is called in the same context where rx_pkt_buf is still valid. |
---|
[2730] | 1535 | */ |
---|
[4844] | 1536 | inline void wlan_mac_low_lock_empty_rx_pkt_buf(){ |
---|
| 1537 | // This function blocks until it safely finds a packet buffer for the PHY RX to store a future reception |
---|
[5401] | 1538 | rx_frame_info_t* rx_frame_info; |
---|
[4953] | 1539 | u32 i = 1; |
---|
[3271] | 1540 | |
---|
[4953] | 1541 | while(1) { |
---|
| 1542 | //rx_pkt_buf is the global shared by all contexts which deal with wireless Rx |
---|
| 1543 | // Rx packet buffers are used in order. Thus incrementing rx_pkt_buf should |
---|
| 1544 | // select the "oldest" packet buffer, the one that is most likely to have already |
---|
| 1545 | // been processed and released by CPU High |
---|
| 1546 | rx_pkt_buf = (rx_pkt_buf+1) % NUM_RX_PKT_BUFS; |
---|
[5401] | 1547 | rx_frame_info = (rx_frame_info_t*) RX_PKT_BUF_TO_ADDR(rx_pkt_buf); |
---|
[3271] | 1548 | |
---|
[5401] | 1549 | if((rx_frame_info->rx_pkt_buf_state) == RX_PKT_BUF_LOW_CTRL){ |
---|
[5400] | 1550 | // By default Rx pkt buffers are not zeroed out, to save the performance penalty of bzero'ing 2KB |
---|
| 1551 | // However zeroing out the pkt buffer can be helpful when debugging Rx MAC/PHY behaviors |
---|
| 1552 | // bzero((void *)(RX_PKT_BUF_TO_ADDR(rx_pkt_buf)), 2048); |
---|
[2730] | 1553 | |
---|
[5400] | 1554 | // Set the OFDM and DSSS PHYs to use the same Rx pkt buffer |
---|
| 1555 | wlan_phy_rx_pkt_buf_ofdm(rx_pkt_buf); |
---|
| 1556 | wlan_phy_rx_pkt_buf_dsss(rx_pkt_buf); |
---|
[2814] | 1557 | |
---|
[5400] | 1558 | if (i > 1) { xil_printf("found in %d iterations.\n", i); } |
---|
[2913] | 1559 | |
---|
[5400] | 1560 | return; |
---|
[3271] | 1561 | |
---|
[4844] | 1562 | } |
---|
| 1563 | |
---|
| 1564 | if (i == 1) { xil_printf("Searching for empty packet buff ... "); } |
---|
| 1565 | i++; |
---|
| 1566 | } |
---|
[2814] | 1567 | } |
---|
| 1568 | |
---|
[4844] | 1569 | |
---|
| 1570 | /*****************************************************************************/ |
---|
[2814] | 1571 | /** |
---|
[2730] | 1572 | * @brief Finish PHY Reception |
---|
| 1573 | * |
---|
[5253] | 1574 | * This function polls the MAC status register until the Rx PHY goes idle. The |
---|
| 1575 | * return value indicates whether the just-completed reception was good |
---|
| 1576 | * (no Rx errors and matching checksum) or bad |
---|
[2730] | 1577 | * |
---|
[4844] | 1578 | * @param None |
---|
| 1579 | * @return u32 - FCS status (RX_MPDU_STATE_FCS_GOOD or RX_MPDU_STATE_FCS_BAD) |
---|
[2730] | 1580 | */ |
---|
[5253] | 1581 | inline u32 wlan_mac_hw_rx_finish() { |
---|
[4844] | 1582 | u32 mac_hw_status; |
---|
[5262] | 1583 | int i = 0; |
---|
[3587] | 1584 | |
---|
[4844] | 1585 | // Wait for the packet to finish |
---|
| 1586 | do{ |
---|
| 1587 | mac_hw_status = wlan_mac_get_status(); |
---|
[5262] | 1588 | if(i++>1000000) {xil_printf("Stuck in wlan_mac_hw_rx_finish! 0x%08x\n", mac_hw_status);} |
---|
[4844] | 1589 | } while(mac_hw_status & WLAN_MAC_STATUS_MASK_RX_PHY_ACTIVE); |
---|
[2730] | 1590 | |
---|
[5253] | 1591 | // Check RX_END_ERROR and FCS |
---|
| 1592 | if( (mac_hw_status & WLAN_MAC_STATUS_MASK_RX_FCS_GOOD) && |
---|
| 1593 | ((mac_hw_status & WLAN_MAC_STATUS_MASK_RX_END_ERROR) == 0)) { |
---|
[5400] | 1594 | return 1; |
---|
[5253] | 1595 | |
---|
[4844] | 1596 | } else { |
---|
[5400] | 1597 | return 0; |
---|
[5253] | 1598 | |
---|
[4844] | 1599 | } |
---|
| 1600 | } |
---|
[3587] | 1601 | |
---|
[4844] | 1602 | |
---|
| 1603 | |
---|
| 1604 | /*****************************************************************************/ |
---|
| 1605 | /** |
---|
| 1606 | * @brief Force reset backoff counter in MAC hardware |
---|
| 1607 | */ |
---|
| 1608 | inline void wlan_mac_reset_backoff_counter() { |
---|
[5113] | 1609 | Xil_Out32(WLAN_MAC_REG_CONTROL, Xil_In32(WLAN_MAC_REG_CONTROL) | WLAN_MAC_CTRL_MASK_RESET_A_BACKOFF); |
---|
| 1610 | Xil_Out32(WLAN_MAC_REG_CONTROL, Xil_In32(WLAN_MAC_REG_CONTROL) & ~WLAN_MAC_CTRL_MASK_RESET_A_BACKOFF); |
---|
[2730] | 1611 | } |
---|
| 1612 | |
---|
[4844] | 1613 | |
---|
| 1614 | |
---|
| 1615 | /*****************************************************************************/ |
---|
[2936] | 1616 | /** |
---|
[4844] | 1617 | * @brief Force reset NAV counter in MAC hardware |
---|
| 1618 | */ |
---|
| 1619 | inline void wlan_mac_reset_NAV_counter() { |
---|
| 1620 | Xil_Out32(WLAN_MAC_REG_CONTROL, Xil_In32(WLAN_MAC_REG_CONTROL) | WLAN_MAC_CTRL_MASK_RESET_NAV); |
---|
| 1621 | Xil_Out32(WLAN_MAC_REG_CONTROL, Xil_In32(WLAN_MAC_REG_CONTROL) & ~WLAN_MAC_CTRL_MASK_RESET_NAV); |
---|
| 1622 | } |
---|
| 1623 | |
---|
| 1624 | |
---|
| 1625 | |
---|
| 1626 | /*****************************************************************************/ |
---|
| 1627 | /** |
---|
[2936] | 1628 | * @brief Convert dBm to Tx Gain Target |
---|
| 1629 | * |
---|
| 1630 | * This function maps a transmit power (in dBm) to a radio gain target. |
---|
| 1631 | * |
---|
[4844] | 1632 | * @param s8 power - Power in dBm |
---|
| 1633 | * @return u8 gain_target - Gain target in range of [0,63] |
---|
[2936] | 1634 | */ |
---|
| 1635 | inline u8 wlan_mac_low_dbm_to_gain_target(s8 power){ |
---|
[4844] | 1636 | s8 power_railed; |
---|
| 1637 | u8 return_value; |
---|
[2730] | 1638 | |
---|
[4844] | 1639 | if(power > TX_POWER_MAX_DBM){ |
---|
| 1640 | power_railed = TX_POWER_MAX_DBM; |
---|
| 1641 | } else if( power < TX_POWER_MIN_DBM){ |
---|
| 1642 | power_railed = TX_POWER_MIN_DBM; |
---|
| 1643 | } else { |
---|
| 1644 | power_railed = power; |
---|
| 1645 | } |
---|
[2730] | 1646 | |
---|
[4844] | 1647 | // This is only save because 'power' is constrained to less than half the dynamic range of an s8 type |
---|
| 1648 | return_value = (u8)((power_railed << 1) + 20); |
---|
[2942] | 1649 | |
---|
[4844] | 1650 | return return_value; |
---|
[2936] | 1651 | } |
---|
[2730] | 1652 | |
---|
[2936] | 1653 | |
---|
[3681] | 1654 | |
---|
[4844] | 1655 | /*****************************************************************************/ |
---|
[3681] | 1656 | /** |
---|
| 1657 | * @brief Map the WLAN channel frequencies onto the convention used by the radio controller |
---|
| 1658 | */ |
---|
| 1659 | inline u32 wlan_mac_low_wlan_chan_to_rc_chan(u32 mac_channel) { |
---|
[4844] | 1660 | int return_value = 0; |
---|
[3681] | 1661 | |
---|
[4844] | 1662 | switch(mac_channel){ |
---|
| 1663 | // 2.4GHz channels |
---|
| 1664 | case 1: |
---|
| 1665 | case 2: |
---|
| 1666 | case 3: |
---|
| 1667 | case 4: |
---|
| 1668 | case 5: |
---|
| 1669 | case 6: |
---|
| 1670 | case 7: |
---|
| 1671 | case 8: |
---|
| 1672 | case 9: |
---|
| 1673 | case 10: |
---|
| 1674 | case 11: |
---|
| 1675 | return_value = mac_channel; |
---|
| 1676 | break; |
---|
| 1677 | // 5GHz channels |
---|
| 1678 | case 36: // 5180MHz |
---|
| 1679 | return_value = 1; |
---|
| 1680 | break; |
---|
| 1681 | case 40: // 5200MHz |
---|
| 1682 | return_value = 2; |
---|
| 1683 | break; |
---|
| 1684 | case 44: // 5220MHz |
---|
| 1685 | return_value = 3; |
---|
| 1686 | break; |
---|
| 1687 | case 48: // 5240MHz |
---|
| 1688 | return_value = 4; |
---|
| 1689 | break; |
---|
| 1690 | } |
---|
[3681] | 1691 | |
---|
[4844] | 1692 | return return_value; |
---|
[3681] | 1693 | } |
---|
[4495] | 1694 | |
---|
[4583] | 1695 | |
---|
[4844] | 1696 | |
---|
| 1697 | /*****************************************************************************/ |
---|
| 1698 | /** |
---|
[4956] | 1699 | * @brief Convert MCS to number of data bits per symbol |
---|
[4844] | 1700 | */ |
---|
[5268] | 1701 | inline u16 wlan_mac_low_mcs_to_n_dbps(u8 mcs, u8 phy_mode) { |
---|
[4956] | 1702 | |
---|
[5268] | 1703 | if(phy_mode == PHY_MODE_NONHT && mcs < (sizeof(mcs_to_n_dbps_nonht_lut)/sizeof(mcs_to_n_dbps_nonht_lut[0]))) { |
---|
| 1704 | return mcs_to_n_dbps_nonht_lut[mcs]; |
---|
| 1705 | } else if(phy_mode == PHY_MODE_HTMF && mcs < (sizeof(mcs_to_n_dbps_htmf_lut)/sizeof(mcs_to_n_dbps_htmf_lut[0]))) { |
---|
| 1706 | return mcs_to_n_dbps_htmf_lut[mcs]; |
---|
| 1707 | } else { |
---|
| 1708 | xil_printf("ERROR (wlan_mac_low_mcs_to_n_dbps): Invalid PHY_MODE (%d) or MCS (%d)\n", phy_mode, mcs); |
---|
| 1709 | return 1; // N_DBPS used as denominator, so better not return 0 |
---|
| 1710 | } |
---|
[4583] | 1711 | } |
---|
| 1712 | |
---|
[4844] | 1713 | |
---|
| 1714 | |
---|
| 1715 | /*****************************************************************************/ |
---|
| 1716 | /** |
---|
| 1717 | * @brief Convert MCS to Control Response MCS |
---|
| 1718 | */ |
---|
[5368] | 1719 | inline u8 wlan_mac_low_mcs_to_ctrl_resp_mcs(u8 mcs, u8 phy_mode){ |
---|
| 1720 | // Returns the fastest NON-HT half-rate MCS lower than the provided MCS and no larger that 24Mbps. |
---|
[4956] | 1721 | // Valid return values are [0, 2, 4] |
---|
[5368] | 1722 | u8 return_value = 0; |
---|
[4583] | 1723 | |
---|
[5368] | 1724 | if(phy_mode == PHY_MODE_NONHT){ |
---|
| 1725 | return_value = mcs; |
---|
| 1726 | if(return_value > 4){ return_value = 4; } |
---|
| 1727 | if(return_value % 2){ return_value--; } |
---|
| 1728 | } else if(phy_mode == PHY_MODE_HTMF) { |
---|
| 1729 | switch(mcs){ |
---|
| 1730 | default: |
---|
| 1731 | case 0: |
---|
| 1732 | return_value = 0; |
---|
| 1733 | break; |
---|
| 1734 | case 1: |
---|
| 1735 | return_value = 2; |
---|
| 1736 | break; |
---|
| 1737 | case 2: |
---|
| 1738 | return_value = 2; |
---|
| 1739 | break; |
---|
| 1740 | case 3: |
---|
| 1741 | case 4: |
---|
| 1742 | case 5: |
---|
| 1743 | case 6: |
---|
| 1744 | case 7: |
---|
| 1745 | return_value = 4; |
---|
| 1746 | break; |
---|
| 1747 | } |
---|
| 1748 | } |
---|
[4844] | 1749 | return return_value; |
---|
[4583] | 1750 | } |
---|
| 1751 | |
---|
[5143] | 1752 | inline u64 wlan_mac_low_get_unique_seq(){ |
---|
| 1753 | return unique_seq; |
---|
| 1754 | } |
---|
| 1755 | |
---|
| 1756 | inline void wlan_mac_low_set_unique_seq(u64 curr_unique_seq){ |
---|
| 1757 | unique_seq = curr_unique_seq; |
---|
| 1758 | } |
---|
| 1759 | |
---|
[5253] | 1760 | inline void wlan_mac_hw_clear_rx_started() { |
---|
| 1761 | wlan_mac_reset_rx_started(1); |
---|
| 1762 | wlan_mac_reset_rx_started(0); |
---|
| 1763 | |
---|
| 1764 | return; |
---|
| 1765 | } |
---|