AM261x MCU+ SDK: Enet Lwip TCP Client Example

| AM261x MCU+ SDK 26.00.00 |

Enet Lwip TCP Client Example

# Introduction

Note
  LwIP features are made available as is from public lwIP project. SDK configuration may only enable and exercise a subset of these features.

This example shows about how to implement simple TCP Client on LwIP networking stack using netconn interface coupled with ethernet driver (ENET)

On AM261X, we can do ethernet based communication using CPSW as HW mechanism

- CPSW is a standard ethernet switch + port HW
- It uses ethernet driver underneath with LwIP TCP/IP networking stack
- CPSW can be configured in two modes: Switch or MAC. For more details, Enet CPSW Operation Modes Demo

The example does the following:

- Initializes the ethernet driver for the underlying HW
- Initializes the LwIP stack for TCP/UDP IP and Starts TCP Client task.
- TCP Client gets server IP using UART terminal menu from USER and connect to server IP on port 8888
- TCP client connects to server, sends data and expects the data from server.

# Supported Combinations

Parameter | Value
--- | ---
CPU + OS | r5fss0-0_freertos
Toolchain | ti-arm-clang
Boards | am261x-lp, am261x-som
Example folder | source/networking/enet/core/examples/lwip/enet_cpsw_tcpclient

For support on am261x-lp please refer Ethernet Add-on Boards

# Packet pool configuration

To change packet pool configuration from syscfg, please refer to Ethernet Packet Pool Allocation Guidelines

# Configuring Syscfg

- Following Syscfg option allows flexibility to configure memory footprint based on the required use case like: Number of DMA descriptors and buffering.
- Supported Options with default configuration

Feature | Section | Description | Remarks/Default Setting
--- | --- | --- | ---
Mdio Manual Mode Enable | TI Networking / Enet (CPSW) / MDIO Config | Flag to enable MDIO manual mode in example. Driver support for Manual mode is enabled, so this parameter configures manual mode in the example. | Default is true. If your silicon is affected with errata i2329— MDIO interface corruption, then TI suggests to use MDIO_MANUAL_MODE as software workaround.
Disable Mac Port1, Disable Mac Port2 | TI Networking / Enet (CPSW) / MAC Port Config | Select which port to disable. | Default is Port1 enabled. If both Port1 and Port 2 are enabled, any port can be used and if operating in switch mode, it enables traffic switching between the two ports.
Enable Packet Pool Allocation | TI Networking / Enet (CPSW) / Packet Pool Config | Flag to enable packet buffer memory allocation from enet utils library. It should be disabled to avoid utils memory wastage, in case application allots packet via other mechanism. | Default is true. If enabled, size of pkt pool size depends on 'Large Pool Packet Size', 'Large Pool Packet Count', 'Medium Pool Packet Size', 'Medium Pool Packet Count', 'Small Pool Packet Size' and 'Small Pool Packet Count'. EnetMem_allocEthPkt API uses this memory to allocate the DMA Ethernet packet.
Only Enable Packet Info Allocation | TI Networking / Enet (CPSW) | Flag to allocate only the DMA Packet Info structures, this does not include the buffer memory. This is useful when the buffer memory is internally allocated by the application. (Ex- Lwip pools) | Default is true. If enabled, "PktInfoMem Only Count" determines the number of additional DMA Packet Info structures allocated. EnetMem_allocEthPktInfoMem uses this memory to allocate empty DMA Packet Info structures.
Number of Tx Packet | TI Networking / Enet (CPSW) / DMA channel config | No of Tx packets required for DMA channel | Default is 16. For LwIP example, the Tx packet buffer memory is internally allocated in lwippools.h. Only the DMA Pkt Info structures are allocated via sysCfg, so this number should match the "PktInfoMem Only Count" described in the above item. To increase the Tx packet count, user needs to update the number correspondingly at "PktInfoMem Only Count" and lwippools.h and build the libs.
Number of Rx Packet | TI Networking / Enet (CPSW) / DMA channel config | No of Rx packets required for DMA channel | Default is 32. It contributes to the size of Pkt Mem Pool, DMA ring buffer and accessories size. Rx packet buffer memory is completely mananged with application sysCfg, this is done by using Rx custom Pbuf in LwIP.
Netif instance | TI Networking / Enet (CPSW) / LWIP Interface config | No of netifs allocated by the example. Only one netif should be set to default when more than one netif is allocated.

# TCP Server using ncat tool

Ncat is a general-purpose command-line tool for reading, writing, redirecting, and encrypting data across a network. It aims to be your network Swiss Army knife, handling a wide variety of security testing and administration tasks. Ncat is suitable for interactive use or as a network-connected back end for other tools.

