FreeRTOS的协程,实际上是线程并发出来的。从协程控制块中没有栈空间就能够知道,每个线程并发出来的协程共用一个栈空间。
/* 协程控制块 */
typedef struct corCoRoutineControlBlock
{
crCOROUTINE_CODE pxCoRoutineFunction; /* 协程函数指针 */
ListItem_t xGenericListItem; /* 状态列表项 */
ListItem_t xEventListItem; /* 事件列表项 */
UBaseType_t uxPriority; /* 协程优先级 */
UBaseType_t uxIndex; /* 协程ID,区别不同协程调用相同函数 */
uint16_t uxState; /* 协程状态 */
}CRCB_t;
创建协程
/* 创建协程 */
BaseType_t xCoRoutineCreate(crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex)
{
BaseType_t xReturn;
CRCB_t *pxCoRoutine;
/* 为协程控制块申请内存空间 */
pxCoRoutine = (CRCB_t *)pvPortMalloc(sizeof(CRCB_t));
if(pxCoRoutine)
{
/* 当前协程为空 */
if(pxCurrentCoRoutine == NULL)
{
/* 将该协程设为当前协程 */
pxCurrentC