RTOS task 修改變量

XiotSamuel
Posts: 52
Joined: Sun Aug 29, 2021 1:50 pm

RTOS task 修改變量

Postby XiotSamuel » Thu Sep 30, 2021 3:26 am

如果RTOS TASK在運行之後可以修改變量嗎?我想用GOLBALVARIABLE 去設定RTOS TASK內的變量但好像不可以改變量的值。

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: RTOS task 修改變量

Postby ESP_Dazz » Sat Oct 09, 2021 7:27 am

FreeRTOS Task 只是一個函數。所以,在 task 內定義的變量的作用域/範圍限制在函數裏 (function scope). 要將數據傳給 task 有以下的方法
  • Task 函數直接使用全局變量。記得加臨界區 (critical section) 或互斥鎖 (mutex)
  • 使用 xTaskCreate() 函數的 void *arg 參數,在 task 初始化時傳數據給 task。也可以傳一個指針,指向一個全局變量或全局結構提 (Pointer to variable/struct)
  • 使用 FreeRTOS 提供的 Queue 函數將數據發給 task

XiotSamuel
Posts: 52
Joined: Sun Aug 29, 2021 1:50 pm

Re: RTOS task 修改變量

Postby XiotSamuel » Sat Oct 09, 2021 4:58 pm

謝。在網上找了一下。我發現這個EXAMPLE可以參考一下。

Code: Select all

 
TaskHandle_t Task0; 
TaskHandle_t Task1; 
QueueHandle_t queue; 
 
#define LED0 27 
#define LED1 25 
#define SWITCH 2 
 
void loop0(void * parameter) { 
for (;;) { 
 
// Add a random number to the queue 
// auto rndNumber = esp_random(); 
 
// Use Arduino implementation for a number between limits 
int rndNumber = random(1, 9); 
 
// Add to the queue - wait forever until space is available 
Serial.println("\t\t\t\t\tMgr 0 - Adding " + String(rndNumber) + " to queue"); 
xQueueSend(queue, &rndNumber, portMAX_DELAY); 
 
// Artificial wait here 
digitalWrite(LED0, HIGH); 
delay(200); 
digitalWrite(LED0, LOW); 
delay(500); 
 
// Slow motion delay here 
//delay(5000); 
 
// Force a tea-break here if switch is pressed 
bool TeaBreak = false; 
while (digitalRead(SWITCH) == LOW){ 
delay(100); 
if (!TeaBreak) { 
Serial.println("\t\t\t\t\tMgr 0 - Tea Break"); 
TeaBreak = true; 
} 
} 
} 
} 
 
void loop1(void * parameter) { 
for (;;) { 
// Get the number of flashes required 
int flashTotal; 
xQueueReceive(queue, &flashTotal, portMAX_DELAY); 
 
Serial.println("Worker - reading " + String(flashTotal)); 
 
// Flash that number 
for (int cnt = 0; cnt < flashTotal; cnt++) { 
digitalWrite(LED1, HIGH); 
delay(150); 
digitalWrite(LED1, LOW); 
delay(150); 
} 
 
// Slow motion delay here 
//delay(1000); 
} 
} 
 
void setup() 
{ 
Serial.begin(115200); 
Serial.println("Setup started."); 
 
pinMode(LED0, OUTPUT); 
pinMode(LED1, OUTPUT); 
pinMode(SWITCH, INPUT_PULLUP); 
 
// Create the queue with 5 slots of 2 bytes 
queue = xQueueCreate(5, sizeof(int)); 
 
xTaskCreatePinnedToCore( 
loop0, /* Function to implement the task */ 
"Task0", /* Name of the task */ 
1000, /* Stack size in words */ 
NULL, /* Task input parameter */ 
0, /* Priority of the task */ 
&Task0, /* Task handle. */ 
1); /* Core where the task should run */ 
 
xTaskCreatePinnedToCore( 
loop1, /* Function to implement the task */ 
"Task1", /* Name of the task */ 
1000, /* Stack size in words */ 
NULL, /* Task input parameter */ 
0, /* Priority of the task */ 
&Task1, /* Task handle. */ 
1); /* Core where the task should run */ 
 
Serial.println("Setup completed."); 
} 
 
void loop() 
{ 
vTaskDelete (NULL); 
} 

Who is online

Users browsing this forum: No registered users and 9 guests