Electoday 2025

ไมโครคอนโทรลเลอร์ => ARM Processors => Topic started by: tha on February 09, 2023, 12:44:22 PM

Title: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 09, 2023, 12:44:22 PM
UM1713

User manual

Developing applications on STM32Cube with LwIP TCP/IP stack

Introduction

STMCube™ เป็นความคิดริเริ่มของ STMicroelectronics ที่ช่วยให้ชีวิตนักพัฒนาง่ายขึ้นโดยลดความพยายาม เวลา และต้นทุนในการพัฒนา. STM32Cube ครอบคลุมผลงานของ STM32.

STM32Cube Version 1.x ประกอบด้วย:
•   The STM32CubeMX, a graphical software configuration tool ที่ช่วยให้สร้าง C initialization code โดยใช้ graphical
     wizards.
•   A comprehensive embedded software platform, ที่ส่งมอบต่อซีรีส์ (อย่างเช่น STM32CubeF4 สำหรับ STM32F4 series)
     –   The STM32Cube HAL, an STM32 abstraction layer embedded software, เพื่อให้มั่นใจว่าสามารถถือได้สูงสุดพาดผ่าน
          STM32 portfolio
     –   ชุดที่ตรงกันของ middleware components อย่างเช่น RTOS, USB, TCP/IP, Graphics
     –   ทุก embedded software utilities มาพร้อมกับชุดเต็มของตัวอย่าง.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 09, 2023, 01:57:00 PM
บาง STM32 microcontrollers มีคุณลักษณะ a high-quality 10/100 Mbit/s Ethernet peripheral ที่รองรับทั้ง Media Independent Interface (MII) และ Reduced Media Independent Interface (RMII) เพื่ออินเตอร์เฟสกับ the Physical Layer (PHY).

เมื่อทำงานกับ an Ethernet communication interface, ส่วนใหญ่ a TCP/IP stack จะถูกใช้สื่อสารผ่าน a local หรือ a wide area network.

คู่มือผู้ใช้นี้มีไว้สำหรับนักพัฒนาที่ใช้ STM32Cube firmware บน STM32 microcontrollers. มันจัดให้มีคำอธิบายแบบเต็มเกี่ยวกับวิธีการรวม a free middleware TCP/IP stack โดยใช้ STM32Cube HAL drivers ลงใน an embedded application บนฐานของ STM32 microcontroller

The middleware TCP/IP stack คือ the LwIP (Lightweight IP) ซึ่งเป็น an open source stack ที่มุ่งหมายสำหรับ embedded devices.

A dedicated STM32Cube firmware package ถูกจัดให้มีสำหรับแต่ละ series. มันประกอบด้วย Ethernet HAL driver, LwIP middleware และ application examples ที่มีและที่ไม่มี RTOS รันบน ST evaluation boards.

Note: เอกสารนี้ใช้ได้กับซีรีส์ STM32 ทั้งหมดที่มี an Ethernet peripheral. อย่างไรก็ตาม, หรือเหตุผลง่ายๆ, STM32F4xx and
          STM32CubeF4 ถูกใช้เป็นแพลตฟอร์มอ้างอิง. คำอธิบาย ชื่อไฟล์ และภาพหน้าจอเดียวกันนี้ใช้ได้กับซีรีส์อื่นๆ ที่มี Ethernet
          connectivity, อย่างเช่น STM32F107xx, STM32F2x7xx and STM32F7xx. หากต้องการทราบข้อมูลเพิ่มเติมเกี่ยวกับ the
          Ethernet examples implementation บน STM32 series ของคุณ, โปรดดูเอกสารที่จัดให้ไว้ภายใน the associated
          STM32Cube firmware package.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 09, 2023, 02:05:46 PM
