an3000-configuring-the-nichelite-tcpip-stack-for-the-stm32f107xx-microcontroller-stmicroelectronics
an3000-configuring-the-nichelite-tcpip-stack-for-the-stm32f107xx-microcontroller-stmicroelectronics
Application note
Configuring the NicheLite™ TCP/IP stack
for the STM32F107xx microcontroller
Introduction
STM32F107xx connectivity line microcontrollers feature a complete 10/100 Ethernet MAC
supporting MII and RMII with hardware support for the IEEE1588 precise time protocol,
enabling Ethernet connectivity for real-time applications.
The NicheLite TCP/IP stack, from InterNiche Technologies, supports STM32F107xx
connectivity line microcontrollers and is provided free-of-charge to ST customers. This stack
is a scaled down version of the full stack available from InterNiche. For download
information, please refer to STMicroelectronics web site www.st.com
The objective of this application note is to introduce the NicheLite stack for STM32F107xx
and describe the integration considerations to be taken into account when developing a user
application including the performance and memory size aspects.
This application note is structured as follows:
■ A short glossary is provided in Section 1
■ A general introduction to TCP/IP is presented in Section 2
■ The Section 3 introduces the NicheLite stack
■ Section 4 and 5 describe the NicheLite stack configuration and optimization
■ NicheLite stack debugging tips are given in Section 6
■ Section 7 presents the NicheTask OS and how to use it in your application
■ Lastly, Section 8 describes the NicheLite package for STM32F107xx and the available
demonstrations
This application note is intended to complement the documentation provided by InterNiche,
which should be referred to for detailed information.
Contents
1 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 TCP/IP introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 TCP/IP a layered protocol stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 The application layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.2 The transport layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.3 The Internet layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.4 Link layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.5 Example of network devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 The protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 Introduction to some TCP/IP protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.1 IP: Internet protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.2 ARP: address resolution protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3.3 ICMP: internet control message protocol . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3.4 UDP: user datagram protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3.5 TCP: transmission control protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.6 BOOTP: bootstrap protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.7 DHCP: dynamic host configuration protocol . . . . . . . . . . . . . . . . . . . . . 15
2.3.8 TFTP: trivial file transfer protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.9 FTP: file transfer protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.10 PPP: point-to-point protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.11 SMTP: simple mail transfer protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.12 POP3: post office protocol version 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.13 SNMP: simple network management protocol . . . . . . . . . . . . . . . . . . . . 16
2.3.14 HTTP: hypertext transfer protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.15 Telnet: remote terminal protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6 Debug tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.1 NicheTool diagnostic console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.2 Network statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.3 Packet debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.4 Socket and packet queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7 NicheTask OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.1.1 Superloop (OS disabled) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.1.2 Multitasking system (OS enabled) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.2 Configuring the NicheTask OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.3 Implementing your own system menus and tasks . . . . . . . . . . . . . . . . . . 42
7.3.1 Task declaration (in demo_server.c file) . . . . . . . . . . . . . . . . . . . . . . . . 42
7.3.2 Task function definition (in demo_server.c file) . . . . . . . . . . . . . . . . . . . 43
7.3.3 Task creation (in tk_misc.c file) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
9 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
List of tables
List of figures
1 Glossary
2 TCP/IP introduction
!PPLICATION LAYER
4HE MAIN INTERFACE FOR THE USER 0ORT NUMBER ANDOR
(IGHER LEVEL PROTOCOLS SUCH AS 4&40 FOR FILE TRANSFER ARE OTHER SESSION IDENTIFIERS
IMPLEMENTED AT THIS LAYER
4RANSPORT ,AYER
0ORT NUMBER
%ND TO END CONNECTIONS 5$0 AND 4#0 AND RELIABILITY 4#0
)NTERNET ,AYER
)0 !DDRESS
4RANSFER DATA FROM SOURCE TO DESTINATION NO GUARANTEE OF DELIVERY
-!# ,AYER
-!# !DDRESS
-ANAGE COMMUNICATION BETWEEN NETWORK ENTITIES
$ATA ,INK PHYSICAL LAYERS FRAME MANAGEMENT PHYSICAL LAYERS ERROR DETECTION
,AYER
0HYSICAL ,AYER
4RANSPORT THE DATA ON THE PHYSICAL SUPPORT ELECTRIC SIGNALx
AI
!PPLICATION LAYER
4HE MAIN INTERFACE FOR THE USER 0ORT NUMBER ANDOR
(IGHER LEVEL PROTOCOLS SUCH AS 4&40 FOR FILE TRANSFER OTHER SESSION IDENTIFIERS
ARE IMPLEMENTED AT THIS LAYER
4RANSPORT LAYER
%ND TO END CONNECTIONS 5$0 AND 4#0 0ORT NUMBER
AND RELIABILITY 4#0
)NTERNET LAYER
,
4RANSFER DATA FROM SOURCE TO DESTINATION )0 ADDRESS 2OUTERS
NO GUARANTEE OF DELIVERY
-!# ,LAYER
-ANAGE COMMUNICATION BETWEEN NETWORK ENTITIES
3WITCHES
PHYSICAL LAYERS FRAME MANAGEMENT -!# ADDRESS
$ATA ,INK LAYER PHYSICAL LAYERS ERROR DETECTION
0HYSICAL LAYER
2EPEATING
4RANSPORT THE DATA ON THE PHYSICAL SUPPORT
ELECTRICAL SIGNAL HUBS
AI
● Contrary to switches and hubs, routers are able to interconnect several networks by
operating at the network layer and using the IP addresses of the packets.
These are simple examples, many products operate at different layers. For example “layer 3
switches” are able to perform some router operations by using IP addresses (layer 3 or
network layer) while also performing their data link layer operations (also called layer 2
operations).
!PPLICATION ,AYER
EMAIL /THER #ONFIGURATION
4RANSPORT ,AYER
4#0 5$0
0ORT NUMBER
)NTERNET ,AYER
2OUTERS )#-0 )0
)0 !DDRESS
!20
,INK ,AYER -EDIA !CCESS #ONTROL
%THERNET
3WITCHES
-!# !DDRESS -!# 000
/THER
2EPEATING 0HYSICAL
0HYSICAL ,AYER 0(9
(UBS ,AYER
AI
On the right part of Figure 3 you can see which layer the most common protocols operate at
and what the interconnections are between these protocols.
To illustrate this further we can follow the “path” of some user data, a user file for example, to
be sent over the network from a device A (MAC Address = AA-AA-AA-AA-AA-AA / IP
address = 128.128.128.128) to a device B (MAC Address = BB-BB-BB-BB-BB-BB / IP
address = 129.129.129.129). This is shown in Figure 4.
Note: Please note that these MAC and IP addresses may be invalid, they are just chosen for this
example.
!PPLICATION ,AYER
EMAIL
E
EMAL
AI /THER
/
/T HER #ONFIGURATION
#ONF
N IGURATI
A ON
IDENTIFIERS A SF ER
&IL E 4RANSFER
4RANS 3.-0 $OMAIN
$OMAI
A N .AMES
.AM
A ES
E
0ING &40
&
&40 4&40 $.3
4RANSPORT ,AYER
4#0 5$0
0ORT NUMBER
)NTERNET ,AYER
2OUTERS )#-0 )0
)0 !DDRESS
!20
,INK ,AYER -EDIA !CCESS #ONTROL
%THERNET
3WITCHES
/THER
-!# !DDRESS -!# 000
2EPEATING 0HYSICAL
0HYSICAL ,AYER 0(9
(UBS ,AYER
AI
5SER
5SER $ATA
!PPLICATION LAYER
4RANSPORT ,AYER
)NTERNET ,AYER
$ATA
,INK -!#
,AYER !DDRESS
%THERNET (EADER )0 (EADER 5$0 (EADER 4&40 (EADER 5SER $ATA %THERNET 4RAILER
AI
)NTERMEDIATE )NTERMEDIATE
NETWORK NODE NETWORK NODE
$ESTINATION DEVICE
$ESTINATION )0 ADDRESS )0 PACKET # )NTERMEDIATE
NETWORK NODE
2ECEPTION ORDER
0ACKET #
)0 PACKET !
0ACKET !
0ACKET ! DUPLICATED
)0 PACKET !
AI
BIT SOURCE PORT NUMBER BIT DESTINATION PORT NUMBER
5$0
(EA DER BIT 5$0 LENGTH BIT 5$0 CHECKSUM
AI
The UDP is a connectionless protocol and has the same drawbacks as the IP layer. It is still
used by a lot of protocols and applications. The simplicity of UDP is an advantage for
applications where the upper layers already implement mechanisms that ensure the
reliability of the communication, or for protocols that do not require this reliability.
)NTERMEDIATE )NTERMEDIATE
NETWORK NODE NETWORK NODE
)0 0ROTOCOL )0 5$0 PACKET #
$ESTINATION )0 5$0 PACKET ! )NTERMEDIATE
)0 ADDRESS )0 5$0 PACKET ! NETWORK NODE
!CKNOWLEDGE
4#0 0ROTOCOL 4#0 PACKET !
4#0SRCPORT $ATA 4#0 PACKET "
4IMEOUT RETRANSMISSION 4#0 PACKET #
)0 0ROTOCOL )0 5$0 PACKET !
)0 5$0 PACKET "
3OURCE )0 ADDRESS )NTERMEDIATE
)0 5$0 PACKET # NODE
)0 5$0 PACKET " RETRANSMITTED
)NTERMEDIATE )NTERMEDIATE
NODE NODE
)0 0ROTOCOL )0 5$0 PACKET #
$ESTINATION
)0 5$0 PACKET !
)NTERMEDIATE
)0 ADDRESS )0 5$0 PACKET ! NODE
)0 5$0 PACKET " RETRANSMITTED
4#0 0ROTOCOL 4#0 PACKET #
4#0$EST
4#0 PACKET !
$ATA 4#0 PACKET !
PORT
5$0 PACKET " RETRANSMITTED
!CKNOWLEDGE
AI
This reliability is achieved with the following mechanisms implemented in the TCP:
● A checksum to check the validity of the datagram like other protocols
● An acknowledge mechanism for the receiver to confirm to the sender the proper
reception of a packet. If the sender does not receive this acknowledge after a timeout it
resends the packet.
● If needed, the data to send over TCP is split in segments, each segment being
transmitted in a separate TCP packet, the receiver has a re-sequencing mechanism to
reorder packets if they are received out of order.
● Duplicated packets are identified and discarded by the TCP protocol
● A TCP window mechanism to allow a device to specify the buffer size available and
prevent another device from sending more data than it can handle.
SMTP is a protocol for transmitting emails between devices that are always connected to the
networks. It has no features for retrieving messages from a server (see POP3)
This section provides an overview of the NicheLite TCP/IP stack. Section 3.1 introduces the
structure of the stack. Section 3.2 describes the protocols available in the stack. Section 3.1
gives an introduction to the NicheLite stack implementation including packet demultiplexing
and buffer management.
!PPLICATION
4#0 )0 3TACK
AI
!PPLICATION
4#0 )0 3TACK
4OOLS
3CHEDULER /3
.ICHE4A SK
0ACKET -ANAGER
(ARDWARE !BSTRACTION ,AYER
(EAP -ANAGER
%THERNET DRIVER /THER 0ERIPHERALS
34- (ARDWARE
AI
4RANSPORT LAYER
4#0 5$0
0ORT NUMBER
)NTERNET LAYER
2OUTERS )#-0 )0
)0 ADDRESS
!20
,INK LAYER -EDIA !CCESS #ONTROL
%THERNET
3WITCHES
/THER
-!# ADDRESS -!# 000
2EPEATING 0HYSICAL
0HYSICAL LAYER 0(9
(UBS LAYER
AI
4RANSPORT ,AYER
4#0 5$0
0ORT NUMBER
)NTERNET ,AYER
2OUTERS )#-0 )0
)0 !DDRESS
!20
,INK ,AYER
%THERNET
2EPEATING
0HYSICAL ,AYER 0(9
(UBS
AI
NicheTool
NicheTool is a UART user interface, that allows commands to be sent using a HyperTerminal
window. The commands already implemented in NicheLite are useful for performing some
TCP/IP stack diagnostics. NicheLite provides APIs that allow you to easily add your own
commands.
ZeroCopy
NicheLite implements a zero copy mechanism, this means that the encapsulation described
in Section 2.2 is done without any recopy when the data are passed from one layer to the
other. This increases the performance.
Minisocket API
NicheLite also implement a mini-socket API that differs from the BSD socket API used by
some other implementations. The mini-socket API is designed to be as close as possible to
the BSD socket API while still allowing a small footprint. The primary differences are that
passive connections are accomplished with a single call, m_listen(), rather than the BSD
bind()-listen()-accept() sequence. The BSD select() call is also replaced with a callback
mechanism.
BSD = Berkeley Software Distribution TCP/UDP/IP Stack IP
m_socket() socket()
m_connect() connect()
m_recv() and/or m_send() - or - tcp_send()
recv() and/or send()
and/or tcp_recv() - (zero-copy I/O)
m_close() close()
!PPLICATION ,AYER
5SER 5$0 #ALLBACK /THER 5$0 5SER 4#0 #ALLBACK /THER 4#0
0ORT NUMBER FUNCTION POINTER MODULES FUNCTION POINTER MODULES
ANDOR OTHER
SESSION IDENTIFIERS
4RANSPORT ,AYER
0ORT NUMBER
(ANDLE 5$0UDPDEMUX (ANDLE 4#0 0ACKET TCP?RCV
)NTERNET ,AYER
)0 !DDRESS
(ANDLE )#-0 ICMPRCV )0 $EMUX IP?RCV
-!# !DDRESS
34- %THERNET $RIVER
(EAP
5SED 5SED
5SED 5SED
5SED
5SED
5SED
5SED
5SED
5SED
!VAILABLE -EMORY
AI
2. Once allocated from the Heap, the buffers are organized in 2 linked lists:
– Big buffer free queue
– Little buffer free queue
FREE
FREE
FREE
FREE
AI
3. During the initialization, the Ethernet driver allocates 2 big buffers from the free queue.
These buffers are used to store the frame are received by the Ethernet peripheral.
FREE FREE
"IG BUFFER FREE
QUEUE
FREE FREE FREE
STM32 driver
!LLOCATED !LLOCATED
AI
4. Upon reception of a frame, the STM32 Ethernet driver copies the data received by the
Ethernet peripheral into one of the 2 previously allocated buffers.
Then this buffer is moved to another queue: the received packet queue. The received
packets are stored in this queue until the stack is able to process it.
This received packet queue can be seen as a kind of cache that allows the reception
mechanism and the TCP/IP stack to act as 2 separate tasks.
FREE
"IG BUFFER FREE
QUEUE
FREE FREE
34- DRIVER
$ATA !LLOCATED
2ECEIVED PACKET
QUEUE !LLOCATED
$ATA
AI
5. Then the driver reallocates a buffer from the big buffer free queue.
The TCP/IP stack handles the received packet, as described in Section 3.3.1. If a UDP
or TCP packet is received and if the corresponding UDP or TCP listener exists, then the
buffer pointer is provided to the user application.
FREE
"IG BUFFER FREE QUEUE
FREE FREE
34- DRIVER
!LLOCATED !LLOCATED
2ECEIVED PACKET
QUEUE
5SER APPLICATION
$ATA
!LLOCATED $ATA
!LLOCATED
AI
6. Once the packet is handled by the user, it has to be given back to the big buffer queue.
Please note that for performance reasons, the data is not erased.
FREE $ATA
"IG BUFFER FREE
QUEUE
FREE
34- D RIVER
!LLOCATED !LLOCATED
2ECEIVED
5SER A PPLICATION
PACKET
!LLOCATED
$ATA
QUEUE
AI
Several listeners
An application can have several listeners for example, one listener for a UDP-based
application and another for a TCP-based application (see Figure 21).
This context has to be identified and checked carefully.
For example, in Figure 21, the 2 listeners (UDP and TCP) are using several buffers and
there is no big buffer left in the big buffer free queue.
This situation is serious but the application is still able to:
● receive 2 packets (there are still 2 buffers allocated in the driver)
● send out some data if they fit in a small buffer (and if there is a small buffer available in
the small buffer free queue)
This will become a critical situation if:
● 2 more buffers are received and the UDP or TCP application does not release the
buffers
● the application needs to send some data that do not fit in a small buffer, and there is no
way of allocating a big buffer to this sending operation
Figure 21. Memory management: a serious situation that may become critical
/THER USER
"IG BUFFER FREE APPLICATION
QUEUE EG 5$0 !LLOCATED
$ATA
34- DRIVER
!LLOCATED !LLOCATED
2ECEIVED
5SER APPLICATION
PACKET
!L $ATA EG 4#0
QUEUE $ATA
AI
No free buffers
If the buffer requirements of an application are underevaluated, or if the buffers are not
returned to the free queue, then we may be in this context.
In this example, all the buffers are kept by the application, the next data received are lost as
there is no buffer left at driver level (see Figure 22) and the application is not able to send
any data (unless they fit in a small buffer).
/LD DATA
!LLOCATED
"IG BUFFER FREE
QUEUE /THER USER
APPLICATION
EG 5$0
$ATA IN USE
!LLOCATED
34- DRIVER
.EW DATA $ATA IN USE
!LLOCATED
$ATA IN USE
!LLOCATED
2ECEIVED
5SER APPLICATION
PACKET
EG 4#0
QUEUE $ATA IN USE
!LLOCATED
AI
4.2 IP address
The application could use a fixed IP address (static) each time it powers up, or it can get an
IP address assigned automatically by a server using the Dynamic Host Configuration
Protocol (DHCP).
The configuration of the IP address is performed in the function pre_task_setup() in the
in_stubs.c file.
Here is an example of an implementation taken from the STM32 demo:
#ifdef DHCP_CLIENT
netstatic[0].n_flags |= NF_DHCPC;
netstatic[i].n_ipaddr = htonl(0x0a0000c5); /* 10.0.0.197 */
#else
netstatic[i].n_ipaddr = htonl(0x0a0000c5); /* 10.0.0.197 */
#endif
netstatic[i].snmask = htonl(0xffffff00); /* 255.255.255.0 */
netstatic[i].n_defgw = htonl(0x0a00008A); /* 10.0.0.138 */
i++;
When using a static IP address you can modify the default address defined by the n_ipaddr
field:
#else
netstatic[i].n_ipaddr = htonl(0x0a0000c5); /* 10.0.0.197 */
#endif
When DHCP is enabled (DHCP_CLIENT is set to 1 in ipport.h file), the n_ipaddr field is
ignored and stack initialization finishes only after the DHCP transaction is completed and a
dynamic IP address is acquired. Otherwise if the DHCP fails the static IP address is
assigned.
4.4 Keepalive
A keepalive is a message sent by one device to another to check that the link between the
two is operating. The message is short and sent at programmable intervals so as not to take
too much bandwidth on the network.
In the TCP/IP stack, the exchanged data (also known as payload) are managed as packets
which are stored in the device internal RAM memory.
When the TCP/IP stack execution starts, all packet buffers are allocated in the Heap
memory (located in RAM) for the entire application run time.
All packets are placed in a pool of free packets (free queue) after allocation. When a packet
is used by the stack it is moved out of this pool and back into it when it is no longer needed
by the stack. The management of the packet buffer queue where packet data are stored is
performed in the pktalloc file.
For more details on buffer management, refer to Section 3.3.2: Packet buffer memory
management on page 23.
These packet buffers are used for the management of the stack's little and big buffers. The
number of these buffers depends mainly on:
● Ethernet frame size (max payload exchanged on the network)
● Application performance and RAM size requirements (which define the Heap memory
size)
However you can modify the number of big and little buffers based on the application
requirements and the characteristics of the network.
Here are some cases to be considered:
● In an application that is mainly sending little packets, increasing the number of little
buffers and decreasing the number of big buffers can be a good option.
● If the network traffic is mainly little packets, reducing the Ethernet frame size (i.e. size of
big buffer) may be another option as well.
● If connected on a public network you may have a lot of broadcast packets sent by
servers or other devices, these packets add an extra load to your application. In this
case the number of big buffers should be adjusted accordingly.
● It can be desirable to increase the number of buffers available in an embedded system
to let the application working perfectly even in the cases where the device receives
more packets than expected in an upgraded network. You should make sure to have
free buffers available during the application execution, to avoid situations when all
buffers are allocated and a need for a new buffer occurs. In this case the new packet is
discarded which may impact the stability of the application.
Heap allocation
The Heap size allocated for the application depends on the application performance
requirements and available RAM memory.
Increasing the Heap size (with a higher number of packet buffers) boosts the application
performance however this costs RAM memory size. Decreasing the Heap size saves more
RAM memory space but the application performance is limited.
The memory allocation listed below is provided as example and it should be tailored to meet
your application requirements. To ensure the robustness of the final application and to
guarantee proper functioning in the worst case, you have to make sure that the application is
tested in a network environment similar to the one that the device is to be linked to.
The heap size is configurable:
● For RVMDK projects: this variable is defined in the startup_stm32f10x_cl.s file as
follows:
Heap_Size EQU 0x00006400
● For EWARM projects: this variable is defined in the stm32f10x_flash.icf file as follows:
define symbol __ICFEDIT_size_heap__ = 0x6400
The Heap size can be retrieved at the application level using getHeapSize() function, that
can be used for example to initialize the Heap when using memory management routines
(such calloc1() and mem_free() which are the embedded systems version of standard
calloc() & free() functions)
Heap_Size = getHeapSize();
6 Debug tips
Some commands are available depending on the stack configuration in the ipport.h file (for
example, the ipstat command in supported only if NET_STATS is defined). For the complete
list and description of available commands please refer to the NicheLite documentation.
For more information on how to implement your own system menu, please refer to
Section 7.3.
0ACKET LOG
FREE
"IG BUFFER
FREE QUEUE
FREE
34-
!LLOCATED !LLOCATED
DRIVER
2ECEIVED 5SER
PACKET $ATA
APPLICATION
QUEUE
AI
Another way to debug a packet buffer is to type the buffers command from the console, in
this case you get more information about the buffer type and dump of its content…
INET> buffers
20001390,1527,200013D4,big:40 40 01 00 5E 00 00 02 00 13 60 74 @@..^.....`t
200019D0,1527,20001A14,big:63 69 01 80 C2 00 00 00 00 14 1C DF ci..........
20002010,1527,20002054,non:FF 41 00 53 54 91 00 02 00 13 C4 A8 .A.ST.......
20002650,1527,20002694,big:FF 41 00 53 54 91 00 02 00 13 C4 A8 .A.ST.......
20002C90,1527,20002CD4,big:63 69 00 00 0C 07 AC 0C 00 53 54 91 ci.......ST.
200032D0,1527,20003314,big:40 40 00 53 54 91 00 02 00 13 C4 A8 @@.ST.......
20003910,1527,20003954,big:FF 41 00 53 54 91 00 02 00 13 C4 A8 .A.ST.......
20003F50,1527,20003F94,big:FF 41 00 00 0C 07 AC 0C 00 53 54 91 .A.......ST.
20004590,1527,200045D4,big:40 40 01 00 5E 00 00 05 00 13 60 74 @@..^.....`t
20004BD0,1527,20004C14,big:40 40 00 53 54 91 00 02 00 13 C4 A8 @@.ST.......
20005210,1527,20005254,non:63 69 00 00 0C 07 AC 0C 00 53 54 91 ci.......ST.
Additional information about other queues can be obtained by typing the queue command
from the console:
INET> queue
bigfreeq: head:20003910, tail:200032D0, len:9, min:8, max:11
lilfreeq: head:20005968, tail:20005850, len:11, min:10, max:11
rcvdq: head:00000000, tail:00000000, len:0, min:0, max:0
The line about rcvdq indicates how big the packet receive queue has gotten. rcvdq len
reports the number of packets in the receive queue that have not yet been processed by the
IP layer. rcvdq max indicates how high len has gotten since the stack start. A high value in
rcvdq max would be an indication that the stack is not processing the receive queue in a
timely manner.
7 NicheTask OS
7.1 Overview
The NicheLite TCP/IP stack presents two operating modes. Both modes allow you to run
multiple processes or tasks simultaneously.
netmain_init_done = 1;
cp = post_task_setup();
if (cp)
panic(cp);
}
#endif
}
Tasks can be created and deleted dynamically by calls to the tasking API. Each task has a
stack and a task control structure. Tasks are created via the routine tk_new(), which returns
a pointer to this structure. This pointer is thereafter used as a task ID.
On task creation, a new stack is allocated from heap (RAM memory) for that task. The size
of each stack is static and determined at compile time. When a task is killed, the allocated
stack memory is released.
There is a task control structure associated with each task instance, and a defined type task
(defined in task.h file), which is a synonym of that structure. Each task structure contains a
pointer to the tasks stack space.
struct task
{
struct task * tk_next; /* pointer to next task */
stack_t * tk_fp; /* task's current frame ptr */
char * tk_name; /* the task's name */
int tk_flags; /* flag set if task is scheduled */
unsigned long tk_count; /* number of wakeups */
stack_t * tk_guard; /* pointer to lowest guardword */
unsigned tk_size; /* stack size */
stack_t * tk_stack; /* base of task's stack */
void * tk_event; /* event to wake blocked task */
unsigned long tk_waketick; /* tick to wake up sleeping task
*/
};
The task control structure forms a circular list, chained by the tk_next member of the
structure. The scheduler is a round robin scheduler; it simply loops through the circular list
of tasks until it finds one which is ready to run (that is, a task whose tk_flags member value
is equal to TF_AWAKE) and then switches to that task. A context switch merely consists of
saving a small amount of state on the stack and calling the routine tk_switch() (which is
called by tk_block()).
When a task stack is allocated, it is filled with guardword: a predefined constant used to
track stack usage. The tk_guard field points to the last (lowest on STK_TOPDOWN
systems) word on the stack. On every context switch, this word is checked to verify that it
still contains a guardword. If it doesn't, the tasking scheduler assumes that the task had a
stack overflow and aborts the system with a call to the panic() routine. Guardwords are also
used by the tk_stats() function to determine how much of the stack has been used (you can
invoke the kstats command from NicheTool console to get this information).
There is a global variable, tk_cur, which is a pointer to the task control structure of the task
which is currently running.
For complete documentation about the NicheTask OS you can refer to
http://www.nichetask.com/
● Libraries folder
– STM32_ETH_Driver subfolder: contains the C source for STM32F107xx Ethernet
low level driver (stm32_eth.c/.h files).
– CMSIS subfolder: contains the STM32F10x CMSIS files
– STM32F10x_StdPeriph_Driver subfolder: contains C sources for STM32F10x
Standard Peripherals drivers
● NicheLite_docs folder: contains NicheLite html documentation.
● NicheLite_src folder: contains NicheLite code source files.
– allports and target subfolders(a): contains all of the port-specific code necessary to
run NicheLite, with the NicheTask OS on STM32F107xx
– h subfolder(b): contains NicheLite header files
– mip subfolder(c): contains the files implementing the IP, ARP, ICMP and most part
of the UDP protocols
– misclib subfolder(c): several miscellaneous files
– mtcp subfolder(c): contains the files implementing the mini socket and TCP
protocols
– net subfolder(c): contains file implementing the DHCP, DNS protocols and the
buffer management
– tftp subfolder(c): contains the files implementing the TFTP protocol
– vfs subfolder(c): implementation of the virtual file system used by the NicheLite
stack
● STM3210C_EVAL folder: contains LCD driver for STM3210C_EVAL board
● Project folder(a): contains the Demos source files and preconfigured projects for
EWARMv5 (IAR) and RVMDK (Keil) toolchains.
a. These folders contains target dependant files; files that are modified to port the stack to STM32F107xx
b. This directory contains both target independent and dependent files. ipport.h file is the most widely included
target file, please refer to Section 4.1 for more details.
c. These folders contain target independent files; files that are mostly identical from one port to another
Prerequisites:
1. Make sure that client and server boards have different MAC addresses. For each board
you have to modify the value of the MAC_ADDRx (x = 1 to 5) defines in the stm32.c file
for the MAC address setting.
2. The board is configured as Server or Client depending on PE13 pin level after reset:
a) PE13 pin level ow (connected to GND): board is configured as Client.
b) PE13 pin level high (connected to VDD): board is configured as Server.
pkt->fhost = 0xFFFFFFFF
Task state is set to CONNECTING state; it is modified to CONNECTED state from UDP
callback when the application receives the UDP Server answer.
Note that Client has to start after Server to be sure server get UDP request.
For this example of a “private” protocol implementation, only data=98 received is processed.
It is the server answer sent in response to the data=99 request in UDP_ServerAddrReq. It is
used to get the server address to be able to communicate through TCP:
if(pkt->nb_prot[0] == '9'&& pkt->nb_prot[1] == '8')
{ /* Server addr answer*/
applicationServerAddr = pkt->fhost; /* extract server address */
...
}
Socket parameters
The application creates a socket for TCP communication on a defined port number and
server IP address.
client_sin.sin_family = AF_INET;
client_sin.sin_addr.s_addr = applicationServerAddr; /*Server IP
address*/
client_sin.sin_port = CLIENT_PORT;/*port used by the application
example*/
Socket creation
client_sock = m_socket();
Connection parameters
client_sock->so_options |= SO_KEEPALIVE;
client_sock->lport = CLIENT_PORT;
client_sock->state |= SS_NBIO; /* Non blocking socket !!!!*/
Keepalive is enabled to check that the link between client and server is operating.
The same port number is used for client (local) and server (remote) ports.
It is mandatory to set the connection state as non blocking to be able to process the server
connection acknowledge from the server.
Open connection
This sends a connection request to the server.
m_connect(client_sock, &client_sin, client_upcall);
The client processing is handled in the demo_client.c file and is presented in Figure 26 on
page 49.
5$0
#LIENT
#,)%.4?#/..%#4).' #ALLBACK
OR #,)%.4?34/00%$ #LIENT #,)%.4?)$,%
/PEN 5$0
STATE CONNECTION
#,)%.4?#/..%#4%$
.OT
2ESULT #,)%.4?34/00%$
ST RD
.OT DATA
DATA 3ET ,%$
BYTE BYTE
2ESET ,%$
#,)%.4?#(%#+ FUNCTION
.OT
#,)%.4?#/..%#4%$
AI
The client task loops and calls the client_init() or client_check() functions depending on the
value of the Client_state variable.
The udp_client_upcall() and client_upcall() functions asynchronously process UDP and
TCP received frames.
Socket parameters
The application creates a socket for TCP communication on the defined port number and for
every IP address.
server_sin.sin_family=AF_INET;
server_sin.sin_addr.s_addr = INADDR_ANY; /* wildcard IP
address */
server_sin.sin_port = SERVER_PORT; /* port used by the
application example*/
Open connection
The server application opens a socket and waits for a connection request from client.
m_listen(&server_sin, server_upcall, &e);
Callback functions
The udp_server_upcall and server_upcall functions asynchronously process UDP and TCP
received frames.
The udp_server_upcall function manages UDP request at packet level.
udp_server_upcall(PACKET pkt, void * data)
For example, to receive server IP address, the client application sends a UDP request on
SERVER_PORT with data = “99”. The server application answers with data = “98”.
udp_send(SERVER_PORT, SERVER_PORT, pkt_send)
The server_upcall callback function manages the TCP request at socket level.
server_upcall(int code, M_SOCK so, void * datapkt)
Where:
● 1st parameter code is the status of the connection
● 2nd parameter is the socket structure
● The last parameter can be a connection request from the client or data received on the
socket from the client.
The server_upcall callback doesn't directly process this information; it saves information that
is processed during the server_check function call.
The server processing is handled in the demo_server.c file and is presented in Figure 27 on
page 52.
5$0
3ERVER
#ALLBACK
3%26%2?34/00%$ 3%26%2?)$,%
3ERVER /PEN 5$0
STATE CONNECTION
3%26%2?25..).'
.OT
2ESULT 3%26%2?34/00%$
'ET?SO
GET SOCKET
4#0
3ERVER
.EW 9ES #ALLBACK
0ROCESS NEW SOCKET
SOCKET /PEN 4#0
CONNECTION
.O
4#0
.O DATA .OT
2ESULT 3%26%2?34/00%$
RECEIVED
9ES
0ROCESS COMMAND
3%26%2?25..).'
3ET2ESET ,%$
3%26%2?).)4 FUNCTION
2ESULT
3END !NSWER
3%26%2?#(%#+ FUNCTION
0UT SO
3AVE SOCKET
/0%.?/+
3END ANSWER
AI
9 Revision history
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.