W5500 how calc RCR and RTR

According formula from datasheet - i can convert RTR&RCR values to timeout in milliseconds.
But how to get reverse operation - calculate new RCR&RTR values for expected timeout in milliseconds.
Ex. i need timeout about 10 sec or 65 seconds.

That is not straightforward, as RCR and RTR have different effects in different situations, and it is explained in the datasheet.

I think you are confused by the fact that you can come to your 10 seconds ir 65 seconds in multiple ways, so you can vary RCR and RTR to get the same “total timeout”. Thus choose any of RTR and RCR values satisfying your requirements under the situations explained in the datasheet. I would not even bother to change from default value unless definitely need to do it as “backward engineering” of the formula is a nightmare.

In simple words:

  • RCR means how much time chip will wait for someting until trying next retry. You should know here that every next retry wait time may double up to tens of seconds, thus, as I said, actual timeout time is not trivial to calculate;
  • RTR means how many retries chip will make.

For simplicity change any of them to get desired result :slight_smile:

yes, you are right - you can get the required timeout in several ways. But i’m not confused about this, I’m very surprised that in datasheet is not have at least one way to calculate values for these registers. It’s very bad if you use slow internet channel (like GPRS modem) and need to tune timeout (like connection timeout, etc).
This problem and another problem “no ACK receive notification” (see my other post) lead me to be very sad about w5500. Yes - this is nice cheap chip, and yes - it have big functional, but also has this critical missings. I’m confused about this - nobody was see this moments before on previosly models?

It does have the formula, and if you are an engineer you will find the way to use it to get right RCR and RTR values for your application. It is hard for person not familiar with the networking, but it is not impossible if person wants to learn.

As i answered in that post that most probably W5500 will free its TX buffer space with data being sent only after it gets ACK from receiving side, but it must be checked as I never checked about how W5500 works at this level (and did not need to).

As any complex device - more complex device is, less it is subject to customization, or it becomes complex to manage. If you want total flexibility and total knowledge of how your network stack works, you must choose L2 networking hardware, and write your own network stack implementation from level 2 up. You can do it with W5500 using MAC RAW through socket 0, but IMHO it would be more beneficial to learn how chip’s built in IP stack works and perform its appropriate programming.

it’s not trivial math solution for me. If it’s so easy - why this solution not in datasheet or any this forum posts?
Yes, may be i’m not good mathematic and can’t research new reverse back calculations for this formula.
Does anyone use the ability to change these registers? How they calc values? And main point - how chip developer was expect users will be calc values for this registers? :slight_smile:

Yes, i will check this.

as i think, external TCP stack (like lwip) it’s not good solution for w5500. More easy for lwip - use MCU included internal Ethernet MAC.

There’s actually very simple math behing that frightening formula. The issue is understanding this math, and understand how register values fit into it.

I tried to double values some time ago, but then decided that default values are ok for me. Most probably they will fit for your application too.

The good thing about such microcontrollers is that main processing unit is as close to the PHY as possible. The bad thing can be price, and most probably even harder configration and programming than W5x00. But if using standard libraries complexity should not be an issue. Size of install base also matters, as well as number and level of experise on the field.

Yes, it’s simple to use formula from datasheet (which calc timeout from RTR and RCR values), but how do reverse calculations? If it simple too - i beging, show me how to do this

I think with ARP timeout you have no issuse, right?

𝐴𝑅𝑃𝑇𝑂 = (𝑅𝑇𝑅 × 0.1𝑚𝑠) × (𝑅𝐶𝑅 + 1)

So in order to get 65 seconds of total timeout you set RTR to 10000 and RCR to 65.

With this one it is trickier

𝑇𝐶𝑃𝑇𝑂 = (∑(𝑅𝑇𝑅 × 2^𝑁) + ((𝑅𝐶𝑅 − 𝑀) × 𝑅𝑇𝑅𝑀𝐴𝑋)) × 0.1𝑚s


N : Retransmission count, 0≤N ≤ M
M : Minimum value when RTR x 2^(M+1) > 65535 and 0 ≤ M ≤ RCR

For 65 seconds

