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