UM1713 User manual Developing applications on STM32Cube with LwIP TCP/IP stack

Started by tha, February 09, 2023, 12:44:22 PM

Previous topic - Next topic

tha

1.5 LwIP API overview

ดังที่กล่าวมาข้างบน, สามประเภทของ APIs ถูกนำเสนอโดย LwIP stack:
•   Raw API
•   Netconn API
•   Socket API

tha

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.





tha

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.




tha

1.5.3 Socket API

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

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


tha

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 ได้.



โดยที่
     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.

tha

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 ที่เหมาะสมที่สุดตามข้อมูลที่จะถูกส่ง

tha

1.6.2 pbuf management APIs

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



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,...).

tha

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.