FreeRTOS

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

Previous topic - Next topic

tha

     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.

         

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

         

tha

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.

tha

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.


tha

     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.

tha

     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.

tha

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 จะถูกส่งไปยังด้านหน้าได้เช่นกัน.

         
               (ภาพดูในลิ้งค์เอานะ จะเป็นภาพเคลื่อนไหว)

               การเขียนถึงและการอ่านจาก 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.

tha

     •  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, ฯลฯ.

tha

     •  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 (ที่มีสิทธิ์เต็ม).