Hello,
I am working with the MIMXRT1180-EVK board and would like to implement a baremetal TCP client using the LwIP stack. Could you kindly provide an example or point me toward resources that would help me achieve this?
I am looking for a basic setup that includes Ethernet initialization and a simple TCP client communication implementation.
Any assistance would be greatly appreciated!
Thank you.
Solved! Go to Solution.
HI @PavanKumarS
Sorry, i forgot it.
ethernetif_input(&netif) is responsible for processing incoming packets. Without this function being called, your application doesn’t receive Ethernet frames from the network interface.
If you remove the loop, network events like incoming packets or timeouts won’t be handled, so the connection won’t be properly maintained.
If you want to avoid relying on this polling loop, you can use LwIP with an operating system (like FreeRTOS) to handle network events asynchronously through interrupts and tasks.
BR
Hang
Hi @PavanKumarS
You can download the MIMXRT1180-EVK SDK.
Select Board | MCUXpresso SDK Builder (nxp.com)
And you can refer to lwip examples.
BR
Hang
Hi @PavanKumarS
To implement a baremetal TCP client on the MIMXRT1180-EVK using the LwIP stack,
Import the lwip_ping_bm_cm33 example.
This examples include the necessary Ethernet initialization, which you can adapt to create a TCP client.
Then you will add client codes.
Here’s a basic example for creating a TCP client. You need to set up a connection and manage sending and receiving data using callbacks.
#include "lwip/tcp.h"
static struct tcp_pcb *tcp_client_pcb;
// Callback when data is received
err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
if (p == NULL) {
// Connection closed
tcp_close(tpcb);
return ERR_OK;
}
// Process received data
tcp_recved(tpcb, p->len);
pbuf_free(p);
return ERR_OK;
}
// Callback when a TCP connection is established
err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
if (err == ERR_OK) {
// Connection successful
// Set the receive callback
tcp_recv(tpcb, tcp_client_recv);
// Send some data
const char *data = "Hello, Server!";
tcp_write(tpcb, data, strlen(data), TCP_WRITE_FLAG_COPY);
} else {
// Handle connection error
tcp_close(tpcb);
}
return err;
}
// Initiate the TCP connection
void tcp_client_connect(void) {
ip_addr_t server_ip;
IP4_ADDR(&server_ip, 192, 168, 1, 100); // Set the server's IP address
// Create a new TCP control block
tcp_client_pcb = tcp_new();
// Connect to the server on port 80 (you can change this to your desired port)
tcp_connect(tcp_client_pcb, &server_ip, 80, tcp_client_connected);
}
Integrate everything in the main() function of your application.
int main(void) {
// Board initialization (clocks, Ethernet, etc.)
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
BOARD_InitDebugConsole();
// Initialize the LwIP stack and network interface
lwip_init_function();
// Start the TCP client
tcp_client_connect();
while (1) {
// LwIP needs to be continuously polled to handle network events
sys_check_timeouts();
}
return 0;
}
BR
Hang
HI @PavanKumarS
Sorry, i forgot it.
ethernetif_input(&netif) is responsible for processing incoming packets. Without this function being called, your application doesn’t receive Ethernet frames from the network interface.
If you remove the loop, network events like incoming packets or timeouts won’t be handled, so the connection won’t be properly maintained.
If you want to avoid relying on this polling loop, you can use LwIP with an operating system (like FreeRTOS) to handle network events asynchronously through interrupts and tasks.
BR
Hang