Esempio n. 1
0
void ENET_IRQHandler (void)
{
    uint32_t int_stat;

    EMAC_PACKETBUF_Type RxDatbuf = {
        .pbDataBuf = uip_buf,
    };

    // Get EMAC interrupt status
    while ((int_stat = (LPC_EMAC->IntStatus & LPC_EMAC->IntEnable)) != 0) {
            LPC_EMAC->IntClear = int_stat; /*clear interrupt*/
           
            if ((int_stat & EMAC_INT_RX_DONE))
            {
                if (EMAC_CheckReceiveDataStatus(EMAC_RINFO_ERR_MASK)) {
                    /*error*/
                    goto rel;
                }
                /*copy frame to uip buffer*/
                uip_len = RxDatbuf.ulDataLen = EMAC_GetReceiveDataSize() - 3;
                EMAC_ReadPacketBuffer(&RxDatbuf);
                packet_received = 1;
            rel:
                /*release frame from EMAC buffer*/
                EMAC_UpdateRxConsumeIndex();
            }
    }
}
Esempio n. 2
0
/* receive an Ethernet frame from MAC/DMA controller */
UNS_32 tapdev_read(void * pPacket)
{
	UNS_32 Size = EMAC_MAX_PACKET_SIZE;
	UNS_32 in_size;
	EMAC_PACKETBUF_Type RxPack;

	// Check Receive status
	if (EMAC_CheckReceiveIndex() == FALSE){
		return (0);
	}

	// Get size of receive data
	in_size = EMAC_GetReceiveDataSize() + 1;

	Size = MIN(Size,in_size);

	// Setup Rx packet
	RxPack.pbDataBuf = (uint32_t *)pPacket;
	RxPack.ulDataLen = Size;
	EMAC_ReadPacketBuffer(&RxPack);

	// update receive status
	EMAC_UpdateRxConsumeIndex();
	return(Size);
}
Esempio n. 3
0
/**
 * @brief Receive Data Packet Done Callback function.
 * Called by EMAC Standard Interrupt handler in EMAC driver
 *
 * Note: All packets are greater than (TX_PACKET_SIZE + 4)
 * will be ignore!
 */
void RxDone_UsrCBS(void)
{
	EMAC_PACKETBUF_Type RxDatbuf;
	uint32_t RxLen;

	/* Packet received, check if packet is valid. */
	if (EMAC_CheckReceiveIndex()){
		if (!EMAC_CheckReceiveDataStatus(EMAC_RINFO_LAST_FLAG)){
			goto rel;
		}
		// Get data size, trip out 4-bytes CRC field, note that length in (-1) style format
		RxLen = EMAC_GetReceiveDataSize() - 3;
		// Note that packet added 4-bytes CRC created by yourself
		if ((RxLen > (TX_PACKET_SIZE + 4)) || (EMAC_CheckReceiveDataStatus(EMAC_RINFO_ERR_MASK))) {
			/* Invalid frame, ignore it and free buffer */
			goto rel;
		}
		ReceiveLength = RxLen;
		// Valid Frame, just copy it
		RxDatbuf.pbDataBuf = (uint32_t *)gRxBuf;
		RxDatbuf.ulDataLen = RxLen;
		EMAC_ReadPacketBuffer(&RxDatbuf);
		PacketReceived = TRUE;

rel:
		/* Release frame from EMAC buffer */
		EMAC_UpdateRxConsumeIndex();
	}
	_DBG_("Rx done");
	RxDoneCount++;
}
Esempio n. 4
0
/**
 * Should allocate a pbuf and transfer the bytes of the incoming
 * packet from the interface into the pbuf.
 *
 * @param netif the lwip network interface structure for this lpc17xx_if
 * @return a pbuf filled with the received packet (including MAC header)
 *         NULL on memory error
 */
