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

ตัวอย่างต่อไปนี้แสดงวิธีเริ่มต้น the Ethernet peripheral, โดยใช้ HAL API, ลงใน the interface API:




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 สำหรับแพ็คเกจนี้.

tha

3 LwIP configuration

LwIP จัดให้มี a file ที่ชื่อ lwipopts.h ที่ช่วยให้ผู้ใช้กำหนดค่า the stack และทุก modules ของมันได้อย่างสมบูรณ์. ผู้ใช้ไม่จำเป็นต้องกำหนดทุก the LwIP options: ถ้าตัวเลือกไหนไม่ถูกกำหนด, ค่าโดยเริ่มต้นที่กำหนดใน opt.h file จะถูกใช้. ดังนั้น, lwipopts.h จัดให้มีวิธีแทนที่ the lwIP behavior จำนวนมาก.

tha

3.1 Modules support

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

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

/* Disable UDP */
#define LWIP_UDP 0

/* Enable DHCP */
#define LWIP_DHCP 1


tha

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.



tha

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



สำหรับ 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

tha

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.



ฟังชั่นต่อไปนี้ถูกเรียก:
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...).

tha

tcp_echoserver_init function description
The tcp_echoserver_init function code เป็นดังต่อไปนี้:



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.

tha

tcp_echoserver_accept function description

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



ฟังก์ชันต่อไปนี้ถูกเรียก:
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 ยังคงถูกส่งหรือไม่).