...
Reception using DMA
...
Initialization
Anchor
Then, the network device driver must allocate a list of descriptors and configure each address field to point to the start address of a Receive buffer. At the same time, the network device driver initializes three pointers: one to track the current descriptor, which is expected to contain the next received frame; a second to remember the descriptor list boundaries; and a third for the descriptor list starting address.
Anchor
...
Figure 7-9 Allocation of buffers
...
F7-9(1) The result of Mem_Init()
and the first step in the intialization of the Network Device Driver is the allocation of buffers. Anchor
...
Figure 7-10 Descriptor allocation
F7-10(1) µC/TCP-IP allocates a list of descriptors based on the network device driver configuration and sets each address field to point to the start address of a receive buffer. Anchor
...
Figure 7-11 Reception descriptor pointers initialization
...
F7-11(1) The network device driver initializes three pointers. One to track the current descriptor which is expected to contain the next received frame
...
F7-11(2) A second pointer to remember the descriptor list boundaries.
...
F7-11(3) Finally, the DMA controller is initialized and hardware is informed of the descriptor list starting address.
...
anchor
Reception
...
Figure 7-12 Receiving an Ethernet frame with DMA
...
F7-12(1) With each new received frame, the network device driver increments BufDescPtrCur
by 1 and wraps around the descriptor list as necessary
...
F7-12(2) The hardware applies the same logic to an internal descriptor pointer.
Anchor
ISR handler
...
Anchor
We use three pointers to manage and keep track of the Rx descriptors:anchor
RxBufDescPtrStart
This pointer
...
doesn’t move, it always points to the first descriptor.
...
RxBufDescPtrCur
Anchor
This pointer must track the current descriptor which data is ready to be processed.
...
RxBufDescPtrEnd
This pointer
...
doesn’t move, it always points to the last descriptor.
...
Initializing device reception descriptors
...
NetDev_Start()
starts the network interface hardware by initializing the receive and transmit descriptors, enabling the transmitter and receiver and starting and enabling the DMA. Initialization the Rx DMA descriptors list can done in a sub-function NetDev_RxDescInit()
. The memory needed by the descriptors must be reserved by the function NetDev_Init()
. Initialization of the Rx descriptor list consist of setting the descriptors pointers of the NET_DEV_DATA
and fill all Receive descriptors with a Receive buffer.
Anchor
You also have to initialize each descriptor. You must initialize descriptor field according to the controller documentation. Note that the descriptor must be configured to be owned by the DMA and not the software. Here is the pseudo code of the descriptor ring initialization:
Anchor
...
rowspan | 20 |
---|
...
Listing 7-7 Descriptor Ring Initialization
L7-7(1) Initialize the descriptor pointer to the first Rx buffer descriptor of pdev_data
.
...
L7-7(2) Initialize current descriptor pointer of pdev_data
to the first Rx buffer descriptor of pdev_data
.
...
L7-7(3) Initialize last descriptor pointer of pdev_data
to the last descriptor declared using pointer arithmetic and the Rx descriptor number defined by RxDescNbr
in NET_DEV_CFG_ETHER
.
...
L7-7(4) Repeat for each descriptor defined in .RxDescNbr
in NET_DEV_CFG_ETHER
.
...
L7-7(5) Initialize the description fields to their initial value as defined by the DMA Descriptor's documentation in the device data sheet. There might be more than a single field to define depending of the specifications of the DMA used (a field describing the size of associated data buffer might be present and require to be initialized to the length of the requested buffer area below.)
...
L7-7(6) Initialize the status bit of the descriptor to specify that it is owned by the DMA engine (not by the software).
...
L7-7(7) Call NetBuf_GetDataPtr()
to get a buffer area and initialize the descriptor's buffer start address to the address of the buffer area.
...
L7-7(8) If an error occurred during the allocation of a buffer area, return as it might mean that there is an issue with the values declared in NET_DEV_CFG_ETHER
and the available device memory or heap size.
...
L7-7(9) Initialize the next descriptor location of the current descriptor to the next descriptor using pointer arithmetic.
...
L7-7(10) Increment the descriptor using pointer arithmetics.
...
Anchor
Once the Rx descriptor ring is ready, you have to configure controller register to enable the controller reception. Controller's interrupt generation should be enabled for the following events: reception of a packet with and without errors and completed transmission of a packet with and without errors. Anchor
What needs to be done in the ISR for reception
...
NetDev_ISR_Handler()
is the function called by the IF layer when a Ethernet related ISR is generated and handled by the BSP layer. When Rx ISR occur, only NetOS_IF_RxTaskSignal()
has to be called. Nothing has to be done on RxBufDescPtrCur
. The complete receive process is delayed in order to have the fastest ISR handler as possible. If an error occurred on RX, you can increment driver statistic into the ISR handler or into NetDev_Rx()
, it’s it’s up to you to determine which of the cases is best. You must always signal the core that a packet is received using NetOS_IF_RxTaskSignal()
. If you fail to notify the core for each packet, a buffer leak will occur and performance will degrade. NetDev_Rx()
will discard the packet and it will say to the µCµC/TCP-IP module that the packet is received with an error. anchorMoving buffers from the device to the TCP-IP stack using DMA
...
NetDev_Rx()
is called by core once a NetOS_IF_RxTaskSignal()
call has been made to recover the received packet. If data received is valid, this function must replace the buffer of the current descriptor with a free buffer. Also, the current descriptor must be restarted (owned by the DMA) to be able to receive again. RxBufDescPtrCur
must be moved to point on the next descriptor. The sub-function NetDev_RxDescPtrCurInc()
is called to restart the current descriptor and to move the pointer to the next descriptor. If an error has occurred, you have to set data and length pointers to 0 and return an error. If there is no free Rx buffer available, the packet must be discarded by leaving the current data buffer assigned to the DMA, increment the current descriptor and return an error to the µCµC/TCP-IP module. Here is a pseudo code of NetDev_Rx()
: 1042325 Anchor
...
rowspan | 45 |
---|
...
Listing 7-8 Packet Reception
L7-8(1) Obtain pointer to the next ready descriptor.
...
L7-8(2) If this descriptor is owned by the DMA (e.g., the DMA is currently receiving data or hasn't started receiving data yet). The descriptor has to be owned by the software to be processed. If owned by the DMA, set *perr
to NET_DEV_ERR_RX
signaling that the interrupt that there was an error within the reception. Set *size
to 0, *p_data
to (CPU_INT08U*)0
and return.
...
L7-8(3) If a reception error is reported in the descriptor set *perr
to NET_DEV_ERR_RX
notifying that an error occured within the reception. Set *size
to 0, *p_data
to (CPU_INT08U*)0
and return.
...
L7-8(4) If the frame length is either runt or overrun set *perr
to NET_DEV_ERR_INVALID_SIZE
signaling that the size of the received frame is invalid. Set *size
to 0, *p_data
to (CPU_INT08U*)0
and return.
...
L7-8(5) Once every error has been handled, acquire a new data buffer to replace the one we're about to take from the descriptor. If no buffers are available set *size
to 0, *p_data
to (CPU_INT08U*)0
, increment pdev_data
current descriptor and return.
...
L7-8(6) Set *size
to the value of the Length field of the current descriptor. This field should specify how many bytes of data were received by the descriptor.
...
L7-8(7) Set *p_data
to the value of the data buffer of the descriptor.
...
L7-8(8) Set the value of the
...
descriptor’s data buffer to the newly allocated data area.
...
L7-8(9) Increment the current descriptor to the next descriptor.
...
L7-8(10) Set *perr
to NET_DEV_ERR_NONE
to notify that no errors were found.
...
Anchor
The following is the pseudo code for NetDev_RxDescPtrCurInc()
:
anchor
...
rowspan | 2 |
---|
...
Listing 7-9 Descriptor Increment
L7-9(1) Get current pdev_data
current descriptor.
...
L7-9(2) Set pdev_data
current descriptor to the next descriptor in the current one.
...
1042348 Anchor
Stopping the reception of packets
Anchor
NetDev_Stop()
is called to shutdown a network interface hardware by disabling the receiver and transmitter, disabling receive and transmit interrupts, free all receive descriptors and deallocate all transmit buffers. When the interface is stopped, you must deallocate the DMA descriptor ring. To do that, a sub-function is called NetDev_RxDescFreeAll()
where each descriptor’s descriptor’s buffer is freed and the DMA controller control is disabled: anchor
...
rowspan | 7 |
---|
...
Listing 7-10 Deallocation of Descriptor Ring
L7-10(1) Get pdev_data
's first descriptor.
...
L7-10(2) For each descriptor defined in .RxDescNbr
in NET_DEV_CFG_ETHER
:
...
L7-10(3) Get the address of the descriptor's buffer.
...
L7-10(4) Deallocate the buffer area.
...
L7-10(5) Set the status of the descriptor to be owned by the software (to disable reception on that descriptor).
...
L7-10(6) Increment the current descriptor using pointer arithmetic.