1 使用FreeRTOS二值信号量
#include <Arduino.h>
#define USE_MULTCORE 0
hw_timer_t * timer = NULL;
SemaphoreHandle_t Binary_Handler;
bool state = 0;
void TaskBlink(void *pvParameters)
{
(void) pvParameters;
bool ON = true;
pinMode(45, OUTPUT);
for (;;)
{
USBSerial.println("TaskBlink ask the semaphore\r\n");
xSemaphoreTake(Binary_Handler,portMAX_DELAY);
if (state == true)
{
digitalWrite(45, LOW);
USBSerial.println("TaskBlink OFF\r\n");
}
else
{
USBSerial.println("TaskBlink ON\r\n");
digitalWrite(45, HIGH);
}
state = !state;
}
}
void app_main(void)
{
int i;
Binary_Handler = xSemaphoreCreateBinary();
if (Binary_Handler == NULL)
{
USBSerial.println("Semaphore Binary Creat Failed!!!\r\n");
}
xSemaphoreGive(Binary_Handler);
xTaskCreatePinnedToCore(
TaskBlink
, "TaskBlink" // A name just for humans
, 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, ARDUINO_RUNNING_CORE);
USBSerial.println("Tasks launched ...");
}
void TC4_Handler(void)
{
BaseType_t err;
err = xSemaphoreGive(Binary_Handler);
if (err != pdTRUE)
{
printf("信号量发送失败!\r\n");
}
}
void ARDUINO_ISR_ATTR onTimer(){
TC4_Handler();
}
void setup()
{
USBSerial.begin(115200);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 5000000, true);
timerAlarmEnable(timer);
delay(10);
app_main();
}
void loop()
{
}
2 使用FreeRTOS计数信号量
#include <Arduino.h>
#define USE_MULTCORE 0
hw_timer_t * timer = NULL;
SemaphoreHandle_t semaphoreHandle;
bool state = 0;
void carInTask(void *pvParam) {
int emptySpace = 0;
BaseType_t iResult;
while (1) {
emptySpace = uxSemaphoreGetCount(semaphoreHandle);
USBSerial.printf("emptySpace = %d\n", emptySpace);
iResult = xSemaphoreTake(semaphoreHandle,0);
if (iResult == pdPASS) {
USBSerial.printf("one car in\n");
} else {
USBSerial.printf("No space\n");
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void carOutTask(void *pvParam) {
while (1) {
vTaskDelay(pdMS_TO_TICKS(6000));
xSemaphoreGive(semaphoreHandle);
USBSerial.printf("one car out\n");
}
}
void TaskBlink(void *pvParameters)
{
(void) pvParameters;
bool ON = true;
pinMode(48, OUTPUT);
for (;;)
{
if (state == true)
{
digitalWrite(48, LOW);
}
else
{
digitalWrite(48, HIGH);
}
state = !state;
vTaskDelay(pdMS_TO_TICKS(6000));
}
}
void app_main(void)
{
int i;
semaphoreHandle = xSemaphoreCreateCounting(5, 5);
if (semaphoreHandle == NULL)
{
USBSerial.println("xSemaphoreCreateCounting Failed!!!\r\n");
}
xTaskCreatePinnedToCore(
TaskBlink
, "TaskBlink" // A name just for humans
, 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, ARDUINO_RUNNING_CORE);
xTaskCreate(
carInTask, /* Task function. */
"carInTask", /* String with name of task. */
4096, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
3, /* Priority of the task.(configMAX_PRIORITIES - 1 being the highest, and 0 being the lowest.) */
NULL);
USBSerial.println("Tasks launched ...");
}
void TC4_Handler(void)
{
BaseType_t err;
err = xSemaphoreGive(semaphoreHandle);
USBSerial.printf("one car out sucess\n");
if (err != pdTRUE)
{
USBSerial.printf("one car out Fail !\r\n");
}
}
void ARDUINO_ISR_ATTR onTimer(){
TC4_Handler();
}
void setup()
{
USBSerial.begin(115200);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 5000000, true);
timerAlarmEnable(timer);
delay(10);
app_main();
}
void loop()
{
}
3 使用FreeRTOS互斥量
#include <Arduino.h>
#define USE_MULTCORE 0
hw_timer_t * timer = NULL;
#define LOW_TASK_PRIO 2
#define LOW_STACK_SIZE 4096
TaskHandle_t LowTask_Handler;
void low_task(void *pvParameters);
#define MIDDLE_TASK_PRIO 3
#define MIDDLE_STACK_SIZE 2048
TaskHandle_t MiddleTask_Handler;
void middle_task(void *pvParameters);
#define HIGH_TASK_PRIO 4
#define HIGH_STACK_SIZE 2048
TaskHandle_t HighTask_Handler;
void high_task(void *pvParameters);
SemaphoreHandle_t mutex_Handler;
bool state = 0;
void low_task(void* pvParameters)
{
long i;
while(1)
{
USBSerial.printf("low task running\r\n");
xSemaphoreTake(mutex_Handler,portMAX_DELAY);
for (i=0; i<22222222; i++);
xSemaphoreGive(mutex_Handler);
USBSerial.printf("low task give semaphore\r\n");
vTaskDelay(1000);
}
}
void middle_task(void* pvParameters)
{
while(1)
{
USBSerial.printf("middle task running\r\n");
vTaskDelay(1000);
}
}
void high_task(void* pvParameters)
{
while(1)
{
USBSerial.printf("high task ask the semaphore\r\n");
xSemaphoreTake(mutex_Handler,portMAX_DELAY);
USBSerial.printf("high task running\r\n");
xSemaphoreGive(mutex_Handler);
vTaskDelay(1000);
}
}
void TaskBlink(void *pvParameters)
{
(void) pvParameters;
bool ON = true;
pinMode(48, OUTPUT);
for (;;)
{
if (state == true)
{
digitalWrite(48, LOW);
}
else
{
digitalWrite(48, HIGH);
}
state = !state;
vTaskDelay(pdMS_TO_TICKS(6000));
}
}
void app_main(void)
{
int i;
mutex_Handler = xSemaphoreCreateMutex();
if (mutex_Handler == NULL)
{
USBSerial.println("xSemaphoreCreateCounting Failed!!!\r\n");
}
xSemaphoreGive(mutex_Handler);
xTaskCreatePinnedToCore(
TaskBlink
, "TaskBlink" // A name just for humans
, 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 beinWg the lowest.
, NULL
, ARDUINO_RUNNING_CORE);
xTaskCreate((TaskFunction_t )high_task,
(const char* )"high_task",
(uint16_t )HIGH_STACK_SIZE,
(void* )NULL,
(UBaseType_t )HIGH_TASK_PRIO,
(TaskHandle_t* )&HighTask_Handler);
xTaskCreate((TaskFunction_t )middle_task,
(const char* )"middle_task",
(uint16_t )MIDDLE_STACK_SIZE,
(void* )NULL,
(UBaseType_t )MIDDLE_TASK_PRIO,
(TaskHandle_t* )&MiddleTask_Handler);
xTaskCreate((TaskFunction_t )low_task,
(const char* )"low_task",
(uint16_t )LOW_STACK_SIZE,
(void* )NULL,
(UBaseType_t )LOW_TASK_PRIO,
(TaskHandle_t* )&LowTask_Handler);
USBSerial.println("Tasks launched ...");
}
void TC4_Handler(void)
{
}
void ARDUINO_ISR_ATTR onTimer(){
TC4_Handler();
}
void setup()
{
USBSerial.begin(115200);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 5000000, true);
timerAlarmEnable(timer);
delay(10);
app_main();
}
void loop()
{
}