Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Udp.parsePacket() is slow #24

Open
rnamdos opened this issue Sep 20, 2021 · 1 comment
Open

Udp.parsePacket() is slow #24

rnamdos opened this issue Sep 20, 2021 · 1 comment

Comments

@rnamdos
Copy link

rnamdos commented Sep 20, 2021

How fast can I expect Udp.parsePacket() to be? In this example code, I am parsing UDP packets and toggling a GPIO pin to see how fast the code runs IRL. The parsing takes 330nS. I expected (based on the clock speeds and the 100MBps interface) the speeds to be orders of magnitude higher... 330ns is around 550 clock cycles. I just want to know if there is something I can do to improve this or that the library is focused on compatibility, not performance. Thanks!

#include <NativeEthernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 6454;      // local port to listen on

// buffers for receiving and sending data
#define UDP_TX_PACKET_MAX_SIZE 1480
unsigned char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

const int PIN_0 = 38;
int packetSize = 0;

void setup() {
//  noInterrupts();
  teensyMAC(mac);
  pinMode(PIN_0, OUTPUT);
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
}

void loop() {

  while (1) {
    digitalToggleFast(PIN_0);
    //delayNanoseconds(1);

    packetSize = Udp.parsePacket();
    if (packetSize) {
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    }
  }
}

/*********************** teensyMAC *********************/
void teensyMAC(uint8_t *mac)
{
  for (uint8_t by = 0; by < 2; by++) mac[by] = (HW_OCOTP_MAC1 >> ((1 - by) * 8)) & 0xFF;
  for (uint8_t by = 0; by < 4; by++) mac[by + 2] = (HW_OCOTP_MAC0 >> ((3 - by) * 8)) & 0xFF;
}
@rnamdos
Copy link
Author

rnamdos commented Sep 20, 2021

I forgot to add that commenting out the lines:

    if (packetSize) {
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    }

doesn't affect the results... parsing is the bottleneck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant