| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /****************************************************************************** |
| 2 | |||
| 3 | MRF24WB0M Driver Data Tx/Rx |
||
| 4 | Module for Microchip TCP/IP Stack |
||
| 5 | -Provides access to MRF24WB0M WiFi controller |
||
| 6 | -Reference: MRF24WB0M Data sheet, IEEE 802.11 Standard |
||
| 7 | |||
| 8 | ******************************************************************************* |
||
| 9 | FileName: WFDataTxRx.c |
||
| 10 | Dependencies: TCP/IP Stack header files |
||
| 11 | Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32 |
||
| 12 | Compiler: Microchip C32 v1.10b or higher |
||
| 13 | Microchip C30 v3.22 or higher |
||
| 14 | Microchip C18 v3.34 or higher |
||
| 15 | Company: Microchip Technology, Inc. |
||
| 16 | |||
| 17 | Software License Agreement |
||
| 18 | |||
| 19 | Copyright (C) 2002-2010 Microchip Technology Inc. All rights reserved. |
||
| 20 | |||
| 21 | Microchip licenses to you the right to use, modify, copy, and distribute: |
||
| 22 | (i) the Software when embedded on a Microchip microcontroller or digital |
||
| 23 | signal controller product ("Device") which is integrated into |
||
| 24 | Licensee's product; or |
||
| 25 | (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
| 26 | ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device used in |
||
| 27 | conjunction with a Microchip ethernet controller for the sole purpose |
||
| 28 | of interfacing with the ethernet controller. |
||
| 29 | |||
| 30 | You should refer to the license agreement accompanying this Software for |
||
| 31 | additional information regarding your rights and obligations. |
||
| 32 | |||
| 33 | THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY |
||
| 34 | KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY |
||
| 35 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
||
| 36 | NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP BE LIABLE FOR ANY INCIDENTAL, |
||
| 37 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST |
||
| 38 | OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS BY |
||
| 39 | THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS |
||
| 40 | FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON |
||
| 41 | THE BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR |
||
| 42 | OTHERWISE. |
||
| 43 | |||
| 44 | |||
| 45 | Author Date Comment |
||
| 46 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 47 | KH 27 Jan 2010 Created for MRF24WB0M |
||
| 48 | ******************************************************************************/ |
||
| 49 | |||
| 50 | /* |
||
| 51 | ********************************************************************************************************* |
||
| 52 | * INCLUDES |
||
| 53 | ********************************************************************************************************* |
||
| 54 | */ |
||
| 55 | |||
| 56 | #include "TCPIP Stack/WFMac.h" |
||
| 57 | #if defined(WF_CS_TRIS) && defined(WF_USE_DATA_TX_RX_FUNCTIONS) |
||
| 58 | |||
| 59 | /* used for assertions */ |
||
| 60 | #ifdef WF_DEBUG |
||
| 61 | #define WF_MODULE_NUMBER WF_MODULE_WF_DATA_TX_RX |
||
| 62 | #endif |
||
| 63 | |||
| 64 | /* |
||
| 65 | ********************************************************************************************************* |
||
| 66 | * DEFINES |
||
| 67 | ********************************************************************************************************* |
||
| 68 | */ |
||
| 69 | |||
| 70 | #define WF_SIZE_OF_TX_DATA_PREAMBLE (sizeof(tTxDataPreamble)) |
||
| 71 | #define WF_SIZE_OF_RX_DATA_PREAMBLE (sizeof(tRxDataPreamble)) |
||
| 72 | |||
| 73 | /* The following are offsets within a RX data packet when mounted into a RAW window on the MRF24WB0M. */ |
||
| 74 | #define RAW_RX_TYPE_INDEX (0) /* [0] */ |
||
| 75 | #define RAW_RX_SUBTYPE_INDEX (1) /* [1] */ |
||
| 76 | #define RAW_RX_RSSI_INDEX (2) /* [2] - [3] */ |
||
| 77 | #define RAW_RX_ARRIV_TIME_TH_INDEX (4) /* [4] - [5] */ |
||
| 78 | #define RAW_RX_ARRIV_TIME_BH_INDEX (6) /* [6] - [7] */ |
||
| 79 | #define RAW_RX_DATA_LENGTH_INDEX (8) /* [8] - [9] */ |
||
| 80 | #define RAW_RX_SNAP_HEADER_INDEX (10) /* [10] - [15] */ |
||
| 81 | #define RAW_RX_DEST_ADD_INDEX (16) /* [16] - [21] */ |
||
| 82 | #define RAW_RX_SRC_ADD_INDEX (22) /* [22] - [27] */ |
||
| 83 | |||
| 84 | /* |
||
| 85 | ********************************************************************************************************* |
||
| 86 | * LOCAL DATA TYPES |
||
| 87 | ********************************************************************************************************* |
||
| 88 | */ |
||
| 89 | |||
| 90 | typedef struct txDataPreambleStruct |
||
| 91 | { |
||
| 92 | UINT8 type; |
||
| 93 | UINT8 subType; |
||
| 94 | UINT8 h2; |
||
| 95 | UINT8 h3; |
||
| 96 | } tTxDataPreamble; |
||
| 97 | |||
| 98 | typedef struct rxDataPreambleStruct |
||
| 99 | { |
||
| 100 | UINT8 type; /* always WF_DATA_RX_INDICATE_TYPE (3) */ |
||
| 101 | UINT8 subType; /* always 1 */ |
||
| 102 | } tRxDataPreamble; |
||
| 103 | |||
| 104 | #if 0 |
||
| 105 | /* This is the format of a RAW receive packet as created by the MRF24WB0M when it receives a data packet. */ |
||
| 106 | /* This structure starts at RAW index 0 within the RAW Rx buffer. */ |
||
| 107 | typedef struct |
||
| 108 | { |
||
| 109 | tRxDataPreamble preamble; /* 2 bytes (type and subtype) */ |
||
| 110 | UINT16 rssi; /* the value of the MRF24WB0M RSSI when the data frame was received */ |
||
| 111 | UINT16 arrivalTime_th; /* value of the 32-bit MRF24WB0M system clock when the frame arrived (bits 31-16) */ |
||
| 112 | UINT16 arrivalTime_bh; /* MRF24WB0M system clock (bits 15-0) */ |
||
| 113 | UINT16 dataLen; /* byte length of the payload which immediately follows this data structure */ |
||
| 114 | UINT8 snapHeader[6]; /* WiFi-specific Snap header */ |
||
| 115 | UINT8 dstAddr[WF_MAC_ADDRESS_LENGTH]; /* MAC Address to which the data frame was directed. */ |
||
| 116 | UINT8 srcAddr[WF_MAC_ADDRESS_LENGTH]; /* MAC Address of the Station that sent the Data frame. */ |
||
| 117 | } tWFRxDataIndicate; /* 28 bytes */ |
||
| 118 | #endif |
||
| 119 | |||
| 120 | /* |
||
| 121 | ********************************************************************************************************* |
||
| 122 | * WF_TxSendCompleteDataPacket() |
||
| 123 | * |
||
| 124 | * Description : Allocates MRF24WB0M memory, writes out the packet to that memory, and directs the MRF24WB0M |
||
| 125 | * to transmit the packet. |
||
| 126 | * |
||
| 127 | * Argument(s) : p_txData - pointer to Tx data packet |
||
| 128 | * length - length, in bytes, of Tx data packet |
||
| 129 | * |
||
| 130 | * Return(s) : Error code |
||
| 131 | * |
||
| 132 | * Caller(s) : Application |
||
| 133 | * |
||
| 134 | * Notes: : None |
||
| 135 | * |
||
| 136 | ********************************************************************************************************* |
||
| 137 | */ |
||
| 138 | void WF_TxSendCompleteDataPacket(UINT8 *p_txData, |
||
| 139 | UINT16 length) |
||
| 140 | |||
| 141 | { |
||
| 142 | WF_TxDataAllocateBuffer(length); |
||
| 143 | WF_TxDataWrite(p_txData, length, 0); |
||
| 144 | WF_TxDataSendPacket(length); |
||
| 145 | } |
||
| 146 | |||
| 147 | /* |
||
| 148 | ********************************************************************************************************* |
||
| 149 | * WF_RxReceiveCompleteDataPacket() |
||
| 150 | * |
||
| 151 | * Description : Reads an entire data rx packet from MRF24WB0M memory and copies it to host memory, then |
||
| 152 | * frees the MRF24WB0M memory so it can be reused. |
||
| 153 | * |
||
| 154 | * Argument(s) : p_rxData - pointer to where to copy received packet |
||
| 155 | * length - length, in bytes, of the Rx packet to copy |
||
| 156 | * |
||
| 157 | * Return(s) : Error code |
||
| 158 | * |
||
| 159 | * Caller(s) : Application |
||
| 160 | * |
||
| 161 | * Notes: : None |
||
| 162 | * |
||
| 163 | ********************************************************************************************************* |
||
| 164 | */ |
||
| 165 | void WF_RxReceiveCompleteDataPacket(UINT8 *p_rxData, |
||
| 166 | UINT16 length) |
||
| 167 | { |
||
| 168 | /* read the entire receive data packet into host memory */ |
||
| 169 | WF_RxDataReadPacket(p_rxData, length, 0); |
||
| 170 | |||
| 171 | /* free MRF24WB0M memory that held the rx data packet */ |
||
| 172 | WF_RxDataDeallocateBuffer(); |
||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | /* |
||
| 177 | ********************************************************************************************************* |
||
| 178 | * WF_TxDataAllocateBuffer() |
||
| 179 | * |
||
| 180 | * Description : Allocates a Tx Data Packet buffer on the MRF24WB0M |
||
| 181 | * |
||
| 182 | * Argument(s) : txDataPacketLength - length, in bytes, of memory needed |
||
| 183 | * |
||
| 184 | * Return(s) : None |
||
| 185 | * |
||
| 186 | * Caller(s) : Application |
||
| 187 | * |
||
| 188 | * Notes: : (1) Allocates memory on the MRF24WB0M to hold a Tx Data Packet |
||
| 189 | * |
||
| 190 | * (2) Do not assume this function always succeeds. During busy periods the MRF24WB0M may not |
||
| 191 | * have available memory until the previous Tx data packet has finished transmitting. |
||
| 192 | * |
||
| 193 | ********************************************************************************************************* |
||
| 194 | */ |
||
| 195 | void WF_TxDataAllocateBuffer(UINT16 txDataPacketLength) |
||
| 196 | { |
||
| 197 | /* Ensure the MRF24WB0M is awake (only applies if PS-Poll was enabled) */ |
||
| 198 | EnsureWFisAwake(); |
||
| 199 | |||
| 200 | if (!AllocateDataTxBuffer(txDataPacketLength)) |
||
| 201 | { |
||
| 202 | WF_ASSERT(FALSE); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | |||
| 207 | /* |
||
| 208 | ********************************************************************************************************* |
||
| 209 | * WF_TxDataWrite() |
||
| 210 | * |
||
| 211 | * Description : Writes Tx packet data from Host CPU to MRF24WB0M tx buffer |
||
| 212 | * |
||
| 213 | * Argument(s) : p_txData - Pointer to Tx data |
||
| 214 | * length - length, in bytes, of Tx data |
||
| 215 | * startIndex - Index within MRF24WB0M Tx data block to start writing |
||
| 216 | * |
||
| 217 | * Return(s) : None |
||
| 218 | * |
||
| 219 | * Caller(s) : Application |
||
| 220 | * |
||
| 221 | * Notes: : (1) MRF24WB0M Tx data block must first be allocated via WF_TxDataAllocateBuffer() |
||
| 222 | * |
||
| 223 | * (2) Current support only for full length Tx data messaages. In other words, startIndex |
||
| 224 | * must be 0, and length must be the total length of the Tx data packet. |
||
| 225 | * |
||
| 226 | ********************************************************************************************************* |
||
| 227 | */ |
||
| 228 | void WF_TxDataWrite(UINT8 *p_txData, |
||
| 229 | UINT16 length, |
||
| 230 | UINT16 startIndex) |
||
| 231 | { |
||
| 232 | WF_ASSERT(startIndex == 0); |
||
| 233 | |||
| 234 | if (length == 0) |
||
| 235 | { |
||
| 236 | return; |
||
| 237 | } |
||
| 238 | |||
| 239 | /* Ensure the MRF24WB0M is awake (only applies if PS-Poll was enabled) */ |
||
| 240 | EnsureWFisAwake(); |
||
| 241 | |||
| 242 | /* set the RAW index to desired location, offset by the Tx data preamble */ |
||
| 243 | RawSetIndex(RAW_TX_ID, startIndex + WF_SIZE_OF_TX_DATA_PREAMBLE); |
||
| 244 | |||
| 245 | /* write tx data to chip */ |
||
| 246 | WriteWFArray(RAW_1_DATA_REG, p_txData, length); |
||
| 247 | } |
||
| 248 | |||
| 249 | /* |
||
| 250 | ********************************************************************************************************* |
||
| 251 | * WF_TxDataSendPacket() |
||
| 252 | * |
||
| 253 | * Description : Directs the MRF24WB0M to transmit a Tx data packet to the 802.11 network. |
||
| 254 | * |
||
| 255 | * Argument(s) : length - length, in bytes, of data packet |
||
| 256 | * |
||
| 257 | * Return(s) : Error code |
||
| 258 | * |
||
| 259 | * Caller(s) : Application |
||
| 260 | * |
||
| 261 | * Notes: : (1) MRF24WB0M Tx data block must first be allocated via WF_TxDataAllocateBuffer() |
||
| 262 | * |
||
| 263 | * (2) Current support only for full length Tx data messaages. In other words, startIndex |
||
| 264 | * must be 0, and length must be the total length of the Tx data packet. |
||
| 265 | * |
||
| 266 | ********************************************************************************************************* |
||
| 267 | */ |
||
| 268 | void WF_TxDataSendPacket(UINT16 length) |
||
| 269 | { |
||
| 270 | tTxDataPreamble txPreamble; |
||
| 271 | |||
| 272 | /* fill in tx data preamble with correct header */ |
||
| 273 | txPreamble.type = WF_DATA_REQUEST_TYPE; |
||
| 274 | txPreamble.subType = WF_STD_DATA_MSG_SUBTYPE; |
||
| 275 | txPreamble.h2 = 1; |
||
| 276 | txPreamble.h3 = 0; |
||
| 277 | |||
| 278 | /* Ensure the MRF24WB0M is awake (only applies if PS-Poll was enabled) */ |
||
| 279 | EnsureWFisAwake(); |
||
| 280 | |||
| 281 | /* write out preamble to MRF24WB0M buffer to the two extra bytes at start of allocated buffer */ |
||
| 282 | RawSetIndex(RAW_TX_ID, 0); |
||
| 283 | WriteWFArray(RAW_1_DATA_REG, (UINT8 *)&txPreamble, WF_SIZE_OF_TX_DATA_PREAMBLE ); |
||
| 284 | |||
| 285 | /* now tell MRF24WB0M to transmit the tx data packet */ |
||
| 286 | RawSendTxBuffer(length); |
||
| 287 | } |
||
| 288 | |||
| 289 | |||
| 290 | /* |
||
| 291 | ********************************************************************************************************* |
||
| 292 | * WF_RxDataReadPacket() |
||
| 293 | * |
||
| 294 | * Description : Reads all or part of an Rx data packet from MRF24WB0M memory to Host CPU memory. |
||
| 295 | * |
||
| 296 | * Argument(s) : p_rxData - pointer to where Rx data packet will be written |
||
| 297 | * length - Number of bytes to read from MRF24WB0M memory |
||
| 298 | * startIndex - start index within MRF24WB0M memory to start read from |
||
| 299 | * |
||
| 300 | * Return(s) : None |
||
| 301 | * |
||
| 302 | * Caller(s) : Application |
||
| 303 | * |
||
| 304 | * Notes: : None |
||
| 305 | * |
||
| 306 | ********************************************************************************************************* |
||
| 307 | */ |
||
| 308 | void WF_RxDataReadPacket(UINT8 *p_rxData, |
||
| 309 | UINT16 length, |
||
| 310 | UINT16 startIndex) |
||
| 311 | { |
||
| 312 | #if !defined(USE_WF_HOST_BUFFER) |
||
| 313 | UINT16 byteCount; |
||
| 314 | #endif |
||
| 315 | |||
| 316 | WF_ASSERT(startIndex == 0); |
||
| 317 | |||
| 318 | /* if application calls this function, and gHostRAWDataPacketReceived is not TRUE, then error, because */ |
||
| 319 | /* driver has not received a data packet. */ |
||
| 320 | if (!g_HostRAWDataPacketReceived) |
||
| 321 | { |
||
| 322 | WF_ASSERT(FALSE); |
||
| 323 | } |
||
| 324 | |||
| 325 | g_HostRAWDataPacketReceived = FALSE; /* clear flag for next data packet */ |
||
| 326 | |||
| 327 | /* Ensure the MRF24WB0M is awake (only applies if PS-Poll was enabled) */ |
||
| 328 | EnsureWFisAwake(); |
||
| 329 | |||
| 330 | #if !defined(USE_WF_HOST_BUFFER) /* when testing with MCHP stack the packet is already mounted */ |
||
| 331 | /* Mount Read FIFO to RAW Rx window. Size of Rx data packet is returned */ |
||
| 332 | byteCount = RawMountRxBuffer(); |
||
| 333 | WF_ASSERT(byteCount > 0); |
||
| 334 | #endif |
||
| 335 | |||
| 336 | /* now that buffer mounted it is safe to reenable interrupts */ |
||
| 337 | WF_EintEnable(); |
||
| 338 | |||
| 339 | /* read the requested bytes into callers buffer */ |
||
| 340 | RawRead(RAW_RX_ID, RAW_RX_DEST_ADD_INDEX + startIndex, length, p_rxData); |
||
| 341 | } |
||
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | /* |
||
| 346 | ********************************************************************************************************* |
||
| 347 | * WF_RxDataDeallocateBuffer() |
||
| 348 | * |
||
| 349 | * Description : Releases the MRF24WB0M memory containing an Rx data packet after the application is done with |
||
| 350 | * it. |
||
| 351 | * |
||
| 352 | * Argument(s) : None |
||
| 353 | * |
||
| 354 | * Return(s) : None |
||
| 355 | * |
||
| 356 | * Caller(s) : Application |
||
| 357 | * |
||
| 358 | * Notes: : (1) Must be called after the application completes processing of an Rx data packet. |
||
| 359 | * |
||
| 360 | ********************************************************************************************************* |
||
| 361 | */ |
||
| 362 | void WF_RxDataDeallocateBuffer(void) |
||
| 363 | { |
||
| 364 | /* Ensure the MRF24WB0M is awake (only applies if PS-Poll was enabled) */ |
||
| 365 | EnsureWFisAwake(); |
||
| 366 | |||
| 367 | DeallocateDataRxBuffer(); |
||
| 368 | } |
||
| 369 | |||
| 370 | #endif /* WF_CS_TRIS && WF_USE_DATA_TX_RX_FUNCTIONS */ |
||
| 371 | |||
| 372 |
Powered by WebSVN v2.8.3