任务创建
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
void taskA(void* param)
{
while(1)
{
ESP_LOGI("main", "Hello World!");
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void app_main(void)
{
xTaskCreatePinnedToCore(taskA, "HelloWorld", 2048, NULL, 3, NULL, 0);
}
队列
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "esp_log.h"
#include <string.h>
QueueHandle_t queueHandle = NULL;
typedef struct
{
int value;
}queue_data_t;
void taskA(void* param)
{
queue_data_t data;
while(1)
{
if (pdTRUE == xQueueReceive(queueHandle, &data, 100))
{
ESP_LOGI("queue", "receive queeu Value:%d", data.value);
}
}
}
void taskB(void* param)
{
queue_data_t data;
memset(&data, 0, sizeof(queue_data_t));
while(1)
{
xQueueSend(queueHandle, &data, 100);
data.value++;
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void app_main(void)
{
queueHandle = xQueueCreate(10, sizeof(queue_data_t));
xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, NULL, 0);
xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 3, NULL, 0);
}
信号量
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/gpio.h"
#include "esp_log.h"
SemaphoreHandle_t bin_sem;
void taskA(void* param)
{
while(1)
{
xSemaphoreGive(bin_sem);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void taskB(void* param)
{
while(1)
{
if (pdTRUE == xSemaphoreTake(bin_sem, portMAX_DELAY))
{
ESP_LOGI("bin", "task B take binsem success");
}
}
}
void app_main(void)
{
bin_sem = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, NULL, 0);
xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 4, NULL, 0);
}
互斥锁
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/gpio.h"
#include "esp_log.h"
SemaphoreHandle_t mutex;
int cnt = 0;
void taskA(void* param)
{
while(1)
{
vTaskDelay(pdMS_TO_TICKS(500));
xSemaphoreTake(mutex, portMAX_DELAY);
cnt++;
vTaskDelay(pdMS_TO_TICKS(10));
ESP_LOGI("taskA", "cnt:%d", cnt);
xSemaphoreGive(mutex);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void taskB(void* param)
{
while(1)
{
vTaskDelay(pdMS_TO_TICKS(500));
xSemaphoreTake(mutex, portMAX_DELAY);
cnt++;
vTaskDelay(pdMS_TO_TICKS(10));
ESP_LOGI("taskB", "cnt:%d", cnt);
xSemaphoreGive(mutex);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void app_main(void)
{
mutex = xSemaphoreCreateMutex();
xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, NULL, 0);
xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 3, NULL, 0);
}
事件组
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "esp_log.h"
#define NUM0_BIT BIT0
#define NUM1_BIT BIT1
static EventGroupHandle_t test_event;
void taskA(void* param)
{
while(1)
{
xEventGroupSetBits(test_event, NUM0_BIT);
vTaskDelay(pdMS_TO_TICKS(1000));
xEventGroupSetBits(test_event, NUM1_BIT);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void taskB(void* param)
{
EventBits_t ev;
while(1)
{
ev = xEventGroupWaitBits(test_event, NUM0_BIT | NUM1_BIT, pdTRUE, pdFALSE, pdMS_TO_TICKS(5000));
if (ev & NUM0_BIT)
{
ESP_LOGI("ev", "get BIT0 event");
}
if (ev & NUM1_BIT)
{
ESP_LOGI("ev", "get BIT1 event");
}
}
}
void app_main(void)
{
test_event = xEventGroupCreate();
xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, NULL, 0);
xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 3, NULL, 0);
}
直达任务通知
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "esp_log.h"
static TaskHandle_t taskAHandle;
static TaskHandle_t taskBHandle;
void taskA(void* param)
{
uint32_t value = 0;
while(1)
{
xTaskNotify(taskBHandle, value, eSetValueWithOverwrite);
vTaskDelay(pdMS_TO_TICKS(1000));
value++;
}
}
void taskB(void* param)
{
uint32_t value;
while(1)
{
xTaskNotifyWait(0x00, ULONG_MAX, &value, portMAX_DELAY);
ESP_LOGI("ev", "notify wait value:%lu", value);
}
}
void app_main(void)
{
xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 3, &taskBHandle, 0);
xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, &taskAHandle, 0);
}