static struct pbuf *
low_level_input(struct netif *netif)
{
  struct pbuf *p, *q;
  u16_t len;

  if (EMAC_CheckReceiveIndex() == FALSE)
    return NULL;

  /* Obtain the size of the packet and put it into the "len"
     variable. */
  len = EMAC_GetReceiveDataSize() + 1;

#if ETH_PAD_SIZE
  len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
#endif

  /* We allocate a pbuf chain of pbufs from the pool. */
  p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  
  if (p != NULL) {

#if ETH_PAD_SIZE
    pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endif

    /* We iterate over the pbuf chain until we have read the entire
     * packet into the pbuf. */
    for(q = p; q != NULL; q = q->next) {
      /* Read enough bytes to fill this pbuf in the chain. The
       * available data in the pbuf is given by the q->len
       * variable.
       * This does not necessarily have to be a memcpy, you can also preallocate
       * pbufs for a DMA-enabled MAC and after receiving truncate it to the
       * actually received size. In this case, ensure the tot_len member of the
       * pbuf is the sum of the chained pbuf len members.
       */
      EMAC_PACKETBUF_Type RxPack;
      RxPack.pbDataBuf = q->payload;
      RxPack.ulDataLen = q->len;
      EMAC_ReadPacketBuffer(&RxPack);
    }

#if ETH_PAD_SIZE
    pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif

    LINK_STATS_INC(link.recv);
  } else {
    LINK_STATS_INC(link.memerr);
    LINK_STATS_INC(link.drop);
  }
  EMAC_UpdateRxConsumeIndex();

  return p;  
}
Esempio n. 5
0
/**
 * Should allocate a pbuf and transfer the bytes of the incoming
 * packet from the interface into the pbuf.
 *
 * @param netif the lwip network interface structure for this ethernetif
 * @return a pbuf filled with the received packet (including MAC header)
 *         NULL on memory error
 */
static struct pbuf *
low_level_input(struct netif *netif)
{
  struct pbuf *p, *q;
  u16_t len;

  if(EMAC_GetBufferSts(EMAC_RX_BUFF) == EMAC_BUFF_EMPTY)
    return NULL;
    
  /* Obtain the size of the packet and put it into the "len"
     variable. */
  len = EMAC_GetRxFrameSize();

#if ETH_PAD_SIZE
  len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
#endif

  /* We allocate a pbuf chain of pbufs from the pool. */
  p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  
  if (p != NULL) {

#if ETH_PAD_SIZE
    pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endif

    /* We iterate over the pbuf chain until we have read the entire
     * packet into the pbuf. */
    uint8_t *ptr = (uint8_t *)EMAC_GetRxBuffer();
    for(q = p; q != NULL; q = q->next) {
      MEMCPY(q->payload, ptr, q->len);
      ptr += q->len;
    }
    EMAC_UpdateRxConsumeIndex();

#if ETH_PAD_SIZE
    pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif

    LINK_STATS_INC(link.recv);
    
  } else {
//    drop packet();
    LINK_STATS_INC(link.memerr);
    LINK_STATS_INC(link.drop);
  }
  
  return p;
}
static void prvEMACHandlerTask( void *pvParameters )
{
size_t xDataLength;
const uint16_t usCRCLength = 4;
xNetworkBufferDescriptor_t *pxNetworkBuffer;
xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };

/* This is not included in the header file for some reason. */
extern uint8_t *EMAC_NextPacketToRead( void );

	( void ) pvParameters;
	configASSERT( xEMACRxEventSemaphore );

	for( ;; )
	{
		/* Wait for the EMAC interrupt to indicate that another packet has been
		received.  The while() loop is only needed if INCLUDE_vTaskSuspend is
		set to 0 in FreeRTOSConfig.h. */
		while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );

		/* At least one packet has been received. */
		while( EMAC_CheckReceiveIndex() != FALSE )
		{
			/* Obtain the length, minus the CRC.  The CRC is four bytes
			but the length is already minus 1. */
			xDataLength = ( size_t ) EMAC_GetReceiveDataSize() - ( usCRCLength - 1U );

			if( xDataLength > 0U )
			{
				/* Obtain a network buffer to pass this data into the
				stack.  No storage is required as the network buffer
				will point directly to the buffer that already holds
				the	received data. */
				pxNetworkBuffer = pxNetworkBufferGet( 0, ( portTickType ) 0 );

				if( pxNetworkBuffer != NULL )
				{
					pxNetworkBuffer->pucEthernetBuffer = EMAC_NextPacketToRead();
					pxNetworkBuffer->xDataLength = xDataLength;
					xRxEvent.pvData = ( void * ) pxNetworkBuffer;

					/* Data was received and stored.  Send a message to the IP
					task to let it know. */
					if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )
					{
						vNetworkBufferRelease( pxNetworkBuffer );
						iptraceETHERNET_RX_EVENT_LOST();
					}
				}
				else
				{
					iptraceETHERNET_RX_EVENT_LOST();
				}

				iptraceNETWORK_INTERFACE_RECEIVE();
			}

			/* Release the frame. */
			EMAC_UpdateRxConsumeIndex();
		}
	}
}
Esempio n. 7
0
int _EMAC_UpdateRxConsumeIndex(uint8_t * args)
{
	EMAC_UpdateRxConsumeIndex();
	return 0;
}
Esempio n. 8
0
static void prvEMACDeferredInterruptHandlerTask( void *pvParameters )
{
xNetworkBufferDescriptor_t *pxNetworkBuffer;
xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };

	( void ) pvParameters;
	configASSERT( xEMACRxEventSemaphore );

	for( ;; )
	{
		/* Wait for the EMAC interrupt to indicate that another packet has been
		received.  The while() loop is only needed if INCLUDE_vTaskSuspend is
		set to 0 in FreeRTOSConfig.h.  If INCLUDE_vTaskSuspend is set to 1
		then portMAX_DELAY would be an indefinite block time and
		xSemaphoreTake() would only return when the semaphore was actually
		obtained. */
		while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );

		/* At least one packet has been received. */
		while( EMAC_CheckReceiveIndex() != FALSE )
		{
			/* The buffer filled by the DMA is going to be passed into the IP
			stack.  Allocate another buffer for the DMA descriptor. */
			pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, ( TickType_t ) 0 );

			if( pxNetworkBuffer != NULL )
			{
				/* Swap the buffer just allocated and referenced from the
				pxNetworkBuffer with the buffer that has already been filled by
				the DMA.  pxNetworkBuffer will then hold a reference to the
				buffer that already contains the data without any data having
				been copied between buffers. */
				EMAC_NextPacketToRead( pxNetworkBuffer );

				#if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1
				{
					if( pxNetworkBuffer->xDataLength > 0 )
					{
						/* If the frame would not be processed by the IP stack then
						don't even bother sending it to the IP stack. */
						if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )
						{
							pxNetworkBuffer->xDataLength = 0;
						}
					}
				}
				#endif

				if( pxNetworkBuffer->xDataLength > 0 )
				{
					/* Store a pointer to the network buffer structure in the
					padding	space that was left in front of the Ethernet frame.
					The pointer	is needed to ensure the network buffer structure
					can be located when it is time for it to be freed if the
					Ethernet frame gets	used as a zero copy buffer. */
					*( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;

					/* Data was received and stored.  Send it to the IP task
					for processing. */
					xRxEvent.pvData = ( void * ) pxNetworkBuffer;
					if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )
					{
						/* The buffer could not be sent to the IP task so the
						buffer must be released. */
						vNetworkBufferRelease( pxNetworkBuffer );
						iptraceETHERNET_RX_EVENT_LOST();
					}
					else
					{
						iptraceNETWORK_INTERFACE_RECEIVE();
					}
				}
				else
				{
					/* The buffer does not contain any data so there is no
					point sending it to the IP task.  Just release it. */
					vNetworkBufferRelease( pxNetworkBuffer );
					iptraceETHERNET_RX_EVENT_LOST();
				}
			}
			else
			{
				iptraceETHERNET_RX_EVENT_LOST();
			}

			/* Release the descriptor. */
			EMAC_UpdateRxConsumeIndex();
		}
	}
}
Esempio n. 9
0
void ENET_IRQHandler( void )
{
uint32_t ulInterruptCause;
extern volatile portBASE_TYPE xInsideISR;
struct pbuf *p;
struct eth_hdr *pxHeader;

	xInsideISR++;

	while( ( ulInterruptCause = LPC_EMAC->IntStatus ) != 0 )
	{
		/* Clear the interrupt. */
		LPC_EMAC->IntClear = ulInterruptCause;

		/* Clear fatal error conditions.  */
		if( ( ulInterruptCause & EMAC_INT_TX_UNDERRUN ) != 0U )
		{
			LPC_EMAC->Command |= EMAC_CR_TX_RES;
		}

		if( ( ulInterruptCause & EMAC_INT_RX_DONE ) != 0UL )
		{
			/* A packet has been received. */
			if( EMAC_CheckReceiveIndex() != FALSE )
			{
				if( EMAC_CheckReceiveDataStatus( EMAC_RINFO_LAST_FLAG ) == SET )
				{
					/* move received packet into a new pbuf */
					p = prvLowLevelInput();

					/* no packet could be read, silently ignore this */
					if( p != NULL )
					{
						/* points to packet payload, which starts with an Ethernet header */
						pxHeader = p->payload;

						switch( htons( pxHeader->type ) )
						{
							/* IP or ARP packet? */
							case ETHTYPE_IP:
							case ETHTYPE_ARP:
												/* Full packet send to tcpip_thread to process */
												configASSERT( pxNetIfInUse );
												if( pxNetIfInUse->input( p, pxNetIfInUse ) != ERR_OK )
												{
													LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
													pbuf_free(p);
													p = NULL;
												}
												break;

							default:
												pbuf_free( p );
												p = NULL;
							break;
						}
					}
					else
					{
						configASSERT( ( volatile void * ) NULL );
					}
				}

				/* Release the frame. */
				EMAC_UpdateRxConsumeIndex();
			}
		}

		if( ( ulInterruptCause & EMAC_INT_TX_DONE ) != 0UL )
		{
			/* Nothing to do here. */
		}
	}

	xInsideISR--;
}
Esempio n. 10
0
// Release the buffer after reading all the content inside
void EndReadFrame(void) {
	// just call EMAC_UpdateConsumeIndex() in EMAC driver
	EMAC_UpdateRxConsumeIndex();
}
Esempio n. 11
0
/*********************************************************************//**
 * @brief		Ethernet service routine handler
 * @param[in]	none
 * @return 		none
 **********************************************************************/