(https://i.imgur.com/gfGXScY.png)
(https://i.imgur.com/WBPKAOr.png)
(https://i.imgur.com/EpLmaX8.png)
(https://i.imgur.com/vBCorrT.png)
(https://i.imgur.com/vX0JdvC.png)
(https://i.imgur.com/GjgYke5.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 09, 2023, 03:10:32 PM
1 LwIP TCP/IP stack description

1.1 Stack features

LwIP เป็น a free TCP/IP stack ที่พัฒนาโดย Adam Dunkels ที่ the Swedish Institute of Computer Science (SICS) และได้รับอนุญาตภายใต้ใบอนุญาต BSD ที่แก้ไขแล้ว.

จุดเน้นของการจัดให้มีใช้ LwIP TCP/IP คือเพื่อลดการใช้ RAM ในขณะที่คงไว้ a full scale TCP/IP stack. สิ่งนี้ทำให้ LwIP เหมาะสำหรับใช้ในระบบฝังตัว.

LwIP มาพร้อมกับโปรโตคอลต่อไปนี้:
•   IPv4 and IPv6 (Internet Protocol v4 and v6)
•   ICMP (Internet Control Message Protocol) for network maintenance and debugging
•   IGMP (Internet Group Management Protocol) for multicast traffic management
•   UDP (User Datagram Protocol)
•   TCP (Transmission Control Protocol)
•   DNS (Domain Name Server)
•   SNMP (Simple Network Management Protocol)
•   DHCP (Dynamic Host Configuration Protocol)
•   PPP (Point to Point Protocol)
•   ARP (Address Resolution Protocol)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 09, 2023, 04:03:51 PM
LwIP มีสาม application programming interfaces (APIs):
•   Raw API คือ the native LwIP API. มันช่วยให้สามารถพัฒนาแอปพลิเคชันโดยใช้ event callbacks. API นี้จัดให้มี the best
     performance และปรับให้ code size ดีขึ้น, แต่จะเพิ่มความซับซ้อนบางประการให้กับ application development.
•   Netconn API คือ a high-level sequential API ที่ต้องการ a real-time operating system (RTOS). The Netconn API เปิดใช้
     งาน multithreaded operations.
•   BSD Socket API: Berkeley-like Socket API (พัฒนาต่อยอดจาก the Netconn API)

The source code สำหรับ the LwIP stack สามารถดาวน์โหลดได้จาก http://savannah.nongnu.org.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 09, 2023, 04:17:09 PM
1.2 License

LwIP ได้รับใบอนุญาตภายใต้ the BSD license. ด้านล่างนี้คือสำเนาของ the LwIP license document ที่ถูกรวมอยู่ใน the source codes:

(https://i.imgur.com/RrKP5Rf.png)
(https://i.imgur.com/xn1Bvco.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 10, 2023, 06:24:12 AM
1.3 LwIP architecture

LwIP เป็นไปตาม the TCP/IP model architecture ซึ่งระบุวิธีการที่ data ถูกจัดรูปแบบ, ส่ง, กำหนดเส้นทาง และรับเพื่อจัดให้มี end-to-end communications.

โมเดลนี้มีสี่ abstraction layers ซึ่งถูกใช้เพื่อจัดเรียงโปรโตคอลที่เกี่ยวข้องทั้งหมดตามขอบเขตของเครือข่ายที่เกี่ยวข้อง (ดูรูปที่ 1) จากต่ำสุดไปสูงสุด, the layers คือ:
•   The link layer บรรจุ communication technologies สำหรับ a single network segment (เชื่อมโยง) ของ a local area
     network.
•   The internet layer (IP) เชื่อมต่อ independent networks, จึงสร้าง internetworking.
•   The transport layer จัดการ host-to-host communications.
•   The application layer บรรจุทุก protocols สำหรับ specific data communications services ในระดับ a process-to-
     process level.

(https://i.imgur.com/nKBGdOD.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 10, 2023, 11:26:29 AM
1.4 LwIP stack folder organization of the

เมื่อ unzipped, the LwIP stack files สามารถถูกพบภายใต้ \Middlewares\Third_Party\LwIP.

(https://i.imgur.com/3LjsGZv.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 10, 2023, 01:27:50 PM
1.5 LwIP API overview

ดังที่กล่าวมาข้างบน, สามประเภทของ APIs ถูกนำเสนอโดย LwIP stack:
•   Raw API
•   Netconn API
•   Socket API
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 10, 2023, 01:42:40 PM
1.5.1 Raw API

The Raw API ตั้งอยู่บนฐานของ the native LwIP API. มันถุกใช้ในการพัฒนา callback-based applications.

เมื่อจะเริ่มต้น the application, ผู้ใช้จำเป็นต้องลงทะเบียน callback functions ให้กับ core events ต่างๆ (อย่างเช่น TCP_Sent, TCP_error,...). The callback functions จะถูกเรียกจาก the LwIP core layer เมื่อ the corresponding event เกิดขึ้น.

ตารางที่ 1 จัดให้มีสรุปรวมของ the Raw API functions สำหรับ TCP applications.
(https://i.imgur.com/XSTweAi.png)
(https://i.imgur.com/dQzZQ3R.png)

(https://i.imgur.com/E0jOuqh.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 10, 2023, 02:53:34 PM
1.5.2 Netconn API

The Netconn API เป็น a high-level sequential API ซึ่งรูปแบบของการปฏิบัติถูกตั้งฐานอยู่บน the blocking open-read-write-close paradigm.

เพื่อทำงานอย่างถูกต้อง, API นี้ต้องรันใน a multithreaded operating mode ที่จัดให้มีใช้ thread เฉพาะสำหรับ the LwIP TCP/IP stack และ/หรือหลาย threads สำหรับ the application.

Table 3 จัดให้มีสรุปรวมของ the Netconn API functions.

(https://i.imgur.com/kKcUJfq.png)
(https://i.imgur.com/hW39OAf.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 10, 2023, 03:46:10 PM
1.5.3 Socket API

LwIP เสนอ the standard BSD socket API. นี้คือ a sequential API ซึ่งถูกสร้างขึ้นภายในบนส่วนบนของ the Netconn API.

Table 4 จัดให้มีสรุปรวมของ the main socket API functions

(https://i.imgur.com/Z7jCErd.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 05:24:36 AM
1.6 LwIP buffer management

1.6.1 Packet buffer structure

LwIP จัดการ packet buffers โดยใช้ a data structure ที่เรียกว่า pbuf. The pbuf structure ช่วยให้สามารถจัดสรร a dynamic memory เพื่อเก็บ a packet content และปล่อยให้ packets อยู่ใน the static memory.

Pbufs สามารถเชื่อมต่อกันเป็นสายโซ่ได้ จึงทำให้ packets สามารถทอดข้ามครอบคลุมไปหลาย pbufs ได้.

(https://i.imgur.com/9J891Pw.png)

โดยที่
     next บรรจุ the pointer ไปยัง the next pbuf ใน a pbuf chain
     payload บรรจุ the pointer ไปยัง the packet data payload
     len คือความยาวของ the data content ของ the pbuf
     tot_len คือผลรวมของ pbuf len บวกกับทุก the len fields ของ the next pbufs ใน the chain
     ref คือ the 4-bit reference count ที่แสดงให้เห็นจำนวนของ pointers ทีชี้ไปยัง the pbuf. A pbuf สามารถถูกปลดปล่อย
     ออกจาก memory ได้ก็ต่อเมื่อ reference count ของมันเป็นศูนย์.
     flags (บน 4 bits) แสดงให้เห็นประเภทของ pbuf.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 07:29:55 AM
LwIP กำหนดสามประเภทของ pbufs, ขึ้นอยู่กับประเภทการจัดสรร:
•   PBUF_POOL
     pbuf allocation ถูกดำเนินการจาก a pool ของ pbufs ที่จัดสรรไว้ล่วงหน้าแบบคงที่ของขนาดที่กำหนดไว้ล่วงหน้า. ข้นอยู่กับ the
     data size ที่ต้องถูกจัดสรร หนึ่งหรือหลาย chained pbufs ถูกต้องการ.
•   PBUF_RAM
     pbuf ถูกจัดสรรแบบไดนามิกใน memory (หนึ่งก้อนที่ติดต่อกันของ memory สำหรับ the full pbuf)
•   PBUF_ROM
     ไม่มี memory space allocation ถูกต้องการสำหรับ user payload: the pbuf payload pointer จะชี้ไปยัง data ใน ROM
     memory ที่สามารถถูกใช้เฉพาะสำหรับการส่ง constant data.

สำหรับการรับแพ็คเก็ต, the suitable pbuf type คือ PBUF_POOL. มันช่วยให้สามารถจัดสรรหน่วยความจำได้อย่างรวดเร็วสำหรับ the packet ที่รับจาก the pool of pbufs. ขึ้นอยู่กับขนาดของแพ็กเก็ตที่ได้รับ, หนึ่งหรือหลาย chained pbufs ถูกจัดสรร. The PBUF_RAM ไม่เหมาะสำหรับ packet reception เนื่องจากการจัดสรรแบบไดนามิกใช้เวลาล่าช้า นอกจากนี้ยังอาจนำไปสู่การแยกส่วนหน่วยความจำ.

สำหรับการส่งแพ็คเก็ต ผู้ใช้สามารถเลือกประเภท pbuf ที่เหมาะสมที่สุดตามข้อมูลที่จะถูกส่ง
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 08:50:52 AM
1.6.2 pbuf management APIs

LwIP มี a specific API สำหรับการทำงานกับ pbufs. API นี้ถูกจัดให้มีใช้ใน the pbuf.c core file.

(https://i.imgur.com/TJzwSWx.png)

Note:  'pbuf' สามารถเป็น a single pbuf หรือ a chain of pbufs.

           เมื่อทำงานกับ the Netconn API, netbufs (network buffers) จะถูกใช้สำหรับ sending/receiving data.

           A netbuf เป็นสิ่งห่อหุ้มอย่างง่ายสำหรับ a pbuf structure. มันสามารถรองรับได้ทั้ง allocated and referenced data.

           API เฉพาะ (ที่จัดให้มีใช้ใน file netbuf.c) ถูกจัดให้มีสำหรับการจัดการ netbufs (จัดสรร, ปลดปล่อย, ผูกมัด, แยก data,...).
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 09:54:41 AM
2 Interfacing LwIP with STM32Cube Ethernet HAL driver

แพ็คเกจนี้ประกอบด้วยการใช้งานสองแบบ:
•   การใช้งานโดยไม่มี operating system (standalone)
•   การใช้งานกับ an operating system โดย CMSIS-RTOS API

สำหรับทั้งสองการใช้งาน, the ethernetif.c file ถูกใช้เพื่อเชื่อมโยง the LwIP stack กับ the STM32 Ethernet network interface.

The port of LwIP stack ที่ต้องถูกต่อกับ STM32F4xx จะอยู่ใน the "lwip/system" folder.

The Ethernet handle of the HAL (ETH_HandleTypeDef) ควรถูกประกาศใน the ethernetif.c file, เช่นเดียวกับ the Ethernet DMA descriptors (ETH_DMADescTypeDef) และ the Rx/Tx buffers of the Ethernet driver.

Table 6 จัดให้มีคำอธิบายของ the LwIP interface API.

(https://i.imgur.com/T5BRtXE.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 10:26:37 AM
ตัวอย่างต่อไปนี้แสดงวิธีเริ่มต้น the Ethernet peripheral, โดยใช้ HAL API, ลงใน the interface API:

(https://i.imgur.com/Py1g6lF.png)
(https://i.imgur.com/8f0kpZt.png)

The ethernet_input() function implementation จะแตกต่างกันระหว่าง standalone และ RTOS modes:
•   ใน standalone applications, ฟังก์ชันนี้ต้องใส่เข้าไปใน the main loop of the application เพื่อวนตรวจสำหรับ received
     packet ใดๆ.
•   ใน RTOS applications, ฟังก์ชันนี้ถูกจัดให้มีใช้เป็น a thread ที่รอ a semaphore เพื่อจัดการ a received packet. The
     semaphore ถูกให้เมื่อ the Ethernet peripheral สร้าง an interrupt สำหรับ a received packet.

The ethernetif.c file จัดให้มีใช้ the Ethernet peripheral MSP routines สำหรับ low layer initialization (GPIO, CLK ...) และ interrupts callbacks ด้วย.

ในกรณีของ RTOS implementation, ไฟล์เพิ่มเติมจะถูกใช้ (sys_arch.c). ไฟล์นี้จัดให้มีใช้ an emulation layer สำหรับ the RTOS services (message ที่ส่งผ่าน RTOS mailbox, semaphores, เป็นต้น). ไฟล์นี้ควรปรับแต่งตาม the current RTOS, นั่นคือ FreeRTOS สำหรับแพ็คเกจนี้.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 11:19:15 AM
3 LwIP configuration

LwIP จัดให้มี a file ที่ชื่อ lwipopts.h ที่ช่วยให้ผู้ใช้กำหนดค่า the stack และทุก modules ของมันได้อย่างสมบูรณ์. ผู้ใช้ไม่จำเป็นต้องกำหนดทุก the LwIP options: ถ้าตัวเลือกไหนไม่ถูกกำหนด, ค่าโดยเริ่มต้นที่กำหนดใน opt.h file จะถูกใช้. ดังนั้น, lwipopts.h จัดให้มีวิธีแทนที่ the lwIP behavior จำนวนมาก.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 11:34:17 AM
3.1 Modules support

ผู้ใช้สามารถเลือกโมดูลที่ต้องการสำหรับแอปพลิเคชันของตน เพื่อให้ขนาดของโค้ดถูกปรับให้ดีขึ้นโดยการคอมไพล์เฉพาะคุณลักษณะที่เลือกไว้.

ดังตัวอย่าง, เพื่อปิดใช้งาน UDP และเปิดใช้งาน DHCP, โค้ดต่อไปนี้ต้องถูกจัดให้มีใช้ใน lwipopts.h file:

/* Disable UDP */
#define LWIP_UDP 0

/* Enable DHCP */
#define LWIP_DHCP 1
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 11, 2023, 01:39:11 PM
3.2 Memory configuration

LwIP จัดให้มีวิธีการที่ยืดหยุ่นในการจัดการ memory pool sizes และการจัดระเบียบ.

มันจะจอง a fixed-size static memory area เป็น the data segment. มันถูกแบ่งลงใน the various pools ที่ lwIP ใช้สำหรับ the various data structures. ดังตัวอย่าง, มี a pool สำหรับ struct tcp_pcb, และ อีก pool สำหรับ struct udp_pcb. แต่ละ pool สามารถถูกกำหนดค่าเพื่อถือ a fixed number of data structures. จำนวนนี้สามารถถูกเปลี่ยนใน the lwipopts.h file. ตัวอย่างเช่น, MEMP_NUM_TCP_PCB and MEMP_NUM_UDP_PCB กำหนด the maximum number of tcp_pcb and udb_pcb structures ที่สามารถแอ็คทีฟในระบบในเวลาที่กำหนด.

The user options สามารถถูกเปลี่ยนใน lwipopts.h. Table 7 จัดให้มีสรุปรวมของ the main RAM memory options.

(https://i.imgur.com/RiTYoEY.png)
(https://i.imgur.com/GACeVws.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 12, 2023, 05:48:13 AM
4 Developing applications with LwIP stack

4.1 Developing in standalone mode using the Raw API

4.1.1 Operation model

ใน standalone mode, the operation model จะตั้งอยู่บน continuous software polling เพื่อเช็คว่า a packet ถูกรับแล้วหรือไม่.

เมื่อ a packet ถูกรับแล้ว, ก่อนอื่นมันจะถูกก๊อปปี้จาก the Ethernet driver buffers ลงใน the LwIP buffers. เพื่อก๊อปปี้ the packet ให้เร็วที่สุดเท่าที่เป็นได้, the LwIP buffers (pbufs) ควรถูกจัดสรรจาก the pool of buffers (PBUF_POOL).

เมื่อ a packet ถูกก๊อปปี้แล้ว, มันจะถูกส่งไปยัง the LwIP stack เพื่อประมวลผล. ขึ้นอยู่กับ the received packet, the stack อาจแจ้งหรือไม่แจ้ง the application layer.

LwIP สื่อสารกับ the application layer โดยใช้ event callback functions. ฟังก์ชันเหล่านี้ควรถูกกำหนดก่อนการสตาร์ท the communication process.

ดูที่ Figure 4 สำหรับการอธิบายของ the standalone operation model flowchart

(https://i.imgur.com/8cRbVwK.png)

สำหรับ TCP applications, the following common callback functions ต้องถูกกำหนด:
•   Callback for incoming TCP connection event, ที่กำหนดโดย TCP_accept API call
•   Callback for incoming TCP data packet event, ที่กำหนดโดย TCP_recev API call
•   Callback for signaling successful data transmission, ที่กำหนดโดย TCP_sent API call
•   Callback for signaling TCP error (after a TCP abort event), ที่กำหนดโดย TCP_err API call
•   Periodic callback (ทุกๆ 1 หรือ 2 วินาที) for polling the application, ที่กำหนดโดย TCP_poll API call
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 12, 2023, 06:52:07 AM
4.1.2 Example of TCP echo server demonstration

The TCP echo server example ที่จัดให้มีใน the \LwIP\LwIP_TCP_Echo_Server folder เป็น a simple application ที่จัดให้มีใช้ a TCP server ซึ่งจะสะท้อน received TCP data packet ใดๆที่มาจาก a remote client.

ตัวอย่างต่อไปนี้ให้คำอธิบายของ the firmware structure. นี่คือคัดลอกจาก the main.c file.

(https://i.imgur.com/I3BCNKp.png)

ฟังชั่นต่อไปนี้ถูกเรียก:
1.  HAL_Init function ถูกเรียกเพื่อรีเซ็ตทุกl peripherals และเพื่อเริ่มต้น the Flash interface และ the Systick timer
2.  lwIP_init function ถูกเรียกเพื่อเริ่มต้น the LwIP stack internal structures และสตาร์ท stack operations.
3.  Netif_config function ถูกเรียกเพื่อกำหนดค่า the network interface (netif).
4.  tcp_echoserver_init function ถูกเรียกเพื่อเริ่มต้น the TCP echo server application.
5.  ethernetif_input function ใน the infinite while loop วนตรวจสำหรับการรับแพ็คเก็ต. เมื่อ a packet ถูกรับ, มันถูกส่งผ่านเพื่อถูก
     จัดการโดย the stack
6.  sys_check_timeouts LwIP function ถูกเรียกเพื่อจัดการ certain LwIP internal periodic tasks (protocol timers,
     retransmission of TCP packets...).
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 12, 2023, 08:27:01 AM
tcp_echoserver_init function description
The tcp_echoserver_init function code เป็นดังต่อไปนี้:

(https://i.imgur.com/eDeDLWI.png)

LwIP API เรียก tcp_new เพื่อจัดสรร a new TCP protocol control block (PCB) (tcp_echoserver_pcb).

The allocated TCP PCB ถูกผูกไว้กับ a local IP address และ port โดยใช้ tcp_bind function.

หลังจากการผูก the TCP PCB, tcp_listen function ถูกเรียกเพื่อสตาร์ท the TCP listening process บน the TCP PCB.

สุดท้าย a tcp_echoserver_accept callback function ควรถูกกำหนดเพื่อจัดการ incoming TCP connections บน the TCP PCB. นี้ถูกทำโดยการใช้ tcp_accept LwIP API function.

เริ่มจากจุดนี้, the TCP server ก็พร้อมที่จะยอมรับ incoming connection ใดๆจาก remote clients.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 12, 2023, 10:37:03 AM
tcp_echoserver_accept function description

ตัวอย่างต่อไปนี้แสดงวิธีที่ incoming TCP connections ถูกจัดการโดย tcp_echoserver_accept user callback function. นี้คือการคัดลอกจากฟังก์ชันนี้.

(https://i.imgur.com/Bd5GNwI.png)

ฟังก์ชันต่อไปนี้ถูกเรียก:
1. The new TCP connection ถูกส่งผ่านไปยัง tcp_echoserver_accept callback function ผ่านทาง newpcb parameter.
2.  An es structure ถูกใช้ในการเก็บ the application status. มันถูกส่งผ่านเป็น an argument ไปยัง the TCP PCB "newpcb"
     connection โดยการเรียก tcp_arg LwIP API.
3.  A TCP receive callback function, tcp_echoserver_recv, ถูกกำหนดโดยการเรียก LwIP API tcp_recv. callback นี้จัดการทุก
     the data ที่รับส่งด้วย the remote client.
4.  A TCP error callback function, tcp_echoserver_error, ถูกกำหนดโดยการเรียก LwIP API tcp_err. callback นี้จัดการ TCP
     errors.
5.  A TCP poll callback function, tcp_echoserver_poll, ถูกกำหนดโดยการเรียก LwIP API tcp_poll เะอจัดการ periodic
     application tasks (อย่างเช่นการเช็คว่า the application data ยังคงถูกส่งหรือไม่).
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 14, 2023, 08:19:16 AM
4.2 Developing with an RTOS using Netconn or Socket API

4.2.1 Operation model

The operation model เมื่อทำงานกับ RTOS มีลักษณะดังต่อไปนี้:

The TCP/IP stack และ the application รันใน threads ที่แยกกัน.

The application สื่อสารกับ the stack ผ่านทาง sequential API calls ที่ใช้ the RTOS mailbox mechanism สำหรับ inter-process communications. The API calls กำลังบล็อกการเรียก ซึ่งหมายความว่า the application thread จะถูกบล็อกจนกว่าการตอบสนองจะถูกรับจาก the stack.

thread เพิ่มเติม, the network interface thread, ถูกใช้เพื่อให้ได้รับ received packets ใดๆจาก driver buffers และจัดเตรียมพวกมันให้กับ the TCP/IP stack โดยใช้ the RTOS mailbox. thread จะถูกแจ้งข่าวของ a packet reception โดยใช้ the Ethernet receive interrupt service routine.

ดูที่ Figure 5 สำหรับคำอธิบายของ the lwIP operation model flowchart ที่มี RTOS.

(https://i.imgur.com/pRrTsIp.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 18, 2023, 02:22:29 PM
4.2.2 Example of a TCP echoserver demonstration using the Netconn API

จากมุมมองของแอปพลิเคชัน Netconn API นำเสนอวิธีที่ง่ายกว่า the raw API สำหรับการพัฒนาแอปพลิเคชัน TCP/IP นี่เป็นเพราะมี Sequential API ที่ใช้งานง่ายกว่า.

ตัวอย่างต่อไปนี้แสดง a TCP echoserver application ที่พัฒนาด้วย the Netconn API. นี้คือส่วนที่คัดลอกจาก the main.c file.

(https://i.imgur.com/RhnEWgg.png)

ฟังชั่นต่อไปนี้ถูกเรียก:
1. tcpip_init function ถูกเรียกเพื่อเริ่มต้น the LwIP stack modules และเพื่อสตาร์ท the TCP/IP stack thread.
2. Netif_config function ถูกเรียกเพื่อกำหนดค่า the network interface (netif).
3. The TCP echo server thread ถูกสร้างใน tcpecho_init function.

(https://i.imgur.com/dG1glYk.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 18, 2023, 02:45:09 PM
tcpecho_thread function description

The TCP echo server thread มีโค้ดต่อไปนี้:

(https://i.imgur.com/ltYjK3D.png)
(https://i.imgur.com/hErYN8n.png)
(https://i.imgur.com/Jd7PDjd.png)

ลำดับต่อไปนี้ถูกปฏิบัติ:
1.  Netconn_new API function ถูกเรียกด้วย NETCONN_TCP parameter จะสร้าง a new TCP connection.
2.  The newly created connection จากนั้นถูกผูกกับ port 7 (echo protocol) โดยการเรียก Netconn_bind API function.
3.  หลังจากการผูก the connection, the application สตาร์ทการมอนิเตอร์ the connection โดยการเรียก Netconn_listen API
     function.
4.  ใน the infinite while(1) loop, the application จะรอสำหรับ a new connection โดยการเรียก the API function
     Netconn_accept. API call นี้จะบล็อก the application task เมื่อไม่มี incoming connection.
5.  เมื่อมี an incoming connection, the application สามารถสตาร์ทการรับ data โดยการเรียก netconn_recv API function.
     Incoming data จะถูกรับใน a netbuf.
6.  The application สามารถได้รับ the received data โดยการเรียก netbuf_data netbuf API function.
7.  The received data จะถูกส่งกลับ (echoed) ไปยัง the remote TCP client โดยการเรียก Netconn_write API function.
8.  Netconn_close และ Netconn_delete ถูกใช้เพื่อปิดและลบ the Netconn connection, ตามลำดับ.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 07:49:56 AM
5 LwIP package description

5.1 LwIP package directories

The package บรรจุชุดของ applications ที่รันบนส่วนบนของ the LwIP stack และ STM32Cube HAL และ BSP drivers. The firmware ประกอบด้วยโมดูลต่อไปนี้:
•   Drivers: บรรจุ the low level drivers ของ STM32F4xx microcontroller
     –   CMSIS
     –   BSP drivers
     –   HAL drivers
•   Middlewares: บรรจุ libraries and protocol components
     –   LwIP TCP/IP stack
     –   FatFS
     –   FreeRTOS
•   Projects: บรรจุ the source file และ configurations ของ the following applications:
     –   Applications ที่รันใน standalone mode (โดยไม่มี an RTOS) ตั้งอยู่บน Raw API:
         A Web server
         A TFTP server
         A TCP echo client application
         A TCP echo server application
         A UDP echo client application
         A UDP echo server application
     –   Applications ที่รันด้วย the FreeRTOS operating system:
         A Web server ตั้งอยู่บน netconn API
         A Web server ตั้งอยู่บน socket API
         A TCP/UDP echo server application ตั้งอยู่บน netconn API.

Applications อยู่ภายใต้ Projects repository ตามเส้นทางนี้: Projects\STM324xx_EVAL\Applications\LwIP\, โดยที่ STM324xx_EVAL อ้างอิงถึง STM32F4xx evaluation board อย่างเช่น STM324xG_EVAL สำหรับ STM32F407xx/417xx devices.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 08:44:34 AM
5.2 Applications settings

5.2.1 PHY interface configuration

The Ethernet peripheral ถูกอินเตอร์เฟสกับ an external PHY เพื่อจัดให้มี physical layer communications. The PHY registers definition และ define statements ถูกวางตำแหน่งอยู่ใน the HAL configuration file stm32f4xx_hal_conf.h.

The PHY สามารถทำงานใน MII หรือ RMII mode. เพื่อเลือกโหมดที่ต้องการ, เติม the MediaInterface parameter ใน Init structure เมื่อเริ่มต้น the Ethernet peripheral.

Note:  โปรดดูที่ the readme file ที่จัดให้มีภายใน your device Ethernet examples เพื่อทราบเกี่ยวกับ the available PHY
            interface modes ให้มากขึ้นเกี่ยวกับบอร์ดที่สนับสนุน.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 08:56:23 AM
5.2.2 MAC and IP address settings

The default MAC address ถูกเซ็ตเป็น 00:00:00:00:00:02. เพื่อเปลี่ยน address นี้, แก้ไข the six bytes ที่กำหนดใน the stm32f4xx_hal_conf.h file.

The default IP address ถูกเซ็ตเป็น: 192.168.0.10. เพื่อเปลี่ยน address นี้, แก้ไข the four bytes ที่กำหนดใน the main.h file.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 09:36:15 AM
5.2.3 Firmware features

แพ็คเกจนี้ประกอบด้วยโมดูลเพื่อปรับปรุงและขยายการใช้งานแอพพลิเคชั่นบางตัว.

The DHCP protocol ถูกรองรับเพื่อว่า the STM32 MCU สามารถทำงานเป็น a DHCP client เพื่อให้ได้รับ a dynamic IP address เมื่อมันถูกต่อกับ a DHCP server. เพื่อเปิดใช้งาน DHCP protocol, ไม่คอมเมนต์ออก the following macro:

#define USE_DHCP" จาก main.h file.

Note: ถ้า the IP address ถูกกำหนดค่าโดย DHCP และ the application ไม่พบ a DHCP server บน the network ซึ่งมันถูกต่อแล้ว,
          the IP address ดังนั้นจะถูกเซ็ตโดยอัตโนมัติเป็น the static address (192.168.0.10).


ผู้ใช้สามารถเปิดใช้งาน the LCD controller โดยการกำหนด the #define USE_LCD macro ใน main.h. ถ้ามันถูกเปิดใช้งาน, text messages จะถูกแสดงเพื่อแจ้งผู้ใช้ให้ทราบถึงสถานะของ the application (assigned IP address, network link status ...)

Note: Getting started applications ไม่รองรับ DHCP and LCD modules. โปรดดูที่ Section 6: Using the LwIP applications
          สำหับข้อมูลที่มากขึ้น.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 09:50:13 AM
5.3 Evaluation boards settings

ก่อนการรัน an Ethernet example, อ่าน the corresponding readme file เพื่อทราบวิธีกำหนดค่า the board jumper เพื่อให้แน่ใจว่าการทำงานถูกต้อง.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 10:38:41 AM
6 Using the LwIP applications

The STM32Cube LwIP package มาพร้อมกับแอพพลิเคชั่นหลายตัวที่ใช้ the different LwIP stack API sets.

The applications ถูกแบ่งออกเป็นสามประเภทดังแสดงใน Table 8.

(https://i.imgur.com/DkC9eZv.png)

Getting started applications ใช้การกำหนดค่าขั้นต่ำเพื่อรัน applications บนด้านบนของ the LwIP stack. LEDs ถูกใช้เพื่อแจ้งให้ผู้ใช้ทราบถึง application status.

Features applications จัดให้มีความยืดหยุ่นและตัวเลือกที่มากขึ้น. พวกมันรองรับ network protocols อย่าง HTTP, DHCP และใช้ LCD messages เพื่อแสดง application status.

Integrated application รองรับ FatFS middleware component และ TFTP protocol เพื่อส่งถ่าย files ถึงและจาก microSD™ card ที่อยู่บน the evaluation board.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 11:08:07 AM
6.1 Getting started applications

6.1.1 TCP echo client

application นี้ถูกใช้เพื่อทดสอบ a basic TCP connection. The STM32 MCU ทำงานเป็น a TCP client ที่ต่อกับ the TCP server. The client ส่ง a string และ the server สะท้อนกลับ the same string ไปยัง the client.

เพื่อทดสอบ the TCP echo client application, ทำตามขั้นตอนเหล่านี้:
1.  ทำให้แน่ใจว่า the STM324xx-EVAL jumper settings อย่างถูกต้อง.
2.  Build และโปรแกรม the demonstration code ลงใน the STM32F4xx Flash memory. LEDs แสดงให้เห็น the LwIP
     initialization สำเร็จหรือล้มเหลว (the dynamic address allocation "DHCP" ไม่ถูกรองรับสำหรับ application นี้).
3.  บน the remote PC, เปิด a command prompt window. ภายใต้ Windows, เลือก Start > All Programs >
     Accessories > Command Prompt.

4.  ที่ the command prompt, ให้ป้อน:
     C:\>echotool /p tcp /s
     โดยที่:
          –   /p tcp คือ the TCP protocol (TCP protocol)
          –   /s คือ the actual mode ของ connection (Server mode)
5.  เมื่อ the Key button ถูกกดบน the STM324xx-EVAL board, the client ส่ง a string และ the server สะท้อนกลับ the same
     string ไปยัง the client.

Note: ตรวจสอบให้แน่ใจว่า the remote PC IP address เหมือนกับ the address ที่กำหนดไว้ใน the main.h file (192.168.0.11
          โดยค่าเริ่มต้น).


Figure 6 แสดงตัวอย่างของ command string นี้และของ the module response.

(https://i.imgur.com/uUGhznu.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 11:30:23 AM
6.1.2 TCP Echo server

application นี้ถูกใช้เพื่อทดสอบ a basic TCP connection. The STM32 MCU ทำงานเป็น a TCP server ที่รอสำหรับr client requests. มันสะท้อนกลับสิ่งที่ถูกส่งมา.

เพื่อทดสอบ the TCP echo server demo, ทำตามขั้นตอนเหล่านี้:
1.  ทำให้แน่ใจว่า the STM324xx-EVAL jumper settings อย่างถูกต้อง.
2.  Build และโปรแกรม the demonstration code ลงใน the STM32F4xx Flash memory. LEDs แสดงให้เห็น the LwIP
     initialization สำเร็จหรือล้มเหลว (the dynamic address allocation "DHCP" ไม่ถูกรองรับสำหรับ application นี้).
3.  บน the remote PC, เปิด a command prompt window. ภายใต้ Windows, เลือก Start > All Programs > Accessories >
     Command Prompt.

4.  ที่ the command prompt, ให้ป้อน:
     C:\>echotool IP_address /p tcp /r 7 /n 15 /t 2 /d Testing LwIP TCP echo server
     โดยที่:
     –   IP_address คือ the actual board IP address. โดยค่าเริ่มต้น, the following static IP address จะถูกใช้:
          192.168.0.10
     –   /p tcp คือ the protocol (TCP protocol)
     –   /r คือ the actual remote port บน the echo server (echo port)
     –   /n คือจำนวนของ echo requests (สำหรับตัวอย่างนี้, 15)
     –   /t คือ the connection timeout เป็น seconds (สำหรับตัวอย่างนี้, 2)
     –   /d คือ the message ที่จะถูกส่งสำหรับการสะท้อน (สำหรับตัวอย่างนี้, "Testing LwIP TCP echo server")

Figure 7 แสดงตัวอย่างของ command string นี้และของ the module response.

(https://i.imgur.com/hMMcgOD.png)

Note: Statistics จัดให้มีจำนวนของ received and corrupted packets เป็นจุดสิ้นสุดของการทดสอบ.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 01:28:12 PM
6.1.3 UDP echo client

application นี้ถูกใช้เพื่อทดสอบ basic UDP echo connections. The STM32 MCU ทำงานเป็น a UDP client ที่ต่อกับ a UDP server.

เพื่อทดสอบ the UDP echo client demonstration, ทำตามขั้นตอนข้างล่าง:
1.  ทำให้แน่ใจว่า the STM324xx-EVAL jumper settings อย่างถูกต้อง.
2.  Build และโปรแกรม the demonstration code ลงใน the STM32F4xx Flash memory. LEDs แสดงให้เห็น the LwIP
     initialization สำเร็จหรือล้มเหลว (the dynamic address allocation "DHCP" ไม่ถูกรองรับสำหรับ application นี้).
3.  บน the remote PC, เปิด a command prompt window. ภายใต้ Windows, เลือก Start > All Programs > Accessories >
     Command Prompt.

4.  ที่ the command prompt, ให้ป้อน:
     C:\>echotool /p udp /s
     โดยที่:
      –   /p udp คือ the protocol (UDP protocol)
      –   /s คือ the actual mode ของ connection (Server mode)
5.  เมื่อ the Key button ถูกกดบน the STM324xx-EVAL board, the client ส่ง a string และ the server สะท้อนกลับ the same
     string ไปยัง the client.

Note: ตรวจสอบให้แน่ใจว่า the remote PC IP address เหมือนกับ the address ที่กำหนดไว้ใน the main.h file (192.168.0.11
          โดยค่าเริ่มต้น).


Figure 8 แสดงตัวอย่างของ command string นี้และของ the module response.

(https://i.imgur.com/yf7udQj.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 01:44:58 PM
6.1.4 UDP echo server

application นี้ถูกใช้เพื่อทดสอบ a basic UDP connection. The STM32 MCU ทำงานเป็น a UDP server ที่รอสำหรับ client requests.

เพื่อทดสอบ the UDP echo server application,, ทำตามขั้นตอนเหล่านี้:
1.  ทำให้แน่ใจว่า the STM324xx-EVAL jumper settings อย่างถูกต้อง.
2.  Build และโปรแกรม the demonstration code ลงใน the STM32F4xx Flash memory. LEDs แสดงให้เห็น the LwIP
     initialization สำเร็จหรือล้มเหลว (the dynamic address allocation "DHCP" ไม่ถูกรองรับสำหรับ application นี้).
3.  บน the remote PC, เปิด a command prompt window. ภายใต้ Windows, เลือก Start > All Programs > Accessories >
     Command Prompt.

4.  ที่ the command prompt, ให้ป้อน:
     C:\>echotool IP_address /p udp /r 7 l/ 7 /n 15 /t 2 /d Testing LwIP UDP echo server
     โดยที่:
     –   IP_address คือ the actual board IP address. โดยค่าเริ่มต้น, the following static IP address จะถูกใช้:
          192.168.0.10
     –   /p คือ the protocol (UDP protocol)
     –   /r คือ the actual remote port บน the echo server (echo port)
     –   /l คือ the actual local port สำหรับ the client (echo port)
     –   /n คือจำนวนของ echo requests (สำหรับตัวอย่างนี้, 15)
     –   /t คือ the connection timeout เป็น seconds (สำหรับตัวอย่างนี้, 2)
     –   /d คือ the message ที่จะถูกส่งสำหรับการสะท้อน (สำหรับตัวอย่างนี้, "Testing LwIP UDP echo server")

Figure 9 แสดงตัวอย่างของ command string นี้และของ the module response.

(https://i.imgur.com/i9pkFUn.png)

Note: Statistics จัดให้มีจำนวนของ received and corrupted packets ถูกให้ที่จุดสิ้นสุดของการทดสอบ.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 02:06:20 PM
6.1.5 UDP TCP echo server based on netconn AP

การสาธิตนี้จัดให้มี the echo service application ทั้งสำหรับ TCP and UDP protocols:
•   เพื่อทดสอบ the UDP TCP echo server netconn demonstration ใน TCP server mode, โปรดดูที่
     Section 6.1.2: TCP Echo server.
•   เพื่อทดสอบ the UDP TCP echo server netconn demonstration ใน UDP server mode, โปรดดูที่
     Section 6.1.4: UDP echo server.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 20, 2023, 06:54:55 PM
6.2 Features applications

6.2.1 Web server based on raw API

application นี้จัดให้มีใช้ a web server ที่ตั้งอยู่บน the LwIP raw API. มันถูกใช้เพื่อต่อกับ the STM32 MCU จาก a web client และเพื่อโหลด HTML pages.

The web server application จัดให้มีใช้คุณลักษณะต่อไปนี้:
•   URL parsing
•   CGI (Common Gateway Interface)
•   SSI (Server Side Includes)
•   Dynamic Header generation
•   HTTP Post request

เพื่อทดสอบ the web server application, ทำตามขั้นตอนเหล่านี้:
1.  ทำให้แน่ใจว่า the STM324xx-EVAL jumper settings อย่างถูกต้อง.
2.  ใน the main.h file, ไม่คอมเมนต์ออกตัวเลือก "USE_DHCP" หรือ "USE_LCD" เพื่อเปิดใช้งาน the DHCP client หรือ LCD
     screen features.
3.  Build และโปรแกรม the demonstration code ลงใน the STM32F4xx Flash memory.
4.  ถ้า "USE_ DHCP" and "USE_LCD" ถูกกำหนด, a message จะถูกแสดงบน the LCD screen เพื่อแสดงให้เห็นความสำเร็จหรือล้ม
     เหลวของ the DHCP IP address allocation, ถ้าเป็นอย่างอื่น the LEDs จะแสดงผลของการทำงานนี.
5.  หลังจาก an IP address ถูกกำหนดแล้ว (อย่างใดอย่างหนึ่ง a static หรือ a dynamic address), ผู้ใช้สามารถสตาร์ท the
     application.
6.  บน the remote PC, เปิด a web client (Mozilla Firefox หรือ Internet Explorer) และพิมพ์ the board IP address ใน a
     web browser. โดยค่าเริ่มต้น, the following static IP address จะถูกใช้: 192.168.0.10.

(https://i.imgur.com/HVTsSCv.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 07:37:53 AM
Server Side Includes (SSI)

The SSI เป็นวิธีการที่ใช้ในการรวม dynamic data ใน HTML code แบบไดนามิก.

นี้ถูกทำได้โดยการวาง a specific tag ภายใน the HTML code ของ the web page. The tag ควรมีรูปแบบดังต่อไปนี้:

<!--#tag-->

สำหรับ the ADC conversion page, the following tag ถูกใช้ภายใน the HTML code:

<!--#t-->

เมื่อมีการร้องขอสำหรับ the ADC webpage (ซึ่งมีนามสกุล  ".shtml"), the server จะแยกวิเคราะห์ the webpage และเมื่อ the tag ถูกพบ, มันจะถูกแทนที่โดย the ADC conversion value.

(https://i.imgur.com/abf0Ex5.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 08:08:25 AM
Common Gateway Interface (CGI)

The CGI เป็น a standard web technique ที่ใช้ในการดำเนินการตามคำร้องขอที่มาจากไคลเอ็นต์ทางฝั่งเซิร์ฟเวอร์ และจากนั้นจึงตอบกลับไปยังไคลเอ็นต์.

ใน LwIP, the CGI จะเสนอการทำงานเฉพาะด้วย GET method requests และสามารถจัดการได้มากถึง 16 parameters ที่เข้ารหัสใน the URI. The CGI handler function ที่ดำเนินการบนฝั่งเซิร์ฟเวอร์จะส่งคืนกลับ a HTML file ที่ the HTTP server ส่งไปยัง the client.

ใน the HTTP server demonstration, วิธีนี้ถูกใช้เพื่อควบคุม LED สี่ดวง (LED1, LED2, LED3 and LED4) ของ the evaluation board.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 09:32:03 AM
6.2.2 Web server based on netconn API

application implement ของ a web server ที่ตั้งอยู่บน the netconn API นี้. มันถูกใช้เพื่อต่อกับ the STM32 MCU จาก a web client และเพื่อโหลด HTML pages.

web server นี้บรรจุสอง HTML pages. เพ็จแรกให้ข้อมูลทั่วไปเกี่ยวกับ STM32F4xx microcontrollers และ the LwIP stack. เพ็จที่สองแสดงรายการ the running tasks และสถานะของพวกมัน. เพ็จนี้ถูกอัปเดตโดยอัตโนมัติทุกๆหนึ่งวินาที (ดูรูปที่ 12).

เพื่อทดสอบ the HTTP server netconn demo, ทำตามขั้นตอนเหล่านี้:
1.  ทำให้แน่ใจว่า the STM324xx-EVAL jumper settings อย่างถูกต้อง.
2.  ใน the main.h file, ไม่คอมเมนต์ออกตัวเลือก "USE_DHCP" หรือ "USE_LCD" เพื่อเปิดใช้งาน the DHCP client หรือ LCD
     screen features.
3.  Build และโปรแกรม the demonstration code ลงใน the STM32F4xx Flash memory.
4.  ถ้า "USE_ DHCP" and "USE_LCD" ถูกกำหนด, a message จะถูกแสดงบน the LCD screen เพื่อแสดงให้เห็นความสำเร็จหรือล้ม
     เหลวของ the DHCP IP address allocation, ถ้าเป็นอย่างอื่น the LEDs จะแสดงผลของการทำงานนี.
5.  หลังจาก an IP address ถูกกำหนดแล้ว (อย่างใดอย่างหนึ่ง a static หรือ a dynamic address), ผู้ใช้สามารถสตาร์ท the
     application.
6.  บน the remote PC, เปิด a web client (Mozilla Firefox หรือ Internet Explorer) และพิมพ์ the board IP address ใน a
     web browser. โดยค่าเริ่มต้น, the following static IP address จะถูกใช้: 192.168.0.10.

(https://i.imgur.com/4TBWhmZ.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 09:35:02 AM
6.2.3 Web server based on socket API

application implement ของ a web server ที่ตั้งอยู่บน the socket API นี้. เพื่อทดสอบการสาธิตนี้, โปรดดูที่ Section 6.2.2: Web server based on netconn API.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 09:57:09 AM
6.3 Integrated applications

6.3.1 TFTP server

The TFTP server คือ a file transfer application ที่ต้องการ a remote TFTP client. The files จะถูกส่งถ่ายไปยังและจาก the microSD card ที่อยู่บน the STM324xx-EVAL board.

The TFTP server รอสำหรับ a request จาก a remote TFTP client. The STM324xx-EVAL evaluation board ต้องถูกต่อผ่าน a remote PC เพื่อดาวน์โหลดหรืออัฟโหลด a file. ในการดำเนินการนี้, a TFTP client ต้องถูกติดตั้งบน the remote PC. นี้สามารถถูกทำได้โดยการใช้ the tftpd32 tool ซึ่งสามารถพบได้ที่ http://tftpd32.jounin.net.

เพื่อทดสอบ the TFTP server application, ทำตามขั้นตอนเหล่านี้:
1.  ทำให้แน่ใจว่า the STM324xx-EVAL jumper settings อย่างถูกต้อง.
2.  ใน the main.h file, ไม่คอมเมนต์ออกตัวเลือก "USE_DHCP" หรือ "USE_LCD" เพื่อเปิดใช้งาน the DHCP client หรือ LCD
     screen features.
3.  Build และโปรแกรม the demonstration code ลงใน the STM32F4xx Flash memory.
4.  ถ้า "USE_ DHCP" and "USE_LCD" ถูกกำหนด, a message จะถูกแสดงบน the LCD screen เพื่อแสดงให้เห็นความสำเร็จหรือล้ม
     เหลวของ the DHCP IP address allocation, ถ้าเป็นอย่างอื่น the LEDs จะแสดงผลของการทำงานนี.
5.  หลังจาก an IP address ถูกกำหนดแล้ว (อย่างใดอย่างหนึ่ง a static หรือ a dynamic address), ผู้ใช้สามารถสตาร์ท the
     application.
6.  บน the remote PC, เปิด the TFTP client (ตัวอย่างเช่น, TFTPD32), และกำหนดค่า the TFTP server address (host
     address ใน TFTPD32).
7.  สตาร์ทการส่งถ่าย files ไปยังและจาก the micro SD card ที่อยู่บน the STM324xx-EVAL board.

Figure 10 ให้ภาพรวมของ the tftpd32 tool.

(https://i.imgur.com/bwPqGUt.png)

Note: ทำให้แน่ใจว่า the microSD card ถูกใส่ลงใน the dedicated connector ก่อนการดาวน์โหลด/อัฟโหลด a file จาก/ไปยัง the
          STM324xx-EVAL board.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 10:14:39 AM
7 Conclusion

LwIP package อนุญาตให้ใช้ lwIP TCP/IP stack กับ the STM32Cube HAL API. open source stack นี้เสนอการบริการของ a full-scale TCP/IP stack ในขณะที่คงไว้ RAM/ROM usage ค่อนข้างต่ำ.

สองการเข้าใกล้ถูกอธิบายสำหรับการพัฒนา TCP/IP applications, อย่างใดอย่างหนึ่งใน a Standalone mode, หรือการใช้ a real-time operating system (RTOS) สำหรับ multi-threaded operations.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 10:47:37 AM
Appendix A FAQ

A.1  ผมจะเลือกระหว่าง static หรือ dynamic (DHCP) IP address allocation ได้อย่างไร?

เมื่อ the macro #define USE_DHCP ที่อยู่ใน main.h ถูกคอมเมนต์ออก, a static IP address จะถูกกำหนดให้กับ the STM32 microcontroller (โดยค่าเริ่มต้น 192.168.0.10, ค่านี้สามารถถูกแก้ไขจาก "main.h" file).

ถ้า the macro #define USE_DHCP ไม่ถูกคอมเมนต์ออก, the DHCP protocol จะถูกเปิดใช้งาน, และ the STM32 จะทำงานเป็น a DHCP client

A.2  แอปพลิเคชันจะทำงานอย่างไรเมื่อสายอีเธอร์เน็ตถูกตัดการเชื่อมต่อ?

เมื่อ the cable ถูกตัดการเชื่อมต่อ the Ethernet peripheral จะหยุดการจราจรทั้งการส่งและการรับและ the network interface จะถูก shut down. ถ้า an LCD controller ถูกใช้, a message จะถูกแสดงผลเพื่อแจ้งให้ผู้ใช้ทราบว่า the cable ไม่ถูกต่อ, มิฉะนั้น the red LED of the evaluation board จะถูก switched on.

เมื่อผู้ใช้ต่อ the cable ใหม่, the Ethernet traffic จะกลับมาทำงานต่อและ the network interface จะถูกตั้งค่า. ถ้า an LCD controller ถูกใช้, a message จะถูกแสดงผลเพื่อแจ้งให้ผู้ใช้ทราบถึง the new IP address อย่างใดอย่างหนึ่งด้วย static หรือ dynamic allocation, มิฉะนั้น the yellow LED of the evaluation board จะถูก switched on.

A.3  แอปพลิเคชันจะสามารถถูกย้ายไปยังฮาร์ดแวร์อื่นได้อย่างไร?

เมื่อแพลตฟอร์มฮาร์ดแวร์อื่นถูกใช้, ตรวจสอบ the GPIO configuration ใน the HAL_ETH_MspInit() function สำหรับ the Ethernet peripheral และใน HAL_PPP_MspInit() หรือ HAL_MspInit() หากแอปพลิเคชันต้องการ PPP peripheral เพิ่มเติม.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 10:48:55 AM
(https://i.imgur.com/hRO2Qo6.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 11:11:08 AM
มีแตกต่างกันนิดหน่อย มีบทที่ 2 เพิ่มเติมมา
https://www.st.com/resource/en/application_note/an3966-lwip-tcpip-stack-demonstration-for-stm32f4x7-microcontrollers-stmicroelectronics.pdf

2 STM32F4x7 low level driver overview

The STM32F4x7 Ethernet low level driver อยู่ใน the \Libraries\STM32F4x7_ETH_Driver\ folder.

ชุดของ functions ที่จัดให้มีใน the driver สามารถถูกแบ่งลงใน the following categories:
•   Global Ethernet MAC/DMA configuration/control functions
•   DMA descriptors handling functions
•   DMA configuration/control functions
•   PHY control functions
•   Power Management (PMT) functions
•   MAC Management Counters (MMC) functions
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 11:17:44 AM
2.1 Global Ethernet MAC/DMA functions

Table 16 จัดให้มีสรุปรวมของ the Global Ethernet MAC/DMA functions ที่ใช้สำหรับการกำหนดค่าของ the media access control (MAC) และ direct memory access (DMA) features.

(https://i.imgur.com/uq6UGc9.png)
(https://i.imgur.com/OYNuYvC.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 11:31:05 AM
2.1.1 Ethernet MAC/DMA configuration parameters

The configuration structure สำหรับ an Ethernet MAC/DMA คือ ETH_InitTypeDef. structure นี้ประกอบด้วย the following MAC and DMA configuration parameters.

(https://i.imgur.com/xhtGJWk.png)
(https://i.imgur.com/IynsFEk.png)
(https://i.imgur.com/4p7rrVT.png)
(https://i.imgur.com/CQ2AytS.png)


(https://i.imgur.com/49VJ41c.png)
(https://i.imgur.com/9mY1uQW.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 01:28:48 PM
2.2 DMA descriptor handling

2.2.1 DMA descriptors

การส่งถ่ายของ Ethernet packets ระหว่าง Transmit/Receive FIFOs และ memory ถูกดำเนินการโดย direct memory access (DMA) โดยใช้ transfer descriptors.

Figure 3 แสดงรูปแบบของ an Ethernet DMA descriptor.

Note:  The following description ไม่นำมาใช้กับ enhanced DMA descriptors.

(https://i.imgur.com/emWeeJA.png)

ดังแสดงในรูปที่ 3, the DMA descriptor สามารถมีสองรูปแบบ:
•   The descriptor ชี้ไปยัง one data buffer เท่านั้นและ the Next Descriptor field ชี้บน next DMA descriptor สำหรับอนุญาต
     ให้ descriptors chaining mechanism
•   The descriptor สามารถชี้ไปยัง two data buffers, Buffer1 and Buffer2
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 01:40:52 PM
ใน the STM32F4x7 Ethernet driver, the selected DMA descriptor format คือ the one allowing descriptor chaining ดังแสดงในรูปที่ 4.

(https://i.imgur.com/ChJJQgV.png)

Note:  1   An Ethernet packet สามารถทอดข้ามครอบคลุมหนึ่งหรือหลาย DMA descriptors.
           2   One DMA descriptor สามารถถูกใช้สำหรับ one Ethernet packet เท่านั้น.
           3   The last descriptor ใน the chain จะชี้ไปยัง the first descriptor เพื่อสร้างวงแหวนของ descriptors.
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 02:06:56 PM
Figure 5 แสดง the buffer and descriptor allocation model ใน memory สำหรับ the STM32F4x7 Ethernet driver.

(https://i.imgur.com/yajbe6R.png)

ดังแสดงใน Figure 5, the following arrays ถูกจัดสรรอย่างคงที่ใน the STM32F4x7 Ethernet driver:
•    Two arrays สำหรับ the DMA descriptors, อาเรย์หนึ่งสำหรับ DMA Rx และอีกอาเรย์หนึ่งสำหรับ DMA Tx:
          ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB],
          DMATxDscrTab[ETH_TXBUFNB];
•    Two arrays ของ driver buffers, array หนึ่งสำหรับ receive buffers และอีก array หนึ่งสำหรับ transmit buffers:
          uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
          uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
     โดยที่:
     –   ETH_RXBUFNB: จำนวนของ driver receive buffers
     –   ETH_TXBUFNB: จำนวนของ driver transmit buffers
     –   ETH_RX_BUF_SIZE: ขนาดเป็นไบต์ของ a receive buffer
     –   ETH_TX_BUF_SIZE: ขนาดเป็นไบต์ของ a transmit buffer
     ค่าโดยเริ่มต้นสำหรับ parameters เหล่านี้ดังที่กำหนดใน file stm32f4x7_eth.h คือ:
     –   ETH_RXBUFNB = 4
     –   ETH_TXBUFNB = 2
     –   ETH_RX_BUF_SIZE = 1524 (max size of Ethernet packet (1522) + 2 bytes for 32-bit alignment)
     –   ETH_TX_BUF_SIZE = 1524 (max size of Ethernet packet (1522) + 2 bytes for 32-bit alignment)

     The above parameter values สามารถถูกเปลี่ยนขึ้นอยู่กับ user specific application needs. นี้สามารถถูกทำโดยการเปิดใช้งาน
     CUSTOM_DRIVER_BUFFERS_CONFIG และเขียนค่าที่กำหนดเองใน the stm32f4x7_eth_conf.h configuration file.

Note: The Ethernet driver อนุญาตให้มี a buffer size (ETH_RX_BUF_SIZE หรือ ETH_TX_BUF_SIZE) ที่ไม่เกิน the
          maximum Ethernet packet size (1522 bytes). Ethernet packets (ที่รับหรือที่จะถูกส่ง) ที่เกิน the configured buffer
          size จะทอดข้ามครอบคลุมไปหลาย buffers (หรือ descriptors).
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 03:08:03 PM
2.2.2 DMA descriptor handling functions

Table 11 จัดให้มีสรุปรวมของ the main driver functions ที่ใช้สำหรับการจัดการ DMA descriptors.

(https://i.imgur.com/9tm1m5j.png)
(https://i.imgur.com/GJUEwXJ.png)

Note: The Ethernet driver จะรักษา two global pointers สำหรับr Rx/Tx DMA descriptor tracking, สำหรับ the next packet
          ที่จะถูกรับหรือที่จะถูกส่ง:

                         __IO ETH_DMADESCTypeDef *DMATxDescToSet;
                         __IO ETH_DMADESCTypeDef *DMARxDescToGet;

(https://i.imgur.com/dyGnk9w.png)
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 03:24:48 PM
2.3 PHY control functions

Table 12 จัดให้มีสรุปรวมของ the functions จัดให้มีใช้สำหรับ PHY control โดย the STM32F4x7 Ethernet driver.

Note: The PHY configuration options (Reset delay, Configuration delay, Status register Speed and Duplex mask
          values) ถูกกำหนดใน the stm32f4x7_eth_conf.h configuration file. ค่าเหล่านี้เปลี่ยนจาก a PHY ไปเป็นค่าอื่น, ดังนั้นผู้ใช้
          จึงต้องอัปเดตค่านี้ขึ้นอยู่กับ the external PHY ที่ใช้.


(https://i.imgur.com/QPpmUK1.png)

The PHY ถูกเข้าถึงในระหว่าง the initialization time (โดย ETH_Init driver function) หลักๆเพื่อ:
•   รีเซ็ต the PHY.
•   เปิดใช้งาน the PHY auto-negotiation mode หรือเลือกโหมดการทำงานแบบด้วยมือ (Fullspeed/Low-speed, Half-
     duplex/Full-duplex).
•   ถ้า the PHY auto-negotiation mode ถูกเลือก, the application จำเป็นต้องวนตรวจ the PHY หรือใช้ a PHY interrupt เพื่อให้
     ได้รับผลลัพธ์ของ auto-negotiation (speed, duplex mode).
Title: Re: UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack
Post by: tha on February 21, 2023, 04:15:21 PM
2.4 Hardware checksum

The STM32F4x7 Ethernet controller มี an embedded hardware checksum accelerator เพื่อลดโหลด the CPU จากการสร้าง การแทรก และการตรวจสอบผลรวมการตรวจสอบความถูกต้องของ the IP, UDP, TCP and ICMP protocols.

ผลรวมการตรวจสอบความถูกต้องสำหรับ TCP, UDP or ICMP จะถูกคำนวณครอบคลุม a complete frame, และจากนั้นใส่ลงใน corresponding header field ของมัน. เนื่องจากข้อกำหนดนี้ ฟังก์ชันนี้จึงถูกเปิดใช้งานเฉพาะเมื่อ the Transmit FIFO ถูกกำหนดค่าสำหรับ Store-and-Forward mode.

Note: โดยค่าเริ่มต้น, the hardware checksum ถูกเปิดใช้งาน. เพื่อปิดใช้งานคุณลักษณะนี้, "คอมเมนต์ออก" the specific
           CHECKSUM_BY_HARDWARE ที่กำหนดใน the LwIPopts.h file ภายใต้ the \inc project folder.