STM32 ETHERNET #8 UDP Client using LWIP NETCONN (RTOS)

Started by tha, February 25, 2023, 07:25:17 AM

Previous topic - Next topic

tha

https://controllerstech.com/udp-client-using-netconn-with-rtos-in-stm32/

ขอแปลหน่อยนะครับ ขอขอบคุณเขามา ณ ที่นี้ด้วย ท่านใดพอจะมีทรัพย์ก็ Donate เขาด้วยนะ ฟังภาษาอังกฤษเขาในยูทูบก็ฟังง่ายดี(เปิดคำบรรยายด้วย)ครับ

                                           UDP Client using LWIP NETCONN (RTOS)

บทช่วยสอนนี้จะครอบคลุม STM32 ในฐานะ a UDP client โดยใช้ the LWIP ที่มี NETCONN and FREE RTOS. เราได้กล่าวถึง the RAW UDP Client เมื่อไม่กี่เดือนก่อน แต่วันนี้เราจะเพิ่ม FREE RTOS และ NETCONN เข้าไปด้วย

NOTE: ฉันใช้ STM32F750 Discovery Board, ซึ่งมี the RMII connection type และ the CubeMX ไม่อนุญาตห้มีการกำหนดค่า
             หน่วยความจำสำหรับ the Ethernet.


เริ่มต้นด้วยการตั้งค่า CubeMX ซึ่งค่อนข้างเหมือนกับ the last tutorial.

tha

                                                                      CubeMX Setup

Let's start with enabling the ethernet itself :



•   ฉันได้เลือก the RMII connection type สำหรับ the ethernet.
•   The PHY Address ถูกเซ็ตเป็น 0, เนื่องจากฉันกำลังใช้ the on board Ethernet module.
•   The Rx Mode ถูกเซ็ตเป็น Interrupt mode.

__________________________________________________________________________________________________________________________________



เนื่องจากฉันได้เลือก the Rx mode เป็น the interrupt mode, the Ethernet global interrupt จึงเปิดโดยอัตโนมัติ.

__________________________________________________________________________________________________________________________________



รูปภาพด้านบนแสดง the Pins ที่ใช้สำหรับ the Ethernet. โปรดทราบว่าทุก the Pins ถูกเซ็ตเป็น "Very High" Speed.

tha

Now let's take a look at the Free RTOS Settings



•   ตามที่แสดงด้านบน ฉันได้เปิดใช้งาน FreeRTOS และฉันใช้ CMSIS_V2 หากคุณไม่สะดวกกับ V2 คุณสามารถใช้ V1 ได้เช่นกัน
•   ทุกอย่างถูกตั้งค่าเป็นค่าเริ่มต้น และฉันไม่ได้ทำการเปลี่ยนแปลงใดๆ ใน RTOS settings

__________________________________________________________________________________________________________________________________



อย่างที่ฉันได้กล่าวไปแล้ว ทุกอย่างถูกตั้งค่าเป็นค่าเริ่มต้น และคุณจะเห็นว่ามี a default Task ซึ่งถูกสร้างขึ้นโดยอัตโนมัติ ฉันปล่อยให้มันเหมือนเดิมและเราจะใช้มันในภายหลังในโค้ดของเรา


tha

Now let's see the LWIP Configuration



•   ตามที่แสดงในภาพด้านบน ฉันได้ปิดใช้งาน DHCP และป้อน the addresses ด้วยตัวเอง สิ่งนี้จะช่วยให้เราได้รับ the static IP เช่นกัน
•   เนื่องจากเราใช้ RTOS คุณจะเห็นว่ามันถูกเปิดใช้งานใน the "RTOS Dependency" section.

___________________________________________________________________________________________________________________________________



•   ใน the "Key Options" Tab ฉันได้เพิ่ม the heap เป็น 10 KiloBytes.
•   นอกเหนือจากนี้, ทุกอย่างยังคงถูกเซ็ตเป็นค่าเริ่มต้น.

นี้คือสำหรับ the Ethernet and LWIP configuration.

tha

The following Configuration is only for the Cortex M7 series MCUs

ตอนนี้เนื่องจากฉันใช้ MCU ที่ใช้ a cortex M7 ซึ่งไม่มี Flash memory เพียงพอ ฉันต้องใช้ the external flash และต้องกำหนดค่า MPU ให้เหมือนกัน ส่วนต่อไปนี้ของการกำหนดค่าใช้ได้สำหรับสิ่งที่เหมือนกันเท่านั้น



•   The MPU Control mode ถูกเซ็ตเป็น "Background Region Privileged access only + MPU disabled during hardfault"
•   ฉันได้เปิดใช้งาน the Instruction prefetch, CPU ICache and DCache.


ต่อไปนี้คือ the MPU Configuration สำหรับ the external Flash, ซึ่งถูกวางตำแหน่งที่ the memory address 0x90000000 และมีขนาด 16 MB.



•   โดยพื้นฐานฉันได้เซ็ต the 16 MB Region (external flash) เป็น the cacheable and bufferable region. สิ่งนี้จะกำหนดค่า the
     memory region เป็น the Normal memory type ที่มี write back attribute.
•   The 1 MB ในตอนเริ่มต้นจะอนุญาตให้ the instructions ถูกปฏิบัติจาก region นี้ได้ด้วย.

หากคุณต้องการเรียนรู้เพิ่มเติมเกี่ยวกับ MPU configuration and memory types, เราขอแนะนำให้คุณดู STM32 MPU Config playlist ใน the topic    https://www.youtube.com/playlist?list=PLfIJKC1ud8gjoY2McCCqkxXWuwiC2iSap

tha

                                                                         Some Insight into the code

The main function:



•   ไม่มีอะไรพิเศษเกิดขึ้นใน the Main function. The default task ถูกสร้างขึ้น, และ the kernel ถูกสตาร์ท.
•   The MPU configuration part จะสัมพันธ์กับ cortex M7 based MCUs

tha

The default Task:



•   ใน the default task, เราจะเรียก the udpclient_init() function เพื่อเริ่มต้น the UDP client.
•   The MX_LWIP_Init() function เริ่มต้น the LWIP และมันมีอยู่ใน the default Task โดยค่าเริ่มต้น.

tha

The udpclient_init Function:



ที่นี่เราจะสร้าง 2 new threads:
•   udpinit_thread จะถูกใช้สำหรับการเริ่มต้น the NETCONN UDP
•   เมื่อ the initialization ถูกทำ, มันจะรอสำหรับ the data ถูกรับโดย the server, และจากนั้นจัดการ data นั้นใน a forever running
     loop.
•   udpsend_thread จะถูกใช้สำหรับการส่ง the data ไปยัง the server เป็นระยะๆ.
•   ด้วยวิธีนี้, ไม่ว่า the server จะส่ง the data หรือไม่ก็ตาม, the client จะส่ง some data ไม่ยัง the server เสมอ.
•   The parameters เป็น NULL
•   The Stack Size ถูกเซ็ตเป็น Default คือ 1024 Bytes
•   และ the priority ถูกเซ็ตเป็น Normal