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

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.


tha

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.



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


tha

tcpecho_thread function description

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





ลำดับต่อไปนี้ถูกปฏิบัติ:
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, ตามลำดับ.


tha

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.

tha

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 ให้มากขึ้นเกี่ยวกับบอร์ดที่สนับสนุน.

tha

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.

tha

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
          สำหับข้อมูลที่มากขึ้น.

tha

5.3 Evaluation boards settings

ก่อนการรัน an Ethernet example, อ่าน the corresponding readme file เพื่อทราบวิธีกำหนดค่า the board jumper เพื่อให้แน่ใจว่าการทำงานถูกต้อง.