WIZnet Developer Forum

W5500 polled TCP receive

Hi all

I’m using a W5500 with an Atmel AVR XMega MCU. Going pretty well so far. I now want to write polled code to receive from a TCP socket - I don’t have the option to write interrupt driven code because I’m working inside a bootloader and trying to present a virtual serial port (serial data encapsulated in TCP) without making changes to the main bootloader code.

It is possible that the W5500 will receive more than one packet before I poll it. If this happens, I believe the Sn_RX_RSR register will contain the total amount of data received, so I will not be able to tell how many packets have been received - and I will not know how many times to issue a RECV command (Sn_CR = RECV). Is this correct?

This problem does not happen with UDP because received data also includes a header with the sender IP, socket and payload length, so individual packets can be identified. Can these headers be enabled for TCP reception?

Note that a similar question to mine is raised in this thread:
[url]TCP receive process - data length]

Any advice much appreciated.

While the number of bytes in the incoming buffer is the total, you can find out the number of parcels by parsing the data.

Each received packet has a header (IP, port and number of bytes), which you can use to find the individual packets, pseudo code:

done=0; while (done<totalInByte) { skip 6 bytes (IP and port) done=done+6; bytes=readword(buffer[done]); done=done+2; // process bytes bytes at buffer[done] done=done+bytes; }

I must confess that I’m coding without hardware just now so I’ve not been able to try this. I know the 8 byte information header is there with UDP packets but the documentation I’m looking at is not clear about TCP. If what you say works - fantastic - and thanks!

I will post when/if I get this working.

I do mostly UDP nowadays, but I assumed the principle is the same. It might be that the exact header changes with sockettype though.

If you can’t find it in the W5500 datasheet, the manuals of the older parts (w5100,w5200) often have a more tutorial like character.

I will talk about TCP mode. There’s RAW mode, which I never used myself, which, as declared, should have info you are looking for.

Chip will receive all packets which fit into its socket’s buffer.

Yes.

RECV command instructs W5500 to check free space in the buffer (RX_RSR), and request remote host to send next packet in sequence (I simplify things a bit); if then sent packets fit buffer W5500 stores them, if not, discards until it has enough space. Thus you need to issue RECV command only once you freed space in the RX buffer.

In TCP mode - no, it provides stream of data only. Try RAW mode.

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.