c++ 将数组传递给FreeRTOS任务时的指针转换问题

s3fp2yjn  于 2023-07-01  发布在  其他
关注(0)|答案(1)|浏览(116)

我是FreeRTOS的新用户。我想将数组作为参数传递给要处理的任务。我引用了一段视频
https://www.youtube.com/watch?v=W271bMnUKH8
代码:

static TaskHandle_t xFuncExec = NULL;
static TaskHandle_t xTaskGive = NULL;
uint8_t gFuncBits[29] = {0};

void taskWait(void * pvParam) {
  uint8_t *funcstat = pvParam;
  uint32_t ulNotificationValue; //用来存放本任务的4个字节的notification value
  BaseType_t xResult;

  while (1) {
    //vTaskDelay(1000);
    xResult = xTaskNotifyWait(0x00, //在运行前这个命令之前,先清除这几位
                              0x00, //运行后,重置所有的bits 0x00 or ULONG_MAX or 0xFFFFFFFF
                              &ulNotificationValue, //重置前的notification value
                              portMAX_DELAY ); //一直等待

    if (xResult == pdTRUE) {
      // Serial.println(ulNotificationValue,BIN); //将自己的notification value以二进制方式打出来
      switch (ulNotificationValue)
      {
        case BIT0:
          if (*(funcstat) == 1)
          {
            Serial.println("Func 1 on");
          }
          else
          {
            Serial.println("Func 1 off");
          }
          break;
        case BIT1:
          if (*(funcstat + 1) == 1)
          {
            Serial.println("Func 2 on");
          }
          else
          {
            Serial.println("Func 2 off");
          }
          break;
        case BIT2:
          if (*(funcstat + 2) == 1)
          {
            Serial.println("Func 3 on");
          }
          else
          {
            Serial.println("Func 3 off");
          }
          break;
        case BIT4:
          if (*(funcstat + 3) == 1)
          {
            Serial.println("Func 4 on");
          }
          else
          {
            Serial.println("Func 4 off");
          }
          break;
        case BIT5:
          if (*(funcstat + 4) == 1)
          {
            Serial.println("Func 5 on");
          }
          else
          {
            Serial.println("Func 5 off");
          }
          break;

      }

    } else {
      Serial.println("Timeout");
    }
  }
}

void taskGive(void *pvParam) {
  BaseType_t xResult;
  while (1) {
    uint8_t incom =  Serial.read();
    switch (incom)
    {
      case 48://"0"

        if (&gFuncBits[0] == 0)
        {
          gFuncBits[0] = 1;
        }
        else
        {
          gFuncBits[0] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT0, eSetValueWithOverwrite );
        break;
      case 49:// "1"
        if (&gFuncBits[1] == 0)
        {
          gFuncBits[1] = 1;
        }
        else
        {
          gFuncBits[1] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT1, eSetValueWithOverwrite );
        break;
      case 50://"2"
        if (&gFuncBits[2] == 0)
        {
          gFuncBits[2] = 1;
        }
        else
        {
          gFuncBits[2] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT2, eSetValueWithOverwrite );
        break;
      case 51://"3"
        if (&gFuncBits[3] == 0)
        {
          gFuncBits[3] = 1;
        }
        else
        {
          gFuncBits[3] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT3, eSetValueWithOverwrite );
        break;
      case 52://"4"
        if (&gFuncBits[4] == 0)
        {
          gFuncBits[4] = 1;
        }
        else
        {
          gFuncBits[4] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT4, eSetValueWithOverwrite );
        break;
    }
    // Serial.println(xResult == pdPASS ? "成功\n":"失败\n");

  }
}

void setup() {
  Serial.begin(115200);

  xTaskCreate(taskGive, "", 1024 * 4, (void *)gFuncBits, 1, &xTaskGive);
  xTaskCreate(taskWait, "", 1024 * 4, NULL, 1, &xFuncExec);
  vTaskDelete(NULL);
}

void loop() { }

但是在任务中,当void指针第一次分配给任务中的uint8_t时,会报告错误。错误:/sketch/sketch.ino: In function 'void taskWait(void*)': invalid conversion from 'void*' to 'uint8_t*' {aka 'unsigned char*'} [-fpermissive]
我不知道如何理解和处理它。

idv4meu8

idv4meu81#

我认识到代码中有一些bug,还有其他教程,现在可以正常工作。
代码:

