Skip to content

Commit d961e81

Browse files
authored
Merge pull request #68 from hectorespert/task_utilities_example
Add task utilities example
2 parents dae9029 + 7d52f21 commit d961e81

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Example of FreeRTOS task utilities
3+
* https://www.freertos.org/a00021.html
4+
*/
5+
6+
// Include Arduino FreeRTOS library
7+
#include <Arduino_FreeRTOS.h>
8+
9+
/**
10+
* Task handlers
11+
* https://www.freertos.org/a00019.html#xTaskHandle
12+
*/
13+
TaskHandle_t taskBlinkHandle;
14+
15+
TaskHandle_t taskDeletedHandle;
16+
17+
TaskHandle_t taskBlockedHandle;
18+
19+
void setup() {
20+
21+
/**
22+
* Task creation
23+
*/
24+
xTaskCreate(TaskBlink, // Task function
25+
"Blink", // Task name
26+
128, // Stack size
27+
NULL,
28+
0, // Priority
29+
&taskBlinkHandle); // Task handler
30+
31+
xTaskCreate(TaskSerial,
32+
"Serial",
33+
128,
34+
NULL,
35+
2,
36+
NULL);
37+
38+
xTaskCreate(TaskDeleted,
39+
"Deleted",
40+
64,
41+
NULL,
42+
1,
43+
&taskDeletedHandle);
44+
45+
xTaskCreate(TaskBlocked,
46+
"Blocked",
47+
64,
48+
NULL,
49+
1,
50+
&taskBlockedHandle);
51+
52+
}
53+
54+
void loop() {}
55+
56+
/**
57+
* Example of utilities usage
58+
*/
59+
void TaskSerial(void *pvParameters)
60+
{
61+
(void) pvParameters;
62+
63+
Serial.begin(9600);
64+
65+
for (;;)
66+
{
67+
Serial.println("======== Tasks status ========");
68+
Serial.print("Tick count: ");
69+
Serial.print(xTaskGetTickCount());
70+
Serial.print(", Task count: ");
71+
Serial.print(uxTaskGetNumberOfTasks());
72+
73+
Serial.println();
74+
Serial.println();
75+
76+
// Serial task status
77+
Serial.print("- TASK ");
78+
Serial.print(pcTaskGetName(NULL)); // Get task name without handler https://www.freertos.org/a00021.html#pcTaskGetName
79+
Serial.print(", High Watermark: ");
80+
Serial.print(uxTaskGetStackHighWaterMark(NULL)); // https://www.freertos.org/uxTaskGetStackHighWaterMark.html
81+
82+
83+
TaskHandle_t taskSerialHandle = xTaskGetCurrentTaskHandle(); // Get current task handle. https://www.freertos.org/a00021.html#xTaskGetCurrentTaskHandle
84+
85+
Serial.println();
86+
87+
Serial.print("- TASK ");
88+
Serial.print(pcTaskGetName(taskBlinkHandle)); // Get task name with handler
89+
Serial.print(", High Watermark: ");
90+
Serial.print(uxTaskGetStackHighWaterMark(taskBlinkHandle));
91+
Serial.println();
92+
93+
Serial.print("- TASK ");
94+
Serial.print(pcTaskGetName(taskDeletedHandle));
95+
Serial.print(", High Watermark: ");
96+
Serial.print(uxTaskGetStackHighWaterMark(taskDeletedHandle));
97+
Serial.println();
98+
99+
Serial.print("- TASK ");
100+
Serial.print(pcTaskGetName(taskBlockedHandle));
101+
Serial.print(", High Watermark: ");
102+
Serial.print(uxTaskGetStackHighWaterMark(taskBlockedHandle));
103+
Serial.println();
104+
105+
Serial.println();
106+
107+
vTaskDelay( 5000 / portTICK_PERIOD_MS );
108+
}
109+
}
110+
111+
/**
112+
* Blocked tasks when run
113+
*/
114+
void TaskBlocked(void *pvParameters) {
115+
(void) pvParameters;
116+
for (;;)
117+
{
118+
vTaskDelay( 900000 / portTICK_PERIOD_MS );
119+
}
120+
}
121+
122+
/**
123+
* Deleted tasks when run
124+
*/
125+
void TaskDeleted(void *pvParameters) {
126+
(void) pvParameters;
127+
128+
vTaskDelete(NULL);
129+
}
130+
131+
/*
132+
* Blink task.
133+
* See Blink_AnalogRead example.
134+
*/
135+
void TaskBlink(void *pvParameters)
136+
{
137+
(void) pvParameters;
138+
139+
pinMode(LED_BUILTIN, OUTPUT);
140+
141+
for (;;)
142+
{
143+
digitalWrite(LED_BUILTIN, HIGH);
144+
vTaskDelay( 250 / portTICK_PERIOD_MS );
145+
digitalWrite(LED_BUILTIN, LOW);
146+
vTaskDelay( 250 / portTICK_PERIOD_MS );
147+
}
148+
}

0 commit comments

Comments
 (0)