/***************************************************************************** Function: bool AllocateDataTxBuffer(uint16_t 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_t bytesNeeded) { uint16_t bufAvail; uint16_t byteCount; /* 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); if (byteCount == 0) { EventEnqueue(WF_EVENT_ERROR, UD_TX_ALLOCATION_FAILED); return false; } /* flag this raw window as mounted (in use) */ SetRawDataWindowState(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: 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) { // TODO: verify data rx is mounted SetRawDataWindowState(RAW_DATA_RX_ID, WF_RAW_UNMOUNTED); /* perform deallocation of raw rx buffer */ RawMove(RAW_DATA_RX_ID, RAW_DATA_POOL, false, 0); }
/* * Initialize RAW (Random Access Window) on MRF24WG */ void RawInit() { // Used in interrupt routine and functions in this module. The reason for // this mechanism is because when waiting for a Raw Move complete interrupt // we need to save the state if any other interrupts occur at the same time so // we don't lose them RawMoveState.rawInterruptMask = 0; // interrupt will write to this RawMoveState.waitingForRawMoveCompleteInterrupt = false; // not waiting for RAW move complete // By default the MRF24WG firmware mounts Scratch to RAW 1 after reset. This // is not being used, so unmount the scratch from this RAW window. ScratchUnmount(RAW_ID_1); /* Permanently mount scratch memory, index defaults to 0. */ /* If one needs to know, this function returns the number of bytes in scratch memory */ ScratchMount(RAW_SCRATCH_ID); SetRawDataWindowState(RAW_DATA_TX_ID, WF_RAW_UNMOUNTED); SetRawDataWindowState(RAW_DATA_RX_ID, WF_RAW_UNMOUNTED); }
/* * Mounts most recent Rx message. * Returns length, a number of bytes in the received message. * * This function mounts the most recent Rx message from the WiFi chip, which * could be either a management or a data message. * * Parameters: * rawId -- RAW ID specifying which raw window to mount the rx packet in. */ uint16_t RawMountRxBuffer(uint8_t rawId) { uint16_t length; length = RawMove(rawId, RAW_MAC, true, 0); // the length should never be 0 if notified of an Rx msg if (length == 0) { EventEnqueue(WF_EVENT_ERROR, UD_ERROR_RAW_RX_MOUNT_FAILED); } /* if mounting a Raw Rx data frame */ if (rawId == RAW_DATA_RX_ID) { /* notify WiFi driver that an Rx data frame is mounted */ SetRawDataWindowState(RAW_DATA_RX_ID, WF_RAW_DATA_MOUNTED); } return length; }