Okay, so I've gone through this in pretty rigorous detail now, and it doesn't look like packet size, or SPI speed has anything to do with the issue. I reorganized my code to send larger packets, and even put some more time between sending packets, but it made no difference. So finally, I just made a simple sketch to test the issue outside of my program, here's what I found.
When sending data using UDP, if you change the IP address or port number between transmissions when using the same socket, there is a significant delay in waiting for the SENT_OK interrupt to trigger on the chip. Sometimes, this wait time can be close to 500ms! But at it's best, it's around 1-5ms. However, if you use two sockets (or two EthernetUDP objects). The wait time is around 128 micro seconds, so, like between 10-1000 times faster.
The delay occurs in the sendUDP function of socket.cpp in the while loop :
Code:
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
That loops basically polls the chip over and over waiting for the SEND_OK bit to trigger in the socket interrupt.
So basically, my question is, does anyone have an idea why it would take so long just by changing the destination for the packet? Is there a setting in the chip that can be modified to not have this waiting? Anyways, here's my sample sketch that illustrates this problem :
Code:
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x6E, 0x44};
IPAddress myIP(192, 168, 0, 105);
IPAddress destinationONE(192, 168, 0, 102);
IPAddress destinationTWO(192, 168, 0, 103);
unsigned int portONE = 8002;
unsigned int portTWO = 8004;
EthernetUDP UdpONE;
EthernetUDP UdpTWO;
uint8_t buffer[24];
uint8_t counter;
elapsedMicros transmissionTime;
void setup() {
Ethernet.begin(mac,myIP);
UdpONE.begin(8000);
UdpTWO.begin(8001);
}
void loop() {
for(int i=0; i<24; i++)
buffer[i] = counter;
if(++counter == 100)
counter = 0;
transmissionTime = 0;
UdpONE.beginPacket(destinationONE,portONE);
UdpONE.write(0xF0);
UdpONE.write(buffer,24);
UdpONE.endPacket();
Serial.print("Packet 1 - ");
Serial.print(transmissionTime);
transmissionTime = 0;
UdpONE.beginPacket(destinationTWO,portTWO);
UdpONE.write(0xF0);
UdpONE.write(buffer,24);
UdpONE.endPacket();
Serial.print("\tPacket 2 - ");
Serial.println(transmissionTime);
// transmissionTime = 0;
// UdpTWO.beginPacket(destinationTWO,portTWO);
// UdpTWO.write(0xF0);
// UdpTWO.write(buffer,24);
// UdpTWO.endPacket();
// Serial.print("\tPacket 2 - ");
// Serial.println(transmissionTime);
delay(10);
}
So, it seems like probably this is an issue with the chip, or something inherent in UDP transmission that I'm not familiar with. Alternatively, I would love to just use the 8 sockets available on the W5200, but I can't get the code to run correctly, has anyone been able to do this? I can get it to compile, but it crashes when I send messages...