Um1713 User Manual: Developing Applications On Stm32Cube With Lwip Tcp/Ip Stack
Um1713 User Manual: Developing Applications On Stm32Cube With Lwip Tcp/Ip Stack
Um1713 User Manual: Developing Applications On Stm32Cube With Lwip Tcp/Ip Stack
User manual
Developing applications on STM32Cube with LwIP TCP/IP stack
Introduction
STMCube™ is an STMicroelectronics original initiative to ease developers life by reducing
development efforts, time and cost. STM32Cube covers STM32 portfolio.
STM32Cube Version 1.x includes:
• The STM32CubeMX, a graphical software configuration tool that allows to generate C
initialization code using graphical wizards.
• A comprehensive embedded software platform, delivered per series (such as
STM32CubeF4 for STM32F4 series)
– The STM32Cube HAL, an STM32 abstraction layer embedded software, ensuring
maximized portability across STM32 portfolio
– A consistent set of middleware components such as RTOS, USB, TCP/IP,
Graphics
– All embedded software utilities coming with a full set of examples.
Some STM32 microcontrollers feature a high-quality 10/100 Mbit/s Ethernet peripheral that
supports both Media Independent Interface (MII) and Reduced Media Independent Interface
(RMII) to interface with the Physical Layer (PHY).
When working with an Ethernet communication interface, a TCP/IP stack is mostly used to
communicate over a local or a wide area network.
This user manual is intended for developers who use STM32Cube firmware on STM32
microcontrollers. It provides a full description of how to integrate a free middleware TCP/IP
stack using STM32Cube HAL drivers into an embedded application based on STM32
microcontroller.
The middleware TCP/IP stack is the LwIP (Lightweight IP) which is an open source stack
intended for embedded devices.
A dedicated STM32Cube firmware package is provided for each series. It includes Ethernet
HAL driver, LwIP middleware and application examples with and without RTOS running on
ST evaluation boards.
Note: This document is applicable to all STM32 series featuring an Ethernet peripheral. However,
or simplicity reason, STM32F4xx and STM32CubeF4 are used as reference platform. The
same description, file names and screenshot are applicable as well to other series offering
Ethernet connectivity, such as STM32F107xx, STM32F2x7xx and STM32F7xx. To know
more about the Ethernet examples implementation on your STM32 series, refer to the
documentation provided within the associated STM32Cube firmware package.
Contents
3 LwIP configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.1 Modules support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 Memory configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Appendix A FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
A.1 How do I choose between static or dynamic (DHCP)
IP address allocation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
A.2 How does the application behave when the Ethernet cable
is disconnected? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
A.3 How can the application be ported on a different hardware? . . . . . . . . . . 39
8 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
List of tables
List of figures
1.2 License
LwIP is licensed under the BSD license. Below is a copy of the LwIP license document that
is included in the source codes:
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
where
doc contains documentation text files
src contains source files of the LwIP stack
api contains Netconn and Socket API files
core contains LwIP core files
include contains LwIP include files
netif contains Network interface files
system contains LwIP port hardware implementation files
arch contains STM32 architecture port files (used data types,...)
OS contains LwIP port hardware implementation files using an operating system
noOS contains LwIP port hardware implementation files in Standalone mode
Table 2 provides a summary of the Raw API functions for UDP applications.
Sends data to the currently connected remote IP/port (not applicable for
netconn_send
TCP connections).
QH[W QH[WSEXIVWUXFWXUH
SD\ORDG
OHQ
WRWBOHQ
IODJV UHI
5RRPIRUSDFNHWKHDGHUV
069
where
next contains the pointer to the next pbuf in a pbuf chain
payload contains the pointer to the packet data payload
len is the length of the data content of the pbuf
tot_len is the sum of pbuf len plus all the len fields of the next pbufs in the chain
ref is the 4-bit reference count that indicates the number of pointers that point to the
pbuf. A pbuf can be released from memory only when its reference count is zero.
flags (on 4 bits) indicate the type of pbuf.
LwIP defines three types of pbufs, depending on the allocation type:
• PBUF_POOL
pbuf allocation is performed from a pool of statically preallocated pbufs of predefined
size. Depending on the data size that needs to be allocated, one or multiple chained
pbufs are required.
• PBUF_RAM
pbuf is dynamically allocated in memory (one contiguous chunk of memory for the full
pbuf)
• PBUF_ROM
No memory space allocation is required for user payload: the pbuf payload pointer
points to data in ROM memory that can be used only for sending constant data.
For packet reception, the suitable pbuf type is PBUF_POOL. It allows to quickly allocate
memory for the packet received from the pool of pbufs. Depending on the size of the
received packet, one or multiple chained pbufs are allocated. The PBUF_RAM is not
suitable for packet reception because dynamic allocation takes some delay. It may also lead
to memory fragmentation.
For packet transmission, the user can choose the most suitable pbuf type according to the
data to be transmitted,.
Chains two pbufs together (but does not change the reference count of the
pbuf_cat
tail pbuf chain).
pbuf_chain Chains two pbufs together (tail chain reference count is incremented).
pbuf_dechain Unchains the first pbuf from its succeeding pbufs in the chain.
pbuf_header Adjusts the payload pointer to hide or reveal headers in the payload.
The following example shows how to initialize the Ethernet peripheral, using HAL API, into
the interface API:
static void low_level_init(struct netif *netif)
{
uint8_t macaddress[6]= {MAC_ADDR0, MAC_ADDR1, MAC_ADDR2, MAC_ADDR3,
MAC_ADDR4, MAC_ADDR5};
EthHandle.Instance = ETH;
EthHandle.Init.MACAddr = macaddress;
EthHandle.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
EthHandle.Init.Speed = ETH_SPEED_100M;
EthHandle.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
EthHandle.Init.MediaInterface = ETH_MEDIA_INTERFACE_MII;
EthHandle.Init.RxMode = ETH_RXINTERRUPT_MODE;
EthHandle.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
EthHandle.Init.PhyAddress = DP83848_PHY_ADDRESS;
...
3 LwIP configuration
LwIP provides a file named lwipopts.h that allows the user to fully configure the stack and all
its modules. The user does not need to define all the LwIP options: if an option is not
defined, a default value defined in opt.h file is used. Therefore, lwipopts.h provides a way to
override much of the lwIP behavior.
/* Enable DHCP */
#define LWIP_DHCP 1
LwIP heap memory size: used for all LwIP dynamic memory
MEM_SIZE
allocations.
0OLL FOR PACKET RECEPTION
.O
.EW PACKET RECEIVED
9ES
#OPY PACKET FROM DRIVER
BUFFERS TO LWI0 BUFFERS
0ROCESSING OF THE PACKET BY
THE LW)0 STACK
.O !PPLICATION
NOTIFICATION NEEDED
9ES
0ROCESSING OF APPLICATION
ASSIGNED CALLBACK FUNCTION
-36
For TCP applications, the following common callback functions must be assigned:
• Callback for incoming TCP connection event, assigned by TCP_accept API call
• Callback for incoming TCP data packet event, assigned by TCP_recev API call
• Callback for signaling successful data transmission, assigned by TCP_sent API call
• Callback for signaling TCP error (after a TCP abort event), assigned by TCP_err API
call
• Periodic callback (every 1 or 2 s) for polling the application, assigned by TCP_poll API
call
...
/* Infinite loop */
while (1)
{
/* Read a received packet from the Ethernet buffers and send it
to the lwIP for handling */
ethernetif_input(&gnetif);
if (tcp_echoserver_pcb != NULL)
{
err_t err;
if (err == ERR_OK)
{
/* start tcp listening for echo_pcb */
tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);
LwIP API calls tcp_new to allocate a new TCP protocol control block (PCB)
(tcp_echoserver_pcb).
The allocated TCP PCB is bound to a local IP address and port using tcp_bind function.
After binding the TCP PCB, tcp_listen function is called in order to start the TCP listening
process on the TCP PCB.
Finally a tcp_echoserver_accept callback function should be assigned to handle incoming
TCP connections on the TCP PCB. This is done by using tcp_accept LwIP API function.
Starting from this point, the TCP server is ready to accept any incoming connection from
remote clients.
ret_err = ERR_OK;
...
}
!PPLICATION
(4404&40 TASK
"LOCKING 3EQUENTIAL
!0) #ALLREAD WRITE
4#0)0 0ACKET
STACK TASK 2ECEPTION
0ACKET 4RANSFER
TO STACK %THERNET
.ETWORK )NTERFACE )32
4ASK
3EMAPHORE -36
for( ;; )
{
}
}
The following functions are called:
1. tcpip_init function is called to initialize the LwIP stack modules and to start the TCP/IP
stack thread.
2. Netif_config function is called to configure the network interface (netif).
3. The TCP echo server thread is created in tcpecho_init function.
void tcpecho_init(void)
{
sys_thread_new("tcpecho_thread", tcpecho_thread, NULL,
DEFAULT_THREAD_STACKSIZE, TCPECHO_THREAD_PRIO);
}
if (conn!=NULL)
{
/* Bind connection to well known port number 7. */
err = netconn_bind(conn, NULL, 7);
if (err == ERR_OK)
{
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1)
{
/* Grab new connection. */
accept_err = netconn_accept(conn, &newconn);
netbuf_delete(buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
else
{
netconn_delete(newconn);
}
}
}
The following sequence is executed:
1. Netconn_new API function is called with NETCONN_TCP parameter will create a new
TCP connection.
2. The newly created connection is then bound to port 7 (echo protocol) by calling
Netconn_bind API function.
3. After binding the connection, the application starts monitoring the connection by calling
Netconn_listen API function.
4. In the infinite while(1) loop, the application waits for a new connection by calling the API
function Netconn_accept. This API call blocks the application task when there is no
incoming connection.
5. When there is an incoming connection, the application can start receiving data by
calling netconn_recv API function. Incoming data is received in a netbuf.
6. The application can get the received data by calling netbuf_data netbuf API function.
7. The received data is sent back (echoed) to the remote TCP client by calling
Netconn_write API function.
8. Netconn_close and Netconn_delete are used to close and delete the Netconn
connection, respectively.
The STM32Cube LwIP package comes with several applications that use the different LwIP
stack API sets.
The applications are divided into three categories as shown in Table 8.
Note: Statistics provide the number of received and corrupted packets is the end of the test.
C:\>echotool /p udp /s
where:
– /p udp is the protocol (UDP protocol)
– /s is the actual mode of connection (Server mode)
5. When the Key button is pressed on the STM324xx-EVAL board, the client sends a
string and the server echoes back the same string to the client.
Note: Make sure that the remote PC IP address is identical to the address defined in the main.h
file (192.168.0.11 by default).
Figure 8 shows an example of this command string and of the module response.
Note: Statistics providing the number of received and corrupted packets are given at the end of
the test.
Note: Make sure that the microSD card is plugged into the dedicated connector prior to
downloading/uploading a file from/to the STM324xx-EVAL board.
7 Conclusion
LwIP package allows to use lwIP TCP/IP stack with the STM32Cube HAL API. This open
source stack offers the services of a full-scale TCP/IP stack while keeping relatively low
RAM/ROM usage.
Two approaches are described for developing TCP/IP applications, either in a Standalone
mode, or using a real-time operating system (RTOS) for multi-threaded operations.
Appendix A FAQ
A.2 How does the application behave when the Ethernet cable
is disconnected?
When the cable is disconnected the Ethernet peripheral stops both transmission and
reception traffics and the network interface is shut down. If an LCD controller is used, a
message is displayed to inform the user that the cable is not connected, otherwise the red
LED of the evaluation board is switched on.
When the user re-connects the cable, the Ethernet traffic resumes and the network interface
is set up. If an LCD controller is used, a message is displayed to inform the user of the new
IP address either with static or dynamic allocation, otherwise the yellow LED of the
evaluation board is switched on.
8 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.