ESP32 FreeRtos 任务通知2(直接任务通知取代二进制信号量)

简介

直接任务通知相当于指定任务的二进制信号量。

需要注意的是,在多任务都需要读取该状态时,建议使用二进制信号量。如需要实现三个发动机任务(因发动机需要经常运转来保持健康状态,所以三个任务需要轮流运行),而直接任务通知是指定任务,无法实现,因此,需要二进制信号量进行任务通信。

API:  

  xTaskNotifyGive  // 相当于精简化的 xTaskNotify() + eIncrement  (+1)

  ulTaskNotifyTake //  waitting for notification, then reset to 0   (-1)

程序实现

TaskHandle_t xflashLED = NULL;

void flashLED(void *pvParam) {
  uint32_t ulNotificationValue;
  pinMode(23, OUTPUT);
  while (1) {
    //返回运行此命令之前的Notification Value
    //命令含义: waitting for notification, then reset
    ulNotificationValue = ulTaskNotifyTake(pdTRUE, //pdTRUE 运行完后,清零
                                           portMAX_DELAY);

    if ( ulNotificationValue > 0 )
    {
      digitalWrite(23, !digitalRead(23));
      vTaskDelay(1000);
    }
  }
}

void readBtn(void *pvParam) {

  pinMode(22, INPUT_PULLUP);

  while (1) {
    if (digitalRead(22) == LOW) {
      //命令含义,相当于精简化的 xTaskNotify() + eIncrement
      xTaskNotifyGive(xflashLED);
      vTaskDelay(120); //button debounce
    }
  }
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  xTaskCreate(flashLED, "Flash LED", 1024 * 4, NULL, 1, &xflashLED);
  xTaskCreate(readBtn, "Read Button", 1024 * 4, NULL, 1, NULL);

}

void loop() {
}

运行环境

直接任务通知取代二进制信号量

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值