Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | MRF24WB0M Driver Management Messages |
||
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: WFMgmtMsg.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 | #include "TCPIP Stack/TCPIP.h" |
||
58 | #if defined(WF_CS_TRIS) |
||
59 | |||
60 | /* |
||
61 | ********************************************************************************************************* |
||
62 | * DEFINES |
||
63 | ********************************************************************************************************* |
||
64 | */ |
||
65 | |||
66 | /* used for assertions */ |
||
67 | #ifdef WF_DEBUG |
||
68 | #define WF_MODULE_NUMBER WF_MODULE_WF_MGMT_MSG |
||
69 | #endif |
||
70 | |||
71 | /* Macro used to determine is application is trying to send a management when in the driver function */ |
||
72 | /* WF_ProcessEvent(). */ |
||
73 | #define isInWFProcessEvent() ((g_FuncFlags & WF_PROCESS_EVENT_FUNC) > 0) |
||
74 | |||
75 | /* |
||
76 | ********************************************************************************************************* |
||
77 | * LOCAL GLOBAL VARIABLES |
||
78 | ********************************************************************************************************* |
||
79 | */ |
||
80 | |||
81 | static volatile BOOL gMgmtConfirmMsgReceived = FALSE; |
||
82 | static BOOL RestoreRxData = FALSE; |
||
83 | |||
84 | #ifdef WF_DEBUG |
||
85 | static UINT8 g_FuncFlags = 0x00; |
||
86 | #endif |
||
87 | |||
88 | |||
89 | /***************************************************************************** |
||
90 | * FUNCTION: SendMgmtMsg |
||
91 | * |
||
92 | * RETURNS: error code |
||
93 | * |
||
94 | * PARAMS: p_header -- pointer to mgmt message header data |
||
95 | * headerLength -- number of bytes in the header |
||
96 | * will be written |
||
97 | * p_data -- pointer to mgmt message data |
||
98 | * dataLength -- number of byte of data |
||
99 | * |
||
100 | * NOTES: Sends a management message |
||
101 | *****************************************************************************/ |
||
102 | void SendMgmtMsg(UINT8 *p_header, |
||
103 | UINT8 headerLength, |
||
104 | UINT8 *p_data, |
||
105 | UINT8 dataLength) |
||
106 | { |
||
107 | UINT32 startTickCount; |
||
108 | UINT32 maxAllowedTicks; |
||
109 | |||
110 | /* cannot send management messages while in WF_ProcessEvent() */ |
||
111 | WF_ASSERT(!isInWFProcessEvent()) |
||
112 | |||
113 | EnsureWFisAwake(); |
||
114 | |||
115 | /* if a Rx Data packet is mounted that has not yet been processed */ |
||
116 | if (GetRawWindowState(RAW_RX_ID) == WF_RAW_DATA_MOUNTED) |
||
117 | { |
||
118 | /* save it, so after mgmt response received it can be restored */ |
||
119 | PushRawWindow(RAW_RX_ID); |
||
120 | RestoreRxData = TRUE; |
||
121 | } |
||
122 | |||
123 | |||
124 | /* mounts a tx mgmt buffer on the MRF24WB0M when data tx is done */ |
||
125 | maxAllowedTicks = TICKS_PER_SECOND / 200; /* 5 ms timeout */ |
||
126 | startTickCount = (UINT32)TickGet(); |
||
127 | while (!WFisTxMgmtReady() ) |
||
128 | { |
||
129 | MACProcess(); |
||
130 | |||
131 | /* DEBUG -- REMOVE AFTER FIGURE OUT WHY TIMING OUT (RARELY HAPPENS) */ |
||
132 | if (TickGet() - startTickCount >= maxAllowedTicks) |
||
133 | { |
||
134 | /* force flags so WFisTxMgmtReady will return TRUE */ |
||
135 | SetRawWindowState(RAW_TX_ID, WF_RAW_UNMOUNTED); |
||
136 | RawWindowReady[RAW_TX_ID] = FALSE; |
||
137 | // putrsUART("."); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /* write out management header */ |
||
142 | RawSetByte(RAW_TX_ID, p_header, headerLength); |
||
143 | |||
144 | /* write out data (if any) */ |
||
145 | if (dataLength > 0) |
||
146 | { |
||
147 | RawSetByte(RAW_TX_ID, p_data, dataLength); |
||
148 | } |
||
149 | |||
150 | /* send mgmt msg to MRF24WB0M */ |
||
151 | SendRAWManagementFrame(headerLength + dataLength); |
||
152 | } |
||
153 | |||
154 | /***************************************************************************** |
||
155 | * FUNCTION: SignalMgmtConfirmReceivedEvent |
||
156 | * |
||
157 | * RETURNS: None |
||
158 | * |
||
159 | * PARAMS: None |
||
160 | * |
||
161 | * NOTES: Called by ProcessMgmtRxMsg when a mgmt confirm has been received. |
||
162 | * This function then sets a local flag for this module indicating |
||
163 | * the event. |
||
164 | *****************************************************************************/ |
||
165 | void SignalMgmtConfirmReceivedEvent(void) |
||
166 | { |
||
167 | gMgmtConfirmMsgReceived = TRUE; |
||
168 | } |
||
169 | |||
170 | /***************************************************************************** |
||
171 | * FUNCTION: WaitForMgmtResponse |
||
172 | * |
||
173 | * RETURNS: None |
||
174 | * |
||
175 | * PARAMS: expectedSubtype -- The expected subtype of the mgmt response |
||
176 | * freeAction -- FREE_MGMT_BUFFER or DO_NOT_FREE_MGMT_BUFFER |
||
177 | * |
||
178 | * NOTES: Called after sending a mgmt request. This function waits for a mgmt |
||
179 | * response. The caller can optionally request the the management |
||
180 | * response be freed immediately (by this function) or not freed. If not |
||
181 | * freed the caller is responsible to free the response buffer. |
||
182 | *****************************************************************************/ |
||
183 | void WaitForMgmtResponse(UINT8 expectedSubtype, UINT8 freeAction) |
||
184 | { |
||
185 | tMgmtMsgRxHdr hdr; |
||
186 | |||
187 | /* Wait until mgmt response is received */ |
||
188 | while (gMgmtConfirmMsgReceived == FALSE) |
||
189 | { |
||
190 | WFProcess(); |
||
191 | |||
192 | /* if received a data packet while waiting for mgmt packet */ |
||
193 | if (g_HostRAWDataPacketReceived) |
||
194 | { |
||
195 | /* loop until stack has processed the received data message */ |
||
196 | while (g_HostRAWDataPacketReceived) |
||
197 | { |
||
198 | StackTask(); |
||
199 | } |
||
200 | |||
201 | /* ensure interrupts enabled */ |
||
202 | WF_EintEnable(); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /* set this back to FALSE so the next mgmt send won't think he has a response before one is received */ |
||
207 | gMgmtConfirmMsgReceived = FALSE; |
||
208 | |||
209 | |||
210 | /* if the caller wants to delete the response immediately (doesn't need any data from it */ |
||
211 | if (freeAction == FREE_MGMT_BUFFER) |
||
212 | { |
||
213 | /* read and verify result before freeing up buffer to ensure our message send was successful */ |
||
214 | RawRead(RAW_RX_ID, 0, (UINT16)(sizeof(tMgmtMsgRxHdr)), (UINT8 *)&hdr); |
||
215 | |||
216 | /* Mgmt response 'result' field should always indicate success. If this assert is hit the error codes are located */ |
||
217 | /* WFApi.h. Search for WF_SUCCESS for the list of error codes. */ |
||
218 | WF_ASSERT(hdr.result == WF_SUCCESS); |
||
219 | |||
220 | /* mgmt response subtype had better match subtype we were expecting */ |
||
221 | WF_ASSERT(hdr.subtype == expectedSubtype); |
||
222 | |||
223 | /* free mgmt buffer */ |
||
224 | DeallocateMgmtRxBuffer(); |
||
225 | |||
226 | /* if there was a mounted data packet prior to the mgmt tx/rx transaction, then restore it */ |
||
227 | if (RestoreRxData == TRUE) |
||
228 | { |
||
229 | RestoreRxData = FALSE; |
||
230 | PopRawWindow(RAW_RX_ID); |
||
231 | SetRawWindowState(RAW_RX_ID, WF_RAW_DATA_MOUNTED); |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | |||
237 | /***************************************************************************** |
||
238 | * FUNCTION: WaitForMgmtRespAndReadData |
||
239 | * |
||
240 | * RETURNS: None |
||
241 | * |
||
242 | * PARAMS: expectedSubtype -- management message subtype that we are expecting |
||
243 | * p_data -- pointer where any desired management data bytes |
||
244 | * will be written |
||
245 | |||
246 | * numDataBytes -- Number of data bytes from mgmt response to write to |
||
247 | * p_data. Data always starts at index 4 of mgmt response. |
||
248 | * skipDataRead -- if TRUE, then no data will be read and the mgmt buffer will not |
||
249 | * be freed. If FALSE, the data will be read and the mgmt buffer |
||
250 | * will be freed. |
||
251 | * |
||
252 | * NOTES: Waits for the mgmt response message and validates it by: |
||
253 | * 1) checking the result field |
||
254 | * 2) verifying that the received subtype matches the execpted subtype |
||
255 | * |
||
256 | * In addition, this function reads the desired number of data bytes from |
||
257 | * the mgmt response, copies them to p_data, and then frees the mgmt buffer. |
||
258 | *****************************************************************************/ |
||
259 | void WaitForMgmtResponseAndReadData(UINT8 expectedSubtype, |
||
260 | UINT8 numDataBytes, |
||
261 | UINT8 startIndex, |
||
262 | UINT8 *p_data) |
||
263 | |||
264 | { |
||
265 | tMgmtMsgRxHdr hdr; /* management msg header struct */ |
||
266 | |||
267 | WaitForMgmtResponse(expectedSubtype, DO_NOT_FREE_MGMT_BUFFER); |
||
268 | |||
269 | /* if made it here then received a management message */ |
||
270 | RawRead(RAW_RX_ID, 0, (UINT16)(sizeof(tMgmtMsgRxHdr)), (UINT8 *)&hdr); |
||
271 | |||
272 | /* check header result field */ |
||
273 | if (hdr.result != WF_SUCCESS) |
||
274 | { |
||
275 | WF_ASSERT(FALSE); |
||
276 | } |
||
277 | |||
278 | /* make sure mgmt response is the expected one */ |
||
279 | if (hdr.subtype != expectedSubtype) |
||
280 | { |
||
281 | WF_ASSERT(FALSE); |
||
282 | } |
||
283 | |||
284 | /* if caller wants to read data from this mgmt response */ |
||
285 | if (numDataBytes > 0) |
||
286 | { |
||
287 | RawRead(RAW_RX_ID, startIndex, numDataBytes, p_data); |
||
288 | } |
||
289 | |||
290 | /* free the mgmt buffer */ |
||
291 | DeallocateMgmtRxBuffer(); |
||
292 | |||
293 | /* if there was a mounted data packet prior to the mgmt tx/rx transaction, then restore it */ |
||
294 | if (RestoreRxData == TRUE) |
||
295 | { |
||
296 | RestoreRxData = FALSE; |
||
297 | PopRawWindow(RAW_RX_ID); |
||
298 | SetRawWindowState(RAW_RX_ID, WF_RAW_DATA_MOUNTED); |
||
299 | } |
||
300 | |||
301 | } |
||
302 | |||
303 | |||
304 | #ifdef WF_DEBUG |
||
305 | /***************************************************************************** |
||
306 | * FUNCTION: WFSetFuncState |
||
307 | * |
||
308 | * RETURNS: None |
||
309 | * |
||
310 | * PARAMS: funcMask -- bit mask indicating the calling function |
||
311 | * state -- WF_ENTERING_FUNCTION or WF_LEAVING_FUNCTION |
||
312 | * |
||
313 | * NOTES: Called by WF_ProcessEvent() to be able to detect if there is an attempt |
||
314 | * to send a management message while processing the event (not allowed). |
||
315 | *****************************************************************************/ |
||
316 | void WFSetFuncState(UINT8 funcMask, UINT8 state) |
||
317 | { |
||
318 | if (state == WF_ENTERING_FUNCTION) |
||
319 | { |
||
320 | g_FuncFlags |= funcMask; |
||
321 | } |
||
322 | else |
||
323 | { |
||
324 | g_FuncFlags &= ~funcMask; |
||
325 | } |
||
326 | |||
327 | } |
||
328 | #endif /* WF_DEBUG */ |
||
329 | |||
330 | |||
331 | #endif /* WF_CS_TRIS */ |
Powered by WebSVN v2.8.3