【FreeRTOS】小白进阶之如何使用FreeRTOS多值信号量(二)
【摘要】
介绍多值信号量的使用。
1、头文件声明
#include "FreeRTOS.h"#include "task.h"#include "semphr.h"#include "supporting_functions.h" #define mainINTERRUPT_NUMBER 3 static void vHandlerTask(...
介绍多值信号量的使用。
1、头文件声明
-
#include "FreeRTOS.h"
-
#include "task.h"
-
#include "semphr.h"
-
#include "supporting_functions.h"
-
-
#define mainINTERRUPT_NUMBER 3
-
-
static void vHandlerTask( void *pvParameters );
-
static void vPeriodicTask( void *pvParameters );
-
-
static uint32_t ulExampleInterruptHandler( void );
-
-
// 定义多值信号量
-
SemaphoreHandle_t xCountingSemaphore;
2、启动任务
任务 vPeriodicRask 用于启动模拟中断:
-
int main( void )
-
{
-
// 创建多值信号量,最小值 0,最大值 10
-
xCountingSemaphore = xSemaphoreCreateCounting( 10, 0 );
-
-
if( xCountingSemaphore != NULL )
-
{
-
xTaskCreate( vHandlerTask, "Handler", 1000, NULL, 3, NULL );
-
-
xTaskCreate( vPeriodicTask, "Periodic", 1000, NULL, 1, NULL );
-
-
vPortSetInterruptHandler( mainINTERRUPT_NUMBER, ulExampleInterruptHandler );
-
-
vTaskStartScheduler();
-
}
-
-
for( ;; );
-
return 0;
-
}
3、定义任务
-
static void vHandlerTask( void *pvParameters )
-
{
-
for( ;; )
-
{
-
xSemaphoreTake( xCountingSemaphore, portMAX_DELAY );
-
-
vPrintString( "Handler task - Processing event.\r\n" );
-
}
-
}
-
-
static void vPeriodicTask( void *pvParameters )
-
{
-
const TickType_t xDelay500ms = pdMS_TO_TICKS( 500UL );
-
-
for( ;; )
-
{
-
vTaskDelay( xDelay500ms );
-
-
vPrintString( "Periodic task - About to generate an interrupt.\r\n" );
-
vPortGenerateSimulatedInterrupt( mainINTERRUPT_NUMBER );
-
vPrintString( "Periodic task - Interrupt generated.\r\n\r\n\r\n" );
-
}
-
}
-
-
static uint32_t ulExampleInterruptHandler( void )
-
{
-
BaseType_t xHigherPriorityTaskWoken;
-
-
xHigherPriorityTaskWoken = pdFALSE;
-
-
xSemaphoreGiveFromISR( xCountingSemaphore, &xHigherPriorityTaskWoken );
-
xSemaphoreGiveFromISR( xCountingSemaphore, &xHigherPriorityTaskWoken );
-
xSemaphoreGiveFromISR( xCountingSemaphore, &xHigherPriorityTaskWoken );
-
-
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
-
}
文章来源: blog.csdn.net,作者:简一商业,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/liwei16611/article/details/82590836
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)