(∑(𝑅𝑇𝑅 × 2^𝑁) + ((𝑅𝐶𝑅 − 𝑀) × 𝑅𝑇𝑅𝑀𝐴𝑋)) × 0.1𝑚s = 65000 ms

∑(𝑅𝑇𝑅 × 2^𝑁) + ((𝑅𝐶𝑅 − 𝑀) × 𝑅𝑇𝑅𝑀𝐴𝑋) = 650000

Looks like really complicated, and unclear. But there’s example for default values of RTR=2000 (7D0) and RCR=8:

TCPTO = (0x07D0+0x0FA0+0x1F40+0x3E80+0x7D00+0xFA00+0xFA00+0xFA00+0xFA00) X 0.1ms = (2000 + 4000 + 8000 + 16000 + 32000 + ((8 - 4) X 64000)) X 0.1ms = 318000 X 0.1ms = 31.8s

You can learn from this formula that value of each term in () can not be more than 65535, there’re RCR+1 operands there, starting with RTR value and each *2 to previous.

This one is much simpler to understand than previous formula, however not clear if it is 1:1 equivalence with it :slight_smile:

Let’s take random value of 6000 for RTR and leave 8 for RCR.
(6000+12000+24000+48000+48000+48000+48000+48000)*0.1 = 28.2 seconds, so you see that timeout is even less than with value of 2000, which is relatively strange.

Let’s take another approach: 2000 for RTR and 12 for RCR:
(2000+4000+8000+16000+32000+64000+64000+64000+64000+64000+64000+64000+64000)*0.1 = 57.4 seconds. Each further increas ein RCR will give +6.4 seconds to the timeout, thus RTR=2000 and RCR=13 will give you 63.8, very close to 65.

As you see there’s no much difference when timeout reaches 64000 (or 65535), it will always add the same value of timeout with next retry. You must focus on starting values instead, in our case 2000 / 4000 / 8000 / … These values define how much of initial timeout chip will wait per retry, until next attempt of communication is performed.

For example, chip sends the packet. then waits initial time value. It can be RTR=2000 like above (200 ms), or RTR=6000 (600 ms)… Next iteration will be waiting for 4000 and 12000 respectively. If you know that your network is quick, but subject to lose packets, then you will be interested to make RTR as less as possible, but RCR as big as possible so that chip did not wait long time until retrying sending duplicate packet (as it may have been lost). However if your network is slow, but does not lose packets, you would better set RTR for higher value, decreasing value of RCR.

Did you get the logic?

No, i’m don’t get the point. :frowning: Looks like your method is some like brute-force.
If set RCR=13 and RTR=2038 we take more close value. Your method dont lead to this. And i don’t see any another method, at present time, how to CALC this reg values. Seems this like a “knapsack problem”, but a bit easier - in some moments we can use bisection method.

О, if only original formula was more simple, like:
𝑇𝐶𝑃𝑇𝑂 = (∑(𝑅𝑇𝑅 × 2^N), 0<=N<=RCR
and we need to find RTR for known total timeout time (𝑇𝐶𝑃𝑇𝑂) and max retry (RCR):
𝑅𝑇𝑅 = 𝑇𝐶𝑃𝑇𝑂 / (2^(RCR+1) - 1)

but i can’t find out how to put in this calculations this part of original formula:

  • ((𝑅𝐶𝑅 − 𝑀) × 𝑅𝑇𝑅𝑀𝐴𝑋)

as i understand - this is compensation 16bit register overflow, but these knowledge dont help

Hm, the explanation is fairly easy. Time being spent waiting for acknowledgment starts with value of RTR in 0.1 ms periods, and each next retry it is doubled, however if value exceeds 65535 is it not doubled, and left as is for next retry.

How did you know it? Calculated, or put into the chip and waited till timeout?

1 Like

:slight_smile: I understand how w5500 timer work, but i don’t understand how to use in practice your method to calc values.

Both. In chip was fast test by logs outputs, wich includes RTC timestamp (so is accuracy about 1 sec).
tomorrow i maybe try to fixate timeout more accuracy (by HW timer or DWT->CYCCNT reg)

Here a visualization of some values… hope it helps in case is still needed.

TCP Timeout calculation

1 Like