FreeRTOS之任务通知代替二值信号量实验

1、 介绍常用函数。
①释放信号量xTaskNotifyGive。xTaskNotifyGive是宏定义,调用xTaskGenericNotify

#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:退出时清除计数值,作为二值信号量恒定为pdTRUE
**参数2:等待时间
*/
uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait )

2、创建任务

void m_create_task_notify2(void)
{
    BaseType_t xReturn = pdPASS;

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


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


    xReturn = xTaskCreate((TaskFunction_t )Send_Task,
                          (const char*    )"Send_Task",
                          (uint16_t       )512,
                          (void*          )NULL,
                          (UBaseType_t    )4,
                          (TaskHandle_t*  )&Send_Task_Handle);
    if(pdPASS == xReturn)
        LOG_BLE("Send_Task create ok\n");
}

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

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

static TaskHandle_t Receive1_Task_Handle = NULL;
static TaskHandle_t Receive2_Task_Handle = NULL;
static TaskHandle_t Send_Task_Handle = NULL;

static void Receive1_Task(void* parameter)
{
    while (1)
    {
        ulTaskNotifyTake(pdTRUE,portMAX_DELAY);//为pdTRUE时,获取成功后将任务计数值设置为0,为pdFALSE时,获取成功后将任务计数值减1
        LOG_BLE("Receive1_Task accept ok\n");
        TOGGLE_LED13();
    }
}
static void Receive2_Task(void* parameter)
{
    while (1)
    {

        ulTaskNotifyTake(pdTRUE,portMAX_DELAY);//为pdTRUE时,获取成功后将任务计数值设置为0,为pdFALSE时,获取成功后将任务计数值减1
        LOG_BLE("Receive2_Task accept ok\n");
        TOGGLE_LED14();
    }
}
static void Send_Task(void* parameter)
{
    BaseType_t xReturn = pdPASS;

    uint8_t button1_sta=0;
    uint8_t button2_sta=0;

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

        } else
        {
            button1_sta = 0;
        }

        if( READ_BUTTON2_P12() == BUTTON_PUSH )
        {
            if (0 == button2_sta)
            {
                button2_sta = 1;
                xReturn = xTaskNotifyGive(Receive2_Task_Handle);

                if( xReturn == pdPASS )
                    LOG_BLE("send task notify for Receive2_Task_Handle ok\n");
            }

        } else
        {
            button2_sta = 0;
        }
        vTaskDelay(20);
    }
}

实验说明和现象
①Receive1_Task和Receive2_Task阻塞等待
②Send_Task判断有按键按下,将发送对应的信号给对应的任务。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值