AT91SAM7x IAR LWIP problem

pbeamtn wrote on Tuesday, October 28, 2008:

I’v been working on porting LWIP to FreeRTOS for the AT91SAM7x with IAR EWARM 5.1.  I now have it compiling fine, but LWIP seems to “run away” whenever it receives a packet.  For example, if I send it an ARP request, it continues to send ARP responses.  It seems to not clear the old receive buffer, so it interprets the buffer over and over as a new packet.

The offending code seems to be in ethernetif.c:

    for( ;; )
            ethernetif = xNetIf->state;

            /* move received packet into a new pbuf */
            p = low_level_input( xNetIf );

            if( p == NULL )
                /* No packet could be read.  Wait a for an interrupt to tell us
                there is more data available. */

        } while( p == NULL );

vEMACWaitForInput() is only run if low_level_input() returns null.  I don’t fully understand what low_level_input() does, but it calls ulEMACInputLength() which always returns a length even if a new packet has not been received.

I don’t understand the initial do loop, since it seems to me you would want to do vEMACWaitForInput() and then process what was received instead of trying to receive and only calling vEMACWaitForInput() if that “fails” – which it never does for me after I receive the first packet.

I am hesitant to start changing this code since it seems to work for someone.  This is the stock Rowley AT91SAM7x logic in the demo distribution.  If someone can explain how this is supposed to work and how it determines whether the data is the buffer is new or not, I would certainly appreciate it.

rtel wrote on Wednesday, October 29, 2008:

I would have to read the data sheet again to remember exactly, but from memory…

Each descriptor has an ‘ownership’ bit that says whether the buffer is owned by the software or hardware.  When it is owned by the hardware the MAC peripheral can write data into the buffer pointed to by the descriptor, when it is owned by the software the buffer already contains data that has not yet been processed.

The software maintains an index that indicates which descriptor it is going to read next.  When it reads data from a descriptor it has to first set (or maybe clear) the ownership bit to pass the buffer back to the hardware, and increment the index so it knows which buffer to read next.

It sounds like your setup if failing to do one or other of these.


pbeamtn wrote on Wednesday, October 29, 2008:

I think I know more about what is going on, but I’m not yet sure how to fix it.

I had to set ETH_PAD_SIZE to 2 to get data aligned properly – otherwise, I get data aborts on address checks.  This increases the len returned by ulEMACInputLength() by 2.  This adversely affects vEMACRead() such that the ownership bit is never reset – ulSectionLength is 60 but ulTotalFrameLength is 62 in my case.  It would seem that the length I need to read should be reduced by ETH_PAD_SIZE.

pbeamtn wrote on Wednesday, October 29, 2008:

This is what I had to do to get the demo project to at least respond to pings:

In ethernetif.c, I modified low_level_input() to look like this:

       if( xSemaphoreTake( xRxSemaphore, netifGUARD_BLOCK_TIME ) )
        /* Obtain the size of the packet. */
        len = ulEMACInputLength();
        if( len )
//            #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 + ETH_PAD_SIZE, PBUF_POOL );

Instead of modifying len by adding the size of ETH_PAD_SIZE, I leave it alone and just allocate len + ETH_PAD_SIZE.

I don’t have Rowley CrossStudio to see how the stock demo works, but it worries me to make this kind of change just to convert to IAR.

If someone wants the IAR/lwip AT91SAM7 project I’m working on, then let me know – pbeam at windrock dot com.  I would love to have another set of eyes look at what I did to get this to work.

jon_newcomb wrote on Monday, March 01, 2010:

Bringing up this thread again, has anyone out there successfully ported lwIP to FreeRTOS using the IAR Embedded Workbench?
I guess I’m looking for a kind of BSP for the AT91SAM7X-EK that I can use as a starting point…
(Already looked at IAR uIP and lwIP GCC demos… Also worth noting that the Atmel software pack includes ‘basic-emac-lwip-project’ – but it is again, only for GCC)
arm at jnewcomb d_o_t com