STM32 ETHERNET #7 UDP Server using LWIP NETCONN (RTOS)

Started by tha, February 22, 2023, 09:47:12 AM

Previous topic - Next topic

tha

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

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

                                                 UDP Server using LWIP NETCONN (RTOS)

นี่เป็นอีกหนึ่งบทช่วยสอนใน STM32 ETHERNET Series แต่ด้วยบทช่วยสอนนี้ เราจะเริ่มต้นอีเทอร์เน็ตด้วย RTOS, NETCONN เพื่อให้แม่นยำ จนถึงตอนนี้เราได้กล่าวถึงโปรโตคอล UDP, TCP และ HTTP แล้ว แต่ทั้งหมดนั้นใช้ RAW Library, ซึ่งไม่ใช่วิธีที่ the ETHERNET ถูกใช้โดยทั่วไป

มันเป็นเพียงการทำความเข้าใจว่าแต่ละโปรโตคอลสามารถทำอะไรได้บ้าง และทำงานอย่างไรในระดับพื้นฐาน เริ่มต้นบทช่วยสอนนี้เป็นต้นไป เราจะใช้ NETCONN Library ซึ่งใช้ RTOS และเป็นอีกเลเยอร์หนึ่งที่อยู่ด้านบนของ the basic UDP, TCP functions.

ดังนั้นในบทช่วยสอนนี้ เราจะเริ่มต้นด้วย UDP Server โดยใช้ NETCONN

NOTE:  ผมกำลังใช้ STM32F750 Discovery Board, ซึ่งมี the RMII connection type และไม่มี memory configuration option.
            หากคุณต้องการอันที่มี the memory configuration, โปรดดูที่
     https://youtu.be/Wg3edgNUsTk

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 udpserver_init() function เพื่อเริ่มต้น the UDP server แน่นอน.
•   The MX_LWIP_Init() function เริ่มต้น the LWIP และมันมีอยู่ใน the default Task โดยค่าเริ่มต้น.

tha

The udpserver_init Function:



ที่นี่เราจะสร้าง a new thread, ที่เรียกว่า the udp thread.

•   The entry function จะเป็น udp_thread
•   The parameter เป็น NULL
•   The Stack Size ถูกเซ็ตเป็นค่าเริ่มต้น คือ 1024 Bytes
•   และ the priority ถูกเซ็ตเป็น Normal