简体   繁体   English

在 Arduino freeRTOS 上设置位

[英]Setting bits on Arduino freeRTOS

I have been studying freeRTOS on Arduino, and I made some code, as it follows So I got this Arduino RTOS code我一直在研究Arduino上的freeRTOS,我做了一些代码,如下所以我得到了这个Arduino RTOS代码

#include <Arduino_FreeRTOS.h>
#include "event_groups.h"

#define red    6
#define Yellow 7
#define blue   8

#define TASK1_BIT (1UL<<0UL) //Unsigned long bit0 set to 1
#define TASK2_BIT (1UL<<1U)

EventGroupHandle_t xEventGroup;

void setup() 
{
  Serial.begin(9600);
  pinMode(red,OUTPUT);
  pinMode(blue,OUTPUT);

  xEventGroup = xEventGroupCreate();

  xTaskCreate(EventBitSettingTask,"Bit Setter",100,NULL,1,NULL);
  xTaskCreate(EventBitReadingTask,"Bit Reader",100,NULL,1,NULL);
}


void EventBitSettingTask(void *pvParameters)
{
  const TickType_t xDelay500ms = pdMS_TO_TICKS(500);
  while(1)
  {
    xEventGroupSetBits(xEventGroup,TASK1_BIT); //Event,bit
    vTaskDelay(xDelay500ms);
    xEventGroupSetBits(xEventGroup,TASK2_BIT);
  }
}


void EventBitReadingTask(void *pvParameters)
{
  const EventBits_t xBitsToWaitFor = (TASK1_BIT | TASK2_BIT);
  EventBits_t xEventGroupValue;
  
  while(1)
  {
    xEventGroupValue = xEventGroupWaitBits(xEventGroup,xBitsToWaitFor,pdTRUE,pdTRUE,portMAX_DELAY);//EventGroup,receive, clear on exit, all bits?,wait
    
    if(xEventGroupValue & TASK1_BIT != 0)
    {
      digitalWrite(red,digitalRead(red)^1);
    }
    
    if(xEventGroupValue & TASK2_BIT != 0)
    {
      digitalWrite(blue,digitalRead(blue)^1);
    }
  }
}

void loop() {


}

While playing with it, I realized if I set TASK1_BIT (1UL<<0UL) to TASK1_BIT (0UL<<0UL) , none of the leds will blink, but if I set TASK2_BIT (1UL<<1UL) to TASK2_BIT (0UL<<1UL) , just the red led will blink.在玩它时,我意识到如果我将TASK1_BIT (1UL<<0UL)设置为TASK1_BIT (0UL<<0UL) ,所有 LED 都不会闪烁,但是如果我将TASK2_BIT (1UL<<1UL)设置为TASK2_BIT (0UL<<1UL) ,只有红色 LED 会闪烁。

Apparently, the event of the blue led is dependent from the event of the red led, but why???显然,蓝色 LED 的事件依赖于红色 LED 的事件,但为什么呢???

According to the FreeRTOS documentation, xEventGroupWaitBits() is used to read the value of an event group and optionally wait in the blocked state for one or more event bits in the event group to become set.根据 FreeRTOS 文档, xEventGroupWaitBits()用于读取事件组的值,并可选择在阻塞的 state 中等待事件组中的一个或多个事件位被设置。

The parameter xBitsToWaitFor specifies which event bits in the event group will be tested.参数xBitsToWaitFor指定将测试事件组中的哪些事件位。 If the parameter xWaitForAllBits is set to pdTRUE, bitwise AND will be used in the test.如果参数xWaitForAllBits设置为 pdTRUE,则测试中将使用按位与。 Setting xTicksToWait to portMAX_DELAY will cause the task to wait without timing out.xTicksToWait设置为 portMAX_DELAY 将导致任务等待而不会超时。 That means the task will leave a blocked state only when unblock condition is met.这意味着只有在满足解锁条件时,任务才会留下一个阻塞的 state。

When TASK1_BIT and TASK2_BIT are set to 0001 and 0010 respectively (in binary), the event group will leave the blocked state only when all the bits in the event group are equal to xWaitForAllBits , in this case 0011. That will happen after two calls to xEventGroupSetBits() .当 TASK1_BIT 和 TASK2_BIT 分别设置为 0001 和 0010(二进制)时,只有当事件组中的所有位都等于 xWaitForAllBits 时,事件组才会离开阻塞的xWaitForAllBits ,在本例中为 0011。这将在两次调用后发生xEventGroupSetBits()

After the event group leaves the blocked state, the value of each bit in the group will be compared to bit's default value, and if the particular bit was set, the led will blink.事件组离开阻塞的 state 后,组中每个位的值将与位的默认值进行比较,如果特定位被设置,则 LED 将闪烁。 So by default, both leds should blink intermittently.所以默认情况下,两个 LED 应该间歇性闪烁。

If you change the value of TASK1_BIT to 0 (by 0UL<<0UL), than the value of xBitsToWaitFor will be 0010. The result of xEventGroupValue & TASK1_BIT (0010 & 0000) will be 0 so the red led won't blink.如果将 TASK1_BIT 的值更改为 0(按 0UL<<0UL),则xBitsToWaitFor的值将为xEventGroupValue & TASK1_BIT (0010 & 0000) 的结果将为 0,因此红色 LED 不会闪烁。 On the other hand, the result of xEventGroupValue & TASK2_BIT (0010 & 0010) will be.= 0 so the blue led should blink.另一方面, xEventGroupValue & TASK2_BIT (0010 & 0010) 的结果将是。= 0 所以蓝色 LED 应该闪烁。

Vice versa, if you set the value of TASK2_BIT to 0 (by 0UL<<1UL), than the value of xBitsToWaitFor will be 0001. xEventGroupValue & TASK1_BIT (0001 & 0001) will return 1 and the red led will blink.反之亦然,如果您将 TASK2_BIT 的值设置为 0(0UL<<1UL),则 xBitsToWaitFor 的值将为xBitsToWaitFor xEventGroupValue & TASK1_BIT (0001 & 0001) 将返回 1,红色 LED 将闪烁。 xEventGroupValue & TASK2_BIT (0001 & 0000) will return 0 so the blue led shouldn't blink. xEventGroupValue & TASK2_BIT (0001 & 0000) 将返回 0,因此蓝色 LED 不应闪烁。

A detailed explanation of how these API-s work can be found at https://freertos.org .有关这些 API 如何工作的详细说明,请访问https://freertos.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM