Electoday 2025

ไมโครคอนโทรลเลอร์ => ARM Processors => Topic started by: tha on October 08, 2021, 08:24:01 AM

Title: FreeRTOS
Post by: tha on October 08, 2021, 08:24:01 AM
https://www.freertos.org/FreeRTOS-quick-start-guide.html (https://www.freertos.org/FreeRTOS-quick-start-guide.html)

FreeRTOS Kernel Quick Start Guide

หน้านี้เริ่มต้นด้วยการอธิบายวิธีทำให้ the RTOS รันบน your target เร็วที่สุดเท่าที่เป็นได้. ด้านล่างนั้น the "Next steps - further reading" section จัดให้มีชุดของลิ้งค์ที่ช่วยให้คุณเพิ่มพูนความรู้ของคุณ, ตอบคำถามทั่วไป, และกลายเป็นผู้ใช้ที่ชำนาญ FreeRTOS.

โปรดดู the Getting Started With Simple FreeRTOS Projects documentation, และ the FreeRTOS books สำหรับการเริ่มต้นที่ดีที่สุดเท่าที่เป็นได้. มีแม้กระทั่ง a FreeRTOS Windows port เพื่อยอมให้ทดลองกับ FreeRTOS บน a Windows host, โดยใช้ free tools, และไม่มี any special hardware requirements. New developers ถูกแนะนำให้ใช้ the configASSERT() macro อีกด้วย.
Title: Re: FreeRTOS
Post by: tha on October 08, 2021, 09:00:02 AM
RTOS quick start instructions

FreeRTOS ถูกพอร์ตไปยัง many different architectures และ compilers. แต่ละ RTOS port ถูกมาพร้อมกับ a pre-configured demo application ให้คุณพร้อมใช้งานได้อย่างรวดเร็ว. ที่ดีกว่ายังคง, แต่ละ demo application ถูกมาพร้อมกับ a documentation page จัดให้มี full information เกี่ยวกับการค้นหา the RTOS demo project source code, building the demo project, และการกำหนดค่า the target hardware.

The demo application documentation page จัดให้มี essential RTOS port specific information อีกด้วย, รวมถึง how to write FreeRTOS compatible interrupt service routines, ซึ่งจำเป็นต้องแตกต่างกันเล็กน้อยบน different microcontroller architectures.
Title: Re: FreeRTOS
Post by: tha on October 08, 2021, 10:43:43 AM
ทำตามคำแนะนำง่ายๆ เหล่านี้เพื่อเริ่มต้นใช้งานในไม่กี่นาที:

1. Download the RTOS source code:

Download the FreeRTOS .zip file. นี้บรรจุ the RTOS kernel source code และ demo projects สำหรับ every official port. อย่าตกใจกับจำนวนไฟล์ เพียงส่วนย่อยเล็กๆ ที่จำเป็นสำหรับ one demo! Unzip the files ลงใน a convenient directory.
Title: Re: FreeRTOS
Post by: tha on October 08, 2021, 11:55:11 AM
2. ค้นหา the relevant documentation page:

ค้นหา the "Supported Devices" ใต้ the Kernel dropdown, จากนั้นคลิก the "Officially Supported Demos" link (ดูรูปภาพทางขวา) เพื่อดู a list of microcontroller vendors ที่ถูกรองรับโดย FreeRTOS. คลิก a microcontroller vendor name จะนำคุณไปยัง a list of documentation pages ที่เฉพาะเจาะจงถึงผู้ขายรายนั้น.

อ้างถึง the Modifying a demo application to run on different hardware page หาก a pre-configured port ที่ไม่มีให้ใช้เป็นประโยชน์สำหรับ your development board.
Title: Re: FreeRTOS
Post by: tha on October 08, 2021, 04:58:59 PM
3. Building the project:

ทำตามคำแนะนำบน the RTOS port documentation page เพื่อค้นหา the required project ภายใน the FreeRTOS directory structure, จากนั้นเปิดและ build the demo project.

4. Running the demo application:

ทำตามคำแนะนำบน the RTOS port documentation page เพื่อตั้งค่า the target hardware, ดาวน์โหลดและปฏิบัติ the demo application. The same documentation page จะจัดให้มี information เกี่ยวกับ the functionality of the demo application เพื่อให้คุณทราบว่ามันทำงานอย่างถูกต้องหรือไม่.

5. Create your own project:

วิธีที่ง่ายที่สุดในการสร้าง your own FreeRTOS project คือให้มันอยู่บนฐานของ the demo application ที่ถูกจัดให้มีสำหรับ your chosen port. เมื่อคุณมี the demo application ที่รันได้แล้ว, ให้ลบ the demo functions และ source files ออกทีละน้อยและแทนที่พวกมันด้วย your own application code.  ความช่วยเหลือในการแก้ไขปัญหามีให้ใช้ประโยชน์ภายใต้ the FAQ "My Application ไม่ทำงาน, มีอะไรที่ผิดรึ?".
Title: Re: FreeRTOS
Post by: tha on October 08, 2021, 05:43:55 PM
Further Reading(อ่านเพิ่มเติม)

วิธีที่ง่ายที่สุดในการสร้าง your own FreeRTOS application คือให้มันอยู่บนฐานของ the demo application ที่ถูกจัดให้มีสำหรับ your chosen port. เมื่อคุณมี the demo application ที่รันได้แล้ว, ให้ลบ the demo functions และ source files ออกทีละน้อยและแทนที่พวกมันด้วย your own application code.

ต่อไปนี้เป็นทางลัดไปยังข้อมูลที่มีค่าสำหรับนักพัฒนาที่จริงจัง:

     •  The FreeRTOS API reference สามารถเข้าถึงได้ผ่านทาง the API Reference menu. ตัวอย่างเช่น, รูปภาพทางด้านขวาแสดงวิธีเข้าถึงเอกสารบนทุก the
         FreeRTOS Task Control functions. A pdf Reference Manual มีให้ใช้ประโยชน์อีกด้วย.
     •  ทำความเข้าใจ the FreeRTOS directory structure.
     •  บทนำสู่ the RTOS demo application projects.
     •  การปรับเปลี่ยน an RTOS demo application เพื่อรันบน different hardware.
     •  ทำความเข้าใจ the FreeRTOS license.
     •  คำถามที่พบบ่อย: แอปพลิเคชันของฉันไม่ทำงาน มีอะไรผิดพลาด?
     •  การใช้ configASSERT() เพื่อดักจับข้อผิดพลาดของผู้ใช้
     •  การได้รับการสนับสนุนฟรี
     •  การได้รับบริการสนับสนุนและการพัฒนาเชิงพาณิชย์ (ลิงก์ไปยังเว็บไซต์พันธมิตรบุคคลที่สาม!)
Title: Re: FreeRTOS
Post by: tha on October 09, 2021, 06:05:43 AM
https://www.freertos.org/Documentation/RTOS_book.html (https://www.freertos.org/Documentation/RTOS_book.html)

FreeRTOS Documentation
PDF files

ความต้องการที่ไม่เคยมีมาก่อนสำหรับ FreeRTOS ทำให้เรายุ่งมาก - มากเสียจนหาเวลาทำหนังสือเล่มล่าสุดของเราให้เสร็จ "Mastering the FreeRTOS Real Time Kernel" เป็นสิ่งที่ท้าทาย! มีการจัดเตรียมสำเนาก่อนเผยแพร่ฟรีให้กับผู้ซื้อหนังสือเก่ามาระยะหนึ่งแล้ว และตอนนี้เราได้ขยายข้อเสนอนี้ให้กับทุกคนแล้ว ใช้ลิงก์ด้านล่างเพื่อดาวน์โหลดสำเนาของคุณ

   Mastering the FreeRTOS Real Time Kernel - a Hands On Tutorial Guide
   FreeRTOS V10.0.0 Reference Manual
   Book companion source code
Title: Re: FreeRTOS
Post by: tha on October 09, 2021, 06:38:18 AM
https://www.freertos.org/RTOS.html (https://www.freertos.org/RTOS.html)

The FreeRTOS™ Kernel
ผู้นำตลาด มาตรฐานโดยพฤตินัย และเคอร์เนล RTOS ข้ามแพลตฟอร์ม

พัฒนาร่วมกับ the world's leading chip companies เป็นระยะเวลา a 18 year, FreeRTOS เป็นผู้นำตลาด real--time operating system (RTOS) สำหรับ microcontrollers และ small microprocessors. แจกจ่ายฟรีภายใต้ the MIT open source license, FreeRTOS รวมเอา a kernel และชุดที่เติบโตขึ้นของ libraries เหมาะสำหรับการใช้งานในทุกภาคส่วนอุตสาหกรรม. ด้วยการดาวน์โหลดหนึ่งครั้งทุกๆ 170 วินาที, FreeRTOS ถูกสร้างขึ้นโดยเน้นที่ความน่าเชื่อถือ ความสามารถเข้าถึงได้ และความสะดวกในการใช้งาน
Title: Re: FreeRTOS
Post by: tha on October 09, 2021, 07:36:28 AM
Did you know? (คุณรู้หรือเปล่า?)

     •  FreeRTOS ถูกดาวน์โหลดทุกๆ 170 seconds (โดยเฉลี่ย, ในระหว่างปี 2019).
     •  FreeRTOS อยู่ในอันดับต้น ๆ ในทุกๆ EETimes Embedded Market Survey ตั้งแต่ปี 2011, ซึ่งเป็นปีแรกที่มันถูกรวมไว้.
     •  FreeRTOS เสนอ lower project risks และ a lower total cost of ownership กว่าทางเลือกเชฺงพาณิชย์เนื่องจาก:
             •  ได้รับการสนับสนุนอย่างเต็มที่และจัดทำเป็นเอกสาร
             •  คนส่วนใหญ่นำผลิตภัณฑ์ออกสู่ตลาดโดยไม่ต้องติดต่อเรา แต่ด้วยความอุ่นใจอย่างเต็มที่ พวกเขาสามารถเลือกที่จะเปลี่ยนไปใช้ใบอนุญาตเชิงพาณิชย์ที่มีการ
                 ชดใช้ค่าเสียหายทั้งหมด (พร้อมการสนับสนุนเฉพาะ) ได้ตลอดเวลา
     •  บาง FreeRTOS ports ไม่เคยปิดการใช้งาน interrupts อย่างสมบูรณ์.
     •  เพื่อวัตถุประสงค์ในการควบคุมคุณภาพที่เข้มงวด และเพื่อขจัดความคลุมเครือในการเป็นเจ้าของ IP ทั้งหมด โค้ด FreeRTOS อย่างเป็นทางการจะถูกแยกออกจากการมี
         ส่วนร่วมของชุมชน
     •  FreeRTOS มีโหมด tick-less เพื่อรองรับ low power applications โดยตรง.
     •  FreeRTOS ถูกออกแบบมาให้เรียบง่ายและใช้งานง่าย: source files เพียง 3 ไฟล์ที่ใช้กับพอร์ต RTOS ทั้งหมด และ one microcontroller specific source
         file ถูกต้องการและ API ของไฟล์นั้นได้รับการออกแบบมาให้ใช้งานง่ายและเข้าใจง่าย
     •  The RL78 port สามารถสร้าง 13 tasks, 2 queues and 4 software timers ในภายใต้ 4K bytes of RAM
Title: Re: FreeRTOS
Post by: tha on October 09, 2021, 11:04:38 AM
Why choose FreeRTOS? (ทำไมต้องเลือก FreeRTOS?)

" ณ จุดนี้อาจพูดได้อย่างปลอดภัยว่า FreeRTOS ผ่าน 'การตรวจสอบโดยเพื่อน' มากกว่า RTOS อื่น ๆ ที่มีอยู่บนโลก ฉันได้ใช้มันในหลายโครงการ - หนึ่งในนั้นคือสภาพแวดล้อมแบบมัลติโปรเซสเซอร์ที่ใช้โปรเซสเซอร์มากกว่า 64 ตัวและ จำเป็นต้องรันเป็นเวลาหลายเดือนอย่างน่าเชื่อถือ แกน RTOS ทำงานได้ดี ลองใช้ FreeRTOS เลย" - John Westmoreland

FreeRTOS จัดให้มีสิ่งดีที่สุดของทุกโลก: FreeRTOS ฟรีและรองรับอย่างแท้จริง, แม้เมื่อใช้ใน commercial applications. The FreeRTOS open source MIT license ไม่ต้องการให้คุณเปิดเผย IP ที่เป็นกรรมสิทธิ์ของคุณ. คุณสามารถนำผลิตภัณฑ์ออกสู่ตลาดโดยใช้ FreeRTOS โดยไม่ต้องพูดคุยกับเรา, ไม่ต้องเสียค่าธรรมเนียมใดๆ,  และผู้คนหลายพันคนทำอย่างนั้น. เมื่อใดก็ตามที่คุณต้องการรับข้อมูลสำรองเพิ่มเติม หรือหากทีมกฎหมายของคุณต้องการหนังสือรับรองหรือการชดใช้ค่าเสียหายเพิ่มเติม เมื่อนั้นมีเส้นทางอัปเกรดเชิงพาณิชย์ที่มีต้นทุนต่ำอย่างง่าย ความอุ่นใจของคุณมาพร้อมกับความรู้ที่คุณสามารถเลือกใช้เส้นทางการค้าได้ตลอดเวลาที่คุณเลือก
Title: Re: FreeRTOS
Post by: tha on October 09, 2021, 11:38:38 AM
นี่คือบางเหตุผลว่าทำใม FreeRTOS เป็นตัวเลือกที่ดีสำหรับ your next application - FreeRTOS...

     •  จัดให้มี a single and independent solution สำหรับ many different architectures และ development tools.
     •  เรียกได้ว่ามีความน่าเชื่อถือ มั่นใจได้ด้วยกิจกรรมที่ดำเนินการโดย the SafeRTOS sister project.
     •  เป็นคุณลักษณะที่หลากหลายและยังคงอยู่ระหว่างการพัฒนาอย่างต่อเนื่อง
     •  มี a minimal ROM, RAM and processing overhead. โดยทั่วไป an RTOS kernel binary image จะอยู่ในขอบเขตของ 6K ถึง 12K bytes.
     •  ง่ายมาก - แกนหลักของเคอร์เนล RTOS มีอยู่ในไฟล์ C เพียง 3 ไฟล์เท่านั้น ไฟล์ส่วนใหญ่จำนวนมากที่รวมอยู่ในการดาวน์โหลดไฟล์ .zip เกี่ยวข้องกับแอปพลิเคชั่น
         สาธิตจำนวนมากเท่านั้น
     •  ใช้งานได้ฟรีอย่างแท้จริงในแอปพลิเคชันเชิงพาณิชย์ (ดูรายละเอียดเงื่อนไขใบอนุญาต)
     •  มีการอนุญาตให้ใช้สิทธิ์เชิงพาณิชย์ การสนับสนุนอย่างมืออาชีพ และบริการการย้ายพอร์ตที่มีอยู่ในรูปแบบ OPENRTOS จากระบบความสมบูรณ์สูงของ
         WITTENSTEIN ซึ่งเป็นพันธมิตรของเรา
     •  มีเส้นทางการโยกย้ายไปยัง SafeRTOS ซึ่งรวมถึงการรับรองสำหรับภาคการแพทย์ ยานยนต์ และอุตสาหกรรม
     •  มีฐานผู้ใช้ที่ใหญ่และเติบโตขึ้นเรื่อยๆ
     •  ประกอบด้วยตัวอย่างที่กำหนดไว้ล่วงหน้าสำหรับแต่ละพอร์ต ไม่จำเป็นต้องหาวิธีตั้งค่าโครงการ - เพียงแค่ดาวน์โหลดและคอมไพล์!
     •  มีฟอรัมการสนับสนุนฟรีที่ยอดเยี่ยม ได้รับการตรวจสอบ และใช้งานได้ฟรี
     •  มีการประกันว่ามีการสนับสนุนทางการค้าหากจำเป็น
     •  ให้เอกสารเพียงพอ
     •  ปรับขนาดได้มาก เรียบง่าย และใช้งานง่าย
     •  FreeRTOS เสนอทางเลือกในการประมวลผลแบบเรียลไทม์ที่เล็กกว่าและง่ายกว่าสำหรับแอปพลิเคชันที่ eCOS, embedded Linux (หรือ Real Time Linux) และ
         แม้แต่ uCLinux จะไม่พอดี ไม่เหมาะสม หรือไม่พร้อมใช้งาน
Title: Re: FreeRTOS
Post by: tha on October 10, 2021, 08:32:50 AM
https://www.freertos.org/FreeRTOS-Coding-Standard-and-Style-Guide.html (https://www.freertos.org/FreeRTOS-Coding-Standard-and-Style-Guide.html)

Coding Standard, Testing and Style Guide

บนหน้านี้:

     •  Coding Standard
     •  Testing
     •  Naming Conventions
     •  Data Types
     •  Style Guide

Coding Standard / MISRA Compliance

The core FreeRTOS source files (ไฟล์ที่ใช้กันทั่วไปในพอร์ตทั้งหมด แต่ไม่ใช่ the port layer) ปฏิบัติตาม the MISRA coding standard guidelines. การปฏิบัติตามถูภเช็คโดยใช้ pc-lint กับ the linked lint configuration files. ดังที่ the standard มีความยาวหลายหน้า และมีให้ใช้ประโยชน์โดยซื้อได้จาก MISRA โดยมีค่าธรรมเนียมเพียงเล็กน้อย เราไม่ได้ทำซ้ำกฎทั้งหมดที่นี่.
Title: Re: FreeRTOS
Post by: tha on October 10, 2021, 09:20:00 AM
การเบี่ยงเบนจากมาตรฐาน MISRA ถูกแสดงรายการไว้ด้านล่าง:

     •  สอง API functions มีมากกว่าหนึ่ง exit point. A deviation ถูกอนุญาติให้ในสองกรณีนี้ด้วยเหตุผลด้านประสิทธิภาพที่สำคัญ.
     •  เมื่อสร้าง tasks, the source code จัดการ memory addresses เพื่อค้นหา the start and end addresses ของ the stack ที่จัดสรรให้กับ the created
         task. The code ต้องทำงานได้กับทุก the architectures ที่ FreeRTOS ถูกพอร์ต - ซึ่งรวมถึง architectures ที่มี 8, 16, 20, 24 and 32-bit buses. สิ่งนี้
         ต้องการบาง pointer arithmetic อย่างหลีกเลี่ยงไม่ได้ . เมื่อ pointer arithmetic ถูกใช้, the arithmetic result ถูกตรวจเช็คโดยทางโปรแกรมเพื่อความถูกต้อง.
Title: Re: FreeRTOS
Post by: tha on October 10, 2021, 10:00:13 AM
     •  The trace macros, โดยค่าเริ่มต้น, จะว่างเปล่า, ดังนั้นไม่สร้างโค้ดใดๆ. ดังนั้น, MISRA compliance checking ถูกดำเนินการด้วย dummy macro
         definitions.
     •  MISRA rules ถูกปิดทีละบรรทัดโดย line basis, ตามความเหมาะสม (นั่นคือ การปฏิบัติตามกฎถูกถือว่าสร้างโค้ดที่เหมาะสมสำหรับ a deeply embedded system
         น้อยกว่าการไม่ปฏิบัติตามอย่างระมัดระวัง). เหตุการณ์ดังกล่าวแต่ละครั้งจะมาพร้อมกับการให้เหตุผลโดยใช้ the special pc-lint MISRA comment mark-up
         syntax.
Title: Re: FreeRTOS
Post by: tha on October 10, 2021, 10:18:36 AM
FreeRTOS สร้างด้วย many different compilers, ซึ่งบางตัวก็ล้ำหน้ากว่าตัวอื่นๆ. ด้วยเหตุผลดังกล่าว FreeRTOS จึงไม่ใช้คุณลักษณะหรือรูปแบบใดๆ ที่นำมาใช้กับภาษา C โดยหรือตั้งแต่มาตรฐาน C99. ข้อยกเว้นประการหนึ่งคือการใช้ the stdint.h header file. The FreeRTOS/Source/include directory บรรจุไฟล์หนึ่งชื่อ stdint.readme ที่สามารถเปลี่ยนชื่อเป็น stdint.h เพื่อจัดให้มี the minimum stdint type definitions ที่จำเป็นต่อการ build FreeRTOS - หาก your compiler ไม่จัดให้มีมันเป็นของตัวเอง.
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 05:17:14 AM
Testing

ส่วนนี้อธิบาย the tests ที่ทำกับโค้ดทั่วไป (the code ที่อยู่ใน the FreeRTOS/Source directory, ที่ถูกสร้างโดยทุก FreeRTOS kernel ports), และ the tests ถูกทำบน the portable layer code (the code ที่อยู่ใน subdirectories ของ the FreeRTOS/Source/portable directory).

     •  Common code

        The standard demo/test files พยายามจัดให้มี 'branch' test coverage (ในกรณีส่วนใหญ่ไฟล์นี้จะบรรลุจริงๆ 'condition' coverage เพราะ the kernel's
        coding style จงใจรักษาเงื่อนไขที่เรียบง่ายเฉพาะเจาะจงสำหรับจุดประสงค์นี้), โดย tests ทำให้แน่ใจว่าทั้ง 'true' and 'false' paths ผ่านแต่ละ decision ถูก
        ปฏิบัติ. 'branch' coverage ถูกวัดโดยใช้ GCOV โดยการกำหนด the mtCOVERAGE_TEST_MARKER() macro ไปยัง a NOP (no operation)
        instruction ใน the 'else' path ของแต่ละ 'if()' condition ถ้า the 'else' path อีกอย่างหนึ่งเป็นว่างเปล่า. mtCOVERAGE_TEST_MARKER() ถูกกำหนด
        เฉพาะในขณะที่ทำการวัด test coverage - โดยปกติมันเป็น an empty macro ที่ไม่ได้สร้างโค้ดใดๆ.

     •  Port layer

        Port layer code ถูกตรวจสอบโดยใช้ 'reg test' tasks, และ, สำหรับ ports ที่รองรับ interrupt nesting, the 'interrupt queue' tasks.

        The 'reg test' tasks สร้างหลาย (ปกติสอง) tasks ที่อย่างแรกเติมทุก the CPU registers ด้วยค่าที่รู้, จากนั้นตรวจสอบต่อไปว่าทุกๆ register รักษาค่าที่ทราบ
        ที่คาดไว้ของมันในขณะที่การทดสอบอื่นๆ ดำเนินการอย่างต่อเนื่อง (soak test) แต่ละ reg test task ใช้ค่าที่ไม่ซ้ำกัน.

        The 'interrupt queue' tasks ทำการตรวจสอบบน interrupts ของ different priorities ที่อย่างน้อย three deep. Macros ถูกใช้ใส่ artificial delays
        ลงใน pertinent points ภายใน the code เพื่อให้แน่ใจว่า the desired test coverage ถูกทำสำเร็จ.

เป็นที่น่าสังเกตว่าการทดสอบอย่างละเอียดถี่ถ้วนมีส่วนในการค้นหาจุดบกพร่องในซิลิคอนหลายครั้ง
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 06:23:32 AM
Naming Conventions

The RTOS kernel และ demo application source code ใช้แบบแผนต่อไปนี้:

     •  Variables
          •  ตัวแปรประเภท uint32_t ถูกนำหน้าด้วย ul, โดยที่ the 'u' หมายถึง 'unsigned' และ the 'l' หมายถึง 'long'.
          •  ตัวแปรประเภท uint16_t ถูกนำหน้าด้วย us, โดยที่ the 'u' หมายถึง 'unsigned' และ the 's' หมายถึง 'short'.
          •  ตัวแปรประเภท uint8_t ถูกนำหน้าด้วย uc, โดยที่ the 'u' หมายถึง 'unsigned' และ the 'c' หมายถึง 'char'.
          •  ตัวแปรของ non stdint types ถูกนำหน้าด้วย x. ตัวอย่างได้แก่ BaseType_t และ TickType_t, ซึ่งเป็น portable layer ที่กำหนด typedefs สำหรับ the
              natural หรือ most efficient type สำหรับ the architecture และ the type ที่ใช้เพื่อถือ the RTOS tick count ตามลำดับ.
          •  Unsigned variables ของ non stdint types มี an additional prefix u. ตัวอย่างเช่น variables of type UBaseType_t (unsigned BaseType_t)
              ถูกนำหน้าด้วย ux.
          •  Variables of type size_t ถูกนำหน้าด้วย x.> อีกด้วย
          •  Enumerated variables ถูกนำหน้าด้วย e
          •  Pointers มี an additional prefixed p, ตัวอย่างเช่น a pointer ไปยัง a uint16_t จะมี prefix pus.
          •  ตามคำแนะนำของ MISRA, unqualified standard char types ถูกอนุญาติให้เฉพาะถือ ASCII characters และถูกนำหน้าด้วย c.
          •  ตามคำแนะนำของ MISRA, variables of type char * ถูกอนุญาติให้เฉพาะถือ pointers ไปยัง ASCII strings และถูกนำหน้าด้วย pc.
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 08:28:40 AM
     •  Functions
         •  File scope static (private) functions ถูกนำหน้าด้วย prv.
         •  API functions ถูกนำหน้าด้วย return type ของพวกมัน, ตามข้อตกลงที่กำหนดสำหรับ variables, ด้วยการเพิ่มของ the prefix v สำหรับ void.
         •  API function names เริ่มด้วยชื่อของ the file ซึ่งพวกมันถูกกำหนด. ตัวอย่างเช่น vTaskDelete ถูกกำหนดใน tasks.c, และมี a void return type.

     •  Macros
         •  Macros ถูกนำหน้าด้วย the file ซึ่งพวกมันถูกกำหนด. The pre-fix เป็นตัวพิมพ์เล็ก. ตัวอย่างเช่น, configUSE_PREEMPTION ถูกกำหนดใน
             FreeRTOSConfig.h.
         •  นอกเหนือจากคำนำหน้าแล้ว มาโครจะถูกเขียนด้วยตัวพิมพ์ใหญ่ทั้งหมด และใช้ขีดล่างเพื่อแยกคำต่างๆ
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 09:03:06 AM
Data Types

เฉพาะ stdint.h types และ the RTOS's own typedefs ถูกใช้, โดยมีข้อยกเว้นต่อไปนี้:

     •  char

         ตามคำแนะนำของ MISRA, unqualified char types ถูกอนุญาติให้, แต่เฉพาะเมื่อพวกมันถูกใช้เพื่อถือ ASCII characters.

     •  char *

         ตามคำแนะนำของ MISRA, unqualified character pointers ถูกอนุญาติให้, แต่เฉพาะเมื่อพวกมันถูกใช้เพื่อชี้ไปยัง ASCII strings. นี้ขจัดความจำเป็นใน
         การระงับ benign compiler warnings เมื่อ standard library functions ที่คาดว่า char * parameters ถูกใช้, โดยเฉพาะอย่างยิ่งการพิจารณาบาง
         compilers มีค่าเริ่มต้น unqualified char types เป็น signed ในขณะที่ compilers อื่นๆมีค่าเริ่มต้น unqualified char types เป็น unsigned.
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 10:01:18 AM
มี 4 types ที่ถูกกำหนดสำหรับแต่ละ port. เหล่านี้คือ:

     •  TickType_t

         ถ้า configUSE_16_BIT_TICKS ถูกตั้งค่าเป็น non-zero (true), ดังนั้น TickType_t ถูกกำหนดเป็น an unsigned 16-bit type. ถ้า
         configUSE_16_BIT_TICKS ถูกตั้งค่าเป็น zero (false), ดังนั้น TickType_t ถูกกำหนดเป็น an unsigned 32-bit type. ดู the customisation section
         ของ the API documentation สำหรับข้อมูลเติม.

         32-bit architectures ควรตั้งค่า configUSE_16_BIT_TICKS เป็น 0 เสมอ.

     •  BaseType_t

        นี้ถูกกำหนดเป็น the most efficient, natural, type สำหรับ the architecture. ตัวอย่างเช่น, บน a 32-bit architecture BaseType_t จะถูกกำหนดเป็น a
        32-bit type. บน a 16-bit architecture BaseType_t จะถูกกำหนดเป็น a 16-bit type. ถ้า BaseType_t ถูกกำหนดเป็น char จะต้องใช้ความระมัดระวังเป็น
        พิเศษเพื่อให้แน่ใจว่า signed chars ถูกใช้สำหรับ function return values ที่สามารถเป็น negative เพื่อแสดง an error.

     •  UBaseType_t

        นี้คือ an unsigned BaseType_t.

     •  StackType_t

        กำหนดไปยัง the type ที่ใช้โดย the architecture สำหรับ items ที่เก็บบน the stack. โดยปกตินี้ควรเป็น a 16-bit type บน 16-bit architectures และ a
        32-bit type บน 32-bit architectures, แม้ว่ามีบางข้อยกเว้น. ที่ใช้ภายในโดย FreeRTOS.
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 10:34:20 AM
Style Guide

     •  Indentation(เยื้อง)

        4 space characters ถูกใช้เพื่อเยื้อง.

     •  Comments

        Comments จะไม่ผ่าน column 80, เว้นแต่พวกมันตาม, และอธิบาย, a parameter.

        C++ style double slash (//) comments ไม่ถูกใช้.

     •  Layout

        The FreeRTOS source code lay out ถูกออกแบบให้ง่ายต่อการดูและอ่านให้มากที่สุดเท่าที่เป็นได้. The code snippets ข้างล่างแสดงอย่างแรก the file
        layout, จากนั้น the C code formatting.

        (ตัวอย่างโค้ดดูในลิ้งค์เอานะครับ)
        https://www.freertos.org/FreeRTOS-Coding-Standard-and-Style-Guide.html (https://www.freertos.org/FreeRTOS-Coding-Standard-and-Style-Guide.html)
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 01:43:20 PM
https://www.freertos.org/freertos/quality.html (https://www.freertos.org/freertos/quality.html)

Implementation Quality Management

FreeRTOS ได้รับการจัดการคุณภาพอย่างเข้มงวด ไม่ใช่แค่ใน software coding standards และดูและรู้สึก, แต่ยังรวมถึงการนำไปใช้งานด้วย ตัวอย่างเช่น:

•  FreeRTOS จะไม่ดำเนินการใด ๆ ที่ไม่ได้กำหนดไว้ อย่างเช่น การเดิน a linked list, จากภายใน  a critical section หรือ interrupt.

•  เราภูมิใจอย่างยิ่งกับ the efficient software timer implementation ที่ไม่ใช้ CPU time ใดๆเว้นแต่ a timer ที่จำเป็นต่อการบริการจริงๆ. Software timers ไม่
   บรรจุตัวแปรที่จำเป็นต้องถูกนับลงเป็นศูนย์.

•  ในทำนองเดียวกัน รายการของ tasks ที่ถูกบล็อก (รอดำเนินการ) ไม่จำเป็นต้องใช้เวลาในการให้บริการเป็นระยะ.
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 02:11:02 PM
•  ตรงไปยัง task notifications ยอมให้ fast task signalling, โดยแทบไม่ต้องเสีย RAM, และสามารถถูกใช้ในส่วนใหญ่ของ inter-task และอินเตอร์รัพท์ไปยัง task
    signalling scenarios.

•  The FreeRTOS queue usage model จัดการรวมความเรียบง่ายกับความหยืดหยุ่น (ในขนาดโค้ดที่เล็ก) - คุณลักษณะที่ปกติแล้วจะไม่เกิดร่วมกัน.

•  FreeRTOS queues เป็นพื้นฐานดั้งเดิมบนส่วนบนซึ่ง communication และ synchronisation primitives อื่นถูกสร้าง. The code re-use ทำให้ขนาดโค้ดโดย
    รวมลดลงอย่างมาก ซึ่งจะช่วยในการทดสอบและช่วยให้มั่นใจได้ถึงความทนทาน.

นอกจากนี้, the TÜV SÜD certified SIL 3 SafeRTOS real time kernel เดิมเกิดมาจาก FreeRTOS, และได้รับ the most stringent analysis และ test process - ซึ่งผลลัพธ์ดังกล่าวก็ถูกป้อนกลับเข้าไปใน the FreeRTOS code base (เมื่อความร่วมกันยังคงมีอยู่).
Title: Re: FreeRTOS
Post by: tha on October 11, 2021, 05:38:05 PM
https://www.freertos.org/differences-between-officially-supported-and-contributed-FreeRTOS-code.html (https://www.freertos.org/differences-between-officially-supported-and-contributed-FreeRTOS-code.html)

'Officially Supported' and 'Contributed' FreeRTOS Code ('รองรับอย่างเป็นทางการ' และ 'สนับสนุน' FreeRTOS Code)

แต่ละ supported architecture และ compiler combination ถูกพิจารณาว่าเป็น a separate FreeRTOS port.

A demo application เป็น a project ที่รัน a specific port บน a specific hardware platform.

The microcontroller architecture specific part ของ a FreeRTOS port ถูกเรียกว่า the port layer. แต่ละ port layer เป็นอย่างใดอย่างหนึ่ง 'officially supported' หรือ 'contributed'. หน้านี้อธิบายความแตกต่างระหว่างทั้งสอง.

Officially Supported Ports

พอร์ตที่รองรับอย่างเป็นทางการ:

     •  ถูกรวมใน the main FreeRTOS .zip file release (contributed ports ตอนนี้ได้ถูกย้ายไปยัง the FreeRTOS Interactive site สำหรับ FreeRTOS
         รุ่น 6.0.4 และใหม่กว่า).

     •  รวมอย่างน้อยหนึ่ง demo application ที่ถูกทำเป็นเอกสารบน the FreeRTOS.org site.

        A demo application เป็น a pre-configured project ที่มีเป้าหมายไปยัง a specific development board และใช้ a specific tool chain. มันมีจุดประสงค์
        เพื่อยอมให้ 'ทำจาก the box' การใช้แม้สำหรับผู้ใช้ใหม่, และรวมเอาหลายตัวอย่างของวิธีที่ the FreeRTOS API สามารถถูกใช้. บาง official ports รวมเอาหลาย
        demo projects เพื่อจัดให้ผู้ใช้มีตัวเลือกของทั้ง hardware และ build environments.

     •  มีต้นกำเนิดที่ทราบ ขจัดข้อสงสัยเกี่ยวกับความเป็นเจ้าของทรัพย์สินทางปัญญา  นี้จะยอมให้ commercial licenses และสัญญาที่รองรับถูก (เป็นทางเลือก) จ่ายโดย
         WITTENSTEIN high integrity systems ภายใต้ the OpenRTOS brand.

     •  ถูกเขียนและ/หรือตรวจสอบอย่างเต็มและทดสอบโดย Amazon Web Services Inc หรือพันธมิตรที่เชื่อถือได้อย่างสมบูรณ์

     •  โดยทั่วไปและหากเป็นไปได้ จะได้รับการบำรุงรักษาและอัปเดตเมื่อ new versions of the core FreeRTOS source code หรือ new versions of the
         relevant build tools ถูกเผยแพร่.

     •  สามารถถูกสนับสนุนอย่างปกติบนการเข้าถึงได้ฟรีและมอนิเตอร์ support forum.

Contributed Ports

พอร์ตที่สนับสนุน:

     •  ถูกจัดให้มีโดย FreeRTOS community users, มากกว่า Amazon Web Services โดยตรง

     •  ถูกทำให้ใช้เป็นประโยชน์ได้ฟรีสำหรับดาวน์โหลดจาก the FreeRTOS Interactive site.

     •  สามารถถูกจัดให้มีเฉพาะภายใต้ the standard open source FreeRTOS license. Commercial licenses ไม่ถูกเสนอสำหรับ contributed code.

     •  ถูกทำเป็นเอกสารโดย the contributors เอง. ปริมาณและคุณภาพของ documentation ที่จัดให้มี ดังนั้นจะแตกต่างกันระหว่าง contributed packages.

     •  บ่อยครั้งไม่สามารถถูกสนับสนุนโดยตรงจาก Amazon Web Services.
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 06:26:54 AM
https://www.freertos.org/features.html (https://www.freertos.org/features.html)

(https://i.imgur.com/TO6tbDp.png)
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 06:46:42 AM
Tasks and Co-routines

ดู the How FreeRTOS Works section สำหรับการแนะนำถึง basic multitasking concepts.

The Tasks and Co-Routine documentation pages จัดให้มี information ที่ยอมให้การตัดสินว่าเมื่อใด co-routine ใช้อาจเหมาะสมและอาจไม่เหมาะสม. ข้างล่างเป็นบทสรุปสั้นๆ. โปรดทราบว่า an application สามารถถูกออกแบบโดยใช้เพียง tasks, เพียง co-routines, หรือการผสมของทั้งสอง - อย่างไรก้ตาม tasks และ co-routines ใช้ API functions ที่แตกต่างกันและดังนั้น a queue (หรือ semaphore) ไม่สามารถถูกใช้เพื่อผ่าน data จาก a task ไปยัง a co-routine หรือในทางกลับกัน.

Co-routines ถูกมุ่งหมายจริงๆเพียงสำหรับใช้บน very small processors ที่มี severe RAM constraints
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 07:28:38 AM
Characteristics of a 'Task'

โดยสังเขป: A real time application ที่ใช้ an RTOS สามารถถูกทำเป็นโครงสร้างเป็น a set of independent tasks. แต่ละ task ปฏิบัติอยู่ภายในบริบทของมันเองที่บังเอิญไม่ขึ้นอยู่กับ tasks อื่นภายใน the system หรือ the RTOS scheduler ของมันเอง. มีเพียงหนึ่ง task เท่านั้นภายใน the application สามารถปฏิบัติที่จุดใดๆของเวลาและ the real time RTOS scheduler จะรับผิดชอบต่อการตัดสินใจว่า task นี้ควรเป็นงานใด. The RTOS scheduler อาจดังนั้นซ้ำๆเริ่มและหยุดแต่ละ task (สลับแต่ละ task เข้าและออก) ขณะที่ the application ดำเนินการ. เนื่องจาก a task ไม่มีความรู้เกี่ยวกับ the RTOS scheduler activity มันเป็นคความรับผิดชอบของ the real time RTOS scheduler เพื่อให้แน่ใจว่า the processor context (register values, stack contents, etc) เมื่อ a task ถูกสลับเข้าเป็นจริงได้ว่าเนื่องจาก the same task เคยสลับออกแล้ว.  เพื่อบรรลุเป้าหมายนี้แต่ละ task ถูกจัดให้มี stack ของมันเอง. เมื่อ the task ถูกสลับออก the execution context ถูกเซฟไปยัง the stack ของ task นั้นดังนั้นมันสามารถถูกเรียกคืนกลับจริงๆอีกด้วยเมื่อ the same task ต่อมาถูกสลับกลับเป็นเข้าอีก. ดู the How FreeRTOS Works section สำหรับข้อมูลเพิ่มเติม.

(https://i.imgur.com/CZa5WIQ.png)
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 08:45:59 AM
Characteristics of a 'Co-routine'

หมายเหตุ: Co-routines ถูกจัดให้มีใช้สำหรับใช้กับ very small devices, แต่ถูกไม่ค่อยได้ใช้มากแล้วใน the field ในปัจจุบันนี้. ด้วยเหตุผลนั้น, ในขณะที่ไม่มีแผนที่จะถอด co-routines จาก the code, ยังไม่มีแผนการพัฒนาพวกมันต่อไปอีกด้วย.

Co-routines มีแนวคิดคล้ายกันกับ tasks แต่มีความแตกต่างพื้นฐานดังต่อไปนี้ (อธิบายเพิ่มเติมใน the co-routine documentation page):

   1. Stack usage

      ทุก the co-routines ภายใน an application แชร์ a single stack. นี้ลดลงอย่างมาก the amount of RAM ที่ต้องการเมื่อเทียบกับ a similar application ที่
      เขียนโดยใช้ tasks.

   2. Scheduling and priorities

      Co-routines ใช้การจัดกำหนดการแบบร่วมมือที่จัดลำดับความสำคัญโดยคำนึงถึง co-routines อื่น, แต่สามารถถูกรวมไปใน an application ที่ใช้ preemptive
      tasks.

   3. Macro implementation

      The co-routine implementation ถูกจัดให้มีผ่านทาง a set of macros.

   4. Restrictions on use

      การใช้ RAM ที่ลดลงต้องแลกมาด้วยข้อจำกัดที่เข้มงวดบางประการในวิธีที่ co-routines สามารถถูกจัดโครงสร้าง

(https://i.imgur.com/sdLu1xR.png)
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 09:47:17 AM
https://www.freertos.org/RTOS-task-states.html (https://www.freertos.org/RTOS-task-states.html)

Tasks

The FreeRTOS Tutorial Books ให้ข้อมูลรายละเอียดเพิ่มเติมเกี่ยวกับ tasks และพฤติกรรมของมัน.

Task States

A task สามารถอยู่ในหนึ่งของสถานะต่อไปนี้:

   •  Running

       เมื่อ a task กำลังดำเนินการจริงๆมันถูกพูดได้ว่าอยู่ใน the Running state. ขณะนี้กำลังใช้ the processor. ถ้า the processor ซึ่ง the RTOS กำลังรันมีเพียง a
       single core ดังนั้นสามารถมีเพียง one task ใน the Running state ในแต่ละครั้ง.

   •  Ready

      Ready tasks คืองานที่สามารถดำเนินการได้ (พวกมันไม่อยู่ใน the Blocked หรือ Suspended state) แต่ไม่ได้กำลังปฏิบัติจริงๆเพราะว่า task อื่นที่มีลำดับความ
      สำคัญเท่ากันหรือสูงกว่าอยู่ใน the Running state อยู่.

   •  Blocked

      A task ถูกพูดได้ว่าอยู่ใน the Blocked state ถ้ามันปัจจุบันกำลังคอยสำหรับอย่างใดอย่างหนึ่ง a temporal หรือ external event. ตัวอย่างเช่น, ถ้า a task เรียก
      ใช้ vTaskDelay() มันจะบล็อก (ถูกวางลงใน the Blocked state) จนกระทั่ง the delay period หมดเวลาลง - a temporal event. Tasks ยังถูกบล็อกอีกด้วย
      เพื่อคอยสำหรับ queue, semaphore, event group, notification หรือ semaphore event. Tasks ใน the Blocked state โดยปกติมี a 'timeout'
      period, หลังจากนั้น the task จะถูกหมดเวลาและจะถูกปลดบล็อก, แม้ว่า the event ที่ the task เคยกำลังคอยอยู่ไม่ได้เกิดขึ้นก็ตาม.

      Tasks ใน the Blocked state ไม่ใช้ any processing time และไม่สามารถถูกเลือกให้เข้าสู่ the Running state.

   •  Suspended

      คล้าย tasks ที่อยู่ใน the Blocked state, tasks ใน the Suspended state ไม่สามารถถูกเลือกให้เข้าสู่ the Running state, แต่ tasks ใน the Suspended
      state ไม่มี a time out(หมดเวลา). แทนด้วย, tasks เข้าสู่หรือออกจาก the Suspended state ได้เพียงเมื่อคำสั่งอย่างชัดเจนให้ทำนั้นผ่านทาง the
      vTaskSuspend() และ xTaskResume() API calls ตามลำดับ.

      (https://i.imgur.com/8owRFda.png)
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 11:03:06 AM
https://www.freertos.org/RTOS-task-priority.html (https://www.freertos.org/RTOS-task-priority.html)

Tasks

The FreeRTOS Tutorial Books ให้ข้อมูลรายละเอียดเพิ่มเติมเกี่ยวกับ tasks และพฤติกรรมของมัน.

Task Priorities

แต่ละ task ถูกระบุ a priority จาก 0 ถึง ( configMAX_PRIORITIES - 1 ), ที่ configMAX_PRIORITIES ถูกกำหนดภายใน FreeRTOSConfig.h.

ถ้า the port ที่ใช้งาน a port optimised task selection mechanism ที่ใช้ a 'count leading zeros' (นับนำหน้าศูนย์) type instruction (สำหรับ task selection ใน a single instruction) และ configUSE_PORT_OPTIMISED_TASK_SELECTION ถูกเซ็ตเป็น 1 in FreeRTOSConfig.h, ดังนั้น configMAX_PRIORITIES ไม่สามารถสูงกว่า 32. ในกรณีอื่นทั้งหมด configMAX_PRIORITIES สามารถเป้นค่าใดๆภายในเหตุผล - แต่สำหรับเหตุผลของการใช้ RAM อย่างมีประสิทธิภาพควรมีค่าต่ำสุดที่จำเป็นจริงๆ.

Low priority numbers หมายถึง low priority tasks. The idle task มี priority zero (tskIDLE_PRIORITY).

The FreeRTOS scheduler ช่วยให้มั่นใจได้ว่า tasks ใน the Ready หรือ Running state จะถูกให้ processor (CPU) time เสมอมากกว่า tasks of a lower priority ที่อยู่ใน the ready state ด้วย. กล่าวอีกนัยหนึ่ง, the task ที่วางลงใน the Running state เป็น the highest priority task เสมอที่สามารถรันได้.

จำนวน tasks เท่าใดก็ได้สามารถแชร์ the same priority. ถ้า configUSE_TIME_SLICING ไม่ถูกกำหนด, หรือถ้า configUSE_TIME_SLICING ถูกเซ็ตเป็น 1, ดังนั้น Ready state tasks ของ equal priority จะแชร์ the available processing time โดยใช้ a time sliced round robin scheduling scheme.
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 11:29:31 AM
https://www.freertos.org/implementing-a-FreeRTOS-task.html (https://www.freertos.org/implementing-a-FreeRTOS-task.html)

(https://i.imgur.com/lU1Z1m5.png)

The type TaskFunction_t ถูกกำหนดเป็น a function ที่รีเทิร์น void และเอา a void pointer เป็น parameter เฉพาะของมัน. ทุก functions ที่จัดให้มีใช้ a task ควรเป็นชนิดนี้. The parameter สามารถถูกใช้ส่งผ่าน information ของ type ใดๆลงใน the task - นี้ถูกแสดงให้เห็นโดยหลาย the standard demo application tasks.

Task functions ไม่ควรรีเทิร์นดังนั้นโดยปกติถูกจัดให้มีใช้เป็น a continuous loop. อีกครั้ง, ดู the RTOS demo application สำหรับตัวอย่างมากมาย.

Tasks ถูกสร้างโดยการเรียกใช้ xTaskCreate() หรือ xTaskCreateStatic(), และลบโดยการเรียกใช้ vTaskDelete().
Title: Re: FreeRTOS
Post by: tha on October 12, 2021, 11:51:20 AM
Task Creation Macros

Task functions สามารถเลือกกำหนดได้โดยใช้ the portTASK_FUNCTION และ portTASK_FUNCTION_PROTO macros. มาโครเหล่านี้ถูกจัดให้มีเพื่อยอมให้ compiler specific syntax ถูกเพิ่มไปยัง the function definition และ prototype ตามลำดับ. ไม่จำเป็นต้องใช้เว้นแต่จะระบุไว้โดยเฉพาะในเอกสารประกอบสำหรับพอร์ตที่ใช้ (ปัจจุบันมีเพียงพอร์ต PIC18 fedC).

The prototype สำหรับ the function แสดงข้างบนสามารถถูกเขียนเป็น:

(https://i.imgur.com/SrPfkjr.png)
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 05:50:20 AM
https://www.freertos.org/co-routine-states.html (https://www.freertos.org/co-routine-states.html)

Co-routines

Co-Routine States

Co-routines ถูกมุ่งหมายเพียงสำหรับใช้กับ very small processors ที่มี severe RAM constraints, และโดยปกติไม่ถูกใช้กับ 32-bit microcontrollers.

A co-routine สามารถดำรงอยุ่ในหนึ่งของสถานะต่อไปนี้:

     •  Running

         เมื่อ a co-routine กำลังดำเนินการจริงๆมันถูกพูดได้ว่าอยู่ใน the Running state. ขณะนี้กำลังใช้ the processor.

     •  Ready

        Ready co-routines คือรูทีนที่สามารถดำเนินการได้ (พวกมันไม่ถูกบล็อก) แต่ขณะนี้ไม่ได้กำลังดำเนินการ. A co-routine อาจอยู่ใน the Ready state เพราะว่า:

           1. co-routine อื่นของ priority ที่เท่ากันหรือสูงกว่าได้อยู่ใน the Running state แล้ว, หรือ
           2. A task อยู่ใน the Running state - นี้สามารถเป็นกรณีเฉพาะถ้า the application ใช้ทั้ง tasks และ co-routines.

     •  Blocked

         A co-routine ถูกพูดได้ว่าอยู่ใน the Blocked state ถ้ามันขณะนี้กำลังคอยสำหรับอย่างใดอย่างหนึ่ง a temporal หรือ external event. ตัวอย่างเช่น, ถ้า a
         co-routine เรียกใช้ crDELAY() มันจะบล็อก (ถูกวางลงใน the Blocked state) จนกระทั่ง the delay period หมดเวลาลง - a temporal event. Blocked
         co-routines ไม่มีให้ใช้ประโยชน์สำหรับ scheduling.

ขณะนี้ไม่มี co-routine ที่เทียบเท่ากับ a tasks Suspended state.

(https://i.imgur.com/WbbBiW3.png)
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 06:05:31 AM
https://www.freertos.org/writing-a-FreeRTOS-co-routine.html (https://www.freertos.org/writing-a-FreeRTOS-co-routine.html)

(https://i.imgur.com/rSFIb3w.png)

The type crCOROUTINE_CODE ถูกกำหนดเป็น a function ที่รีเทิร์น void และเอา an CoRoutineHandle_t และ an index เป็น parameters ของมัน. ทุก functions ที่จัดให้มีใช้ a co-routine ควรเป็นชนิดนี้ (ที่แสดงไว้ข้างบน).

Co-routines ถูกสร้างโดยการเรียกใช้ xCoRoutineCreate().

Points to note:(ข้อสังเกต:)

     •  ทุก co-routine functions ต้องสตาร์ทด้วยการเรียกไปยัง crSTART().
     •  ทุก co-routine functions ต้องจบด้วยการเรียกไปยัง crEND().
     •  Co-routine functions ไม่ควรรีเทิร์นดังนั้นถูกจัดให้มีใช้โดยทั่วไปเป็น a continuous loop.
     •  co-routines มากมายสามารถถูกสร้างจาก a single co-routine function. The uxIndex parameter ถูกจัดให้มีเป็นวิธีการที่แยกความแตกต่างระหว่าง co-
        routines ดังกล่าว.
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 07:12:23 AM
https://www.freertos.org/co-routine-priorities.html (https://www.freertos.org/co-routine-priorities.html)

Co-routines

Co-Routine Priorities

แต่ละ co-routine ถูกระบุ a priority จาก 0 ถึง ( configMAX_CO_ROUTINE_PRIORITIES - 1 ). configMAX_CO_ROUTINE_PRIORITIES ถูกกำหนดภายใน FreeRTOSConfig.h และสามารถถูกเซ็ตบน an application โดย application basis.

Low priority numbers หมายถึง low priority co-routines.

Co-routine priorities เฉพาะที่คำนึงถึง co-routines อื่นๆ. Tasks จะได้รับ priority เหนือ co-routines เสมอหากคุณผสม tasks และ co-routines ภายใน the same application.
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 07:30:33 AM
https://www.freertos.org/scheduling-coroutines-with-tasks.html (https://www.freertos.org/scheduling-coroutines-with-tasks.html)

Co-routines

Scheduling Co-Routines

Co-routines ถูกจัดวางกำหนดโดยการเรียกซ้ำไปยัง vCoRoutineSchedule(). สถานที่ที่ดีที่สุดเพื่อเรียก vCoRoutineSchedule() คือจาก the idle task hook. กรณีนี้แม้ว่าแอปพลิเคชันของคุณจะใช้เฉพาะ co-routines เนื่องจาก the idle task จะยังคงถูกสร้างโดยอัตโนมัติเมื่อ the scheduler ถูกสตาร์ท. ดูตัวอย่างในภายหลัง.

Mixing Tasks and Co-Routines

จัดวางกำหนด co-routines จากภายใน the idle task ยอมให้ tasks และ co-routines ผสมกันง่ายภายใน the same application. เมื่อสิ่งนี้ถูกทำ the co-routines จะปฏิบัติเฉพาะเมื่อไม่มี tasks ที่มี priority สูงกว่า the idle task ที่สามารถปฏิบัติ. ดูตัวอย่างในภายหลัง.
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 09:07:32 AM
https://www.freertos.org/co-routine-limitations.html (https://www.freertos.org/co-routine-limitations.html)

Co-routines

Limitations and Restrictions

ประโยชน์ของ a co-routines การใช้ RAM ลดลงเมื่อเทียบกับ an equivalent task ต้องแลกมาด้วยข้อจำกัดบางประการเกี่ยวกับวิธีที่ a co-routine สามารถถูกใช้. Co-routines มีข้อจำกัดและซับซ้อนในการใช้มากกว่า tasks.

    •  Sharing a stack

       The stack ของ a co-routine ไม่ถูกรักษาไว้เมื่อ a co-routine บล็อก. ซึ่งหมายความว่าตัวแปรที่จัดสรรบน the stack มักจะสูญเสียค่าของมันไป. ในการเอาชนะ
       สิ่งนี้ ตัวแปรที่ต้องรักษาค่าของมันไว้ตลอดa blocking call จะต้องถูกประกาศให้เป็นแบบ static. ตัวอย่างเช่น:

        (https://i.imgur.com/QOobNJw.png)

       ผลที่ตามมาอีกประการของการแชร์ a stack คือว่าการเรียกใช้ฟังก์ชัน API ที่อาจเป็นเหตุให้ co-routine บล็อกสามารถทำได้จากฟังก์ชัน co-routine เองเท่านั้น -
       ไม่ใช่จากภายในฟังก์ชันที่เรียกโดย co-routine ตัวอย่างเช่น:

       (https://i.imgur.com/QdmOzaU.png)

     •  Use of switch statements

        The default co-routine implementation ที่รวมอยู่ใน the FreeRTOS download ไม่ยอมให้ a blocking call ถูกทำจากภายใน a switch statement.
        ตัวอย่างเช่น:

        (https://i.imgur.com/IQslBAM.png)
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 10:40:16 AM
https://www.freertos.org/coroutine-example-code.html (https://www.freertos.org/coroutine-example-code.html)

Co-routines

Quick Co-routine Example

ตัวอย่างสั้นๆ นี้แสดงให้เห็นถึงการใช้ co-routines.

     1. Creating a simple co-routine to flash an LED

         โค้ดต่อไปนี้กำหนด co-routine ที่ง่ายมากซึ่งไม่ทำอะไรเลยนอกจากจะกะพริบไฟ LED เป็นระยะ

         (https://i.imgur.com/g8D48YH.png)
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 10:56:21 AM
      2. Scheduling the co-routine

         Co-routines ถูกจัดวางกำหนดโดยการเรียกซ้ำไปยัง vCoRoutineSchedule(). สถานที่ที่ดีที่สุดเพื่อทำนี้คือเรียกจากภายใน the idle task โดยการเขียน an
         idle task hook. อย่างแรกต้องแน่ใจว่า configUSE_IDLE_HOOK ถูกเซ็ตเป็น 1 ภายใน FreeRTOSConfig.h. จากนั้นเขียน the idle task hook ดังนี้:

        void vApplicationIdleHook( void )
            {
                  vCoRoutineSchedule( void );
            }

       อีกทางหนึ่ง, ถ้า the idle task ไม่กำลังดำเนินการฟังชั่นอื่นใดๆมันจะมีประสิทธิภาพมากขึ้นถ้าเรียก vCoRoutineSchedule() จากภายใน a loop ดังนี้ :

       void vApplicationIdleHook( void )
          {
            for( ;; )
              {
                 vCoRoutineSchedule( void );
              }
          }
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 11:07:12 AM
     (https://i.imgur.com/aCqH8QT.png)
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 11:32:00 AM
     4. Extending the example: Using the index parameter

         ตอนนี้สมมติว่าเราต้องการสร้าง 8 co-routines ดังกล่าวจาก the same function. แต่ละ co-routine จะกระพริบ a LED ที่ต่างกันในอัตราที่ต่างกัน. The
         index parameter สามารถถูกใช้เพื่อแยกความแตกต่างระหว่าง the co-routines จากภายใน the co-routine function ของมันเอง.

         คราวนี้เราจะสร้าง 8 co-routines และส่งผ่าน index ที่ต่างกันไปยังแต่ละ co-routine.

         (https://i.imgur.com/ay5p7iR.png)

         The co-routine function ถูกขยายออกไปอีกด้วยดังนั้นใช้ LED ที่ต่างกันและต่างอัตตราการกระพริบ.

         (https://i.imgur.com/yhQ1Ung.png)
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 02:05:46 PM
https://www.freertos.org/croutine-standard-demo-examples.html (https://www.freertos.org/croutine-standard-demo-examples.html)

Co-routines

FreeRTOS Demo Application Examples

Two files ถูกรวมไว้ใน the download ที่แสดงการใช้ co-routines ที่มี queues:

     1. crflash.c

         นี้เทียบเท่ากับฟังชั่นของ the standard demo file flash.c แต่ใช้ co-routines แทน tasks. นอกจากนี้, และเพียงเพื่อจุดประสงค์ในการสาธิต, แทนที่จะสลับ
         LED โดยตรงจากภายใน a co-routine (ตาม the quick example ข้างบน) จำนวนของ the LED ที่ควรถูกสลับถูกส่งผ่านบน a queue ไปยัง a higher
         priority co-routine.

     2. crhook.c

         สาธิตการส่งผ่าน data จาก a interrupt ไปยัง a co-routine. A tick hook function ถูกใช้เป็น the data source.
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 02:20:30 PM
The PC และหนึ่งของ the older ARM Cortex-M3 demo applications ถูกกำหนดไว้ก่อนแล้วเพื่อใช้ sample co-routine files เหล่านี้และสามารถถูกใช้เป็นตัวอ้างอิง. ทุก the other demo applications ถูกกำหนดเพื่อใช้ tasks เท่านั้น, แต่สามารถถูกแปลงง่ายๆเพื่อสาธิต co-routines โดยทำตามขั้นตอนข้างล่าง. นี้แทนที่ the functionality ที่จัดให้มีใช้ภายใน flash.c ด้วยที่จัดให้มีใช้ด้วย crflash.c:

     1. ใน FreeRTOSConfig.h เซ็ต configUSE_CO_ROUTINES และ configUSE_IDLE_HOOK เป็น 1.

     2. ใน the IDE project หรือ project makefile (ขึ้นอยู่กับ the demo project ที่กำลังถูกใช้):

          i. แทนที่ the reference ไปยังไฟล์ FreeRTOS/Demo/Common/Minimal/flash.c ด้วย FreeRTOS/Demo/Common/Minimal/crflash.c.
          ii. เพิ่ม the file FreeRTOS/Source/croutine.c ไปยัง the build.
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 02:40:24 PM
     3. ใน main.c:

         i. Include the header file croutine.h ซึ่งบรรจุ the co-routine macros และ function prototypes.
         ii. แทนที่การ include ของ flash.h ด้วย crflash.h.
         iii. เอาออก the call ไปยัง the function ที่สร้าง the flash tasks vStartLEDFlashTasks() ....
         iv. ... และแทนที่มันด้วย the function ที่สร้าง the flash co-routines vStartFlashCoRoutines( n ), โดยที่ n เป็นจำนวนของ co-routines ที่ควรถูก
             สร้าง. แต่ละ co-routine จะกระพริบ a different LED ที่ a different rate.
         v. เพิ่มฟังก์ชัน idle hook ที่จัดกำหนดเวลา co-routines เป็น:

              void vApplicationIdleHook( void )
                {
                    vCoRoutineSchedule( void );
                 }

   
             ถ้า main() บรรจุ an idle hook อยู่แล้วดังนั้นเพิ่มการเรียกง่ายๆถึง vCoRoutineSchedule() ไปยัง the existing hook function.
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 02:54:26 PM
     4. แทนที่ the flash tasks ด้วย the flash co-routines หมายความว่ามีอย่างน้อยสอง stacks ที่จำเป็นต้องจัดสรรน้อยลงและ heap space น้อยลงดังนั้นสามารถถูก
         เซ็ตไปด้านหนึ่งสำหรับใช้โดย the RTOS scheduler. ถ้า your project มี  RAM ไม่เพียงพอเพื่อ include croutine.c ใน the build ดังนั้นลดง่ายๆ the
         definition ของ portTOTAL_HEAP_SPACE โดย ( 2 * portMINIMAL_STACK_SIZE ) ภายใน FreeRTOSConfig.h.
Title: Re: FreeRTOS
Post by: tha on October 13, 2021, 06:09:41 PM
https://www.freertos.org/Embedded-RTOS-Queues.html (https://www.freertos.org/Embedded-RTOS-Queues.html)

FreeRTOS Queues
[Inter-task communication and synchronisation]

FreeRTOS Queues

[See also Blocking on Multiple RTOS Objects]

Queues เป็นรูปแบบหลักของการสื่อสารระหว่างกัน. พวกมันสามารถถูกใช้ส่ง messages ระหว่างn tasks, และระหว่าง interrupts และ tasks.  ในกรณีส่วนใหญ่พวกมันถูกใช้เป็น thread safe FIFO (First In First Out) buffers โดย new data จะถูกส่งไปยังด้านหลังของ the queue, แม้ว่า data จะถูกส่งไปยังด้านหน้าได้เช่นกัน.

          (https://i.imgur.com/sK3K0gH.png)
               (ภาพดูในลิ้งค์เอานะ จะเป็นภาพเคลื่อนไหว)

               การเขียนถึงและการอ่านจาก a queue. ในตัวอย่างนี้ the queue ถูกสร้างเพื่อถือ 5 items, และ the queue ไม่มีวันเต็ม.

User Model: Maximum Simplicity, Maximum Flexibility . . .

The FreeRTOS queue usage model จัดการรวมความเรียบง่ายเข้ากับความหยืดหยุ่น - คุณลักษณะที่ปกติแล้วจะไม่เกิดร่วมกัน. Messages ถูกส่งผ่านทาง queues โดยการคัดลอก, หมายความว่า the data (ซึ่งสามารถเป็น a pointer ไปยัง buffers ที่ใหญ่กว่า) ถูกคัดลอกตัวเองลงใน the queue ค่อนข้างมากกว่า the queue เก็บเพียง a reference ไปยัง the data เสมอ. นี่เป็นแนวทางที่ดีที่สุดเพราะ:

     •  Small messages ที่ถูกบรรจุใน C variables แล้ว (integers, small structures, ฯลฯ) สามารถถูกส่งลงใน a queue โดยตรง. ไม่มีความจำเป็นต้องจัดสรร a
         buffer สำหรับ the message และจากนั้นคัดลอก the variable ลงใน the allocated buffer. ในทำนองเดียวกัน, messages สามารถถูกอ่านจาก queues
         โดยตรงลงใน C variables.

        นอกจากนี้ การส่งไปยัง a queue ในลักษณะนี้ยอมให้ the sending task เขียนทับ the variable หรือ buffer ที่ถูกส่งไปยัง the queue แล้วทันที , แม้ว่า the
        sent message ยังคงอยู่ใน the queue. เพราะว่า the data ที่บรรจุใน the variable ถูกคัดลอกลงใน the queue แล้ว the variable itself มีให้ใช้ประโยชน์
        สำหรับ re-use. ไม่มีความต้องการสำหรับ the task ที่ส่ง the message และ the task ที่รับ the message เพื่อตกลงว่า task ใดเป็นเจ้าของ the message,
        และ task ใดมีหน้าที่รับผิดชอบในการปล่อยเป็นอิสระ the message เมื่อมันไม่ถูกต้องการอีกต่อไป

     •  การใช้ queues ที่ส่งผ่าน data โดยการคัดลอกไม่ได้ป้องกัน queues จากการถูกใช้ส่งผ่าน data โดย reference. เมื่อขนาดของ a message ถึงจุดที่มันไม่
         สามารถคัดลอก the entire message ลงใน the queue byte for byte, กำหนด the queue เพื่อถือ pointers และคัดลอกเพียง a pointer to the
         message ลงใน the queue แทน. นี่เป็นวิธีจริงๆที่ the FreeRTOS+UDP implementation ส่งผ่าน large network buffers รอบ the FreeRTOS IP
         stack.
Title: Re: FreeRTOS
Post by: tha on October 14, 2021, 08:21:29 AM
     •  The kernel รับผิดชอบอย่างเต็มที่ในการจัดสรร the memory ที่ใช้เป็น the queue storage area.

     •  Variable sized messages สามารถถูกส่งโดยการกำหนด queues เพื่อถือ structures ที่บรรจุ a member ที่ชี้ไปยัง the queued message, และ member
         อื่นที่ถือขนาดของ the queued message.

     •  A single queue สามารถถูกใช้รับ different message types, และ messages จากหลาย locations, โดยการกำหนด the queue เพื่อถือ a structure ที่มี a
         member ที่ถือ the message type, และ member อื่นที่ถือ the message data (หรือ a pointer ไปที่ the message data). วิธีที่ the data ถูกตีความขึ้น
         อยู่กับ the message type. นี่เป็นวิธีจริงๆที่ the task ที่จัดการ the FreeRTOS+UDP IP stack ให้สามารถใช้ a single queue รับ notifications of ARP
         timer events, packets กำลังถูกรับจาก the Ethernet hardware, packets กำลังถูกรับจาก the application, network down events, ฯลฯ.
Title: Re: FreeRTOS
Post by: tha on October 14, 2021, 08:50:37 AM
     •  The implementation เหมาะอย่างยิ่งสำหรับใช้ใน a memory protected environment. A task ที่ถูกจำกัดให้อยู่ใน a protected memory area สามารถส่ง
         ผ่าน data ไปยัง a task ที่ถูกจำกัดให้อยู่ใน a different protected memory area เพาระว่าการปลุก the RTOS โดยการเรียก the queue send function จะ
         ยกขึ้น the microcontroller privilege level. The queue storage area ถูกเข้าถึงได้เฉพาะโดย the RTOS (ที่มีสิทธิ์เต็ม).
Title: Re: FreeRTOS
Post by: tha on October 14, 2021, 01:13:39 PM
     •  A separate(ที่แยกต่างหาก) API ถูกจัดให้มีสำหรับในภายในของ an interrupt. การแยก the API ที่ใช้จาก an RTOS task จากที่ใช้จาก an interrupt
         service routine หมายความว่า the implementation of the RTOS API functions ไม่เสียค่าใช้จ่ายของการตรวจเช็ค call context ของพวกมันแต่ละครั้งที่
         พวกมันปฏิบัติ. การใช้ a separate interrupt API ยังหมายถึง, ในกรณีส่วนใหญ่, การสร้าง RTOS aware interrupt service routines ง่ายสำหรับผู้ใช้ปลาย
         ทางกว่าเมื่อเทียบกับ RTOS products ทางเลือกอื่น.

     •  API นั้นง่ายกว่าในทุก ๆ ด้าน

The FreeRTOS tutorial book จัดให้มี additional information เกี่ยวกับ queues, binary semaphores, mutexes, counting semaphores และ recursive semaphores, พร้อมด้วย simple worked examples ใน a set of accompanying example projects.
Title: Re: FreeRTOS
Post by: tha on October 14, 2021, 02:04:31 PM
Blocking on Queues

Queue API functions ยอมให้ a block time ถูกระบุ.

เมื่อ a task พยายามอ่านจาก an empty queue the task จะถูกวางลงใน the Blocked state (ดังนั้นมันไม่กิน any CPU time และ tasks อื่นสามารถ run) จนกระทั่งอย่างใดอย่างหนึ่ง data กลายมาเป็นมีให้ใช้ประโยชน์ได้บน the queue, หรือ the block time หมดเวลาลง.

เมื่อ a task พยายามเขียนถึง a full queue the task  จะถูกวางลงใน the Blocked state (ดังนั้นมันไม่กิน any CPU time และ tasks อื่นสามารถ run) จนกระทั่งอย่างใดอย่างหนึ่ง space กลายมาเป็นมีให้ใช้ประโยชน์ได้ใน the queue, หรือ the block time หมดเวลาลง.

ถ้ามีมากกว่าหนึ่ง task ถูกบล็อกบน the same queue ดังนั้น the task ที่มี the highest priorityจะเป็น the task ที่ถูก unblocked ก่อน.

ดู the Queue Management section of the user documentation สำหรับรายการของ queue related API functions. ค้นหา the files ใน the FreeRTOS/Demo/Common/Minimal directory จะแสดงหลายตัวอย่างการใช้งานของพวกมัน

โปรดทราบว่า interrupts ต้องไม่ใช้ API functions ที่ไม่สิ้นสุดใน "FromISR".
Title: Re: FreeRTOS
Post by: tha on October 15, 2021, 06:39:37 AM
https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html (https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html)

FreeRTOS Binary Semaphores
[Inter-task communication and synchronisation]

[See also Blocking on Multiple RTOS Objects]

The FreeRTOS tutorial book จัดให้มี additional information เกี่ยวกับ queues, binary semaphores, mutexes, counting semaphores และ recursive semaphores, พร้อมด้วย simple worked examples ใน a set of accompanying example projects.

FreeRTOS Binary Semaphores

               TIP: 'Task Notifications' สามารถจัดให้มีทางเลือกที่มีน้ำหนักเบาไปยัง binary semaphores ในหลาย ๆ สถานการณ์

Binary semaphores ถูกใช้สำหรับวัตถุประสงค์ทั้ง mutual exclusion (กันไว้สำหรับคนพิเศษ ใช่ไหม?) และ synchronisation.

Binary semaphores and mutexes มีความคล้ายคลึงกันมาก แต่มีความแตกต่างเล็กน้อย: Mutexes รวมเอา a priority inheritance mechanism, binary semaphores ไม่. สิ่งนี้ทำให้ binary semaphores เป็นตัวเลือกที่ดีกว่าในการจัดให้มีใช้ synchronisation (ระหว่าง tasks หรือระหว่าง tasks และ an interrupt), และ mutexes เป็นตัวเลือกที่ดีกว่าในการจัดให้มีใช้ simple mutual exclusion. คำอธิบายของวิธีที่ a mutex สามารถถูกใช้เป็น a mutual exclusion mechanism ถือเท่าๆกันสำหรับ binary semaphores. ส่วนย่อยนี้จะอธิบายเฉพาะการใช้ binary semaphores สำหรับ synchronisation.

Semaphore API functions ยอมให้ a block time ถูกระบุ. The block time แสดงจำนวนสูงสุดของ 'ticks' ที่ a task ควรเข้าสู่ the Blocked state เมื่อพยายามให้ 'ได้' a semaphore, หาก the semaphore ไม่มีให้ใช้ประโยชน์โดยทันที. ถ้ามีมากกว่าหนึ่ง task ถูกบล็อกบน the same semaphore ดังนั้น the task ที่มี the highest priority จะเป็น the task ที่ถูก unblocked ในครั้งถัดไปที่ the semaphore กลายมาเป็นมีให้ใช้ประโยชน์.
Title: Re: FreeRTOS
Post by: tha on October 16, 2021, 06:57:33 AM
คิดว่า a binary semaphore เป็น a queue ที่สามารถถือหนึ่ง item เท่านั้น. The queue ดังนั้นสามารถว่างเปล่าหรือเติมเท่านั้น(จึงเป็น binary). Tasks และ interrupts ที่ใช้ the queue ไม่สนใจว่าอะไรที่ the queue ถือ - พวกมันต้องการทราบเพียงว่า the queue ว่างเปล่าหรือเติม. กลไกนี้สามารถถูกหาประโยชน์จากการซิงโครไนซ์  (ตัวอย่างเช่น) a task ด้วย an interrupt.

พิจารณากรณีที่ a task ถูกใช้เพื่อบริการ a peripheral. การวนตรวจ the peripheral จะสิ้นเปลืองทรัพยากรของ CPU , และป้องกันไม่ให้ tasks อื่นๆทำงาน. ดังนั้นจึงควรที่ the task จะใช้เวลาส่วนใหญ่ใน the Blocked state ดีกว่า (ยอมให้ tasks อื่นๆทำงาน) และดำเนินการเองเฉพาะเมื่อมีบางอย่างที่ต้องทำจริงๆ เท่านั้น. สิ่งนี้ถูกทำให้สำเร็จโดยใช้ a binary semaphore โดยที่ the task Block ขณะที่พยายามให้ 'ได้' the semaphore. An interrupt routine ดังนั้นถูกเขียนขึ้นสำหรับ the peripheral ที่เพียง 'ให้' the semaphore เมื่อ the peripheral ต้องการการบริการ  The task 'ได้' the semaphore เสมอ (อ่านจาก the queue ทำให้ the queue ว่างเปล่า), แต่ไม่เคย 'ให้' มัน. The interrupt 'ให้' the semaphore เสมอ (เขียนไปยัง the queue ทำให้มันเติม) แต่ไม่เคยได้มัน. The source code ที่จัดให้มีบน the xSemaphoreGiveFromISR() documentation page จะทำให้สิ่งนี้ชัดเจนขึ้น. ดู RTOS task notifications อีกด้วย, ซึ่งสามารถถูกใช้เป็นทางเลือก binary semaphore ที่เร็วขึ้นและน้ำหนักเบาขึ้นในบางสถานะการณ์.

Task prioritisation สามารถถูกใช้เพื่อให้แน่ใจว่า peripherals ได้รับการบริการในเวลาที่เหมาะสม - การสร้าง a 'deferred interrupt' scheme อย่างมีประสิทธิภาพ. (โปรดทราบว่า  FreeRTOS มี a built in deferred interrupt mechanism อีกด้วย). อีกวิธีหนึ่งคือการใช้ a queue แทน the semaphore.  เมื่อสิ่งนี้ถูกทำ the interrupt routine สามารถจับ the data ที่เกี่ยวข้องกับ the peripheral event และส่งมันไปบน a queue ไปยัง the task. The task ปลดบล็อกเมื่อ data กลายมาเป็นมีให้ใช้ประโยชน์บน the queue, ดึงเอา the data จาก the queue, จากนั้นดำเนินการ any data processing ที่ต้องการ. แบบแผนที่สองนี้ยอมให้ interrupts คงอยู่สั้นที่สุดเท่าที่จะเป็นไปได้ โดยกระบวนการโพสต์ทั้งหมดจะเกิดขึ้นแทนภายใน a task.

ดู the Semaphores/Mutexes section ของ the user documentation สำหรับรายการของ semaphore related API functions. ค้นหา the files ใน the FreeRTOS/Demo/Common/Minimal directory จะแสดงหลายตัวอย่างของการใช้ของพวกมัน. โปรดทราบว่า interrupts ต้องไม่ใช้ API functions ที่ไม่สิ้นสุดใน "FromISR".

                                           (https://i.imgur.com/iepazYY.png)
                                                 (ภาพดูในลิ้งค์เอานะ จะเป็นภาพเคลื่อนไหว)
การใช้ a semaphore เพื่อซิงโครไนซ์ a task กับ an interrupt. The interrupt  'ให้' the semaphore เท่านั้น, ขณะที่ the task 'ได้' the semaphore เท่านั้น.
Title: Re: FreeRTOS
Post by: tha on October 16, 2021, 10:42:57 AM
https://www.freertos.org/Real-time-embedded-RTOS-Counting-Semaphores.html (https://www.freertos.org/Real-time-embedded-RTOS-Counting-Semaphores.html)

FreeRTOS Counting Semaphores
[Inter-task communication and synchronisation]

[See also Blocking on Multiple RTOS Objects]

The FreeRTOS tutorial book จัดให้มี additional information เกี่ยวกับ queues, binary semaphores, mutexes, counting semaphores และ recursive semaphores, พร้อมด้วย simple worked examples ใน a set of accompanying example projects.

FreeRTOS Counting Semaphores

               TIP: 'Task Notifications' สามารถจัดให้มีทางเลือกที่มีน้ำหนักเบาไปยัง counting semaphores ในหลาย ๆ สถานการณ์

เช่นเดียวกับ binary semaphores สามารถคิดได้ว่าเป็น queues ที่มีความยาวหนึ่ง, counting semaphores สามารถคิดได้ว่าเป็น queues ที่มีความยาวมากกว่าหนึ่ง. อีกครั้ง, ผู้ใช้ the semaphore ไม่ต้องสนใจ the data ที่เก็บไว้ใน the queue - เพียงสนใจว่า the queue ว่างเปล่าหรือไม่.
Title: Re: FreeRTOS
Post by: tha on October 16, 2021, 11:02:00 AM
     1. Counting events

         ในสถานการณ์การใช้งานนี้ an event handler จะ 'ให้' a semaphore แต่ละครั้งที่ an event เกิดขึ้น (เพิ่ม the semaphore count value), และ a handler
         task จะ 'ได้' a semaphore แต่ละครั้งที่มันประมวลผล an event (ลด the semaphore count value). The count value จึงเป็นความแตกต่างกันระหว่าง
         จำนวนของ events ที่เกิดขึ้นแล้วและจำนวนที่ถูกประมวลผลแล้ว. ในกรณีนี้ขอแนะนำให้ the count value เป็นศูนย์เมื่อ the semaphore ถูกสร้าง.
Title: Re: FreeRTOS
Post by: tha on October 16, 2021, 11:17:40 AM
     2. Resource management.

         ในสถานการณ์การใช้งานนี้ the count value แสดงจำนวนของ resources ที่มีให้ใช้ประโยชน์. เพื่อให้ได้มาซึ่งการควบคุม a resource a task ต้องได้รับ a
         semaphore ก่อน - ลด the semaphore count value. เมื่อ the count value ถึงศูนย์จะไม่มี free resources. เมื่อ a task เสร็จสิ้นการใช้ the resource
         มันจะ 'ให้' the semaphore กลับ - เพิ่ม the semaphore count value. ในกรณีนี้ขอแนะนำให้ the count value เป็นเท่ากับ the maximum count
         value เมื่อ the semaphore ถูกสร้าง.

ดู the Semaphores/Mutexes section ของ the user documentation สำหรับรายการของ semaphore related API functions. ค้นหา the files ใน the FreeRTOS/Demo/Common/Minimal directory จะแสดงหลายตัวอย่างของการใช้ของพวกมัน. โปรดทราบว่า interrupts ต้องไม่ใช้ API functions ที่ไม่สิ้นสุดใน "FromISR".
Title: Re: FreeRTOS
Post by: tha on October 16, 2021, 01:19:28 PM
https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html (https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html)

FreeRTOS Mutexes
[Inter-task communication and synchronisation]

[See also Blocking on Multiple RTOS Objects]

The FreeRTOS tutorial book จัดให้มี additional information เกี่ยวกับ queues, binary semaphores, mutexes, counting semaphores และ recursive semaphores, พร้อมด้วย simple worked examples ใน a set of accompanying example projects.

FreeRTOS Mutexes

Mutexes เป็น binary semaphores ที่รวมเอา a priority inheritance mechanism. ขณะที่ binary semaphores เป็นทางเลือกที่ดีกว่าสำหรับการจัดให้มีใช้ synchronisation (ระหว่าง tasks หรือระหว่าง tasks และ an interrupt), mutexes เป็นทางเลือกที่ดีกว่าสำหรับการจัดให้มีใช้ simple mutual exclusion(กันไว้สำหรับคนพิเศษ) (ด้วยเหตุนี้ 'MUT'ual 'EX'clusion).

เมื่อถูกใช้สำหรับ mutual exclusion(กันไว้สำหรับคนพิเศษ) the mutex จะทำหน้าที่เหมือน a token ที่ถูกใช้เพื่อปกป้อง a resource. เมื่อ a task ต้องการเข้าถึง the resource มันต้องได้รับ ('ได้') the token ก่อน. เมื่อมันสำเร็จกับ the resource นี้แล้วมันต้อง 'ให้' the token กลับคืน - ยอมให้ tasks อื่นมีโอกาศเข้าถึง the same resource.
Title: Re: FreeRTOS
Post by: tha on October 17, 2021, 05:35:25 AM
Mutexes ใช้ the same semaphore เข้าถึง API functions ดังนั้นยอมให้ a block time ถูกระบุอีกด้วย. The block time แสดงจำนวนสูงสุดของ 'ticks' ที่ a task เข้าสู่ the Blocked state เมื่อพยายามให้ 'ได้' a mutex ถ้า the mutex ไม่มีให้ใช้ประโยชน์ทันที. ไม่เหมือน binary semaphores อย่างไรก็ตาม - mutexes ใช้ priority inheritance. นี้หมายความว่าถ้า a high priority task ที่ถูกบล็อกขณะที่พยายามให้ได้รับ a mutex (token) ที่ปัจจุบันถูกถือโดย a lower priority task, ดังนั้น the priority ของ the task ที่ถือ the token จะถูกยกขึ้นชั่วคราวถึง the blocking task นั้น. กลไกนี้ถูกออกแบบเพื่ให้แน่ใจว่า the higher priority task ถูกเก็บไว้ใน the blocked state เป็นเวลาที่สั้นที่สุดเท่าที่เป็นได้, และเพื่อลด the 'priority inversion' ที่ได้เกิดขึ้นแล้ว.

Priority inheritance ไม่ได้แก้ไข priority inversion! มันแค่ลดผลกระทบของมันในบางสถานะการณ์. Hard real time applications ควรถูกออกแบบเพื่อ priority inversion ไม่เกิดขึ้นในตั้งแต่แรก.

Mutexes ไม่ควรถูกใช้จาก an interrupt เนื่องจาก:

     •  พวกมันรวม a priority inheritance mechanism ซึ่งเข้าที่เข้าทางเฉพาะถ้า the mutex ถูกให้และถูกได้รับจาก a task, ไม่ใช่ an interrupt

     •  An interrupt ไม่สามารถบล็อกเพื่อคอย a resource ที่ถูกป้องกันโดย a mutex เพื่อกลายมาเป็นมีให้ใช้ประโยชน์.

                        (https://i.imgur.com/1ryFsEY.png)
                         (ภาพดูในลิ้งค์เอานะ จะเป็นภาพเคลื่อนไหว)
                        การใช้ a mutex เพื่อป้องกันการเข้าถึงทรัพยากรที่ใช้ร่วมกัน.
Title: Re: FreeRTOS
Post by: tha on October 17, 2021, 09:19:10 AM
https://www.freertos.org/RTOS-Recursive-Mutexes.html (https://www.freertos.org/RTOS-Recursive-Mutexes.html)

FreeRTOS Recursive Mutexes
[Inter-task communication and synchronisation]

[See also Blocking on Multiple RTOS Objects]

The FreeRTOS tutorial book จัดให้มี additional information เกี่ยวกับ queues, binary semaphores, mutexes, counting semaphores และ recursive semaphores, พร้อมด้วย simple worked examples ใน a set of accompanying example projects.

FreeRTOS Recursive Mutexes

A mutex ที่ถูกใช้ซ้ำๆสามารถถูก 'ได้' ซ้ำๆโดยเจ้าของ. The mutex ไม่กลายมาเป็นมีให้ใช้ประโยช์อีกครั้งจนกระทั่ง the owner เรียกใช้ xSemaphoreGiveRecursive() สำหรับแต่ละความสำเร็จการร้องขอ xSemaphoreTakeRecursive(). ตัวอย่างเช่น, ถ้า a task สำเร็จการ 'ได้' the same mutex 5 ครั้งดังนั้น the mutex จะไม่มีให้ใช้ประโยชน์ไปยัง task อื่นใดๆจนกระทั่งมันได้ 'ให้' the mutex กลับคืนห้าครั้งจริงๆพอดี.

semaphore ชนิดนี้ใช้ a priority inheritance mechanism ดังนั้น a task 'ได้' a semaphore ต้อง 'ให้' the semaphore กลับคืนเสมอทันทีที่ the semaphore ไม่ถูกต้องการอีกต่อไป.

Mutex type semaphores ไม่สามารถถูกใช้จากภายใน interrupt service routines.

Mutexes ไม่ควรถูกใช้จาก an interrupt เนื่องจาก:

     •  พวกมันรวม a priority inheritance mechanism ซึ่งเข้าที่เข้าทางเฉพาะถ้า the mutex ถูกให้และถูกได้รับจาก a task, ไม่ใช่ an interrupt

     •  An interrupt ไม่สามารถบล็อกเพื่อคอย a resource ที่ถูกป้องกันโดย a mutex เพื่อกลายมาเป็นมีให้ใช้ประโยชน์.
Title: Re: FreeRTOS
Post by: tha on October 17, 2021, 11:52:47 AM
https://www.freertos.org/RTOS-task-notifications.html (https://www.freertos.org/RTOS-task-notifications.html)

RTOS Task Notifications
[Inter-task communication and synchronisation]

                                          มีให้ใช้ประโยชน์จาก FreeRTOS V8.2.0
                                    รองรับหลาย Notifications ต่อ Task จาก V10.4.0


Description

[Also see the blog post "Decrease RAM Footprint and Accelerate Execution with FreeRTOS Notifications"]

แต่ละ RTOS task มี an array of task notifications. แต่ละ task notification มี a notification state ที่สามารถเป็นอย่างใดอย่างหนึ่ง 'รอดำเนินการ' หรือ 'ไม่รอดำเนินการ', และ a 32-bit notification value. ค่าคงที่ configTASK_NOTIFICATION_ARRAY_ENTRIES ตั้งค่าจำนวนของดัชนีใน the task notification array. ก่อนหน้า FreeRTOS V10.4.0 tasks มีเพียง a single task notification, ไม่ใช่ an array of notifications.

การแจ้งเตือนงานโดยตรงไปยังงานคือ an event ที่ส่งโดยตรงไปยัง a task, แทนที่จะเป็นโดยอ้อมไปยัง a task ผ่านทางวัตถุตัวกลางอย่าง a queue, event group หรือ semaphore. การส่งการแจ้งเตือนงานไปยังงานโดยตรงจะตั้งค่าสถานะของ the target task notification เป็น 'รอดำเนินการ'. เช่นเดียวกับ a task สามารถบล็อกบนผ่านทางวัตถุตัวกลางอย่าง a semaphore เพื่อรอสำหรับ semaphore นั้นมีให้ใช้เป็นประโยชน์, a task สามารถบล็อกบน a task notification เพื่อรอให้สถานะของการแจ้งเตือนนั้นกลายมาเป็นรอดำเนินการ.
Title: Re: FreeRTOS
Post by: tha on October 17, 2021, 12:08:11 PM
การส่งการแจ้งเตือนงานโดยตรงไปยังงานยังสามารถเลือกที่จะอัพเดตค่าของ the target notification ด้วยวิธีใดวิธีหนึ่งต่อไปนี้:

     •  เขียนทับค่าโดยไม่คำนึงถึงว่า the receiving task ได้อ่านค่าที่ถูกเขียนทับแล้วหรือไม่
     •  เขียนทับค่า แต่ถ้า the receiving task ได้อ่านค่าที่ถูกเขียนทับแล้วเท่านั้น
     •  ตั้งค่าบิตอย่างน้อยหนึ่งบิตในค่านั้น
     •  เพิ่ม (เพิ่มหนึ่งไปยัง) ค่านั้น
Title: Re: FreeRTOS
Post by: tha on October 17, 2021, 01:00:01 PM
การเรียก xTaskNotifyWait()/xTaskNotifyWaitIndexed() เพื่ออ่าน a notification value จะเคลียร์ notification's state นั้นเป็น 'ไม่รอดำเนินการ'. Notification states สามารถถูกตั้งค่าอย่างชัดเจนเป้น 'ไม่รอดำเนินการ' ได้อีกด้วยโดยการเรียก xTaskNotifyStateClear()/xTaskNotifyStateClearIndexed().

หมายเหตุ: แต่ละ notification ภายใน the array ทำงานอย่างอิสระ - a task สามารถบล็อกเฉพาะบนหนึ่ง notification ภายใน the array ในแต่ละครั้งและจะไม่ถูกปลดบล็อกโดย a notification ที่ส่งไปยังดัชนีอาร์เรย์อื่นใดๆ.

RTOS task notification functionality ถูกเปิดการใช้งานโดยค่าเริ่มต้น, และสามารถถูกแยกออกจาห a build (ประหยัด 8 bytes ต่อ array index ต่อ task) โดยการเซ็ต configUSE_TASK_NOTIFICATIONS เป็น 0 ใน FreeRTOSConfig.h.

หมายเหตุสำคัญ: FreeRTOS Stream and Message Buffers ใช้ the task notification ที่ array index 0. ถ้าคุณต้องการรักษาสถานะของ a task notification ข้ามการเรียกไปยัง a Stream or Message Buffer API function ดังนั้นใช้ a task notification ที่ an array index ที่มากกว่า 0.
Title: Re: FreeRTOS
Post by: tha on October 17, 2021, 03:51:32 PM
Performance Benefits and Usage Restrictions

ความหยืดหยุ่นของ task notifications ยอมให้พวกมันถูกใช้มิฉะนั้นมันจำเป็นต้องสร้าง a separate(ที่แยกกัน) queue, binary semaphore, counting semaphore or event group. การปลดบล็อก an RTOS task ด้วย a direct notification จะ 45% เร็วขึ้น * และใช้ RAM น้อยกว่าการปลดบล็อก a task โดยใช้ an intermediary object อย่างเช่น a binary semaphore. ตามที่คาดไว้, ประโยชน์ด้านประสิทธิภาพเหล่านี้จำเป็นต้องมีข้อจำกัดกรณีการใช้งานบางประการ:

     1. RTOS task notifications สามารถถูกใช้เฉพาะเมื่อมีเพียงหนึ่ง task เท่านั้นที่สามารถเป็นผู้รับของ the event. เงื่อนไขนี้อย่างไรก็ตามถูกพบในส่วนใหญ่ของกรณี
         ใช้งานจริง, อย่างเช่น an interrupt ปลดบล็อก a task ที่จะประมวลผล the data ที่รับจาก the interrupt.

     2. เฉพาะในกรณีที่ an RTOS task notification ถูกใช้แทน a queue: ในขณะที่ a receiving task สามารถรอ a notification ใน the Blocked state (ดังนั้น
         ไม่กิน any CPU time), a sending task ไม่สามารถรอใน the Blocked state เพื่อให้การส่งเสร็จสมบูรณ์ถ้าการส่งไม่สามารถเสร็จสมบูรณ์โดยทันที.
Title: Re: FreeRTOS
Post by: tha on October 17, 2021, 04:14:18 PM
Use Cases

Notifications ถูกส่งโดยใช้ the xTaskNotifyIndexed() และ xTaskNotifyGiveIndexed() API functions (and their interrupt safe equivalents), และยังคง pending จนกระทั่ง the receiving RTOS task เรียกใช้อย่างใดอย่างหนึ่งของ the xTaskNotifyWaitIndexed() หรือ ulTaskNotifyTakeIndexed() API functions แต่ละของ API functions เหล่านี้มีความเทียบเท่ากับที่ไม่มี "Indexed" นำหน้า. The non "Indexed" versions ทำงานบน the task notification ที่ array index 0 เสมอ. ตัวอย่างเช่น xTaskNotifyGive( TargetTask ) เทียบเท่ากับ xTaskNotifyGiveIndexed( TargetTask, 0 ) - ทั้งคู่เพิ่ม the task notification ที่ index 0 ของ the task ที่อ้างอิงโดย the task ที่จัดการ TargetTask.

Examples

     •  Using RTOS task notifications as a light weight binary semaphore
     •  Using RTOS task notifications as a light weight counting semaphore
     •  Using RTOS task notifications as a light weight event group
     •  Using RTOS task notifications as a light weight mailbox

* ถูกวัดโดยใช้ the binary semaphore implementation จาก FreeRTOS V8.1.2, ที่คอมไพล์ด้วย GCC at -O2 optimisation, และไม่มีการกำหนด configASSERT(). A 35% improvement ยังคงสามารถถูกได้รับโดยใช้ the improved binary semaphore implementation ที่พบใน FreeRTOS V8.2.0 และสูงกว่า.
Title: Re: FreeRTOS
Post by: tha on October 18, 2021, 07:05:39 AM
https://www.freertos.org/RTOS_Task_Notification_As_Binary_Semaphore.html (https://www.freertos.org/RTOS_Task_Notification_As_Binary_Semaphore.html)

RTOS Task Notifications
ถูกใช้อย่าง Light Weight Binary Semaphores

หน้าที่เกี่ยวข้อง:

     •  RTOS task notifications
     •  Using RTOS task notifications as a light weight counting semaphore
     •  Using RTOS task notifications as a light weight event group
     •  Using RTOS task notifications as a light weight mailbox

การปลดบล็อก an RTOS task ด้วย a direct notification เร็วขึ้น 45% และใช้ RAM น้อยกว่าการปลดบล็อก a task ด้วย a binary semaphore. หน้านี้แสดงวิธีการดำเนินการนี้.

A binary semaphore เป็น a semaphore ที่มี a maximum count เท่ากับ 1, ดังนั้นจึงได้ชื่อ the 'binary'. A task สามารถ 'ได้' the semaphore เฉพาะถ้ามันมีให้ใช้ประโยชน์, และ the semaphore มีให้ใช้ประโยชน์เฉพาะถ้าตัวนับของมันเป็น 1.

เมื่อ a task notification ถูกใช้แทน a binary semaphore ค่าการแจ้งเตือนของ the receiving task ถูกใช้แทนค่านับของ the binary semaphore, และ the ulTaskNotifyTake() (หรือ ulTaskNotifyTakeIndexed()) API function ถูกใช้แทน the semaphore's xSemaphoreTake() API function. The ulTaskNotifyTake() function's xClearOnExit parameter ถูกเซ็ตเป็น pdTRUE ดังนั้น the count value จะถูกรีเทิร์นเป็นศูนย์แต่ละที่ the notification ถูกได้ - จำลอง a binary semaphore.

ในทำนองเดียวกัน the xTaskNotifyGive() (หรือ xTaskNotifyGiveIndexed()) หรือ vTaskNotifyGiveFromISR() (หรือ vTaskNotifyGiveIndexedFromISR()) functions ถูกใช้แทน the semaphore's xSemaphoreGive() and xSemaphoreGiveFromISR() functions.

ดู the example ด้านล่าง.
(ตัวอย่างโค้ดดูในลิ้งเอานะครับ)
Title: Re: FreeRTOS
Post by: tha on October 18, 2021, 08:57:04 AM
https://www.freertos.org/RTOS_Task_Notification_As_Counting_Semaphore.html (https://www.freertos.org/RTOS_Task_Notification_As_Counting_Semaphore.html)

RTOS Task Notifications
ถูกใช้อย่าง Light Weight Counting Semaphores

หน้าที่เกี่ยวข้อง:

     •  RTOS task notifications
     •  Using RTOS task notifications as a light weight counting semaphore
     •  Using RTOS task notifications as a light weight event group
     •  Using RTOS task notifications as a light weight mailbox

การปลดบล็อก an RTOS task ด้วย a direct notification เร็วขึ้น 45% และใช้ RAM น้อยกว่าการปลดบล็อก a task ด้วย a semaphore. หน้านี้แสดงวิธีการดำเนินการนี้.

A counting semaphore เป็น a semaphore ที่สามารถมี a count value ของ zero จนถึง a maximum value เซ็ตเมื่อ the semaphore ถูกสร้าง. A task สามารถ 'ได้' the semaphore เฉพาะถ้ามันมีให้ใช้ประโยชน์, และ the semaphore มีให้ใช้ประโยชน์เฉพาะถ้าตัวนับของมันมากกว่าศูนย์.

เมื่อ a task notification ถูกใช้แทน a counting semaphore ค่าการแจ้งเตือนของ the receiving task ถูกใช้แทนค่านับของ the counting semaphore, และ the ulTaskNotifyTake() (หรือ ulTaskNotifyTakeIndexed()) API function ถูกใช้แทน the semaphore's xSemaphoreTake() API function. The ulTaskNotifyTake() function's xClearOnExit parameter ถูกเซ็ตเป็น pdFALSE ดังนั้น the count value จะถูกลดลงเท่านั้น (แทนที่จะถูกเคลียร์) ทุกครั้งที่ the notification ถูกได้ - จำลอง a counting semaphore.

ในทำนองเดียวกัน the xTaskNotifyGive() (หรือ xTaskNotifyGiveIndexed()) หรือ vTaskNotifyGiveFromISR() (หรือ vTaskNotifyGiveIndexedFromISR()) functions ถูกใช้แทน the semaphore's xSemaphoreGive() and xSemaphoreGiveFromISR() functions.

ตัวอย่างแรกด้านล่างใช้ the receiving task's notification value เป็น a counting semaphore. The second example จัดให้มีการนำไปใช้ในทางปฏิบัติและมีประสิทธิภาพมากขึ้น.

Example 1:

(ตัวอย่างโค้ดดูในลิ้งเอานะครับ)

Example 2:

ตัวอย่างนี้แสดงการใช้งานจริงและมีประสิทธิภาพมากขึ้นสำหรับ the RTOS task. ในการจัดให้มีใช้งานนี้ ค่าที่รีเทิร์นจาก ulTaskNotifyTake() จะถูกใช้เพื่อให้ทราบจำนวนเท่าใดที่ ISR events ที่สำคัญต้องถูกประมวลผล, ยอมให้ the RTOS task's notification count ถูกเคลียร์กลับไปเป็นศูนย์แต่ละครั้งที่ ulTaskNotifyTake() ถูกเรียกใช้. The interrupt service routine (ISR) ถูกทึกทักเอาว่าเป็นอย่างที่แสดงใน example 1 ข้างบน.

(ตัวอย่างโค้ดดูในลิ้งเอานะครับ)
Title: Re: FreeRTOS
Post by: tha on October 19, 2021, 05:18:01 AM
https://www.freertos.org/RTOS_Task_Notification_As_Event_Group.html (https://www.freertos.org/RTOS_Task_Notification_As_Event_Group.html)

RTOS Task Notifications
Used As Light Weight Event Group

หน้าที่เกี่ยวข้อง:

     •  RTOS task notifications
     •  Using RTOS task notifications as a light weight counting semaphore
     •  Using RTOS task notifications as a light weight event group
     •  Using RTOS task notifications as a light weight mailbox

An event group เป็นชุดของ binary flags (หรือ bits), ซึ่งแต่ละอัน the application writer สามารถกำหนดความหมายได้. An RTOS task สามารถเข้าสู่ the Blocked state เพื่อรอสำหรับ flags หนึ่งหรือมากกว่าภายใน the group กลายมาเป็น active. The RTOS task จะไม่กิน CPU time ใดๆในขณะที่มันอยู่ใน the Blocked state.

เมื่อ a task notification ถูกใช้แทน an event group ค่าการแจ้งเตือนของ the receiving task ถูกใช้แทน the event group, bits ภายในค่าการแจ้งเตือนของ the receiving task ถูกใช้เป็น event flags, และ the xTaskNotifyWait() API function ถูกใช้แทน the event group's xEventGroupWaitBits() API function.

ในทำนองเดียวกัน, bits จะถูกเซ็ตโดยใช้ the xTaskNotify() และ xTaskNotifyFromISR() API functions (ด้วย eAction parameter ของพวกมันถูกเซ็ตเป็น eSetBits) แทน the xEventGroupSetBits() และ xEventGroupSetBitsFromISR() functions ตามลำดับ.

xTaskNotifyFromISR() มีประโยชน์ด้านประสิทธิภาพอย่างมีนัยสำคัญเมื่อเปรียบเทียบกับ xEventGroupSetBitsFromISR() เนื่องจาก xTaskNotifyFromISR() ดำเนินการทั้งหมดใน the ISR, ในขณะที่  xEventGroupSetBitsFromISR() ต้องเลื่อนการประมวลผลบางอย่างไปยัง the RTOS daemon task

ต่างจากเมื่อใช้ an event group the receiving task ไม่สามารถระบุได้ว่ามันต้องการออกจาก the Blocked state เท่านั้นเมื่อการรวมกันของบิตหลายบิต active ในเวลาเดียวกัน. แต่ the task จะถูกปลดบล็อกเมื่อ bit หนึ่งใดๆกลายมาเป็น active, และต้องทดสอบสำหรับการรวมบิตด้วยตัวมันเอง.

ดูตัวอย่างด้านล่าง :
(ตัวอย่างโค้ดดูในลิ้งเอานะครับ)
Title: Re: FreeRTOS
Post by: tha on October 19, 2021, 09:33:50 AM
https://www.freertos.org/RTOS_Task_Notification_As_Mailbox.html (https://www.freertos.org/RTOS_Task_Notification_As_Mailbox.html)

RTOS Task Notifications
Used As Light Weight Mailbox

หน้าที่เกี่ยวข้อง:

     •  RTOS task notifications
     •  Using RTOS task notifications as a light weight counting semaphore
     •  Using RTOS task notifications as a light weight event group
     •  Using RTOS task notifications as a light weight mailbox

RTOS task notifications สามารถถูกใช้ส่ง data ไปยัง a task, แต่ในวิธีที่จำกัดมากกว่าที่สามารถถูกทำสำเร็จได้ด้วย an RTOS queue เนื่องจาก:

     1. มีเพียง 32-bit values สามารถถูกส่ง
     2. ค่าถูกบันทึกเป็น the receiving task's notification value, และสามารถเป็นเพียงหนึ่ง notification value เท่านั้น ณ เวลาใดเวลาหนึ่ง

ดังนั้น the phrase 'lightweight mailbox' ถูกใช้แทน 'lightweight queue'. The task's notification value คือ the mailbox value.

Data ถูกส่งไปยัง a task โดยใช้ the xTaskNotify() (หรือ xTaskNotifyIndexed()) และ xTaskNotifyFromISR() (หรือ xTaskNotifyIndexedFromISR()) API functions ด้วย their eAction parameter ที่เซ็ตเป็นอย่างใดอย่างหนึ่ง eSetValueWithOverwrite หรือ eSetValueWithoutOverwrite. ถ้า eAction ถูกเซ็ตเป็น eSetValueWithOverwrite ดังนั้น the receiving task's notification value จะถูกอัปเดตถึงแม้ว่า the receiving task จะมี a notification pending อยู่แล้วก็ตาม. ถ้า eAction ถูกเซ็ตเป็น eSetValueWithoutOverwrite ดังนั้น the receiving task's notification value จะถูกอัปเดตเฉพาะถ้า the receiving task ยังไม่มี a notification pending - ในการอัพเดท the notification value จะเขียนทับค่าก่อนหน้าก่อน the receiving task จะประมวลผลมัน.

A task สามารถอ่าน notification value ของมันเองโดยใช้ xTaskNotifyWait() (หรือ xTaskNotifyWaitIndexed()).

ดู the documentation สำหรับ the relevant API functions for examples.
Title: Re: FreeRTOS
Post by: tha on October 20, 2021, 07:07:23 AM
https://www.freertos.org/RTOS-stream-message-buffers.html (https://www.freertos.org/RTOS-stream-message-buffers.html)

RTOS Stream & Message Buffers
[Inter-task communication and synchronisation]

                            มีให้ใช้ประโยชน์จาก FreeRTOS V10.0.0

Introduction

Stream buffers เป็นดั้งเดิมการสื่อสาร an RTOS task ไปยัง RTOS task, และ interrupt ไปยัง task. ต่างจาก FreeRTOS communications primitives อื่นๆส่วนใหญ่, สิ่งเหล่านี้ได้รับการปรับให้เหมาะสมสำหรับสถานการณ์ single reader single writer, อย่างเช่นการส่งผ่าน data จาก an interrupt service routine ไปยัง a task, หรือจาก one microcontroller core ไปยังคอร์อื่นบน dual core CPUs. Data ถูกส่งผ่านโดยการก๊อปปี้ - the data ถูกก็อปปี้ลงใน the buffer โดยตัวส่งและออกจาก the buffer โดยการอ่าน.

Stream buffers ส่งผ่าน bytes ที่ไหลอย่างต่อเนื่อง. Message buffers ส่งผ่านขนาดตัวแปรแต่ messages ไม่ต่อเนื่อง. Message buffers ใช้ stream buffers สำหรับ data transfer.

หมายเหตุสำคัญ: มีลักษณะเฉพาะในหมู่ FreeRTOS objects, the stream buffer implementation (ดังนั้น the message buffer implementation อีกด้วย, ดังที่ message buffers ถูกสร้างบนส่วนบนสุดของ stream buffers) ถือว่ามีเพียง task เดียวหรือ interrupt เดียวที่จะเขียนไปยัง the buffer (the writer), และมีเพียง task เดียวหรือ interrupt เดียวที่จะอ่านจาก the buffer (the reader).  มันจะปลอดภัยสำหรับ the writer and reader ที่เป็น tasks หรือ interrupts ที่แตกต่างกัน, แต่, ต่างจาก FreeRTOS objects อื่นๆ, มันไม่ปลอดภัยที่มีหลาย writers ที่แตกต่างกันหรือหลาย readers ที่แตกต่างกัน. ถ้ามีเป็น multiple different writers ดังนั้น the application writer ต้องวางแต่ละการเรียกใช้ถึง a writing API function (อย่างเช่น xStreamBufferSend()) ภายใน a critical section และใช้ a send block time เป็น 0. ในทำนองเดียวกัน, ถ้ามีเป็น multiple different readers ดังนั้น the application writer ต้องวางแต่ละการเรียกใช้ถึง a reading API function (อย่างเช่น xStreamBufferReceive()) ภายใน a critical section และใช้ a receive block time เป็น 0.

Further Reading(อ่านเพิ่มเติม)

หน้าต่อไปนี้จะอธิบาย stream buffers and message buffers ในรายละเอียดเพิ่มเติม, และจัดให้มีตัวอย่างของการใช้เพื่อจัดให้มีใช้ interrupt to task และ processor core to processor core communications ของพวกมันตามลำดับ.

ข้อมูลเพิ่มเติมเกี่ยวกับ Stream Buffers...
ข้อมูลเพิ่มเติมเกี่ยวกับ Message Buffers...
บล็อกเกี่ยวกับการใช้ message buffers for core to core communication...
Title: Re: FreeRTOS
Post by: tha on October 20, 2021, 07:44:17 AM
https://www.freertos.org/RTOS-stream-buffer-example.html (https://www.freertos.org/RTOS-stream-buffer-example.html)

RTOS Stream Buffers
[Stream Buffers and Message Buffers]

                      มีให้ใช้ประโยชน์จาก FreeRTOS V10.0.0

ในหน้านี้:

     1. Introduction(บทนำ)
     2. Getting started(เริ่มต้น)
     3. Blocking reads and Trigger Levels (การบล็อกการอ่านและระดับทริกเกอร์)
     4. Blocking Writes (การปิดกั้นการเขียน)
     5. Send and Receive Complete Macros (for multicore use) (ส่งและรับมาโครที่สมบูรณ์ (สำหรับการใช้งานมัลติคอร์))

Introduction

Stream buffers ยอมให้ a stream of bytes ถูกส่งผ่านจาก an interrupt service routine ไปยัง a task, หรือจาก task หนึ่งไปยัง task อื่น. A byte stream สามารถมีความยาวตามอำเภอใจและไม่จำเป็นต้องมีการเริ่มต้นหรือสิ้นสุด.  จำนวนของ bytes ใดๆสามารถถูกเขียนในครั้งเดียวและจำนวนของ bytes ใดๆสามารถถูกอ่านในครั้งเดียว. Data ถูกส่่งผ่านโดยการก๊อปปี้ - the data ถูกก็อปปี้ลงใน the buffer โดยตัวส่งและออกจาก the buffer โดยการอ่าน.

ไม่เหมือนกับ FreeRTOS communications primitives อื่นส่วนใหญ่, stream buffers ได้รับการปรับให้เหมาะสมสำหรับสถานการณ์ single reader single writer, อย่างเช่นส่งผ่าน data จาก an interrupt service routine ไปยัง a task, หรือจาก one microcontroller core ไปยังคอร์อื่นบน a dual core CPU.

Stream buffer functionality ถูกเปิดการใช้งานโดยการ include the FreeRTOS/source/stream_buffer.c source file ในการ build.

The stream buffer implementation ใช้ direct to task notifications. ดังนั้น, การเรียกใช้ a stream buffer API function ที่วาง the calling task ลงใน the Blocked state สามารถเปลี่ยน the calling task's notification state and value.

หมายเหตุสำคัญ: มีลักษณะเฉพาะในหมู่ FreeRTOS objects, the stream buffer implementation (ดังนั้น the message buffer implementation อีกด้วย, ดังที่ message buffers ถูกสร้างบนส่วนบนสุดของ stream buffers) ถือว่ามีเพียง task เดียวหรือ interrupt เดียวที่จะเขียนไปยัง the buffer (the writer), และมีเพียง task เดียวหรือ interrupt เดียวที่จะอ่านจาก the buffer (the reader).  มันจะปลอดภัยสำหรับ the writer and reader ที่เป็น tasks หรือ interrupts ที่แตกต่างกัน, แต่, ต่างจาก FreeRTOS objects อื่นๆ, มันไม่ปลอดภัยที่มีหลาย writers ที่แตกต่างกันหรือหลาย readers ที่แตกต่างกัน. ถ้ามีเป็น multiple different writers ดังนั้น the application writer ต้องวางแต่ละการเรียกใช้ถึง a writing API function (อย่างเช่น xStreamBufferSend()) ภายใน a critical section และใช้ a send block time เป็น 0. ในทำนองเดียวกัน, ถ้ามีเป็น multiple different readers ดังนั้น the application writer ต้องวางแต่ละการเรียกใช้ถึง a reading API function (อย่างเช่น xStreamBufferReceive()) ภายใน a critical section และใช้ a receive block time เป็น 0.
Title: Re: FreeRTOS
Post by: tha on October 21, 2021, 07:02:53 AM
Getting Started

The FreeRTOS/Demo/Common/Minimal/StreamBufferInterrupt.c source file จัดให้มีตัวอย่างความคิดเห็นอย่างมากของวิธีใช้  a stream buffer เพื่อส่งผ่าน data จาก an interrupt service routine ไปยัง a task.

ดู the stream buffer section ของ the user documentation สำหรับรายการของ stream buffer related API functions, ในหลายกรณีรวมถึง code snippets ที่สาธิต the functions ที่กำลังถูกใช้.

Blocking Reads and Trigger Levels

xStreamBufferReceive() ถูกใช้อ่าน data ออกจาก a stream buffer จาก an RTOS task. xStreamBufferReceiveFromISR()) ถูกใช้อ่าน data ออกจาก a stream buffer จาก an interrupt service routine (ISR).

xStreamBufferReceive() ยอมให้ a block time ถูกระบุ. ถ้า a non zero block time ถูกระบุเมื่อ a task ใช้ xStreamBufferReceive() เพื่ออ่านจาก a stream buffer ที่เกิดความว่างเปล่า the task จะถูกวางลงใน the Blocked state (ดังนั้นมันจะไม่กิน CPU time ใดๆและ tasks อื่นสามารถ run) จนกระทั่งอย่างใดอย่างหนึ่ง a specified amount of data กลายมาเป็นมีให้ใช้ประโยชน์ใน the stream buffer, หรือ the block time หมดเวลาลง. The amount of data ที่ต้องอยู่ใน the stream buffer ก่อน a task ที่กำลังรอสำหรับ data ถูกถอดออกจาก the blocked state ถูกเรียกใช้ the stream buffer's Trigger Level. ตัวอย่างเช่น:

     •  ถ้า a task ถูกบล็อกบนการอ่านของ an empty stream buffer ที่มี a trigger level เป็น 1 ดังนั้น the task จะถูกปลดบล็อกเมื่อ a single byte ถูกเขียนไป
         ยัง the buffer หรือ the task's block time หมดเวลาลง.

     •  ถ้า a task ถูกบล็อกบนการอ่านของ an empty stream buffer ที่มี a trigger level เป็น 10 ดังนั้น the task จะไม่ถูกปลดบล็อกจนกระทั่ง the stream
         buffer บรรจุอย่างน้อย 10 bytes หรือ the task's block time หมดเวลาลง.

ถ้า a reading task's block time หมดเวลาลงก่อน the trigger level ถูกถึงดังนั้น the task จะยังคงรับอย่างไรก็ตาม bytes มากมายที่มีให้ใช้ประโยชน์จริง.

Notes:

     •  ไม่ถูกต้องในการตั้งค่า the trigger level เป้น 0. การพยายามตั้งค่า the trigger level เป็น 0 จะเป็นผลให้ผลใน a trigger level เป็น 1 ถูกใช้.

     •  ไม่ถูกต้องอีกด้วยในการระบุ a trigger level ที่มากกว่า the stream buffer's size.

The stream buffer's trigger level ถูกตั้งค่าโดยเริ่มต้นเมื่อ the stream buffer ถูกสร้าง, และสามารถถูกเปลี่ยนแปลงโดยใช้ the xStreamBufferSetTriggerLevel() API function.

Blocking Writes

xStreamBufferSend()) ถูกใช้เพื่อส่ง data ไปยัง a stream buffer จาก an RTOS task. xStreamBufferSendFromISR()) ถูกใช้เพื่อส่ง data ไปยัง a stream buffer จาก an interrupt service routine (ISR).

ถ้า a non zero block time ถูกระบุเมื่อ a task ใช้ xStreamBufferSend() เพื่อเขียนไปยัง a stream buffer ที่เกิดเติม the task จะถูกวางลงใน the Blocked state (ดังนั้นมันจะไม่กิน CPU time ใดๆและ tasks อื่นสามารถ run) จนกระทั่งอย่างใดอย่างหนึ่ง space กลายมาเป็นมีให้ใช้ประโยชน์ใน the stream buffer, หรือ the block time หมดเวลาลง.

Send Complete and Receive Complete Macros

[Also see the blog on using message buffers for dual core - core to core communication.]
Title: Re: FreeRTOS
Post by: tha on October 21, 2021, 08:12:02 AM
sbSEND_COMPLETED() (and sbSEND_COMPLETED_FROM_ISR())

sbSEND_COMPLETED() เป็น a macro ที่ถูกเรียกใช้ (ในภายใน the FreeRTOS API functions) เมื่อ data ถูกเขียนไปยัง a stream buffer. มันได้รับ a single parameter, ซึ่งเป็น the handle of the stream buffer ที่ถูกอัปเดตแล้ว.

sbSEND_COMPLETED() ตรวจเช็คดูว่าถ้ามี a task ถูกบล็อกบน the stream buffer ที่รอสำหรับ data, และถ้าดังนั้น, ถอด the task ออกจาก the Blocked state.

มันสามารถเป็นได้สำหรับ the application writer ที่จะเปลี่ยนพฤติกรรมเริ่มต้นนี้โดยการจัดให้มีการจัดให้มีใช้ sbSEND_COMPLETED() ของพวอมันเองใน FreeRTOSConfig.h. ซึ่งมีประโยชน์เมื่อ a stream buffer ถูกใช้เพื่อส่งผ่าน data ระหว่าง cores บน a multicore processor. ในสถานการณ์นั้น, sbSEND_COMPLETED() สามารถถูกจัดให้มีใช้เพื่อสร้าง an interrupt ใน the other CPU core, และ the interrupt's service routine สามารถดังนั้นใช้ the xStreamBufferSendCompletedFromISR() API function เพื่อตรวจเช็ค, และถ้าถ้าจำเป็นปลดบล็อก, a task ที่กำลังรอแล้วสำหรับ the data. The FreeRTOS/Demo/Common/Minimal/MessageBufferAMP.c source file จัดให้มี a heavily commented example ของสถานะการณ์นั้นจริงๆ.

sbRECEIVE_COMPLETED() (and sbRECEIVE_COMPLETED_FROM_ISR())

sbRECEIVE_COMPLETED() คือตัวรับที่เทียบเท่ากับ sbSEND_COMPLETED(). มันถูกเรียกใช้It is called (ในภายใน the FreeRTOS API functions) เมื่อ data ถูกอ่านจาก a stream buffer. The macro ตรวจเช็คเพื่อดูว่าถ้ามี a task ถูกบล็อกบน the stream buffer ที่รอสำหรับ space กลายมาเป็นมีให้ใช้ประโยชน์ภายใน the buffer, และถ้าดังนั้น, ถอด the task ออกจาก the Blocked state. เช่นเดียวกับ sbSEND_COMPLETED(), the default behaviour of sbRECEIVE_COMPLETED() โดยการจัดให้มี an alternative implementation ใน FreeRTOSConfig.h
Title: Re: FreeRTOS
Post by: tha on October 21, 2021, 10:01:01 AM
https://www.freertos.org/RTOS-message-buffer-example.html (https://www.freertos.org/RTOS-message-buffer-example.html)

RTOS Message Buffers
[Stream Buffers and Message Buffers]

                      มีให้ใช้ประโยชน์จาก FreeRTOS V10.0.0

ในหน้านี้:

     1. Introduction(บทนำ)
     2. Getting started(เริ่มต้น)
     3. Sizing a Message Buffer (การปรับขนาด a Message Buffer)
     4. Blocking Reads and Writes (การปิดกั้นการอ่านและการเขียน)
     5. Send and Receive Complete Macros (for multicore use) (ส่งและรับมาโครที่สมบูรณ์ (สำหรับการใช้งานมัลติคอร์))

Introduction

Message buffers ยอมให้ variable length discrete messages ถูกส่งผ่านจาก an interrupt service routine ไปยัง a task, หรือจาก task หนึ่งไปยัง task อื่น.  ตัวอย่างเช่น, messages ที่มีความยาว 10, 20 and 123 bytes ทั้งหมดสามารถถูกเขียนไปยัง, และถูกอ่านจาก, the same message buffer. ต่างจากเมื่อใช้ a stream buffer, a 10 byte message สามารถเฉพาะถูกอ่านออกเป็น a 10 byte message, ไม่ใช่เป็นราย bytes ได้.  Message buffers ถูกสร้างบนส่วนบนของ stream buffers (นั่นคือ, พวกมันใช้ the stream buffer implementation).

Data ถูกส่งผ่านผ่านทาง a message buffer โดยการคัดลอก - the data ถูกคัดลอกลงใน the buffer โดย the sender และออกจาก the buffer โดยการอ่าน.

โปรดดู the definition of the configMESSAGE_BUFFER_LENGTH_TYPE configuration parameter.

ไม่เหมือนกับ FreeRTOS communications primitives อื่นส่วนใหญ่, stream buffers, และดังนั้น message buffers อีกด้วย, ได้รับการปรับให้เหมาะสมสำหรับสถานการณ์ single reader single writer, อย่างเช่นส่งผ่าน data จาก an interrupt service routine ไปยัง a task, หรือจาก one microcontroller core ไปยังคอร์อื่นบน a dual core CPU.

Message buffer functionality ถูกเปิดการใช้งานโดยการ include the FreeRTOS/source/stream_buffer.c source file ในการ build.

The message buffer implementation ใช้ direct to task notifications. ดังนั้น, การเรียกใช้ a message buffer API function ที่วาง the calling task ลงใน the Blocked state สามารถเปลี่ยน the calling task's notification state and value.

หมายเหตุสำคัญ: มีลักษณะเฉพาะในหมู่ FreeRTOS objects, the stream buffer implementation (ดังนั้น the message buffer implementation อีกด้วย, ดังที่ message buffers ถูกสร้างบนส่วนบนสุดของ stream buffers) ถือว่ามีเพียง task เดียวหรือ interrupt เดียวที่จะเขียนไปยัง the buffer (the writer), และมีเพียง task เดียวหรือ interrupt เดียวที่จะอ่านจาก the buffer (the reader).  มันจะปลอดภัยสำหรับ the writer and reader ที่เป็น tasks หรือ interrupts ที่แตกต่างกัน, แต่, ต่างจาก FreeRTOS objects อื่นๆ, มันไม่ปลอดภัยที่มีหลาย writers ที่แตกต่างกันหรือหลาย readers ที่แตกต่างกัน. ถ้ามีเป็น multiple different writers ดังนั้น the application writer ต้องวางแต่ละการเรียกใช้ถึง a writing API function (อย่างเช่น xStreamBufferSend()) ภายใน a critical section และใช้ a send block time เป็น 0. ในทำนองเดียวกัน, ถ้ามีเป็น multiple different readers ดังนั้น the application writer ต้องวางแต่ละการเรียกใช้ถึง a reading API function (อย่างเช่น xStreamBufferReceive()) ภายใน a critical section และใช้ a receive block time เป็น 0.
Title: Re: FreeRTOS
Post by: tha on October 21, 2021, 10:33:54 AM
Getting Started

The FreeRTOS/Demo/Common/Minimal/MessageBufferAMP.c source file จัดให้มี a heavily commented example เกี่ยวกับวิธีใช้ a message buffer เพื่อส่งผ่าน variable length data จาก one MCU core ไปยังอีกคอร์หนึ่งบน a multi-core MCU. นั่นเป็นสถานการณ์ขั้นสูงทีเดียว, แต่ the mechanism สำหรับ creating, sending to and receiving จาก the message buffer เป็นเหมือนกับใน simpler single core scenarios ที่, ไม่เหมือนใน the demo, มันไม่มีความจำเป็นต้องข้าม the sbSEND_COMPLETE() macros.

ดู the message buffer section ของ the user documentation สำหรับรายการของ message buffer related API functions, ในหลายกรณีรวมถึง code snippets ที่สาธิต the functions ที่กำลังถูกใช้.
Title: Re: FreeRTOS
Post by: tha on October 21, 2021, 10:52:09 AM
Sizing a Message Buffer

เพื่อเปิดการใช้งาน message buffers เพื่อจัดการ variable sized messages ความยาวของแต่ละ message จะถูกเขียนลงใน the message buffer ก่อน the message ของมันเอง (ที่เกิดขึ้นภายในด้วย the FreeRTOS API functions). ความยาวจะถูกเก็บใน, ชนิดที่ซึ่งถูกเซ็ตโดย the configMESSAGE_BUFFER_LENGTH_TYPE constant ใน FreeRTOSConfig.h. configMESSAGE_BUFFER_LENGTH_TYPE  ค่าเริ่มต้นจะเป็นประเภท size_t หากไม่ได้กำหนดไว้. size_t โดยทั่วไปคือ 4-bytes บน a 32-bit architecture. ดังนั้น, ตัวอย่างเช่น, การเขียน a 10 byte message ลงใน a message buffer จะใช้จริงๆ 14 bytes of buffer space เมื่อ configMESSAGE_BUFFER_LENGTH_TYPE คือ 4-bytes. ในทำนองเดียวกัน การเขียน a 100 byte message ลงใน a message buffer จะเก็บจริงๆ 104 bytes of buffer space.
Title: Re: FreeRTOS
Post by: tha on October 21, 2021, 11:12:38 AM
Blocking Reads and Writes

xMessageBufferReceive()) ถูกใช้เพื่ออ่าน data จาก a message buffer จาก an RTOS task. xMessageBufferReceiveFromISR()) ถูกใช้เพื่ออ่าน data จาก a message buffer จาก an interrupt service routine (ISR). xMessageBufferSend()) ถูกใช้เพื่อส่ง data ไปยัง a message buffer จาก an RTOS task. xMessageBufferSendFromISR()) ถูกใช้เพื่อส่ง data ไปยัง a message buffer จาก an interrupt service routine (ISR).

ถ้า a non zero block time ถูกระบุเมื่อ a task ใช้ xMessageBufferReceive() เพื่ออ่านจาก a message buffer ที่เกิดว่างเปล่า the task จะถูกวางลงใน the Blocked state (ดังนั้นมันจะไม่กิน CPU time ใดๆและ tasks อื่นสามารถ run) จนกระทั่งอย่างใดอย่างหนึ่ง data กลายมาเป็นมีให้ใช้ประโยชน์ใน the message buffer, หรือ the block time หมดเวลาลง.

ถ้า a non zero block time ถูกระบุเมื่อ a task ใช้ xMessageBufferSend() เพื่อเขียน a message buffer ที่เกิดเติม the task จะถูกวางลงใน the Blocked state (ดังนั้นมันจะไม่กิน CPU time ใดๆและ tasks อื่นสามารถ run) จนกระทั่งอย่างใดอย่างหนึ่ง space กลายมาเป็นมีให้ใช้ประโยชน์ใน the message buffer, หรือ the block time หมดเวลาลง.

Send Complete and Receive Complete Macros

เนื่องจาก message buffers ถูกสร้างบน stream buffers the sbSEND_COMPLETE() and sbRECEIVE_COMPLETE() macros จะทำงานตรงตามที่อธิบายไว้ในหน้าที่อธิบาย stream buffers.
Title: Re: FreeRTOS
Post by: tha on October 22, 2021, 07:00:31 AM
https://www.freertos.org/RTOS-software-timer.html (https://www.freertos.org/RTOS-software-timer.html)

Software Timers

จากหน้านี้:

     •  Software timers in a nutshell
     •  Efficiency considerations in software timer implementations
     •  Important information on writing timer callback functions
     •  The timer service/daemon task, and the timer command queue
     •  Configuring an application to use software timers
     •  One-shot timers versus auto-reload timers
     •  Resetting a software timer
     •  API

Software timers in a nutshell (โดยสังเขป)

A software timer (หรือเพียง a 'timer') ยอมให้ a function ถูกปฏิบัติตามเวลาที่ตั้งค่าไว้ในอนาคต. The function ที่ปฏิบัติโดย the timer ถูกเรียกว่า the timer's callback function. เวลาระหว่าง a timer กำลังถูกสตาร์ท, และ callback function ของมันที่กำลังถูกปฏิบัติ, ถูกเรียกว่า the timer's period. พูดง่ายๆ, the timer's callback function ถูกปฏิบัติเมื่อ the timer's period หมดเวลาลง.

หมายเหตุ, a software timer ต้องถูกสร้างอย่างชัดเจนก่อนมันสามารถถูกใช้.

Efficiency considerations in software timer implementations (ข้อควรพิจารณาด้านประสิทธิภาพใน software timer implementations)

Software timer functionality ใช้งานง่าย, แต่ยากต่อการใช้งานอย่างมีประสิทธิภาพ. The FreeRTOS implementation ไม่ปฏิบัติ timer callback functions จาก an interrupt context, ไม่กิน processing time ใดๆเว้นแต่ a timer หมดเวลาลงไปแล้วจริงๆ, ไม่เพิ่ม processing overhead ใดๆไปยัง the tick interrupt, และไม่ดำเนินการ link list structures ใดๆในขณะที่ interrupts ถูกปิดการใช้งาน.

The timer service task (โดยพื้นฐานแล้ว) ใช้ประโยชน์จากคุณสมบัติ FreeRTOS ที่มีอยู่, ยอมให้ timer functionality ถูกเพิ่มไปยัง an application โดยมีผลกระทบน้อยที่สุดต่อขนาดของไบนารีที่ปฏิบัติการได้ของแอปพลิเคชัน.

Important information on writing timer callback functions

Timer callback functions ปฏิบัติใน the context of the timer service task. ดังนั้นจึงจำเป็นที่ timer callback functions จะไม่พยายามบล็อก. ตัวอย่างเช่น, a timer callback function ต้องไม่เรียกใช้ vTaskDelay(), vTaskDelayUntil(), หรือระบุ a non zero block time เมื่อเข้าถึง a queue หรือ a semaphore.
Title: Re: FreeRTOS
Post by: tha on October 22, 2021, 08:29:48 AM
https://www.freertos.org/RTOS-software-timer-service-daemon-task.html (https://www.freertos.org/RTOS-software-timer-service-daemon-task.html)

Software Timers
[More about software timers...]

See also the RTOS Deamon Task Startup Hook function.

The timer service/daemon task, and the timer command queue

Timer functionality เป็นทางเลือก, และไม่เป็นส่วนหนึ่งของ the core FreeRTOS kernel. มันแทนที่จัดให้มีโดย a timer service (หรือ daemon) task.

FreeRTOS จัดให้มี a set of timer related API functions. จำนวนมากของ functions เหล่านี้ใช้ a standard FreeRTOS queue เพื่อส่ง commands ไปยัง the timer service task. The queue ที่ใช้สำหรับจุดประสงค์นี้ถูกเรียกว่า the 'timer command queue'. The 'timer command queue' เป็นส่วนตัวถึง the FreeRTOS timer implementation, และไม่สามารถถูกเข้าถึงโดยตรง.

The diagram ด้านล่างแสดงสถานะการณ์นี้. The code บนด้านซ้ายแสดง a function ที่เป็นส่วนหนึ่งของ a user application, และถูกเรียกจาก a task ที่ถูกสร้างเป็นส่วนหนึ่งของ the same user application. The code บนด้านขวาแสดง the timer service task implementation.  The timer command queue เป็นตัวลิ้งค์ระหว่าง the application task และ the timer service task. ในกรณีตัวอย่างนี้, the xTimerReset() API function ถูกเรียกจาก the application code. ซึ่งส่งผลให้ a reset command กำลังถูกส่งไปยัง the timer command queue สำหรับการประมวลผลโดย the timer service task. The application code เพียงเรียกใช้ the xTimerReset() API function - มันไม่ (และไม่สามารถ) เข้าถึง the timer command queue โดยตรง.

                                  (https://i.imgur.com/TTb8bzV.png)
Title: Re: FreeRTOS
Post by: tha on October 22, 2021, 11:04:42 AM
https://www.freertos.org/Configuring-a-real-time-RTOS-application-to-use-software-timers.html (https://www.freertos.org/Configuring-a-real-time-RTOS-application-to-use-software-timers.html)

Software Timers
[More about software timers...]

Configuring an application to use software timers

เพื่อทำ the FreeRTOS software timer API ให้ใช้เป็นประโยชน์ใน an application, ง่ายๆ:

     1. เพิ่ม the FreeRTOS/Source/timers.c source file ไปยัง your project, และ
     2. กำหนด the constants ที่ให้รายละเอียดในตารางข้างล่างใน the applications FreeRTOSConfig.h header file

           Constant                                                                                      Description

configUSE_TIMERS                            เซ็ตเป็น 1 เพื่อ include timer functionality. The timer service task จะถูกสร้างโดยอัตโนมัติเป็น the RTOS
                                                         scheduler สตาร์ทเมื่อ configUSE_TIMERS ถูกเซ็ตเป็น 1.

configTIMER_TASK_PRIORITY              เซ็ต the priority of the timer service task. เช่นเดียวกับทุก tasks, the timer service task สามารถรันที่
                                                         priority ใดๆระหว่าง 0 และ ( configMAX_PRIORITIES - 1 ).

                                                         ค่านี้จำเป็ต้องถูกเลือกอย่างรอบคอบเพื่อให้เป็นไปตามความต้องการของ the application. ตัวอย่างเช่น, ถ้า the timer
                                                         service task ถูกทำเป็น the highest priority task ใน the system, ดังนั้น commands ส่งไปยัง the timer
                                                         service task (เมื่อ a timer API function ถูกเรียก) และ timers ที่หมดเวลาลงทั้งคู่จะได้รับการประมวลผลโดยทันที.
                                                         ในทางกลับกัน, ถ้า the timer service task ถูกให้เป็น a low priority, ดังนั้น commands ส่งไปยัง the timer
                                                         service task และ expired timers จะไม่ได้รับการประมวลผลจนกระทั่ง the timer service task เป็น the highest
                                                         priority task ที่สามารถรันได้. อย่างไรก็ตาม เป็นที่น่าสังเกต, ว่า timer expiry times ถูกคำนวณสัมพันธ์กับเวลาที่ a
                                                         command ถูกส่ง, และไม่สัมพันธ์กับเวลาที่ a command ถูกประมวลผล.

configTIMER_QUEUE_LENGTH              นี้ตั้งค่าจำนวนสูงสุดของ commands ที่ไม่ได้ประมวลผลที่ the timer command queue สามารถถือเอาไว้ในแต่ละครั้ง.

                                                         สาเหตุ the timer command queue อาจเติมให้เต็มรวมถึง :

                                                              •  ทำการเรียกใช้ timer API function หลายครั้งก่อน the RTOS scheduler ถูกสตาร์ท, และดังนั้นก่อน the
                                                                  timer service task ถูกสร้าง.
                                                              • ทำการเรียกใช้ (interrupt safe) timer API function หลายครั้งจาก an interrupt service routine (ISR).
                                                              • ทำการเรียกใช้ timer API function หลายครั้งจาก a task ที่มี a priority เหนือ the timer service task.

configTIMER_TASK_STACK_DEPTH   ตั้งค่าขนาดของ the stack (ใน words, ไม่ bytes) ที่จัดสรรไปยัง the timer service task.

                                                        Timer callback functions ปฏิบัติใน the context of the timer service task. The stack requirement of the
                                                        timer service task ดังนั้นขึ้นอยู่กับ the stack requirements of the timer callback functions.
Title: Re: FreeRTOS
Post by: tha on October 22, 2021, 02:01:40 PM
https://www.freertos.org/One-shot-Vs-auto-reload-real-time-software-timers.html (https://www.freertos.org/One-shot-Vs-auto-reload-real-time-software-timers.html)

Software Timers
[More about software timers...]

One-shot timers versus auto-reload timers

มีสองประเภทของ timer, one-shot timers, และ auto-reload timers. เมื่อถูกสตาร์ท, a one-shot timer จะปฏิบัติ callback function ของมันเพียงครั้งเดียว. มันสามารถถูกรีสตาร์ทแบบแมนน้วลได้, แต่จะไม่รีสาตร์ทตัวมันแบบอัตโนมัติ. ในทางกลับกัน, เมื่อถูกสตาร์ท, an auto-reload timer จะรีสาตร์ทตัวมันแบบอัตโนมัติหลังจากแต่ละการปฏิบัติการของ callback function ของมัน, เป็นผลให้ periodic callback execution.

ความแตกต่างของพฤติกรรมระหว่าง a one-shot timer และ an auto-reload timer ถูกแสดงโดย the timeline ใน the diagram ข้างล่าง. ใน diagram นี้, Timer 1 เป็น a one-shot timer ที่มีคาบเวลาเท่ากับ 100, และ Timer 2 เป็น an auto-reload timer ที่มีคาบเวลาเท่ากับ 200.

                         (https://i.imgur.com/f3rMLVW.png)
Title: Re: FreeRTOS
Post by: tha on October 22, 2021, 02:24:05 PM
https://www.freertos.org/Resetting-an-RTOS-software-timer.html (https://www.freertos.org/Resetting-an-RTOS-software-timer.html)

Software Timers
[More about software timers...]

Resetting a software timer

มันสามารถเป็นได้ที่จะรีเซ็ต a timer ที่ถูกสตาร์ทให้รันแล้ว. การรีเซ็ต a timer ส่งผลให้ the timer คำนวณใหม่ expiry time ของมันดังนั้น the expiry time กลายมาเป็นสัมพันธ์กับเมื่อ the timer ถูกรีเซ็ตแล้ว, และไม่เมื่อ the timer ถูกสตาร์ทแต่เดิม. พฤติกรรมนี้ถูกแสดงใน the next diagram, ที่ Timer 1 คือ a one-shot timer ที่มีคาบเวลาเท่ากับ 5 seconds.

ในตัวอย่างที่แสดงให้เห็น, ถือว่า the application เปิดไฟ an LCD back-light เมื่อ a key ถูกกด, และ the back-light จะยังคงเปิดอยู่จนกระทั่ง 5 seconds ผ่านไปโดยไม่มี keys ใดๆถูกกดเลย. Timer 1 ถูกใช้ปิดไฟ the LCD back-light เมื่อ 5 seconds นี้ผ่านไป.

                   (https://i.imgur.com/1vvPcYp.png)
Title: Re: FreeRTOS
Post by: tha on October 23, 2021, 05:53:05 AM
https://www.freertos.org/FreeRTOS-Event-Groups.html (https://www.freertos.org/FreeRTOS-Event-Groups.html)

Event Bits (or flags) and Event Groups
[Inter-task communication and synchronisation]

                          TIP: 'Task Notifications' สามารถจัดให้มี a light weight alternative ให้กับ event groups ในหลายสถานะการณ์

บนหน้านี้:

     •  Event Bits (Event Flags)
     •  Event Groups
     •  Event Group and Event Bits Data Types
     •  Event Group RTOS API Functions
     •  ความท้าทายที่ RTOS ต้องเอาชนะเมื่อใช้งาน Event Groups
     •  Example code

Event Bits (Event Flags)

Event bits ถูกใช้แสดงถ้า an event เกิดขึ้นแล้วหรือไม่. Event bits มักจะถูกอ้างถึงเป็น event flags. ตัวอย่างเช่น, an application อาจ:

     •  กำหนด a bit (หรือ flag) ที่หมายถึง "A message ถูกรับแล้วและพร้อมสำหรับการประมวลผล" เมื่อมันถูกเซ็ตเป็น 1, และ "ไม่มี messages รอให้ถูกประมวลผล"
         เมื่อมันถูกเซ็ตเป็น 0.

     •  กำหนด a bit (หรือ flag) ที่หมายถึง "The application ได้จัดคิว a message ที่พร้อมจะถูกส่งไปยัง a network" เมื่อมันถูกเซ็ตเป็น 1, และ "ไม่มี messages
         ถูกจัดคิวพร้อมจะถูกส่งไปยัง the network" เมื่อมันถูกเซ็ตเป็น 0.

     •  กำหนด a bit (หรือ flag) ที่หมายถึง "ถึงเวลาที่จะส่ง a heartbeat message ไปยัง a network" เมื่อมันถูกเซ็ตเป็น 1, และ "ยังไม่ถึงเวลาที่จะส่ง another
         heartbeat message" เมื่อมันถูกเซ็ตเป็น 0.

Event Groups

An event group คือ a set of event bits. Individual event bits ภายใน an event group ถูกอ้างอิงโดย a bit number. การขยาย the example ถูกจัดให้มีข้างบน:

     •  The event bit ที่หมายถึง "A message ถูกรับแล้วและพร้อมสำหรับการประมวลผล" อาจเป็น bit number 0 ภายใน an event group

     •  The event bit ที่หมายถึง "The application ได้จัดคิว a message ที่พร้อมจะถูกส่งไปยัง a network" อาจเป็น bit number 1 ภายใน the same event
         group.

     •  The event bit ที่หมายถึง "ถึงเวลาที่จะส่ง a heartbeat message ไปยัง a network" อาจเป็น bit number 2 ภายใน the same event group.

Event Group and Event Bits Data Types

Event groups ถูกอ้างอิงโดยตัวแปรประเภท EventGroupHandle_t.

จำนวนของ bits (หรือ flags) ที่เก็บภายใน an event group เป็น 8 ถ้า configUSE_16_BIT_TICKS ถูกเซ็ตเป็น 1, หรือ 24 ถ้า configUSE_16_BIT_TICKS ถูกเซ็ตเป็น 0. การขึ้นอยู่กับ configUSE_16_BIT_TICKS เป็นผลจาก the data type ที่ใช้สำหรับ thread local storage ใน the internal implementation of tasks.

ทุก the event bits ใน an event group ถูกเก็บใน a single unsigned variable of type EventBits_t. Event bit 0 ถูกเก็บใน bit position 0, event bit 1 ถูกเก็บใน bit position 1, และต่อไป.

The image ข้างล่างแสดง a 24-bit event group ที่ใช้ three bits เพื่อถือ the three example events ที่ได้อธิบายแล้ว. ใน the image มีเพียง event bit 2 ที่ถูกเซ็ต.

                         (https://i.imgur.com/jmY32Mo.png)

Event Group RTOS API Functions

Event group API functions ถูกจัดให้มีที่ยอมให้ a task ทำการ, ในหมู่สิ่งอื่น, เซ็ต event bits หนึ่งหรือมากกว่าภายใน an event group, เคลียร์ event bits หนึ่งหรือมากกว่าภายใน an event group, และค้างไว้ (เข้าสู่ the Blocked state ดังนั้น the task จะไม่กิน any processing time) เพื่อรอสำหรับชุดหนึ่งของ event bits หนึ่งหรือมากกว่ากลายมาเป็นเซ็ตภายใน an event group.

Event groups ยังสามารถถูกใช้เพื่อซิงโครไนซ์ tasks, สร้างสิ่งที่มักจะถูกอ้างถึงเป็น a task 'นัดพบ'. A task synchronisation point เป็นตำแหน่งใน application code ที่ซึ่ง a task จะรอใน the Blocked state (ไม่กิน any CPU time) จนกระทั่ง tasks อื่นทั้งหมดที่มีส่วนร่วมใน the synchronisation ถึง synchronisation point ของพวกมันด้วย.
Title: Re: FreeRTOS
Post by: tha on October 23, 2021, 09:17:01 AM
The Challenges an RTOS Must Overcome When Implementing Event Groups

ความท้าทายหลักสองประการที่ RTOS ต้องเอาชนะเมื่อจัดให้มีใช้ Event Groups คือ :

     1. หลีกเลี่ยงการสร้างเงื่อนไขการแข่งขันใน the user's application

         An event group implementation จะสร้างเงื่อนไขการแข่งขันใน the application ถ้า:
 
           •  ไม่ชัดเจนว่าใครเป็นผู้รับผิดชอบในการล้าง bits แต่ละบิต (หรือ flags).

           •  ไม่ชัดเจนเมื่อใด a bit จะถูกเคลียร์.

           •  ไม่ชัดเจนถ้า a bit ถูกเซ็ตหรือเคลียร์แล้วในเวลาที่ a task ออกจาก the API function ที่ทดสอบ the bit's value (อาจเป็นเพราะ another task หรือ
               interrupt ได้เปลี่ยน the bit's state มาแล้ว).

         The FreeRTOS event groups implementation ขจัดความสามารถเป็นได้ของ race conditions โดยการรวม built in intelligence เพื่อให้แน่ใจว่า
         setting, testing and clearing of bits ปรากฎเป็น atomic. Thread local storage และการใช้อย่างระมัดระวังของ API function return values ทำให้สิ่ง
         นี้เป็นไปได้.

      2. Avoiding non-determinism(หลีกเลี่ยงการไม่กำหนดไว้)

          The event group concept แสดงถึง non-deterministic behaviour เพราะว่ามันไม่รู้ว่า tasks จำนวนเท่าใดที่ถูกบล็อกบน an event group, และดังนั้นมัน
          ไม่รู้ว่า conditions จำนวนเท่าใดที่จะจำเป็นต้องถูกทดสอบหรือ tasks ถูกปลดบล็อกเมื่อ an event bit ถูกเซ็ต.

         The FreeRTOS quality standards ไม่ยอมให้ non-deterministic actions ถูกดำเนินการในขณะที่ interrupts ถูกปิดการใช้งาน, หรือจากภายใน interrupt
         service routines. เพื่อให้แน่ใจว่า these strict quality standards ไม่ถูกละเมิดเมื่อ an event bit ถูกเซ็ต:

            •  The RTOS scheduler's locking mechanism ถูกใช้เพื่อให้แน่ใจว่า interrupts ยังคงถูกเปิดการใช้งานเมื่อ an event bit ถูกเซ็ตจาก an RTOS
                task.

            •  The centralised deferred interrupt mechanism ถูกใช้เพื่อเลื่อน the action of setting a bit ไปยัง a task เมื่อความพยายามถูกทำเพื่อเซ็ต an
                event bit จาก an interrupt service routine.

Example Code

Example code snippets ถูกจัดให้มีใน the API documentation, and a comprehensive example ถูกจัดให้มีใน the EventGroupsDemo.c ชุดของ standard demo tasks (the source file ถูกวางตำแหน่งใน the FreeRTOS/Demo/Common/Minimal directory of the main FreeRTOS download).
Title: Re: FreeRTOS
Post by: tha on October 23, 2021, 10:49:54 AM
https://www.freertos.org/a00017.html (https://www.freertos.org/a00017.html)

Source Organization

Every RTOS port มาพร้อมกับ a pre-configured demo application ที่สร้าง the necessary RTOS source files แล้ว, และ includes the necessary RTOS header files. ขอแนะนำอย่างยิ่งให้ the provided demos ถุกใช้เป็นพื้นฐานสำหรับ all new FreeRTOS based applications. หน้านี้ถูกจัดให้มีเพื่อช่วยในการหาตำแหน่งและเข้าใจ the provided projects.

Basic directory structure

The FreeRTOS download รวมเอา source code สำหรับทุก processor port, และทุก demo application. การวาง the ports ทั้งหมดใน a single download ง่ายต่อการแจกจ่ายอย่างมาก, แต่จำนวนของไฟล์อาจดูน่ากลัว. The directory structure นั้นง่ายมาก, และ the FreeRTOS real time kernel ถูกบรรจุในเพียง 3 files (ไฟล์เพิ่มเติมถูกต้องการถ้า software timer, event group หรือ co-routine functionality ถูกต้องการ).

จากด้านบนสุด, the download ถูกแบ่งออกเป็นสอง sub directories; FreeRTOS, and FreeRTOS-Plus. เหล่านี้แสดงไว้ข้างล่าง:

(https://i.imgur.com/3dXK1pR.png)

The FreeRTOS-Plus directory tree บรรจุหลาย readme files ที่อธิบายสิ่งที่บรรจุภายในของมัน.
Title: Re: FreeRTOS
Post by: tha on October 23, 2021, 12:03:55 PM
FreeRTOS kernel directory structure

The core FreeRTOS kernel source files และ demo projects ถูกบรรจุใน two sub directories ดังแสดงข้างล่าง:

(https://i.imgur.com/c7wstE8.png)

The core RTOS code ถูกบรรจุในสามไฟล์, ซึ่งถูกเรียกว่า tasks.c, queue.c และ list.c. สามไฟล์เหล่านี้อยู่ใน the FreeRTOS/Source directory. The same directory บรรจุไฟล์ทางเลือกสองไฟล์ที่เรียกว่า timers.c และ croutine.c ซึ่งจัดให้มีใช้ software timer และ co-routine functionality ตามลำดับ.

แต่ละ supported processor architecture ต้องการ a small amount of architecture specific RTOS code. นี้คือ the RTOS portable layer, และมันถูกวางตำแหน่งใน the FreeRTOS/Source/Portable/[compiler]/[architecture] sub directories, เมื่อ [compiler] and [architecture] คือ the compiler ที่ใช้สร้าง the port, และ the architecture ที่ซึ่ง the port รัน, ตามลำดับ.

ด้วยเหตุผลที่กล่าวไว้บน the memory management page, the sample heap allocation schemes ถูกวางตำแหน่งใน the portable layer ด้วย. The various sample heap_x.c files ถูกวางตำแหน่งใน the FreeRTOS/Source/portable/MemMang directory.
Title: Re: FreeRTOS
Post by: tha on October 23, 2021, 01:34:19 PM
ตัวอย่างของ portable layer directories:

     •  ถ้าใช้ the TriCore 1782 port ด้วย the GCC compiler:

        The TriCore specific file (port.c) อยู่ใน the FreeRTOS/Source/Portable/GCC/TriCore_1782 directory.
        FreeRTOS/Source/Portable sub directories อื่นทั้งหมด, นอกเหนือจาก FreeRTOS/Source/Portable/MemMang, สามารถถูกเพิกเฉยหรือลบออก.

     •  ถ้าใช้ the Renesas RX600 port ด้วย the IAR compiler:

        The RX600 specific file (port.c) อยู่ใน the FreeRTOS/Source/Portable/IAR/RX600 directory. FreeRTOS/Source/Portable sub
        directories อื่นทั้งหมด, นอกเหนือจาก FreeRTOS/Source/Portable/MemMang, สามารถถูกเพิกเฉยหรือลบออก.

     •  และต่อไปสำหรับทุก the ports ...

The structure of the FreeRTOS/Source directory ถูกแสดงไว้ข้างล่าง.

(https://i.imgur.com/cQQpWGj.png)
Title: Re: FreeRTOS
Post by: tha on October 23, 2021, 02:06:08 PM
The FreeRTOS download ยังบรรจุ a demo application สำหรับ every processor architecture และ compiler port. ส่วนใหญ่ของ the demo application code ที่ใช้ร่วมกับ ports ทั้งหมดและถูกบรรจุใน the FreeRTOS/Demo/Common/Minimal directory (the code ที่วางตำแหน่งใน the FreeRTOS/Demo/Common/Full directory เป็นแบบเดิม, และถูกใช้โดย the PC port เท่านั้น).

The remaining FreeRTOS/Demo sub directories บรรจุ pre-configured projects ที่ใช้บิ้วด์ individual demo applications. The directories ถูกให้ชื่อเพื่อแสดง the port ที่พวกมันเกี่ยวข้อง. แต่ละ RTOS port ยังมี its own web page ที่ให้รายละเอียด the directory ที่ซึ่ง the demo application สำหรับ port นั้นสามารถถูกพบ.

Examples of demo directories:

     •  ถ้าบิ้วด์ the TriCore GCC demo application ที่เป้าหมายคือ the Infineon TriBoard hardware

        The TriCore demo application project file ถูกวางตำแหน่งใน the FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC directory. the other sub
         directories ทั้งหมดที่บรรจุใน the FreeRTOS/Demo directory (นอกเหนือจาก the Common directory) สามารถถูกเพิกเฉยหรือลบออก.

     •  ถ้าบิ้วด์ the Renesas RX6000 IAR demo application ที่เป้าหมายคือ the RX62N RDK hardware:

        The IAR workspace file ถูกวางตำแหน่งใน the FreeRTOS/Demo/RX600_RX62N-RDK_IAR directory. the other sub directories ทั้งหมดที่บรรจุ
        ใน the FreeRTOS/Demo directory (นอกเหนือจาก the Common directory) สามารถถูกเพิกเฉยหรือลบออก.

     •  และต่อไปสำหรับทุก the ports ...

The structure of the FreeRTOS/Demo directory ถูกแสดงไว้ข้างล่าง.

(https://i.imgur.com/pNajB6e.png)
Title: Re: FreeRTOS
Post by: tha on October 23, 2021, 02:49:00 PM
Creating your own application
[รายละเอียดที่มากขึ้นถูกจัดให้มีบน the Creating a New FreeRTOS Application page]

The pre-configured demo applications ถูกแจกจ่ายเพื่อให้แน่ใจว่า projects มีอยู่แล้วพร้อมกับ the correct RTOS kernel source files ถูกรวม, และ the correct compiler options ที่เซ็ต, และดังนั้น build ด้วยความพยายามของผู้ใช้ที่น้อยมากๆ.  ดังนั้นขอแนะนำเป็นอย่างยิ่งให้ new applications ถูกสร้างโดยการแก้ไข an existing pre-configured demo application. นี้สามารถถูกทำโดยอย่างแรกคือการบิ้วด์ an existing demo application เพื่อให้แน่ใจว่า a clean build สามารถถูกทำให้สำเร็จ, และจากนั้นเพิ่มการแทนที่ the files ที่รวมใน the project จาก the FreeRTOS/Demo directory ด้วย your own application source files.
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 04:36:39 AM
https://www.freertos.org/a00110.html (https://www.freertos.org/a00110.html)

Customisation
[Configuration]

FreeRTOS ถูกปรับแต่งโดยใช้ a configuration file ที่เรียกว่า FreeRTOSConfig.h. ทุก FreeRTOS application ต้องมี a FreeRTOSConfig.h header file ใน its pre-processor include path. FreeRTOSConfig.h ปรับแต่ง the RTOS kernel ให้เข้ากับ the application ที่กำลังถูก built. ดังนั้นจึงเป็นข้อมูลเฉพาะสำหรับ the application, ไม่ใช่ the RTOS, และควรถูกวางตำแหน่งใน an application directory, ไม่ในหนึ่งใดของ the RTOS kernel source code directories.

แต่ละ demo application ที่ถูกรวมใน the RTOS source code download มี its own FreeRTOSConfig.h file. บางรายการของ the demos ค่อนข้างเก่าและไม่บรรจุ the available configuration options ทั้งหมด. Configuration options ที่ถูกละเว้นจะถูกตั้งค่าเป็น a default value ภายใน an RTOS source file.

นี่คือ a typical FreeRTOSConfig.h definition, ตามด้วยคำอธิบายของแต่ละ parameter:
(ตัวอย่างโค้ดดูในลิ้งค์เอานะครับ)
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 05:22:29 AM
'config' Parameters

configUSE_PREEMPTION

ตั้งค่าเป็น 1 เพื่อใช้ the preemptive RTOS scheduler, หรือ 0 เพื่อใช้ the cooperative RTOS scheduler.

configUSE_PORT_OPTIMISED_TASK_SELECTION

บาง FreeRTOS ports มีสองวิธีของการเลือก the next task เพื่อปฏิบัติ - วิธีทั่วไป, และ a method ที่เฉพาะเจาะจงไปยัง port นั้น.

The Generic method:

     •  ถูกใช้เมื่อ configUSE_PORT_OPTIMISED_TASK_SELECTION ถูกเซ็ตเป็น 0, หรือเมื่อ a port specific method ไม่ได้ถูกจัดให้มีใช้.

     •  สามารถถูกใช้กับ all FreeRTOS ports

     •  ถูกเขียนอย่างสมบูรณ์ในภาษา C, ทำให้มันมีประสิทธิ์ภาพน้อยกว่า a port specific method.

     •  ไม่กำหนดขีดจำกัดของ the maximum number of available priorities

A port specific method:

     •  ไม่มีให้ใช้ประโยชน์สำหรับ ports ทั้งหมด

     •  ถูกใช้เมื่อ configUSE_PORT_OPTIMISED_TASK_SELECTION ถูกเซ็ตเป็น 1.

     •  อาศัยอย่างน้อยหนึ่ง architecture specific assembly instructions (โดยทั่วไปคือ a Count Leading Zeros [CLZ] หรือ equivalent instruction) ดังนั้น
        สามารถถูกใช้เฉพาะกับ the architecture ซึ่งมันถูกเขียนไว้โดยเฉพาะเท่านั้น.

     •  มีประสิทธิภาพมากกว่า the generic method

     •  โดยทั่วไปจะกำหนดขีดจำกัดไว้ที่ 32 สำหรับ the maximum number of available priorities.
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 05:59:16 AM
configUSE_TICKLESS_IDLE

ตั้งค่า configUSE_TICKLESS_IDLE เป็น 1 เพื่อใช้ the low power tickless mode, หรือ 0 เพื่อให้ the tick interrupt รันอยู่ตลอดเวลา. Low power tickless implementations ไม่ถูกจัดให้มีสำหรับ all FreeRTOS ports.

configUSE_IDLE_HOOK

ตั้งค่าเป็น 1 ถ้าคุณต้องการใช้ an idle hook, หรือ 0 เพื่อละเว้น an idle hook.

configUSE_MALLOC_FAILED_HOOK

The kernel ใช้การเรียกไปยัง pvPortMalloc() เพื่อจัดสรร memory จาก the heap แต่ละครั้งที่ a task, queue or semaphore ถูกสร้างขึ้น. The official FreeRTOS download รวมเอา four sample memory allocation schemes สำหรับวัตถุประสงค์นี้. The schemes ถูกจัดให้มีใน the heap_1.c, heap_2.c, heap_3.c, heap_4.c and heap_5.c source files ตามลำดับ. configUSE_MALLOC_FAILED_HOOK จะเกี่ยวข้องก็ต่อเมื่อหนึ่งใน these three sample schemes กำลังถูกใช้.

The malloc() failed hook function คือ a hook (or callback) function ที่, ถ้ากำหนดและกำหนดค่าไว้, จะถูกเรียกใช้ถ้า pvPortMalloc() เคยรีเทิร์น NULL. NULL จะถูกรีเทิร์นเฉพาะถ้ามี FreeRTOS heap memory ที่เหลืออยู่ไม่เพียงพอสำหรับ the requested allocation ให้สำเร็จ.

ถ้า configUSE_MALLOC_FAILED_HOOK ถูกเซ็ตเป็น 1 ดังนั้น the application ต้องกำหนด a malloc() failed hook function. ถ้า configUSE_MALLOC_FAILED_HOOK ถูกเซ็ตเป็น 0 ดังนั้น the malloc() failed hook function จะไม่ถูกเรียกใช้, แม้ว่าจะมีการกำหนดไว้ก็ตาม. Malloc() failed hook functions ต้องมี the name and prototype ตามแสดงข้างล่าง.

void vApplicationMallocFailedHook( void );
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 08:12:03 AM
configUSE_DAEMON_TASK_STARTUP_HOOK

ถ้า configUSE_TIMERS และ configUSE_DAEMON_TASK_STARTUP_HOOK ทั้งคู่ถูกเซ็ตเป็น 1 ดังนั้น the application ต้องกำหนด a hook function ที่มี the exact name และ prototype ดังแสดงข้างล่าง. The hook function จะถูกเรียกใช้จริงๆครั้งเดียวเมื่อ the RTOS daemon task (หรือที่เรียกว่า the timer service task) ปฏิบัติเป็นครั้งแรก. Any application initialisation code ที่จำเป็นต้องให้ the RTOS ทำงานอยู่สามารถถูกวางใน the hook function.

void void vApplicationDaemonTaskStartupHook( void );

configUSE_TICK_HOOK

ตั้งค่าเป็น 1 ถ้าคุณต้องการใช้ a tick hook, หรือ 0 เพื่อละเว้น a tick hook
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 08:49:24 AM
configCPU_CLOCK_HZ

ป้อน the frequency in Hz ที่ the internal clock ที่ขับเคลื่อน the peripheral ที่ใช้สร้าง the tick interrupt ที่จะกำลังปฏิบัติ - ซึ่งปกติจะเป็น the same clock ที่ขับเคลื่อน the internal CPU clock. ค่านี้ถูกต้องการเพื่อกำหนดค่าอย่างถูกต้อง timer peripherals.

configSYSTICK_CLOCK_HZ

เป็น parameter เสริมสำหรับ ARM Cortex-M ports เท่านั้น.

โดยค่าเริ่มต้น ARM Cortex-M ports สร้าง the RTOS tick interrupt จาก the Cortex-M SysTick timer. Cortex-M MCUs ส่วนใหญ่รัน the SysTick timer ที่ความถี่เดียวกันกับ the MCU มันเอง - เมื่อเป็นกรณีนี้ configSYSTICK_CLOCK_HZ ไม่ถูกต้องการและควรถูกทิ้งไว้ไม่กำหนด. ถ้า the SysTick timer ถูก clocked ที่ความถี่ที่ต่างกับ the MCU core ดังนั้นเซ็ต configCPU_CLOCK_HZ ไปยัง the MCU clock frequency, ตามปกติ, และ configSYSTICK_CLOCK_HZ ไปยัง the SysTick clock frequency.
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 09:18:14 AM
configTICK_RATE_HZ

The frequency of the RTOS tick interrupt.

The tick interrupt ถูกใช้วัดเวลา. ดังนั้น a higher tick frequency หมายถึงเวลาสามารถถูกวัดได้ด้วย a higher resolution. อย่างไรก็ตาม, a high tick frequency ยังหมายความว่า the RTOS kernel จะใช้ CPU time มากขึ้นดังนั้นประสิทธิภาพจึงน้อยลง. The RTOS demo applications ทั้งหมดใช้ a tick rate of 1000Hz. นี้ถูกใช้เพื่อทดสอบ the RTOS kernel และสูงกว่าปกติที่จำเป็น.

มากกว่าหนึ่ง task สามารถแชร์ the same priority. The RTOS scheduler จะแชร์ processor time ระหว่าง tasks of the same priority โดยการสลับไปมาระหว่าง the tasks ในระหว่างแต่ละ RTOS tick. A high tick rate frequency จะดังนั้นยังมีผลของการลด the 'time slice' ที่ให้ไปยังแต่ละ task.
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 09:36:47 AM
configMAX_PRIORITIES

จำนวนของ priorities ที่มีให้ใช้ประโยชน์กับ the application tasks. tasks จำนวนเท่าใดก็ได้สามารถแชร์ the same priority. Co-routines ถูกจัดลำดับความสำคัญแยกต่างหาก - ดู configMAX_CO_ROUTINE_PRIORITIES.

แต่ละ available priority จะกิน a little RAM ภายใน the RTOS kernel ดังนั้นค่านี้ควรไม่ถูกเซ็ตให้สูงกว่าที่ต้องการจริงโดย your application.

The maximum permissible value จะถูกครอบถ้า configUSE_PORT_OPTIMISED_TASK_SELECTION ถูกเซ็ตเป็น 1.
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 09:56:11 AM
configMINIMAL_STACK_SIZE

The size of the stack ที่ใช้โดย the idle task. โดยทั่วไปแล้ว ค่านี้ไม่ควรถูกลดลงจากค่าที่ตั้งไว้ใน the FreeRTOSConfig.h file ที่จัดให้มีกับ the demo application สำหรับ the port ที่คุณกำลังใช้.

เช่นเดียวกับ the stack size parameter ถึง the xTaskCreate() and xTaskCreateStatic() functions, the stack size ถูกระบุใน words, ไม่ bytes. ถ้าแต่ละ item ที่วางบน the stack คือ 32-bits, ดังนั้น a stack size of 100 จะหมายถึง 400 bytes (แต่ละ 32-bit stack item จะใช้ 4 bytes).
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 10:20:20 AM
configMAX_TASK_NAME_LEN

The maximum permissible length of the descriptive name ที่ให้กับ a task เมื่อ the task ถูกสร้าง. The length ถูกระบุใน the number of characters รวมถึง the NULL termination byte.

configUSE_TRACE_FACILITY

ตั้งค่าเป็น 1 ถ้าคุณอยากรวม additional structure members and functions เพื่อช่วยในการดำเนินการ visualisation and tracing.

configUSE_STATS_FORMATTING_FUNCTIONS

ตั้งค่า configUSE_TRACE_FACILITY and configUSE_STATS_FORMATTING_FUNCTIONS เป็น 1 เพื่อรวม the vTaskList() and vTaskGetRunTimeStats() functions ในการบิ้วด์. การตั้งค่าเป็น 0 จะละเว้น vTaskList() and vTaskGetRunTimeStates() จากการบิ้วด์.
Title: Re: FreeRTOS
Post by: tha on October 24, 2021, 10:54:41 AM
configUSE_16_BIT_TICKS

Time ที่ถูกวัดเป็น 'ticks' - ซึ่งคือจำนวนครั้งที่ the tick interrupt ปฏิบัติแล้วตั้งแต่ the RTOS kernel ถูกสตาร์ทมาแล้ว. The tick count ถูกเก็บไว้ในตัวแปรประเภท TickType_t.

การกำหนด configUSE_16_BIT_TICKS เป็น 1 เป็นเหตุให้ TickType_t ถูกกำหนด (typedef'ed) เป็น an unsigned 16bit type. การกำหนด configUSE_16_BIT_TICKS เป็น 0 เป็นเหตุให้ TickType_t ถูกกำหนด (typedef'ed) เป็น an unsigned 32bit type.

การใช้ a 16 bit type จะช่วยปรับปรุงประสิทธิภาพการทำงานบน 8 and 16 bit architectures, แต่จำกัด the maximum specifiable time period เป็น 65535 'ticks'. ดังนั้น, ถือว่า a tick frequency of 250Hz, the maximum time a task สามารถ delay หรือ block เมื่อ a 16bit counter ถูกใช้คือ 262 seconds, เปรียบเทียบกับ 17179869 seconds เมื่อใช้ a 32bit counter.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 05:28:47 AM
configIDLE_SHOULD_YIELD

parameter นี้ควบคุมพฤติกรรมของ tasks ที่ the idle priority. มันมีผลเฉพาะถ้า:

     1. The preemptive scheduler กำลังถูกใช้.
     2. The application สร้าง tasks ที่รันที่ the idle priority

ถ้า configUSE_TIME_SLICING ถูกเซ็ตเป็น 1 (หรือไม่ได้กำหนด) ดังนั้น tasks ที่แชร์ the same priority จะแบ่งเวลา. ถ้าไม่มี the tasks ใดได้รับการจองไว้ก่อนดังนั้นก็อาจสันนิษฐานได้ว่าแต่ละ task ที่ given priority จะถูกจัดสรรเวลาในการประมวลผลที่เท่ากัน - และถ้า the priority อยู่เหนือ the idle priority ดังนั้นจะเป็นกรณีนี้จริง.

เมื่อ tasks แชร์ the idle priority พฤติกรรมสามารถแตกต่างกันเล็กน้อย. ถ้า configIDLE_SHOULD_YIELD ถูกเซ็ตเป็น 1 ดังนั้น the idle task จะส่งผลทันทีถ้า task อื่นใดๆที่ the idle priority พร้อมที่จะรัน.  สิ่งนี้ทำให้แน่ใจได้ว่าเวลาที่น้อยที่สุดถูกใช้ใน the idle task เมื่อ application tasks มีให้ใช้ประโยชน์สำหรับการจัดกำหนดการ. อย่างไรก็ตามพฤติกรรมนี้สามารถมีผลที่ไม่พึงประสงค์ (ขึ้นอยู่กับความต้องการของ your application) ดังที่แสดงด้านล่าง:

          (https://i.imgur.com/aaDkOEB.png)

The diagram ข้างบนแสดง the execution pattern ของ four tasks ที่ทั้งหมดกำลังรันที่ the idle priority. Tasks A, B and C เป็น application tasks. Task I เป็น the idle task. A context switch เกิดขึ้นด้วยคาบเวลาประจำที่เวลา T0, T1, ..., T6. เมื่อ the idle task ส่งผลให้ task A สตาร์ทเพื่อปฏิบัติ - แต่ the idle task ได้กินบางส่วนของ the current time slice ไปแล้ว. นี้ส่งผลให้ task I and task A แชร์อย่างมีประสิทธิภาพ the same time slice. The application tasks B and C ดังนั้นได้รับ processing time มากกว่า the application task A.

สถานการณ์นี้สามารถถูกหลีกเลี่ยงโดย:

     •  ถ้าเหมาะสม, ใช้ an idle hook แทน tasks ที่แยกกันที่ the idle priority.
     •  สร้าง application tasks ทั้งหมดที่ a priority ที่ใหญ่กว่า the idle priority.
     •  ตั้งค่า configIDLE_SHOULD_YIELD เป็น 0.

การตั้งค่า configIDLE_SHOULD_YIELD เป็น 0 ป้องกัน the idle task จากการให้ processing time จนกระทั่งสิ้นสุด its time slice. นี้ทำให้แน่ใจว่า tasks ทั้งหมดที่ the idle priority ถูกจัดสรรในจำนวนที่เท่ากันของ processing time (ถ้าไม่มี the tasks ใดได้รับการจองไว้ล่วงหน้า) - แต่ด้วยต้นทุนในสัดส่วนที่มากขึ้นของเวลาการประมวลผลทั้งหมดที่จัดสรรให้กับ the idle task.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 06:00:07 AM
configUSE_TASK_NOTIFICATIONS

การตั้งค่า configUSE_TASK_NOTIFICATIONS เป็น 1 (หรือทิ้งไว้ไม่กำหนด configUSE_TASK_NOTIFICATIONS) จะรวม direct to task notification functionality และ its associated API ในการบิ้วด์.

การตั้งค่า configUSE_TASK_NOTIFICATIONS เป็น 0 จะไม่รวม direct to task notification functionality และ its associated API จากการบิ้วด์.

แต่ละ task กิน 8 additional bytes of RAM เมื่อ direct to task notifications ถูกรวมในการบิ้วด์.

configTASK_NOTIFICATION_ARRAY_ENTRIES

แต่ละ RTOS task มี an array of task notifications. configTASK_NOTIFICATION_ARRAY_ENTRIES ตั้งค่า the number of indexes ใน the array.

ก่อนหน้า FreeRTOS V10.4.0 tasks มีเพียง a single notification value, ไม่ an array of values, ดังนั้นสำหรับความเข้ากันได้แบบย้อนหลัง configTASK_NOTIFICATION_ARRAY_ENTRIES ค่าเริ่มต้นเป็น 1 ถ้ามันถูกทิ้งไว้ไม่กำหนด.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 06:17:05 AM
configUSE_MUTEXES

เซ็ตเป็น 1 เพื่อรวม mutex functionality ใน the build, หรือ 0 เพื่อละเว้น mutex functionality จาก the build. ผู้อ่านควรทำความค้นเคยด้วยตัวเองกับความแตกต่างระหว่าง mutexes and binary semaphores ที่เกี่ยวข้องกับ the FreeRTOS functionality.

configUSE_RECURSIVE_MUTEXES

เซ็ตเป็น 1 เพื่อรวม recursive mutex functionality ใน the build, หรือ 0 เพื่อละเว้น recursive mutex functionality จาก the build.

configUSE_COUNTING_SEMAPHORES

เซ็ตเป็น 1 เพื่อรวม counting semaphore functionality in the build, หรือ 0 เพื่อละเว้น counting semaphore functionality จาก the build.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 07:55:05 AM
configUSE_ALTERNATIVE_API

เซ็ตเป็น 1 เพื่อรวม the 'alternative' queue functions in the build, หรือ 0 เพื่อละเว้น the 'alternative' queue functions จาก the build. The alternative API ถูกอธิบายไว้ภายใน the queue.h header file. The alternative API เลิกใช้แล้วและไม่ควรใช้ในการออกแบบใหม่.

configCHECK_FOR_STACK_OVERFLOW

The stack overflow detection page อธิบายการใช้ของ parameter นี้.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 08:13:40 AM
configQUEUE_REGISTRY_SIZE

The queue registry มีจุดประสงค์สองประการ ซึ่งทั้งสองอย่างนี้เกี่ยวข้องกับ RTOS kernel aware debugging:

     1. มันยอมให้ a textual name ถูกเกี่ยวข้องกับ a queue สำหรับง่ายต่อ queue identification ภายใน a debugging GUI.
     2. มันบรรจุ the information ที่ต้องการโดย a debugger เพื่อค้นหาแต่ละ registered queue and semaphore.

The queue registry ไม่มีจุดประสงค์เว้นแต่คุณกำลังใช้ a RTOS kernel aware debugger.

configQUEUE_REGISTRY_SIZE กำหนด the maximum number of queues and semaphores ที่สามารถถูก registered. มีเพียง queues and semaphores เหล่านั้นที่คุณต้องการจะดูโดยใช้ a RTOS kernel aware debugger จำเป็นต้องถูก registered. ดู the API reference documentation for vQueueAddToRegistry() and vQueueUnregisterQueue() สำหรับข้อมูลที่เพิ่มขึ้น.

configUSE_QUEUE_SETS

เซ็ตเป็น 1 เพื่อรวม queue set functionality (ความสามารถในการ block, หรือ pend, บนหลาย queues and semaphores), หรือ 0 เพื่อละเว้นt queue set functionality.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 08:49:53 AM
configUSE_TIME_SLICING

โดยค่าเริ่มต้น (ถ้า configUSE_TIME_SLICING ไม่ถูกกำหนด, หรือถ้า configUSE_TIME_SLICING ถูกกำหนดเป็น 1) FreeRTOS ใช้ prioritised preemptive scheduling ด้วย time slicing. นั่นหมายความว่า the RTOS scheduler จะรัน the highest priority task เสมอที่อยู่ใน the Ready state, และจะ switch ระหว่าง tasks of equal priority บนทุก RTOS tick interrupt.  ถ้า configUSE_TIME_SLICING ถูกเซ็ตเป็น 0 ดังนั้น the RTOS scheduler จะยังคงรัน the highest priority task ที่อยู่ใน the Ready state, แต่จะไม่ switch ระหว่าง tasks of equal priority เพียงเพราะว่า a tick interrupt ได้เกิดขึ้น.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 09:08:32 AM
configUSE_NEWLIB_REENTRANT

ถ้า configUSE_NEWLIB_REENTRANT ถูกเซ็ตเป็น 1 ดังนั้น a newlib reent structure จะถูกจัดสรรสำหรับแต่ละ created task.

หมายเหตุ Newlib support ถูกรวมไว้โดยความต้องการที่ได้รับความนิยม แต่ไม่ได้ถูกใช้โดย the FreeRTOS maintainers themselves. FreeRTOS จะไม่รับผิดชอบต่อผลลัพธ์ newlib operation. ผู้ใช้ต้องทำความคุ้นเคยกับ newlib และต้องจัดให้มี system-wide implementations of the necessary stubs. ขอเตือนว่า (ในขณะที่เขียน)  the current newlib design จัดให้มีใช้ a system-wide malloc() ที่ต้องถูกจัดให้มีพร้อมด้วย locks.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 09:24:42 AM
configENABLE_BACKWARD_COMPATIBILITY

The FreeRTOS.h header file รวมเอาชุดของ #define macros ที่แม็ป the names of data types ที่ใช้ใน versions of FreeRTOS ก่อน version 8.0.0 กับ the names ที่ใช้ใน FreeRTOS version 8.0.0. The macros ยอมให้ application code อัปเดต the version of FreeRTOS ที่พวกมันถูกบิ้วด์จาก a pre 8.0.0 version ถึง a post 8.0.0 version โดยปราศจากการแก้ไข. การตั้งค่า configENABLE_BACKWARD_COMPATIBILITY เป็น 0 ใน FreeRTOSConfig.h จะไม่รวม the macros จาก the build,  และดังนั้นจึงอนุญาตให้ตรวจสอบได้ว่าไม่มีก่อน version 8.0.0 names ถูกใช้.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 09:56:53 AM
configNUM_THREAD_LOCAL_STORAGE_POINTERS

เซ็ต the number of indexes ในแต่ละ task's thread local storage array.

configSTACK_DEPTH_TYPE

เซ็ต the type ที่ใช้ระบุ the stack depth ในการเรียกถึง xTaskCreate(), และในสถานที่อื่นๆทั้งหลายที่ stack sizes ถูกใช้ (ตัวอย่างเช่น, เมื่อรีเทิร์น the stack high water mark).

เวอร์ชั่นเก่าของ FreeRTOS จะระบุ stack sizes โดยใช้ตัวแปรประเภท UBaseType_t, แต่ถูกพบว่ามีข้อจำกัดเกินไปบน 8-bit microcontrollers. configSTACK_DEPTH_TYPE ได้ลบข้อจำกัดนั้นโดยเปิดการใช้งานให้ application developers ระบุประเภทที่จะใช้.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 10:43:19 AM
configMESSAGE_BUFFER_LENGTH_TYPE

FreeRTOS Message buffers ใช้ตัวแปรประเภท configMESSAGE_BUFFER_LENGTH_TYPE เพื่อเก็บความยาวของแต่ละ message. ถ้า configMESSAGE_BUFFER_LENGTH_TYPE ไม่ถูกกำหนดดังนั้นมันจะมีค่าเริ่มต้นเป็น size_t. ถ้า the messages ที่เก็บใน a message buffer จะไม่เคยใหญ่กว่า 255 bytes ดังนั้นการกำหนด configMESSAGE_BUFFER_LENGTH_TYPE เป็น uint8_t จะบันทึก 3 bytes ต่อ message บน a 32-bit microcontroller. ในทำนองเดียวกันถ้า the messages ที่เก็บใน a message buffer จะไม่เคยใหญ่กว่า 65535 bytes ดังนั้นการกำหนด configMESSAGE_BUFFER_LENGTH_TYPE เป็น uint16_t จะบันทึก 2 bytes ต่อ message บน a 32-bit microcontroller.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 11:20:53 AM
configSUPPORT_STATIC_ALLOCATION

ถ้า configSUPPORT_STATIC_ALLOCATION ถูกเซ็ตเป็น 1 ดังนั้น RTOS objects สามารถถูกสร้างโดยใช้ RAM ที่จัดให้มีโดย the application writer.

ถ้า configSUPPORT_STATIC_ALLOCATION ถูกเซ็ตเป็น 0 ดังนั้น RTOS objects สามารถถูกสร้างโดยใช้ RAM ที่จัดสรรจาก the FreeRTOS heap.

ถ้า configSUPPORT_STATIC_ALLOCATION ถูกทิ้งไว้ไม่กำหนดมันจะมีค่าเริ่มต้นเป็น 0.

ถ้า configSUPPORT_STATIC_ALLOCATION ถูกเซ็ตเป็น 1 ดังนั้น the application writer ต้องจัดให้มี two callback functions อีกด้วย: vApplicationGetIdleTaskMemory() เพื่อจัดให้มี the memory สำหรับใช้โดย the RTOS Idle task, และ (ถ้า configUSE_TIMERS ถูกเซ็ตเป็น 1) vApplicationGetTimerTaskMemory() เพื่อจัดให้มี memory สำหรับใช้โดย the RTOS Daemon/Timer Service task. ตัวอย่างถูกจัดให้มีข้างล่าง.

(ตัวอย่างโค้ดดูในลิ้งค์เอานะครับ)
https://www.freertos.org/a00110.html (https://www.freertos.org/a00110.html)

ดู the Static Vs Dynamic Memory Allocation page สำหรับข้อมูลเพิ่มเติม.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 12:02:01 PM
configSUPPORT_DYNAMIC_ALLOCATION

ถ้า configSUPPORT_DYNAMIC_ALLOCATION ถูกเซ็ตเป็น 1 ดังนั้น RTOS objects สามารถถูกสร้างโดยใช้ RAM ที่ถูกจัดสรรโดยอัตโนมัติจาก the FreeRTOS heap.

ถ้า configSUPPORT_DYNAMIC_ALLOCATION ถูกเซ็ตเป็น 1 ดังนั้น RTOS objects สามารถถูกสร้างโดยใช้ RAM ที่จัดให้มีโดย the application writer เท่านั้น.

ถ้า configSUPPORT_DYNAMIC_ALLOCATION ถูกทิ้งไว้ไม่กำหนดมันจะมีค่าเริ่มต้นเป็น 1.

ดู the Static Vs Dynamic Memory Allocation page สำหรับข้อมูลเพิ่มเติม.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 12:07:52 PM
configTOTAL_HEAP_SIZE

The total amount of RAM มีให้ใช้ประโยชน์ใน the FreeRTOS heap.

ค่านี้จะถูกใช้เฉพาะถ้า configSUPPORT_DYNAMIC_ALLOCATION ถูกเซ็ตเป็น 1 และ the application ทำการใช้ของหนึ่งของ the sample memory allocation schemes ที่จัดให้มีใน the FreeRTOS source code download. ดู the memory configuration section สำหรับรายละเอียดที่มากขึ้น.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 01:36:18 PM
configAPPLICATION_ALLOCATED_HEAP

โดยค่าเริ่มต้น the FreeRTOS heap ถูกประกาศโดย FreeRTOS และถูกวางใน memory โดย the linker. การเซ็ต configAPPLICATION_ALLOCATED_HEAP เป็น 1 ยอมให้ the heap ถูกประกาศแทนโดย the application writer, ซึ่งยอมให้ the application writer วาง the heap ที่ไหนก็ได้ที่พวกมันชอบใน memory.

ถ้า heap_1.c, heap_2.c or heap_4.c ถูกใช้, และ configAPPLICATION_ALLOCATED_HEAP ถูกเซ็ตเป็น 1, ดังนั้น the application writer ต้องจัดให้มี a uint8_t array ที่มี the exact name and dimension ดังแสดงข้างล่าง. The array จะถูกใช้เป็น the FreeRTOS heap. วิธีที่ the array ถูกวางที่ a specific memory location ขึ้นอยู่กับ on the compiler ที่กำลังถูกใช้ - โปรดดู your compiler's documentation.

uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 04:21:02 PM
configSTACK_ALLOCATION_FROM_SEPARATE_HEAP

ถ้า configSTACK_ALLOCATION_FROM_SEPARATE_HEAP ถูกเซ็ตเป็น 1 ดังนั้น stack สำหรับ task ใดๆที่สร้างโดยใช้ xTaskCreate or xTaskCreateRestricted API ถูกจัดสรรโดยใช้ pvPortMallocStack และปล่อยฟรีโดยใช้ vPortFreeStack function. The user จำเป็นต้องจัดให้มี thread-safe implementations of pvPortMallocStack and vPortFreeStack functions. นี้ทำให้ the user จัดสรร stacks สำหรับ tasks จาก a separate region of memory (อาจเป็น heap อื่นที่แตกต่างจาก the FreeRTOS heap).

ถ้า configSTACK_ALLOCATION_FROM_SEPARATE_HEAP ถูกทิ้งไว้ไม่กำหนดมันจะมีค่าเริ่มต้นเป็น 0.

ตัวอย่าง implementation of pvPortMallocStack and vPortFreeStack functions คือข้างล่าง :

(https://i.imgur.com/oqiYI0S.png)
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 04:32:45 PM
configGENERATE_RUN_TIME_STATS

The Run Time Stats page อธิบายการใช้ของ parameter นี้.

configUSE_CO_ROUTINES

เซ็ตเป็น 1 เพื่อรวม co-routine functionality ใน the build, หรือ 0 เพื่อละเว้น co-routine functionality จาก the build. เพื่อรวม co-routines croutine.c ต้องถูกรวมใน the project.

configMAX_CO_ROUTINE_PRIORITIES

จำนวนของ priorities ที่มีให้ใช้ประโยชน์กับ the application co-routines. จำนวนเท่าใดก็ได้ของ co-routines สามารถแชร์ the same priority. Tasks ถูกจัดลำดับความสำคัญแยกต่างกัน - ดู configMAX_PRIORITIES.
Title: Re: FreeRTOS
Post by: tha on October 25, 2021, 04:39:49 PM
configUSE_TIMERS

เซ็ตเป็น 1 เพื่อรวม software timer functionality, หรือ 0 เพื่อละเว้น software timer functionality. ดู the FreeRTOS software timers page สำหรับการอธิบายเติม.

configTIMER_TASK_PRIORITY

เซ็ต the priority of the software timer service/daemon task. ดู the FreeRTOS software timers page สำหรับการอธิบายเติม.

configTIMER_QUEUE_LENGTH

เซ็ตความยาวของ the software timer command queue. ดู the FreeRTOS software timers page สำหรับการอธิบายเติม.

configTIMER_TASK_STACK_DEPTH

เซ็ต the stack depth ที่จัดสรรให้กับ the software timer service/daemon task. ดู the FreeRTOS software timers page สำหรับการอธิบายเติม.
Title: Re: FreeRTOS
Post by: tha on October 26, 2021, 05:26:49 AM
configKERNEL_INTERRUPT_PRIORITY
configMAX_SYSCALL_INTERRUPT_PRIORITY and
configMAX_API_CALL_INTERRUPT_PRIORITY


Ports ที่บรรจุ a configKERNEL_INTERRUPT_PRIORITY ตั้งค่า include ARM Cortex-M3, PIC24, dsPIC, PIC32, SuperH and RX600. Ports ที่บรรจุ a configMAX_SYSCALL_INTERRUPT_PRIORITY ตั้งค่า include PIC32, RX600, ARM Cortex-A and ARM Cortex-M ports.

ARM Cortex-M3 and ARM Cortex-M4 ผู้ใช้โปรดใช้หมายเหตุพิเศษในตอนท้ายของส่วนนี้!

configMAX_API_CALL_INTERRUPT_PRIORITY เป็นชื่อใหม่สำหรับ configMAX_SYSCALL_INTERRUPT_PRIORITY ที่ถูกใช้โดยพอร์ตที่ใหม่กว่าเท่านั้น ทั้งสองมีค่าเท่ากัน.

configKERNEL_INTERRUPT_PRIORITY ควรถูกเซ็ตไปยัง the lowest priority.

หมายเหตุในการสนทนาต่อไปนี้ที่เฉพาะ API functions ที่สิ้นสุดใน "FromISR" เท่านั้นสามารถถูกเรียกจากภายใน an interrupt service routine
Title: Re: FreeRTOS
Post by: tha on October 26, 2021, 05:43:13 AM
สำหรับ ports ที่จัดให้มีใช้เฉพาะ configKERNEL_INTERRUPT_PRIORITY
configKERNEL_INTERRUPT_PRIORITY เซ็ต the interrupt priority ที่ใช้โดย the RTOS kernel ของมันเอง. Interrupts ที่เรียก API functions ต้องปฏิบัติที่ priority นี้อีกด้วย. Interrupts ที่ไม่เรียก API functions สามารถปฏิบัติที่ higher priorities และดังนั้นไม่เคยมีการปฏิบัติของพวกมันถูกหน่วงเวลาโดย the RTOS kernel activity ภายในข้อจำกัดของ the hardware มันเอง).

สำหรับ ports ที่จัดให้มีใช้ทั้ง configKERNEL_INTERRUPT_PRIORITY and configMAX_SYSCALL_INTERRUPT_PRIORITY:
configKERNEL_INTERRUPT_PRIORITY เซ็ต the interrupt priority ที่ใช้โดย the RTOS kernel ของมันเอง.. configMAX_SYSCALL_INTERRUPT_PRIORITY เซ็ต the highest interrupt priority จากซึ่ง interrupt safe FreeRTOS API functions สามารถถูกเรียก.
Title: Re: FreeRTOS
Post by: tha on October 26, 2021, 06:23:55 AM
A full interrupt nesting model ถูกทำให้สำเร็จโดยการเซ็ต configMAX_SYSCALL_INTERRUPT_PRIORITY เหนือ (นั่นคือ, ที่ a higher priority level) กว่า configKERNEL_INTERRUPT_PRIORITY. นี้หมายความว่า the FreeRTOS kernel ไม่ปิดการใช้งาน interrupts อย่างสมบูรณ์, แม้ภายใน critical sections. นอกจากนี้, นี้ถูกทำให้สำเร็จโดยไม่มีข้อเสียของ a segmented kernel architecture. อย่างไรก็ตาม โปรดทราบว่า, certain microcontroller architectures จะ (ใน hardware) ปิดการใช้งาน interrupts เมื่อ a new interrupt ถูกยอมรับ - หมายความว่า interrupts ถูกปิดใช้งานอย่างหลีกเลี่ยงไม่ได้ในช่วงเวลาสั้น ๆ ระหว่าง the hardware ยอมรับ the interrupt, และ the FreeRTOS code จะเปิดการใช้งาน interrupts ใหม่.

Interrupts ที่ไม่เรียก API functions สามารถปฏิบัติที่ priorities เหนือ configMAX_SYSCALL_INTERRUPT_PRIORITY และดังนั้นไม่เคยถูกหน่วงเวลาโดย the RTOS kernel execution

ตัวอย่างเช่น ลองนึกภาพ a hypothetical microcontroller ที่มี 8 interrupt priority levels - 0 เป็น the lowest and 7 เป็น the highest (ดู the special note สำหรับ ARM Cortex-M3 users ในตอนท้ายของส่วนนี้). รูปภาพข้างล่างอธิบายอะไรสามารถและไม่สามารถถูกทำที่แต่ละ priority level หาก the two configuration constants ถูกเซ็ตเป็น 4 and 0 ดังแสดง:

                    (https://i.imgur.com/qeamMzb.png)
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 06:05:47 AM
พารามิเตอร์การกำหนดค่าเหล่านี้ช่วยให้การจัดการอินเตอร์รัปต์มีความยืดหยุ่นมาก:

     •  Interrupt handling 'tasks' สามารถถูกเขียนและจัดลำดับความสำคัญตาม task อื่นใดๆใน the system. มี tasks ที่ถูกปลุกโดย an interrupt. The
         interrupt service routine (ISR) มันเองควรถูกเขียนให้สั้นที่สุดเท่าที่มันสามารถเป็นได้ - มันเพียงแค่จับ the data จากนั้นปลุก the high priority handler
         task. The ISR จากนั้นรีเทิร์นโดยตรงลงใน the woken handler task - ดังนั้น interrupt processing จะต่อเนื่องทันเวลา ราวกับว่ามันทำทั้งหมดใน the ISR
         ของตัวเอง. ประโยชน์ของสิ่งนี้คือ interrupts ทั้งหมดยังคงเปิดใช้งานในขณะที่ the handler task ดำเนินการ

     •  Ports ที่จัดให้มีใช้ configMAX_SYSCALL_INTERRUPT_PRIORITY จะก้าวต่อไป - ยอมให้ a fully nested model ที่ interrupts ระหว่าง the RTOS
         kernel interrupt priority and configMAX_SYSCALL_INTERRUPT_PRIORITY สามารถซ้อนกันและทำการเรียก applicable API. Interrupts ที่มี
         priority เหนือ configMAX_SYSCALL_INTERRUPT_PRIORITY ไม่เคยถูกหน่วงเวลาโดย the RTOS kernel activity.

     •  ISR ที่ทำงานอยู่เหนือ the maximum syscall priority จะไม่เคยถูกปิดบังโดย the RTOS kernel ตัวเอง, ดังนั้นการตอบสนองของพวกมันจะไม่ถูกกระทบโดย
         the RTOS kernel functionality. นี้คือแนวคิดสำหรับ interrupts ที่ต้องการ very high temporal accuracy - ตัวอย่างเช่่น interrupts ที่กระทำา motor
         commutation. อย่างไรก็ตาม ISR ดังกล่าวไม่สามารถใช้ the FreeRTOS API functions.
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 06:50:00 AM
เพื่อใช้ประโยชน์โครงร่างนี้ your application design ต้องยึดมั่นตามกฎต่อไปนี้: interrupt ใดๆที่ใช้ the FreeRTOS API ต้องถูกเซ็ตเป็น the same priority เช่นเดียวกับ the RTOS kernel (ดังที่กำหนดค่าโดย the configKERNEL_INTERRUPT_PRIORITY macro), หรือที่หรือต่ำกว่า configMAX_SYSCALL_INTERRUPT_PRIORITY สำหรับ ports ที่รวมฟังชั่นนี้.

A special note for ARM Cortex-M3 and ARM Cortex-M4 users: โปรดอ่าน the page dedicated to interrupt priority settings on ARM Cortex-M devices.  อย่างน้อยที่สุด โปรดจำไว้ว่า ARM Cortex-M3 cores ใช้ตัวเลข priority numbers ที่ต่ำเพื่อแสดง HIGH priority interrupts, ซึ่งอาจดูเหมือนขัดกับสัญชาตญาณและลืมง่าย!  ถ้าคุณอยากกำหนด an interrupt เป็น a low priority อย่ากำหนดมันเป็น a priority 0 (หรือ low numeric value อื่นๆ) เนื่องจากนี้จะสามารถส่งผลให้ the interrupt จริงๆแล้วมี the highest priority ใน the system - และดังนั้นอาจทำให้ your system ขัดข้องถ้า priority นี้อยู่เหนือ configMAX_SYSCALL_INTERRUPT_PRIORITY.

The lowest priority on a ARM Cortex-M3 core จริงๆแล้วคือ 255 - อย่างไรก็ตาม different ARM Cortex-M3 vendors จัดให้มีใช้จำนวนที่แตกต่างกันของ priority bits และจัดหาให้ library functions ที่คาดว่า priorities ถูกระบุในวิธีที่แตกต่างกัน. ตัวอย่างเช่น, บน the STM32 the lowest priority คุณสามารถระบุใน an ST driver library call จริงๆแล้วคือ 15 - และ the highest priority คุณสามารถระบุคือ 0.
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 08:22:32 AM
configASSERT

ความหมายของ the configASSERT() macro เป็นเหมือนกับ the standard C assert() macro. การถือสิทธิ์(ยืนยัน)จะถูกทริกถ้า the parameter ทีส่งผ่านลงใน configASSERT() เป็นศูนย์.

configASSERT() ถูกเรียกทั่วทั้ง the FreeRTOS source files เพื่อตรวจสอบว่า the application กำลังใช้ FreeRTOS อย่างไร. ขอแนะนำอย่างยิ่งให้พัฒนา FreeRTOS applications ด้วยการกำหนด configASSERT().

The example definition (แสดงที่ด้านบนของ the file และจำลองแบบด้านล่าง) เรียก vAssertCalled(), ส่งผ่าน in the file name and line number ของ the triggering configASSERT() call (__FILE__ and __LINE__ เป็น standard macros ที่จัดให้มีโดย compilers ส่วนใหญ่). นี้เป็นเพียงสำหรับการสาธิตเท่านั้นเนื่องจาก as vAssertCalled() ไม่ใช่ a FreeRTOS function, configASSERT() สามารถถูกกำหนดเพื่อดำเนินการอะไรก็ได้ตาม the application writer เห็นสมควร.

เป็นเรื่องปกติที่จะกำหนด configASSERT() ในลักษณะที่จะป้องกันไม่ให้แอปพลิเคชันดำเนินการต่อไป. ด้วยเหตุผลสองประการ; การหยุด the application ในจุดที่ the assertion ยอมให้สาเหตุของการถือสิทธิ์ถูกดีบีก, และการดำเนินการผ่านการยืนยัน(ถือสิทธิ์)ที่ทริกเกอร์อาจส่งผลให้เกิดการขัดข้องอยู่ดี.

หมายเหตุ การกำหนด configASSERT() จะเพิ่มทั้ง the application code size และ execution time. เมื่อ the application มีเสถียรภาพค่าโสหุ้ยเพิ่มเติมสามารถเอาออกได้โดยการ commenting out ง่ายๆ the configASSERT() definition ใน FreeRTOSConfig.h.

(https://i.imgur.com/ComxoNd.png)

ถ้ารัน FreeRTOS ภายใต้การควบคุมของ a debugger, ดังนั้น configASSERT() สามารถถูกกำหนดเพียงเพื่อปิดการใช้งาน interrupts และนั่งใน a loop, ดังสาธิตข้างล่าง. ที่จะมีผลของการหยุด the code บน the line ที่ล้มเหลว the assert test - การหยุดชั่วคราว the debugger ดังนั้นจะนำคุณไปยังบรรทัดที่ละเมิดทันที ดังนั้นคุณสามารถเห็นว่าทำใมมันถึงล้มเหลว.

(https://i.imgur.com/Zu5RkEq.png)
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 09:09:12 AM
configINCLUDE_APPLICATION_DEFINED_PRIVILEGED_FUNCTIONS

configINCLUDE_APPLICATION_DEFINED_PRIVILEGED_FUNCTIONS ถูกใช้เฉพาะโดย FreeRTOS MPU.

ถ้า configINCLUDE_APPLICATION_DEFINED_PRIVILEGED_FUNCTIONS ถูกเซ็ตเป็น 1 ดังนั้น the application writer ต้องจัดให้มี a header file ที่เรียกว่า "application_defined_privileged_functions.h", ซึ่งเป็นฟังชั่นที่ the application writer จำเป็นต้องปฏิบัติใน privileged mode สามารถถูกจัดให้มีใช้. โปรดทราบว่า, แม้จะมี a .h extension, the header file ควรบรรจุ the implementation of the C functions, ไม่ใช่เพียง the functions' prototypes

ฟังชั่นที่จัดให้มีใช้ใน "application_defined_privileged_functions.h" ต้องบันทึกและกู้คืน the processor's privilege state โดยใช้ the prvRaisePrivilege() function and portRESET_PRIVILEGE() macro ตามลำดับ. ตัวอย่างเช่น, ถ้า a library ที่จัดให้มี print function เข้าถึง RAM ที่อยู่ภายนอกการควบคุมของ the application writer, และดังนั้นไม่สามารถถูกจัดสรรให้กับ a memory ที่ป้องกัน user mode task, ดังนั้น the print function สามารถถูกห่อหุ้มใน a privileged function โดยใช้โค้ดต่อไปนี้:

(https://i.imgur.com/DnxPEJ5.png)

เทคนิคนี้ควรใช้ในระหว่างการพัฒนาเท่านั้น ไม่ควรปรับใช้ เนื่องจากจะหลีกเลี่ยง the memory protection.
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 09:33:11 AM
configTOTAL_MPU_REGIONS

FreeRTOS MPU (Memory Protection Unit) ports สำหรับ ARM Cortex-M4 microcontrollers support devices ที่มี 16 MPU regions. เซ็ต configTOTAL_MPU_REGIONS เป็น 16 สำหรับ devices ที่มี 16 MPU regions. ถ้าทิ้งไว้ไม่กำหนด, มันจะมีค่าเริ่มต้นเป็น 8.

configTEX_S_C_B_FLASH

The TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits กำหนด the memory type, และที่จำเป็น the cacheable and shareable properties(คุณสมบัติ) of an MPU region. configTEX_S_C_B_FLASH ยอมให้ application writers ข้ามผ่านค่าเริ่มต้นสำหรับ the for TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits สำหรับ the MPU region ครอบคุม Flash. ถ้าทิ้งไว้ไม่กำหนด, มันจะมีค่าเริ่มต้นเป็น 0x07UL ซึ่งหมายความว่า TEX=000, S=1, C=1, B=1.
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 09:37:18 AM
configTEX_S_C_B_SRAM

The TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits กำหนด the memory type, และที่จำเป็น the cacheable and shareable properties(คุณสมบัติ) of an MPU region. configTEX_S_C_B_SRAM ยอมให้ application writers ข้ามผ่านค่าเริ่มต้นสำหรับ the for TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits สำหรับ the MPU region ครอบคุม RAM. ถ้าทิ้งไว้ไม่กำหนด, มันจะมีค่าเริ่มต้นเป็น 0x07UL ซึ่งหมายความว่า TEX=000, S=1, C=1, B=1.
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 10:07:01 AM
configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY

configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY สามารถถูกกำหนดให้เป็น 1 เพื่อป้องกัน any privilege escalations ที่กำเนิดมาจากภายนอกของ the kernel code (นอกเหนือจาก escalations ที่กระทำโดย the hardware เองเมื่อ an interrupt ถูกเข้าสู่). เมื่อ configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY ถูกเซ็ตเป็น 1 ใน FreeRTOSConfig.h, variables __syscalls_flash_start__ and __syscalls_flash_end__ จำเป็นต้องถูกนำเข้าจาก linker script เพื่อแสดง the start and end addresses of the system calls memory ตามลำดับ. ขอแนะนำให้กำหนดมันเป็น 1 เพื่อความปลอดภัยสูงสุด.
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 10:40:53 AM
secureconfigMAX_SECURE_CONTEXTS

ARMv8-M secure side ports เท่านั้น.

Tasks ที่เรียกใช้ secure functions จาก the non-secure side of an ARMv8-M MCU (ARM Cortex-M23, Cortex-M33 and Cortex-M55) มีสอง contexts – หนึ่งบน the non-secure side และหนึ่งบน the secure-side. ก่อน FreeRTOS v10.4.5, ARMv8-M secure-side ports ที่จัดสรร the structures ที่อ้างถึง secure-side contexts ในขณะทำงาน. จาก FreeRTOS V10.4.5 the structures จะได้รับการจัดสรรแบบคงที่ ณ compile time. secureconfigMAX_SECURE_CONTEXTS เซ็ตจำนวนของ statically allocated secure contexts. secureconfigMAX_SECURE_CONTEXTS ค่าเริ่มต้นเป็น 8 ถ้าทิ้งไว้ไม่กำหนด.  Applications ที่ใช้เฉพาะ FreeRTOS code บน the non-secure side of ARMv8-M microcontrollers, อย่างเช่นเหล่านั้นที่รัน third-party code บน the secure-side, ไม่ต้องการค่าคงที่นี้.
Title: Re: FreeRTOS
Post by: tha on October 27, 2021, 10:55:22 AM
INCLUDE Parameters

The macros ที่เริ่มด้วย 'INCLUDE' ยอมให้ส่วนประกอบเหล่านั้นของ the real time kernel ที่ไม่เป็นประโยชน์ต่อ your application ถูกเอาออกจาก your build. นี้ทำให้แน่ใจว่า the RTOS จะไม่ใช้ ROM or RAM ใดๆมากกว่าความจำเป็นสำหรับ your particular embedded application.

แต่ละ macro จะมีรูปแบบ ...

INCLUDE_FunctionName

... โดยที่ FunctionName แสดง the API function (หรือชุดของ functions) ที่สามารถมีทางเลือกให้ถูกเอาออกได้. เพื่อรวม the API function ตั้งค่า the macro เป็น 1, เพื่อเอาออก the function ตั้งค่า the macro เป็น 0. ตัวอย่างเช่น, เพื่อรวม the vTaskDelete() API function ใช้:

#define INCLUDE_vTaskDelete    1

เพื่อเอา vTaskDelete() ออกจาก your build ใช้:

#define INCLUDE_vTaskDelete    0
Title: Re: FreeRTOS
Post by: tha on October 28, 2021, 05:59:01 AM
https://www.freertos.org/Static_Vs_Dynamic_Memory_Allocation.html (https://www.freertos.org/Static_Vs_Dynamic_Memory_Allocation.html)

Static Vs Dynamic Memory Allocation

Introduction

FreeRTOS versions ก่อนถึง V9.0.0 จัดสรร the memory ที่ใช้โดย the RTOS objects ที่แสดงรายการข้างล่างจาก the special FreeRTOS heap. FreeRTOS V9.0.0 และขึ้นไปให้ the application writer มีความสามารถจัดให้มี the memory ของพวกมันเองแทน, ยอมให้ the following objects มีทางเลือกถูกสร้างโดยไม่มี memory ใดๆถูกจัดสรรแบบไดนามิก:

     •  Tasks
     •  Software Timers
     •  Queues
     •  Event Groups
     •  Binary Semaphores
     •  Counting Semaphores
     •  Recursive Semaphores
     •  Mutexes

ไม่ว่าจะชอบมากกว่าที่จะใช้ static or dynamic memory allocation ขึ้นอยู่กับ the application, และความพึงพอใจของ the application writer. ทั้งสองวิธีมีข้อดีและข้อเสีย และทั้งสองวิธีสามารถถูกใช้ได้ภายในแอปพลิเคชัน RTOS เดียวกัน.

ตัวอย่าง Win32 อย่างง่ายที่อยู่ใน the FreeRTOS/Source/WIN32-MSVC-Static-Allocation-Only directory ของ the main FreeRTOS download สาธิตวิธีที่ a FreeRTOS application สามารถถูกสร้างโดยไม่มีการรวมใดๆของ the FreeRTOS heap implementations ใน a project.
Title: Re: FreeRTOS
Post by: tha on October 28, 2021, 07:22:49 AM
Creating an RTOS Object Using Dynamically Allocated RAM

การสร้าง RTOS objects แบบไดนามิกมีประโยชน์ในด้านความเรียบง่ายมากขึ้นและศักยภาพทำให้น้อยสุด the application's maximum RAM usage:

     •  function parameters ถูกต้องการน้อยลงเมื่อ an object ถูกสร้าง

     •  The memory allocation เกิดขึ้นโดยอัตโนมัติ, ภายใน the RTOS API functions.

     •  The application writer ไม่จำเป็นต้องกังวลเกี่ยวกับพวกมันเหล่านั้นด้วยการจัดสรรหน่วยความจำตัวพวกมันเอง

     •  The RAM ที่ใช้โดย an RTOS object สามารถถูกใช้ใหม่ถ้า the object ถูกลบ, มีศักยภาพในการลด the application's maximum RAM footprint.

     •  RTOS API functions ถูกจัดให้มี return information บนการใช้ heap, ยอมให้ the heap size ถูกจัดให้เหมาะสม.

     •  The memory allocation scheme ที่ใช้สามารถถูกเลือกให้เหมาะที่สุดกับ the application, ไม่ว่าจะเป็น heap_1.c เพื่อความเรียบง่ายและเชิงกำหนดมักจะ
         จำเป็นสำหรับ safety critical applications, heap_4.c สำหรับ fragmentation protection, heap_5.c เพื่อแบ่ง the heap ข้ามเป็นหลาย RAM
         regions, หรือ an allocation scheme ที่จัดให้มีโดย the application writer พวกเขาเหล่านั้น.

The following API functions, ซึ่งมีให้ใช้ประโยชน์ถ้า configSUPPORT_DYNAMIC_ALLOCATION ถูกเซ็ตเป็น 1 หรือถูกทิ้งไว้ไม่กำหนด, สร้าง RTOS objects โดยใช้ dynamically allocated RAM:

     •  xTaskCreate()
     •  xQueueCreate()
     •  xTimerCreate()
     •  xEventGroupCreate()
     •  xSemaphoreCreateBinary()
     •  xSemaphoreCreateCounting()
     •  xSemaphoreCreateMutex()
     •  xSemaphoreCreateRecursiveMutex()
Title: Re: FreeRTOS
Post by: tha on October 28, 2021, 09:03:57 AM
Creating an RTOS Object Using Statically Allocated RAM

การสร้าง RTOS objects โดยใช้ statically allocated RAM มีประโยชน์ในการจัดให้ the application writer ด้วยการควบคุมที่มากกว่า :

     •  RTOS objects สามารถถูกวางที่ specific memory locations

     •  The maximum RAM footprint สามารถถูกกำหนดที่ link time, มากกว่า run time

     •  The application writer ไม่จำเป็นต้องกังวลเกี่ยวกับพวกมันเหล่านั้นด้วยการจัดการอย่างงดงามของ memory allocation failures

     •  มันยอมให้ the RTOS ถูกใช้ใน applications ที่ง่ายไม่ยอมให้ any dynamic memory allocation (แม้ว่า FreeRTOS รวม allocation schemes ที่สามารถ
         เอาชนะการคัดค้านส่วนใหญ่).

ฟังก์ชัน API ต่อไปนี้, ซึ่งมีให้ใช้ประโยชน์ถ้า configSUPPORT_STATIC_ALLOCATION ถูกเซ็ตเป็น 1, ยอมให้ RTOS objects ถูกสร้างโดยใช้ memory ที่จัดให้มีโดย the application writer. เพื่อจัดให้มี memory the application writer จำเป็นแค่ประกาศตัวแปรของ the appropriate object type, จากนั้นส่งผ่าน the address ของ the variable ลงใน the RTOS API function. The StaticAllocation.c standard demo/test task ถูกจัดให้เพื่อสาธิตวิธีที่ the functions ถูกใช้:

     •  xTaskCreateStatic()
     •  xQueueCreateStatic()
     •  xTimerCreateStatic()
     •  xEventGroupCreateStatic()
     •  xSemaphoreCreateBinaryStatic()
     •  xSemaphoreCreateCountingStatic()
     •  xSemaphoreCreateMutexStatic()
     •  xSemaphoreCreateRecursiveMutexStatic()
Title: Re: FreeRTOS
Post by: tha on October 29, 2021, 05:52:20 AM
https://www.freertos.org/a00111.html (https://www.freertos.org/a00111.html)

Memory Management

[ดู the Static Vs Dynamic Memory Allocation page อีกด้วย, ซึ่งอธิบายข้อดีและข้อเสียของการจัดสรร RTOS objects แบบคงที่ (ไม่มีการใช้ the FreeRTOS heap) หรือแบบไดนามิก, คำอธิบายของ the configAPPLICATION_ALLOCATED_HEAP constant, ซึ่งสามารถถูกกำหนดใน FreeRTOSConfig.h, และ the reference project ที่สาธิตวิธีการใช้ FreeRTOS โดยไม่มี a heap implementation.]

The RTOS kernel ต้องการ RAM แต่ละครั้งที่ a task, queue, mutex, software timer, semaphore or event group ถูกสร้าง. The RAM สามารถถูกจัดสรรแบบไดนามิกโดยอัตโนมัติจาก the RTOS heap ภายใน the RTOS API object creation functions, หรือมันสามารถถูกจัดให้มีโดย the application writer.

ถ้า RTOS objects ถูกสร้างแบบไดนามิกดังนั้น the standard C library malloc() and free() functions บางครั้งก็สามารถถูกใช้สำหรับวัตถุประสงค์นี้, แต่ ...

     1. พวกมันไม่มีให้ใช้ประโยชน์เสมอไปบน embedded systems.
     2. พวกมันใช้พื้นที่โค้ดอันมีค่า,
     3. พวกมันไม่เป็น thread safe, และ
     4. พวกมันไม่ได้กำหนดขึ้น (ระยะเวลาที่ใช้ในการดำเนินการ the function จะแตกต่างกันจาก call ถึง call

... ดังนั้นบ่อยครั้งกว่าที่ไม่ต้องการ an alternative memory allocation implementation.

หนึ่ง embedded / real time system สามารถมี RAM และความต้องการเวลาที่แตกต่างกันมากกับตัวอื่น - ดังนั้น a single RAM allocation algorithm จะเหมาะสำหรับชุดย่อยของ applications เท่านั้น.

เพื่อแก้ไขปัญหานี้ , FreeRTOS เก็บ the memory allocation API ใน portable layer ของมัน. The portable layer เป็นภายนอกของ the source files ที่จัดให้มีใช้ the core RTOS functionality, ยอมให้ an application เฉพาะเจาะจงการจัดให้มีใช้ที่เหมาะสมสำหรับ the real time system กำลังถูกพัฒนาถูกจัดให้มี. เมื่อ the RTOS kernel ต้องการ RAM, แทนที่จะเรียก malloc(), มันเรียก pvPortMalloc() แทน. เมื่อ RAM กำลังถูกปล่อยฟรี, แทนที่จะเรียก free(), the RTOS kernel เรียก vPortFree().

FreeRTOS เสนอ several heap management schemes ที่ย่านในความซับซ้อนและคุณลักษณะ. มันสามารถเป็นได้ที่จะจัดให้มี your own heap implementation อีกด้วย, และแม้แต่จะใช้ two heap implementations พร้อมกัน. การใช้ two heap implementations พร้อมกันยอมให้ task stacks และ RTOS objects อื่นๆถูกวางใน fast internal RAM, และ application data ถูกวางใน slower external RAM.
Title: Re: FreeRTOS
Post by: tha on October 29, 2021, 06:52:35 AM
Memory allocation implementations ที่รวมอยู่ใน the RTOS source code download

The FreeRTOS download รวมเอาไว้ five sample memory allocation implementations, แต่ละรายการถูกอธิบายในส่วนย่อยต่อไปนี้. The subsections ยังได้รวมเอา information เมื่อแต่ละ the provided implementations อาจเหมาะสมที่สุดในการเลือก.

แต่ละ provided implementation ถูกบรรจุใน a separate source file (heap_1.c, heap_2.c, heap_3.c, heap_4.c and heap_5.c ตามลำดับ) ซึ่งถูกวางตำแหน่งใน the Source/Portable/MemMang directory ของ the main RTOS source code download. implementations อื่นๆสามารถถูกเพิ่มเมื่อต้องการ. จริงๆแล้วหนึ่งของ source files เหล่านั้นควรถูกรวมใน a project ในแต่ละครั้ง [the heap ที่กำหนดโดย these portable layer functions จะถูกใช้โดย the RTOS kernel ถึงแม้ว่า the application ที่กำลังใช้ the RTOS เลือกที่จะใช้ its own heap implementation].

ข้างล่างดังต่อไปนี้:

     •  heap_1 - ที่ง่ายที่สุด, ไม่ยอมให้ memory ถูกปล่อยฟรี.
     •  heap_2 - ยอมให้ memory ถูกปล่อยฟรี, แต่ไม่ coalescence adjacent free blocks.
     •  heap_3 - ห่ออย่างง่าย the standard malloc() and free() สำหรับ thread safety.
     •  heap_4 - coalescences adjacent free blocks เพื่อหลีกเลี่ยง fragmentation. รวม absolute address placement option.
     •  heap_5 - ตาม heap_4, ด้วยความสามารถขยาย the heap พาดผ่านไปหลาย non-adjacent memory areas.

หมายเหตุ:

     •  heap_1 มีประโยชน์น้อยเนื่องจาก FreeRTOS เพิ่มการสนับสนุนสำหรับ static allocation.
     •  heap_2 ตอนนี้ถูกพิจารณาว่าเป็นมรดกเนื่องจาก the newer heap_4 implementation ถูกชอบ.
Title: Re: FreeRTOS
Post by: tha on October 29, 2021, 10:15:19 AM
heap_1.c heap_1 มีประโยชน์น้อยลงตั้งแต่ FreeRTOS เพิ่มการสนับสนุนสำหรับ static allocation.

heap_1 เป็น the simplest implementation ของทั้งหมด. มันไม่ยอมให้ memory ถูกปล่อยฟรีเมื่อมันถูกจัดสรรแล้ว. อย่างไรก็ตาม, heap_1.c เหมาะสมสำหรับจำนวนมากของ embedded applications. นี้เป็นเพราะว่า small and deeply embedded applications จำนวนมากสร้าง the tasks, queues, semaphores, ฯลฯทั้งหมด. ถูกต้องการเมื่อ the system boots, และจากนั้นใช้ทั้งหมดของ objects เหล่านี้เป็นเวลาตลอดอายุของโปรแกรม (จนกระทั่ง the application ถูกปิดอีกครั้ง, หรือถูก rebooted). ไม่มีอะไรถูกลบ.

The implementation แบ่งย่อยง่ายๆ a single array ลงใน smaller blocks เมื่อ RAM ถูกร้องขอ. The total size ของ the array (the total size of the heap) ถูกเซ็ตโดย configTOTAL_HEAP_SIZE - ซึ่งถูกกำหนดใน FreeRTOSConfig.h. The configAPPLICATION_ALLOCATED_HEAP FreeRTOSConfig.h configuration constant ถูกจัดให้มีเพื่อยอมให้ the heap ถูกวางที่ a specific address ใน memory.

The xPortGetFreeHeapSize() API function รีเทิร์น the total amount of heap space ที่ยังคงไม่ถูกจัดสรร, ยอมให้การตั้งค่า the configTOTAL_HEAP_SIZE ถูกทำให้เหมาะ.

The heap_1 implementation:

     •  สามารถถูกใช้ถ้า your application ไม่เคยลบ a task, queue, semaphore, mutex, etc. (ซึ่งจริงๆแล้วครอบคุม the majority of applications ที่
         FreeRTOS ถูกใช้).

     •  ถูกกำหนดไว้เสมอ (ใช้เวลาในการดำเนินการเท่ากันเสมอ) และไม่สามารถส่งผลให้เกิดการกระจายตัวของหน่วยความจำได้

     •  จะเรียบง่ายและจัดสรร memory จาก a statically allocated array, หมายความว่ามันมักจะเพมาะสำหรับใช้ใน applications ที่ไม่ยอมให้ true dynamic
         memory allocation.
Title: Re: FreeRTOS
Post by: tha on October 30, 2021, 06:15:32 AM
heap_2.c heap_2 ตอนนี้ถูกพิจารณาว่าเป็นมรดกเนื่องจาก heap_4 ถูกชอบ..

heap_2 ใช้ a best fit algorithm และ, ต่างจาก scheme 1, ยอมให้ previously allocated blocks ถูกปล่อยฟรี. มันไม่รวม adjacent free blocks ลงใน a single large block. ดู heap_4.c สำหรับ an implementation ที่ทำ coalescence free blocks.

The total amount of available heap space ถูกเซ็ตโดย configTOTAL_HEAP_SIZE - ซึ่งถูกกำหนดใน FreeRTOSConfig.h. The configAPPLICATION_ALLOCATED_HEAP FreeRTOSConfig.h configuration constant ถูกจัดให้มีเพื่อยอมให้ the heap ถูกวางที่ a specific address ใน memory.

The xPortGetFreeHeapSize() API function รีเทิร์น the total amount of heap space ที่ยังคงไม่ถูกจัดสรร, (ยอมให้การตั้งค่า the configTOTAL_HEAP_SIZE ถูกทำให้เหมาะ), แต่ไม่จัดให้มี information เกี่ยวกับวิธีที่ the unallocated memory ถูกแยกส่วนออกเป็น smaller blocks อย่างไร.

การจัดให้มีใช้นี้:

     •  สามารถถูกใช้แม้เมื่อ the application ลบ tasks, queues, semaphores, mutexes, etc. ซ้ำ, โดยมีข้อแม้ด้านล่างเกี่ยวกับ memory fragmentation.

     •  ไม่ควรถูกใช้ถ้า the memory ที่กำลังถูกจัดสรรและปล่อยฟรีเป็น a random size. ตัวอย่างเช่น

           •  ถ้า an application สร้างและลบ tasks แบบไดนามิก, และ the size of the stack ที่จัดสรรถึง the tasks ที่กำลังถูกสร้างเป็นเหมือนกันเสมอ, ดังนั้น
               heap2.c สามารถถูกใช้ในกรณีส่วนใหญ่. อย่างไรก็ตาม, ถ้า the size of the stack ที่จัดสรรไปยัง the tasks ที่กำลังถูกสร้างไม่เท่ากันเสมอไป, ดังนั้น the
               available free memory อาจกระจายตัวลงใน small blocks จำนวนมาก, ผลท้ายสุดล้มเหลวในการจัดสรร. heap_4.c จะเป็นตัวเลือกที่ดีกว่าในกรณีนี้.

           •  ถ้า an application สร้างและลบ queues แบบไดนามิก, และ the queue storage area เป็นเหมือนกันในแต่ละกรณี (the queue storage area คือ
               the queue item size คุณด้วย the length of the queue), ดังนั้น heap_2.c สามารถถูกใช้ในกรณีส่วนใหญ่. อย่างไรก็ตาม, ถ้า the queue storage
               area ไม่เหมือนกันในแต่ละกรณี, ดังนั้น the available free memory อาจกระจายตัวลงใน small blocks จำนวนมาก, ผลท้ายสุดล้มเหลวในการจัดสรร.
               heap_4.c จะเป็นตัวเลือกที่ดีกว่าในกรณีนี้.

           •  The application เรียกใช้ pvPortMalloc() and vPortFree() โดยตรง, แทนที่จะเป็นเพียงโดยอ้อมผ่านทาง FreeRTOS API functions อื่นๆ.

     •  อาจส่งผลให้เกิด memory fragmentation problems ถ้า your application queues, tasks, semaphores, mutexes, etc. ใน an unpredictable
         order. สิ่งนี้ไม่น่าจะเป็นไปได้สำหรับแอปพลิเคชันเกือบทั้งหมด แต่ควรคำนึงถึง.

     •  ไม่ได้กำหนดไว้ - แต่มีประสิทธิภาพมากกว่าที่ส่วนใหญ่ standard C library malloc implementations.

heap_2.c เหมาะสำหรับ many small real time systems ที่ต้อง dynamically create objects. ดู heap_4 สำหรับ a similar implementation ที่รวม free memory blocks ลงใน single larger blocks
Title: Re: FreeRTOS
Post by: tha on October 30, 2021, 09:05:57 AM
heap_3.c การจัดให้มีใช้ตัวห่ออย่างง่ายนี้สำหรับ the standard C library malloc() and free() functions ที่จะ, ในกรณีส่วนใหญ่, ถูกแจกจ่ายพร้อมกับ your chosen compiler. The wrapper ทำง่ายๆ the malloc() and free() functions thread safe.

การจัดให้มีใช้นี้ :

     •  ต้องการ the linker เพื่อตั้งค่า a heap, และ the compiler library เพื่อจัดให้มี malloc() and free() implementations

     •  ไม่ได้ถูกกำหนดขึ้น

     •  อาจจะเพิ่ม the RTOS kernel code size.

โปรดทราบว่าการตั้งค่า the configTOTAL_HEAP_SIZE ใน FreeRTOSConfig.h ไม่มีผลกระทบเมื่อ heap_3 ถูกใช้.
Title: Re: FreeRTOS
Post by: tha on October 30, 2021, 09:49:38 AM
heap_4.c โครงร่างนี้ใช้ a first fit algorithm และ, ไม่เหมือน scheme 2, มันรวม adjacent free memory blocks ลงใน a single large block (มันรวม a coalescence algorithm).

The total amount of available heap space ถูกเซ็ตโดย configTOTAL_HEAP_SIZE - ซึ่งถูกกำหนดใน FreeRTOSConfig.h. The configAPPLICATION_ALLOCATED_HEAP FreeRTOSConfig.h configuration constant ถูกจัดให้มีเพื่อยอมให้ the heap ถูกวางที่ a specific address ใน memory.

The xPortGetFreeHeapSize() API function รีเทิร์น the total amount of heap space ที่ยังคงไม่ถูกจัดสรรเมื่อ the function ถูกเรียก, และ the xPortGetMinimumEverFreeHeapSize() API function รีเทิร์นจำนวนต่ำสุดของ free heap space ที่มีอยู่ในระบบเมื่อ the FreeRTOS application ถูกบู๊ต. ฟังก์ชันทั้งสองไม่ได้จัดให้มี information เกี่ยวกับวิธีที่ the unallocated memory ถูกกระจายตัวลงใน smaller blocks.

The vPortGetHeapStats() API function จัดให้มีข้อมูลเพิ่มเติม. มันเติม the members of a heap_t structure, ดังแสดงข้างล่าง.

(https://i.imgur.com/dD6NgDK.png)
Title: Re: FreeRTOS
Post by: tha on October 30, 2021, 10:15:50 AM
heap_4:

     •  สามารถถูกใช้แม้เมื่อ the application ลบ tasks, queues, semaphores, mutexes, ฯลฯ ซ้ำๆ.

     •  มีโอกาสน้อยกว่าการใช้งาน heap_2 มากที่จะส่งผลให้ a heap space ที่ถูกแยกส่วนอย่างไม่ดีออกเป็นบล็อกเล็กๆ หลายบล็อก - แม้ว่าหน่วยความจำที่จัดสรรและ
         ปล่อยฟรีจะมีขนาดแบบสุ่มก็ตาม.

      •  ไม่ได้กำหนดไว้ - แต่มีประสิทธิภาพมากกว่าที่ส่วนใหญ่ standard C library malloc implementations.

heap_4.c มีประโยชน์อย่างยิ่งสำหรับ applications ที่ต้องการใช้ the portable layer memory allocation schemes โดยตรงใน the application code (แทนที่จะเพียงโดยอ้อมโดยการเรียก API functions ที่ตัวพวกมันเองเรียก pvPortMalloc() and vPortFree())
Title: Re: FreeRTOS
Post by: tha on October 30, 2021, 04:05:41 PM
heap_5.c แบบแผนนี้ใช้ the same first fit และ memory coalescence algorithms อย่าง heap_4, และยอมให้ the heap ขยายไปหลายไม่ติดกัน (ไม่ต่อเนื่อง) memory regions.

Heap_5 ถูกเริ่มต้นโดยการเรียก vPortDefineHeapRegions(), และไม่สามารถถูกใช้จนกระทั่งหลังจาก vPortDefineHeapRegions() ปฏิบัติแล้ว. การสร้าง an RTOS object (task, queue, semaphore, etc.) จะเรียก pvPortMalloc() โดยปริยายดังนั้นมันจึงจำเป็นที่, เมื่อใช้ heap_5, vPortDefineHeapRegions() จะถูกเรียกก่อนการสร้างของ object ดังกล่าวใดๆ.

vPortDefineHeapRegions() จะรับ a single parameter. The parameter คือ an array of HeapRegion_t structures. HeapRegion_t ถูกกำหนดใน portable.h ดังนี้

(https://i.imgur.com/ppEKMiV.png)

The array ถูกสิ้นสุดโดยใช้ a NULL zero sized region definition, และ the memory regions ที่กำหนดใน the array ต้องปรากฎใน address order, จาก low address ถึง high address. The following source code snippets จัดให้มีตัวอย่าง. The MSVC Win32 simulator demo ใช้ heap_5 อีกด้วยดังนั้นสามารถถูกใช้เป็นตัวอ้างอิง.

(https://i.imgur.com/HnPQiKP.png)

The xPortGetFreeHeapSize() API function รีเทิร์น the total amount of heap space ที่ยังคงไม่ถูกจัดสรรเมื่อ the function ถูกเรียก, และ the xPortGetMinimumEverFreeHeapSize() API function รีเทิร์นจำนวนต่ำสุดของ free heap space ที่มีอยู่ในระบบเมื่อ the FreeRTOS application ถูกบู๊ต. ฟังก์ชันทั้งสองไม่ได้จัดให้มี information เกี่ยวกับวิธีที่ the unallocated memory ถูกกระจายตัวลงใน smaller blocks.

The vPortGetHeapStats() API function จัดให้มีข้อมูลเพิ่มเติมเกี่ยวกับ the heap status.
Title: Re: FreeRTOS
Post by: tha on October 31, 2021, 05:48:12 AM
https://www.freertos.org/Stacks-and-stack-overflow-checking.html (https://www.freertos.org/Stacks-and-stack-overflow-checking.html)

Stack Usage and Stack Overflow Checking

Stack Usage

[Also see the uxTaskGetStackHighWaterMark() API function]

แต่ละ task จะรักษา stack ของมันเอง. ถ้า a task ถูกสร้างโดยใช้ xTaskCreate() ดังนั้น the memory ที่ใช้เป็น the task's stack ถูกจัดสรรโดยอัตโนมัติจาก the FreeRTOS heap, และกำหนดขนาดโดย a parameter ที่ส่งผ่านไปยัง the xTaskCreate() API function. ถ้า a task ถูกสร้างโดยใช้ xTaskCreateStatic() ดังนั้น the memory ที่ใช้เป็น the task's stack ถูกจัดสรรก่อนโดย the application writer. Stack overflow เป็นสาเหตุที่พบบ่อยมากของความไม่เสถียรของแอปพลิเคชัน. FreeRTOS ดังนั้นจัดให้มี two optional mechanisms ที่สามารถถูกใช้เพื่อช่วยในการตรวจจับและแก้ไขเหตุการณ์ดังกล่าวได้. ทางเลือกที่ใช้ถูกกำหนดค่าโดยใช้ the configCHECK_FOR_STACK_OVERFLOW configuration constant.

โปรดทราบว่าทางเลือกเหล่านี้จะมีให้ใช้ประโยชน์ได้เฉพาะบน architectures ที่ the memory map ไม่ถูกแบ่งส่วน นอกจากนี้, บาง processors สามารถสร้าง a fault หรือ exception เพื่อตอบสนองต่อ a stack corruption ก่อน the RTOS kernel overflow check สามารถเกิดขึ้น. The application ต้องจัดให้มี a stack overflow hook function ถ้า configCHECK_FOR_STACK_OVERFLOW ไม่ถูกเซ็ต 0. The hook function ต้องถูกเรียกว่า vApplicationStackOverflowHook(), และมีต้นแบบข้างล่าง:

void vApplicationStackOverflowHook( TaskHandle_t xTask,
                                    signed char *pcTaskName );

The xTask and pcTaskName parameters ส่งผ่านไปยัง the hook function, the handle and name ของ the offending task ตามลำดับ. อย่างไรก็ตาม โปรดทราบว่าขึ้นอยู่กับความรุนแรงของ the overflow, พารามิเตอร์เหล่านี้อาจเสียหายได้ ในกรณีนี้ ตัวแปร pxCurrentTCB สามารถถูกตรวจสอบได้โดยตรง.

Stack overflow checking ทำให้เกิด a context switch overhead ดังนั้นขอแนะนำให้ใช้เฉพาะในระหว่าง the development or testing phases.
Title: Re: FreeRTOS
Post by: tha on October 31, 2021, 11:14:19 AM
Stack Overflow Detection - Method 1

มีแนวโน้มว่า the stackจะถึงค่าสูงสุด (ลึกที่สุด) หลังจากที่ the RTOS kernel สลับ the task ออกจาก of the Running state เนื่องจากเป็นช่วงที่ the stack จะบรรจุ the task context. ณ จุดนี้ the RTOS kernel สามารถเช็คว่า the processor stack pointer ยังคงอยู่ภายใน the valid stack space. The stack overflow hook function ถูกเรียกถ้า the stack pointer บรรจุค่าที่อยู่ภายนอกของ the valid stack range.

วิธีนี้รวดเร็วแต่ไม่รับประกันว่าจะตรวจจับ stack overflows ทั้งหมดได้. ตั้งค่า configCHECK_FOR_STACK_OVERFLOW เป็น 1 เพื่อใช้วิธีนี้.
Title: Re: FreeRTOS
Post by: tha on October 31, 2021, 11:25:43 AM
Stack Overflow Detection - Method 2

เมื่อ a task ถูกสร้างเป็นครั้งแรก stack ของมันจะถูกเติมด้วยค่าที่รู้. เมื่อสลับ a task ออกจาก the Running state the RTOS kernel สามารถเช็ค the last 16 bytes ภายใน the valid stack range เพื่อให้แน่ใจว่าค่าที่รู้เหล่านี้ไม่ได้ถูกเขียนทับโดย the task หรือ interrupt activity. The stack overflow hook function ถูกเรียกหาก 16 bytes เหล่านี้ไม่คงอยู่ที่ค่าเริ่มต้นของพวกมัน.

วิธีนี้มีประสิทธิภาพน้อยกว่าวิธีที่หนึ่ง แต่ก็ยังค่อนข้างเร็ว มีความเป็นไปได้สูงที่จะดักจับการล้นของสแต็ก แต่ก็ยังไม่รับประกันว่าจะดักจับโอเวอร์โฟลว์ทั้งหมดได้

ตั้งค่า configCHECK_FOR_STACK_OVERFLOW เป็น 2 เพื่อใช้วิธีนี้.
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 05:42:27 AM
https://www.freertos.org/Creating-a-new-FreeRTOS-project.html (https://www.freertos.org/Creating-a-new-FreeRTOS-project.html)

Creating a New FreeRTOS Project

Introduction

FreeRTOS ถูกออกแบบมาให้เรียบง่ายและง่ายต่อการใช้: มีเพียง 3 source files ที่ถูกใช้ร่วมกับ RTOS ports ทั้งหมด, และ one microcontroller specific source file ถูกต้องการ, และ API ของมันถูกออกแบบมาให้เรียบง่ายและหยั่งรู้ง่ายๆ.

FreeRTOS ถูกพอร์ตไปยัง many different microcontroller architectures, และ many different compilers. แต่ละ official port มาพร้อมกับ an official demo ที่ (อย่างน้อยในช่วงเวลาของการสร้างของมัน) คอมไพล์และปฏิบัติบน the hardware platform ซึ่งมันถูกพัฒนาแล้วโดยไม่ต้องแก้ไขใดๆ.

The demo projects ถูกจัดให้มีเพื่อให้แน่ใจว่าผู้ใช้ใหม่สามารถเริ่มต้นกับ FreeRTOS ได้ในเวลาที่รวดเร็วที่สุดและด้วยความยุ่งยากน้อยที่สุด.

แต่ละ architecture ที่สนับสนุนโดย FreeRTOS ถูกใช้ใน many different microcontrollers, หมายความว่า FreeRTOS สามารถถูกใช้กับหลายพันของ different microcontroller part numbers อย่างแท้จริง.  คุณจำนวนนี้ด้วยจำนวนของ supported compilers, และจากนั้นคุณอีกครั้งด้วยจำนวนที่เพิ่มขึ้นเรื่อยๆของ starter kits และ evaluation boards ที่กำลังถูกนำออกสู่ตลาด, และเห็นได้ชัดว่าถึงแม้เราจะพยายามอย่างเต็มที่แล้ว เราสามารถเพียงจัดให้มี official demo projects ที่ตรงกับส่วนเล็กๆ ของ possible combinations.

ขอแนะนำเสมอให้ a new FreeRTOS project ถูกสร้างโดยการสตาร์ทด้วย, และจากนั้นปรับแต่ง, หนึ่งของ the provided pre-configured demos. การทำอย่างนี้ทำให้แน่ใจว่า the new project รวมเอา the necessary source and header files ไว้ทั้งหมด, และติดตั้ง the necessary interrupt service routines, โดยไม่ต้องใช้ความพยายามในส่วนของผู้สร้างโปรเจ็กต์.

ผู้ใช้ FreeRTOS บางรายต้องการทราบวิธีสร้างโปรเจ็กต์ FreeRTOS ด้วยวิธีการอื่นนอกเหนือจากการปรับโปรเจ็กต์ที่มีอยู่ ขั้นตอนในการทำเช่นนี้ได้รับการบันทึกไว้ด้านล่าง
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 06:21:13 AM
Anatomy of a FreeRTOS Project

A FreeRTOS application จะเริ่มต้นและดำเนินการเหมือนกับ a non-RTOS application จนกระทั่ง vTaskStartScheduler() ถูกเรียก. vTaskStartScheduler() โดยปกติถูกเรียกจาก the application's main() function. The RTOS เพียงควบคุม the execution sequencing หลังจาก vTaskStartScheduler() ถูกเรียกแล้ว.

ขอแนะนำเป็นอย่างยิ่งเพื่อให้แน่ใจว่าโค้ดกำลังทำงานอย่างถูกต้อง (start up code ที่ถูกต้อง linker configuration ที่ถูกต้อง ฯลฯ) บนเป้าหมายที่เลือกก่อนที่จะพยายามใช้ฟังก์ชัน RTOS ใดๆ
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 06:37:53 AM
Source Files

FreeRTOS ถูกจัดส่งเป็น standard C source files ที่จะถูก built พร้อมกับ C files อื่นๆทั้งหมดใน your project. The FreeRTOS source files ถูกแจกจ่ายใน a zip file. The RTOS source code organisation page อธิบาย the structure of the files ใน the zip file.

อย่างน้อยที่สุด , the following source files ต้องถูกรวมใน your project:

     •  FreeRTOS/Source/tasks.c
     •  FreeRTOS/Source/queue.c
     •  FreeRTOS/Source/list.c
     •  FreeRTOS/Source/portable/[compiler]/[architecture]/port.c.
     •  FreeRTOS/Source/portable/MemMang/heap_x.c โดยที่ 'x' คือ 1, 2, 3, 4 or 5.

ถ้า the directory ที่บรรจุ the port.c file บรรจุ an assembly language file อีกด้วย, ดังนั้น the assembly language file ต้องถูกใช้ด้วย.
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 08:10:36 AM
Optional Source Files

ถ้าคุณต้องการ software timer functionality, ดังนั้นเพิ่ม FreeRTOS/Source/timers.c ไปยัง your project.

ถ้าคุณต้องการ event group functionality, ดังนั้นเพิ่ม FreeRTOS/Source/event_groups.c to your project.

ถ้าคุณต้องการ stream buffer or message buffer functionality, ดังนั้นเพิ่ม FreeRTOS/Source/stream_buffer.c to your project.

ถ้าคุณต้องการ co-routine functionality, ดังนั้นเพิ่ม FreeRTOS/Source/croutine.c ไปยัง your project (โปรดทราบว่า co-routines ถูกเลิกใช้และไม่แนะนำสำหรับ new designs).
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 08:19:23 AM
Header Files

The following directories ต้องอยู่ใน the compiler's include path (the compiler ต้องถูกบอกเพื่อหา directories เหล่านี้สำหรับ header files):

     •  FreeRTOS/Source/include
     •  FreeRTOS/Source/portable/[compiler]/[architecture].
     •  ไดเรกทอรีใดก็ตามที่มีไฟล์ FreeRTOSConfig.h ที่จะใช้ - ดูย่อหน้าไฟล์การกำหนดค่าด้านล่าง

ทั้งนี้ขึ้นอยู่กับพอร์ต อาจจำเป็นต้องให้ไดเร็กทอรีเดียวกันอยู่ใน the assembler's include path.
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 09:50:10 AM
Configuration File

ทุกๆ project ต้องการไฟล์ที่เรียกว่า FreeRTOSConfig.h อีกด้วย. FreeRTOSConfig.h ปรับแต่ง the RTOS kernel ให้เข้ากับ the application ที่กำลังถูก built. มันดังนั้นเฉพาะเจาะจงไปยัง the application, ไม่ใช่ the RTOS, และควรถูกให้อยู่ใน an application directory, ไม่ใช่ในหนึ่งของ the RTOS kernel source code directories.

ถ้า heap_1, heap_2, heap_4 or heap_5 ถูกรวมใน your project, ดังนั้น the FreeRTOSConfig.h definition configTOTAL_HEAP_SIZE จะกำหนดขนาด the FreeRTOS heap. Your application จะไม่ link ถ้า configTOTAL_HEAP_SIZE ถูกเซ็ตสูงเกินไป.

The FreeRTOSConfig.h definition configMINIMAL_STACK_SIZE เซ็ตขนาดของ the stack ที่ใช้โดย the idle task. ถ้า configMINIMAL_STACK_SIZE ถูกเซ็ตต่ำเกินไป, ดังนั้น the idle task จะทำให้เกิด stack overflows. ขอแนะนำให้คัดลอกการตั้งค่า the configMINIMAL_STACK_SIZE จาก an official FreeRTOS demo ที่จัดให้มีสำหรับ the same microcontroller architecture. The FreeRTOS demo projects ถูกเก็บใน sub directories ของ the FreeRTOS/Demo directory. โปรดทราบว่า some demo projects เก่าแล้ว, ดังนั้นไม่บรรจุ the available configuration options ทั้งหมด.
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 10:02:39 AM
Interrupt Vectors

[Cortex-M users: Information เกี่ยวกับการติดตั้ง interrupt handers ถูกจัดให้มีใน the "The application I created compiles, but does not run" FAQ]

ทุกๆ RTOS port ใช้ a timer เพื่อสร้าง a periodic tick interrupt. Many ports ใช้ interrupts เพิ่มเติมเพื่อจัดการ context switching. The interrupts ที่ต้องการโดย an RTOS port ถูกให้บริการโดย the provided RTOS port source files.

The method ที่ใช้เพื่อติดตั้ง the interrupt handlers ที่จัดให้มีโดย the RTOS port ขึ้นอยู่กับ the port and compiler ที่กำลังถูกใช้. อ้างอิงถึง, และถ้าจำเป็นคัดลอก, the provided official demo application สำหรับ the port ที่กำลังถูกใช้. อ้างอิงถึง the documentation page ที่จัดให้มีสำหรับ the official demo application อีกด้วย.
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 10:33:40 AM
https://www.freertos.org/kernel/secondarydocs.html (https://www.freertos.org/kernel/secondarydocs.html)

(https://i.imgur.com/Idb3b0c.png)
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 11:26:08 AM
https://www.freertos.org/RTOS-idle-task.html (https://www.freertos.org/RTOS-idle-task.html)

Tasks
[More about tasks...]

The FreeRTOS Tutorial Books จัดให้มี additional detailed information เกี่ยวกับ tasks และพฤติกรรมของพวกมัน.

The Idle Task

The idle task ถูกสร้างโดยอัตโนมัติเมื่อ the RTOS scheduler ถูกสตาร์ทเพื่อให้แน่ใจว่ามีอย่างน้อยหนึ่ง task เสมอที่สามารถรัน. มันถูกสร้างที่ the lowest possible priority เพื่อให้แน่ใจว่ามันไม่ใช้ any CPU time ถ้ามี higher priority application tasks ใน the ready state

The idle task รับผิดชอบต่อการปล่อยฟรี memory ที่จัดสรรโดย the RTOS ไปยัง tasks ที่ถูกลบไปแล้ว. จึงเป็นเรื่องสำคัญใน applications ที่ใช้ the vTaskDelete() function เพื่อให้แน่ใจว่า the idle task ไม่อดอยาก processing time. The idle task ไม่มี active functions อื่นๆดังนั้นสามารถอดอยาก microcontroller time ภายใต้เงื่อนไขอื่นๆ ทั้งหมด.

มันเป็นไปได้สำหรับ application tasks ที่จะแชร์ the idle task priority (tskIDLE_PRIORITY). ดู the configIDLE_SHOULD_YIELD configuration parameter สำหรับ information เกี่ยวกับวิธีที่พฤติกรรมนี้สามารถถูกกำหนดค่า.
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 02:09:20 PM
The Idle Task Hook

An idle task hook เป็น a function ที่ถูกเรียกในระหว่างแต่ละรอบของ the idle task. ถ้าคุณต้องการ application functionality ให้รันที่ the idle priority ดังนั้นมีสองทางเลือก:

     1. จัดให้มีใช้ the functionality ใน an idle task hook

        ต้องมีอย่างน้อยหนึ่ง task เสมอที่พร้อมรัน. ดังนั้นจึงจำเป็นที่ the hook function ไม่เรียก API functions ใดๆที่อาจเป็นเหตุให้ the idle task โดนบล็อก
        (vTaskDelay(), หรือ a queue or semaphore function ที่มี a block time, สำหรับตัวอย่าง). มันจะ ok สำหรับ co-routines โดนบล็อกภายใน the hook
        function.

     2. สร้าง an idle priority task เพื่อจัดให้มีใช้ the functionality.

         จะเป็นการแก้ปัญหาที่หยืดหยุ่นมากกว่าแต่มี a higher RAM usage overhead.

ดู the Embedded software application design section สำหรับข้อมูลที่มากขึ้นเกี่ยวกับการใช้ an idle hook.

เพื่อสร้าง an idle hook:

     1. ตั้งค่า configUSE_IDLE_HOOK เป็น 1 ใน FreeRTOSConfig.h.

     2. กำหนด a function ที่มี the following name and prototype:

                    void vApplicationIdleHook( void );

เป็นเรื่องปกติที่จะใช้ the idle hook function เพื่อวาง the microcontroller CPU ลงใน a power saving mode.
Title: Re: FreeRTOS
Post by: tha on November 01, 2021, 05:10:54 PM
https://www.freertos.org/a00016.html (https://www.freertos.org/a00016.html)

Hook Functions

Idle Hook Function

The idle task สามารถเป็นทางเลือกเรียก an application ที่กำหนด hook (หรือ callback) function - the idle hook. The idle task รันที่ the very lowest priority, ดังนั้น an idle hook function ดังกล่าวจะได้รับการปฏิบัติเฉพาะเมื่อไม่มี tasks of higher priority ที่สามารถรัน. นี้ทำให้ makes the idle hook function ในอุดมคติได้วางใส่ the processor ลงใน a low power state - จัดให้มี an automatic power saving เมื่อไรก็ตามไม่มีการประมวลผลถูกดำเนินการ.

The idle hook จะถุกเรียกก็ต่อเมื่อ configUSE_IDLE_HOOK ถูกเซ็ตเป็น 1 ภายใน FreeRTOSConfig.h. เมื่อสิ่งนี้ถูกเซ็ต the application ต้องจัดให้มี the hook function ด้วยต้นแบบต่อไปนี้:

                          void vApplicationIdleHook( void );

The idle hook จะถูกเรียกซ้ำๆตราบเท่าที่ the idle task กำลังรัน. เป็นสิ่งสำคัญยิ่งที่ the idle hook function ไม่เรียก API functions ใดๆที่สามารถเป็นเหตุให้มันโดนบล็อก.  นอกจากนี้, ถ้า the application ใช้ the vTaskDelete() API function ดังนั้น the idle task hook ต้องถูกยอมให้รีเทิร์นเป็นระยะๆ (นี้เนื่องจาก the idle task รับผิดชอบต่อการทำความสะอาด the resources ที่ถูกจัดสรรโดย the RTOS kernel ไปยัง the task ที่ถูกลบแล้วนั้น).
Title: Re: FreeRTOS
Post by: tha on November 02, 2021, 05:41:25 AM
Tick Hook Function

The tick interrupt สามารถเป็นทางเลือกเรียก an application ที่กำหนด hook (or callback) function - the tick hook. The tick hook จัดให้มีสถานที่ที่สะดวกเพื่อจัดให้มีใช้ timer functionality.

The tick hook จะถุกเรียกก็ต่อเมื่อ configUSE_TICK_HOOK ถูกเซ็ตเป็น 1 ภายใน FreeRTOSConfig.h. เมื่อสิ่งนี้ถูกเซ็ต the application ต้องจัดให้มี the hook function ด้วยต้นแบบต่อไปนี้:

                        void vApplicationTickHook( void );

vApplicationTickHook() ปฏิบัติจากภายในภายใน an ISR ดังนั้นต้องสั้นมากๆ, ไม่ใช้ stack มากเกิน, และไม่เรียก API functions ใดๆที่ไม่สิ้นสุดใน "FromISR" or "FROM_ISR".

ดู the demo application file crhook.c สำหรับตัวอย่างของวิธีที่ใช้ a tick hook.
Title: Re: FreeRTOS
Post by: tha on November 02, 2021, 05:44:33 AM
Malloc Failed Hook Function

The memory allocation schemes ที่จัดให้มีใช้โดย heap_1.c, heap_2.c, heap_3.c, heap_4.c and heap_5.c สามารถมีทางเลือกรวมเอา a malloc() failure hook (or callback) function ที่สามารถถูกกำหนดค่าให้ได้รับการเรียกถ้า pvPortMalloc() เคยรีเทิร์น NULL.

การกำหนด the malloc() failure hook จะช่วยวินิจฉัยปัญหาที่เหตูมาจากการขาดของ heap memory - โดยเฉพาะอย่างยิ่งเมื่อการเรียกไปยัง pvPortMalloc() ล้มเหลวภายใน an API function.

The malloc failed hook จะถุกเรียกก็ต่อเมื่อ configUSE_MALLOC_FAILED_HOOK ถูกเซ็ตเป็น 1 ภายใน FreeRTOSConfig.h. เมื่อสิ่งนี้ถูกเซ็ต the application ต้องจัดให้มี the hook function ด้วยต้นแบบต่อไปนี้:

                      void vApplicationMallocFailedHook( void );

Stack Overflow Hook Function

ดู the Stack Overflow Protection page สำหรับรายละเอียด.
Title: Re: FreeRTOS
Post by: tha on November 02, 2021, 06:07:37 AM
Daemon Task Startup Hook

The RTOS daemon task เป็นเหมือนอย่าง the Timer Service Task. บางทีมันถูกอ้างอิงถึงเป็นดั่ง the daemon task เนื่องจาก the task ตอนนี้ถูกใช้สำหรับมากว่าเป็นเพียง servicing timers.

ถ้า configUSE_DAEMON_TASK_STARTUP_HOOK ถูกเซ็ตเป็น 1 ใน FreeRTOSConfig.h ดังนั้น the Daemon Task Startup Hook จะถูกเรียกทันทีที่ the Daemon Task สตาร์ทการปฏิบัติเป็นครั้งแรก. สิ่งนี้มีประโยชน์ถ้า the application รวมเอา initialisation code ที่จะให้ประโยชน์จากการปฏิบัติหลังจาก the scheduler ถูกสตาร์ทแล้ว, ซึ่งยอมให้ the initialisation code ใช้ the RTOS functionality ได้.

ถ้า configUSE_DAEMON_TASK_STARTUP_HOOK ถูกเซ็ตเป็น 1 ดังนั้น the application writer ต้องจัดให้มี an implementation of the Daemon Task startup hook function ที่มี the following name and prototype.

                  void vApplicationDaemonTaskStartupHook( void );
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 06:22:07 AM
https://www.freertos.org/symmetric-multiprocessing-introduction.html (https://www.freertos.org/symmetric-multiprocessing-introduction.html)

Symmetric Multiprocessing (SMP) with FreeRTOS

SMP รองรับใน the FreeRTOS Kernel ทำให้ one instance of the FreeRTOS kernel วางกำหนดเวลา tasks พาดผ่าน multiple identical processor cores. The core architectures ต้องเหมือนกันและแชร์ the same memory.

บนหน้านี้:

     •  Getting started
     •  Modifying an application to use FreeRTOS SMP functionality
     •  SMP specific APIs
     •  SMP specific hook functions
     •  SMP specific configuration options

Getting Started with FreeRTOS and SMP

วิธีที่ง่ายที่สุดที่จะเริ่มต้นคือใช้หนึ่งของ the following pre-configured example projects:

     •  XCORE AI
     •  Raspberry Pi Pico

Modifying an Application to use FreeRTOS SMP Functionality

The FreeRTOS API ยังคงเหมือนเดิมอย่างมากระหว่าง single core and SMP versions ยกเว้นสำหรับส่วนเพิ่มเติมเหล่านี้. ดังนั้น, an application ที่เขียนสำหรับ the FreeRTOS single core version ควรคอมไพล์ด้วย the SMP version ด้วยไม่มีความพยายามแม้แต่น้อย. อย่างไรก็ตาม อาจมีปัญหาการทำงานบางอย่าง เนื่องจากข้อสันนิษฐานบางอย่างที่เป็นจริงสำหรับ single core applications อาจไม่เป็นจริงสำหรับ multi-core applications อีกต่อไป.

สมมติฐานทั่วไปประการหนึ่งก็คือ a lower priority task ไม่สามารถรันในระหว่างที่ a higher priority task กำลังรันอยู่. ขณะที่สิ่งนี้เป็นจริงบน a single core, มันไม่เป็นจริงอีกต่อไปสำหรับ multi-cores, เนื่องจากหลาย tasks สามารถรันได้พร้อมกัน. ถ้า the application อาศัย relative task priorities เพื่อจัดให้มี mutual exclusion(กันไว้สำหรับคนพิเศษ), มันอาจสังเกตุเห็นผลที่ไม่ได้คาดหวังใน a multi-core environment. The application writer มีตัวเลือกสองทางเพื่อแก้ไขปัญหานี้:

     1. ทางเลือกที่ดีที่สุดคืออัปเดต the application เพื่อให้มันไม่ขึ้นอยู่กับ task priorities และใช้ synchronization primitives แทน.
     2. อีกทางเลือกหนึ่งคือการปักหมุด the tasks ทั้งหมดซึ่งต้องไม่กำลังรันอยู่พร้อมกันไปยัง core หนึ่งโดยใช้ the vTaskCoreAffinitySet API.
     3. อีกทางเลือกหนึ่งคือกำหนด configRUN_MULTIPLE_PRIORITIES เป็น 0 ซึ่งจะแน่ใจว่าหลาย tasks จะรันพร้อมกันเฉพาะถ้าพวกมันมี the same priority.
         โปรดทราบว่าการดำเนินการนี้อาจส่งผลให้มีการใช้ประโยชน์น้อยเกินไปและใส่บาง cores ไปยัง idle เมื่อพวกมันควรถูกใช้เพื่อรัน low priority tasks อื่นๆ.

สมมติฐานทั่วไปอีกประการหนึ่งคือว่า ISRs ไม่สามารถทำงานพร้อมกันกับแต่ละอื่นๆหรือกับ tasks อื่นๆ ได้. สิ่งนี้ไม่เป็นความจริงอีกต่อไปใน a multi-core environment และ the application writer จำเป็นต้องเพื่อให้แน่ใจว่า mutual exclusion ที่เหมาะสมในขณะที่เข้าถึง data ที่แชร์ระหว่าง tasks and ISRs. The macros taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() สามารถถูกใช้ใน ISRs และ the macros taskENTER_CRITICAL() and taskEXIT_CRITICAL() สามารถถูกใช้ใน tasks เพื่อจัดให้มีการกันไว้สำหรับคนพิเศษดังกล่าว.
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 07:18:36 AM
SMP Specific APIs

APIs เพิ่มเติมเหล่านี้มีให้ใช้ประโยชน์กับ the FreeRTOS-SMP kernel:

     •  vTaskCoreAffinitySet
     •  vTaskCoreAffinityGet
     •  vTaskPreemptionDisable
     •  vTaskPreemptionEnable

vTaskCoreAffinitySet

void vTaskCoreAffinitySet( const TaskHandle_t xTask, UBaseType_t uxCoreAffinityMask );

configUSE_CORE_AFFINITY ต้องถูกกำหนดเป็น 1 สำหรับฟังชั่นนี้เพื่อมีให้ใช้ประโยชน์.

เซ็ต the core affinity mask สำหรับ a task, เช่น the cores ที่ a task สามารถรันได้.

Parameters:

     •  xTask - The handle of the task ที่ the core affinity mask เป็นสำหรับ. การส่งผ่าน NULL จะตั้งค่า the core affinity mask สำหรับ the calling task.

     •  uxCoreAffinityMask - A bitwise value ที่แสดง the cores ที่ the task สามารถรันได้. Cores ถูกให้จำนวนจาก 0 ถึง configNUM_CORES - 1. ตัวอย่างเช่น, เพื่อให้แน่ใจว่า a task สามารถรันบน core 0 and core 1, ตั้งค่า uxCoreAffinityMask เป็น 0x03.

Example Usage:

(https://i.imgur.com/O2KQPU4.png)
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 07:22:34 AM
vTaskCoreAffinityGet

UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask );

configUSE_CORE_AFFINITY ต้องถูกกำหนดเป็น 1 สำหรับฟังชั่นนี้เพื่อมีให้ใช้ประโยชน์.

รับ the core affinity mask สำหรับ a task, เช่น the cores ที่ a task สามารถรันได้.

Parameters:

     •  xTask - The handle of the task ที่ the core affinity mask เป็นสำหรับ. การส่งผ่าน NULL จะตั้งค่า the core affinity mask สำหรับ the calling task.

Returns:

     •  The core affinity mask, ซึ่งเป็น a bitwise value ที่แสดง the cores ที่ a task สามารถรันได้. Cores ถูกให้จำนวนจาก 0 ถึง configNUM_CORES - 1.
         ตัวอย่างเช่น, ถ้า a task สามารถรันบน core 0 and core 1, the core affinity mask เป็น 0x03.

Example Usage:

(https://i.imgur.com/MRNvPaB.png)
(https://i.imgur.com/zN6BANq.png)
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 08:00:47 AM
vTaskPreemptionDisable

void vTaskPreemptionDisable( const TaskHandle_t xTask );

configUSE_TASK_PREEMPTION_DISABLE ต้องถูกกำหนดเป็น 1 สำหรับฟังชั่นนี้มีให้ใช้ประโยชน์.

ปิดการใช้งานการยึดเอาไว้ก่อนสำหรับ a task.

Parameters:

     •  xTask - The handle of the task ซึ่ง preemption จะถูกปิดการใช้งาน. การส่งผ่าน NULL จะปิดการใช้งาน preemption สำหรับ the calling task.

Example Usage:

(https://i.imgur.com/c4Fegjo.png)
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 08:23:20 AM
vTaskPreemptionEnable

void vTaskPreemptionEnable( const TaskHandle_t xTask );

configUSE_TASK_PREEMPTION_DISABLE ต้องถูกกำหนดเป็น 1 สำหรับฟังชั่นนี้มีให้ใช้ประโยชน์.

เปิดการใช้งานการยึดเอาไว้ก่อนสำหรับ a task.

Parameters:

     •  xTask - The handle of the task ซึ่ง preemption จะถูกเปิดการใช้งาน. การส่งผ่าน NULL จะเปิดการใช้งาน preemption สำหรับ the calling task.

Example Usage:

(https://i.imgur.com/niLcnyj.png)
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 03:49:12 PM
SMP Specific Hook Functions

Minimal Idle Hook Function

The FreeRTOS SMP kernel มีสองประเภทของ Idle tasks:

     1. Idle Task - มี the standard Idle task ที่ใช้ใน single core FreeRTOS applications.
     2. Minimal Idle Tasks - มี configNUM_CORES - 1 Minimal Idle tasks ซึ่งถูกรันบน idle cores และซึ่งไม่ทำงานอะไร.

The minimal idle tasks สามารถเป็นทางเลือกเรียก an application-defined hook (or callback) function - the minimal idle hook. The minimal idle tasks รันที่ the very lowest priority, ดังนั้น an idle hook function ดังกล่าวจะรันเฉพาะเมื่อไมมี tasks of higher priority ที่สามารถรัน.

The minimal idle hook จะถูกเรียกก็ต่อเมื่อ configUSE_MINIMAL_IDLE_HOOK ถูกเซ็ตเป็น 1 ภายใน FreeRTOSConfig.h. เมื่อสิ่งนี้ถูกเซ็ต, the application ต้องจัดให้มี the hook function ที่มีต้นแบบต่อไปนี้ :

void vApplicationMinimalIdleHook( void );

The minimal idle hook ถูกเรียกซ้ำๆโดย the minimal idle tasks ตราบเท่าที่หนึ่งใดๆของพวกมันกำลังรันอยู่. เป็นสิ่งสำคัญยิ่งที่ the minimal idle hook function ไม่เรียก API functions ใดๆทีอาจเป็นเหตุให้มันโดนบล็อก.
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 04:19:50 PM
SMP Specific Configuration Options

additional configuration options เหล่านี้มีให้ใช้ประโยชน์กับ the FreeRTOS-SMP Kernel:

     •  configNUM_CORES
     •  configRUN_MULTIPLE_PRIORITIES
     •  configUSE_CORE_AFFINITY
     •  configUSE_TASK_PREEMPTION_DISABLE

configNUM_CORES

เซ็ตจำนวนของ available processor cores.

configRUN_MULTIPLE_PRIORITIES

ใน a single core FreeRTOS application, a lower priority task จะไม่เคยรันถ้ามี a higher priority task ที่สามารถรันอยู่. ใน an SMP FreeRTOS application the RTOS kernel จะรัน tasks ได่มากเท่าที่ cores มีให้ใช้ประโยชน์ - ดังนั้นเป็นไปได้ที่ a lower priority task จะรันบน core หนึ่งในเวลาเดียวกันกับ a higher priority task รันบนอีก core หนึ่ง. ซึ่งสามารถเป็นเหตุให้เกิดปัญหาถ้า your application or library ถูกเขียนไว้สำหรับ a single core environment, และดังนั้นจึงเป็นการตั้งสมมติฐานเกี่ยวกับลำดับที่ tasks ดำเนินการ. ดังนั้น configRUN_MULTIPLE_PRIORITIES ถูกจัดให้มีเพื่อควบคุมพฤติกรรมนี้.

ถ้า configRUN_MULTIPLE_PRIORITIES ถูกกำหนดเป็น 0, หลาย tasks อาจรันพร้อมกันเฉพาะถเาพวกมันมี priority ที่เท่ากัน - รักษากระบวนทัศน์ของ a lower priority task ไม่เคยรันถ้ามี a higher priority task ที่สามารถรันอยู่. ถ้า configRUN_MULTIPLE_PRIORITIES ถูกกำหนดเป็น 1, หลาย tasks ที่มี priorities ต่างกันอาจรันพร้อมกันได้ - ดังนั้น a higher and lower priority task อาจรันบนต่าง cores กันในเวลาเดียวกัน.
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 04:29:22 PM
configUSE_CORE_AFFINITY

ยอมให้ the application writer ควบคุมซึ่ง cores ใดที่ a task สามารถรันบน. ถ้า configUSE_CORE_AFFINITY ถูกกำหนดเป็น 1, vTaskCoreAffinitySet สามารถถูกใช้ควบคุมซึ่ง cores ใดที่ a task สามารถรันบน, and vTaskCoreAffinityGet สามารถถูกใช้เพื่อสอบถามซึ่ง cores ใดที่ a task สามารถรันบน. ถ้า configUSE_CORE_AFFINITY เป็น 0 ดังนั้น the FreeRTOS scheduler ปล่อยฟรีให้รัน task ใดๆบน available core ใดๆ.
Title: Re: FreeRTOS
Post by: tha on November 03, 2021, 04:34:58 PM
configUSE_TASK_PREEMPTION_DISABLE

ใน a single core FreeRTOS application the FreeRTOS scheduler สามารถถูกกำหนดเป็นอย่างใดอย่างหนึ่ง pre-emptive หรือ co-operative. ดูการกำหนดของ configUSE_PREEMPTION. ใน SMP FreeRTOS application, ถ้า configUSE_TASK_PREEMPTION_DISABLE ถูกกำหนดเป็น 1, ดังนั้น tasks แต่ละรายสามารถถูกเซ็ตเป็นอย่างใดอย่างหนึ่ง pre-emptive or co-operative mode โดยใช้ the vTaskPreemptionDisable and vTaskPreemptionEnable API functions.
Title: Re: FreeRTOS
Post by: tha on November 04, 2021, 05:50:26 AM
https://www.freertos.org/FreeRTOS-MPU-memory-protection-unit.html (https://www.freertos.org/FreeRTOS-MPU-memory-protection-unit.html)

Memory Protection Unit (MPU) Support

          ดู the Upgrading section สำหรับข้อมูลข่าวสารเกี่ยวกับการอัปเดต FreeRTOS-MPU projects ไปเป็น FreeRTOS V10.4.0

บนหน้านี้:

     •  Introduction
          •  MPU costs and benefits
          •  MPU strategies

     •  Examples
          •  Upgrading MPU projects to FreeRTOS V10.4.0
          •  FreeRTOS-MPU Specifics
          •  FreeRTOS-MPU features
          •  Creating tasks

Introduction

FreeRTOS จัดให้มี official Memory Protection Unit (MPU) สนับสนุนบน ARMv7-M (Cortex-M3, Cortex-M4 and Cortex-M7 microcontrollers) and ARMv8-M (Cortex-M23 and Cortex-M33 microcontroller) cores:

     •  มีสอง FreeRTOS ports for ARMv7-M cores, หนึ่งที่รวม MPU support และหนึ่งที่ไม่.
     •  มีเพียงหนึ่ง FreeRTOS port สำหรับ ARMv8-M cores เนื่องจาก MPU support เป็น a compile time option.

Third parties รักษา MPU ports ไปยัง microcontroller cores อื่นๆ.

MPU costs and benefits

FreeRTOS MPU ports ช่วยให้ microcontroller applications มีความแข็งแกร่งมากขึ้นและมีความปลอดภัยมากขึ้นโดยอย่างแรกเปิดการใช้งาน tasks ให้รันในอย่างใดอย่างหนึ่ง privileged or unprivileged mode, และอย่างที่สองจำกัดการเข้าถึงไปยัง resources อย่างเช่น RAM, executable code, peripherals, and memory ที่เกินขีดจำกัดของ a task's stack. ประโยชน์มหาศาลถูกได้รับจาก, ตัวอย่างเช่น, การป้องกัน code จากที่เคยปฏิบัติจาก RAM การทำเช่นนี้จะช่วยป้องกันสู้ many attack vectors อย่างเช่น buffer overflow exploits หรือการปฏิบัติของ malicious code ที่โหลดลงใน RAM.

การใช้ MPU จำเป็นต้องทำให้ application design ซับซ้อนมากขึ้น เนื่องจากอย่างแรกคือ the MPU's memory region restrictions ต้องถูกกำหนดและอธิบายไปยัง the RTOS, และอย่างที่สองคือ the MPU จะจำกัดว่า application tasks ใดสามารถและไม่สามารถทำ.
Title: Re: FreeRTOS
Post by: tha on November 04, 2021, 06:57:51 AM
MPU strategies

การสร้าง an application ที่จำกัดทุกๆ task ไปยัง memory region ของมันเองอาจปลอดภัยที่สุด, แต่ก็ the most complex ในการ design และ implement อีกด้วย. บ่อยครั้ง เป็นการดีที่สุดที่จะใช้ MPU เพื่อสร้าง a pseudo process and thread model - โดยที่ groups of threads ถูกยอมให้แชร์ a memory space. ตัวอย่างเช่น, สร้าง memory space หนึ่งสามารถเข้าถึงได้ไปยัง trusted first party code, และอีก memory space หนึ่งที่สามารถเข้าถึงได้เฉพาะไปยัง untrusted third party code.
Title: Re: FreeRTOS
Post by: tha on November 04, 2021, 07:12:41 AM
Additional examples

The LPC17xx edition of the FreeRTOS eBook บรรจุ a chapter on using FreeRTOS-MPU, แม้ว่าข้อมูลที่มีอยู่จะล้าสมัยเล็กน้อย

The Using FreeRTOS on ARMv8-M Microcontrollers blog กล่าวถึงวิธีใช้ MPU บน ARMv8-M microcontrollers.

The demo project ใน the FreeRTOS/Demo/CORTEX_MPU_Simulator_Keil_GCC directory, ซึ่งใช้ Keil uVision เพื่อบิวด์และจำลอง a GCC project, จัดให้มี a worked example ที่ไม่ต้องการ hardware เฉพาะใดๆ. FreeRTOS-MPU examples projects เพิ่มเติมได้รวม the Nuvoton NuMaker-PFM-M2351 demo และ the NXP LPCXpresso55S69 demo demo, ท่ามกลางตัวอย่างอื่นๆ.

[The FreeRTOS-MPU demo projects ที่อยู่ใน the FreeRTOS/Demo/CORTEX_MPU_LPC1768_GCC_RedSuite and FreeRTOS/Demo/CORTEX_MPU_LM3Sxxxx_Rowley directories ถูกยกเลิกก่อนการเปิดตัวของ FreeRTOS V9.0.0]
Title: Re: FreeRTOS
Post by: tha on November 04, 2021, 11:39:17 AM
Upgrade Information

FreeRTOS MPU ports ถูกอัปเดตเพื่อตอบสนองต่อ the end-user feedback. section นี้อธิบายการเปลี่ยนแปลงที่จำเป็นในการอัปเกรดเป็น FreeRTOS V10.4.0 หรือใหม่กว่า และก่อนหน้านั้น เพื่ออัปเกรดเป็น FreeRTOS V10.3.0 หรือใหม่กว่า.

Changes in FreeRTOS version 10.4.0:

     •  FreeRTOS V10.4.0 แนะนำตัวแปรใหม่ __privileged_functions_start__ เพื่อแสดง the starting location ของ the privileged code. มันจำเป็นต้องถูก
         ส่งออกจาก linker script ในลักษณะเดียวกับ the pre-existing linker variables อย่างเช่น __privileged_functions_end__.

         ถ้าคุณได้รับ a linker error ใน an earlier created project สำหรับเครื่องหมายที่ยังไม่ได้แก้ไข __privileged_functions_start__, คุณจำเป็นต้องถูกส่ง
         ออกตัวแปร __privileged_functions_start__ ที่มีค่าเท่ากับ __FLASH_segment_start__.

     •  Heap memory ตอนนี้ถูกวางใน the privileged section, และด้วยเหตุนี้, unprivileged tasks ไม่สามารถเรียก pvPortMalloc() or vPortFree().

     •  xTaskCreate() ไม่สามารถถูกใช้สร้าง an unprivileged task อีกต่อไป. ใช้ xTaskCreateRestricted() แทน.

     •  FreeRTOS-MPU ports สำหรับ ARM Cortex-M4 microcontrollers ตอนนี้รองรับ microcontrollers ที่มี 16 MPU regions. เพื่อใช้ 16 regions เซ็ต
        configTOTAL_MPU_REGIONS เป็น 16 ใน FreeRTOSConfig.h.

     •  Application writers ตอนนี้สามารถแทนที่ค่าเริ่มต้นสำหรับ the for TEX, Shareable (S), Cacheable (C) and Bufferable (B) bits เป็นสำหรับ MPU
         regions ที่ครอบคุม Flash and RAM โดยการกำหนด configTEX_S_C_B_FLASH and configTEX_S_C_B_SRAM ตามลำดับใน FreeRTOSConfig.h.

Changes in FreeRTOS version 10.3.0:

     •  ขณะนี้สามารถป้องกัน privilege escalations ใดๆที่มาจากภายนอกของ the kernel code (นอกเหนือจาก escalations ที่กระทำโดย the hardware เองเมื่อ
        an interrupt ถูกเข้าสู่. เพื่อทำดังนี้เซ็ต configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY เป็น 1 ใน FreeRTOSConfig.h, และกำหนด the
        linker variables __syscalls_flash_start__ and __syscalls_flash_end__ เป็น the start and end addresses ของ the system calls memory
        ตามลำดับ.
Title: Re: FreeRTOS
Post by: tha on November 04, 2021, 01:18:41 PM
FreeRTOS-MPU Specifics

FreeRTOS-MPU features

     •  เข้ากันได้กับ the standard ARM Cortex-M3 and Cortex-M4F ports.

     •  Tasks สามารถถูกสร้างเพื่อรันในอย่างใดอย่างหนึ่ง Privileged mode หรือ Unprivileged mode. Unprivileged tasks สามารถเข้าถึงเฉพาะ stack ของพวก
         มันเองและสูงถึง three user definable memory regions (three ต่อ task). User definable memory regions ถูกกำหนดไปยัง tasks เมื่อ the task
         ถูกสร้าง, และสามารถถูกกำหนดค่าใหม่ขณะ run time ถ้าต้องการ.

     •  User definable memory regions สามารถถูกกำหนดพารามิเตอร์แต่ละรายแยกกันได้. ตัวอย่างเช่น, บาง regions อาจถูกเซ็ตให้อ่านเพียงอย่างเดียว, ในขณะที่
         regions อื่นอาจถูกเซ็ตไม่ให้ปฏิบัติงานได้ (ไม่ดำเนินการใดๆ หรือเพียงแค่ XN, ใน ARM terminology), เป็นต้น.

     •  ไม่มี data memory ถูกแชร์ระหว่าง Unprivileged tasks, แต่ Unprivileged tasks สามารถส่งผ่าน messages ไปยังแต่ละ tasks อื่นๆโดยใช้ the
         standard queue and semaphore mechanisms. Shared memory regions สามารถถูกสร้างอย่างชัดเจนโดยการใช้ a user definable memory
         region แต่เราไม่แนะนำ.

     •  A Privileged mode task สามารถเซ็ตตัวเองลงใน Unprivileged mode, แต่เมื่ออยู่ใน Unprivileged mode มันไม่สามารถเซ็ตตัวเองกลับไปยัง Privileged
         mode.

     •  The FreeRTOS API ที่อยู่ใน a region of Flash ที่สามารถถูกเข้าถึงเฉพาะในขณะที่ the microcontroller อยู่ใน Privileged mode (การเรียก an API
         function เป็นเหตุให้เกิดการสลับชั่วคราวไปยัง Privilege mode).

     •  The data ที่ดูแลรักษาโดย the RTOS kernel ( non stack data ทั้งหมดที่เป็นส่วนตัวถึง the FreeRTOS source files) ที่อยู่ใน a region of RAM ที่
         สามารถถูกเข้าถึงเฉพาะในขณะที่ the microcontroller อยู่ใน Privileged mode.

     •  System peripherals สามารถถูกเข้าถึงเฉพาะในขณะที่ the microcontroller อยู่ใน Privileged mode. Standard peripherals (UARTs, ฯลฯ.) สามารถ
         เข้าถึงได้โดย code ใดๆแต่สามารถถูกป้องกันอย่างชัดเจนโดยใช้ a user definable memory region.
Title: Re: FreeRTOS
Post by: tha on November 04, 2021, 04:40:34 PM
Creating Tasks

FreeRTOS-MPU port สามารถมีสองปรเภทของ tasks:

     1.  Privileged Tasks: A privileged task สามารถเข้าถึงไปยัง the entire memory map. Privileged tasks สามารถถูกสร้างโดยใช้อย่างใด
          อย่างหนึ่ง the xTaskCreate() หรือ xTaskCreateRestricted() API function.

     2.  Unprivileged Tasks: An unprivileged task สามารถเข้าถึงเฉพาะไปยัง stack ของมันเท่านั้น.  นอกจากนี้, มันสามารถถูกยินยอมให้เข้าถึงสูงถึง three user
          definable memory regions (three ต่อ task). Unprivilged tasks สามารถถูกสร้างโดยใช้เฉพาะ the xTaskCreateRestricted() API. โปรดทราบว่า
          xTaskCreate() API ต้องไม่ถูกใช้สร้าง an unprivileged task.

ถ้า a task ต้องการใช้ the MPU ดังนั้น the following additional information ต้องถูกจัดให้มี:

     •  The address ของ the task stack.
     •  The start, size and access parameters สำหรับสูงถึง three user definable memory regions.

จำนวนทั้งหมดของ parameters ที่ต้องการเพื่อสร้าง a task ดังนั้นค่อนข้างมากทีเดียว.  เพื่อให้การสร้าง tasks รับรู้ MPU ง่ายขึ้น FreeRTOS-MPU ใช้ฟังก์ชัน API ที่เรียกว่า xTaskCreateRestricted(). นี้ยอมให้ทั้งหมดแต่หนึ่งเดียวของ the parameters ถูกกำหนดใน a const struct, ที่มี the struct ถูกส่งผ่าน (by reference) ลงใน xTaskCreateRestricted() เป็น a single parameter.

A Privileged mode task สามารถเรียก portSWITCH_TO_USER_MODE() เพื่อเซ็ตตัวเองลงใน Unprivileged mode. A task ที่กำลังรันใน Unprivileged mode ไม่สามารถเซ็ตตัวเองลงใน Privileged mode.

The memory regions ที่จัดสรรไปยัง a task สามารถถูกเปลี่ยนโดยใช้ vTaskAllocateMPURegions(). ดู the xTaskCreateRestricted() and vTaskAllocateMPURegions() API documentation สำหรับข้อมูลที่มากขึ้น.
Title: Re: FreeRTOS
Post by: tha on November 05, 2021, 05:51:33 AM
https://www.freertos.org/thread-local-storage-pointers.html (https://www.freertos.org/thread-local-storage-pointers.html)

Thread Local Storage Pointers

Introduction

Thread local storage (or TLS) ยอมให้ the application writer เก็บค่าไว้ภายใน a task's control block, ทำให้ the value เฉพาะเจาะจงไปยัง (local to) the task ของมันเอง, และยอมให้แต่ละ task มีมีค่าเฉพาะของตัวเอง.

Thread local storage มักถูกใช้เก็บค่าที่ a single threaded program มิฉะนั้นจะเก็บไว้ใน a global variable. ตัวอย่างเช่น, many libraries รวม a global variable ที่เรียกว่า errno. ถ้า a library function รีเทิร์น an error condition ไปยัง the calling function, ดังนั้น the calling function สามารถตรวจสอบ the errno value เพื่อตัดสินว่า the error คืออะไร. ใน a single threaded application มันเพียงพอที่จะประกาศ errno เป็น a global variable, แต่ใน a multi-threaded application แต่ละ thread (task) ต้องมี unique errno value ของตัวเอง - มิฉะนั้น one task อาจอ่าน an errno value ที่ถูกมุ่งหมายสำหรับ task อื่น.

Thread Local Storage Pointers

FreeRTOS จัดให้ the application writer ด้วย a flexible thread local storage mechanism ผ่านทางการใช้ของ thread local storage pointers.

The configNUM_THREAD_LOCAL_STORAGE_POINTERS คอมไพล์ time configuration constant dimensions a per task อาร์เรย์ของ void pointers (void*). The vTaskSetThreadLocalStoragePointer() API function ถูกใช้เพื่อเซ็ตค่าภายในอาร์เรย์ของ void pointers, and pvTaskGetThreadLocalStoragePointer() API function ถูกใช้เพื่ออ่านค่าจากอาร์เรย์ของ void pointers.
Title: Re: FreeRTOS
Post by: tha on November 05, 2021, 06:15:46 AM
Thread Local Integers

ค่าที่มีขนาดน้อยกว่าหรือเท่ากับขนาดของ a void pointer สามารถถูกเก็บโดยตรงภายใน the thread local storage pointer array. ตัวอย่างเช่น, ถ้า sizeof( void * ) เป็น 4 ดังนั้น a 32-bit value สามารถถูกเก็บใน a void pointer variable, โดยใช้ a simple cast เพื่อหลีกเลี่ยง compiler warnings. อย่างไรก็ตาม, ถ้า sizeof( void * ) เป็น 2, ดังนั้นมีเพียง a 16-bit value เท่านั้นสามารถถูกเก็บได้โดยตรง.

(https://i.imgur.com/yuVicjt.png)
Title: Re: FreeRTOS
Post by: tha on November 05, 2021, 06:48:04 AM
Thread Local Structures

ตัวอย่างก่อนหน้านี้เก็บค่าลงใน the thread local storage array โดยตรง. ตัวอย่างถัดไปนี้จะสาธิตวิธีการใช้ค่าใน the array เป็น a pointer ไปยัง a structure ที่มีอยู่ที่อื่นใน memory.

(https://i.imgur.com/ksQIu8R.png)
Title: Re: FreeRTOS
Post by: tha on November 06, 2021, 05:20:17 AM
https://www.freertos.org/Pend-on-multiple-rtos-objects.html (https://www.freertos.org/Pend-on-multiple-rtos-objects.html)

Blocking on Multiple RTOS Objects

Introduction to Queue Sets

Queue sets เป็น a FreeRTOS feature ที่ช่วยให้ an RTOS task โดนบล็อก (ค้าง) เมื่อมีการรับจากหลายๆ queues และ/หรือ semaphores ในเวลาเดียวกัน. Queues and semaphores ถูกจัดกลุ่มลงในชุด, ดังนั้น, แทนที่จะบล็อกบน queue หรือ semaphore แต่ละรายการ, a task โดนบล็อกบนชุด(the set) แทน.

หมายเหตุ: แม้ว่าบางครั้งอาจจำเป็นต้องบล็อก (รอดำเนินการ) บน queue มากกว่าหนึ่ง queue หากคุณรวม FreeRTOS กับรหัสดั้งเดิมของบริษัทอื่น การออกแบบที่ปราศจากข้อจำกัดดังกล่าวโดยปกติสามารถบรรลุฟังก์ชันการทำงานเดียวกันได้อย่างมีประสิทธิภาพมากขึ้นโดยใช้ทางเลือกรูปแบบการออกแบบอื่น ที่บันทึกไว้ที่ด้านล่างของหน้านี้
Title: Re: FreeRTOS
Post by: tha on November 06, 2021, 05:43:01 AM
Using Queue Sets

Queue sets ถูกใช้ในลักษณะเดียวกันกับ the select() API function, และ functions ที่เกี่ยวข้อง, ที่เป็นส่วนหนึ่งของ the standard Berkeley sockets networking API.

Queue sets สามารถบรรจุ queues and semaphores, ซึ่งรวมกันถูกทราบว่าเป็น queue set members. API function parameters และ return values ที่สามารถได้รับอย่างใดอย่างหนึ่ง a queue handle หรือ a semaphore handle ใช้ the QueueSetMemberHandle_t type. ตัวแปรของประเภท QueueHandle_t และ SemaphoreHandle_t โดยปกติสามารถแปลงโดยปริยายเป็น an QueueSetMemberHandle_t parameter หรือ return value โดยไม่มี compiler warnings ถูกสร้าง (explicit casting ไปยังและจาก the QueueSetMemberHandle_t type โดยปกติไม่ถูกต้องการ).

Creating a queue set                        ก่อนที่ a queue set สามารถถูกใช้มันต้องถูกสร้างโดยใช้ the xQueueCreateSet() API function. เมื่อสร้างแล้ว
                                                               the queue set จะถูกอ้างอิงโดยตัวแปรของประเภท QueueSetHandle_t.

Adding a member to a                     The xQueueAddToSet() API function ถูกใช้เพิ่ม a queue or semaphore ไปยัง a queue set.
queue set

Blocking (pending) on a                    The xQueueSelectFromSet() API function ถูกใช้เพื่อทดสอบว่า the set members ใดๆพร้อมสำหรับการ
queue set                                             อ่าน - โดยที่การอ่านหมายถึง 'receiving' เมื่อ the member เป็น a queue, และ 'taking' เมื่อ the member
                                                                เป็น a semaphore.

                                                                เช่นเดียวกับเมื่อใช้ the xQueueReceive() and xSemaphoreTake() API functions,
                                                                xQueueSelectFromSet() ยอมให้ the calling task มีทางเลือกโดนบล็อกได้จนกระทั่ง a member of the
                                                                queue set พร้อมสำหรับการอ่าน.

                                                                NULL ถูกรีเทิร์นถ้าการเรียกไปยัง xQueueSelectFromSet() หมดเวลาลง. มิฉะนั้น xQueueSelectFromSet()
                                                                จะรีเทิร์น the handle of the queue set member ที่พร้อมสำหรับการอ่าน, ยอมให้ the calling task เรียก
                                                                xQueueReceive() or xSemaphoreTake() โดยทันที (บน a queue handle or semaphore handle ตาม
                                                                ลำดับ)  พร้อมรับประกันว่าการดำเนินการจะสำเร็จ.
Title: Re: FreeRTOS
Post by: tha on November 06, 2021, 06:16:51 AM
Source Code Examples

The xQueueCreateSet() API function documentation page รวมไว้ a source code example.

The standard demo/test file ที่เรียกว่า QueueSet.c (อยู่ใน the FreeRTOS/Demo/Common/Minimal/ directory of the main FreeRTOS zip file download) บรรจุตัวอย่างที่ทำให้เข้าใจ.
Title: Re: FreeRTOS
Post by: tha on November 06, 2021, 07:26:36 AM
Alternatives to Using Queue Sets

แม้ว่าจะมี a specific integration ปล่อยออกมาว่าจำเป็นต้องบล็อกบน multiple queues, the same functionality ปกติสามารถทำให้สำเร็จได้ด้วย a lower code size, RAM size, and run time overhead โดยใช้ a single queue. The FreeRTOS+UDP implementation จัดให้มีตัวอย่างที่สะดวกเกี่ยวกับวิธีการดำเนินการนี้ และอธิบายไว้ในส่วนย่อยต่อไปนี้.

UDP/IP Stack: Problem Definition

The task ที่จัดการ the FreeRTOS+UDP stack คือ event driven. มีหลาย event sources. บาง events ไม่มี data ใดๆที่เกี่ยวข้องกับพวกมัน. บาง events มีแปรผันจำนวนของ data ที่เกี่ยวข้องกับพวกมัน. Events ประกอบด้วย:

     •  The Ethernet hardware รับ a frame. Frames บรรจุใหญ่และแปรผันจำนวนของ data

     •  The Ethernet hardware เสร็จสมบูรณ์ the transmission of a frame, ปล่อยฟรี network and DMA buffers

     •  An application task ส่ง a packet. Packets สามารถบรรจุใหญ่และแปรผันจำนวนของ data.

     •  software timers มากมาย, รวมทั้ง the ARP timer. Timer events จะไม่เกี่ยวข้องกับ data ใดๆ.
Title: Re: FreeRTOS
Post by: tha on November 06, 2021, 09:08:48 AM
UDP/IP Stack: Solution

The UDP/IP stack สามารถใช้ a different queue สำหรับแต่ละ event source, จากนั้นใช้ a queue set เพื่อบล็อกบน the queues ทั้งหมดในคราวเดียว. แทน, the UDP/IP stack:

     1.  กำหนด a structure ที่บรรจุ a member เพื่อเก็บ the event type, และ member อีกตัวหนึ่งเพื่อเก็บ the data (หรือ a pointer to the data) ที่ถูก
          เกี่ยวข้องกับ the event.

     2.  ใช้ a single queue ที่ถูกสร้างเพื่อเก็บ the defined structure. แต่ละ event source โพสต์ไปยัง the same queue.

The structure definition ถูกแสดงข้างล่าง.

(https://i.imgur.com/DIAAARZ.png)
Title: Re: FreeRTOS
Post by: tha on November 06, 2021, 09:26:51 AM
ตัวอย่างของวิธีที่ structure นี้ถูกใช้:

     •  เมื่อ the ARP timer หมดเวลาลงมันจะส่ง an event ไปยัง the queue ด้วย eEventType ที่เซ็ตถึง eARPTimerEvent (an enumerated type). ARP
         timer events ไม่ถูกเกี่ยวข้องกับ data ใดๆดังนั้น pvData ไม่ถูกเซ็ต.

     •  เมื่อ the Ethernet driver รับ a frame มันจะส่ง an event ไปยัง the queue ด้วย eEventType ที่เซ็ตถึง eEthernetRxEvent, และ pvData เซ็ตเพื่อชี้ไป
         ยัง the frame buffer.

     •  และสิ่งอื่นๆ

The UDP/IP task ประมวลผล events โดยใช้ a simple loop:

(https://i.imgur.com/5df68yz.png)
(https://i.imgur.com/1Y4Nhd0.png)
Title: Re: FreeRTOS
Post by: tha on November 07, 2021, 06:40:25 AM
https://www.freertos.org/deferred_interrupt_processing.html (https://www.freertos.org/deferred_interrupt_processing.html)

Deferred Interrupt Handling

What is deferred interrupt handling?

ใน FreeRTOS, a deferred interrupt handler หมายถึง an RTOS task ที่ถูกปลดบล็อก (ถูกทริก) โดย an interrupt service routine (ISR) ดังนั้น the processing ที่จำเป็นโดย the interrupt สามารถถูกดำเนินการใน the unblocked task, แทนที่จะโดยตรงใน the ISR. The mechanism แตกต่างจาก standard interrupt processing, ที่ the processing ทั้งหมดถูกดำเนินการภายใน the ISR, เนื่องจากส่วนใหญ่ของ the processing ถูกเลื่อนออกไปจนกระทั่งหลังจาก the ISR ได้ออกแล้ว:

     •  Standard ISR Processing

        Standard ISR processing มักจะเกี่ยวข้องกับการบันทึกสาเหตุของ the interrupt, การเคลียร์ the interrupt, จากนั้นดำเนินการประมวลผลใดๆที่จำเป็นโดย
        the interrupt, ทั้งหมดนี้อยู่ภายใน the ISR ของมันเอง.

     •  Deferred Interrupt Processing

        Deferred interrupt processing มักจะเกี่ยวข้องกับการบันทึกสาเหตุของ the interrupt และการเคลียร์ the interrupt ภายใน the ISR, แต่จากนั้นปลดบล็อก
        an RTOS task ดังนั้น the processing ที่จำเป็นโดย the interrupt สามารถถูกดำเนินการโดย the unblocked task, แทนที่จะภายใน the ISR.

        ถ้า the task ซึ่งประมวลผลอินเตอร์รัพท์ที่เลื่อนออกไปถูกกำหนด a high enough priority ดังนั้น the ISR จะรีเทิร์นโดยตรงไปยัง the unblocked task
        (the interrupt จะอินเตอร์รัพท์ task หนึ่ง, แต่จากนั้นรีเทิร์นไปยัง a different task), เป็นผลให้ the processing ทั้งหมดที่จำเป็นโดย the interrupt ถูก
        ดำเนินการอย่างต่อเนื่องในเวลา (โดยไม่มีช่องว่าง) ราวกับว่า the processing ถูกดำเนินการใน the ISR มันเอง.  ดังจะสามารถเห็นได้จากภาพด้านล่าง ซึ่ง the
        interrupt processing ทั้งหมดเกิดขึ้นระหว่างเวลา t2 และ t4 แม้ว่าส่วนหนึ่งของการประมวลผลจะถูกดำเนินการโดย a task ก็ตาม.

        (https://i.imgur.com/qpUu4tO.png)

โดยอ้างอิงจากภาพด้านบน:

     1.  ณ เวลา t2: A low priority task ถูกยึดไว้ก่อนโดย an interrupt.
     2.  ณ เวลา t3: The ISR รีเทิร์นโดยตรงไปยัง a task ที่เคยถูกปลดบล็อกจากภายใน the ISR. ส่วนใหญ่ของ interrupt processing ถูกดำเนินการภายใน the
          unblocked task.
     3.  ณ เวลา t4: The task ที่เคยถูกปลดบล็อกโดย the ISR รีเทิร์นไปยัง the Blocked state เพื่อรอสำหรับ the next interrupt, ยอมให้ the lower priority
          application task ต่อเนื่องการปฏิบัติของมัน
Title: Re: FreeRTOS
Post by: tha on November 07, 2021, 08:10:49 AM
When to use deferred interrupt handling

embedded engineers ส่วนใหญ่จะพยายามลดเวลาที่ใช้ใน an ISR (เพื่อลดการกระวนกระวายใจใน the system, เปิดการใช้งาน interrupts อื่นของ the same or lower priority เพื่อปฏิบัติ, เพิ่มการตอบสนอง interrupt ให้สูงสุด ฯลฯ), และเทคนิคของการเลื่อน interrupt processing ไปยัง a task จัดให้มีวิธีที่สะดวกของการทำให้เป็นผลสำเร็จนี้. อย่างไรก็ตาม, กลไกของการปลดบล็อกในครั้งแรกและจากนั้นสวิทช์ไปยัง, an RTOS task เองนั้นใช้เวลาจำกัด ดังนั้นโดยทั่วไปแล้ว an application จะได้ประโยชน์จาก deferring interrupt processing เฉาพะถ้า the processing:

     •  จำเป็นต้องดำเนินการเป็นเวลานาน, หรือ
     •  จะได้รับประโยชน์จากการใช้ the full RTOS API, แทนที่จะเพียง the ISR safe API, หรือ
     •  จำเป็นต้องดำเนินการที่ไม่ได้กำหนดไว้ภายในขอบเขตที่สมเหตุสมผล
Title: Re: FreeRTOS
Post by: tha on November 07, 2021, 11:37:39 AM
Techniques for deferring interrupt processing to a task

วิธีของ deferring interrupts ไปยัง tasks แบ่งออกเป็นสองประเภท:

     1. Centralised Deferred Interrupt Handling

         Centralised deferred interrupt handling ถูกเรียกเช่นนั้นเนื่องจากแต่ละ interrupt ที่ใช้วิธีนี้ปฏิบัติใน the context of the same RTOS daemon task.
         The RTOS daemon task ถูกสร้างโดย FreeRTOS, และถูกเรียกอีกอย่าง the timer service task.

         เพื่อเลื่อน interrupt processing ไปยัง the RTOS daemon task ส่งผ่าน a pointer to the interrupt processing function เป็น the
         xFunctionToPend parameter ในการเรียกถึง xTimerPendFunctionCallFromISR() API function. ดู the xTimerPendFunctionCallFromISR()
         documentation page สำหรับ a worked example.

         ข้อดีของ centralised deferred interrupt handling รวมเอาการใช้ทรัพยากรน้อยที่สุด, เนื่องจากแต่ละ deferred interrupt handler ใช้ the same task.

         ข้อเสียของ centralised deferred interrupt handling ได้แก่:

              •  the deferred interrupt handler functions ทั้งหมดปฏิบัติใน the context of the same RTOS daemon task, และดังนั้นปฏิบัติด้วย the same
                  RTOS task priority.

              •  xTimerPendFunctionCallFromISR() ส่ง pointers to the deferred interrupt handling functions ไปยัง the RTOS daemon task ผ่านทาง
                  the timer command queue. ดังนั้น the RTOS daemon task ประมวลผล the functions ในลำดับที่พวกมันถูกรับบน the queue,  ไม่จำเป็นต้อง
                  ใน interrupt priority order.

              •  การเขียนไปยัง, และจากนั้นต่อมาอ่านจาก, the timer command queue จะเพิ่มเวลาแฝงเพิ่มเติม.
Title: Re: FreeRTOS
Post by: tha on November 07, 2021, 12:17:23 PM
     2. Application Controlled Deferred Interrupt Handling

         Application controlled deferred interrupt handling ถูกเรียกเช่นนั้นเนื่องจากแต่ละ interrupt ที่ใช้วิธีนี้ปฏิบัติใน the context of a task ที่สร้างโดย the
         application writer. ดู the Using an RTOS Task Notification as a Light Weight Counting Semaphore documentation page สำหรับ a
         worked example.

         ข้อดีของ application controlled deferred interrupt handling ได้แก่:

              •  ลดเวลาแฝง (function pointers ไม่ถูกส่งผ่านผ่านทาง a queue).

              •  ความสามารถในการกำหนด a different priority ไปยังแต่ละ deferred interrupt handling RTOS task - ยอมให้ the relative priority of
                  deferred interrupt task ตรงกับ the relative priority of their respective interrupts.

         ข้อเสียของ application controlled deferred interrupt handling ได้แก่ การใช้ทรัพยากรที่มากขึ้น เนื่องจากโดยทั่วไปมี tasks มากขึ้นถูกต้องการ.
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 05:27:15 AM
https://www.freertos.org/low-power-tickless-rtos.html (https://www.freertos.org/low-power-tickless-rtos.html)

Low Power Support

Tickless Idle Mode [ดูอีกด้วย Low Power Features For ARM Cortex-M MCUs] [ดูอีกด้วย Tickless Demos on SAM4L, RX100, STM32L, CEC1302 and EFM32 MCUs ]

Power Saving Introduction

เป็นเรื่องปกติที่จะลดพลังงานที่ใช้โดย the microcontroller ที่ FreeRTOS กำลังรันอยู่โดยการใช้ the Idle task hook เพื่อวาง the microcontroller ลงใน a low power state. การประหยัดพลังงานที่สามารถทำได้โดยวิธีง่ายๆ นี้ถูกจำกัดโดยความจำเป็นในการออกเป็นระยะๆ แล้วกลับเข้าสู่ the low power state อีกเพื่อประมวลผล tick interrupts. นอกจากนี้, ถ้า the frequency ของ the tick interrupt สูงเกินไป, the energy and time ที่ใช้เข้าสู่และจากนั้นออกจาก a low power state สำหรับทุกๆ tick จะมีค่ามากกว่าการประหยัดพลังงานที่อาจเกิดขึ้นสำหรับทั้งหมด ยกเว้น the lightest power saving modes. The FreeRTOS tickless idle mode หยุด the periodic tick interrupt ในระหว่าง idle periods (คาบบเวลาที่ไม่มี application tasks ที่สามารถปฏิบัติ), จากนั้นทำการปรับที่ถูกต้องไปยัง the RTOS tick count value เมื่อ the tick interrupt ถูกสตาร์ทใหม่. การหยุด the tick interrupt ยอมให้ the microcontroller ยังคงอยู่ใน a deep power saving state จนกระทั่งอย่างใดอย่างหนึ่ง an interrupt เกิดขึ้น, หรือถึงเวลาสำหรับ the RTOS kernel ที่จะเปลี่ยนผ่าน a task ลงใน the Ready state. 
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 07:28:42 AM
The portSUPPRESS_TICKS_AND_SLEEP() Macro

    portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime )

Built in tickless idle functionality ถูกเปิดการใช้งานโดยการกำหนด configUSE_TICKLESS_IDLE เป็น 1 ใน FreeRTOSConfig.h (สำหรับ ports ที่รองรับคุณลักษณะนี้). User defined tickless idle functionality สามารถถูกจัดให้มีสำหรับ FreeRTOS port ใดๆ (รวมถึงเหล่านั้นที่รวม a built in implementation) โดยการกำหนด configUSE_TICKLESS_IDLE เป็น 2 ใน FreeRTOSConfig.h. เมื่อ the tickless idle functionality ถูกเปิดการใช้งน the kernel จะเรียก the portSUPPRESS_TICKS_AND_SLEEP() macro เมื่อสองเงื่อนไขต่อไปนี้เป็นจริงทั้งคู่ :

     1. The Idle task เป็น the only task ที่สามารถรันเนื่องจาก the application tasks ทั้งหมดอยู่ในอย่างใดอย่างหนึ่ง the Blocked state หรือใน the
         Suspended state.

     2. อย่างน้อย n further complete tick periods จะผ่านก่อนที่ the kernel จะครบกำหนดการเปลี่ยน an application task ออกจาก the Blocked state,
         โดยที่ n ถูกเซ็ตโดย the configEXPECTED_IDLE_TIME_BEFORE_SLEEP definition ใน FreeRTOSConfig.h.
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 11:14:21 AM
ค่าของ portSUPPRESS_TICKS_AND_SLEEP()'s single parameter เท่ากับจำนวนทั้งหมดของ tick periods ก่อนที่ a task จะครบกำหนดถูกย้ายลงใน the Ready state. The parameter value ดังนั้นคือเวลาที่ the microcontroller สามารถอยู่อย่างปลอดภัยใน a deep sleep state, โดยที่ the tick interrupt ถูกหยุด (ถูกระงับ). หมายเหตุ: ถ้า eTaskConfirmSleepModeStatus() รีเทิร์น eNoTasksWaitingTimeout เมื่อมันถูกเรียกจากภายใน portSUPPRESS_TICKS_AND_SLEEP() ดังนั้น the microcontroller สามารถคงอยู่ใน a deep sleep state โดยไม่มีกำหนด. eTaskConfirmSleepModeStatus() จะรีเทิร์น eNoTasksWaitingTimeout เฉพาะเมื่อเงื่อนไขต่อไปนี้เป็นจริง:

     1. Software timers ไม่ได้กำลังถูกใช้, ดังนั้น the scheduler จะไม่ครบกำหนดปฏิบัติ a timer callback function ที่เวลาใดๆในอนาคต.
     2. the application tasks ทั้งหมดอยู่ในอย่างใดอย่างหนึ่ง the Suspended state, หรือใน the Blocked state ที่มี an infinite timeout (a timeout value of
         portMAX_DELAY), ดังนั้น the scheduler จะไม่ครบกำหนดเปลี่ยนผ่าน a task ออกจาก the Blocked state ในเวลาที่กำหนดใดๆในอนาคต.
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 11:37:45 AM
เพื่อหลีกเลี่ยงสภาวะการแข่ง the RTOS scheduler จะถูกแขวน(ระงับหยุดไว้)ก่อน portSUPPRESS_TICKS_AND_SLEEP() จะถูกเรียก, และกลับมาทำงานต่อเมื่อ portSUPPRESS_TICKS_AND_SLEEP() เสร็จสมบูรณ์. สิ่งนี้ทำให้แน่ใจว่า application tasks ไม่สามารถปฏิบัติระหว่าง the microcontroller กำลังออกจาก low power state ของมันและ portSUPPRESS_TICKS_AND_SLEEP() กำลังเสร็จสมบูรณ์การปฏิบัติของมัน. นอกจากนี้ จำเป็นสำหรับ the portSUPPRESS_TICKS_AND_SLEEP() function เพื่อสร้าง a small critical section ระหว่าง the tick source ที่กำลังถูกหยุดและ the microcontroller กำลังเข้าสู่ the sleep state. eTaskConfirmSleepModeStatus() ควรถูกเรียกจาก critical section นี้. GCC, IAR and Keil ARM Cortex-M ports ทั้งหมดตอนนี้จัดให้มี a default portSUPPRESS_TICKS_AND_SLEEP() implementation. ข้อมูลสำคัญเกี่ยวกับการใช้ the ARM Cortex-M implementation ถูกจัดให้มีบน the Low Power Features For ARM Cortex-M MCUs page. Default implementations จะถูกเพิ่มไปยัง FreeRTOS ports อื่นๆ เมื่อเวลาผ่านไป ในระหว่างนี้ , the hooks ที่อธิบายข้างล่างสามารถถูกใช้เพื่อเพิ่ม tickless functionality ไปยัง port ใดๆ.
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 12:14:04 PM
Implementing portSUPPRESS_TICKS_AND_SLEEP()

ถ้า the FreeRTOS port in use ไม่จัดให้มี a default implementation of portSUPPRESS_TICKS_AND_SLEEP(), ดังนั้น the application writer สามารถจัดให้ implementation ของตัวเองโดยการกำหนด portSUPPRESS_TICKS_AND_SLEEP() ใน FreeRTOSConfig.h. ถ้า the FreeRTOS port in use จัดให้มี a default implementation of portSUPPRESS_TICKS_AND_SLEEP(), ดังนั้น the application writer สามารถแทนที่ the default implementation โดยการกำหนด portSUPPRESS_TICKS_AND_SLEEP() ใน FreeRTOSConfig.h. ซอร์สโค้ดต่อไปนี้เป็นตัวอย่างของวิธีที่ portSUPPRESS_TICKS_AND_SLEEP() อาจถูกจัดให้มีใช้งานโดย an application writer. ตัวอย่างเป็นพื้นฐาน และจะนำสู่การเลื่อนไถลระหว่างเวลาที่ดูแลโดยเคอร์เนลและเวลาในปฏิทิน Official FreeRTOS versions พยายามเอาออกการเลื่อนไถลใดๆ (เท่าที่เป็นไปได้)) โดยการจัดให้มี a more intricate implementation. ของ the functions calls ที่แสดงในตัวอย่าง, เฉพาะ vTaskStepTick() and eTaskConfirmSleepModeStatus() เท่านั้นที่เป็นส่วนหนึ่งของ the FreeRTOS API. ฟังชั่นอื่นๆที่เฉพาะเจาะจงไปยัง the clocks and power saving modes มีให้ใช้ประโยชน์บน the hardware in use, และดังนั้น, ต้องถูกจัดให้มีโดย the application writer.

(ตัวอย่างซ้อสโค้ดดูในลิ้งค์เอานะครับ)
https://www.freertos.org/low-power-tickless-rtos.html (https://www.freertos.org/low-power-tickless-rtos.html)
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 02:41:26 PM
https://www.freertos.org/rtos-trace-macros.html (https://www.freertos.org/rtos-trace-macros.html)

Trace Hook Macros

      มีให้ใช้ประโยชน์อีกด้วย: FreeRTOS+Trace, a third party trace tool สำหรับ FreeRTOS - ส่วนหนึ่งของ the FreeRTOS+ ecosystem

Description

Trace hook macros เป็นคุณสมบัติที่ทรงพลังมากที่อนุญาตให้คุณรวบรวมข้อมูลเกี่ยวกับวิธีที่ your embedded application กำลังมีพฤติกรรม.

จุดสำคัญที่น่าสนใจภายใน the FreeRTOS source code ที่บรรจุมาโครที่ว่างเปล่าที่ an application สามารถกำหนดใหม่สำหรับวัตถุประสงค์ของ providing application specific trace facilities. The application จำเป็นจัดให้มีใช้เฉพาะมาโครเหล่านั้นที่น่าสนใจโดยเฉพาะเท่านั้น - โดยที่มาโครที่ไม่ใช้ยังคงว่างเปล่าและดังนั้นไม่มีผลกระทบต่อ the application timing.
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 02:58:58 PM
Examples

ต่อไปนี้คือบางตัวอย่างของวิธีที่มาโครเหล่านี้สามารถถูกใช้:

     •  การตั้งค่า a digital output เพื่อแสดงซึ่ง task กำลังปฏิบัติ - ยอมให้ a logic analyzer ถูกใช้เพื่อดูและบันทึก the task execution sequence and timing.
     •  ในทำนองเดียวกัน - การตั้งค่า an analogue output เป็น a voltage ที่แสดงซึ่ง task กำลังปฏิบัติ - ยอมให้ an oscilloscope ถูกใช้เพื่อดูและบันทึก the task
         execution sequence and timing.
     •  การบันทึก task execution sequences, task timing, RTOS kernel events and API calls สำหรับ offline analysis.
     •  การรวม RTOS kernel events เข้ากับ third party debuggers.
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 03:12:05 PM
Example 1

The FreeRTOS task tag functionality จัดให้มีกลไกง่ายๆสำหรับการตั้งค่าการบันทึกโดยทาง digital or analogue outputs. ตัวอย่างเช่น, the tag value สามารถถูกตั้งค่าเป็น a voltage ที่เป็นหนึ่งเดียวไปยัง task นั้น. The traceSWITCHED_IN() macro ดังนั้นสามารถถูกกำหนดเพื่อตั้งค่าง่ายๆ an analogue output เป็นค่าที่สัมพันธ์กันกับ the task ที่กำลังถูกสลับเข้า. ตัวอย่างเช่น :

(https://i.imgur.com/CLsMOMl.png)
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 03:19:11 PM
Example 2

API call logging สามารถถูกใช้เพื่อบันทึกสาเหตุที่ a context switch ที่เกิดขึ้น. RTOS kernel call logging สามารถถูกใช้เพื่อบันทึกลำดับที่ tasks ปฏิบัติ. ตัวอย่างเช่น:

(https://i.imgur.com/yORHvJX.png)
Title: Re: FreeRTOS
Post by: tha on November 08, 2021, 04:39:53 PM
Defining

Macros ที่ถูกเรียกจากภายใน interrupts, โดยเฉพาะ the tick interrupt, ต้องปฏิบัติโดยเร็วและไม่ใช้ stack space มาก. การตั้งค่าตัวแปร, การเขียนไปยัง trace registers, หรือการเอ๊าพุทไปยัง ports สามารถยอมรับได้ทั้งหมด. การพยายามถึง fprintf() บันทึก data ไปยัง a slow disk จะไม่ทำงาน!

Macro definitions ต้องเกิดขึ้นก่อนการรวมของ FreeRTOS.h. สถานที่ง่ายสุดเพือกำหนด trace macros คือที่ด้านล่างของ FreeRTOSConfig.h, หรือใน a separate header file ที่ถูกรวมจากตอนล่างของ FreeRTOSConfig.h

ตารางข้างล่างอธิบาย the available macros. The macro parameters ถุกใช้เพื่อแสดงซึ่ง task, queue, semaphore or mutex ถุกเกี่ยวข้องกับ the event ที่กำลังถูกบันทึก.

(ตารางดูในลิ้งค์เอานะครับ ไม่แปลนะครับ สั้นๆใช้กูเกิ้ลแปลเอาก็ได้หรือถามผู้รู้เอา ไม่ค่อยจะได้ใช้ ผ่านก่อน!)
https://www.freertos.org/rtos-trace-macros.html (https://www.freertos.org/rtos-trace-macros.html)
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 05:07:16 AM
https://www.freertos.org/rtos-run-time-stats.html (https://www.freertos.org/rtos-run-time-stats.html)

Run Time Statistics

Description

FreeRTOS สามารถเลือกรวบรวมข้อมูลเกี่ยวกับจำนวนของ processing time ที่ถุกใช้แล้วโดยแต่ละ task. The vTaskGetRunTimeStats() API function ดังนั้นสามาถถูกใช้เพื่อแสดงข้อมูลนี้ใน a tabular format, ดังแสดงบนด้านขวา.

สองค่าถูกให้สำหรับแต่ละ task:

     1. Abs Time (absolute time)

         นี้คือ the total 'time' ที่ the task ถูกปฏิบัติแล้วจริงๆ (the total time ที่ the task อยู่แล้วใน the Running state). มันขึ้นอยู่กับผู้ใช้จะเลือก a suitable
         time base สำหรับ application ของพวกเขา.

     2. % Time (percentage time)

         นี้แสดง the same information เป็นหลักเป็น a percentage of the total processing time แทนที่จะเป็น an absolute time.
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 06:03:02 AM
Configuration and Usage

Three macros ถูกต้องการ. เหล่านี้สามารถถูกกำหนดใน FreeRTOSConfig.h.

     1. configGENERATE_RUN_TIME_STATS

         การรวบรวมของCollection of run time statistics ถูกเปิดการใช้งานโดย #defining configGENERATE_RUN_TIME_STATS เป็น 1. เมื่อสิ่งนี้ถูกตั้งค่าแล้ว
         มาโครอื่นอีกสองต้องถูกกำหนดด้วยเพื่อสำเร็จ a successful compilation.

     2. portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()

          The run time statistics time base จำเป้นต้องมีความละเอียดที่สูงกว่า the tick interrupt - มืฉะนั้น the statistics อาจไม่ถูกต้องเกินกว่าจะเป็นประโยชน์
          อย่างแท้จริง ขอแนะนำให้ทำให้ the time base ระหว่าง 10 และ 100 เท่าเร็วกว่า the tick interrupt. ยิ่งฐานเวลาเร็วเท่าไหร่ สถิติก็จะยิ่งแม่นยำมากขึ้นเท่านั้น
          แต่ the timer value จะโอเวอร์โฟลเร็วขึ้นด้วย.

          ถ้า configGENERATE_RUN_TIME_STATS ถูกกำหนดเป็น 1 ดังนั้น the RTOS kernel จะเรียก
          portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() โดยอัตโนมัติเมื่อมันถูกสตาร์ท (มันถูกเรียกจากภายใน the vTaskStartScheduler() API
          function). มีวัตถุประสงค์เพื่อให้ the application designer ใช้ the macro เพื่อกำหนดค่า a suitable time base. บางตัวอย่างถุกจัดให้มีข้างล่าง.

     3. portGET_RUN_TIME_COUNTER_VALUE()

         มาโครนี้ควรรีเทิร์นเพียง the current 'time', ตามที่กำหนดค่าโดย portCONFIGURE_TIMER_FOR_RUN_TIME_STATS(). อีกครั้งบางตัวอย่างถุกจัดให้มี
         ข้างล่าง.

The vTaskGetRunTimeStats() API function ถูกใช้เพื่อดึง the gathered statistics.
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 07:37:49 AM
Examples

[ท่ามกลางคนอื่น ๆ มากมาย] The LPC17xx LPCXpresso and the LM3Sxxxx Eclipse web server demo applications ถูกกำหนดค่าเพื่อสร้าง run time stats.

LM3Sxxxx example

The LM3Sxxxx Eclipse demo application ได้รวม a 20KHz timer test แล้ว. The interrupt handler ถูกอัปเดตเพื่อเพิ่มง่ายๆ a variable ที่เรียกว่า ulHighFrequencyTimerTicks แต่ละครั้งที่มันปฏิบัติ. portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() ตั้งค่าง่ายๆตัวแปรนี้เป็น 0 และ portGET_RUN_TIME_COUNTER_VALUE() จะรีเทิร์นค่าของมัน. เพื่อจัดให้มีใช้สิ่งนี้สองสามบรรทัดต่อไปนี้ถูกเพิ่มแล้วไปยัง FreeRTOSConfig.h:

(https://i.imgur.com/YghTepY.png)

LPC17xx example

The LPC17xx demo application ไม่ได้รวม the high frequency interrupt test, ดังนั้น portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() ถูกใช้กำหนดค่า the timer 0 peripheral เพื่อสร้าง the time base. portGET_RUN_TIME_COUNTER_VALUE() รีเทิร์นง่ายๆ the current timer 0 counter value. สิ่งนี้ถูกจัดให้มีใช้โดยใช้ the following functions and macros.

(https://i.imgur.com/zRfEfa8.png)
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 09:52:11 AM
https://www.freertos.org/FreeRTOS-porting-guide.html (https://www.freertos.org/FreeRTOS-porting-guide.html)

Creating a New FreeRTOS Port

คุณไม่จำเป็นต้องอ่านหรือทำความเข้าใจหน้านี้หากใช้ one of the many existing ports และ demo applications!

การพอร์ต FreeRTOS ไปยังไมโครคอนโทรลเลอร์ที่ต่างไปจากเดิมอย่างสิ้นเชิงและยังไม่ได้รับการสนับสนุนนั้นไม่ใช่เรื่องง่าย จุดมุ่งหมายของหน้านี้คือการอธิบายการเตรียมการบ้านเบื้องต้นที่จำเป็นในการเริ่มต้นพอร์ตใหม่

แต่ละพอร์ตมีความไม่ซ้ำกันในระดับปานกลางและขึ้นอยู่กับ processor and tools ที่ใช้เป็นอย่างมาก ดังนั้นหน้านี้จึงไม่สามารถระบุรายละเอียดเฉพาะของพอร์ตได้ อย่างไรก็ตาม มีพอร์ต FreeRTOS อื่นๆ ที่มีอยู่มากมายอยู่แล้ว และขอแนะนำให้ใช้พอร์ตเหล่านี้เพื่ออ้างอิง

การพอร์ตภายในตระกูลโปรเซสเซอร์เดียวกันเป็นงานที่ตรงไปตรงมามากขึ้น - ตัวอย่างเช่น จากอุปกรณ์ที่ใช้ ARM7 เครื่องหนึ่งไปยังอีกเครื่องหนึ่ง หน้าเอกสารที่มีรายละเอียดวิธีแก้ไข an existing demo application จะเป็นจุดเริ่มต้นที่ดีในการเริ่มอ่านหากนี่คือเป้าหมายของคุณ.
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 10:54:09 AM
Setting up the Directory Structure

The FreeRTOS kernel source code โดยทั่วไปถูกบรรจุภายใน 3 source files (4 ถ้า co-routines ถูกใช้)  ซึ่งเป็นเรื่องปกติสำหรับพอร์ตทั้งหมด, และหนึ่งหรือสอง 'port' files ที่ปรับแต่ง the RTOS kernel ไปยัง a particular architecture.

ขั้นตอนที่แนะนำ:

     1. ดาวน์โหลดเวอร์ชันล่าสุดของซอร์สโค้ด FreeRTOS

     2. แตก the files ลงในตำแหน่งที่สะดวก ดูแลรักษา the directory structure

     3. ทำความคุ้นเคยด้วยตัวคุณเองกับ the source code organisation และ directory structure.

     4. สร้าง a directory ที่จะบรรจุ the 'port' files สำหรับ the [architecture] port. ตามแบบแผนที่ระบุไว้ในลิงก์, the directory ควรถูกอยู่ในฟอร์ม:
         FreeRTOS/Source/portable/[compiler name]/[processor name]. ตัวอย่างเช่น หากคุณกำลังใช้ the GCC compiler คุณสามารถสร้าง a
         [architecture] directory จาก the existing FreeRTOS/Source/portable/GCC directory.

     5. ก๊อปปี้ empty port.c and portmacro.h files ลงใน the directory ที่คุณเพิ่งสร้าง. ไฟล์เหล่านี้ควรบรรจุเพียงต้นขั้วของ the functions and macro's ที่
         ต้องการจัดให้มีใช้งาน. ดู port.c and portmacro.h files ที่มีอยู่สำหรับรายการของฟังชั่นและมาโครดังกล่าว. คุณสามารถสร้าง a stub file จากหนึ่งของไฟล์ที่มี
         อยู่เหล่านี้โดยการลบง่ายๆเนื้อความ the function and macro.
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 12:02:30 PM
     6. ถ้า the stack บน the microcontroller ที่ถุกพอร์ตเติบโตลงต่ำจาก high memory ไปยัง low memory ดังนั้นตั้งค่า portSTACK_GROWTH ใน
         portmacro.h เป็น -1, มิฉะนั้นตั้งค่า portSTACK_GROWTH เป็น 1.

     7. สร้าง a directory ที่จะบรรจุ the demo application files สำหรับ the [architecture] port. ทำตามแบบแผนอีกครั้งนี้ควรถูกอยู่ในฟอร์ม
         FreeRTOS/Demo/[architecture_compiler], หรือบางสิ่งที่คล้ายกัน.

     8. ก๊อปปี้ FreeRTOSConfig.h and main.c files ที่มีอยู่ลงใน the directory ที่เพิ่งสร้าง. เหล่านี้ควรถูกแก้ไขให้เป็นเพียงไฟล์ต้นขั้วอีกครั้ง.

     9. ดูที่ไฟล์ FreeRTOSConfig.h มันมีมาโครบางตัวที่จะต้องตั้งค่าสำหรับฮาร์ดแวร์ที่คุณเลือก

     10. สร้าง a directory จาก the directory ที่เพิ่งสร้างและเรียกมันว่า ParTest (อาจเป้น FreeRTOS/Demo/[architecture_compiler]/ParTest). ก๊อปปี้ลงใน
           directory นี้ a ParTest.c stub file.

     11. ParTest.c บรรจุ three simple functions เพื่อ:

               1. ตั้งค่าบาง GPIO ที่สามารถแฟรช LEDs สองสามดวง,
               2. เซ็ตและเคลียร์ LED ที่ระบุ, และ
               3. สลับสถานะของ an LED.

ฟังก์ชันทั้งสามนี้จำเป็นต้องนำไปใช้สำหรับบอร์ดพัฒนาของคุณ การมีเอาต์พุต LED ทำงานจะช่วยอำนวยความสะดวกในส่วนที่เหลือของการทำงานที่จำเป็น ดูไฟล์ ParTest.c ที่มีอยู่จำนวนมากที่รวมอยู่ในโปรเจ็กต์สาธิตอื่นๆเป็นตัวอย่าง (ชื่อ ParTest เป็นความผิดปกติในอดีตสำหรับการทดสอบพอร์ต PARallel) และหน้าที่อธิบายวิธีแก้ไขแอปพลิเคชันสาธิตที่มีอยู่สำหรับข้อมูลเกี่ยวกับการเขียนและการทดสอบฟังก์ชัน ParTest.c
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 02:10:14 PM
Creating a Project

ตอนนี้ไฟล์ที่ต้องการทั้งหมดอยู่ในที่ที่คุณต้องการเพื่อสร้าง a project (หรือ makefile) ที่จะบิวด์พวกมันสำเร็จลง. เห็นได้ชัดว่าพวกมันมีโครงเท่านั้นจึงจะยังไม่ทำอะไร แต่เมื่อกำลังบิวด์ ต้นขั้วจะถูกแทนที่ด้วยฟังก์ชันการทำงานทีละน้อย.

The project จะจำเป็นต้องบรรจุไฟล์ต่อไปนี้:

     •  Source/tasks.c
     •  Source/Queue.c
     •  Source/List.c
     •  Source/portable/[compiler name]/[processor name]/port.c
     •  Source/portable/MemMang/heap_1.c (or heap_2.c or heap_3.c or heap_4.c)
     •  Demo/[processor name]/main.c
     •  Demo/[Processor name]/ParTest/ParTest.c

ไดเร็กทอรีต่อไปนี้จำเป็นต้องอยู่ใน the include path - โปรดใช้ relative paths จาก the Demo/[Process name] directory - ไม่ใช่ absolute paths:

     •  Demo/Common (i.e. ../Common)
     •  Demo/[Processor Name]
     •  Source/include
     •  Source/portable/[Compiler name]/[Processor name]
Title: Re: FreeRTOS
Post by: tha on November 09, 2021, 02:24:07 PM
Implementing the Stubs

ตอนนี้ยากลำบากนิดหน่อย. เมื่อ the project กำลังคอมไพล์ the portable layer stubs ต้องการมีการนำไปใช้. ขอแนะนำว่า pxPortInitialiseStack() เป็น the first function ที่ถูกนำมาใช้. ในการใช้งาน pxPortInitialiseStack() สิ่งแรกคุณต้องตัดสินใจโดยขึ้นอยู่กับ your task context stack frame structure, ซึ่งขึ้นอยู่กับ architecture มาก

Getting Help

อย่าลืมว่า WITTENSTEIN high integrity systems เสนอ a full porting and testing service!
Title: Re: FreeRTOS
Post by: tha on November 10, 2021, 05:30:14 AM
https://www.freertos.org/a00086.html (https://www.freertos.org/a00086.html)

Legacy Trace Utility

[The page แสดงเอกสาร the legacy trace utility - ซึ่งถูกลบออกไปแล้วใน FreeRTOS V7.1.0 - ผู้ใช้อาจพบ the newer Trace Hook Macros ที่ง่ายกว่าและมีพลังมากกว่าให้ใช้.]

The trace visualisation utility ยอมให้ the RTOS activity ถูกตรวจสอบ.

มันบันทึกลำดับซึ่ง tasks ถูกให้ microcontroller processing time.

เพื่อใช้ผลประโยชน์นี้มาโคร configUSE_TRACE_FACILITY ต้องถูกกำหนดเป็น 1 ภายใน FreeRTOSConfig.h เมื่อ the application ถูกคอมไพล์. ดู the configuration section ใน the RTOS API documentation สำหรับข้อมูลที่มากขึ้น.

The trace ถูกสตาร์ทโดยการเรียก vTaskStartTrace() และสิ้นสุดโดยการเรียก ulTaskEndTrace(). มันจะสิ้นสุดโดยอัตโนมัติถ้า buffer ของมันกลายมาเป็นเติม.

The completed trace buffer สามารถถูกเก็บลง disk สำหรับการตรวจสอบแบบออฟไลน์. The DOS/Windows utility tracecon.exe จะแปลง the stored buffer เป็น a tab delimited text file. ดังนั้นสิ่งนี้สามารถถูกเปิดและตรวจสอบใน a spread sheet application.

ด้านล่างคือ a 10 millisecond example output ที่รวบรวมจาก the AMD 186 demo application. The x axis แสดงเวลาที่ผ่านไป, และ the y axis คือหมายเลขของ the task ที่กำลังรัน.

(https://i.imgur.com/bBDf0V0.png)
Title: Re: FreeRTOS
Post by: tha on November 10, 2021, 06:07:22 AM
แต่ละ task ถูกจัดสรรตัวเลขโดยอัตโนมัติเมื่อมันถูกสร้าง. vTaskList() สามารถถูกใช้เพื่อให้ได้ตัวเลขที่จัดสรรให้แต่ละ task, พร้อมกันกับข้อมูลที่เป็นประโยชน์อื่นๆบางอย่าง. The information ที่รีเทิร์นโดย vTaskList() ในระหว่าง the demo application ถูกแสดงข้างล่าง, โดยที่:

     •  Name - คือ the name ที่ให้ไปยัง the task ตอนเมื่อมันถูกสร้าง. โปรดทราบว่า the demo application สร้างมากว่าหนึ่ง instance ของบาง tasks.
     •  State - แสดงสถานะของ a task. นี้สามารถเป็นอย่างใดอย่างหนึ่ง 'B'locked, 'R'eady, 'S'uspended หรือ 'D'eleted.
     •  Priority - คือ the priority ที่ให้ไปยัง the task ตอนเมื่อมันถูกสร้าง.
     •  Stack - แสดง the high water mark of the task stack. นี้คือจำนวนต่ำสุดของ free stack ที่มีให้ใช้ประโยชน์ได้ในระหว่างตลอดอายุของ the task.
     •  Num - คือหมายเลขที่จัดสรรให้กับ the task โดยอัตโนมัติ

(https://i.imgur.com/ADAlHAd.png)
Title: Re: FreeRTOS
Post by: tha on November 10, 2021, 07:21:34 AM
ในตัวอย่างนี้ จะเห็นได้ว่า tasks 6, 7, 8 and 14 ทั้งหมดกำลังรันที่ priority 0. ดังนั้นพวกมันจะแบ่งเวลาของพวกมันเองและ the other priority 0 tasks (รวมทั้ง the idle task).  Task 14 อ่าน a message จาก a queue (ดู BlockQ.c ใน the demo application). นี้ปล่อยฟรี a space บน the queue. Task 13 ที่ถูกบล็อกอยู่กำลังรอสำหรับ a space มีให้ใช้ประโยชน์, ดังนั้นตอนนี้ปลุกขึ้นมา, โพสต์ a message จากนั้นบล็อกอีกครั้ง.

หมายเหตุ: ในการจัดให้มีใช้งานในตอนนี้, the time resolution of the trace เท่ากันกับ the tick rate. Context switches สามารถเกิดขึ้นได้บ่อยกว่า the system tick (ถ้า a task โดนบล็อกเป็นต้น). เมื่อสิ่งนี้เกิดขึ้น the trace จะแสดงว่า a context switch ได้เกิดขึ้นแล้วและจะแสดง the context switch sequencing อย่างแม่นยำ.  อย่างไรก็ตาม, ช่วงเวลาของ context switches ที่เกิดขึ้นระหว่าง system ticks ไม่สามารถถูกบันทึกได้อย่างแม่นยำ. The ports สามารถปรับเปลี่ยนได้อย่างง่ายดายเพื่อจัดให้มี a higher resolution time stamp โดยใช้ a free running timer.
Title: Re: FreeRTOS
Post by: tha on November 10, 2021, 10:26:25 AM
https://www.freertos.org/implementation/main.html (https://www.freertos.org/implementation/main.html)

RTOS Implementation

ส่วนนี้อธิบายส่วนหนึ่งของ the FreeRTOS implementation.

The pages จะช่วยถ้าคุณ:

     •  อยากจะแก้ไข the FreeRTOS source code.
     •  พอร์ต the real time kernel ไปยัง microcontroller อีกตัวหนึ่งหรือ prototyping board.
     •  ใหม่ในการใช้ an RTOS และอยากจะได้รับข้อมูลที่มากขึ้นเกี่ยวกับ operation and implementation ของพวกมัน.

มีสองบทที่เข้าถึงได้จาก the menu frame ทางด้านซ้าย:

     1. - Fundamentals and RTOS concepts

         นี้บรรจุข้อมูลพื้นฐานเกี่ยวกับ multitasking และ basic real time concepts และถูกมุ่งหมายไว้สำหรับผู้เริ่มต้น.

     2. - RTOS Implementation

         นี้อธิบาย the real time kernel context switching source code จากข้างล่างขึ้นบน.

         The FreeRTOS real time kernel ถูกพอร์ตไปยังจำนวนหนึ่งของ different microcontroller architectures. The Atmel AVR port ถูกเลือกสำหรับ
         ตัวอย่างนี้เนื่องจาก:

             •  ความเรียบง่ายของ the AVR architecture.
             •  มีให้ใช้ประโยชน์ฟรีๆของ the utilized WinAVR (GCC) development tools.
             •  ต้นทุนต่ำของ the STK500 prototyping board

         ส่วนนี้จะจบลงด้วยการดูทีละขั้นตอนโดยละเอียดที่ one complete context switch.

Next: What is a RTOS? ... Next: RTOS fundamentals
Title: Re: FreeRTOS
Post by: tha on November 10, 2021, 10:45:15 AM
https://www.freertos.org/implementation/a00002.html (https://www.freertos.org/implementation/a00002.html)

(https://i.imgur.com/myz38On.png)

Detailed Description

ส่วนนี้ให้ข้อมูลเบื้องต้นสั้นๆ เกี่ยวกับแนวคิดแบบเรียลไทม์และการทำงานหลายอย่างพร้อมกัน ต้องเข้าใจก่อนอ่านส่วน 2

Next: RTOS Fundamentals - Multitasking
Title: Re: FreeRTOS
Post by: tha on November 10, 2021, 11:35:03 AM
https://www.freertos.org/implementation/a00004.html (https://www.freertos.org/implementation/a00004.html)

Multitasking
[RTOS Fundamentals]

เคอร์เนลเป็นองค์ประกอบหลักภายในระบบปฏิบัติการ ระบบปฏิบัติการเช่น Linux ใช้เคอร์เนลที่อนุญาตให้ผู้ใช้เข้าถึงคอมพิวเตอร์ได้พร้อมกัน ผู้ใช้หลายคนสามารถรันหลายโปรแกรมพร้อมกันได้อย่างชัดเจน

แต่ละ executing program คือ a task (หรือ thread) ภายใต้การควบคุมของ the operating system. ถ้า an operating system สามารถปฏิบัติหลาย tasks ในลักษณะนี้มันถุกพูดได้ว่าเป็น multitasking.

การใช้ของ a multitasking operating system สามารถลดความซับซ้อนของการออกแบบของสิ่งที่มิฉะนั้นอาจเป็น a complex software application:

     •  The multitasking and inter-task communications features of the operating system ยอมให้ the complex application แบ่งพาร์ติชันเป็นชุดของ
         tasks ที่มีขนาดเล็กลงและสามารถจัดการได้มากขึ้น
     •  การแบ่งพาร์ติชันสามารถทำให้การทดสอบซอฟต์แวร์ง่ายขึ้น การแยกย่อยงานภายในทีม และการใช้โค้ดซ้ำ
     •  รายละเอียดเวลาและลำดับที่ซับซ้อนสามารถเอาออกจาก the application code และกลายมาเป็นความรับผิดชอบของระบบปฏิบัติการ

ดูคำถามที่พบบ่อย "เหตุใดจึงต้องใช้ RTOS"
Title: Re: FreeRTOS
Post by: tha on November 10, 2021, 12:07:26 PM
Multitasking Vs Concurrency

โปรเซสเซอร์แบบเดิมสามารถดำเนินการงานได้ครั้งละหนึ่ง task เท่านั้น แต่ด้วยการสลับระหว่าง task อย่างรวดเร็ว a multitasking operating system สามารถทำให้ดูเหมือนว่าแต่ละ task ดำเนินการพร้อมกัน นี้ถูกวาดให้เห็นโดย the diagram ด้านล่างซึ่งแสดง the execution pattern ของสาม tasks ที่สัมพันธ์กับเวลา.  The task names ถูกให้ระหัสเป็นสีและเขียนลงทางด้านซ้ายมือ. เวลาจะเคลื่อนจากซ้ายไปขวา โดยมีเส้นสีแสดงว่า task ใดกำลังดำเนินการอยู่ ณ เวลาใดเวลาหนึ่ง The upper diagram แสดง the perceived concurrent execution pattern, และรูปด้านล่างแสดง the actual multitasking execution pattern

(https://i.imgur.com/qPVdq7T.png)
Title: Re: FreeRTOS
Post by: tha on November 11, 2021, 05:57:50 AM
https://www.freertos.org/implementation/a00005.html (https://www.freertos.org/implementation/a00005.html)

Scheduling
[RTOS Fundamentals]

The scheduler เป็นส่วนหนึ่งของ the kernel ที่รับผิดชอบในการตัดสินใจว่า task ควรปฏิบัติในเวลาใดเวลาหนึ่ง. The kernel สามารถแขวน(ระงับ)ไว้และภายหลังกลับคืนมาทำงาน a task หลายครั้งในช่วงอายุของงาน(the task lifetime).

The scheduling policy เป็น the algorithm ที่ใช้โดย the scheduler ในการตัดสินใจว่า task ใดจะปฏิบัติที่จุดใดๆในเวลา. นโยบายของ a (non real time) multi user system มักจะยอมให้แต่ละ task  มีสัดส่วน "ยุติธรรม" ของ processor time. The policy ที่ใช้ใน real time / embedded systems จะถูกอธิบายในภายหลัง

นอกเหนือจากการถูกระงับโดยไม่ได้ตั้งใจโดย the kernel a task สามารถเลือกที่จะแขวนตัวมันเอง. มันจะทำสิ่งนี้ถ้ามันอย่างใดอย่างหนึ่งต้องการที่จะ delay (sleep) เป็นเวลาคาบเวลาคงที่, หรือรอ (block) สำหรับทรัพยากรกลายมาเป็นมีให้ใช้ประโยชน์ (ตัวอย่างเช่น a serial port) หรือ an event เกิดขึ้น (ตัวอย่างเช่น a key press). A blocked หรือ sleeping task ไม่สามารถปฏิบัติ, และจะไม่ถูกจัดสรร processing time ใดๆ.

(https://i.imgur.com/SPBg6Kb.png)

อ้างถึงตัวเลขในแผนภาพด้านบน:

     •  ที่ (1) task 1 กำลังปฏิบัติ.
     •  At (2) the kernel ระงับ (สลับออก) task 1 ...
     •  ... และที่ (3) กลับมาทำงาน task 2 ต่อ.
     •  ขณะที่ task 2 กำลังปฏิบัติ (4), มันจะล็อก a processor peripheral เพื่อการเข้าถึงแบบเอกสิทธิ์เฉพาะของตนเอง.
     •  ที่ (5) the kernel ระงับ task 2 ...
     •  ... และที่ (6) กลับมาทำงาน task 3 ต่อ.
     •  Task 3 พยายามเข้าถึง the same processor peripheral, พบว่ามันถูกล็อค task 3 ไม่สามารถทำงานต่อดังนั้นระงับตัวเองที่ (7).
     •  ที่ ( 8 ) the kernel กลับมาทำงาน task 1 ต่อ.
     •  ฯลฯ.
     •  เวลาถัดไป task 2 กำลังปฏิบัติ (9) มันเสร็จการใช้ the processor peripheral และปลดล็อคมัน.
     •  เวลาถัดไป task 3 กำลังปฏิบัติ (10) มันพบว่าตอนนี้มันสามารถเข้าถึง the processor peripheral และเวลานี้ปฏิบัติจนกระทั่งถูกระงับโดย the kernel.

Next: RTOS Fundamentals - Context Switching
Title: Re: FreeRTOS
Post by: tha on November 11, 2021, 11:01:08 AM
https://www.freertos.org/implementation/a00006.html (https://www.freertos.org/implementation/a00006.html)

Context Switching
[RTOS Fundamentals]

ในขณะที่ a task ปฏิบัติมันจะใช้ the processor / microcontroller registers แลละเข้าถึง RAM and ROM เช่นเดียวกับโปรแกรมอื่น ๆ ทรัพยากรเหล่านี้ร่วมกัน (the processor registers, stack, etc.) ประกอบเป็น the task execution context.

(https://i.imgur.com/uaGLwRP.png)

A task คือชิ้นส่วนที่เรียงตามลำดับของโค้ด - มันไม่รู้ว่ามันกำลังไปได้รับการถูกระงับ (สลับออกหรือสวิทช์ออก) หรือกลับมาทำงานต่อ (สลับเข้าหรือสวิทช์เข้า) โดย the kernel และไม่รู้ด้วยซ้ำว่าสิ่งนี้เกิดขึ้นเมื่อใด.  พิจารณาตัวอย่างของ a task ที่ถูกระงับทันทีการการปฏิบัติคำสั่งที่บวกค่าที่บรรจุอยู่ภายใน two processor registers. ในขณะที่ the task ถูกระงับ tasks อื่นๆจะปฏิบัติและอาจปรับเปลี่ยน the processor register values. เมื่อกลับมาทำงานต่อ the task จะไม่รู้ว่า the processor registers ถูกปรับเปลี่ยนไปแล้ว - ถ้ามันใช้ค่าที่ถูกแก้ไข ผลรวมที่ได้จะส่งผลให้ได้ค่าไม่ถูกต้อง.

เพื่อป้องกันข้อผิดพลาดประเภทนี้ จำเป็นที่เมื่อกลับมาทำงานต่อต้องมีบริบทที่เหมือนกันก่อนที่จะหยุดการทำงานของมัน.  The operating system kernel มีหน้าที่รับผิดชอบเพื่อให้แน่ใจว่านี้เป็นกรณี - และทำโดยการบันทึกบริบทของ task ในขณะที่มันถูกระงับ เมื่อ task กลับมาทำงานต่อ บริบทที่บันทึกไว้จะถูกกู้คืนโดย the operating system kerne ก่อนการดำเนินการของมัน ขบวนการของการบันทึก the context of a task ที่ถูกระงับและกู้คืน the context of a task ที่กลับมาทำงานต่อถูกเรียกว่า context switching.


Next: RTOS Fundamentals - Real Time Applications
Title: Re: FreeRTOS
Post by: tha on November 12, 2021, 06:02:52 AM
https://www.freertos.org/implementation/a00007.html (https://www.freertos.org/implementation/a00007.html)

Real Time Applications
[RTOS Fundamentals]

Real time operating systems (RTOSes) สำเร็จ multitasking โดยใช้หลักการเดียวกันเหล่านี้ - แต่วัตถุประสงค์ของระบบปฏิบัติการนั้นแตกต่างอย่างมากจากระบบที่ไม่ใช่แบบเรียลไทม์ . วัตถุประสงค์ที่แตกต่างกันจะสะท้อนให้เห็นในนโยบายการจัดกำหนดการ.  Real time / embedded systems ถูกออกแบบมาเพื่อให้ตอบสนองต่อเหตุการณ์ในโลกแห่งความเป็นจริงได้ทันท่วงที. เหตุการณ์ที่เกิดขึ้นในโลกแห่งความเป็นจริงอาจมีกำหนดเวลาก่อนที่ the real time / embedded system จะต้องตอบสนอง และ the RTOS scheduling policy จะต้องทำให้แน่ใจว่ากำหนดเวลาเหล่านี้ถูกตรงตาม.

เพื่อสำเร็จวัตถุประสงค์นี้ the software engineer อย่างแรกต้องกำหนด a priority ไปยังแต่ละ task. The scheduling policy of the RTOS  นั้นทำเพียงเพื่อให้แน่ใจว่า the highest priority task ที่สามารถปฏิบัติคือ the task ที่ถูกให้ processing time. ซึ่งอาจต้องการเวลาในการประมวลผลร่วมกัน "อย่างยุติธรรม" ระหว่าง tasks ที่มีลำดับความสำคัญเท่ากัน หากพวกมันพร้อมที่จะทำงานพร้อมกัน
Title: Re: FreeRTOS
Post by: tha on November 12, 2021, 06:06:44 AM
Example:

ตัวอย่างพื้นฐานที่สุดของสิ่งนี้คือ a real time system ที่รวมเข้าด้วยกัน a keypad และ LCD. ผู้ใช้ต้องได้รับผลตอบรับด้วยภาพของการกดปุ่มแต่ละครั้งภายในระยะเวลาที่เหมาะสม - หากผู้ใช้ไม่สามารถเห็นว่าการกดปุ่มนั้นถูกยอมรับแล้วภายในระยะเวลานี้ the software product อย่างดีที่สุดจะเชื่องช้าในการใช้. หากระยะเวลาที่ยอมรับได้ยาวนานที่สุดคือ 100ms - การตอบสนองใดๆ ระหว่าง 0 ถึง 100ms จะเป็นที่ยอมรับ ฟังก์ชันนี้สามารถถูกจัดให้มีใช้อย่าง task แบบอิสระโดยมีโครงสร้างดังต่อไปนี้:

(https://i.imgur.com/BRzzL0U.png)
Title: Re: FreeRTOS
Post by: tha on November 12, 2021, 07:33:25 AM
ตอนนี้สมมุติว่า the real time system กำลังกระทำ a control function อีกด้วยที่ขึ้นอยู่กับ a digitally filtered input. The input ต้องถูกสุ่ม, ถูกกรองและ the control cycle ถูกปฏิบัติทุกๆ 2ms. เพื่อ correct operation of the filter ควมสม่ำเสมอชั่วขณะ(the temporal regularity)ของการสุ่มต้องแม่นยำถึง 0.5ms. ฟังชั่นนี้สามารถจัดให้มีใช้เป็น an autonomous task โดยมี the following structure:

(https://i.imgur.com/PH4nxOd.png)

The software engineer ต้องกำหนด the control task เป็น the highest priority ดังนี้:

     1. กำหนดเวลาสำหรับ the control task จะเข้มงวดกว่านั่นของ the key handling task.
     2. ผลที่ตามมาของกำหนดเวลาที่พลาดไปจะใหญ่ขึ้นสำหรับ the control task มากกว่าสำหรับ the key handler task

หน้าถัดไปสาธิตวิธีที่ tasks เหล่านี้จะถูกวางกำหนดเวลาโดย a real time operating system.


Next: RTOS Fundamentals - Real Time Scheduling
Title: Re: FreeRTOS
Post by: tha on November 12, 2021, 09:10:57 AM
https://www.freertos.org/implementation/a00008.html (https://www.freertos.org/implementation/a00008.html)

Real Time Scheduling
[RTOS Fundamentals]

ไดอะแกรมด้านล่างแสดงให้เห็นว่า tasks ที่กำหนดไว้ในหน้าก่อนหน้านี้จะถูกจัดกำหนดเวลาโดย a real time operating system. The RTOS ได้สร้าง tasks ของมันเอง - the idle task - ซึ่งจะปฏิบัติเฉพาะเมื่อไม่มี tasks อื่นสามารถทำงานได้. The RTOS idle task จะอยู่ในสถานะที่มันสามารถปฏิบัติได้เสมอ.

(https://i.imgur.com/WzbAgho.png)
Title: Re: FreeRTOS
Post by: tha on November 12, 2021, 09:37:47 AM
อ้างอิงจากแผนภาพด้านบน:

     •  ในตอนเริ่มต้น tasks ทั้งสองของเราไม่สามารถรันได้ - vControlTask กำลังรอสำหรับ the correct time เพื่อสตาร์ท a new control cycle และ
         vKeyHandlerTask กำลังรอสำหรับ a key ถูกกด. Processor time ถูกให้ไปยัง the RTOS idle task.
     •  ที่เวลา t1, a key press เกิดขึ้น. vKeyHandlerTask ตอนนี้สามารถปฏิบัติ - มันมี priority สูงกว่า the RTOS idle task ดังนั้นจึงถูกให้ processor time.
     •  ที่เวลา t2 vKeyHandlerTask เสร็จสมบูรณ์การประมวลผล the key และการอัปเดต the LCD. มันไม่สามารถทำงานต่อไปได้จนกว่า key อื่นจะถูกกดดังนั้นจึงระงับ
         ตัวมันเองและ the RTOS idle task กลับมาทำงานต่ออีกครั้ง.
     •  ที่เวลา t3 a timer event แสดงให้เห็นว่ามันถึงเวลาดำเนินการ the next control cycle. vControlTask ตอนนี้สามารถปฏิบัติและเป็น the highest priority
         task ถูกวางกำหนดเวลา processor time โดยทันที.
     •  ระหว่างเวลา t3 และ t4, ในขณะที่ vControlTask กำลังยังคงปฏิบัติ, a key press เกิดขึ้น. vKeyHandlerTask ตอนนี้สามารถปฏิบัติ, แต่เนื่องจากมันมี priority
         ต่ำกว่า vControlTask มันไม่ถูกวางกำหนดเวลา processor time ใดๆ.
Title: Re: FreeRTOS
Post by: tha on November 12, 2021, 10:31:28 AM
     •  ที่ t4 vControlTask เสร็จสมบูรณ์การประมวลผล the control cycle และไม่สามารถสตาร์ทใหม่จนกว่า the next timer event - มันระงับตัวมันเอง.
         vKeyHandlerTask ตอนนี้เป็น the task ที่มี the highest priority ที่สามารถรันดังนั้นถูงวางกำหนดเวลา processor time เพื่อประมวลผล the previous
         key press.
     •  ที่ t5 the key press ถูกประมวลผลแล้ว, และ vKeyHandlerTask ระงับตัวมันเองเพื่อรอสำหรับ the next key event. อีกครั้งไม่มี tasks ของเราที่สามารถ
         ปฏิบัติและ the RTOS idle task ถูกวางกำหนดเวลา processor time.
     •  ระหว่าง t5 and t6 a timer event ถูกประมวลผล, แต่ไม่มี key presses เกิดขึ้นเพิ่มเติม.
     •  The next key press เกิดขึ้นที่เวลา t6, แต่ก่อนที่ vKeyHandlerTask จะเสร็จสมบูรณ์การประมวลผล the key, a timer event ได้เกิดขึ้น. ตอนนี้ทั้งสอง
         tasks สามารถปฏิบัติ. เนื่องจาก vControlTask มี the higher priority, vKeyHandlerTask ถูกระงับก่อนที่มันจะเสร็จสมบูรณ์การประมวลผล the key, and
         vControlTask ถูกวางกำหนดเวลา processor time.
     •  ที่ t8 vControlTask เสร็จสมบูรณ์การประมวลผล the control cycle และระงับตัวมันเองเพื่อรอสำหรับ the next. vKeyHandlerTask เป็น the highest
         priority task อีกครั้งที่สามารถรันดังนั้นถูกวางกำหนดเวลา processor time ดังนั้น the key press processing สามารถถูกเสร็จสมบูรณ์.


Next: Section 2 - RTOS Implementation
Title: Re: FreeRTOS
Post by: tha on November 13, 2021, 05:41:18 AM
https://www.freertos.org/implementation/a00003.html (https://www.freertos.org/implementation/a00003.html)

RTOS Implementation

Pages

     •  Building Blocks
     •  Detailed Example


Detailed Description

ส่วนนี้อธิบาน the RTOS context switch source code จากข้างล่างขึ้นบน. The FreeRTOS Atmel AVR microcontroller port ถูกใช้เป็นตัวอย่าง. ส่วนนี้จบด้วยดูทีละขั้นตอนโดยละเอียดที่ one complete context switch.

Next: RTOS Implementation - Development Tools
Title: Re: FreeRTOS
Post by: tha on November 13, 2021, 05:43:20 AM
https://www.freertos.org/implementation/a00009.html (https://www.freertos.org/implementation/a00009.html)

(https://i.imgur.com/7bsBcWc.png)
Title: Re: FreeRTOS
Post by: tha on November 13, 2021, 06:03:05 AM
https://www.freertos.org/implementation/a00010.html (https://www.freertos.org/implementation/a00010.html)

C Development Tools
[RTOS Implementation Building Blocks]

เป้าหมายของ FreeRTOS คือความเรียบง่ายและเข้าใจง่าย ด้วยเหตุนี้ ส่วนใหญ่ของ the RTOS source code ถูกเขียนใน C, ไม่ assembler.

ตัวอย่างที่นำเสนอที่นี่ใช้ the WinAVR development tools. WinAVR เป็น a free Windows ไปยัง AVR cross compiler บนฐานของ GCC.

Next: RTOS Implementation - The RTOS Tick
Title: Re: FreeRTOS
Post by: tha on November 14, 2021, 06:00:31 AM
https://www.freertos.org/implementation/a00011.html (https://www.freertos.org/implementation/a00011.html)

The RTOS Tick
[RTOS Implementation Building Blocks]

เมื่ออยู่ในโหมดสลีป, an RTOS task จะระบุเวลาหลังจากที่มันต้องการจะ 'ปลุก'. เมื่อโดนบล็อก, an RTOS task สามารถระบุเวลาสูงสุดที่มันอยากจะรอ.

The FreeRTOS real time kernel วัดเวลาโดยใช้ a tick count variable. A timer interrupt (the RTOS tick interrupt) เพิ่ม the tick count ด้วย strict temporal accuracy - ยอมให้ the real time kernel วัดเวลาเป็นความละเอียดของ the chosen timer interrupt frequency.

แต่ละครั้งที่ the tick count ถูกเพิ่ม the real time kernel ต้องเช็คเพื่อดูถ้าตอนนี้มันเป็นเวลาปลดบล็อกหรือปลุก a task  มันเป็นไปได้ว่า a task ที่ถูกปลุกหรือปลดบล็อกในระหว่าง the tick ISR จะมี a priority สูงกว่านั่นของ task ที่ถูกอินเตอร์รัพท์. หากเป็นกรณีนี้ the tick ISR จะรีเทิร์นไปยัง the newly woken/unblocked task - อินเตอร์รัพท์ task หนึ่งอย่างมีประสิทธิภาพแต่จะรีเทิร์นไปยังอีก task หนึ่ง. นี้ถูกแสดงเป็นภาพข้างล่าง:

(https://i.imgur.com/ZtX8yyZ.png)

อ้างถึงตัวเลขในแผนภาพด้านบน:

     •  ที่ (1) the RTOS idle task กำลังปฏิบัติ.
     •  ที่ (2) the RTOS tick เกิดขึ้น, และการควบคุมถ่ายโอนไปยัง the tick ISR (3).
     •  The RTOS tick ISR ทำให้ vControlTask พร้อมที่จะรัน, และตามที่ vControlTask มี  priority สูงกว่า the RTOS idle task, สวิทช์ the context ไปยังนั่น
         ของ vControlTask.
     •  ตามที่ the execution context ตอนนี้เป็นนั่นของ vControlTask, การออกจาก the ISR (4) รีเทิร์นการควบคุมไปยัง vControlTask, ซึ่งสตาร์ทการปฏิบัติ (5).

A context switch ที่เกิดขึ้นในลักษณะนี้ถูกพูดได้ว่าเป็น Preemptive, ดังที่ task ที่ถูกอินเตอร์รัพท์ ถูกยึดเอาไว้ก่อนโดยไม่มีการระงับตัวเองด้วยสมัครใจ.

The AVR port of FreeRTOS ใช้ a compare match event บน timer 1 สร้าง the RTOS tick. หน้าต่อไปนี้อธิบายวิธีที่the RTOS tick ISR ถูกจัดให้มีใช้โดยใช้ the WinAVR development tools.

Next: RTOS Implementation - The GCC Signal Attribute
Title: Re: FreeRTOS
Post by: tha on November 15, 2021, 05:54:53 AM
https://www.freertos.org/implementation/a00012.html (https://www.freertos.org/implementation/a00012.html)

GCC Signal Attribute
[RTOS Implementation Building Blocks]

The GCC development tools ยอมให้ interrupts ถูกเขียนใน C. A compare match event บน the AVR timer 1 peripheral สามารถถูกเขียนโดยใช้ the following syntax.

(https://i.imgur.com/Tp8xEsT.png)

The '__attribute__ ( ( signal ) )' คำสั่งบน the function prototype แจ้ง the compiler ว่า the function เป็น an ISR และส่งผลให้เกิดการเปลี่ยนแปลงที่สำคัญสองประการใน the compiler output.

     1. The 'signal' attribute ทำให้แน่ใจว่าทุกๆ processor register ที่ได้รับการแก้ไขในระหว่าง the ISR จะถูกกู้คืนเป็นค่าเดิมเมื่อ the ISR ออก. นี้ถูกต้องการ
         เนื่องจาก the compiler ไม่สามารถคาดเดาได้ว่าเมื่อใด the interrupt จะปฏิบัติ, และดังนั้นไม่สามารถ optimize ว่า processor registers ใดต้องการบันทึก
         และตัวใดไม่ต้องบันทึก.
     2. The 'signal' attribute บังคับใช้ a 'return from interrupt' instruction (RETI) ถูกใช้แทนที่ของ the 'return' instruction (RET) ที่มิฉะนั้นแล้วจะถูก
         ใช้. The AVR microcontroller ปิดการใช้งาน interrupts เมื่อเข้าสู่ an ISR และ the RETI instruction ถูกต้องการเพื่อเปิดการใช้งานพวกมันใหม่เมื่อออกจาก
         อินเตอร์รัพท์.

Code ส่งออกโดย the compiler:

(https://i.imgur.com/6LJrMFK.png)
(https://i.imgur.com/xciCTbu.png)


Next: RTOS Implementation - The GCC Naked Attribute
Title: Re: FreeRTOS
Post by: tha on November 15, 2021, 06:46:33 AM
https://www.freertos.org/implementation/a00013.html (https://www.freertos.org/implementation/a00013.html)

GCC Naked Attribute
[RTOS Implementation Building Blocks]

ส่วนก่อนหน้านี้แสดงให้เห็นว่า the 'signal' attribute สามารถถูกใช้เขียน an ISR ใน C และผลลัพธ์นี้ในส่วนของ the execution context ถูกบันทึกโดยอัตโนมัติอย่างไร (เฉพาะ the processor registers ที่แก้ไขโดย the ISR เท่านั้นที่ได้รับการบันทึก). การดำเนินการ a context switch อย่างไรก็ตามต้องการ the entire context ถูกบันทึก.

The application code สามารถบันทึก the processor registers ทั้งหมดอย่างชัดเจนเมื่อเข้าสู่ the ISR, แต่การทำเช่นนั้นจะส่งผลให้บาง processor registers ถูกบันทึกสองครั้ง - ครั้งหนึ่งโดย the compiler สร้าง code และจากนั้นอีกครั้งโดย the application code. สิ่งนี้ไม่พึงปรารถนาและสามารถหลีกเลี่ยงได้โดยใช้ the 'naked' attribute นอกเหนือจาก the 'signal' attribute.

(https://i.imgur.com/YoDsRCS.png)

The 'naked' attribute ป้องกันไม่ให้ the compiler สร้าง function entry หรือ exit code ใดๆ. ตอนนี้การคอมไพล์ the code ส่งผลให้ได้ผลลัพธ์ที่ง่ายกว่ามาก:

(https://i.imgur.com/3ZXtcxQ.png)

เมื่อ the 'naked' attribute ถูกใช้ the compiler ไม่สร้าง function entry or exit code ใดๆดังนั้นสิ่งนี้ตอนนี้ต้องถูกเพิ่มอย่างชัดแจ้ง. The RTOS macros portSAVE_CONTEXT() and portRESTORE_CONTEXT() บันทึกและกู้คืน the entire execution context ตามลำดับ.:

(https://i.imgur.com/61eNmza.png)

The 'naked' attribute ช่วยให้ the application code ควบคุมเวลาและวิธีที่ the AVR context ถูกบันทึกได้อย่างสมบูรณ์. ถ้า the application code บันทึก the entire context เมื่อเข้าสู่ the ISR ไม่มีความจำเป็นต้องบันทึกมันอีกครั้งการดำเนินการ a context switch ดังนั้นจึงไม่มี the processor registers ได้รับการบันทึกสองครั้ง.


Next: RTOS Implementation - The FreeRTOS Tick Code
Title: Re: FreeRTOS
Post by: tha on November 15, 2021, 08:13:53 AM
https://www.freertos.org/implementation/a00014.html (https://www.freertos.org/implementation/a00014.html)

FreeRTOS Tick Code
[RTOS Implementation Building Blocks]

The actual source code ที่ใช้โดย the FreeRTOS AVR port แตกต่างเล็กน้อยจากตัวอย่างที่แสดงบนหน้าก่อนหน้า. vPortYieldFromTick() ถูกจัดให้มีใช้โดยตัวมันเองเป็น a 'naked' function, และ the context ถูกบันทึกและกู้คืนภายใน vPortYieldFromTick(). มันถูกทำโดยวิธีนี้เนื่องจากการจัดให้มีใช้ของ non-preemptive context switches (โดยที่ a task จะบล็อกตัวมันเอง) - ซึ่งไม่ถูกอธิบายที่นี่.

The FreeRTOS implementation of the RTOS tick จึงเป็นเช่นนี้ (ดู the comments ใน the source code snippets สำหรับรายละเอียดเพิ่มเติม):

(https://i.imgur.com/JJ8pWt2.png)
(https://i.imgur.com/fLUCnHW.png)

Next: RTOS Implementation - The AVR Context
Title: Re: FreeRTOS
Post by: tha on November 16, 2021, 06:09:56 AM
https://www.freertos.org/implementation/a00015.html (https://www.freertos.org/implementation/a00015.html)

The AVR Context
[RTOS Implementation Building Blocks]

A context switch ต้องการ the entire execution context ถูกบันทึก. บน the AVR microcontroller the context ประกอบด้วย:

     •  32 general purpose processor registers. The gcc development tools จะถือว่า register R1 ถูกตั้งค่าเป็นศูนย์.
     •  Status register. ค่าของ the status register มีผลต่อ instruction execution, และต้องได้รับการเก็บรักษาไว้ตลอดการ context switches.
     •  Program counter. เมื่อกลับคืนมาใหม่, a task ต้องปฏิบัติต่อเนื่งจากคำสั่งที่กำลังจะถูกปฏิบัติทันทีก่อนการระงับของมัน.
     •  The two stack pointer registers

(https://i.imgur.com/EwtfJHz.png)

Next: RTOS Implementation - Saving The Context
Title: Re: FreeRTOS
Post by: tha on November 16, 2021, 06:34:22 AM
https://www.freertos.org/implementation/a00016.html (https://www.freertos.org/implementation/a00016.html)

Saving the RTOS Task Context
[RTOS Implementation Building Blocks]

แต่ละ real time task มี stack memory area ของตัวเองดังนั้น the context สามารถถูกบันทึกโดยการ push ง่ายๆ processor registers ลงใน the task stack. การบันทึก the AVR context เป็นที่หนึ่งที่ assembly code ไม่สามารถหลีกเลี่ยงได้.

portSAVE_CONTEXT() ถูกจัดให้มีใช้เป็น a macro, the source code ซึ่งถูกให้ข้างล่าง:

(https://i.imgur.com/2cNgWMt.png)

อ้างถึง the source code ข้างบน:

     •  Processor register R0 ถูกบันทึกก่อนเนื่องจากมันถูกใช้เมื่อ the status register ถูกบันทึก, และต้องถูกบันทึกด้วยค่าดั้งเดิมของมัน.
     •  The status register ถูกย้ายลงใย R0 (2) ดังัน้นมันสามารถถูกบันทึกลงใน the stack (4).
     •  Processor interrupts ถูกปิดการใช้งาน (3). ถ้า portSAVE_CONTEXT() ถูกเรียกจากภายใน an ISR แล้วเท่านั้น ก็ไม่จำเป็นต้องปิดการใช้งาน interrupts
         อย่างชัดแจ้งเนื่องจาก the AVR ได้กระทำไปแล้ว. ตามที่ the portSAVE_CONTEXT() macro ถูกใช้ภายนอกของ interrupt service routines ได้อีก
         ด้วย(เมื่อ a task ระงับตัวมันเอง) interrupts ต้องถูกเคลียร์อย่างชัดแจ้งโดยเร็วที่สุดเท่าที่เป็นได้.
     •  The code ที่สร้างโดย the compiler จาก the ISR C source code ถือว่า R1 ถูกเซ็ตเป็นศูนย์. ค่าดั้งเดิมของ R1 ถูกบันทึก (5) ก่อนที่ R1 จะถูกเคลียร์ (6).
     •  ระหว่าง (7) and ( 8 ) processor registers ที่เหลืออยู่ทั้งหมดถูกบันทึกตามลำดับตัวเลข.
     •  The stack of the task ที่ถูกระงับตอนนี้บรรจุสำเนาของ the tasks execution context. The kernel เก็บ the tasks stack pointer ดังนั้น the context
         สามารถถูกดึงและกู้คืนเมื่อ the task ถูกคืนกลับมาทำงานใหม่. The X processor register ถูกโหลดด้วย the address ที่ the stack pointer ถูกบันทึก (8
         and 9).
     •  The stack pointer ถูกบันทึก, แรกคือ the low byte (10 and 11), จากนั้น the high nibble (12 and 13).

Next: RTOS Implementation - Restoring The Context
Title: Re: FreeRTOS
Post by: tha on November 16, 2021, 07:42:50 AM
https://www.freertos.org/implementation/a00017.html (https://www.freertos.org/implementation/a00017.html)

Restoring the Context
[RTOS Implementation Building Blocks]

The RTOS macro portRESTORE_CONTEXT() เป็นการกลับตรงกันข้ามของ portSAVE_CONTEXT(). The context of the task ที่กำลังถูกกลับคืนมาทำงานต่อ ที่ถูกเก็บก่อนหน้านี้ใน the tasks stack. The real time kernel ดึง the stack pointer สำหรับ the task จากนั้น POP's the context กลับลงใน the correct processor registers.

(https://i.imgur.com/5KFEggf.png)

อ้างถึง the code ข้างบน:

     •  The FreeRTOS pxCurrentTCB variable ถือ the address จากที่ the tasks stack pointer สามารถถูกดึง. นี้ถูกโหลดลงใน the X register (1 and 2).
     •  The stack pointer สำหรับ the task ที่กำลังถูกกลับคืนมาทำงานต่อ ถูกโหลดลงใน the AVR stack pointer, อย่างแรกคือ the low byte (3), จากนั้น the
         high nibble (4).
     •  The processor registers จากนั้นถูก popped จาก the stack ตามลำดับตัวเลขที่ย้อนกลับ, ลงไปยัง R1.
     •  The status register ที่เก็บไว้บน the stack ระหว่าง registers R1 and R0, ดังนั้นถูกกู้คืน (6) ก่อน R0 (7).

Next: RTOS Implementation - Putting It All Together
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 05:46:56 AM
https://www.freertos.org/implementation/a00018.html (https://www.freertos.org/implementation/a00018.html)

(https://i.imgur.com/J1VhTxK.png)

Detailed Description

ส่วนสุดท้ายของ section 2 แสดงวิธีที่ building blocks and source code modules เหล่านี้ถูกใช้เพื่อให้สำเร็จ a context switch บน the AVR microcontroller. The example สาธิตในเจ็ดขั้นตอนขบวนการของการสวิทชิ่งจาก a lower priority task, ที่เรียกว่า TaskA, ไปยัง a higher priority task, ที่เรียกว่า TaskB.

The source code เข้ากันได้กับ the WinAVR development tools.
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 05:52:12 AM
https://www.freertos.org/implementation/a00019.html (https://www.freertos.org/implementation/a00019.html)

Putting It All Together
[Detailed Example]

ส่วนสุดท้ายของ section 2 แสดงวิธีที่ building blocks and source code modules เหล่านี้ถูกใช้เพื่อให้สำเร็จ a context switch บน the AVR microcontroller. The example สาธิตในเจ็ดขั้นตอนขบวนการของการสวิทชิ่งจาก a lower priority task, ที่เรียกว่า TaskA, ไปยัง a higher priority task, ที่เรียกว่า TaskB.

The source code เข้ากันได้กับ the WinAVR development tools.

Next: RTOS Implementation - Detailed Example Step 1
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 06:05:25 AM
https://www.freertos.org/implementation/a00020.html (https://www.freertos.org/implementation/a00020.html)

RTOS Context Switch - Step 1
[Detailed Example]

Prior to the RTOS tick interrupt

ตัวอย่างนี้สตาร์ทด้วย TaskA executing. TaskB ถูกระงับไปก่อนหน้านี้แล้วดังนั้น context ของมันถูกเก็บไว้แล้วบน the TaskB stack.

TaskA มี the context ที่แสดงโดยไดอะแกรมข้างล่าง.

(https://i.imgur.com/eP4g4NU.png)

The (A) label ภายในแต่ละ register แสดงว่า the register บรรจุค่าที่ถูกต้องสำหรับ the context of task A.

Next: RTOS Implementation - Detailed Example Step 2
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 06:23:05 AM
https://www.freertos.org/implementation/a00021.html (https://www.freertos.org/implementation/a00021.html)

RTOS Context Switch - Step 2
[Detailed Example]

The RTOS tick interrupt occurs

The RTOS tick เกิดขึ้นขณะที่ TaskA กำลังจะปฏิบัติ an LDI instruction. เมื่อ the interrupt เกิดขึ้น the AVR microcontroller วางโดยอัตโนมัติ the current program counter (PC) ไปยังบน the stack ก่อนกระโดดไปยังจุดเริ่มต้นของ the RTOS tick ISR.

(https://i.imgur.com/fRDXZWF.png)

Next: RTOS Implementation - Detailed Example Step 3
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 07:32:59 AM
https://www.freertos.org/implementation/a00022.html (https://www.freertos.org/implementation/a00022.html)

RTOS Context Switch - Step 3
[Detailed Example]

The RTOS tick interrupt executes

The ISR source code ถูกให้ข้างล่าง. The comments ถูกลบออกเพื่อให้ง่ายต่อการอ่าน, สามารถถูกดูได้บนหน้าก่อนหน้านี้.

(https://i.imgur.com/WXfl37k.png)

SIG_OUTPUT_COMPARE1A() เป็น a naked function, ดังนั้น the first instruction คือการเรียกไปยัง vPortYieldFromTick(). vPortYieldFromTick() ก็เป็น a naked function อีกด้วยดังนั้น the AVR execution context จะถูกบันทึกอย่างชัดแจ้งโดยการเรียกไปยัง portSAVE_CONTEXT().

portSAVE_CONTEXT() ใส่(push) the entire AVR execution context ไปยังบน the stack of TaskA, ส่งผลให้ the stack ที่แสดงข้างล่าง. The stack pointer สำหรับ TaskA ตอนนี้ชี้ไปยังบนสุดของ context ของตัวเอง. portSAVE_CONTEXT() เสร็จสมบูรณ์โดยการเก็บสำเนาของ the stack pointer. The real time kernel มีสำเนาของ the TaskB stack pointer แล้ว - เอาเครั้งสุดท้ายที่ TaskB ถูกระงับ.

(https://i.imgur.com/2P0Simz.png)

Next: RTOS Implementation - Detailed Example Step 4
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 08:12:57 AM
https://www.freertos.org/implementation/a00023.html (https://www.freertos.org/implementation/a00023.html)

RTOS Context Switch - Step 4
[Detailed Example]

Incrementing the Tick Count

The RTOS function vTaskIncrementTick() จะปฏิบัติหลังจาก the TaskA context ถูกบันทึกแล้ว. สำหรับจุดมุ่งหมายของตัวอย่างงนี้ทึกทักเอาว่าการเพิ่ม the tick count เป็นเหตุให้ TaskB กลาบมาเป็นพร้อมที่จะรัน. TaskB มี priority สูงกว่า TaskA ดังนั้น vTaskSwitchContext() เลือก TaskB เป็น the task ที่ถูกให้ processing time เมื่อ the ISR เสร็จสมบูรณ์.

Next: RTOS Implementation - Detailed Example Step 5
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 08:24:01 AM
https://www.freertos.org/implementation/a00024.html (https://www.freertos.org/implementation/a00024.html)

RTOS Context Switch - Step 5
[Detailed Example]

The TaskB stack pointer is retrieved

(https://i.imgur.com/jx4R9Fv.png)

The TaskB context ต้องถูกกู้คืน. สิ่งแรกที่ RTOS macro portRESTORE_CONTEXT ทำคือดึง the TaskB stack pointer จากสำเนาที่เอาไว้เมื่อ TaskB ถูกระงับ. The TaskB stack pointer ถูกโหลดลงใน the processor stack pointer, ดังนั้นตอนนี้ the AVR stack ชี้ไปยังส่วนบนสุดของ the TaskB context.

Next: RTOS Implementation - Detailed Example Step 6
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 08:55:19 AM
https://www.freertos.org/implementation/a00025.html (https://www.freertos.org/implementation/a00025.html)

RTOS Context Switch - Step 6
[Detailed Example]

Restore the TaskB context

(https://i.imgur.com/nlNgxeN.png)

portRESTORE_CONTEXT() เสร็จสมบูรณ์โดยการกู้คืน the TaskB context จาก stack ของมันลงใน the appropriate processor registers.

มีเพียง the program counter เท่านั้นยังคงอยู่บน the stack.

Next: RTOS Implementation - Detailed Example Step 7
Title: Re: FreeRTOS
Post by: tha on November 17, 2021, 09:21:47 AM
https://www.freertos.org/implementation/a00026.html (https://www.freertos.org/implementation/a00026.html)

RTOS Context Switch - Step 7
[Detailed Example]

The RTOS tick exits

vPortYieldFromTick() รีเทิร์นไปยัง SIG_OUTPUT_COMPARE1A() ที่ the final instruction คือ a return from interrupt (RETI). A RETI instruction ทึกทักเอาว่า the next value บน the stack คือ a return address ที่วางลงบน the stack เมื่อ the interrupt เกิดขึ้น.

(https://i.imgur.com/ZTh6pGe.png)

เมื่อ the RTOS tick interrupt สตาร์ท the AVR วางโดยอัตโนมัติ the TaskA return address ลงบน the stack - the address ของ the next instruction ที่จะปฏิบัติใน TaskA. The RTOS tick handler ได้เปลี่ยน the stack pointer ดันั้นมันตอนนี้ชี้ไปยัง the TaskB stack. ดันั้น the return address ที่ POP จาก the stack โดย the RETI instruction จริงๆแล้วคือ the address ของ the instruction TaskB ที่กำลังจะปฏิบัติโดยทันทีก่อนที่มันจะถูกระงับ.

The RTOS tick interrupt อินเตอร์รัพท์ TaskA, แต่กำลังรีเทิร์นไปยัง TaskB - the context switch is complete!
Title: Re: FreeRTOS
Post by: tha on November 18, 2021, 05:05:31 AM
https://www.freertos.org/tutorial/index.html (https://www.freertos.org/tutorial/index.html)

Real Time Application Design Tutorial
Using FreeRTOS in small embedded systems

หากคุณตรงมาที่นี่แล้วเริ่มโดย:

     1. การดู the Homepage เพื่อใส่สิ่งนี้ลงใน context
     2. การดูว่าอะไรคือ FreeRTOS page เพื่อดูที่ไหน FreeRTOS เหมาะ


<<< | >>>
คำแนะนำ : ใช้ลูกศร <<< and >>>  เพื่อไปยังส่วนนี้.

Introduction

section นี้จัดให้มี a tutorial เกี่ยวกับการเขียน applications ที่ใช้ an RTOS บน microcontrollers ที่มีหน่วยความจำจำกัด. มันไม่เป็น a FreeRTOS specific tutorial. ถ้าคุณกำลังมองหาสำหรับ a specific FreeRTOS tutorial, หรือ a more complete tutorial เกี่ยวกับการใช้ an RTOS ใน an embedded system, ดังนั้น the FreeRTOS books จะเป็นทรัพยากรที่มีค่ามากขึ้น.

ส่วนนี้ของ the web site นำเสนอ four contrasting design solutions ไปยัง a hypothetical embedded real time application. ความเหมาะสมของแต่ละ solution จะพิจารณาจาก embedded computers ที่มี RAM, ROM และความสามารถในการประมวลผลที่แตกต่างกัน. นอกจากนี้ความเรียบง่ายและความสามารถในการบำรุงรักษาที่สอดคล้องกันของแต่ละการออกแบบถูกประเมิน.

สิ่งนี้ไม่ได้มีวัตถุประสงค์เพื่อแสดงรายการการออกแบบที่เป็นไปได้ทั้งหมด แต่เป็นแนวทางวิธีที่ the FreeRTOS real time kernel สามารถถูกใช้.

ควรสังเกตว่าส่วนนี้เขียนขึ้นเมื่อหลายปีก่อน - เมื่อ FreeRTOS ถูกใช้เป็นหลักในไมโครคอนโทรลเลอร์ขนาดเล็กมาก นับแต่นั้นเป็นต้นมา การใช้ FreeRTOS กับไมโครคอนโทรลเลอร์ที่ใหญ่กว่านั้นกลายเป็นเรื่องธรรมดามากขึ้น ซึ่งไม่ได้จำกัดอยู่ใน ROM และ RAM ที่มีให้

หมายเหตุ: หน้าเหล่านี้ยังไม่ได้รับการอัปเดตตั้งแต่เปิดตัว FreeRTOS V4.0.0 V4.0.0 แนะนำแนวคิดของ co-routines ซึ่งจะให้โซลูชันที่แตกต่างและแปลกใหม่สำหรับเหล่านั้นที่นำเสนอที่นี่ The Tasks and Co-routines documentation จัดให้มีข้อมูลเพิ่มเติม
Title: Re: FreeRTOS
Post by: tha on November 18, 2021, 05:59:40 AM
(https://i.imgur.com/uAlirUM.png)

The application จะปฏิบัติบน an embedded single board computer ที่ต้องควบคุม a plant ในขณะที่รักษาทั้ง local และ remote user interfaces.

รูปด้านบน, the system ประกอบด้วย:

     1. An embedded computer ภายใน a control terminal.
     2. Two fieldbus networked sensors.
     3. The plant ที่กำลังถูกควบคุม (อาจเป็นอะไรก็ได้, motor, heater, etc.). สิ่งนี้ถูกต่อบน the same fieldbus network.
     4. A matrix keypad ที่ถูกสแกนโดยใช้ general purpose IO.
     5. Two LED indicators.
     6. An LCD display.
     7. An embedded web server ซึ่ง a remote monitoring computer สามารถต่อเข้าได้.
     8. An RS232 อินเตอร์เฟสไปยัง a configuration utility ที่รันบน a PDA.
Title: Re: FreeRTOS
Post by: tha on November 18, 2021, 06:19:57 AM
Top Level Software Requirements

ที่นี่เราสนใจใน the sequencing and timing requirements, มากกว่า the exact functional requirements.

Plant Control

แต่ละ control cycle จะต้องดำเนินการตามลำดับต่อไปนี้:

     1. ส่ง a frame ไปบน the fieldbus เพื่อร้องขอ data จาก the networked sensors.
     2. รอรับ data จากทั้งสอง sensors.
     3. ปฏิบัติ the control algorithm.
     4. ส่งคำสั่งไปยัง the plant.

The control function of the embedded computer จะต้องส่งคำร้องขอไปทุกๆ 10ms จริงๆ, และคำสั่งผลลัพธ์จะถูกส่งภายใน 5ms ของคำร้องขอนี้. The control algorithm อาศัยการจับเวลาที่แม่นยำ, ดังนั้นจึงเป็นสิ่งสำคัญยิ่งที่ timing requirements เหล่านี้ถูกตรงตาม.
Title: Re: FreeRTOS
Post by: tha on November 18, 2021, 07:10:09 AM
Local Operator Interface [keypad and LCD]

The keypad and LCD สามารถถูกใช้โดย the operator เพื่อเลือก, ดูและแก้ไข system data. The operator interface จะต้องทำหน้าที่ในขณะที่ the plant กำลังถูกควบคุม.

เพื่อให้แน่ใจว่าไม่มี key presses ถูกพลาดไป the keypad จะต้องถูกสแกนอย่างน้อบทุกๆ 15ms. The LCD จะต้องอัปเดตภายใน 50ms ของ a key ถูกกด.

LED

The LED จะต้องถูกใช้เพื่อแสดง the system status. A flashing green LED จะต้องแสดงว่า the system กำลังรันอยู่ตามที่คาดไว้. A flashing red LED จะต้องแสดง a fault condition.

The correct LED จะต้องกระพริบ on and off หนึ่งครั้งทุกๆวินาที. flash rate นี้จะต้องถูกคงไว้ภายใน 50ms.
Title: Re: FreeRTOS
Post by: tha on November 18, 2021, 07:36:01 AM
RS232 PDA Interface

The PDA RS232 interface จะต้องดูและเข้าถึง data เดียวกันกับ the local operator interface, และต้องใช้ข้อจำกัดด้านเวลาเดียวกัน - ลดเวลาในการส่งข้อมูลใดๆ.

TCP/IP Interface

The embedded web server จะต้องบริการ HTTP requests ภายในหนึ่งวินาที.
Title: Re: FreeRTOS
Post by: tha on November 18, 2021, 07:56:00 AM
Application components

The timing requirements of the hypothetical system สามารถถูกแบ่งออกเป็นสามประเภท:

     1. Strict timing - the plant control

         The control function มี a very strict timing requirement เนื่องจากมันต้องปฏิบัติทุกๆ 10ms.

     2. Flexible timing - the LED

         ในขณะที่ the LED outputs มีข้อจำกัดด้านเวลาทั้งสูงสุดและต่ำสุด มีแถบเวลาขนาดใหญ่ภายในที่พวกมันสามารถทำงานได้.

     3. Deadline only timing - the human interfaces

         นี้รวมถึง the keypad, LCD, RS232 and TCP/IP Ethernet communications.

         The human interface functions มีชนิดที่แตกต่างกันของ timing requirement เนื่องจากมีเพียง a maximum limit เท่านั้นที่ถูกระบุ. ตัวอย่างเช่น, the
         keypad ต้องถูกสแกนอย่างน้อยทุกๆ 10ms, แต่อัตราใดๆที่สูงถึง 10ms สามารถยอมรับได้.

NEXT >>> Solution #1: Why use an RTOS kernel?
Title: Re: FreeRTOS
Post by: tha on November 19, 2021, 06:27:18 AM
https://www.freertos.org/tutorial/solution1.html (https://www.freertos.org/tutorial/solution1.html)

Solution #1
Why Use an RTOS Kernel?

<<< | >>>

ดูเพิ่มเติมที่รายการคำถามที่พบบ่อย "Why use an RTOS?".

Synopsis(สรุป)

applications จำนวนมากสามารถถูกผลิตโดยไม่มีการใช้ของ an RTOS kernel และหน้านี้อธิบายการเข้าใกล้ที่อาจได้รับ.

แม้ว่าแอปพลิเคชันในกรณีนี้อาจซับซ้อนเกินไปสำหรับวิธีการดังกล่าว หน้าก็รวมไว้ทั้งการเน้นถึงปัญหาที่อาจเกิดขึ้นและจัดให้มีความแตกต่างกันไปยังการออกแบบซอฟต์แวร์ที่ใช้ RTOS ต่อไปนี้

Implementation

โซลูชันนี้ใช้ a traditional infinite loop approach, โดยแต่ละองค์ประกอบของ the application ถูกแสดงโดย a function ที่ปฏิบัติจนเสร็จสมบูรณ์.

ตามหลักการแล้ว a hardware timer จะถูกใช้เพื่อวางกำหนดเวลา the time critical plant control function. อย่างไรก็ตาม การต้องรอการมาถึงของ data และ the complex calculation ที่ดำเนินการทำให้ the control function ไม่เหมาะสำหรับการปฏิบัติภายใน an interrupt service routine.

Concept of Operation

ความบ่อยและลำดับซึ่ง the components ถูกเรียกภายใน the infinite loop สามารถถูกแก้ไขเพื่อนำสู่การจัดลำดับความสำคัญบางอย่าง. ทางเลือกอื่นในการจัดลำดับดังกล่าวถูกจัดให้มีในตัวอย่างด้านล่าง.

Scheduler Configuration

The RTOS scheduler ไม่ถูกใช้.

(https://i.imgur.com/Rqfxoet.png)

Conclusion

The simple loop approach จะดีมากสำหรับ small applications และ applications ที่มี flexible timing requirements - แต่สามารถกลายมาเป็นความซับซ้อน, ยากต่อการวิเคราะห์และยากต่อการซ่อมแซมถ้าปรับขนาดเป็นระบบที่ใหญ่ขึ้น.
Title: Re: FreeRTOS
Post by: tha on November 19, 2021, 08:37:19 AM
Example

ตัวอย่างนี้เป็นการจัดให้มีใช้บางส่วนของ the hypothetical application ที่แนะนำก่อนหน้านี้

The Plant Control Function

The control function สามารถถูกแสดงโดย the following pseudo code:

(https://i.imgur.com/xMED2M0.png)
Title: Re: FreeRTOS
Post by: tha on November 19, 2021, 08:58:22 AM
The Human Interface Functions

นี้รวม the keypad, LCD, RS232 communications และ embedded web server.

The following pseudo code แสดง a simple infinite loop structure สำหรับการควบคุม interfaces เหล่านี้.

(https://i.imgur.com/fm97K33.png)

นี้ทึกทักเอาสองสิ่ง: สิ่งแรก, The communications IO ถูกบัฟเฟอร์โดย interrupt service routines ดังนั้น peripherals ไม่ต้องการการวนตรวจเช็ค. สิ่งที่สอง, the individual function เรียกภายใน the loop ปฏิบัติอย่างรวดเร็วเพียงพอสำหรับ the maximum timing requirements ทั้งหมดถูกตรงตาม.
Title: Re: FreeRTOS
Post by: tha on November 20, 2021, 05:57:43 AM
Scheduling the Plant Control Function

ความยาวของ the control function หมายความว่ามันไม่สามารถถูกเรียกได้ง่ายๆจาก a 10ms timer interrupt.

การเพิ่มมันไปยัง the infinite loop จะต้องการการนำเข้ามาของการควบคุมชั่วคราวบางอย่าง. ตัวอย่างเช่น ... :

(https://i.imgur.com/A8RyAfJ.png)
(https://i.imgur.com/W8eU5sP.png)

... แต่นี่ไม่ใช่วิธีแก้ปัญหาที่ยอมรับได้:

     •  A delay or fault บน the field bus ส่งผลให้ an increased execution time ของ the plant control function. The timing requirements ของ the
         interface functions ส่วนใหญ่มักจะถูกละเมิด.

     •  การดำเนินการฟังก์ชันทั้งหมดในแต่ละรอบอาจส่งผลให้เกิดการละเมิดของ the control cycle timing.

     •  ค่าที่แตกต่างอย่างกะทันหันใน the execution time อาจทำให้พลาดรอบ ตัวอย่างเช่น the execution time of ProcessHTTPRequests() อาจมีความสำคัญ
         เล็กน้อยเมื่อไม่มี HTTP requests ถูกรับ, แต่ค่อนข้างนานเมื่อ a page กำลังถูกให้บริการ.

     •  ไม่สามารถบำรุงรักษาได้มาก - ขึ้นอยู่กับทุกฟังก์ชันที่ดำเนินการภายในเวลาสูงสุด

     •  The communication buffers จะให้บริการเพียงครั้งเดียวต่อรอบโดยจำเป็นต้องมีความยาวของบัฟเฟอร์มากกว่าที่จำเป็น.
Title: Re: FreeRTOS
Post by: tha on November 21, 2021, 05:45:57 AM
Alternative Structures

สองปัจจัยสามารถถูกระบุว่าจำกัดความเหมาะสมของ the simple loop structure ที่อธิบายไว้จนถึงตอนนี้.

.     1. ความยาวของแต่ละ function call

          การอนุญาตให้แต่ละฟังก์ชันดำเนินการอย่างครบถ้วนใช้เวลานานเกินไป ซึ่งสามารถป้องกันได้โดยการแบ่งแต่ละฟังก์ชันออกเป็นหลายสถานะ มีเพียงสถานะเดียวถูก
          ดำเนินการในแต่ละครั้ง โดยใช้ the control function ดังตัวอย่าง:

(https://i.imgur.com/rixRrTh.png)
(https://i.imgur.com/L2zirGD.png)

          ฟังก์ชันนี้ซับซ้อนมากขึ้นในเชิงโครงสร้าง และนำสู่ปัญหาการจัดกำหนดการเพิ่มเติม ตัวโค้ดเองจะเข้าใจยากขึ้นเมื่อมีการเพิ่มสถานะพิเศษ เช่น เพื่อจัดการกับเงื่อนไข
          การหมดเวลาและข้อผิดพลาด
Title: Re: FreeRTOS
Post by: tha on November 21, 2021, 05:55:50 AM
     2. ความละเอียดของ the time

         A shorter timer interval จะทำให้มีความยืดหยุ่นมากขึ้น.

         การจัดให้มีใช้ the control function เป็น a state machine (ในการทำให้แต่ะละการ call สั้นลง) อาจยอมให้มันถูกเรียกจาก a timer interrupt. The timer
         interval จะต้องสั้นเพียงพอเพื่อให้แน่ใจว่า the function ได้รับการเรียกด้วยความถี่ที่ตรงตาม timing requirements ของมัน. ตัวเลือกนี้ล้มเหลวด้วย timing
         and maintenance problems.

         อีกทางหนึ่ง the infinite loop solution สามารถถูกแก้ไขเพื่อเรียก functions ที่แตกต่างกันในแต่ละ loop - ด้วย the high priority control function
         ถูกเรียกบ่อยมากกว่า:

(https://i.imgur.com/qKaEx7t.png)
(https://i.imgur.com/Tiagkc5.png)

         intelligence ที่มากขึ้นสามารถถูกนำสู่โดยวิธีการของ event counters, โดยที่ the lower priority functionality ถูกเรียกเฉพาะถ้า an event ได้เกิดขึ้นซึ่ง
         ต้องการการบริการ:

(https://i.imgur.com/OG7zTwZ.png)
(https://i.imgur.com/LpL1cUv.png)

         การประมวลผล events ในลักษณะนี้จะลดรอบ CPU ที่สูญเปล่าแต่ the design จะยังคงแสดงค่าที่แตกต่างอย่างกะทันหันในความถี่ซึ่ง the control cycle ปฏิบัติ.

NEXT >>> Solution #2: A fully preemptive system
Title: Re: FreeRTOS
Post by: tha on November 21, 2021, 08:38:15 AM
https://www.freertos.org/tutorial/solution2.html (https://www.freertos.org/tutorial/solution2.html)

Solution #2
A Fully Preemptive System

<<< | >>>

Synopsis

นี้คือ a traditional preemptive multitasking solution. มันทำการใช้ของ the RTOS services อย่างเต็มที่โดยไม่คำนึงถึง the resultant memory and processor overhead. มีการแบ่งส่วนอย่างง่ายๆของฟังชั่นที่จำเป็นให้กับ tasks อิสระจำนวนหนึ่ง.

Implementation

task ที่แยกกันถูกสร้างสำหรับแต่ละส่วนของ the system ที่สามารถถูกระบุได้ว่าสามารถอยู่แยกกันได้, หรือว่ามี a particular timing requirement.

(https://i.imgur.com/DjCRdca.png)

Tasks จะโดนบล็อกจนกระทั่ง an event แสดงให้เห็นว่าการประมวลผลถูกต้องการ. Events สามารถอย่างใดอย่างหนึ่งเป็นภายนอก (อย่างเช่น a key กำลังถูกกด), หรือภายใน (อย่างเช่น a timer หมดเวลาลง). event นี้ที่ขับเข้าใกล้หมายความว่าไม่มี CPU time เสียไปกับการวนตรวจสอบ events ที่ยังไม่ได้เกิดขึ้น.

Priorities ถูกจัดสรรให้กับ tasks สอดคล้องกับ timing requirements ของมัน. ยิ่ง the timing requirement เข้มงวดเท่าใด the priority ก็ยิ่งสูงขึ้น (ไม่ใช่การประเมินการมอบหมายลำดับความสำคัญทั้งหมดจะง่ายขนาดนั้น).
Title: Re: FreeRTOS
Post by: tha on November 21, 2021, 09:16:36 AM
Concept of Operation

The highest priority task ที่สามารถปฏิบัติ (ไม่ถูกบล็อก) คือ the task ที่ได้รับประกันโดย the RTOS ว่าจะได้รับ processor time. The kernel จะระงับ an executing task ทันทีหาก a higher priority task กลายมาเป็นมีให้ใช้ประโยชน์.

การจัดกำหนดเวลานี้เกิดขึ้นโดยอัตโนมัติ โดยไม่มีความรู้ โครงสร้าง หรือคำสั่งที่ชัดเจนภายในซอร์สโค้ดของแอปพลิเคชัน อย่างไรก็ตาม เป็นความรับผิดชอบของผู้ออกแบบแอปพลิเคชันที่จะต้องแน่ใจว่า tasks ได้รับการจัดสรรตามลำดับความสำคัญที่เหมาะสม

เมื่อไม่มี task สามารถปฏิบัติ the idle task จะปฏิบัติ. The idle task มีทางเลือกที่จะวาง the processor ลงใน power save mode.
Title: Re: FreeRTOS
Post by: tha on November 21, 2021, 02:25:43 PM
Evaluation

     ข้อดี - การออกแบบที่เรียบง่าย แบ่งส่วน ยืดหยุ่น บำรุงรักษาได้ โดยมีการพึ่งพาอาศัยกันเพียงเล็กน้อย
     ข้อดี - การใช้งานตัวประมวลผลจะเปลี่ยนจากงานหนึ่งไปอีกงานหนึ่งโดยอัตโนมัติตามความจำเป็นเร่งด่วนที่สุด โดยไม่ต้องดำเนินการใดๆ อย่างชัดเจนภายใน the
              application source code.
     ข้อดี - The event ที่ขับเคลื่อน structure ทำให้แน่ใจว่าไม่มี CPU time เสียไปกับการวนตรวจสอบสำหรับ events ที่ไม่ได้เกิดขึ้น. การประมวลผลถูกดำเนินการ
               เฉพาะเมื่อมีงานที่จำเป็นต้องถูกทำเท่านั้น.
     ปานกลาง - Power consumption สามารถถูกลดลงได้ถ้า the idle task วาง the processor ลงใน power save (sleep) mode, แต่อาจะเสียไปอีกด้วย
                     เนื่องจาก the tick interrupt จะบางครั้งปลุก the processor โดยไม่จำเป็น.
     ปานกลาง - The kernel functionality จะใช้ processing resources. ขอบเขตของสิ่งนี้จะขึ้นอยู่กับ the chosen kernel tick frequency.
     ข้อเสีย - โซลูชันนี้ต้องการ tasks จำนวนมาก ซึ่งแต่ละ task ต้องการ stack ของตัวเอง และหลายๆ tasks ต้องการ a queue ซึ่ง events สามารถถูกรับ ดังนั้นวิธีนี้จึง
                  ใช้ RAM จำนวนมาก.
     ข้อเสีย - ความบ่อยของ context switching ระหว่าง tasks of the same priority จะเสีย processor cycles.

Conclusion

นี้สามารถเป็น a good solution ที่จัดให้มี the RAM and processing capacity มีให้ใช้ประโยชน์. การแบ่งพาติชั่นของ the application ลงใน tasks and the priority ที่กำหนดไปยังแต่ละ task ต้องการการพิจารณาอย่างระมัดระวัง.
Title: Re: FreeRTOS
Post by: tha on November 21, 2021, 06:31:53 PM
Example

ตัวอย่างนี้เป็นการจัดให้มีใช้บางส่วนของ the hypothetical application ที่แนะนำมาก่อนหน้านี้. The FreeRTOS API ถูกใช้.

Plant Control Task

task นี้จัดให้มีใช้ the control functionality ทั้งหมด. มันมี critical timing requirements และดังนั้นถูกให้ the highest priority ภายใน the system:

(https://i.imgur.com/E4edZBL.png)
(https://i.imgur.com/RjkAeog.png)

อ้างถึง the labels ภายใน the code fragment  ด้านบน:

     A. xLastWakeTime ถูกเริ่มต้น. ตัวแปรนี้ถูกใช้ด้วย the vTaskDelayUntil() API function เพื่อควบคุมความถี่ที่ the control function ปฏิบัติ.

     B. ฟังชั่นนี้ปฏิบัติเป็น an autonomous task ดังนั้นต้องไม่เคยออก

    C. vTaskDelayUntil() บอก the kernel ว่า task นี้ควรสตาร์ทการปฏิบัติจริงๆ 10ms หลังจาก the time ที่เก็บใน xLastWakeTime. จนกระทั่งเวลานี้ถูกถึง the
        control task จะ block. เนื่องจากนี้คือ the highest priority task ถายใน the system มันถูกรับประกันเพื่อสตาร์ทการปฏิบัติอีกครั้งที่ the correct time
        จริงๆ. มันจะยึดเอาไว้ก่อน(หยุด) lower priority task ใดๆที่กำลังรันอยู่.

     D. มี a finite time ระหว่าง data ที่กำลังถูกร้องขอจาก the networked sensors และกับ data ที่กำลังถูกรับ. Data มาถึงบน the field bus ถูกวางใน the
         xFieldBusQueue โดย an interrupt service routine, the control task ดังนั้นสามารถทำการบล็อกแล้วเรียกไปบน the queue เพื่อรอสำหรับ data มีให้ใช้
         ประโยชน์. ตามที่ก่อนหน้า, เนื่องจากมันเป็น the highest priority task ใน the system มันถูกรับประกันการปฏิบัติต่อไปทันทีที่ data มีให้ใช้ประโยชน์.

     E. เหมือน 'D', การรอสำหรับ data จาก the second sensor.

A return value เป็น 0 จาก xQueueReceive() แสดงให้เห็นว่าไม่มี data มาถึงภายใน the specified block period. นี้เป็น an error condition ที่ the task ต้องรับมือ. นี้และ other error handling functionality ถูกละเว้นละเลยเพื่อความเรียบง่าย.
Title: Re: FreeRTOS
Post by: tha on November 22, 2021, 05:45:28 AM
Embedded Web Server Task

The embedded web server task สามารถถูกแสดงด้วย the following pseudo code. นี้ใช้ processor time เฉพาะเมื่อ data มีให้ใช้ประโยชน์แต่จะต้องใช้ตัวแปรและเวลาค่อนข้างนานเพื่อเสร็จสมบูรณ์. ดังนั้นมันถูกให้ a low priority เพื่อป้องกันไม่ให้มันส่งผลในทางลบต่อ the timing of the plant control, RS232 or keypad scanning tasks.

(https://i.imgur.com/aJDF6Wj.png)
Title: Re: FreeRTOS
Post by: tha on November 22, 2021, 06:03:31 AM
RS232 Interface

นี้จะเหมือนกันมากใน structure กับ the embedded web server task. มันถูกให้ a medium priority เพื่อให้แน่ใจว่ามันไม่ส่งผลในทางลบต่อ the timing of the plant control task.

(https://i.imgur.com/ev8EdC2.png)
Title: Re: FreeRTOS
Post by: tha on November 22, 2021, 06:48:28 AM
Keypad Scanning Task

นี้คือ a simple cyclical task. มันถูกให้ a medium priority เนื่องจาก timing requirements ของมันเหมือนกันกับ the RS232 task.

The cycle time ถูกตั้งค่าเร็วกว่าขีดจำกัดที่ระบุมาก. ทั้งนี้เนื่องจากข้อเท็จจริงที่ว่ามันอาจไม่ได้รับ processor time ทันทีที่ร้องขอ - และเมื่อปฏิบัติอาจได้รับการยึดเอาไว้ก่อน(ระงับ)โดย the plant control task.

(https://i.imgur.com/h3yQaHl.png)

ถ้า the overall system timing เป็นเช่นนั้นนี้สามารถทำให้ the lowest priority task ดังนั้น the call to vTaskDelayUntil() สามารถถูกลบออกทั้งหมด. The key scan function ดังนั้นจะปฏิบัติอย่างต่อเนื่องเมื่อไรก็ตามที่ the higher priority tasks ทั้งหมดถูกบล็อกแล้ว - แทนที่ the idle task อย่างมีประสิทธิภาพ.
Title: Re: FreeRTOS
Post by: tha on November 22, 2021, 07:19:48 AM
(https://i.imgur.com/UwbC3Jn.png)
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 05:36:10 AM
https://www.freertos.org/tutorial/solution3.html (https://www.freertos.org/tutorial/solution3.html)

Solution #3
Reducing RAM Utilisation

<<< | >>>

หมายเหตุ: หน้าเหล่านี้ไม่ถูกอัปเดตมาตั้งแต่ the introduction of FreeRTOS V4.0.0. V4.0.0 แนะนำ the concept of co-routines ซึ่งจะจัดให้มีโซลูชันที่แตกต่างและแปลกใหม่ถึงเหล่านั้นที่แสดงที่นี่. The Tasks and Co-routines documentation จัดให้มีข้อมูลเพิ่มเติม.

Synopsis

Solution #2 ทำการใช้อย่างเต็มที่ของ the RTOS. นี้ส่งผลให้ a clean design, แต่หนึ่งนั้นสามารถถูกใช้เฉพาะกับ embedded computers ที่มี RAM และ processing resource เพียงพอ. Solution #3 พยายามลด the RAM usage โดยการเปลี่ยนการแบ่งพาติชั่นของฟังชั่นลงใน tasks.

(https://i.imgur.com/rycX4UO.png)

ก่อนหน้านี้เราได้เห็นแล้วว่า the timing requirements of our hypothetical application สามารถแบ่งออกเป็นสามประเภทได้อย่างไร:

     1. Strict timing - the plant control

         ดังก่อนหน้านี้, a high priority task ถูกสร้างเพื่อบริการ the critical control functionality.

     2. Deadline only timing - the human interfaces

         Solution #3 จับกลุ่ม the RS232, keyscan and LED functionality ลงใน a single medium priority task.

         ด้วยเหตุผลที่ระบุไว้ก่อนหน้านี้ มันเป็นที่น่าพอใจสำหรับ the embedded web server task ที่ทำงานที่ a lower priority. แทนที่จะสร้าง a task เป็นพิเศษ
         สำหรับ the web server an idle task hook ถูกจัดให้มีใช้เพื่อเพิ่ม the web server functionality ให้กับ the idle task. The web server ต้องถูกเขียน
         ให้แน่ใจว่ามันไม่เคยโดนบล็อก!

     3. Flexible timing - the LED

         The LED functionality นั้นง่ายเกินไปที่จะรับประกัน task ของตัวเองหาก RAM อยู่ในระดับพรีเมี่ยม. สำหรับเหตุผลของการสาธิต ตัวอย่างนี้รวมเอา the LED
         functionality ไว้ใน the single medium priority task. แน่นอนว่ามันสามารถถูกนำไปใช้ได้หลายวิธี (จาก a peripheral timer ตัวอย่างเช่น).

Tasks งานอื่นนอกเหนือจาก the idle task จะถูกบล็อกจนกระทั่ง an event แสดงให้เห็นว่าการประมวลผลถูกต้องการ. Events สามารถเป็นอย่างใดอย่างหนึ่งภายนอก external (อย่างเช่น a key กำลังถูกกด), หรือภายใน (อย่างเช่น a timer หมดเวลาลง).
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 07:05:53 AM
Concept of Operation

การจัดกลุ่มของฟังชั่นลงใน the medium priority task มีข้อได้เปรียบที่สำคัญสามประการเหนือ the infinite loop implementation ที่แสดงใน solution #1:

     1. การใช้ของ a queue ยอมให้ the medium priority task ถูกบล็อกจนกระทั่ง an event ที่ทำให้ data มีให้ใช้ประโยชน์ - และดังนั้นกระโดดทันทีไปยังฟังชั่นที่
         เกี่ยวข้องเพื่อจัดการ the event. สิ่งนี้จะป้องกันการสูญเปล่า processor cycles - ในทางตรงกันข้ามกับ the infinite loop implementation โดยที่ an event
         จะถูกประมวลผลเฉพาะเมื่อ the loop วนรอบไปยัง the appropriate handler.

     2. การใช้ของ the real time kernel เอาออกความต้องการพิจารณาอย่างชัดแจ้ง the scheduling of the time critical task ภายใน the application source
         code.

     3. การเอาออกของ the embedded web server function จาก the loop ทำให้ the execution time สามารถคาดการณ์ได้มากขึ้น.

นอกจากนี้, ฟังชั่นที่ถูกจัดกลุ่มลงใน a single task ถูกได้รับจากหลาย tasks ที่ก่อนหน้านี้แชร์ the same priority ร่วมกัน (ยกเว้น the LED function). ความถี่ที่ code ที่ priority นี้ดำเนินการจะไม่เปลี่ยนแปลงไม่ว่าจะเป็น task เดียวหรือหลาย tasks.

The plant control task, เป็น the highest priority task, ได้รับการประกันว่าได้รับจัดสรร processing time เมื่อไรก็ตามที่มันต้องการ. มันจะยึดเอาไว้ก่อน(ระงับ) the low and medium priority tasks หากจำเป็น. The idle task จะปฏิบัติเมื่อไรก็ตามที่ทั้งสอง application tasks ถูกบล็อก. The idle task มีทางเลือกของการวาง the processor ลงใน power save mode.
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 08:28:38 AM
Evaluation

     ข้อดี - สร้างเพียงสอง application tasks ดังนั้นจึงใช้ RAM น้อยกว่า solution #2 มาก.
     ข้อดี - Processor utilisation จะเปลี่ยนจาก task หนึ่งไปอีก task หนึ่งโดยอัตโนมัติตามความจำเป็นเร่งด่วนที่สุด.
     ข้อดี - การใช้ประโยชน์ the idle task สร้าง three application task priorities อย่างมีประสิทธิภาพที่มีค่าโสหุ่ยเพียงสอง.
     ปานกลาง - The design ยังคงง่ายแต่ the execution time of the functions ภายใน the medium priority task สามารถนำสู่ปัญหาเรื่องเวลาได้.
                การแยกออกของ the embedded web server task จะลดความเสี่ยงนี้และไม่ว่าในกรณีใดๆปัญหาดังกล่าวจะไม่ส่งผลกระทบต่อ the plant control task.
     ปานกลาง - Power consumption สามารถถูกลดลงได้ถ้า the idle task วาง the processor ลงใน power save (sleep) mode, แต่อาจะเสียไปเนื่องจาก the
                tick interrupt จะบางครั้งปลุก the processor โดยไม่จำเป็นอีกด้วย.
     ปานกลาง - The RTOS functionality จะใช้ processing resources. ขอบเขตของสิ่งนี้จะขึ้นอยู่กับ the chosen kernel tick frequency.
     ข้อเสีย - การออกแบบอาจไม่ปรับขนาดหากแอปพลิเคชันมีขนาดใหญ่เกินไป

Conclusion

นี้สามารถเป็น a good solution สำหรับ systems ที่มี RAM ที่จำกัดแต่มันยังใช้ processor ที่เข้มข้นอยู่. Spare capacity ภายใน the system ควรถูกตรวจสอบเพื่อยอมให้สำหรับการขยายในอนาคต.
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 09:11:28 AM
Example

ตัวอย่างนี้เป็นการจัดให้มีใช้บางส่วนของ the hypothetical application ที่แนะนำมาก่อนหน้านี้. The FreeRTOS API ถูกใช้.

Plant Control Task

The plant control task เหมือนกับที่อธิบายไว้ใน solution #2.

The Embedded web Server

นี้เป็นฟังชั่นง่ายๆที่ถูกเรียกจาก the idle task และทำงานจนเสร็จสิ้น.
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 10:05:11 AM
(https://i.imgur.com/1kdQqQe.png)
(https://i.imgur.com/lMVnjA7.png)

อ้างถึง the labels ภายใน the code fragment ข้างบน:

     A. The task first blocks กำลังรอสำหรับ a communications event. The block time ค่อนข้างสั้น.

     B. The do-while loop ปฏิบัติจนกระทั่งไม่มี data เหลืออยู่ใน the queue. การจัดให้มีใช้นี้จะต้องถูกแก้ไขถ้า data มาถึงเร็วเกินไปสำหรับ the queue ที่ว่างเปล่า
          อย่างสมบูรณ์เสมอ.

     C. อย่างใดอย่างหนึ่ง the queue ถูกทำให้ว่างเปล่าของ data ทั้งหมด, หรือไม่มี data มาถึงภายใน the specified blocking period. The maximum time ที่
         สามารถถูกใช้บล็อกรอสำหรับ data ต้องสั้นเพียงพอเพื่อให้แน่ใจว่า the keypad ถูกสแกนได้บ่อยเพียงพอเพื่อให้ตรงตาม the specified timing constraints.

     D. ตรวจสอบดูว่าถ้ามันเป็นเวลาที่จะกะพริบไฟ LED. จะมีค่าที่แตกต่างอย่างกะทันหันบางอยางในความถี่ซึ่ง line นี้ปฏิบัติ, แต่ the LED timing requirements มี
         ความหยืดหยุ่นเพียงพอเพื่อให้ถูกตรงตามโดย implementation นี้.

NEXT >>> Solution #4: Reducing the processor overhead
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 10:51:54 AM
https://www.freertos.org/tutorial/solution4.html (https://www.freertos.org/tutorial/solution4.html)

Solution #4
Reducing the Processor Overhead

<<< | >>>

หมายเหตุ: หน้าเหล่านี้ไม่ถูกอัปเดตมาตั้งแต่ the introduction of FreeRTOS V4.0.0. V4.0.0 แนะนำ the concept of co-routines ซึ่งจะจัดให้มีโซลูชันที่แตกต่างและแปลกใหม่ถึงเหล่านั้นที่แสดงที่นี่. The Tasks and Co-routines documentation จัดให้มีข้อมูลเพิ่มเติม.

Synopsis

Solution #2 แสดงวิธีที่ a clean application สามารถถูกผลิตโดยการใช้ประโยชน์อย่างเต็มที่ the RTOS functionality. Solution #3 แสดงวิธีที่สิ่งนี้สามารถถูกปรับใช้สำหรับ embedded computers ที่มี limited RAM resource. Solution #4 ทำการแก้ไขปรับปรุงเพิ่มเติมโดยมีวัตถุประสงค์เพื่อลด the RTOS processing overhead.

A hybrid scheduling algorithm (ไม่ทั้ง fully preemptive หรือ fully cooperative) ถูกสร้างโดยการกำหนดค่า the kernel สำหรับ cooperative scheduling, จากนั้นดำเนินการ context switching จากภายใน event interrupt service routines.
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 12:27:18 PM
(https://i.imgur.com/mc1QR4F.png)

The critical plant control functionality ถูกจัดให้มีใช้อีกครั้งหนึ่งโดย a high priority task แต่การใช้ของ the cooperative scheduler จำเป็นต้องเปลี่ยนแปลงการจัดให้มีใช้ของมัน. ก่อนหน้านี้ the timing ถูกรักษาโดยใช้ the vTaskDelayUntil() API function. เมื่อ the preemptive scheduler ถูกใช้ในอดีต, การกำหนด the control task ให้เป็น the highest priority เพื่อให้แน่ใจว่ามันจะสตาร์ทการปฏิบัติที่เวลาที่ระบุไว้จริงๆ. ตอนนี้ the cooperative scheduler กำลังถูกใช้ - ดังนั้น a task switch จะเกิดขึ้นเฉพาะเมื่อถูกร้องขออย่างชัดแจ้งจาก the application source code ดังนั้น the guaranteed timing จะหายไป.

Solution #4 ใช้ an interrupt จาก a peripheral timer เพื่อให้แน่ใจว่า a context switch ถูกร้องขอที่ the exact frequency ที่ต้องการโดย the control task. The scheduler เพื่อให้แน่ใจว่าแต่ละ requested context switch ส่งผลให้ a switch ไปยัง the highest priority task ที่สามารถรัน.

The keypad scanning function ต้องการ regular processor time อีกด้วยดังนั้นมันจึงถูกปฏิบัติภายใน the task ที่ทริกโดย the timer interrupt ด้วย. เวลาของ task นี้สามารถถูกประเมินได้ง่าย processing time ในกรณีที่เลวร้ายที่สุดของ the control function จะถูกให้โดย the error case - เมื่อไม่มี data จาก the networked sensors เป็นเหตุให้ the control function หมดเวลา The execution time of the keypad scanning function โดยทั่วไปถูกทำให้คงที่. ดังนั้นเราจึงสามารถมั่นใจได้ว่าการโยงฟังก์ชันการทำงานในลักษณะนี้จะไม่ส่งผลให้เกิดค่าที่แตกต่างอย่างกะทันหันใน the control cycle frequency - หรือแย่กว่านั้นคือยังพลาดรอบการควบคุม

The RS232 task จะถูกวางกำหนดเวลาโดย the RS232 interrupt service routine.

The flexible timing requirements of the LED functionality หมายความว่ามันสามารถบางทีร่วมกันกับ the embedded web server task ภายใน the idle task hook. ถ้าสิ่งนี้ไม่เพียงพอดังนั้นมันสามารถถูกย้ายขึ้นไปยัง the high priority task ได้ด้วย.
Title: Re: FreeRTOS
Post by: tha on November 23, 2021, 05:01:27 PM
Concept of Operation

The cooperative scheduler จะดำเนินการ a context switch เฉพาะเมื่อถูกร้องขออย่างชัดแจ้งเท่านั้น. นี้ลด the processor overhead ที่กำหนดโดย the RTOS อย่างมาก [ยกเว้นสำหรับความจริงที่ว่า the idle task ไม่สามารถใส่ the processor ลงใน a power saving mode ได้อีกต่อไป?]. The idle task, รวมถึง the embedded web server functionality, จะปฏิบัติโดยไม่มี interruptions ที่ไม่จำเป็นใดๆจาก the kernel.

An interrupt ที่เกิดจากอย่างใดอย่างหนึ่ง the RS232 หรือ timer peripheral จะส่งผลให้เกิด a context switch จริงๆและเฉาพะเมื่อจำเป็นเท่านั้น. วิธีนี้ the RS232 task จะยังคงยึด(ระงับ) the idle task เอาไว้ก่อน, และตัวเองยังคงสามารถถูกยึด(ระงับ)เอาไว้ก่อนโดย the plant control task - รักษา the prioritised system functionality.

Scheduler Configuration

The scheduler ถูกกำหนดค่าสำหรับ cooperative operation. The kernel tick ถูกใช้เพื่อรักษา the real time tick value เท่านั้น.
Title: Re: FreeRTOS
Post by: tha on November 24, 2021, 06:27:26 AM
Evaluation

     ข้อดี - สร้างเพียงสอง application tasks ดังนั้นจึงใช้ RAM น้อยกว่า solution #2 มาก.
     ปานกลาง - The RTOS context switching overhead ถูกลดลงเหลือน้อยที่สุด - แม้ว่า more CPU cycles อาจถูกใช้ประโยชน์โดย the idle task ซึ่งไม่สามารถ
                   ทำการใช้ของ power saving modes ได้อีกต่อไป.
     ปานกลาง - เฉพาะส่วนย่อยของ the RTOS features ถูกใช้. สิ่งนี้จำเป็นต้องพิจารณา the timing and execution environment ที่ the application source
                   code level ให้มากขึ้น, แต่ยังคงยอมให้สำหรับการออกแบบที่ง่ายขึ้นอย่างมาก (เมื่อเทียบกับ solution #1).
     ข้อเสีย - พึ่งพา processor peripherals. ไม่ portable
     ข้อเสีย - ปัญหาของการวิเคราะห์และการพึ่งพาอาศัยกันระหว่างโมดูลต่างๆ ตามที่ระบุด้วยโซลูชัน # 1 กำลังเริ่มมีการพิจารณาอีกครั้ง - แม้ว่าจะมีขอบเขตน้อยกว่ามาก
     ข้อเสีย - การออกแบบอาจไม่ปรับขนาดหากแอปพลิเคชันมีขนาดใหญ่เกินไป

Conclusion

คุณลักษณะของ the RTOS kernel สามารถถูกใช้ด้วย very little overhead, ทำให้สามารถ a simplified design แม้ในระบบที่ processor and memory constraints ป้องกันไม่ให้ a fully preemptive solution.
Title: Re: FreeRTOS
Post by: tha on November 24, 2021, 07:44:09 AM
Example

ตัวอย่างนี้เป็นการจัดให้มีใช้บางส่วนของ the hypothetical application ที่แนะนำมาก่อนหน้านี้. The FreeRTOS API ถูกใช้.

High Priority Task

The high priority task ถูกทริกโดย a semaphore 'ที่ให้' โดย a periodic interrupt service routine:

(https://i.imgur.com/ePsbzSc.png)

โปรดทราบว่า the syntax ที่ใช้บังคับให้ a context switch จากภายใน an ISR จะแตกต่างกันสำหรับ ports ที่แตกต่างกัน. ไม่ก๊อปปี้ตัวอย่างนี้โดยตรงแต่แทนด้วยการเช็ค the documentation สำหรับ the port ที่คุณกำลังใช้.

The high priority task บรรจุทั้ง the plant control and keypad functionality. PlantControlCycle() ถูกเรียกก่อนเพื่อให้แน่ใจว่ามีความสม่ำเสมอในเวลาของมัน.

(https://i.imgur.com/PCG4FCu.png)
Title: Re: FreeRTOS
Post by: tha on November 24, 2021, 07:56:04 AM
RS232 Task

The RS232 task โดนบล็อกง่ายๆบน a queue รอสำหรับ data มาถึง. The RS232 interrupt service routine ต้องโพสต์ the data ลงบน the queue - ทำให้ the task พร้อมรัน - จากนั้นบังคับ a context switch. กลไกลนี้เป็นไปตาม the timer interrupt pseudo code ที่ให้ไว้ด้านบน.

The RS232 task ดังนั้นสามารถถูกแสดงโดย the following pseudo code:

(https://i.imgur.com/njMVNnj.png)
Title: Re: FreeRTOS
Post by: tha on November 24, 2021, 08:05:17 AM
The Embedded Web Server and LED Functionality

ฟังก์ชันการทำงานของระบบที่เหลือถูกวางไว้ภายใน the idle task hook.  นี่เป็นเพียงฟังก์ชันที่ถูกเรียกใช้โดยแต่ละรอบของ the idle task.

(https://i.imgur.com/fCtNaI0.png)
Title: Re: FreeRTOS
Post by: tha on November 24, 2021, 02:51:45 PM
https://www.freertos.org/building-demos-in-eclipse.html (https://www.freertos.org/building-demos-in-eclipse.html)

Import and Build a Demo Project in Eclipse

หน้านี้จัดให้มีข้อมูลทั่วไปเกี่ยวกับวิธีการใช้ Eclipse เพื่อนำเข้าและบิ้วด์ the many FreeRTOS demo applications ที่จัดให้มีเป็น Eclipse projects. The Demo application specific documentation pages จัดให้มีข้อมูลเพิ่มเติมเฉพาะเจาะจงไปยังโปรแกรมสาธิตที่ให้, รวมถึงตำแหน่งของโปรเจ็คภายใน the FreeRTOS directory structure. The demostration ข้างล่างอิงตาม the base Eclipse Embedded CDT version บน a Windows enviornment.

NOTE: Vendor-specific Eclipse distributions อาจดูแตกต่างจาก the base Eclipse version ที่ใช้สร้าง the screen shots ข้างล่าง.
Title: Re: FreeRTOS
Post by: tha on November 24, 2021, 05:46:04 PM
Prerequisites:

     1. ติดตั้ง an Eclipse distribution

        The base Eclipse version, Eclipse Embedded CDT (C/C++ Development Tools), สามารถถูกดาวน์โหลดได้ที่นี่.

       หมายเหตุ: ส่วนใหญ่ของ the vendor-specific Eclipse-based IDEs รวมเอา the toolchain ในการแจกจ่าย, และรับมือการตั้งค่าเส้นทางไปยังมันโดยอัตโนมัติ
       และโปร่งใสสำหรับคุณ. โปรดตรวจสอบด้วย specific demo page ของคุณที่เชื่อมต่อไปบน Demo application specific documentation. คุณอาจข้ามขั้นตอน
       ดังต่อไปนี้ถ้า vendor-specific IDE ของคุณจัดให้มี the toolchain และ build tools.

     2. ติดตั้ง the GCC Toolchain

        อ้างถึง the Demo application specific documentation pages สำหรับข้อมูลเกี่ยวกับ the specific toolchain ที่จำเป็นสำหรับ demo ของคุณ.

     3. ติดตั้ง the build tool (Windows)

        POSIX platforms โดยทั่วไปรวมเอา make ไว้ใน the system distribution, หรือพวกเขาอาจต้องการให้คุณติดตั้งบาง developer packages (ตัวอย่างเช่น,
        บน macOS คุณจำเป็นต้องติดตั้ง the Xcode Command Line Tools). ทำตาม the "Windows Build Tools" section ในคู่มือนี้เพื่อติดตั้ง the Make build
        tools.

       หมายเหตุ: หากคุณมีสภาพแวดล้อมอื่นติดตั้งอยู่ เช่น MinGW หรือ Cygwin และต้องการใช้ the Make tool ที่จัดให้มีโดย environment นั้น, ทำให้แน่ใจว่า the
       make program มีชื่อ "make.exe". ในวิธีเดียวกัน, สำหรับ MinGW, เปลี่ยนชื่อ (หรือสร้าง a copy และเปลี่ยนชื่อ) "mingw32-make.exe" เป็น
       "make.exe".
Title: Re: FreeRTOS
Post by: tha on November 25, 2021, 09:31:00 AM
     4. จัดการ the PATH Environment variable

        The base Eclipse version อาศัยการตั้งค่า the environment variable PATH เพื่อเข้าถึง the toolchain binaries. The PATH สามารถถูกเซ็ตจาก:

             • The system global PATH setting หรือต่อผู้ใช้ PATH setting.
             • The Eclipse workspace's common settings for all projects.
             • The project's build configuration.

        เราขอแนะนำอย่างยิ่งให้คุณเซ็ต the PATH จาก the project's build configuration เพื่อหลีกเลี่ยงข้อขัดแย้งใดๆเมือใช้หลาย toolchains ที่อาจถูกติดตั้งบน
        your computer. เพื่อเซ็ต the project's PATH variable ใน Eclipse:

             i. คลิกขวา the demo project จาก the "Project Explorer" และเลือก "Properties".

(https://i.imgur.com/3zdgx9h.png)
(https://i.imgur.com/fPinBpb.png)

             ii. บนด้านซ้ายของ the pop-up window, เลือก "C/C++ build --> Environment" และจากนั้นคลิก the "Add..." button บนด้านขวาเพื่อเพิ่ม a new
                 variable.

(https://i.imgur.com/KR9Rh6a.png)

             iii. ป้อน "PATH" เป็น the variable name และเซ็ต the value ด้วย the path ไปยัง your toolchain และ build tools binaries folder.

(https://i.imgur.com/CHWJjFp.png)
Title: Re: FreeRTOS
Post by: tha on November 25, 2021, 11:54:03 AM
Import and Build a Demo Project:

สำคัญ! FreeRTOS Eclipse projects ใช้เส้นทางไปยัง source files ที่เกี่ยวข้องและดังนั้นจะไม่ build ถ้า your directory structure แตกต่างจาก the directory structure ที่ใช้ใน the official FreeRTOS zip-file releases. ต้องแน่ใจว่า the 'copy projects into workspace' check box ไม่ถูกติ๊กเมื่อนำเข้า the project ลงใน the Eclipse workspace.

     1. สตาร์ท Eclipse, จากนั้นเลือก an existing, หรือ create a new workspace เมื่อได้รับแจ้ง.

     2. เลือก "Import..." จาก the Eclipse "File" menu. The Import dialog box จะเปิดขึ้น.

     3. ใน the Import dialog box, เลือก "General -> Existing Project into Workspace". The Import Projects dialog box จะเปิดขึ้น.

(https://i.imgur.com/1KGKRLd.png)

     4. ใน the Import Projects dialog box, ค้นหาไปยังและเลือก the FreeRTOS/Demo/<YOUR_PROJECT> directory, และทำให้แน่ใจว่า the 'Copy
         projects into workspace' check box ไม่ถูกติ๊ก.

(https://i.imgur.com/ItJzH7H.png)

     5. ใน the "Projects" window of the Import Projects dialog box, เลือก the RTOSDemo project, และเลือก "finish".

     6. เลือก "Build all" จาก the Eclipse "Project" menu. ทำให้แน่ใจว่า the project บิวด์โดยไม่มี errors หรือ warnings ใดๆ.
Title: Re: FreeRTOS
Post by: tha on November 26, 2021, 05:11:23 AM
https://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html (https://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html)

FreeRTOS Windows Port
For Visual Studio or Eclipse and MingW
[RTOS Ports]

หมายเหตุ! The Visual Studio projects ใน the FreeRTOS distribution ถูกสร้างในช่วงเวลาแตกต่างกันและดังนั้นใช้ free versions of the Visual Studio for C/C++ tools ที่แตกต่างกัน. โดยทั่วไปคุณไม่จำเป็นต้องใช้ the exact same version of Visual Studio กับที่ใช้สร้าง the project, และ Visual Studio จะจัดให้มีคำแนะนำเกี่ยวกับการกำหนดเป้าหมายใหม่ a project ถ้ามี a version ที่ไม่ตรงกัน. อย่างไรก็ตาม ในบางครั้งที่มีปัญหาความเข้ากันได้แบบย้อนหลัง อาจจำเป็นต้องอัปเดตเวอร์ชัน Visual Studio ของคุณเป็นเวอร์ชันล่าสุดที่มีให้ใช้ประโยชน์.


Preamble - for beginners

ถ้าคุณยังใหม่กับ FreeRTOS ดังนั้นขอแนะนำให้สตาร์ทโดยการดู the Getting Started With Simple FreeRTOS Projects documentation (ซึ่งอธิบายวิธีการใช้ the FreeRTOS Windows port ด้วย), ก่อนการดูหน้านี้.
Title: Re: FreeRTOS
Post by: tha on November 26, 2021, 06:01:11 AM
Introduction

หน้านี้แสดง a Windows port layer สำหรับ FreeRTOS ที่ถูกพัฒนาและทดสอบโดยใช้ทั้ง Visual Studio Community Edition และ the Eclipse IDE for C และ C++ Developers ที่มี the MingW GCC based compiler. Demo projects ถูกจัดให้มีสำหรับทั้งสอง tool chains. ทั้งสอง tool chains ให้ฟรีอีกด้วย, แม้ว่า Visual Studio ต้องถูกลงทะเบียนถ้ามันถูกใช้สำหรับสิ่งใดๆนอกเหนือจาก evaluation purposes.

The port ถูกพัฒนาบน a dual core Intel processor running 32 bit Windows XP, และตอนนี้ถูกสานต่อบน a quad core Intel processor running 64-bit Windows 10 (แม้ว่า the project สร้าง a 32-bit binary).
Title: Re: FreeRTOS
Post by: tha on November 26, 2021, 06:05:20 AM
(https://i.imgur.com/uHqhGUH.png)
Title: Re: FreeRTOS
Post by: tha on November 26, 2021, 07:23:40 AM
Principle of Operation

Threads that run tasks

The Windows port layer สร้าง a low priority Windows thread สำหรับแต่ละ FreeRTOS task ที่สร้างโดย the FreeRTOS application. ทุก the low priority Windows threads ดังนั้นถูกเก็บใน the suspended state, นอกเหนือจาก the Windows thread ที่กำลังรัน the FreeRTOS task ที่เลือกโดย the FreeRTOS scheduler จะอยู่ใน the Running state. ด้วยวิธีนี้, the FreeRTOS scheduler จะเลือกซึ่ง low priority Windows thread เพื่อรันให้สอดคล้องกับ scheduling policy ของมัน. ทุก the other low priority windows threads ไม่สามารถรันเนื่องจากพวกมันถูกระงับ.

FreeRTOS ports ที่รันบน microcontrollers จะต้องกระทำ complex context switching เพื่อบันทึกและกู้คืน the microcontroller context (registers, etc.) เนื่องจาก tasks เข้าสู่และออกจาก the Running state. ในทางตรงกันข้าม, the Windows simulator layer เพียงแค่ต้องระงับและกลับมาทำงาน Windows threads ต่อเนื่องจาก the tasks ที่แสดงถึงการเข้าสู่และออกจาก the Running state. The real context switching ถูกออกจากไปยัง Windows.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 05:25:32 AM
Simulating the tick interrupt

The tick interrupt generation ถูกจำลองโดย a high priority Windows thread ที่จะยึด the low priority threads ที่กำลังรัน tasks เอาไว้ก่อนเป็นระยะๆ. The tick rate achievable ถูกจำกัดจำกัดโดย the Windows system clock, ซึ่งใน normal FreeRTOS terms จะช้าและมี a very low precision. ดังนั้นมันไม่สามารถเป็นได้ที่จะให้ true real time behaviour.

Simulating interrupt processing

Simulated interrupt processing ถูกดำเนินการโดย a second higher priority Windows thread ที่, เพราะว่า priority ของมัน, สามารถยึด(ระงับ) the low priority threads ที่กำลังรัน FreeRTOS tasks เอาไว้ก่อนได้อีกด้วย.  The thread ที่ simulates interrupt processing รอจนกระทั่งมันถูกแจ้งข่าวโดย thread อื่นใน the system ว่ามี an interrupt pending(ค้างอยู่).  ตัวอย่างเช่น the thread ที่จำลอง the generation of tick interrupts เซ็ต an interrupt pending bit, และจากนั้นแจ้งไปยัง the Windows thread ว่า simulates interrupts กำลังถูกประมวลผลว่า an interrupt กำลังค้างอยู่. The simulated interrupt processing thread ดังนั้นจะปฏิบัติและมองไปที่ทุก the possible interrupt pending bits - การดำเนินการ simulated interrupt processing ใดๆและการเคลียร์ interrupt pending bits ตามความจำเป็น.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 06:30:04 AM
Items to Note Before Using the Simulator

Windows programming by embedded engineers

ก่อนการใช้ the provided Windows projects, โปรดทราบว่าผม (ผู้เขียนของ the Windows simulator) เป็น an embedded programmer, ไม่ได้เป็น a Windows programmer. The implementation อาจจะไร้เดียงสา. ข้อเสนอแนะใด ๆ ที่ให้ไว้เกี่ยวกับการจัดให้มีใช้ปัจจุบันโดยผู้ที่มีความรู้มากขึ้นเกี่ยวกับการเขียนโปรแกรม Windows จะถูกรับอย่างสุดซึ้ง.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 07:23:54 AM
Deleting tasks on Windows

เมื่อ a FreeRTOS task ถูกลบ, the Windows port จะยุติ the thread ที่รับผิดชอบในการรัน the task. อย่างไรก็ตาม, ภายใต้ Windows, การยุติ a thread จาก thread อื่นจะไม่เป็นเหตุให้ the resources ที่เคยถูกใช้โดย the terminated thread ถูกรีเทิร์นไปยัง the system. นี้หมายความว่าที่ run time มีการจำกัดจำนวณครั้งที่ the FreeRTOS vTaskDelete() API function สามารถถูกเรียก. ขีดจำกัดสูงมาก (หลายพัน) แต่ป้องกันไม่ให้ the standard demo 'death' tasks จากการปฏิบัติอย่างไม่มีกำหนดก่อน the demo tasks สตาร์ทเพื่อรายงาน errors.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 08:15:18 AM
Load on the CPU of the host Windows machine

The load บน the CPU of the host Windows machine จะสูงมากในขณะที่ a FreeRTOS application กำลังถูกรันอยู่. การตอบสนองไม่ควรได้รับผลกระทบมากเกินไป เพราะว่ามีเพียง low priority threads เท่านั้นถูกใช้, แต่ the CPU core temperature จะพุ่งขึ้นและ the CPU cooling fans จะตอบสนองให้สอดคล้องกัน.

หากคุณกังวลเกี่ยวกับความสามารถของคอมพิวเตอร์ในการรับมือกับอุณหภูมิสูงที่สร้างขึ้น ดังนั้นฉันขอแนะนำให้ใช้ยูทิลิตี้ที่ให้ข้อมูลเกี่ยวกับทั้ง the current CPU core temperature, และใกล้แค่ไหนที่อุณหภูมิปัจจุบันจะถึง the maximum temperature rating ของ your particular CPU. โดยส่วนตัวฉันใช้ the free Core Temp utility เพื่อจุดประสงค์นี้
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 09:02:13 AM
Source Code Organisation

Eclipse and MingW (GCC)

The Eclipse project สำหรับ the FreeRTOS simulator demo application อยู่ใน the FreeRTOS/Demo/WIN32-MingW directory ของ the main FreeRTOS download. นี้จำเป็นต้องถูกนำเข้าลงใน the Eclipse workspace เพื่อบิ้วด์ the project.

Visual Studio

The Visual Studio solution สำหรับ the FreeRTOS simulator demo application ถูกเรียกว่า WIN32.sln และอยู่ใน the FreeRTOS/Demo/WIN32-MSVN directory of the main FreeRTOS download.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 09:38:49 AM
Using the Eclipse and MingW (GCC) Demo

Obtaining the compiler

The MingW compilation tools ไม่ถูกรวมเป็นส่วนหนึ่งของ the Eclipse distribution และต้องถูกดาวน์โหลดแยกต่างหาก.

Importing the FreeRTOS simulator project into an Eclipse workspace

เพื่อนำเข้า the FreeRTOS simulator project ลงใน Eclipse:

     1. สตาร์ท the Eclipse IDE, และไปยัง the Eclipse Workbench.
     2. เลือกt 'Import' จาก the Eclipse 'File' menu. A dialogue box จะปรากฎ.
     3. ใน the dialogue box, เลือก 'General | Existing Projects Into Workspace'. dialogue box อื่นจะปรากฎที่ยอมให้คุณค้นหาไปยังและเลือก a root
         directory.
     4. เลือก FreeRTOS/Demo/WIN32-MingW เป็น the directory - นี้จะเปิดเผย a project ชื่อ RTOSDemo, ซึ่งเป็น the project ที่ควรถูกนำเข้า.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 10:14:53 AM
The Demo Application

Functionality

ค่าคงที่ mainCREATE_SIMPLE_BLINKY_DEMO_ONLY, ซึ่งถูก #defined ที่ส่วนบนของ main.c, ถูกใช้เพื่อสลับไปมาระหว่าง a simply Blinky style demo, และการทดสอบที่ครอบคลุมยิ่งขึ้นและ demo application, ตามที่อธิบายใน the next two sections.

Functionality when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1

ถ้า mainCREATE_SIMPLE_BLINKY_DEMO_ONLY ถูกเซ็ตเป็น 1 ดังนั้น main() จะเรียก main_blinky(), ซึ่งถูกจัดให้มีใช้ใน main_blinky.c.

main_blinky() สร้าง a very simple demo ที่รวมเอาสอง tasks และหนึ่ง queue ไว้. task หนึ่งจะส่งซ้ำๆ the value 100 ไปยังอีก task หนึ่งผ่านทาง the queue. The receiving task จะพิมพ์ออก a message แต่ละครั้งที่มันรับค่าบน the queue.

Functionality when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0

ถ้า mainCREATE_SIMPLE_BLINKY_DEMO_ONLY ถูกเซ็ตเป็น 0 ดังนั้น main() จะเรียก main_full(), ซึ่งถูกจัดให้มีใช้ใน main_full.c.

The demo ที่สร้างโดย main_full() นั้นครอบคลุมมาก. The tasks ที่สร้างประกอบด้วยส่วนหลักของ the standard demo tasks - ซึ่งไม่ดำเนินการฟังก์ชันเฉพาะใดๆ นอกเหนือจากการทดสอบ the port และสาธิตวิธีที่ the FreeRTOS API สามารถถูกใช้.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 10:41:03 AM
The 'check' task created by main_full()

The full demo สร้าง a 'check' task นอกเหนือจาก the standard demo tasks. นี้จะปฏิบัติเฉพาะทุกๆ (ที่จำลอง) 5 วินาที, แต่มี the highest priority เพื่อให้แน่ใจว่ามันจะได้รับ processing time.  หน้าที่หลักของมันคือตรวจสอบว่า the standard demo tasks ทั้งหมดยังคงทำงานอยู่.

The check task จะรักษา a status string ที่ส่งออกไปยัง the console แต่ละครั้งที่มันปฏิบัติ. ถ้า the standard demo tasks ทั้งหมดกำลังรันอยู่โดยไม่มี error ดังนั้น the string จะพิมพ์ออก "OK" และ the current tick count. ถ้า an error ถูกตรวจพบแล้วดังนั้น the string จะพิมพ์ออก a message ที่แสดงให้เห็นซึ่ง task ใดที่ the error ถูกรายงาน.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 11:05:26 AM
Viewing the console output

The eclipse project จะส่งออก strings ไปยัง an integrated console. เพื่อดู strings เหล่านี้ the "RTOSDemo.exe" console ต้องถูกเลือกโดยใช้ the drop down list ที่สามารถเข้าถึงได้จาก the little computer monitor icon speed button - ดังที่แสดงในภาพข้างล่าง.

The Visual Studio console output จะปรากฎใน a command prompt window.

(https://i.imgur.com/HkZT5Um.png)
                                                                เลือก the "RTOSDemo.exe" console ในระหว่าง an Eclipse debug session
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 11:31:35 AM
Defining and Using Simulated Interrupt Service Routines

Defining a handler for a simulated interrupt service routine

Interrupt service routines ต้องมีต้นแบบต่อไปนี้:

unsigned long ulInterruptName( void );

โดยที่ 'ulInterruptName' สามารถเป็นชื่อฟังก์ชันที่เหมาะสม

ถ้าการปฏิบัติ the routine ส่งผลให้เกิด a context switch ดังนั้น the interrupt function ต้องรีเทิร์น pdTRUE. มิฉะนั้น the interrupt function จะรีเทิร์น pdFALSE.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 11:55:24 AM
Installing a handler for a simulated interrupt service routine

Handlers สำหรับ simulated interrupt service routines สามารถถูกติดตั้งโดยใช้ the vPortSetInterruptHandler() function ซึ่งถูก defined ใน the Win32 port layer. นี้มีต้นแบบที่แสดงข้างล่าง:

void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) );

ulInterruptNumber ต้องเป็นค่าที่รวมอยู่ในช่วง 3 ถึง 31 และไม่ซ้ำกันภายใน the application (หมายถึงทั้งหมดของ 29 simulated interrupts สามารถถูก defined ใน application ใดๆ). ตัวเลขที่รวม 0 ถึง 2 ถูกใช้โดย the simulator เอง.

pvHandler ควรชี้ไปยัง the handler function สำหรับ the interrupt number ที่กำลังถูกติดตั้ง.
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 12:10:02 PM
Triggering a simulated interrupt service routine

Interrupts สามารถถูกเซ็ต pending(ให้ค้างไว้) และ, ถ้าเหมาะสม, ปฏิบัติโดยการเรียก the vPortGenerateSimulatedInterrupt() function, ซึ่งถูก defined เป็นส่วนหนึ่งของ the Win32 port layer อีกด้วย. มันมีต้นแบบที่แสดงข้างล่าง:

void vPortGenerateSimulatedInterrupt( unsigned long ulInterruptNumber );

ulInterruptNumber เป็นจำนวนของ the interrupt that ที่ถูกเซ็ตให้ค้างไว้, และตรงกันกับ the ulInterruptNumber parameter ของ vPortSetInterruptHandler().
Title: Re: FreeRTOS
Post by: tha on November 27, 2021, 12:28:08 PM
An example of installing and triggering an interrupt

The simulator เองใช้สาม interrupts, หนึ่งสำหรับ a task yield, หนึ่งสำหรับ the simulated tick, และหนึ่งสำหรับการยุติ a Windows thread ที่เคยกำลังปฏิบัติ a FreeRTOS task ที่ถูกลบไปแล้ว. ดังตัวอย่างง่ายๆ ที่แสดงด้านล่างคือ the code สำหรับ the yield interrupt.

The interrupt function ไม่มีอะไรนอกเหนือจากการร้องขอ a context switch, ดังนั้นเพียงแค่รีเทิร์น pdTRUE. มันถูกกำนดโดยใช้ the following code:

(https://i.imgur.com/KYtknT5.png)

The simulated interrupt handler function ดังนั้นถูกติดตั้งโดยใช้ the following call, โดยที่ portINTERRUPT_YIELD ถูกกำหนดเป็น 2:

vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );

นี้คือ the interrupt ที่ควรปฏิบัติเมื่อใดก็ตามที่ taskYIELD()/portYIELD() ถูกเรียก, ดังนั้น the Win32 port version of portYIELD() ถูก defined เป็น:

#define portYIELD() vPortGenerateSimulatedInterrupt( portINTERRUPT_YIELD )
Title: Re: FreeRTOS
Post by: tha on November 28, 2021, 06:14:49 AM
https://www.freertos.org/FreeRTOS-simulator-for-Linux.html?? (https://www.freertos.org/FreeRTOS-simulator-for-Linux.html??)

Posix/Linux Simulator Demo for FreeRTOS
using GCC
[RTOS Ports]

The FreeRTOS port ที่บันทึกไว้ในหน้านี้ช่วยให้ FreeRTOS ทำงานบน Linux ได้เช่นเดียวกับ the FreeRTOS Windows port (มักเรียกว่า the FreeRTOS Windows simulator) อนุญาตให้ FreeRTOS ทำงานบน Windows ได้เสมอ พอร์ตนี้สนับสนุนโดย David Vrabel, และได้รับแรงบันดาลใจจากพอร์ต Linux 2008 ดั้งเดิมของ William Davy.

การจัดให้มีใช้ของ the port layer จะใช้ POSIX threading, ดังนั้น the port จึงถูกเรียกว่า the POSIX port ด้วย. มันไม่ควรสับสนกับ the FreeRTOS+POSIX library – เพราะมันทำสิ่งที่ตรงกันข้ามอย่างสิ้นเชิง.  FreeRTOS+POSIX จัดให้มี a POSIX threading wrapper สำหรับ the native FreeRTOS API ที่ช่วยให้ applications ที่เขียนโดยใช้ the POSIX API ทำงานบน FreeRTOS ได้, ในขณะที่ the Linux/POSIX FreeRTOS port ช่วยให้ FreeRTOS applications ทำงานบน POSIX operating systems ได้.

เช่นเดียวกับ the Windows port, the FreeRTOS Linux port จัดให้มีสภาพแวดล้อมที่สะดวกสบายซึ่งคุณสามารถทดลองกับ FreeRTOS และพัฒนา FreeRTOS applications ที่มีไว้สำหรับพอร์ตในภายหลังไปยัง embedded hardware จริงๆ - แต่ FreeRTOS applications ที่ใช้ the Linux port  จะไม่แสดงพฤติกรรมแบบ real-time
Title: Re: FreeRTOS
Post by: tha on November 28, 2021, 07:21:19 AM
(https://i.imgur.com/gJqNFCi.png)
Title: Re: FreeRTOS
Post by: tha on November 28, 2021, 07:35:02 AM
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.
Title: Re: FreeRTOS
Post by: tha on November 29, 2021, 06:21:26 AM
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.
Title: Re: FreeRTOS
Post by: tha on November 29, 2021, 08:12:44 AM
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 กำลังปฏิบัติ.
Title: Re: FreeRTOS
Post by: tha on November 29, 2021, 10:46:43 AM
Building the Posix/Linux Simulator Demos

เนื้อหาดูในลิ้งค์เอานะครับ  https://www.freertos.org/FreeRTOS-simulator-for-Linux.html?? (https://www.freertos.org/FreeRTOS-simulator-for-Linux.html??)
Title: Re: FreeRTOS
Post by: tha on November 30, 2021, 05:43:42 AM
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 แทบจะไม่เพิ่มขึ้นเลย.
Title: Re: FreeRTOS
Post by: tha on November 30, 2021, 07:37:29 AM
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.
Title: Re: FreeRTOS
Post by: tha on November 30, 2021, 09:27:04 AM
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.
Title: Re: FreeRTOS
Post by: tha on November 30, 2021, 10:17:42 AM
Common Problems and Solutions

Creating Threads

Problem

การสร้าง external threads ด้วย pthread_create (ตัวอย่างเช่น: การจำลอง interrupts) สามารถส่งผลต่อ FreeRTOS schedules tasks, เนื่องจาก the underlying RTOS port อาศัยการรับของ signals บางอย่างในการทำงาน. The created thread สามารถรับ the signal แทน ซึ่งเป็นเหตุให้ the scheduler ปล้น the thread จากจุดประสงค์ของมันเพื่อให้บริการกับ FreeRTOS specific tasks. นี้สามารถนำไปสู่การชนกันและแม้กระทั่งการหยุดชะงัก(ล็อคตาย).

Solution

เมื่อสร้าง a thread ด้วย pthread_create, signals ควรถูกบล็อกบน thread นั้นโดยการเพิ่มบางสิ่งต่อไปนี้ในเนื้อหาของ the thread:

sigset_t set;
sigfillset( &set );

pthread_sigmask( SIG_SETMASK, &set, NULL );

อีกทางหนึ่ง มีวิธีแก้ปัญหาที่ดีกว่า, แต่ non portable, แก้ปัญหาโดยการบล็อก signals ก่อนที่ the thread จะถูกสร้าง:

void * args;
sigset_t set;
pthread_t tid;
pthread_attr attr;

sigfillset( &set );
pthread_attr_init( &attr );
pthread_attr_setsigmask_np( &attr, &set );

pthread_create( &tid, &attr, start_routine, args );
Title: Re: FreeRTOS
Post by: tha on November 30, 2021, 11:07:03 AM
Creating new tasks

RTOS tasks ที่สร้างด้วย xTaskCreate() ส่งผ่าน their created stack ลงใน port.c เพื่อยอมให้ the pthread library ใช้ stack นั้นด้วย pthread_attr_setstack. A limitation มีอยู่สำหรับ the minimal stack size ที่สามารถถูกสร้าง. limitation นี้เป็น platform dependent และเท่ากันกับ PTHREAD_STACK_MIN. . ด้วยเหตุนี้, pthread_create() จะสร้าง stack ของตัวมันเองและจะไม่สนใจ the stack ที่ส่งผ่านโดย FreeRTOS. ไม่มีอะไรเลวร้ายจะเกิดขึ้นกับ the runtime system หากเกิดเหตุการณ์นี้ขึ้น, แต่ผู้ใช้บางรายที่ใช้ FreeRTOS debugging tools เพื่อแสดงข้อมูลสแต็กหรือตัวแปร FreeRTOS อื่น ๆ จะสังเกตเห็นความไม่สอดคล้องกันบางอย่าง.

วิธีแก้ปัญหา, ส่งผ่าน a stack ขนาด PTHREAD_STACK_MIN เสมอถ้า the desired stack น้อยกว่า PTHREAD_STACK_MIN เมื่อรันบน a Posix port.
Title: Re: FreeRTOS
Post by: tha on November 30, 2021, 11:51:38 AM
https://www.freertos.org/RTOS_ports.html (https://www.freertos.org/RTOS_ports.html)

FreeRTOS Kernel Ports

ไม่เห็นความตรงกันจริงๆสำหรับ your microcontroller part number และ compiler vendor choice? demos เหล่านี้สามารถถูกปรับให้เข้ากับ microcontroller ใดๆภายใน a supported microcontroller family. ดู the Creating a new FreeRTOS application และ Adapting a FreeRTOS Demo documentation pages. เนื่องจาก IDEs จำนวนมากตอนนี้อิงตาม Eclipse, ดูหน้าที่อธิบายวิธีการใช้ virtual and linked paths ใน the Eclipse project explorer เพื่อให้แน่ใจว่าคุณไม่จำเป็ต้องก๊อปปี้y the RTOS source files ลงใน an Eclipse project directory.

FreeRTOS ports ถูกจัดประเภทเป็นอย่างใดอย่างหนึ่ง ถูกรองรับอย่างเป็นทางการ, หรือที่มีส่วนช่วย. The Official and Contributed Definitions page อธิบายหมวดหมู่และเหตุผลในการสร้างความแตกต่าง หน้านี้แสดงรายการพอร์ต RTOS อย่างเป็นทางการเท่านั้น:

(ตารางดูในลิ้งค์เอานะ สั้นๆแปลกันเอาเองนะครับ)
Title: Re: FreeRTOS
Post by: tha on November 30, 2021, 12:21:40 PM
https://www.freertos.org/a00090.html (https://www.freertos.org/a00090.html)

Supported Demos
[Supported Devices]

ไม่เห็นความตรงกันจริงๆสำหรับ your microcontroller part number และ compiler vendor choice? demos เหล่านี้สามารถถูกปรับให้เข้ากับ microcontroller ใดๆภายใน a supported microcontroller family. ดู the Creating a new FreeRTOS application และ Adapting a FreeRTOS Demo documentation pages.

The 'Officially Supported' and 'Contributed' FreeRTOS Code page จัดให้มีคำอธิบายโดยละเอียดเกี่ยวกับความแตกต่างระหว่างที่รองรับอย่างเป็นทางการ, หรือที่มีส่วนช่วย FreeRTOS ports. Officially supported FreeRTOS demos ถูกจัดให้มีสำหรับ target microcontrollers จาก the following manufacturers:

(สั้นๆเลือกแปลเอาเองนะ บางตัวก็ไม่ได้ใช้ก็เลือกดูที่ใช้กันเอาเองนะครับ)
Title: Re: FreeRTOS
Post by: tha on December 01, 2021, 06:22:23 AM
https://www.freertos.org/a00102.html (https://www.freertos.org/a00102.html)

FreeRTOS Demo Applications

Introduction

The RTOS source code download รวม a demonstration project สำหรับแต่ละ port. The sample projects ถูกกำหนดค่าล่วงหน้าเพื่อปฏิบัติบน the single board computer หรือ prototyping board ที่ใช้ในระหว่าง the port development. แต่ละควร build ได้โดยตรงตามที่ดาวน์โหลดมาโดยไม่มี warnings หรือ errors ใดๆ. ชุดของ hardware independent starter functions ถูกจัดให้มีอีกด้วย.

The demonstration projects ถูกจัดให้มีดังนี้:

     1. An aid to learning how to use FreeRTOS - แต่ละ source file แสดงให้เห็นถึงส่วนประกอบของ the RTOS.

     2. A preconfigured starting point for new applications - เพื่อให้แน่ใจว่า the correct development tool setup (compiler switches,
         debugger format, etc) ขอแนะนำว่า new applications ถูกสร้างโดย modifying the existing demo projects. เมื่อคุณมี the demo application
         กำลังรัน, ให้ถอด the demo functions and source files ออกทีละน้อยและแทนที่พวกมันด้วย application code ของตัวคุณเอง.

Locating a demo application

Every demo application ที่อยู่ใน a subdirectory ของ the FreeRTOS/Demo directory. ชื่อของแต่ละ subdirectory ดังกล่าวอธิบายการกำหนดค่าของ the demo application ที่มันบรรจุ. โปรดดู the FreeRTOS source code organization page สำหรับการอธิบายโดยละเอียดของ the FreeRTOS directory structure, และ the quick start guide สำหรับข้อมูลที่เป็นประโยชน์เพิ่มเติม.

Demo specific documentation

เว็บไซต์นี้บรรจุ a documentation page สำรหรับแต่ละ demo application ที่รวมใน the FreeRTOS download. หน้าเหล่านี้บรรจุหน้าข้อมูลที่มีค่าและประหยัดเวลา เช่น วิธีการตั้งค่าฮาร์ดแวร์และวิธีการบิวด์ the demo. การคลิกชื่อผู้ขายจะนำคุณไปยังรายการหน้าเอกสารเฉพาะสำหรับผู้ขายรายนั้น.
Title: Re: FreeRTOS
Post by: tha on December 01, 2021, 08:35:14 AM
The structure of a demo application

แต่ละ demo application สร้างชุดของ demo real time tasks และ/หรือ co-routines - ซึ่งส่วนใหญ่ไม่ได้เฉพาะเจาะจงไปยัง demo ใด demo หนึ่ง แต่จะทั่วไปถึงหลาย demo. tasks เหล่านี้ถูกสร้างภายใน main(), ซึ่งในทางกลับกันถูก defined ภายใน main.c. ตัวอย่างเช่น, the main() function สำหรับ the Luminary Micro LM3S811 GCC demo ถูกบรรจุไว้ภายใน FreeRTOS/Demo/CORTEX_LM3S811_GCC/main.c.

demos applications สร้าง a 'check' task ในรูปแบบใดรูปแบบหนึ่งอีกด้วย. The 'check' task จะดำเนินการไม่บ่อยนัก (โดยทั่วไปคือทุกๆ 3 หรือ 5 วินาที) แต่มี a high priority ดังนั้นถูกรับประกันได้ว่าจะได้รับ processor time. ความรับผิดชอบหลักของมันคือการตรวจสอบว่า tasks อื่นๆ ทั้งหมดยังคงทำงานตามที่คาดไว้, และไม่มี errors ถูกตรวจพบ. The check task จะรายงาน the system status อย่างใดอย่างหนึ่ง บน an LCD (ถ้ามี) หรือโดยการสลับ an LED.

A typical main() function จะมี the following structure:

(https://i.imgur.com/CkcncyN.png)
Title: Re: FreeRTOS
Post by: tha on December 01, 2021, 09:25:34 AM
Partest.c - Accessing LEDs

demo applications จำนวนมากมีไฟล์ที่ชื่อ partest.c (ชื่อนี้มาจากประวัติศาสตร์และสูญเสียความหมายไป แต่มาจาก 'parallel port test'). The file บรรจุ the interface functions สำหรับ setting LEDs, clearing LEDs และ toggling LEDs. มีการกล่าวถึงที่นี่ด้วยเหตุผลสองประการ: ประการแรกเนื่องจากฟังก์ชันของไฟล์ไม่ชัดเจนจากชื่อของมัน และประการที่สองเนื่องจากการให้เอาต์พุต LED ทำงานเป็นปกติขั้นตอนแรกเมื่อย้ายการสาธิตจากแพลตฟอร์มฮาร์ดแวร์หนึ่งไปยังอีกแพลตฟอร์มหนึ่ง
Title: Re: FreeRTOS
Post by: tha on December 01, 2021, 10:38:56 AM
Demo application files

The files ที่จัดให้มีใช้ the tasks ที่ถูกใช้โดยทุก the demo applications จะอยู่ใน the FreeRTOS/Demo/Common/Minimal directory. The files ใน the FreeRTOS/Demo/Common/Full directory ไม่ถูกใช้อีกต่อไป, แต่ยังคงถูกแจกจ่ายเพื่อให้แน่ใจว่าตัวอย่างเก่ามากๆยังคงสามารถถูกบิวด์.

ข้อมูลพื้นฐานเกี่ยวกับ the common demo task ถูกจัดให้มี, แต่โปรดทราบว่า the documentation มักจะล้าสมัยเนื่องจาก new common demo tasks ถูกสร้างขึ้นบ่อยครั้ง. Demo task files ที่ถูกเพิ่มมาเร็วๆนี้มีแนวโน้มที่จะถูกออกแบบให้เฉพาะเจาะจงสำหรับการตรวจสอบมากว่าเพียงสำหรับการสาธิต, ดังนั้นจึงอาจซับซ้อนได้.
Title: Re: FreeRTOS
Post by: tha on December 01, 2021, 11:35:40 AM
บางจุดที่ควรทราบ:

     •  ไม่ทุก the Demo/Common files ถูกใช้ในทุกๆ demonstration project. จำนวน files เท่าใดถูกใช้ขึ้นอยู่กับ processor resources.

     •  The demo projects มักจะใช้ the available RAM ทั้งหมดบน the target processor. นี้หมายความว่าคุณไม่สามารถเพิ่ม tasks เพิ่มเติมไปยัง the project
         โดยไม่มีประการแรกลบบางสิ่งออก! นี้ถูกต้องเป็นพิเศษสำหรับ the projects ที่กำหนดค่าให้รันบน the low end 8 and 16-bit processors.

     •  นอกเหนือจาก the standard demo projects, embedded web and FTP server projects ถูกจัดให้มี, ซึ่งจัดให้มีตัวอย่าง application ที่เน้นมากขึ้น.

     •  The standard demo project files ถูกจัดให้มีสำหรับจุดประสงค์ของการสาธิตการใช้ของ the RTOS kernel และไม่ได้มุ่งหมายเพื่อจัดให้มีการแก้ปัญหาที่เหมาะ
         สมที่สุด. นี้เป็นจริงอย่างยิ่งกับ comtest.c (ซึ่งใช้ an example UART driver), ซึ่งโดยทั่วไปถูกเขียนขึ้นโดยมีจุดประสงค์ของการเน้นย้ำ (และด้วยเหตุนี้จึงทำการ
         ทดสอบ) the RTOS kernel implementation แทนที่จะจัดให้มีตัวอย่างของการแก้ปัญหาที่เหมาะสมที่สุด.

The demo application ไม่ได้ปล่อยเป็นอิสระ resources ของมันทั้งหมดเมื่อมันออก, แม้ว่า the RTOS kernel จะทำ. นี้ถูกทำขึ้นอย่างหมดจดเพื่อลดบรรทัดของ code.
Title: Re: FreeRTOS
Post by: tha on December 01, 2021, 02:38:35 PM
https://www.freertos.org/simple-freertos-demos.html (https://www.freertos.org/simple-freertos-demos.html)

Getting Started with Simple FreeRTOS Projects

[โปรดดู the Quick Start Guide and the hardware independent starter functions ที่จัดให้มี.]

Simply Blinky Projects

The most recent(ล่าสุด) FreeRTOS demos สามารถถูกกำหนดค่าเพื่อ build อย่างใดอย่างหนึ่ง a simple blinky starter application, หรือ a comprehensive test and demo application:

     1. Simple blinky demos

         Blinky demos ถูกมุ่งหมายสำหรับผู้เริ่มต้น. โดยปกติแล้วพวกเขาจะสร้างเพียงสอง tasks, หนึ่ง queue, และบางที่ก็มี a software timer ด้วย. ฟังชั่นของพวก
         มันถูกบรรจุไว้ใน a single C source file ที่เรียกว่า main_blinky.c.

     2. Comprehensive demos

         Comprehensive demos จะสาธิตและทดสอบ FreeRTOS features จำนวนมาก, รวมเอา tasks, direct to task notifications, queues, semaphores,
         recursive semaphores, software timers, และอื่นๆ. Comprehensive demos สร้าง tasks จำนวนมากและใช้ API functions จำนวนมาก, ดังนั้นไม่ขอ
         แนะนำสำหรับผู้เริ่มต้น.

แต่ละ official FreeRTOS project มี documentation page ของตัวเอง. The documentation page อธิบายวิธีกำหนดค่า the project เพือ build อย่างใดอย่างหนึ่ง the simply blinky demo หรือ the comprehensive demo, และอธิบายพฤติกรรมของทั้งสองทางเลือก.

หมายเหตุ: A project ที่ถูกกำหนดค่าเพื่อบิวด์ a simply blinky demo จะยังคงบิวด์ทุก the source files ที่ใช้โดย the comprehensive demo, ถึงแม้ว่า the simply blinky functionality ถูกบรรจุไว้ภายใน the single main_blinky.c source file. อ้างอิงถึง the Source Code Organisation และ Creating a New FreeRTOS Project pages สำหรับคำอธิบายของ the RTOS และ demo application source files.
Title: Re: FreeRTOS
Post by: tha on December 01, 2021, 03:28:05 PM
Try It Now, Using the Windows Port

ยังไม่มีฮาร์ดแวร์? ไม่ต้องกังวล - คุณสามารถรัน a simple blinky demo ใน a Windows environment โดยใช้ free tools และ the FreeRTOS Windows port, ถึงแม้ว่า the FreeRTOS Windows port จะไม่แสดง true real time behaviour.

หากคุณเป็นมือใหม่ อย่าเพิ่งอ่าน the main documentation page for the Windows demo yet, หรือให้ความสนใจกับ the comprehensive demo ที่สามารถถูกบิวด์โดย the same project (the comprehensive demo for the Windows port จะครอบคลุมมากเนื่องจากมันถูกใช้เป็น a development and test platform!) - เพียงแค่เริ่มต้นโดยการใช้คำแนะนำง่ายๆข้างล่างนี้:

     1. หากคุณยังไม่ได้ติดตั้ง ให้ดาวน์โหลดและติดตั้ง Microsoft Visual Studio เวอร์ชันฟรี

     2. หากคุณยังไม่ได้ดำเนินการดังกล่าว ให้ดาวน์โหลดและ unzip the official FreeRTOS distribution.

     3. สตาร์ท Visual Studio, จากนั้นใช้ the File|Open|Project/Solution menu item เพื่อเปิด the Win32.sln solution file, ซึ่งอยู่ใน the
         FreeRTOS/Demo/WIN32-MSVC directory of the official FreeRTOS distribution.

     4. ค้นหาการกำหนดของ mainCREATE_SIMPLE_BLINKY_DEMO_ONLY ที่ส่วนบนของ main.c, และทำให้แน่ใจว่ามันถูกเซ็ตเป็น 1.

     5. อ่าน the comments ที่ส่วนบนของ main_blinky.c, ก่อนการคอมไพล์และจากนั้นอย่างใดอย่างหนึ่ง debugging หรือ running the application.

(รูปให้ดูในลิ้งค์เอานะครับ)
Title: Re: FreeRTOS
Post by: tha on December 02, 2021, 05:23:23 AM
https://www.freertos.org/Hardware-independent-RTOS-example.html (https://www.freertos.org/Hardware-independent-RTOS-example.html)

Hardware independent FreeRTOS example

[โปรดดู the Quick Start Guide, and the Getting Started With Simple FreeRTOS Projects documentation page.]

Introduction

The RTOS download รวม example projects จำนวนมากที่ถูกกำหนดค่าไว้ล่วงหน้าเพื่อทำงาน "นอกกรอบ". ทั้งนี้เพื่อให้แน่ใจว่าผู้ใช้จะสามารถได้รับ a working RTOS project รันบน real hardware  ในเวลาที่สั้นที่สุด. อย่างไรก็ตาม เป็นไปไม่ได้ที่จะสนับสนุน microcontroller, compiler and development board ทุกชุดรวมกัน, และเรามักถูกขอให้ยกตัวอย่างที่ไม่อิงกับตัวแปรเหล่านี้. The code ที่จัดให้มีในหน้านี้ถูกจัดให้มีเพื่อวัตถุประสงค์นั้น. ความเรียบง่ายของ the code หมายถึงมันบริการเป็น a good starter project สำหรับผู้ที่ยังไม่คุ้นเคยกับ FreeRTOS อีกด้วย.

The source code ไม่เข้าถึง hardware specific IO ใดๆ. ตัวอย่างเช่น, มันเพียงเพิ่มค่าตัวแปรแทนที่จะพยายามสลับปิดเปิด an LED. อย่างไรก็ตาม, ไม่เหมือน the pre-packaged examples, มันต้องการให้ผู้ใช้สร้าง their own compiler project. ที่เกี่ยวข้องกับ:

     1. สตาร์ทด้วย a working (ไม่ใช่ RTOS) project เพื่อให้แน่ใจว่า the correct start up files และ linker scripts กำลังถูกใช้.

     2. รวม the correct RTOS source files. ใช้ existing official RTOS demo projects เป็นตัวอ้างอิง.

     3. การรวม the correct compiler include paths, ซึ่งต้องรวม the RTOS kernel header files (FreeRTOS/Source/include ใน the RTOS download)
         และ the portable layer header file (FreeRTOS/Source/[compiler]/[architecture]/portmacro.h ใน the RTOS download)

     4. การกำหนด a FreeRTOSConfig.h file - หรืออย่างน้อย - การก๊อปปี้ a FreeRTOSConfig.h จาก an official RTOS demo ที่ตรงกันอย่างใกล้เคียงกับ the
         project ที่กำลังถูกสร้าง.

ดู the Creating a New FreeRTOS Project documentation สำหรับข้อมูลเพิ่มเติม.
Title: Re: FreeRTOS
Post by: tha on December 02, 2021, 07:17:58 AM
Hardware Independent RTOS Demo Functionality

นี่คือคำอธิบายของฟังก์ชันของตัวอย่าง:

The main() Function

main() สร้าง the tasks และ software timers ทีอธิบายใน section นี้, ก่อนการสตาร์ท the scheduler.

The Queue Send task

The queue send task ถูกจัดให้มีใช้โดย the prvQueueSendTask() function. The task ใช้ the FreeRTOS vTaskDelayUntil() and xQueueSend() API functions เพื่อส่งตัวเลข 100 ไปบน a queue เป็นระยะๆ. คาบเวลาถูกเซ็ตเป็น 200ms. ดู the comments ใน the function สำหรับรายละเอียดเพิ่มเติม.
Title: Re: FreeRTOS
Post by: tha on December 02, 2021, 09:01:11 AM
The Queue Receive task

The queue receive task ถูกจัดให้มีใช้โดย the prvQueueReceiveTask() function. The task ใช้ the FreeRTOS xQueueReceive() API function เพื่อรับค่าจาก a queue. The values ที่รับคือเหล่านั้นที่ส่งโดย the queue send task. The queue receive task เพิ่ม the ulCountOfItemsReceivedOnQueue variable แต่ละครั้งที่มันรับค่า 100. ดังนั้น, ตามที่ค่าถูกส่งไปยัง the queue ทุกๆ 200ms, ค่าของ ulCountOfItemsReceivedOnQueue จะเพิ่มขึ้น 5 ทุกๆวินาที.
Title: Re: FreeRTOS
Post by: tha on December 02, 2021, 09:25:30 AM
An example software timer

A software timer ถูกสร้างขึ้นด้วย an auto reloading คาบเวลาที่ 1000ms. The timer's callback function เพิ่ม the ulCountOfTimerCallbackExecutions variable แต่ละครั้งที่มันถูกเรียก. ดังนั้นค่าของ ulCountOfTimerCallbackExecutions จะนับเป็นวินาที.
Title: Re: FreeRTOS
Post by: tha on December 02, 2021, 09:44:51 AM
The FreeRTOS RTOS tick hook (or callback) function

The tick hook function ปฏิบัติใน the context of the FreeRTOS tick interrupt. The function 'ให้' a semaphore ทุกๆ 500th ครั้งที่มันปฏิบัติ. The semaphore ถูกใช้เพื่อซิงโครไนส์กับ the event semaphore task, ซึ่งจะถูกอธิบายถัดไป. ในตัวอย่างนี้, the tick interrupt ถูกใช้ในการเลือก an interrupt ที่สร้างโดย a peripheral เพื่อให้แน่ใจว่า the hardware neutrality ถูกรักษา.
Title: Re: FreeRTOS
Post by: tha on December 02, 2021, 10:14:27 AM
The event semaphore task

The event semaphore task ใช้ the FreeRTOS xSemaphoreTake() API function เพื่อรอสำหรับ the semaphore ที่ถูกให้โดย the RTOS tick hook function. The task เพิ่ม the ulCountOfReceivedSemaphores variable แต่ละครั้งที่ the semaphore ถูกรับ. ตามที่ the semaphore ถูกให้ทุกๆ 500ms (ทึกทักเอาว่า a tick frequency ที่ 1KHz), ค่าของ ulCountOfReceivedSemaphores จะเพิ่มขึ้น 2 ในแต่ละวินาที.

NOTE: A semaphore ถูกใช้สำหรับวัตถุประสงค์เป็นตัวอย่าง. ใน a real application มันควรใช้ a direct to task notification ดีกว่า, ซึ่งจะเร็วกว่าและใช้ RAM น้อยลง.
Title: Re: FreeRTOS
Post by: tha on December 02, 2021, 10:41:06 AM
The idle hook (or callback) function

The idle hook function สอบถามจำนวนของ FreeRTOS heap space ที่เป็นอิสระพร้อมใช้งาน. ดู vApplicationIdleHook() ใน the code.

The malloc failed and stack overflow hook (or callback) functions

สอง hook functions นี้ถูกจัดให้มีเป็นตัวอย่าง, แต่ไม่ได้บรรจุฟังชันใดๆ.

Source Code

ค้นหาสำหรับ TODO ใน the source files เพื่อวางในที่ๆการแก้ไขอาจถูกต้องการ.
(ตัวอย่างโค้ดดูในลิ้งค์เอานะครับ)
https://www.freertos.org/Hardware-independent-RTOS-example.html (https://www.freertos.org/Hardware-independent-RTOS-example.html)
Title: Re: FreeRTOS
Post by: tha on December 03, 2021, 06:53:05 AM
https://www.freertos.org/porting-a-freertos-demo-to-different-hardware.html (https://www.freertos.org/porting-a-freertos-demo-to-different-hardware.html)

Modifying a FreeRTOS Demo
เพื่อใช้ compiler อื่นหรือรันบน hardware อื่น

เพจนี้เกี่ยวกับอะไร

[โปรดดู "Creating a new FreeRTOS project"]

FreeRTOS ได้รวม demo applications จำนวนมากไว้แล้ว - แต่ละตัวมีเป้าหมายที่:

     1. A specific microcontroller.
     2. A specific development tool (compiler, debugger, etc.).
     3. A specific hardware platform (prototyping or evaluation board).

เหล่านี้ถูกทำเป็นเอกสารภายใต้ 'Supported Devices' ในกรอบเมนูทางด้านซ้าย.

ขออภัย เป็นไปไม่ได้ที่จะจัดให้มี a demo project สำหรับทุกชุดของ microcontroller, compiler and evaluation board - ดังนั้นอาจเป็นไปได้ว่า a demo application ไม่มีที่ตรงกันกับการเซ็ตอัฟที่ต้องการของคุณจริงๆ. หน้านี้จัดทำเอกสารวิธีที่ demo applications ที่มีอยู่สามารถถูกแก้ไขหรือถูกรวมเพื่อให้ตรงดีขึ้นกับ the setup ที่คุณต้องการ.

โดยทั่วไปเป็นงานง่ายที่เอา an existing demo สำหรับ evaluation board หนึ่งและแก้ไขมันไปรันบนอีกบอร์ดหนึ่ง - และซับซ้อนกว่าเล็กน้อยเท่านั้นในการเอา a demo สำหรับ compiler หนึ่งและแก้ไขมันไปใช้กับอีก compiler หนึ่ง. หน้านี้จัดให้มีคำแนะนำเกี่ยวกับเหล่านี้และ porting type activities ที่คล้ายกัน. มันไม่ง่ายนัก อย่างไรก็ตาม ในการใช้ a FreeRTOS port และแปลงมันเพื่อรันบน processor core architecture ที่แตกต่างจากเดิมโดยสิ้นเชิง, และยังไม่ได้ถูกรองรับ.  หน้านี้จึงไม่ครอบคุมหัวข้อของการสร้าง RTOS ports ใหม่อย่างสมบูรณ์, นอกจากนี้ยังมีหน้าแยกต่างหากซึ่งให้คำแนะนำเกี่ยวกับวิธีที่การพัฒนาดังกล่าวถูกเข้าใกล้.
Title: Re: FreeRTOS
Post by: tha on December 04, 2021, 06:25:32 AM
Converting a Demo to Use a Different Evaluation Board

subsection นี้ทำเอกสารขั้นตอนที่จำเป็นเพื่อแปลง an existing demo application จาก prototyping board หนึ่งไปยังอีกบอร์ดหนึ่ง, โดยไม่มีการเปลี่ยนอย่างใดอย่างหนึ่ง the microcontroller หรือ compiler ที่ใช้. ดังตัวอย่าง, คำแนะนำเหล่านี้จะถูกใช้เพื่อแปลง the IAR SAM7S demo เป้าหมายที่ the SAM7S-EK hardware เพื่อแทนด้วยเป้าหมาย the Olimex SAM7-P64 prototyping board.

ตรวจสอบให้แน่ใจว่าแต่ละขั้นตอนเสร็จสมบูรณ์ก่อนที่จะย้ายไปขั้นตอนถัดไป:

     1. Initial compilation:

        An existing demo application ควรถูกใช้เป็น a starting point สำหรับ the conversion exercise, ดังนั้นก่อนอื่นเช็คว่าคุณสามารถคอมไพล์ the existing
        demo application ได้สำเร็จจริงๆตามที่ดาวน์โหลด - ก่อนทำการแก้ไขใดๆ. ในกรณีส่วนใหญ่ the demo application ควรคอมไพล์โดยไม่มี errors หรือ
        warnings ใดๆ.

        เว็บไซ้ท์นี้บรรจุ a documentation page for each demo application รวมอยู่ใน the FreeRTOS download. โปรดแน่ใจว่าได้อ่านสิ่งนี้อย่างครบถ้วน Build
        instructions ถูกรวมอยู่ด้วย.

     2. Modifying the LED IO:

        LEDs จัดให้มีวิธีที่ง่ายที่สุดของการได้รับการป้อนกลับที่มองเห็นว่า the demo application กำลังรันอยู่, ดังนั้นจึงเป็นประโยชน์ที่จะทำให้ LED บนแพลตฟอร์ม
        ฮาร์ดแวร์ใหม่ทำงานได้โดยเร็วที่สุด

        ไม่น่าเป็นไปได้ที่ the hardware platform ที่ the demo ที่ถูกพอร์ทมี LEDs บน IO ports เดียวกันกับ the hardware platform ที่ the demo ถูกพัฒนา
        แล้ว - ดังนั้นแก้ไขเล็กน้อยบางอย่างถูกต้องการ.

        The function vParTestInitialise() ภายใน partest.c บรรจุ the IO port mode and direction configuration. The function prvSetupHardware()
        ภายใน main.c บรรจุ more generic hardware configuration (ตัวอย่างเช่น, เปิดการใช้งาน the clock ไปยัง the IO peripheral) และอาจต้องการการ
        แก้ไขบางอย่างอีกด้วย, ขึ้นอยู่กับ the port ที่ถูกใช้.

        ทำการเปลี่ยนแปลงที่จำเป็นกับฟังก์ชันทั้งสองที่ไฮไลต์ในย่อหน้าด้านบน จากนั้นเขียนโปรแกรมง่ายๆ เพื่อตรวจสอบว่าเอาต์พุต LED ทำงานหรือไม่ โปรแกรมง่ายๆ นี้ไม่
        จำเป็นต้องใช้ FreeRTOS - สิ่งที่น่าสนใจทั้งหมดในขั้นตอนนี้คือการทำให้แน่ใจว่าไฟ LED ทำงาน - ดังนั้นสำหรับตอนนี้เอาออก the existing main() function
        และแทนมันด้วยบางสิ่งที่คล้ายกับตัวอย่างต่อไปนี้:

(https://i.imgur.com/zIYecQy.png)
Title: Re: FreeRTOS
Post by: tha on December 04, 2021, 07:39:18 AM
    3. Introducing the RTOS scheduler:

        เมื่อ the LEDs ถูกทราบว่ากำลังทำงาน the dummy main() function สามารถถูกเอาออก, และ the original main() function เอากลับคืน.

        ขอแนะนำให้เริ่มต้นด้วย the simplest multitasking application เท่าที่เป็นได้. The standard 'flash test' tasks มักจะถูกใช้ตอนเริ่มต้นเป็น a
        multitasking เทียบได้กับ a 'hello world' type application.

        The standard 'flash test' tasks คือชุดของ 3 very simple tasks - แต่ละ task จะสลับ a single LED ที่ความถี่คงที่, ด้วยแต่ละ task จะใช้ a different
        frequency. tasks เหล่านี้ถูกรวมใน the demo applications เกือบทั้งหมด, และถูกสตาร์ทภายใน main() โดยการเรียกไปยัง the function
        vStartLEDFlashTasks() (หรือ vStartFlashCoRoutines() หาก the co-routine version ถูกใช้แทน). ถ้า the main() function of the demo ที่คุณ
        กำลังใช้ไม่เรียก vStartLEDFlashTasks() (หืออีกทางหนึ่ง vStartFlashCoRoutines()) ดังนั้นเพิ่มอย่างง่ายๆ the file
        FreeRTOS/Demo/Common/Minimal/Flash.c ไปยัง your build และเพิ่มการเรียกถึง vStartLEDFlashTasks() แบบแมนน้วล.

        เอาออกทุกๆ function ที่ถูกใช้ตอนเริ่มต้นหนึ่งหรือมากว่า demo tasks, นอกเหนือจากการเรียกไปยัง vStartLEDFlashTasks(). มีแนวโน้มว่า main() จะเรียกใช้
        ฟังก์ชันสามฟังก์ชันเท่านั้น:  prvSetupHardware(), vStartLEDFlashTasks(), and vTaskStartScheduler(). For example (ตาม the typical main() 
        ที่แนะนำก่อนหน้านี้):

(https://i.imgur.com/axg34s1.png)

        แอปพลิเคชั่นที่เรียบง่ายมากนี้ทำงานอย่างถูกต้องหากไฟ LED 0 ถึง 2 (รวม) อยู่ภายใต้การควบคุมของ the 'flash' tasks และแต่ละรายการมีการสลับที่ความถี่คงที่
        แต่ต่างกัน
Title: Re: FreeRTOS
Post by: tha on December 04, 2021, 09:24:33 AM
     4. Finishing off:

         เมื่อ the simple flash demo ถูกปฏิบัติคุณสามารถกู้คืน the full demo application ที่มี the demo tasks ทั้งหมดถูกสร้าง, หรืออีกทางหนึ่ง, เริ่มต้นสร้าง
         your own application tasks.

         ข้อควรจำ:

              •  ถ้า the demo application แต่เดิมไม่มีการเรียกไปยัง vTaskCreateFlashTasks(), และการเรียกไปยังฟังชั่นนี้ถูกเพิ่มแล้วแบบ manually, ดังนั้นการ
                  เรียกควรถูกเอาออกอีกครั้ง. ด้วยเหตุผลสองประการ อย่างแรก the flash tasks อาจใช้ LED outputs ที่ถูกใช้ที่อื่นแล้วภายใน the demo, และอย่างที่สอง
                  the full demo อาจใช้ the available RAM ทั้งหมดแล้ว, หมายความว่าไม่มีที่ว่างสำหรับ tasks เพิ่มเติมถูกสร้าง.

              •  The standard 'com test' tasks (ถ้าถูกรวมใน the demo) จะใช้ประโยชน์ the microcontrollers UART peripherals ตัวหนึ่ง. ตรวจสอบว่า the
                  UART ที่ใช้ใช้ได้สำหรับ the hardware ที่ซึ่งคุณได้พอร์ท the demo.

              •  ไม่น่าเป็นไปได้ที่ peripherals อย่างเช่น LCDs จะทำงานโดยไม่มีการปรับเปลี่ยน เพื่อพิจารณาสำหรับความแตกต่าง hardware หรือ interface ใดๆ.
Title: Re: FreeRTOS
Post by: tha on December 05, 2021, 06:24:16 AM
Combining or Modifying Existing Demo Projects

ส่วนย่อยนี้เน้นรายละเอียดที่ต้องการพิจารณาเพื่ออย่างใดอย่างหนึ่ง ปรับเปลี่ยนโครงการที่มีอยู่ หรือรวมโครงการที่มีอยู่สองโครงการ, ทั้งคู่มีเป้าหมายของการสร้าง a project เฉพาะเจาะจงตามความต้องการของคุณ. ตัวอย่างเช่น, คุณอาจอยากจะสร้าง an STR9 demo project ที่ใช้ the GCC compiler. ในขณะที่ the FreeRTOS download อาจไม่ (ในขณะนั้นหรือการเขียน) รวม a GCC STR9 demo, มันรวม an IAR STR9 demo, และ a GCC STR75x demo.  ข้อมูลที่จำเป็นในการสร้าง the STR9 GCC project สามารถถูกรวบรวมได้จากทั้งสอง existing project. มีสองวิธีนี้สามารถถูกทำ:

     1. เอา an existing demo project ที่ใช้ the correct compiler แต่กำหนดเป้าหมายเป็น microcontroller อื่น, และกำหนดเป้าหมายใหม่นี้ไปยัง
         microcontroller ที่ต้องการ.
     2. สร้าง a new project โดยใช้ compiler ที่คุณเลือก. เมื่อตัวเลือกนี้ถูกเอา an existing demo project สามารถถูกใช้เป็นแนวทางที่ files และการเซ็ตถูก
         ต้องการ, ถึงแม้ว่า the existing project จะใช้ compiler ที่ต่างกัน.
Title: Re: FreeRTOS
Post by: tha on December 05, 2021, 10:09:24 AM
หมายเหตุต่อไปนี้เน้นข้อมูลที่ต้องการการพิจารณาว่าวิธีใดถูกใช้:

     •  การระบุ the FreeRTOS kernel files ที่เฉพาะเจาะจงไปยัง the microcontroller ที่กำลังถูกใช้ :

         The FreeRTOS source code organization page จัดให้มีข้อมูลทั้งหมดที่ถูกต้องการเพื่อเข้าใจ the FreeRTOS directory structure.

         ส่วนใหญ่ (ถ้าไม่ใช่ทั้งหมด) the code ที่เฉพาะเจาะจงไปยัง a single port ถูกบรรจุใน a file ที่เรียกว่า
         FreeRTOS/source/portable/[compiler]/[microcontroller]/port.c และ an accompanying header file ที่เรียกว่า
         FreeRTOS/source/portable/[compiler]/[microcontroller]/portmacro.h, โดยที่ [compiler] คือชื่อของคอมไพเลอร์ที่กำลังถูกใช้งาน และ
         [microcontroller] คือชื่อตระกูลไมโครคอนโทรลเลอร์ที่กำลังถูกใช้

         สำหรับคอมไพเลอร์บางตัว the port.c and portmacro.h files คือทั้งหมดที่ถูกต้องการ. สำหรับตัวอื่น ๆ (เหล่านั้นที่มีคุณสมบัติที่ยืดหยุ่นน้อยกว่า) an
         assembler file ถูกต้องการด้วย. นี้จะถูกเรียกว่า portasm.s or portasm.asm.

         สุดท้าย, และเฉพาะเจาะจงไปยัง ARM7 GCC ports เท่านั้น, จะมี a file ที่เรียกว่า portISR.c. portISR.c ถูกใช้อีกด้วยเพือแยกออกจาก port.c ที่ code ซึ่ง
         ต้องถูกคอมไพล์เป็น ARM mode เสมอ - the code ที่ยังคงอยู่ใน port.c ดังนั้นสามารถถูกคอมไพล์เป็นอย่างใดอย่างหนึ่ง ARM หรือ THUMB mode.
Title: Re: FreeRTOS
Post by: tha on December 05, 2021, 11:01:19 AM
     •  การระบุ files ที่เฉพาะเจาะจงไปยัง the compiler ที่กำลังถูกใช้:

        Compilers ที่เป้าหมายเป็น embedded systems จัดให้มีส่วนขยายบางอย่างสำหรับ the C language. ตัวอย่างเช่น, a special keyword อาจมีอยู่ที่ถูกใช้เพื่อ
        ระบุว่า a particular function ควรถูกคอมไพล์เป็น an interrupt handler.

        ส่วนขยายของ the C language, โดยการกำหนด, อยู่นอก the C standard ดังนั้นจึงแตกต่างจาก compiler หนึ่งถึง compiler หนึ่ง. The FreeRTOS files ที่
        บรรจุ non-standard syntax ดังกล่าวจะเป็นไฟล์เหล่านั้นภายใน the FreeRTOS/source/portable directory tree (ตามที่ไฮไลต์ด้านบน).  นอกจากนี้, some
        demo applications จะติดตั้ง interrupt handlers ซึ่งไม่ได้เป็นส่วนหนึ่งของ  FreeRTOS เอง. การกำหนดของ interrupt handlers ดังกล่าวและวิธีการติดตั้ง
        the interrupt handler อาจเป็นคำเฉพาะของคอมไพเลอร์ด้วย.
Title: Re: FreeRTOS
Post by: tha on December 06, 2021, 06:17:26 AM
     •  Low level files:

        The C startup file and linker script เป็นโดยเฉพาะ processor and compiler โดยทั่วไป. ไม่ควรสร้าง from เหล่านี้จากเริ่มต้น - แทนด้วยการมองหาผ่าน
        ทาง the existing FreeRTOS demo projects สำหรับ a file ที่เป็นตัวเลือกที่เหมาะสมสำหรับการปรับเปลี่ยน.

        ใช้ความระมัดระวังเป็นพิเศษกับ ARM7 C startup files. เหล่านี้ต้องกำหนดอย่างใดอย่างหนึ่ง the IRQ handler ถึง vector โดยตรงไปยัง the interrupt
        handler หรือ vector ไปยัง a common entry point. ตัวอย่างถูกจัดให้มีของทั้งสองวิธี. ไม่ว่าในกรณีใด the first code ที่ปฏิบัติหลังจาก the jump ต้องเป็น
        the FreeRTOS context save code, ไม่ใช้ code ตัวกลางที่จัดให้มีโดยคอมไพเลอร์ใดๆ. อีกครั้ง - ใช้ the existing files เป็นตัวอ้างอิง.

        Linker scripts ต้องถูกปรับและอธิบายอย่างถูกต้อง the memory map of the microcontroller ที่กำลังถูกใช้.
Title: Re: FreeRTOS
Post by: tha on December 06, 2021, 09:04:59 AM
     •  Project settings:

        ทุกๆ project จะกำหนดโดยปกติ a preprocessor macro ที่เฉพาะเจาะจงไปยัง the port ที่กำลังถูกคอมไพล์. The preprocessor macro ระบุซึ่ง
        portmacro.h file จะถูกรวมด้วย. ตัวอย่างเช่น, GCC_MEGA_AVR ต้องถูกกำหนดเมื่อใช้ GCC เพื่อคอมไพล์ the MegaAVR port. IAR_MEGA_AVR  ต้องถูก
        กำหนดเมื่อใช้ IAR เพื่อคอมไพล์ the MegaAVR port, ฯลฯ. อ้างถึง existing demo application projects และ the file
        FreeRTOS/source/include/portable.h เพื่อค้นหาการกำหนดที่ถูกต้องสำหรับ project ของคุณ. ถ้า the preprocessor macro ไม่ถูกกำหนดดังนั้น the
        directory ซึ่ง the relevant portmacro.h file ถูกอยู่ต้องถูกรวมใน the preprocessors include search path.

        การตั้งค่าคอมไพเลอร์อื่นๆ เช่น ตัวเลือกการปรับให้เหมาะสมก็มีความสำคัญเช่นกัน. อีกครั้งอ้างถึง existing demo application projects สำหรับตัวอย่าง.

        Compilers ที่มี an IDE based interface โดยทั่วไปจะรวม the target microcontroller เป็นส่วนหนึ่งของ of the project settings - นี้ต้องถูกปรับเพื่อให้
        ถูกต้องสำหรับ the new target. ในทำนองเดียวกันในกรณีที่ a makefile ถูกใช้, ตัวเลือกภายใน makefile จะต้องได้รับการอัปเดตเพื่อให้ถูกต้องสำหรับเป้าหมาย
        ไมโครคอนโทรลเลอร์ใหม่.

     • Configuring the tick interrupt:

        The tick interrupt ถูกกำหนดค่าโดย a function ที่เรียกว่า prvSetupTimerInterrupt(), ซึ่งสามารถถูกเอาไว้อยู่ภายใน
        FreeRTOS/source/portable/[compiler]/[microcontroller]/port.c.
Title: Re: FreeRTOS
Post by: tha on December 06, 2021, 10:35:39 AM
     •  RAM usage and ROM usage:

        The Memory Management section จัดให้มีข้อมูลทั้งหมดที่ต้องการเพื่อเข้าใจวิธีที่ FreeRTOS ใช้ RAM, และวิธีที่ RAM นี้ถูกจัดสรรไปยัง the RTOS
        kernel.

        ถ้าคุณกำลังแปลง an existing demo application เพื่อรันบน a microcontroller ที่มี RAM น้อยกว่าดังนั้นคุณอาจจำเป็นต้องลด the
        configTOTAL_HEAP_SIZE value - ที่ถูกเอาไว้อยู่ภายใน FreeRTOSConfig.h - และลดจำนวนของ tasks ที่ the demo สร้าง. การลดจำนวนของ tasks ที่
        สร้างสามารถถูกทำให้สำเร็จลงโดยเอาออกอย่างง่ายๆ the function calls ที่ใช้สร้าง the tasks ภายใน main(), ดังที่แสดงไว้ก่อนหน้านี้.

        ถ้าคุณกำลังแปลง an existing demo application เพื่อรันบน a microcontroller ที่มี ROM น้อยกว่าดังนั้นคุณอาจจำเป็นต้องลดจำนวนของ demo
        application files ที่ถูก included ในการบิวด์. เหล่านี้คือ the files ที่ถูกเอาไว้อยู่ภายใน the FreeRTOS/Demo/common directory tree. เมื่อคุณเอาออก
        a demo application file จากการบิวด์คุณจะต้องเอาออก the call ภายใน main() ที่ใช้สร้าง the tasks ที่ไม่ได้ included อีกต่อไปด้วย.