esp-open-rtosを使ってみる

Event Groups機能

こちらでTask Notifications機能を紹介しましたが、よく似た機能にEvent Group機能が有ります。
Task Notifications機能は、イベント通知先のTaskHandleを使いますが、
Event Group機能は、イベント通知先のEventGroupHandleを使います。

サンプルコードが無かったので、以下のコードで、Event Group機能を確認をしてみました。
/* The example of esp-free-rtos
 *
 * This sample code is in the public domain.
 */
#include <stdlib.h>
#include <string.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "esp8266.h"
#include "event_groups.h"

#define BIT_0   ( 1 << 0 )
#define BIT_4   ( 1 << 4 )

/* Declare a variable to hold the created event group. */
EventGroupHandle_t xEventGroup;

// SetBits task
void task1(void *pvParameters)
{
  TickType_t nowTick;
  EventBits_t uxBits;
  nowTick = xTaskGetTickCount();
  printf("[%s:%d] Start uxBits=%d\n",pcTaskGetName(0),nowTick,sizeof(uxBits));

  while(1) {
  /* Set bit 0 in xEventGroup. */
    uxBits = xEventGroupSetBits( xEventGroup, BIT_0 );/* The bits being set. */
    nowTick = xTaskGetTickCount();
    printf("[%s:%d] uxBits=%02x\n",pcTaskGetName(0),nowTick,uxBits);
    vTaskDelay(5000 / portTICK_PERIOD_MS);
  }
}

// SetBits task
void task2(void *pvParameters)
{
  TickType_t nowTick;
  EventBits_t uxBits;
  nowTick = xTaskGetTickCount();
  printf("[%s:%d] Start uxBits=%d\n",pcTaskGetName(0),nowTick,sizeof(uxBits));

  while(1) {
  /* Set bit 4 in xEventGroup. */
    uxBits = xEventGroupSetBits( xEventGroup, BIT_4 );/* The bits being set. */
    nowTick = xTaskGetTickCount();
    printf("[%s:%d] uxBits=%02x\n",pcTaskGetName(0),nowTick,uxBits);
    vTaskDelay(8000 / portTICK_PERIOD_MS);
  }
}

// WaitBits task
void task3(void *pvParameters)
{
  TickType_t nowTick;
  EventBits_t uxBits;
  nowTick = xTaskGetTickCount();
  printf("[%s:%d] Start uxBits=%d\n",pcTaskGetName(0),nowTick,sizeof(uxBits));

  while(1) {
    /* Wait forever for either bit 0 or bit 4 to be set within the event group.
       Clear the bits before exiting. */
    uxBits = xEventGroupWaitBits( xEventGroup,
            BIT_0 | BIT_4, /* The bits within the event group to wait for. */
            pdTRUE,        /* BIT_0 & BIT_4 should be cleared before returning. */
            pdFALSE,       /* Don't wait for both bits, either bit will do. */
            portMAX_DELAY);/* Wait forever. */
    nowTick = xTaskGetTickCount();
    if ( ( uxBits & BIT_0 ) == BIT_0 ) {
      printf("[%s:%d] Event from SetBit1\n",pcTaskGetName(0),nowTick);
    }
    if ( ( uxBits & BIT_4 ) == BIT_4 ) {
      printf("[%s:%d] Event from SetBit2\n",pcTaskGetName(0),nowTick);
    }
  }
}


void user_init(void)
{
  uart_set_baud(0, 115200);
  printf("SDK version:%s\n", sdk_system_get_sdk_version());
  printf("pcTaskGetName=%s\n",pcTaskGetName(0));


  /* Attempt to create the event group. */
  xEventGroup = xEventGroupCreate();
  /* Check everything was created. */
  configASSERT( xEventGroup );

  xTaskCreate(task1, "SetBit1", 256, NULL, 2, NULL);
  xTaskCreate(task2, "SetBit2", 256, NULL, 2, NULL);
  xTaskCreate(task3, "WaitBit", 256, NULL, 2, NULL);
}

EventGroupで使えるビット数については、こちらに 説明が有りました。

The number of bits (or flags) stored within an event group is 8 if configUSE_16_BIT_TICKS is set to 1, or 24 if configUSE_16_BIT_TICKS is set to 0.

EventBits_t変数のサイズは4バイトですが、有効なビット数は24ビットです。

結果は以下の様になります。
SetBit1のタスクは500Tick毎にBit0のイベントを、SetBit2のタスクは800Tick毎にBit4のイベントを発行しま す。
WaitBitのタスクはBit0とBit4の両方のイベントを待ちます。
1Tick目でSetBit1とSetBit2の両方のタスクがイベントを発行するので、
2Tick目のWaitBitでは両方のイベントを受けています。
その後はSetBit1とSetBit2の周期に応じたイベントを受けます。
SDK version:0.9.9
pcTaskGetName=uiT
[SetBit1:1] Start uxBits=4
[SetBit1:1] uxBits=01
[SetBit2:1] Start uxBits=4
[SetBit2:1] uxBits=11
[WaitBit:1] Start uxBits=4
[WaitBit:2] Event from SetBit1
[WaitBit:2] Event from SetBit2
[SetBit1:501] uxBits=00
[WaitBit:501] Event from SetBit1
[SetBit2:801] uxBits=00
[WaitBit:801] Event from SetBit2
[SetBit1:1001] uxBits=00
[WaitBit:1001] Event from SetBit1
[SetBit1:1501] uxBits=00
[WaitBit:1501] Event from SetBit1
[SetBit2:1601] uxBits=00
[WaitBit:1601] Event from SetBit2
[SetBit1:2001] uxBits=00
[WaitBit:2001] Event from SetBit1
[SetBit2:2401] uxBits=00
[WaitBit:2401] Event from SetBit2
[SetBit1:2501] uxBits=00
[WaitBit:2501] Event from SetBit1
[SetBit1:3001] uxBits=00
[WaitBit:3001] Event from SetBit1
[SetBit2:3201] uxBits=00
[WaitBit:3201] Event from SetBit2

続く....