- Ncat is started as server to which EVM connects.
- Version used for this example Version 7+ ( https://nmap.org/ncat )

# Steps to Run the Example

## To Configure Static IP

Please refer to Ethernet LwIP TCP/IP Static IP.

## Build the example

- When using CCS projects to build, import the CCS project for the required combination and build it using the CCS project menu (see Using SDK with CCS Projects).
- When using makefiles to build, note the required combination and build using make command (see Using SDK with Makefiles)

## HW Setup

Note
  Make sure you have setup the EVM with cable connections as shown here, EVM Setup. In addition do below steps.

## Create a network between EVM and host PC

- The EVM will get an IP address using DHCP, so make sure to connect the other end of the cable to a network which has a DHCP server running.
- To get started, one can create a simple local network between the EVM and the host PC by using a home broadband/wifi router as shown below. Most such routers run a DHCP server

Local network between PC and EVM

- To check the router connection with host PC, recommend to disconnect all other networking connections on the PC, sometimes you may need to disable firewall SW, and make sure the router is able to assign a IP address to your host PC
- After we run the example on the EVM (next step), the EVM will similarly be assigned a IP address, and then host can communicate with the EVM using the assigned IP address.
- To enable static IP, set the static IP in the ipAddr variable in the App_setupNetif() before passing it as arguement to initiate the netif, and stop the dhcp from starting in the App_allocateIPAddress() function.

## Run the example

Attention
  If you need to reload and run again, a CPU power-cycle is MUST

- Launch a CCS debug session and run the example executable, see CCS Launch, Load and Run
- You will see logs in the UART terminal as shown in the next section.
- Note the IP address seen in the log, this is what we will use to communicate with the EVM.

## Steps to execute

1. Start TCP server using 'ncat' cmds as shown below. Below steps have been tried with a Linux Ubuntu 18.04 host PC running bash shell
2. Install 'ncat' if not installed by doing below
   $sudo apt install ncat
3. Invoke 'ncat' to start TCP echo server on port 8888 as below
   $ncat -e /bin/cat -kv -l 8888
4. Then, run the example on EVM.
5. Enter host PC IP address when example prompts on UART terminal.

## Sample output for CPSW example

==========================
CPSW LWIP TCP CLIENT
==========================
EnetPhy_bindDriver:1729
PHY 3 is alive
PHY 12 is alive
Starting lwIP, local interface IP is dhcp-enabled
Host MAC address-0 : 70:ff:76:1e:7e:52
[LWIPIF_LWIP] NETIF INIT SUCCESS
Enet IF UP Event. Local interface IP:0.0.0.0
[LWIPIF_LWIP] Enet has been started successfully
Waiting for network UP ...
Waiting for network UP ...
Cpsw_handleLinkUp:1323
MAC Port 1: link up
Network Link UP Event
Waiting for network UP ...
Enet IF UP Event. Local interface IP:192.168.1.10
Network is UP ...
TCP Client Menu:
Enter TCP server IPv4 address:(example: 192.168.101.100)
192.168.1.20
IP eneterd is: 192.168.1.20
<<<< ITERATION 1 >>>>
Connecting to: 192.168.1.20:8888
Connection with the server is established
"Hello over TCP 1" was sent to the Server
Successfully received the packet 1
"Hello over TCP 2" was sent to the Server
Successfully received the packet 2
"Hello over TCP 3" was sent to the Server
Successfully received the packet 3
"Hello over TCP 4" was sent to the Server
Successfully received the packet 4
"Hello over TCP 5" was sent to the Server
Successfully received the packet 5
Connection closed
<<<< ITERATION 2 >>>>
Connecting to: 192.168.1.20:8888
Connection with the server is established
"Hello over TCP 1" was sent to the Server
Successfully received the packet 1
"Hello over TCP 2" was sent to the Server
Successfully received the packet 2
"Hello over TCP 3" was sent to the Server
Successfully received the packet 3
"Hello over TCP 4" was sent to the Server
Successfully received the packet 4
"Hello over TCP 5" was sent to the Server
Successfully received the packet 5
Connection closed

## Troubleshooting issues

- For am261x-lp, Software configures MDIO based on Board version(E1/E2) which is read from EEPROM. It expects on-board EEPROM to be pre-programmed for E2 EVMs. If EEPROM(0x51) is not programmed, the software considers EVM as "E1" version.
- If you see a valid, non-zero MAC address and continuously see "Waiting for network UP..." prints on the UART terminal
  - Make sure you see `Enet IF UP Event.` message, if not check the ethernet cable
  - Check the local network and check if the DHCP server is indeed running as expected
  - When using a home broadband/wifi router, its possible to check the clients connected to the DHCP server via a web browser. Check your router user manual for more details.

# See Also