static TaskHandle_t xFuncExec = NULL;
static TaskHandle_t xTaskGive = NULL;
uint8_t gFuncBits[29] = {0};

void taskWait(void * pvParam) {
  uint8_t *pFuncStat;
  pFuncStat = (uint8_t *)pvParam;
  uint32_t ulNotificationValue; //用来存放本任务的4个字节的notification value
  BaseType_t xResult;
  while (1) {
    //vTaskDelay(1000);
    xResult = xTaskNotifyWait(0x00, //在运行前这个命令之前,先清除这几位
                              0x00, //运行后,重置所有的bits 0x00 or ULONG_MAX or 0xFFFFFFFF
                              &ulNotificationValue, //重置前的notification value
                              portMAX_DELAY ); //一直等待

    if (xResult == pdTRUE) {
      // Serial.println(ulNotificationValue,BIN); //将自己的notification value以二进制方式打出来
      switch (ulNotificationValue)
      {
        case BIT0:
          Serial.print(*pFuncStat);
          if (*(pFuncStat) == 1)
          {
            Serial.println("Func 1 on");
          }
          else
          {
            Serial.println("Func 1 off");
          }
          break;
        case BIT1:
          if (*(pFuncStat + 1) == 1)
          {
            Serial.println("Func 2 on");
          }
          else
          {
            Serial.println("Func 2 off");
          }
          break;
        case BIT2:
          if (*(pFuncStat + 2) == 1)
          {
            Serial.println("Func 3 on");
          }
          else
          {
            Serial.println("Func 3 off");
          }
          break;
        case BIT4:
          if (*(pFuncStat + 3) == 1)
          {
            Serial.println("Func 4 on");
          }
          else
          {
            Serial.println("Func 4 off");
          }
          break;
        case BIT5:
          if (*(pFuncStat + 4) == 1)
          {
            Serial.println("Func 5 on");
          }
          else
          {
            Serial.println("Func 5 off");
          }
          break;
      }
    } else {
      Serial.println("Timeout");
    }
  }
}
void taskGive(void *pvParam) {
  BaseType_t xResult;
  while (1) {
    uint8_t incom =  Serial.read();
    switch (incom)
    {
      case 48://"0"
        //
        if (gFuncBits[0] == 0)
        {
          gFuncBits[0] = 1;
        }
        else
        {
          gFuncBits[0] = 0;
        }
        Serial.println(gFuncBits[0]);
        xResult = xTaskNotify( xFuncExec, BIT0, eSetValueWithOverwrite );
        break;
      case 49:// "1"

        if (gFuncBits[1] == 0)
        {
          gFuncBits[1] = 1;
        }
        else
        {
          gFuncBits[1] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT1, eSetValueWithOverwrite );
        break;
      case 50://"2"
        if (gFuncBits[2] == 0)
        {
          gFuncBits[2] = 1;
        }
        else
        {
          gFuncBits[2] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT2, eSetValueWithOverwrite );
        break;
      case 51://"3"
        if (gFuncBits[3] == 0)
        {
          gFuncBits[3] = 1;
        }
        else
        {
          gFuncBits[3] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT3, eSetValueWithOverwrite );
        break;
      case 52://"4"
        if (gFuncBits[4] == 0)
        {
          gFuncBits[4] = 1;
        }
        else
        {
          gFuncBits[4] = 0;
        }
        xResult = xTaskNotify( xFuncExec, BIT4, eSetValueWithOverwrite );
        break;
    }
    // Serial.println(xResult == pdPASS ? "成功\n":"失败\n");

  }
}

void setup() {
  Serial.begin(115200);

  xTaskCreate(taskGive, "", 1024 * 4, NULL, 1, &xTaskGive);
  xTaskCreate(taskWait, "", 1024 * 4, (void *)gFuncBits, 1, &xFuncExec);
  vTaskDelete(NULL);
}

void loop() { }

错误一:

&gFuncBits[2] == 0

变化

gFuncBits[2] == 0

并且gFuncBits[X] =值;
错误二:

xTaskCreate(taskWait, "", 1024 * 4, (void *)gFuncBits, 1, &xFuncExec);

taskWait任务传递数组,没有taskGive任务
指针转换修改:

uint8_t *pFuncStat;
pFuncStat = (uint8_t *)pvParam;

我不明白,但我明白。

相关问题