FreeRTOS之任务通知代替计数信号量实验

1、 介绍常用函数。
①释放信号量xTaskNotifyGive。xTaskNotifyGive是宏定义,调用xTaskGenericNotify
_和二值信号一样!!!

#define xTaskNotifyGive( xTaskToNotify ) \
xTaskGenericNotify( ( xTaskToNotify ), ( 0 ), eIncrement, NULL )

#define xTaskNotifyGive( xTaskToNotify ) \
xTaskGenericNotify( ( xTaskToNotify ), ( 0 ), eIncrement, NULL )

/*
**参数1:任务控制块
**参数2:数据
**参数3:作为二值信号量和计数信号量,只能eIncrement
**参数4:更新以前通知值到参数4
*/
BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify, 
								uint32_t ulValue,
								eNotifyAction eAction, 
								uint32_t *pulPreviousNotificationValue )

②获取信号量

/*
**参数1:退出时清除计数值,作为二值信号量恒定为pdFALSE
**参数2:等待时间
*/
uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait )

2、创建任务

void m_create_task_notify3(void)
{
    BaseType_t xReturn = pdPASS;

    xReturn = xTaskCreate((TaskFunction_t )take_Task,
                          (const char*    )"take_Task",
                          (uint16_t       )512,
                          (void*          )NULL,
                          (UBaseType_t    )2,
                          (TaskHandle_t*  )&take_Task_Handle);
    if(pdPASS == xReturn)
        LOG_BLE("Receive1_Task create ok\n");


    xReturn = xTaskCreate((TaskFunction_t )give_Task,
                          (const char*    )"give_Task",
                          (uint16_t       )512,
                          (void*          )NULL,
                          (UBaseType_t    )3,
                          (TaskHandle_t*  )&give_Task_Handle);
    if(pdPASS == xReturn)
        LOG_BLE("Receive2_Task create ok\n");
}

3、 编写内存的测试任务入口函数

#include "limits.h"

#ifndef TAG_BLE
#define LOG_BLE(...)
#endif

//任务通知替代计数信号
static TaskHandle_t take_Task_Handle = NULL;
static TaskHandle_t give_Task_Handle = NULL;


static void take_Task(void* parameter)
{
    uint8_t button2_sta=0;
    uint32_t take_num=0;
    while (1)
    {
        if( READ_BUTTON2_P12() == BUTTON_PUSH )
        {
            if (0 == button2_sta)
            {
                button2_sta = 1;
                take_num=ulTaskNotifyTake(pdFALSE,0);//
                if(take_num > 0)
                {
                    LOG_BLE("take ok value:%d\n",take_num-1);
                } else
                {
                    LOG_BLE("take ok error\n");
                }
            }
        } else
        {
            button2_sta = 0;
        }
        vTaskDelay(20);
    }
}

static void give_Task(void* parameter)
{
    BaseType_t xReturn = pdPASS;
    uint8_t button1_sta=0;

    while (1)
    {
        if( READ_BUTTON1_P11() == BUTTON_PUSH )
        {
            if ( 0 == button1_sta)
            {
                button1_sta = 1;
                xReturn = xTaskNotifyGive(take_Task_Handle);//xTaskGenericNotify( ( xTaskToNotify ), ( 0 ), eIncrement, NULL )
                if( xReturn == pdPASS )
                    LOG_BLE("give task send ok\n");
            }
        } else
        {
            button1_sta = 0;
        }


        vTaskDelay(20);
    }
}

实验说明和现象
①take_Task任务:每按下一次按键将获取信号量,获取成功将打印剩余信号量。
②give_Task任务:每按下一次按键将释放信号量。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值