2024-04-05 04:21 PM - edited 2024-04-05 04:22 PM
Body:
Hello STM32 Community,
I'm encountering an issue with an Ethernet application I'm developing on an STM32 platform, where I can't seem to send UDP packets to a specific IP address within my local network. Broadcast UDP packets are sent and received successfully, but directed packets do not reach their destination.
Here's a summary of the setup and issue with github gists going into deeper details below:
Wireshark captures the broadcast packets, but not the packets directed to 192.168.1.141. However, when I use the broadcast address 192.168.1.255, the messages are received and visible in Wireshark and on the nc -lu 6543 listener on my notebook.
Attempts to ping the development board's IP (192.168.1.150) result in timeouts,
Despite the board's IP being in the destination computer's ARP table. I've also confirmed that firewalls are disabled on everywhere on the router and destination computer.
Unfortunately the router's current network management portal does not let me see list of devices connected.
The relevant code files are attached and briefly noted here. Started by initializing the network and send UDP messages, pretty standard stuff, follows the examples closely:
void udp_network_init(void)
{
// set device's ip address, netmask and gateway
ipaddr_aton("192.168.1.255", &broadcast_ip);
ipaddr_aton("192.168.1.150", &device_ip);
ipaddr_aton("192.168.1.99", &computer_ip);
ipaddr_aton("192.168.1.1", &router_ip);
ipaddr_aton("255.255.255.0", &netmask);
ipaddr_aton("192.168.1.1", &gateway);
// initialize the ethernet interface
eth_init(); // inits device mac adrress, starts eth clock, gpio, interrupts & checksums for ethernet packet
eth_mac_init(); // starts the ETH DMA Rx & Tx
lwip_init(); // starts network stack
// set up the network interface
if (!netif_add(&pnetif, &device_ip, &netmask, &gateway, NULL, ethernetif_init, ethernet_input)) {
ERROR("Failed to add network if");
}
netif_set_default(&pnetif);
netif_set_up(&pnetif);
udp_pcb = udp_new();
if (udp_pcb != NULL) {
err_t err = udp_bind(udp_pcb, &device_ip, UDP_PORT_SRC);
if (err != ERR_OK) {
INFO("Can't bind to device ip/ src port");
}
err = udp_connect(udp_pcb, &computer_ip, UDP_PORT_DST);
if (err != ERR_OK) {
INFO("Can't connect to computer ip");
}
udp_recv(udp_pcb, udp_recv_callback, NULL);
}
task_custom_size(ethernet_thread,
"ethernet_thread",
BYTES_TO_STACK_SIZE(2048),
NULL, PRIORITY_MINIMUM
);
INFO("Ready to receive udp packets");
// udp_send_message("targetted ip test", &computer_ip, UDP_PORT_DST);
// udp_send_message("broadcast ip test", &broadcast_ip, UDP_PORT_DST);
}
void udp_send_message(const char* message, const ip_addr_t* target_ip, u16_t target_port)
{
err_t wr_err = ERR_OK;
struct pbuf* p = pbuf_alloc(PBUF_TRANSPORT, strlen(message), PBUF_RAM);
if (p != NULL) {
memcpy(p->payload, message, strlen(message));
wr_err = udp_sendto_if(udp_pcb, p, target_ip, target_port, &pnetif);
if (wr_err) {
INFO("UDP_SENDTO_IF error %d", (int)wr_err);
}
pbuf_free(p);
}
}
static void ethernet_thread(void* v)
{
(void)v;
for (;;) {
ethernet_loop();
msleep(ETHERNET_LOOP_TIME);
}
}
void ethernet_loop(void)
{
ethernetif_input(&pnetif);
sys_check_timeouts();
}
ping test:
https://gist.github.com/amitrahman1026/945babb9c4eadcf939634e145d48de76
static arp:
https://gist.github.com/amitrahman1026/d5abb897afdb3c57be05d4075dd0c8c8
min stack size for lwip:
https://gist.github.com/amitrahman1026/e214ff01ba8c895d8d37844d7e9de016
Attached are the most relevant files as well.
Considering the above, I have a few questions for the community:
Any insights, suggestions, or guidance you can provide would be greatly appreciated. Thank you in advance for your time and help!