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();
}
#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();
}
#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.