Skip to content

Commit be081ac

Browse files
lutfisanme-no-dev
authored andcommitted
Add FreeRTOS multi-thread example (#2067)
* Add FreeRTOS exsample * Update Code * Change stack size * Fix LED_BUILTIN not defined Fix LED_BUILTIN not defined
1 parent 4d3f6ca commit be081ac

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

Diff for: libraries/FreeRTOS/FreeRTOS.ino

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#if CONFIG_FREERTOS_UNICORE
2+
#define ARDUINO_RUNNING_CORE 0
3+
#else
4+
#define ARDUINO_RUNNING_CORE 1
5+
#endif
6+
7+
#ifndef LED_BUILTIN
8+
#define LED_BUILTIN 13
9+
#endif
10+
11+
// define two tasks for Blink & AnalogRead
12+
void TaskBlink( void *pvParameters );
13+
void TaskAnalogReadA3( void *pvParameters );
14+
15+
// the setup function runs once when you press reset or power the board
16+
void setup() {
17+
18+
// initialize serial communication at 115200 bits per second:
19+
Serial.begin(115200);
20+
21+
// Now set up two tasks to run independently.
22+
xTaskCreatePinnedToCore(
23+
TaskBlink
24+
, "TaskBlink" // A name just for humans
25+
, 1024 // This stack size can be checked & adjusted by reading the Stack Highwater
26+
, NULL
27+
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
28+
, NULL
29+
, ARDUINO_RUNNING_CORE);
30+
31+
xTaskCreatePinnedToCore(
32+
TaskAnalogReadA3
33+
, "AnalogReadA3"
34+
, 1024 // Stack size
35+
, NULL
36+
, 1 // Priority
37+
, NULL
38+
, ARDUINO_RUNNING_CORE);
39+
40+
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
41+
}
42+
43+
void loop()
44+
{
45+
// Empty. Things are done in Tasks.
46+
}
47+
48+
/*--------------------------------------------------*/
49+
/*---------------------- Tasks ---------------------*/
50+
/*--------------------------------------------------*/
51+
52+
void TaskBlink(void *pvParameters) // This is a task.
53+
{
54+
(void) pvParameters;
55+
56+
/*
57+
Blink
58+
Turns on an LED on for one second, then off for one second, repeatedly.
59+
60+
If you want to know what pin the on-board LED is connected to on your ESP32 model, check
61+
the Technical Specs of your board.
62+
*/
63+
64+
// initialize digital LED_BUILTIN on pin 13 as an output.
65+
pinMode(LED_BUILTIN, OUTPUT);
66+
67+
for (;;) // A Task shall never return or exit.
68+
{
69+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
70+
vTaskDelay(100); // one tick delay (15ms) in between reads for stability
71+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
72+
vTaskDelay(100); // one tick delay (15ms) in between reads for stability
73+
}
74+
}
75+
76+
void TaskAnalogReadA3(void *pvParameters) // This is a task.
77+
{
78+
(void) pvParameters;
79+
80+
/*
81+
AnalogReadSerial
82+
Reads an analog input on pin A3, prints the result to the serial monitor.
83+
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
84+
Attach the center pin of a potentiometer to pin A3, and the outside pins to +5V and ground.
85+
86+
This example code is in the public domain.
87+
*/
88+
89+
for (;;)
90+
{
91+
// read the input on analog pin A3:
92+
int sensorValueA3 = analogRead(A3);
93+
// print out the value you read:
94+
Serial.println(sensorValueA3);
95+
vTaskDelay(10); // one tick delay (15ms) in between reads for stability
96+
}
97+
}

0 commit comments

Comments
 (0)