#define emacFULL_DUPLEX_ENABLED ( 0x0004 )\r
#define emac10BASE_T_MODE ( 0x0002 )\r
\r
+/* If no buffers are available, then wait this long before looking again.... */\r
+#define emacBUFFER_WAIT_DELAY ( 3 / portTICK_RATE_MS )\r
+\r
+/* ...and don't look more than this many times. */\r
+#define emacBUFFER_WAIT_ATTEMPTS ( 30 )\r
+\r
+#define emacTX_DESC_INDEX ( 0 )\r
+\r
/* The semaphore used to wake the uIP task when data arives. */\r
-xSemaphoreHandle xEMACSemaphore = NULL;\r
+extern xSemaphoreHandle xEMACSemaphore;\r
\r
static unsigned short *rptr;\r
static unsigned short *tptr;\r
static void prvSetupEMACHardware( void );\r
static void prvConfigurePHY( void );\r
static long prvSetupLinkStatus( void );\r
+static unsigned char *prvGetNextBuffer( void );\r
+static void prvReturnBuffer( unsigned char *pucBuffer );\r
+\r
+/* Each ucBufferInUse index corresponds to a position in the same index in the\r
+ucMACBuffers array. If the index contains a 1 then the buffer within\r
+ucMACBuffers is in use, if it contains a 0 then the buffer is free. */\r
+static unsigned char ucBufferInUse[ ETH_NUM_BUFFERS ] = { pdFALSE };\r
+\r
+/* The uip_buffer is not a fixed array, but instead gets pointed to the buffers\r
+allocated within this file. */\r
+unsigned char * uip_buf;\r
+\r
+/* Store the length of the data being sent so the data can be sent twice. The\r
+value will be set back to 0 once the data has been sent twice. */\r
+static unsigned short usSendLen = 0;\r
\r
/*-----------------------------------------------------------*/\r
\r
}\r
/*-----------------------------------------------------------*/\r
\r
-unsigned short read_PHY( unsigned char ucPhyReg, portBASE_TYPE *pxStatus )\r
+unsigned short read_PHY( unsigned char ucPhyReg, long *plStatus )\r
{\r
long x;\r
const long lMaxTime = 10;\r
\r
if( x >= lMaxTime )\r
{\r
- *pxStatus = pdFAIL;\r
+ *plStatus = pdFAIL;\r
}\r
\r
return( MAC_MRDD );\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static void prvInitDescriptors( void )\r
+static unsigned char *prvGetNextBuffer( void )\r
{\r
long x;\r
+unsigned char *pucReturn = NULL;\r
+unsigned long ulAttempts = 0;\r
+\r
+ while( pucReturn == NULL )\r
+ {\r
+ /* Look through the buffers to find one that is not in use by\r
+ anything else. */\r
+ for( x = 0; x < ETH_NUM_BUFFERS; x++ )\r
+ {\r
+ if( ucBufferInUse[ x ] == pdFALSE )\r
+ {\r
+ ucBufferInUse[ x ] = pdTRUE;\r
+ pucReturn = ( unsigned char * ) ETH_BUF( x );\r
+ break;\r
+ }\r
+ }\r
+\r
+ /* Was a buffer found? */\r
+ if( pucReturn == NULL )\r
+ {\r
+ ulAttempts++;\r
+\r
+ if( ulAttempts >= emacBUFFER_WAIT_ATTEMPTS )\r
+ {\r
+ break;\r
+ }\r
+\r
+ /* Wait then look again. */\r
+ vTaskDelay( emacBUFFER_WAIT_DELAY );\r
+ }\r
+ }\r
+\r
+ return pucReturn;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvInitDescriptors( void )\r
+{\r
+long x, lNextBuffer = 0;\r
\r
for( x = 0; x < NUM_RX_FRAG; x++ )\r
{\r
- RX_DESC_PACKET( x ) = RX_BUF( x );\r
+ /* Allocate the next Ethernet buffer to this descriptor. */\r
+ RX_DESC_PACKET( x ) = ETH_BUF( lNextBuffer );\r
RX_DESC_CTRL( x ) = RCTRL_INT | ( ETH_FRAG_SIZE - 1 );\r
RX_STAT_INFO( x ) = 0;\r
RX_STAT_HASHCRC( x ) = 0;\r
+\r
+ /* The Ethernet buffer is now in use. */\r
+ ucBufferInUse[ lNextBuffer ] = pdTRUE;\r
+ lNextBuffer++;\r
}\r
\r
/* Set EMAC Receive Descriptor Registers. */\r
/* Rx Descriptors Point to 0 */\r
MAC_RXCONSUMEINDEX = 0;\r
\r
+ /* A buffer is not allocated to the Tx descriptors until they are actually\r
+ used. */\r
for( x = 0; x < NUM_TX_FRAG; x++ )\r
{\r
- TX_DESC_PACKET( x ) = TX_BUF( x );\r
+ TX_DESC_PACKET( x ) = NULL;\r
TX_DESC_CTRL( x ) = 0;\r
TX_STAT_INFO( x ) = 0;\r
}\r
static void prvSetupEMACHardware( void )\r
{\r
unsigned short us;\r
-long x;\r
+long x, lDummy;\r
\r
/* Enable P1 Ethernet Pins. */\r
PINSEL2 = emacPINSEL2_VALUE;\r
for( x = 0; x < 100; x++ )\r
{\r
vTaskDelay( emacSHORT_DELAY * 5 );\r
- us = read_PHY( PHY_REG_BMCR, &us );\r
+ us = read_PHY( PHY_REG_BMCR, &lDummy );\r
if( !( us & MCFG_RES_MII ) )\r
{\r
/* Reset complete */\r
static void prvConfigurePHY( void )\r
{\r
unsigned short us;\r
-long x;\r
+long x, lDummy;\r
\r
/* Auto negotiate the configuration. */\r
if( write_PHY( PHY_REG_BMCR, PHY_AUTO_NEG ) )\r
\r
for( x = 0; x < 10; x++ )\r
{\r
- us = read_PHY( PHY_REG_BMSR, &us );\r
+ us = read_PHY( PHY_REG_BMSR, &lDummy );\r
\r
if( us & PHY_AUTO_NEG_COMPLETE )\r
{\r
}\r
/*-----------------------------------------------------------*/\r
\r
-portBASE_TYPE Init_EMAC( void )\r
+long Init_EMAC( void )\r
{\r
-portBASE_TYPE xReturn = pdPASS;\r
+long lReturn = pdPASS;\r
volatile unsigned long regv, tout;\r
unsigned long ulID1, ulID2;\r
\r
prvSetupEMACHardware();\r
\r
/* Check if connected to a DP83848C PHY. */\r
- ulID1 = read_PHY( PHY_REG_IDR1, &xReturn );\r
- ulID2 = read_PHY( PHY_REG_IDR2, &xReturn );\r
+ ulID1 = read_PHY( PHY_REG_IDR1, &lReturn );\r
+ ulID2 = read_PHY( PHY_REG_IDR2, &lReturn );\r
if( ( (ulID1 << 16UL ) | ( ulID2 & 0xFFF0UL ) ) == DP83848C_ID )\r
{\r
/* Set the Ethernet MAC Address registers */\r
/* Receive Broadcast and Perfect Match Packets */\r
MAC_RXFILTERCTRL = RFC_UCAST_EN | RFC_BCAST_EN | RFC_PERFECT_EN;\r
\r
- /* Create the semaphore used to wake the uIP task. */\r
- vSemaphoreCreateBinary( xEMACSemaphore );\r
-\r
/* Setup the PHY. */\r
prvConfigurePHY();\r
}\r
else\r
{\r
- xReturn = pdFAIL;\r
+ lReturn = pdFAIL;\r
}\r
\r
/* Check the link status. */\r
- if( xReturn == pdPASS )\r
+ if( lReturn == pdPASS )\r
{\r
- xReturn = prvSetupLinkStatus();\r
+ lReturn = prvSetupLinkStatus();\r
}\r
\r
- if( xReturn == pdPASS )\r
+ if( lReturn == pdPASS )\r
{\r
+ /* Initialise uip_buf to ensure it points somewhere valid. */\r
+ uip_buf = prvGetNextBuffer();\r
+\r
/* Reset all interrupts */\r
- MAC_INTCLEAR = 0xFFFF;\r
+ MAC_INTCLEAR = ( INT_RX_OVERRUN | INT_RX_ERR | INT_RX_FIN | INT_RX_DONE | INT_TX_UNDERRUN | INT_TX_ERR | INT_TX_FIN | INT_TX_DONE | INT_SOFT_INT | INT_WAKEUP );\r
\r
/* Enable receive and transmit mode of MAC Ethernet core */\r
MAC_COMMAND |= ( CR_RX_EN | CR_TX_EN );\r
MAC_MAC1 |= MAC1_REC_EN;\r
}\r
\r
- return xReturn;\r
+ return lReturn;\r
}\r
/*-----------------------------------------------------------*/\r
\r
-// reads a word in little-endian byte order from RX_BUFFER\r
-unsigned short ReadFrame_EMAC( void )\r
+static void prvReturnBuffer( unsigned char *pucBuffer )\r
{\r
- return( *rptr++ );\r
-}\r
+unsigned long ul;\r
\r
-// copies bytes from frame port to MCU-memory\r
-// NOTES: * an odd number of byte may only be transfered\r
-// if the frame is read to the end!\r
-// * MCU-memory MUST start at word-boundary\r
-void CopyFromFrame_EMAC( void *Dest, unsigned short Size )\r
-{\r
- unsigned short *piDest; // Keil: Pointer added to correct expression\r
- piDest = Dest; // Keil: Line added\r
- while( Size > 1 )\r
+ /* Mark a buffer as free for use. */\r
+ for( ul = 0; ul < ETH_NUM_BUFFERS; ul++ )\r
{\r
- *piDest++ = ReadFrame_EMAC();\r
- Size -= 2;\r
+ if( ETH_BUF( ul ) == ( unsigned long ) pucBuffer )\r
+ {\r
+ ucBufferInUse[ ul ] = pdFALSE;\r
+ break;\r
+ }\r
}\r
-\r
- if( Size )\r
- { // check for leftover byte...\r
- *( unsigned char * ) piDest = ( char ) ReadFrame_EMAC(); // the LAN-Controller will return 0\r
- } // for the highbyte\r
}\r
+/*-----------------------------------------------------------*/\r
\r
-\r
-// Reads the length of the received ethernet frame and checks if the\r
-// destination address is a broadcast message or not\r
-// returns the frame length\r
-unsigned short StartReadFrame( void )\r
+unsigned long ulGetEMACRxData( void )\r
{\r
- unsigned short RxLen;\r
- unsigned int idx;\r
+unsigned long ulLen = 0;\r
+long lIndex;\r
\r
- idx = MAC_RXCONSUMEINDEX;\r
- RxLen = ( RX_STAT_INFO(idx) & RINFO_SIZE ) - 3;\r
- rptr = ( unsigned short * ) RX_DESC_PACKET( idx );\r
- return( RxLen );\r
-}\r
+ if( MAC_RXPRODUCEINDEX != MAC_RXCONSUMEINDEX )\r
+ {\r
+ /* Mark the current buffer as free as uip_buf is going to be set to\r
+ the buffer that contains the received data. */\r
+ prvReturnBuffer( uip_buf );\r
\r
-void EndReadFrame( void )\r
-{\r
- unsigned int idx;\r
+ ulLen = ( RX_STAT_INFO( MAC_RXCONSUMEINDEX ) & RINFO_SIZE ) - 3;\r
+ uip_buf = ( unsigned char * ) RX_DESC_PACKET( MAC_RXCONSUMEINDEX );\r
\r
- /* DMA free packet. */\r
- idx = MAC_RXCONSUMEINDEX;\r
+ /* Allocate a new buffer to the descriptor. */\r
+ RX_DESC_PACKET( MAC_RXCONSUMEINDEX ) = ( unsigned long ) prvGetNextBuffer();\r
\r
- if( ++idx == NUM_RX_FRAG )\r
- {\r
- idx = 0;\r
+ /* Move the consume index onto the next position, ensuring it wraps to\r
+ the beginning at the appropriate place. */\r
+ lIndex = MAC_RXCONSUMEINDEX;\r
+\r
+ lIndex++;\r
+ if( lIndex >= NUM_RX_FRAG )\r
+ {\r
+ lIndex = 0;\r
+ }\r
+\r
+ MAC_RXCONSUMEINDEX = lIndex;\r
}\r
\r
- MAC_RXCONSUMEINDEX = idx;\r
+ return ulLen;\r
}\r
+/*-----------------------------------------------------------*/\r
\r
-unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
+void vSendEMACTxData( unsigned short usTxDataLen )\r
{\r
- unsigned int uiLen = 0;\r
+unsigned long ulAttempts = 0UL;\r
\r
- if( MAC_RXPRODUCEINDEX != MAC_RXCONSUMEINDEX )\r
+ /* Check to see if the Tx descriptor is free, indicated by its buffer being\r
+ NULL. */\r
+ while( TX_DESC_PACKET( emacTX_DESC_INDEX ) != NULL )\r
{\r
- uiLen = StartReadFrame();\r
- CopyFromFrame_EMAC( ucBuffer, uiLen );\r
- EndReadFrame();\r
+ /* Wait for the Tx descriptor to become available. */\r
+ vTaskDelay( emacBUFFER_WAIT_DELAY );\r
+\r
+ ulAttempts++;\r
+ if( ulAttempts > emacBUFFER_WAIT_ATTEMPTS )\r
+ {\r
+ /* Something has gone wrong as the Tx descriptor is still in use.\r
+ Clear it down manually, the data it was sending will probably be\r
+ lost. */\r
+ prvReturnBuffer( ( unsigned char * ) TX_DESC_PACKET( emacTX_DESC_INDEX ) );\r
+ break;\r
+ }\r
}\r
\r
- return uiLen;\r
+ /* Setup the Tx descriptor for transmission. Remember the length of the\r
+ data being sent so the second descriptor can be used to send it again from\r
+ within the ISR. */\r
+ usSendLen = usTxDataLen;\r
+ TX_DESC_PACKET( emacTX_DESC_INDEX ) = ( unsigned long ) uip_buf;\r
+ TX_DESC_CTRL( emacTX_DESC_INDEX ) = ( usTxDataLen | TCTRL_LAST | TCTRL_INT );\r
+ MAC_TXPRODUCEINDEX = ( emacTX_DESC_INDEX + 1 );\r
+\r
+ /* uip_buf is being sent by the Tx descriptor. Allocate a new buffer. */\r
+ uip_buf = prvGetNextBuffer();\r
}\r
+/*-----------------------------------------------------------*/\r
\r
-// requests space in EMAC memory for storing an outgoing frame\r
-void RequestSend( void )\r
-{\r
- unsigned int idx;\r
\r
- idx = MAC_TXPRODUCEINDEX;\r
- tptr = ( unsigned short * ) TX_DESC_PACKET( idx );\r
-}\r
+static char c[ 256 ] = { 0 };\r
+static int i = 0;\r
\r
-// writes a word in little-endian byte order to TX_BUFFER\r
-void WriteFrame_EMAC( unsigned short Data )\r
-{\r
- *tptr++ = Data;\r
-}\r
\r
-// copies bytes from MCU-memory to frame port\r
-// NOTES: * an odd number of byte may only be transfered\r
-// if the frame is written to the end!\r
-// * MCU-memory MUST start at word-boundary\r
-void CopyToFrame_EMAC( void *Source, unsigned int Size )\r
+void vEMAC_ISR( void )\r
{\r
- unsigned short *piSource;\r
+unsigned long ulStatus;\r
+portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
\r
- piSource = Source;\r
- Size = ( Size + 1 ) & 0xFFFE; // round Size up to next even number\r
- while( Size > 0 )\r
- {\r
- WriteFrame_EMAC( *piSource++ );\r
- Size -= 2;\r
- }\r
-}\r
+ ulStatus = MAC_INTSTATUS;\r
\r
-void DoSend_EMAC( unsigned short FrameSize )\r
-{\r
- unsigned int idx;\r
+ /* Clear the interrupt. */\r
+ MAC_INTCLEAR = ulStatus;\r
\r
- idx = MAC_TXPRODUCEINDEX;\r
- TX_DESC_CTRL( idx ) = FrameSize | TCTRL_LAST;\r
- if( ++idx == NUM_TX_FRAG )\r
+ if( ulStatus & INT_RX_DONE )\r
{\r
- idx = 0;\r
+ /* Ensure the uIP task is not blocked as data has arrived. */\r
+ xSemaphoreGiveFromISR( xEMACSemaphore, &xHigherPriorityTaskWoken );\r
}\r
\r
- MAC_TXPRODUCEINDEX = idx;\r
-}\r
-\r
-void vEMAC_ISR( void )\r
-{\r
- portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
-\r
- /* Clear the interrupt. */\r
- MAC_INTCLEAR = 0xffff;\r
+ if( ulStatus & INT_TX_DONE )\r
+ {\r
+ if( usSendLen > 0 )\r
+ {\r
+if( i < 255 )\r
+ c[ i++ ] = 1;\r
+ /* Send the data again, using the second descriptor. As there are\r
+ only two descriptors the index is set back to 0. */\r
+ TX_DESC_PACKET( ( emacTX_DESC_INDEX + 1 ) ) = TX_DESC_PACKET( emacTX_DESC_INDEX );\r
+ TX_DESC_CTRL( ( emacTX_DESC_INDEX + 1 ) ) = ( usSendLen | TCTRL_LAST | TCTRL_INT );\r
+ MAC_TXPRODUCEINDEX = ( emacTX_DESC_INDEX );\r
+\r
+ /* This is the second Tx so set usSendLen to 0 to indicate that the\r
+ Tx descriptors will be free again. */\r
+ usSendLen = 0UL;\r
+ }\r
+ else\r
+ {\r
+if( i < 255 )\r
+ c[ i++ ] = 1;\r
\r
- /* Ensure the uIP task is not blocked as data has arrived. */\r
- xSemaphoreGiveFromISR( xEMACSemaphore, &xHigherPriorityTaskWoken );\r
+ /* The Tx buffer is no longer required. */\r
+ prvReturnBuffer( ( unsigned char * ) TX_DESC_PACKET( emacTX_DESC_INDEX ) );\r
+ TX_DESC_PACKET( emacTX_DESC_INDEX ) = NULL;\r
+ }\r
+ }\r
\r
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
}\r
\r
/* Demo includes. */\r
#include "EthDev_LPC17xx.h"\r
-#include "LED.h"\r
+#include "EthDev.h"\r
+#include "ParTest.h"\r
\r
#include "LPC17xx.h"\r
#include "core_cm3.h"\r
\r
/*-----------------------------------------------------------*/\r
\r
-/*\r
- * Send the uIP buffer to the MAC.\r
- */\r
-static void prvENET_Send(void);\r
-\r
/*\r
* Setup the MAC address in the MAC itself, and in the uIP stack.\r
*/\r
/*-----------------------------------------------------------*/\r
\r
/* The semaphore used by the ISR to wake the uIP task. */\r
-extern xSemaphoreHandle xEMACSemaphore;\r
+xSemaphoreHandle xEMACSemaphore = NULL;\r
\r
/*-----------------------------------------------------------*/\r
\r
\r
( void ) pvParameters;\r
\r
- /* Create the semaphore used by the ISR to wake this task. */\r
- vSemaphoreCreateBinary( xEMACSemaphore );\r
-\r
/* Initialise the uIP stack. */\r
timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
uip_setnetmask( xIPAddr );\r
httpd_init();\r
\r
+ /* Create the semaphore used to wake the uIP task. */\r
+ vSemaphoreCreateBinary( xEMACSemaphore );\r
+\r
/* Initialise the MAC. */\r
while( Init_EMAC() != pdPASS )\r
{\r
\r
portENTER_CRITICAL();\r
{\r
- ETH_INTENABLE = INT_RX_DONE;\r
+ ETH_INTENABLE = ( INT_RX_DONE | INT_TX_DONE );\r
/* set the interrupt priority */\r
NVIC_SetPriority( ENET_IRQn, configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
/* enable the interrupt */\r
for( ;; )\r
{\r
/* Is there received data ready to be processed? */\r
- uip_len = uiGetEMACRxData( uip_buf );\r
+ uip_len = ulGetEMACRxData();\r
\r
- if( uip_len > 0 )\r
+ if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
{\r
/* Standard uIP loop taken from the uIP manual. */\r
if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
if( uip_len > 0 )\r
{\r
uip_arp_out();\r
- prvENET_Send();\r
+ vSendEMACTxData( uip_len );\r
}\r
}\r
else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
uip_len is set to a value > 0. */\r
if( uip_len > 0 )\r
{\r
- prvENET_Send();\r
+ vSendEMACTxData( uip_len );\r
}\r
}\r
}\r
else\r
{\r
- if( timer_expired( &periodic_timer ) )\r
+ if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )\r
{\r
timer_reset( &periodic_timer );\r
for( i = 0; i < UIP_CONNS; i++ )\r
if( uip_len > 0 )\r
{\r
uip_arp_out();\r
- prvENET_Send();\r
+ vSendEMACTxData( uip_len );\r
}\r
}\r
\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static void prvENET_Send(void)\r
-{\r
- RequestSend();\r
-\r
- /* Copy the header into the Tx buffer. */\r
- CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
- if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
- {\r
- CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
- }\r
-\r
- DoSend_EMAC( uip_len );\r
-\r
- RequestSend();\r
-\r
- /* Copy the header into the Tx buffer. */\r
- CopyToFrame_EMAC( uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
- if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
- {\r
- CopyToFrame_EMAC( uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
- }\r
-\r
- DoSend_EMAC( uip_len );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
static void prvSetMACAddress( void )\r
{\r
struct uip_eth_addr xAddr;\r