我正在开发基于 ESP32 和 freeRTOS 的高频 DAQ。将有 2 个任务,第一个任务将在核心 0 上运行,与传感器通信以 4000SPS 收集数据,使用 SPI 总线。任务 2 将在核心 1 上运行,从第一个任务接收数据并通过 TCP 异步发送。理想情况下,任务 2 应该在任务 1 收集最新数据的同时发送数据(双核,对吗?)当我进行测试运行时,我让 SPI 总线尽可能快地运行,但它只会阻止任务 2 运行! !我不想使用 vTaskDelay(1),因为它会影响收集数据的速度。我现在应该怎么做,让两个任务同时运行?谢谢!!
void inFromSensor(void *parameter)
{
for (;;)
{
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
// digitalWrite(SS, LOW);
SPI.write(0b000011100);
SPI.write(0b000011100);
SPI.write(0b000011100);
SPI.endTransaction();
// vTaskDelay(1000/portTICK_PERIOD_MS);
// for (int i = 0; i < 88; i++)
// {
// testBuffer1[i] = 8;
// itoa(testBuffer1[i], &buffer1[i], 10);
// }
// buffer1[0] = '1';
// buffer1[87] = '\n';
// UBaseType_t res = xRingbufferSend(buf_handle, buffer1, sizeof(buffer1), pdMS_TO_TICKS(1000));
// if (res != pdTRUE)
// {
// printf("Failed to send item\n");
// }
TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed = 1;
TIMERG0.wdt_wprotect = 0;
}
vTaskDelete(NULL);
}
void outToTCP(void *parameter)
{
for (;;)
{
Serial.println("a");
WiFiClient client = wifiServer.available();
if (client)
{
Serial.println("client connected");
while (client.connected())
{
unsigned long start = millis();
while (millis() - start < 5000)
{
size_t item_size;
char *item = (char *)xRingbufferReceive(buf_handle, &item_size, pdMS_TO_TICKS(1000));
if (item != NULL)
{
client.write(item);
//Return Item
vRingbufferReturnItem(buf_handle, (void *)item);
}
else
{
//Failed to receive item
printf("Failed to receive item\n");
}
}
Serial.println("done");
return;
}
client.stop();
Serial.println("Client disconnected");
}
TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed = 1;
TIMERG0.wdt_wprotect = 0;
}
vTaskDelete(NULL);
}
void setup()
{
Serial.begin(112500);
WiFi.begin(ssid, password);
SPI.begin(SCK, MISO, MOSI, SS);
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi..");
}
wifiServer.begin();
buf_handle = xRingbufferCreate(50280, RINGBUF_TYPE_NOSPLIT);
if (buf_handle == NULL)
{
printf("Failed to create ring buffer\n");
}
Serial.println("ready");
xTaskCreatePinnedToCore(
inFromSensor, /* Task function. */
"lowPriorityTask", /* name of task. */
15000, /* Stack size of task */
NULL, /* parameter of the task */
4, /* priority of the task */
NULL, /* Task handle to keep track of created task */
1);
delay(500);
xTaskCreatePinnedToCore(
outToTCP, /* Task function. */
"highPriorityTask", /* name of task. */
15000, /* Stack size of task */
NULL, /* parameter of the task */
6, /* priority of the task */
NULL, /* Task handle to keep track of created task */
0);
delay(500);
Serial.println("ready");}