Arduino UNO + Ethernet Shield. W5100. Clear RECV bit in SnIR after receiving

Dear, Community,

I have Arduino UNO + Ethernet Schield. I am writing a TCP Client Application that interracts with a TCP server. In my setup Client sends a packet, and server upon reception sends the same packet back to the Client.

The purpose of the Client is to measure time passed between sending own packet and receiving the packet from the Server. I need a nanoseconds precision, that is why I don’t use milis() or micros() and instead count ticks using timer1.

Below is the code snippet of the client. As you see, when packet comes from the server, the hardware sets the RECV bit in Socket n Interrupt Register (RECV is the third bit in the register: SnIR::RECV == 0x4). The problem is that this bit stays set all the time. Clearing this bit with W5100.writeSnIR(s, W5100.readSnIR(s) & ~(SnIR::RECV)); doesn’t work. Moreover, before clearing the content of the SnIR is 0x07. After clearing it is 0x4.

I would very grateful for any thoughts on this subject!
Thank you!

while((W5100.readSnIR(s) & SnIR::RECV) != SnIR::RECV){ 
        error(3);
}	
	
if( (recvd_del = recv(s,rbuf,1000)) > 0){
	
	restTicks = TCNT1; // save rest ticks
	TIMSK1 &= ~(1 << TOIE1); // disable timer1.
	
	Serial.print("Received delay:");
	Serial.print(rbuf[0]);
	Serial.println();
	
	W5100.writeSnIR(s, W5100.readSnIR(s) & ~(SnIR::RECV));
	Serial.print("SnIR: 0x");
	Serial.print(W5100.readSnIR(s));
	Serial.println();
}

The purpose of the Client is to measure time passed between sending own packet and receiving the packet from the Server. I need a nanoseconds precision, that is why I don't use milis() or micros() and instead count ticks using timer1.

With the Arduino/Ethernet shield setup you won't reach microsecond precision so if you're requesting nanosecond precision you've definitely chosen the wrong platform. To get a microsecond precision (maybe even a bit more) you can choose one of the embedded linux boards (Raspberry Pi, Cubieboard, etc.) but nanosecond precision is left to special hardware.

I'm curious: what do you want to achieve?

pylon is right. Considering the relatively slow speed of the SPI bus, you will be lucky to get microsecond precision.

Are you emptying the socket rx buffer before trying to reset the SnIR? If you don't, the datasheet implies you will not be able to reset the interrupt bit. Actually it will reset, but immediately activate if there are characters in the socket rx buffer, or receives more characters (another packet).