void ENET_IRQHandler (void)
{
	EMAC_PACKETBUF_Type RxDatbuf;
	uint32_t RxLen;

	/* EMAC Ethernet Controller Interrupt function. */
	uint32_t int_stat;
	// Get EMAC interrupt status
	while ((int_stat = (LPC_EMAC->IntStatus & LPC_EMAC->IntEnable)) != 0) {
		// Clear interrupt status
		LPC_EMAC->IntClear = int_stat;
		/* scan interrupt status source */

		/* ---------- receive overrun ------------*/
		if((int_stat & EMAC_INT_RX_OVERRUN))
		{
			RXOverrunCount++;
			_DBG_("Rx overrun");
		}

		/*-----------  receive error -------------*/
		/* Note:
		 * The EMAC doesn't distinguish the frame type and frame length,
		 * so, e.g. when the IP(0x8000) or ARP(0x0806) packets are received,
		 * it compares the frame type with the max length and gives the
		 * "Range" error. In fact, this bit is not an error indication,
		 * but simply a statement by the chip regarding the status of
		 * the received frame
		 */
		if ((int_stat & EMAC_INT_RX_ERR))
		{
			if (EMAC_CheckReceiveDataStatus(EMAC_RINFO_RANGE_ERR) == RESET){
				RXErrorCount++;
				_DBG_("Rx error: ");
			}
		}

		/* ---------- RX Finished Process Descriptors ----------*/
		if ((int_stat & EMAC_INT_RX_FIN))
		{
			RxFinishedCount++;
			_DBG_("Rx finish");
		}

		/* ---------- Receive Done -----------------------------*/
		/* Note: All packets are greater than (TX_PACKET_SIZE + 4)
		 * will be ignore!
		 */
		if ((int_stat & EMAC_INT_RX_DONE))
		{
			/* Packet received, check if packet is valid. */
			if (EMAC_CheckReceiveIndex()){
				if (!EMAC_CheckReceiveDataStatus(EMAC_RINFO_LAST_FLAG)){
					goto rel;
				}
				// Get data size, trip out 4-bytes CRC field, note that length in (-1) style format
				RxLen = EMAC_GetReceiveDataSize() - 3;
				// Note that packet added 4-bytes CRC created by yourself
				if ((RxLen > (TX_PACKET_SIZE + 4)) || (EMAC_CheckReceiveDataStatus(EMAC_RINFO_ERR_MASK))) {
					/* Invalid frame, ignore it and free buffer */
					goto rel;
				}
				ReceiveLength = RxLen;
				// Valid Frame, just copy it
				RxDatbuf.pbDataBuf = (uint32_t *)gRxBuf;
				RxDatbuf.ulDataLen = RxLen;
				EMAC_ReadPacketBuffer(&RxDatbuf);
				PacketReceived = TRUE;

		rel:
				/* Release frame from EMAC buffer */
				EMAC_UpdateRxConsumeIndex();
			}
			_DBG_("Rx done");
			RxDoneCount++;
		}

		/*------------------- Transmit Underrun -----------------------*/
		if ((int_stat & EMAC_INT_TX_UNDERRUN))
		{
			TXUnderrunCount++;
			_DBG_("Tx under-run");
		}

		/*------------------- Transmit Error --------------------------*/
		if ((int_stat & EMAC_INT_TX_ERR))
		{
			TXErrorCount++;
			_DBG_("Tx error");
		}

		/* ----------------- TX Finished Process Descriptors ----------*/
		if ((int_stat & EMAC_INT_TX_FIN))
		{
			TxFinishedCount++;
			_DBG_("Tx finish");
		}

		/* ----------------- Transmit Done ----------------------------*/
		if ((int_stat & EMAC_INT_TX_DONE))
		{
			TxDoneCount++;
			_DBG_("Tx done");
		}
#if ENABLE_WOL
		/* ------------------ Wakeup Event Interrupt ------------------*/
		/* Never gone here since interrupt in this
		 * functionality has been disable, even if in wake-up mode
		 */
		if ((int_stat & EMAC_INT_WAKEUP))
		{
			WOLCount++;
		}
#endif
	}
}