下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

内核
最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

协程
[更多关于协程的信息……]

快速协程示例

此快速示例展示了协程的使用方法。
  1. 创建一个简单的协程来闪烁 LED

    以下代码定义了一个非常简单的协程,它只会定期闪烁 LED。

    void vFlashCoRoutine( CoRoutineHandle_t xHandle,
                     UBaseType_t uxIndex )
    {
       // Co-routines must start with a call to crSTART().
       crSTART( xHandle );
    
       for( ;; )
       {
          // Delay for a fixed period.
          crDELAY( xHandle, 10 );
    
          // Flash an LED.
          vParTestToggleLED( 0 );
       }
    
       // Co-routines must end with a call to crEND().
       crEND();
    }
    
    就是这样!

  2. 调度协程

    通过重复调用 vCoRoutineSchedule() 来调度协程。 最好的方法就是在空闲任务中编写一个 空闲任务钩子。 首先,请确保在 FreeRTOSConfig.h 中将 configUSE_IDLE_HOOK 设置为 1。 然后编写空闲任务钩子,如下所示:

    void vApplicationIdleHook( void )
    {
       vCoRoutineSchedule( void );
    }
    
    如果空闲任务没有执行任何其他函数,那按以下方式在循环中调用 vCoRoutineSchedule() 效率会更高:
    void vApplicationIdleHook( void )
    {
       for( ;; )
       {
          vCoRoutineSchedule( void );
       }
    }
    

  3. 创建协程并启动 RTOS 调度程序

    协程可在 main() 中创建。

    #include "task.h"
    #include "croutine.h"
    
    #define PRIORITY_0 0
    
    void main( void )
    {
       // In this case the index is not used and is passed 
       // in as 0.
       xCoRoutineCreate( vFlashCoRoutine, PRIORITY_0, 0 );
    
       // NOTE:  Tasks can also be created here!
    
       // Start the RTOS scheduler.
       vTaskStartScheduler();
    }
    

  4. 示例扩展:使用索引参数

    现在假设我们要从同一函数中创建 8 个这样的协程。 每个协程都将以不同的速度闪烁不同的 LED。 索引参数可用于在协程函数中 区分协程。

    这一次,我们将创建 8 个协程,并向每个协程传递不同的索引。

    #include "task.h"
    #include "croutine.h"
    
    #define PRIORITY_0        0
    #define NUM_COROUTINES    8
    
    void main( void )
    {
    int i;
    
       for( i = 0; i < NUM_COROUTINES; i++ )
       {
          // This time i is passed in as the index.
          xCoRoutineCreate( vFlashCoRoutine, PRIORITY_0, i );
       }
    
       // NOTE: Tasks can also be created here!
    
       // Start the RTOS scheduler.
       vTaskStartScheduler();
    }
    
    协程函数也被扩展,因此每个协程使用的 LED 和闪烁速度都不同。
    const int iFlashRates[ NUM_COROUTINES ] = { 10, 20, 30, 40, 50, 60, 70, 80 };
    const int iLEDToFlash[ NUM_COROUTINES ] = { 0, 1, 2, 3, 4, 5, 6, 7 }
    
    void vFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
    {
       // Co-routines must start with a call to crSTART().
       crSTART( xHandle );
    
       for( ;; )
       {
          // Delay for a fixed period.  uxIndex is used to index into
          // the iFlashRates.  As each co-routine was created with
          // a different index value each will delay for a different
          // period.
          crDELAY( xHandle, iFlashRate[ uxIndex ] );
    
          // Flash an LED.  Again uxIndex is used as an array index,
          // this time to locate the LED that should be toggled.
          vParTestToggleLED( iLEDToFlash[ uxIndex ] );
       }
    
       // Co-routines must end with a call to crEND().
       crEND();
    }
    





Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.