/***************************************************************************** Function: BOOL AllocateDataTxBuffer(UINT16 bytesNeeded) Summary: Allocates a Data Tx buffer for use by the TCP/IP stack. Description: Determines if WiFi chip has enough memory to allocate a tx data buffer, and, if so, allocates it. Precondition: None Parameters: bytesNeeded -- number of bytes needed for the data tx message Returns: True if data tx buffer successfully allocated, else False Remarks: None *****************************************************************************/ BOOL AllocateDataTxBuffer(UINT16 bytesNeeded) { UINT16 bufAvail; UINT16 byteCount; WF_ASSERT(GetRawWindowState(RAW_DATA_TX_ID) != WF_RAW_DATA_MOUNTED); /* Ensure the MRF24W is awake (only applies if PS-Poll was enabled) */ EnsureWFisAwake(); /* get total bytes available for DATA tx memory pool */ bufAvail = Read16BitWFRegister(WF_HOST_WFIFO_BCNT0_REG) & 0x0fff; /* LS 12 bits contain length */ /* if enough bytes available to allocate */ if ( bufAvail >= bytesNeeded ) { /* allocate and create the new Tx buffer (mgmt or data) */ byteCount = RawMove(RAW_DATA_TX_ID, RAW_DATA_POOL, TRUE, bytesNeeded); WF_ASSERT(byteCount != 0); /* flag this raw window as mounted (in use) */ SetRawWindowState(RAW_DATA_TX_ID, WF_RAW_DATA_MOUNTED); return TRUE; } /* else not enough bytes available at this time to satisfy request */ else { return FALSE; } }
/***************************************************************************** * FUNCTION: RawGetByte * * RETURNS: error code * * PARAMS: * rawId - RAW ID * pBuffer - Buffer to read bytes into * length - number of bytes to read * * NOTES: Reads bytes from the RAW engine *****************************************************************************/ void RawGetByte(UINT16 rawId, UINT8 *pBuffer, UINT16 length) { UINT8 regId; #if defined(OUTPUT_RAW_TX_RX) char buf[8]; #endif /* if reading a data message do following check */ if (!g_WaitingForMgmtResponse) { // if RAW index previously set out of range and caller is trying to do illegal read if ((rawId == RAW_RX_ID) && g_rxIndexSetBeyondBuffer && (GetRawWindowState(RAW_RX_ID) == WF_RAW_DATA_MOUNTED)) { WF_ASSERT(FALSE); /* attempting to read past end of RAW buffer */ } } regId = (rawId == RAW_ID_0) ? RAW_0_DATA_REG : RAW_1_DATA_REG; ReadWFArray(regId, pBuffer, length); #if defined(OUTPUT_RAW_TX_RX) putrsUART("R:"); while (length-- != 0) { sprintf(buf," %02X", *pBuffer++); putsUART(buf); } putrsUART("\r\n"); #endif }
/***************************************************************************** * FUNCTION: RawSetByte * * RETURNS: None * * PARAMS: * rawId - RAW ID * pBuffer - Buffer containing bytes to write * length - number of bytes to read * * NOTES: Writes bytes to RAW window *****************************************************************************/ void RawSetByte(UINT16 rawId, UINT8 *pBuffer, UINT16 length) { UINT8 regId; #if defined(OUTPUT_RAW_TX_RX) char buf [8]; #endif /* if previously set index past legal range and now trying to write to RAW engine */ if ( (rawId == 0) && g_rxIndexSetBeyondBuffer && (GetRawWindowState(RAW_TX_ID) == WF_RAW_DATA_MOUNTED) ) { // WF_ASSERT(FALSE); /* attempting to write past end of RAW window */ } /* write RAW data to chip */ regId = (rawId == RAW_ID_0) ? RAW_0_DATA_REG : RAW_1_DATA_REG; WriteWFArray(regId, pBuffer, length); #if defined(OUTPUT_RAW_TX_RX) putrsUART("T:"); while (length-- != 0) { sprintf(buf," %02X", *pBuffer++); putsUART(buf); } putrsUART("\r\n"); #endif }
/***************************************************************************** * FUNCTION: RawSetByte * * RETURNS: None * * PARAMS: * rawId - RAW ID * pBuffer - Buffer containing bytes to write * length - number of bytes to read * * NOTES: Writes bytes to RAW window *****************************************************************************/ void RawSetByte(uint16_t rawId, uint8_t *pBuffer, uint16_t length) { uint8_t regId; #if defined(OUTPUT_RAW_TX_RX) uint16_t i; #endif /* if previously set index past legal range and now trying to write to RAW engine */ if ( (rawId == 0) && g_rxIndexSetBeyondBuffer && (GetRawWindowState(RAW_TX_ID) == WF_RAW_DATA_MOUNTED) ) { SYS_ASSERT(false, ""); /* attempting to write past end of RAW window */ } /* write RAW data to chip */ regId = (rawId==RAW_ID_0)?RAW_0_DATA_REG:RAW_1_DATA_REG; WriteWFArray(regId, pBuffer, length); #if defined(OUTPUT_RAW_TX_RX) for (i = 0; i < length; ++i) { char buf[16]; sprintf(buf,"T: %#x\r\n", pBuffer[i]); SYS_CONSOLE_MESSAGE(buf); } #endif }
/***************************************************************************** * FUNCTION: RawGetByte * * RETURNS: error code * * PARAMS: * rawId - RAW ID * pBuffer - Buffer to read bytes into * length - number of bytes to read * * NOTES: Reads bytes from the RAW engine *****************************************************************************/ void RawGetByte(uint16_t rawId, uint8_t *pBuffer, uint16_t length) { uint8_t regId; #if defined(OUTPUT_RAW_TX_RX) uint16_t i; #endif /* if reading a data message do following check */ if (!g_WaitingForMgmtResponse) { // if RAW index previously set out of range and caller is trying to do illegal read if ( (rawId==RAW_RX_ID) && g_rxIndexSetBeyondBuffer && (GetRawWindowState(RAW_RX_ID) == WF_RAW_DATA_MOUNTED) ) { SYS_ASSERT(false, ""); /* attempting to read past end of RAW buffer */ } } regId = (rawId==RAW_ID_0)?RAW_0_DATA_REG:RAW_1_DATA_REG; ReadWFArray(regId, pBuffer, length); #if defined(OUTPUT_RAW_TX_RX) for (i = 0; i < length; ++i) { char buf[16]; sprintf(buf,"R: %#x\r\n", pBuffer[i]); SYS_CONSOLE_MESSAGE(buf); } #endif }
/***************************************************************************** * FUNCTION: RawSetByte * * RETURNS: None * * PARAMS: * rawId - RAW ID * pBuffer - Buffer containing bytes to write * length - number of bytes to read * * NOTES: Writes bytes to RAW window *****************************************************************************/ void RawSetByte(UINT16 rawId, UINT8 *pBuffer, UINT16 length) { UINT8 regId; #if defined(OUTPUT_RAW_TX_RX) UINT16 i; #endif /* if previously set index past legal range and now trying to write to RAW engine */ if ( (rawId == 0) && g_rxIndexSetBeyondBuffer && (GetRawWindowState(RAW_TX_ID) == WF_RAW_DATA_MOUNTED) ) { // WF_ASSERT(FALSE); /* attempting to write past end of RAW window */ } /* write RAW data to chip */ regId = (rawId==RAW_ID_0)?RAW_0_DATA_REG:RAW_1_DATA_REG; WriteWFArray(regId, pBuffer, length); #if defined(OUTPUT_RAW_TX_RX) for (i = 0; i < length; ++i) { char buf[16]; sprintf(buf,"T: %#x\r\n", pBuffer[i]); putsUART(buf); } #endif }
/***************************************************************************** * FUNCTION: RawGetByte * * RETURNS: error code * * PARAMS: * rawId - RAW ID * pBuffer - Buffer to read bytes into * length - number of bytes to read * * NOTES: Reads bytes from the RAW engine *****************************************************************************/ void RawGetByte(UINT16 rawId, UINT8 *pBuffer, UINT16 length) { UINT8 regId; #if defined(OUTPUT_RAW_TX_RX) UINT16 i; #endif /* if reading a data message do following check */ if (!g_MgmtResponseInProgress) { // if RAW index previously set out of range and caller is trying to do illegal read if ( (rawId==RAW_RX_ID) && g_rxIndexSetBeyondBuffer && (GetRawWindowState(RAW_RX_ID) == WF_RAW_DATA_MOUNTED) ) { WF_ASSERT(FALSE); /* attempting to read past end of RAW buffer */ } } regId = (rawId==RAW_ID_0)?RAW_0_DATA_REG:RAW_1_DATA_REG; ReadWFArray(regId, pBuffer, length); #if defined(OUTPUT_RAW_TX_RX) for (i = 0; i < length; ++i) { char buf[16]; sprintf(buf,"R: %#x\r\n", pBuffer[i]); putsUART(buf); } #endif }
/***************************************************************************** * FUNCTION: SendMgmtMsg * * RETURNS: error code * * PARAMS: p_header -- pointer to mgmt message header data * headerLength -- number of bytes in the header * will be written * p_data -- pointer to mgmt message data * dataLength -- number of byte of data * * NOTES: Sends a management message *****************************************************************************/ void SendMgmtMsg(UINT8 *p_header, UINT8 headerLength, UINT8 *p_data, UINT8 dataLength) { #if defined(__18CXX) static UINT32 startTickCount; static UINT32 maxAllowedTicks; #else UINT32 startTickCount; UINT32 maxAllowedTicks; #endif /* cannot send management messages while in WF_ProcessEvent() */ WF_ASSERT(!isInWFProcessEvent()); EnsureWFisAwake(); /* if a Rx Data packet is mounted that has not yet been processed */ if (GetRawWindowState(RAW_RX_ID) == WF_RAW_DATA_MOUNTED) { /* save it, so after mgmt response received it can be restored */ PushRawWindow(RAW_RX_ID); RestoreRxData = TRUE; } /* mounts a tx mgmt buffer on the MRF24W when data tx is done */ maxAllowedTicks = TICKS_PER_SECOND / 200; /* 5 ms timeout */ startTickCount = (UINT32)TickGet(); while (!WFisTxMgmtReady() ) { MACProcess(); /* DEBUG -- REMOVE AFTER FIGURE OUT WHY TIMING OUT (RARELY HAPPENS) */ if (TickGet() - startTickCount >= maxAllowedTicks) { /* force flags so WFisTxMgmtReady will return TRUE */ SetRawWindowState(RAW_TX_ID, WF_RAW_UNMOUNTED); RawWindowReady[RAW_TX_ID] = FALSE; } } /* write out management header */ RawSetByte(RAW_TX_ID, p_header, headerLength); /* write out data (if any) */ if (dataLength > 0) { RawSetByte(RAW_TX_ID, p_data, dataLength); } /* send mgmt msg to MRF24W */ SendRAWManagementFrame(headerLength + dataLength); }
/***************************************************************************** Function: void DeallocateDataRxBuffer(void) Summary: Deallocates a Data Rx buffer Description: Typically called by MACGetHeader(), the assumption being that when the stack is checking for a newly received data message it is finished with the previously received data message. Also called by MACGetHeader() if the SNAP header is invalid and the packet is thrown away. Precondition: None Parameters: None Returns: None Remarks: None *****************************************************************************/ void DeallocateDataRxBuffer(void) { WF_ASSERT(GetRawWindowState(RAW_DATA_RX_ID) == WF_RAW_DATA_MOUNTED); /* perform deallocation of raw rx buffer */ RawMove(RAW_DATA_RX_ID, RAW_DATA_POOL, FALSE, 0); /* set state flag */ SetRawWindowState(RAW_DATA_RX_ID, WF_RAW_UNMOUNTED); /* the current length of the Rx data packet is now 0 (used by MAC) */ SetRxDataPacketLength(0); }