安富萊電子 www.armfly.com
安富萊_STM32-V5開發(fā)板_FreeRTOS教程(V1.0)
使用 FreeRTOS 的事件標志組要包含頭文件#include "event_groups.h"
主函數(shù)初始化里創(chuàng)建任務(wù)通信機制
AppObjCreate();
/*
*********************************************************************************************************
*? 函 數(shù) 名: AppObjCreate
*? 功能說明: 創(chuàng)建任務(wù)通信機制
*? 形 參: 無
*? 返 回 值: 無
*********************************************************************************************************
*/
static void AppObjCreate (void)
{
/* 創(chuàng)建事件標志組 */
xCreatedEventGroup = xEventGroupCreate();
if(xCreatedEventGroup == NULL)
{
/* 沒有創(chuàng)建成功,用戶可以在這里加入創(chuàng)建失敗的處理機制 */
}
/* 設(shè)置事件標志組的 bit0 */
uxBits = xEventGroupSetBits(xCreatedEventGroup, BIT_0);
if((uxBits & BIT_0) != 0)
{
printf("事件標志的 bit0 被設(shè)置\r\n");
}
else
{
printf("事件標志的 bit0 被清除,說明任務(wù) vTaskMsgPro 已經(jīng)接受到 bit0 和 bit1 被設(shè)置的情況\r\n");
}
/* 設(shè)置事件標志組的 bit1 */
uxBits = xEventGroupSetBits(xCreatedEventGroup, BIT_1);
if((uxBits & BIT_1) != 0)
{
printf("K3 鍵按下,事件標志的 bit1 被設(shè)置\r\n");
}
else
{
printf("事件標志的 bit1 被清除,說明任務(wù) vTaskMsgPro 已經(jīng)接受到 bit0 和 bit1 被設(shè)置的情況\r\n");
}
/*
*********************************************************************************************************
*? 函 數(shù) 名: vTaskMsgPro
*? 功能說明: 消息處理,使用函數(shù) xEventGroupWaitBits 接收任務(wù) vTaskTaskUserIF 發(fā)送的事件標志
*? 形 參: pvParameters 是在創(chuàng)建該任務(wù)時傳遞的形參
*? 返 回 值: 無
* 優(yōu) 先 級: 3
*********************************************************************************************************
*/
static void vTaskMsgPro(void *pvParameters)
{
EventBits_t uxBits;
const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS; /* 最大延遲 100ms */
while(1)
{
/* 等設(shè)置 bit0 和 設(shè)置 bit1 */
uxBits = xEventGroupWaitBits(xCreatedEventGroup, /* 事件標志組句柄 */
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?BIT_ALL, /* 等待 bit0 和 bit1 被設(shè)置 */
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?pdTRUE, /* 退出前 bit0 和 bit1 被清除,這里是 bit0 和 bit1都被設(shè)置才表示“退出”*/
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?pdTRUE, /* 設(shè)置為 pdTRUE表示等待 bit1和 bit0 都被設(shè)置*/
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?xTicksToWait); /* 等待延遲時間 */
if((uxBits & BIT_ALL) == BIT_ALL)
{
/* 接收到 bit1 和 bit0 都被設(shè)置的消息 */
printf("接收到 bit0 和 bit1 都被設(shè)置的消息\r\n");
}
else
{
/* 超時,另外注意僅接收到一個bit位置位的消息時,變量 uxBits 的相應(yīng) bit 也是被設(shè)置的 */
bsp_LedToggle(3);
}
}
}
事件標志組(中斷方式)
使用事件標志組中的函數(shù) xEventGroupSetBitsFromISR,務(wù)必使能以下三個宏定義
?? #define INCLUDE_xEventGroupSetBitFromISR 1
?? #define INCLUDE_xTimerPendFunctionCall 1
?? #define configUSE_TIMERS? 1
因為使能了 FreeRTOS 的定時器組任務(wù),定時器組的其它宏定義也做一下配置,配置如下:
?? #define configTIMER_TASK_PRIORITY ( 5 )
?? #define configTIMER_QUEUE_LENGTH? 20
?? #define configTIMER_TASK_STACK_DEPTH? ( configMINIMAL_STACK_SIZE * 2 )
初始化添加定時器函數(shù),用來實現(xiàn)中斷
bsp_InitHardTimer(); /* 初始化 TIM2 定時器 */
uxBits = xEventGroupSetBits(xCreatedEventGroup, BIT_0);被修改為啟動單次定時器中斷,在中斷回調(diào)函數(shù)里實現(xiàn)該功能
printf("啟動單次定時器中斷,50ms 后在定時器中斷給任務(wù) vTaskMsgPro 發(fā)送事件標志\r\n")
bsp_StartHardTimer(1 ,50000, (void *)TIM_CallBack1);
uxBits = xEventGroupSetBits(xCreatedEventGroup, BIT_1);被修改為啟動單次定時器中斷,在中斷回調(diào)函數(shù)里實現(xiàn)該功能
printf("啟動單次定時器中斷,50ms 后在定時器中斷給任務(wù) vTaskMsgPro 發(fā)送事件標志\r\n");
bsp_StartHardTimer(2 ,50000, (void *)TIM_CallBack2);
定時器中斷回調(diào)函數(shù)中給任務(wù)發(fā)送事件標志組設(shè)置消息:
/*
*********************************************************************************************************
*? 函 數(shù) 名: TIM_CallBack1 和 TIM_CallBack2
*? 功能說明: 定時器中斷的回調(diào)函數(shù),此函數(shù)被 bsp_StartHardTimer 所調(diào)用。
*? 形 參: 無
*? 返 回 值: 無
*********************************************************************************************************
*/
static void TIM_CallBack1(void)
{
BaseType_t xResult;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
/* 向任務(wù) vTaskMsgPro 發(fā)送事件標志 */
xResult = xEventGroupSetBitsFromISR(xCreatedEventGroup, /* 事件標志組句柄 */
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? BIT_0 , /* 設(shè)置 bit0 */
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????&xHigherPriorityTaskWoken );
/* 消息被成功發(fā)出 */
if( xResult != pdFAIL )
{
/* 如果 xHigherPriorityTaskWoken = pdTRUE,那么退出中斷后切到當前最高優(yōu)先級任務(wù)執(zhí)行 */
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
static void TIM_CallBack2(void)
{
BaseType_t xResult;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
/* 向任務(wù) vTaskMsgPro 發(fā)送事件標志 */
xResult = xEventGroupSetBitsFromISR(xCreatedEventGroup, /* 事件標志組句柄 */
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? BIT_1, /* 設(shè)置 bit1 */
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? &xHigherPriorityTaskWoken );
/* 消息被成功發(fā)出 */
if( xResult != pdFAIL )
{
/* 如果 xHigherPriorityTaskWoken = pdTRUE,那么退出中斷后切到當前最高優(yōu)先級任務(wù)執(zhí)行 */
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}