FreeRTOS

Started by tha, October 08, 2021, 08:24:01 AM

Previous topic - Next topic

tha


tha

Source Code Organization

The FreeRTOS zip file download บรรจุ the source code สำหรับ the FreeRTOS ports and demo applications ทั้งหมด – ดังนั้นจึงมีไฟล์มากกว่าที่จำเป็นในการ build และรัน the pre-configured demos ที่ใช้ the FreeRTOS Linux port. ดู the Source Code Organization page สำหรับข้อมูลเกี่ยวกับ the zip file's directory structure.

     •  The RTOS port layer for Linux (POSIX) อยู่ใน the FreeRTOS/Source/portable/ThirdParty/GCC/Posix directory.

     •  มีสอง demo projects: A Kernel only demo ซึ่งอยู่ใน the FreeRTOS/Demo/POSIX_GCC directory, และ a networking demo ซึ่งอยู่ใน the
         FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Echo_Posix directory.

tha

The Posix/Linux Simulator Demos

Kernel Demo Project

project นี้สาธิต FreeRTOS kernel functionality โดยใช้ the Linux (POSIX) port. The project สามารถถูกกำหนดค่าเพื่อรันอย่างใดอย่างหนึ่ง a simple Blinky style demo (BLINKY_DEMO), หรือ a more comprehensive style demo (FULL_DEMO) โดยการเซ็ตค่าคงที่ mainSELECTED_APPLICATION, ซึ่งถูก defined ไว้ที่ส่วนบนของ main.c.

     •  Blinky Demo

        ถ้า mainSELECTED_APPLICATION ถูกเซ็ตเป็น BLINKY_DEMO, ดังนั้น main() จะเรียก main_blinky(), ซึ่งถูกจัดให้มีใช้ใน main_blinky.c.
        main_blinky() สร้าง a very simple demo ที่ includes สอง tasks, a software timer, และ a queue. task หนึ่งส่งซ้ำๆค่า 100 ที่ความถี่ 200 มิลลิ
        วินาทีไปยังอีก task หนึ่งผ่านทาง the queue, ในขณะที่ the timer ส่งค่า 200 ทุกๆ 2000ms ไปยัง the same queue. The receiving task พิมพ์ออก a
        message แต่ละครั้งที่มันรับอย่างใดอย่างหนึ่งของค่าจาก the queue.

     •  Full Demo

        ถ้า mainSELECTED_APPLICATION ถูกเซ็ตเป็น FULL_DEMO, ดังนั้น main() จะเรียก main_full(), ซึ่งถูกจัดให้มีใช้ใน main_full.c. The demo ที่สร้าง
        โดย main_full() ประกอบด้วย the standard demo tasks เป็นหลัก, ซึ่งไม่กระทำฟังชั่นเฉพาะใดๆนอกเหนือจากการตรวจสอบ the RTOS port และการสาธิต
        วิธีที่ the FreeRTOS API สามารถถูกใช้.

        The full demo รวมเอา a 'check' task ที่ปฏิบัติทุกๆ (จำลอง) 10 วินาที, แต่มี the highest priority เพื่อให้แน่ใจว่ามันจะได้รับ processing time. หน้าที่
        หลักของมันคือตรวจสอบว่า the standard demo tasks ทั้งหมดยังคงทำงาน.  The check task จะรักษา a status string ที่ถูก output ไปยัง the console
        แต่ละครั้งที่มันปฏิบัติ. ถ้า the standard demo tasks ทั้งหมดกำลังรันโดยไม่มี error, ดังนั้น the string บรรจุ "OK" และ the current tick count. ถ้า an
        error ถูกตรวจพบ, ดังนั้น the string บรรจุ a message ที่แสดงให้เห็นซึ่ง task ที่รายงาน the error.


tha

Networking Demo Project

project นี้สาธิต networking บน Linux โดยใช้ the FreeRTOS+TCP TCP/IP stack. มันใช้ the TCP echo client demo ใหม่ที่แต่เดิมถูกเขียนสำหรับ the demo ที่ใช้ FreeRTOS+TCP ด้วย the Windows RTOS port.

The TCP echo demo ใช้ the FreeRTOS+TCP TCP/IP stack เพื่อเชื่อมต่อและสื่อสารกับ a standard TCP echo server บน TCP port 7. The FreeRTOS+TCP network อินเตอร์เฟสสำหรับ Linux จะใช้ libpcap เพื่อเข้าถึง the network.

การกำหนดค่า the TCP/IP stack สำหรับใช้กับ the demo:

     •  ปฏิบัติตามคำแนะนำในหัวข้อ the Software Setup #1, Software Setup #2, and Software Setup #4 sections ในหน้าที่อธิบายการใช้
         FreeRTOS+TCP บน Windows hosts (ขั้นตอนในหัวข้อ Software Setup #3 ไม่ถูกต้องการ).

     •  เซ็ตค่าคงที่ configECHO_SERVER_ADDR0 ถึง configECHO_SERVER_ADDR3 เป็น the address of the echo server ใน FreeRTOSConfig.h.

เป็นเรื่องปกติที่ TCP port 7 (the standard echo port) จะถูกบล็อกโดย firewalls. หากเป็นกรณีนี้ ให้เปลี่ยน the port number ที่ใช้โดยทั้ง the FreeRTOS application และ the echo server ไป เป็นตัวเลขที่สูงแต่ถูกต้อง เช่น 50000. The port number ที่ใช้โดย the FreeRTOS application จะถูกเซ็ตโดย the echoECHO_PORT constant ใน TCPEchoClient_SingleTasks.c. ถ้าคุณสร้าง a TCP echo server โดยใช้ the nc command ใน Linux ดังนั้น the port number จะถูกเซ็ตโดยใช้ the -l switch:

$ sudo nc -l 7

Network troubleshooting

ARP responses อาจไม่ถูกส่งถ้า the echo server กำลังรันบนคอมพิวเตอร์เดียวกันกับ the FreeRTOS demo, ส่งผลให้ the demo ไม่สามารถเชื่อมต่อไปยัง the echo server. ถ้านี้คือปัญหา, ดังนั้นให้รัน the echo server บนคอมพิวเตอร์เครื่องอื่นมากกว่านั้นที่ the RTOS demo กำลังปฏิบัติ.

tha

Building the Posix/Linux Simulator Demos

เนื้อหาดูในลิ้งค์เอานะครับ  https://www.freertos.org/FreeRTOS-simulator-for-Linux.html??

tha

GDB Debugging Tips

section นี้ทึกทักเอาว่าคุณได้ติดตั้ง, และทำความคุ้นเคยกับ, gdb. คุณสามารถหา the gdb documentation ที่นี่.

The port layer ใช้สอง process signals: SIGUSR1, and SIGALRM. ถ้า a pthread ไม่ได้กำลังรอ the signal, ดังนั้น GDB จะหยุด the process ชั่วคราวเมื่อมันรับ the signal. GDB ต้องถูกบอกให้ละเลย (และไม่พิมพ์) the signal SIGUSR1 เนื่องจากมันถูกรับแบบ asynchronous โดยแต่ละ thread. ใน GDB, ให้ป้อน:

$ handle SIGUSR1 nostop noprint pass

เพื่อให้แน่ใจว่า debugging ไม่ถูกอินเตอร์รัพท์โดย the signals. ดู:

$ man signal

สำหรับข้อมูลที่มากขึ้น.

หรืออีกทางหนึ่ง, สร้าง a file ใน your home directory ที่ชื่อ .gdbinit และใส่สองบรรทัดต่อไปนี้ลงไปในมัน:

handle SIGUSR1 nostop noignore noprint
handle SIGALRM nostop noignore noprint

เมื่อคุณเพิ่มสองบรรทัดนี้ลงในไฟล์ .gdbinit มันจะบอก GDB ไม่ให้เบรคบนสัญญาณเหล่านั้น

มี timers ที่แตกต่างกันสามตัวมีให้ใช้ประโยชน์สำหรับใช้เป็น the System tick: ITIMER_REAL, ITIMER_VIRTUAL and ITIMER_PROF. The default timer คือ ITIMER_VIRTUAL เพราะว่ามันเพียงแต่นับเมื่อ the process กำลังปฏิบัติใน user space, ดังนั้นมันจะหยุดเมื่อ a break-point ถูกตี. ITIMER_PROF เทียบเท่ากับ ITIMER_VIRTUAL แต่มันรวมเวลาที่ใช้ในการเรียกระบบ. ITIMER_REAL จะนับต่อเนื่องแม้เมื่อ the process ไม่ได้กำลังปฏิบัติแล้วก็ตาม, ดังนั้นมันจะแสดง real-time. ITIMER_REAL เป็นตัวเลือกเดียวที่ใช้งานได้ เนื่องจากตัวจับเวลาอื่นๆ จะไม่ติ๊ก เว้นแต่ว่ากระบวนการกำลังทำงานอยู่จริง ด้วยเหตุผลดังกล่าว, ถ้า nanosleep ถูกเรียกใน the IDLE task hook, the time ที่รายงานโดย the non-real timers แทบจะไม่เพิ่มขึ้นเลย.

tha

Port-Layer Design Description

การจัดให้มีใช้อย่างง่ายของ a FreeRTOS Simulator จะห่อหุ้ม the platform native threads, และทุกการเรียกเพื่อสลับ Task contexts จะเรียก the OS suspend and resume thread API. simulator นี้ใช้ the Posix condition variables and Signals เพื่อควบคุมการปฏิบัติของ the underlying Posix threads. Signals สามารถถูกส่งไปยัง the threads แบบอะซิงโครนัส, ดังนั้นพวกมันอินเตอร์รัพท์การปฏิบัติของ the target thread, ในขณะที่ suspended threads จะรอบ condition variables เพื่อกลับมาทำงานต่อ.

โดยปกติ, เมื่อออกแบบ a multi-threaded process, เราใช้หลาย threads เพื่อยอมให้สำหรับการปฏิบัติพร้อมกันและเพื่อจัดให้มีใช้ a degree of non-blocking on IO tasks. simulator นี้ไม่ใช้ the threads เพื่อสำเร็จ concurrent execution, เพียงแต่เก็บ the context of the execution. Signals, mutexes and condition variables ถูกใช้เพื่อซิงโครไนส์ context switching, แต่ในท้ายที่สุด การตัดสินใจเปลี่ยน the context ถูกขับเคลื่อนโดย the FreeRTOS scheduler

เมื่อ a new Task ถูกสร้าง, a pthread ถูกสร้างเป็น the context สำหรับการปฏิบัติของ Task นั้น. The pthread ระงับตัวเองทันที, และรีเทิร์น the execution ไปยัง the creator. เมื่อ a pthread ถูกระงับ, มันกำลังรออยู่ในการเรียกถึง pthread_cond_wait, ซึ่งจะถูกบล็อกจนกระทั่งมันได้รับ a resume signal pthread_cond_signal.

FreeRTOS Tasks สามารถถูกสลับในสองทาง, co-operatively(ทำงานร่วมกัน) โดยการเรียก taskYIELD() หรือ pre-emptively(ยึดเอาให้ตัวเองก่อน) เป็นส่วนหนึ่งของ the RTOS system Tick. ใน simulator นี้, the Task contexts ถูกสลับโดยกลับมาทำงานต่อ the next task context (ที่ตัดสินใจโดย the FreeRTOS Scheduler) และการระงับ the current context (ที่มีการจับมือกันสั้นๆ ระหว่างทั้งสอง).

The RTOS system tick ถูกสร้างขึ้นโดยใช้ an ITIMER และ the signal ถูกส่ง (เฉพาะไปยัง) the currently executing pthread. The RTPS system tick signal handler จะเพิ่ม the tick count และเลือก the next RTOS task context. มันกลับมาทำงาน thread นั้นต่อและส่ง a signal ไปยังตัวเองเพื่อระงับ. The suspend จะถูกประมวลผลเฉพาะเมื่อออกจาก the TOS system tick signal handler , เนื่องจาก signals ถูก queued.

tha

Known Issues (รู้ปัญหา)

pthread_create และ pthread_exit/cancel เป็นการเรียกที่เข้มข้นของระบบ ซึ่งอาจทำให้เวลาในการประมวลผลอิ่มตัวอย่างรวดเร็ว

ถ้าคุณเรียก system and library functions ทีบล็อกthat block (printf), นี้สามารถเป็นเหตุให้ the whole process หยุดชะงัก. ถ้าจำเป็นต้องทำ system calls, ทุก signals บน thread นั้นต้องถูกบัง, จากนั้นยอมให้ใหมหลังจาก the system call เสร็จสิ้นการปฏิบัติ. threads เพิ่มเติมสามารถถูกสร้างเพื่อจำลอง interrupts, แต่ signals ควรถูกบังใน threads เหล่านั้นเช่นกัน, ดังนั้นพวกมันจะไม่รับ signals และดังนั้นจะถูกวางกำหนดเวลาเพื่อปฏิบัติโดย the FreeRTOS scheduler และกลายมาเป็นส่วนหนึ่งของ the regular FreeRTOS tasks.

เพื่อป้องกันไม่ให้ the process จากการขโมยทั้งหมดของ the Idle execution time ของ the Host OS, ให้ใช้ nano_sleep(). มันจะไม่ใช้ signals ใดๆในการจัดให้มีใช้ของมันแต่จะยกเลิกจาก the sleep/suspend process ทันทีเพื่อให้บริการ a signal. ดังนั้น วิธีที่ดีที่สุดในการใช้มันคือการเซ็ต a sleep time ให้ยาวนานกว่า a FreeRTOS execution time-slice และเรียกมันจาก the Idle task ดังนั้น the process จะระงับจนกระทั่ง the next tick.