FreeRTOS任务不应返回-ESP32 [英] FreeRTOS Task should not return - ESP32

查看:674
本文介绍了FreeRTOS任务不应返回-ESP32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ESP32上使用多线程.我创建了两个任务并将它们固定到核心1.使用其中一个任务,我得到以下错误:

I'm using multi-threading on my ESP32. I have created two tasks and pinned them to core 1. With one of them, I get the following error:

E (20426) FreeRTOS: FreeRTOS Task "MeasurementTask" should not return, Aborting now!
abort() was called at PC 0x4008b8f3 on core 1

Backtrace: 0x4008f34c:0x3ffd0a40 0x4008f57d:0x3ffd0a60 0x4008b8f3:0x3ffd0a80

Rebooting...

但是,我的"MeasurementTask"中没有 return 语句;(请参见下面的代码).这是什么问题?

However, there is no return statement in my "MeasurementTask" (see code below). What's the issue here?

tracker.cpp :

#include "tracker.h"

void threadedLoop(void *pvParameters) {
  Serial.println("Loop task pinned");
  for(;;) {
    checkAPTrigger();
    mqttLoop();
  }
}

void setupTracker() {
  Serial.print("Setup start: ");
  Serial.println(millis());
  Wire.begin();
  setup_sensors();
  if(setupAP()) {
    setupTime();
    setupMQTT();
  }
  Serial.print("Setup done: ");
  Serial.println(millis());

  Serial.println("Pinning measurement");
  TaskHandle_t measureTask;
  xTaskCreatePinnedToCore(
    takeMeasurement,
    "MeasurementTask",
    2048,
    NULL,
    1,
    NULL,
    ARDUINO_RUNNING_CORE
  );

  Serial.println("Pinning loop");
  TaskHandle_t loopTask;
  xTaskCreatePinnedToCore(
    threadedLoop,
    "LoopTask",
    2048,
    NULL,
    1,
    NULL,
    ARDUINO_RUNNING_CORE
  );
}

void loopTracker() {
  //takeMeasurement();
}

void takeMeasurement(void *pvParameters) {
  Serial.println("Measurement task pinned");
  DynamicJsonDocument root(512);
  JsonObject rootObj = root.to<JsonObject>();
  read_sensors(rootObj);

  if(!(settings.mqttUsed && publishData(rootObj))) {
    appendFile("data", root);
  }

  serializeJsonPretty(root, Serial);
  Serial.println("\n---\n");
}

素描:

#include <tracker.h>

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

void loop() {
  // put your main code here, to run repeatedly:
  loopTracker();
}

tracker.h 中包含很多文件,但我认为它们与此问题无关. takeMeasurement 中使用的某些函数确实具有返回值,但是我从来没有在该函数本身中返回它们.

There are quite some files included in the tracker.h, but I don't believe they're relevant to this problem. Some of the functions used in the takeMeasurement do have return values, but I never return them in that function itself.

推荐答案

在FreeRTOS中, tasks xTaskCreate ... 开始,以 vTaskDelete 结尾.任务功能可能不能简单地结束",这是不允许的.

In FreeRTOS, tasks are started with xTaskCreate... and ended with vTaskDelete. A task function may not simply "end", this is not allowed.

在函数末尾放置 vTaskDelete(NULL); 以正常结束任务:

Put vTaskDelete(NULL); at the end of the function to gracefully end the task:

void takeMeasurement(void *pvParameters) {
  // . . . task code . . .
  vTaskDelete(NULL);
}

话虽如此,任务通常是长期运行的.例如,测量任务可能是一个无休止的循环,需要进行测量,休眠一会儿然后重复执行.

Having said that, tasks are usually meant to be long-running. For example, the measurement task could be an endless loop that takes the measurement, sleeps for a while, and repeats.

这篇关于FreeRTOS任务不应返回-ESP32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