野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3527|回复: 1

如何使用一个定时器 和 信号 去实现 阻塞一个任务

[复制链接]
发表于 2019-3-9 11:52:54 | 显示全部楼层 |阅读模式
我想在task 中take 信号量,另外 用个timer 定时10ms  give 一下信号,
但是信号一直give 失败, task 里面也一直 take 不到信号
代码 如下:
#include "FreeRTOS.h"
#include "semphr.h"
#include "projdefs.h"
#include "timers.h"
static TaskHandle_t task_handles;
TimerHandle_t timer_hadles;
SemaphoreHandle_t run_semaphore;
static void timer_generate(void)
{
//10ms 跑一次 给出一下信号
if(xSemaphoreGive( run_semaphore ) != pdTRUE)
{
OUT_LOG_I("GIVE NG");
}
}
void task(void)
{
// 创建信息量
run_semaphore = xSemaphoreCreateBinary();
if( run_semaphore != NULL )
{
OUT_LOG_I("run_semaphore ok");
}
for (;;)
{
//等待信号量到来
if( xSemaphoreTake( run_semaphore, portMAX_DELAY ) == pdTRUE )
{
Msg_Process();
}
else
{
OUT_LOG_I("=====semaphore not get====");
}
}
}
void task_create(void)
{
OUT_LOG_I("task create");
xTaskCreate(task,"task",640,NULL,1,&task_handles);
//每10ms产生一个信号量
timer_hadles = xTimerCreate("timer", TIMER_BASE/portTICK_PERIOD_MS, pdTRUE, NULL, timer_generate);
xTimerStart(timer_hadles, 0);
}

后面我使用 tasknotify 的函数去实现,但是 也是没反应
在 config  中 开了#define configUSE_TASK_NOTIFICATIONS 1

但是就是一直阻塞没有进入Msg_Process();


#include "FreeRTOS.h"
#include "semphr.h"
#include "projdefs.h"
#include "timers.h"
#include "task.h"

static TaskHandle_t     task_handles;
TimerHandle_t   timer_hadles;


static void timer_generate(void)
{
      //10ms
      xTaskNotifyGive( task_handles );
}


void task(void)
{
       for (;;)
       {
           //阻塞等待 notify
            ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
           Msg_Process();
        }
}

void task_create(void)
{
         OUT_LOG_I("task create");
         xTaskCreate(task,"task",640,NULL,1,&task_handles);
         //每10ms notify 一下
        timer_hadles = xTimerCreate("timer", TIMER_BASE/portTICK_PERIOD_MS, pdTRUE, NULL, timer_generate);
         xTimerStart(timer_hadles, 0);
}


回复

使用道具 举报

发表于 2019-3-12 08:34:51 | 显示全部楼层
这个不太了解,帮顶
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-17 11:17 , Processed in 0.038213 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表