我正在为使用 FreeRTOS 的 ESP-IDF 的 ESP32-S3 编写一些代码。 我创建了一个简单的任务来从
stdin
读取数据,并且我想在读取后调用回调。
这是我的代码:
main.c
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "tasks/task_nmea.h"
TaskHandle_t taskNMEAHandle = NULL;
void Task_NMEA_cb(char *line)
{
printf(line);
}
void app_main(void)
{
xTaskCreate(Nmea_Task, "task_nmea", 2048, &Task_NMEA_cb, 10, &taskNMEAHandle);
while (1)
{
vTaskDelay(1);
}
}
task_nmea.h
#ifndef TASK_NMEA_H
#define TASK_NMEA_H
typedef void (*nmea_cb_t)(char *);
void Nmea_Task(void *arg);
#endif // TASK_NMEA_H
task_nmea.c
#include "task_nmea.h"
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <driver/usb_serial_jtag.h>
#define NMEA_BUFFER_SIZE 256
char data[NMEA_BUFFER_SIZE];
nmea_cb_t callback;
void Nmea_Task(void *arg)
{
callback = arg; // register callback
usb_serial_jtag_driver_config_t usb_serial_jtag_config =
{
.rx_buffer_size = NMEA_BUFFER_SIZE,
.tx_buffer_size = NMEA_BUFFER_SIZE,
};
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usb_serial_jtag_config));
while (1)
{
int len = usb_serial_jtag_read_bytes(data, (NMEA_BUFFER_SIZE - 1), 20 / portTICK_PERIOD_MS);
if (len)
{
// I used this function to be sure the chars are received!
//usb_serial_jtag_write_bytes((const char *)data, len, 20 / portTICK_PERIOD_MS);
data[len] = '\0';
callback(data);
}
vTaskDelay(1);
}
}
如果我在
stdin
上发送一些字符(使用 JTAG/USB uart),它不会执行回调(即该行不会打印在 stdout
上)。
void vATaskFunction( void *pvParameters )
{
for( ;; )
{
-- Task application code here. --
}
Tasks must not attempt to return from their implementing
function or otherwise exit. In newer FreeRTOS port
attempting to do so will result in an configASSERT() being
called if it is defined. If it is necessary for a task to
exit then have the task call vTaskDelete( NULL ) to ensure
its exit is clean. */
vTaskDelete( NULL );
}
我在您的任务代码中没有看到任何 vTaskDelete。
创建任务时使用 while 循环和 vTaskDelay 的目的是什么?