以下是 FreeRTOS 中互斥锁的基本使用方法:
-
引入头文件
在使用互斥锁之前,需要引入 FreeRTOS 的头文件。
#include “FreeRTOS.h”
#include “semphr.h”
2. 创建互斥锁
使用 xSemaphoreCreateMutex() 函数创建一个互斥锁。这个函数返回一个指向互斥锁的句柄。
声明
SemaphoreHandle_t mutex1;
BaseType_t xStatus1;
必须初始化
mutex1 = xSemaphoreCreateMutex();
使用的地方
#ifdef DBG_Task
xStatus1 = xSemaphoreTake(mutex1, portMAX_DELAY);
printf(“CalibU cal_index:%d,fQv=%0.4f\r\n”,cal_index,cal_flow[cal_index]);
printf(“CalibU fQv:%0.4f,fPv:0.4f,fT:%0.4f,fBP:%0.4f\r\n”,fQv,fPv,fT,fBP);
xSemaphoreGive(mutex1);
#endif