Skip to content

Commit 19093fb

Browse files
committed
Fix: also execute loop function after vTaskStartScheduler.
1 parent 27d49d1 commit 19093fb

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

libraries/Arduino_FreeRTOS/examples/FreeRTOS-Blink/FreeRTOS-Blink.ino

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* GLOBAL VARIABLES
99
**************************************************************************************/
1010

11-
TaskHandle_t blinky_thread_hdl;
11+
TaskHandle_t loop_task, blinky_task;
1212

1313
/**************************************************************************************
1414
* SETUP/LOOP
@@ -19,37 +19,78 @@ void setup()
1919
Serial.begin(115200);
2020
while (!Serial) { }
2121

22-
/* Init RTOS tasks. */
23-
auto const rc = xTaskCreate
22+
pinMode(LEDR, OUTPUT);
23+
digitalWrite(LEDR, LOW);
24+
25+
/* Init a task that calls 'loop'
26+
* since after the call to
27+
* 'vTaskStartScheduler' we'll never
28+
* get out of setup() and therefore
29+
* would never get to loop(), as we
30+
* are leaving the default execution
31+
* flow.
32+
*/
33+
#if 1
34+
auto const rc_loop = xTaskCreate
35+
(
36+
loop_thread_func,
37+
static_cast<const char*>("Loop Thread"),
38+
512 / 4, /* usStackDepth in words */
39+
nullptr, /* pvParameters */
40+
1, /* uxPriority */
41+
&loop_task /* pxCreatedTask */
42+
);
43+
44+
if (rc_loop != pdPASS) {
45+
Serial.println("Failed to create 'loop' thread");
46+
return;
47+
}
48+
#endif
49+
#if 1
50+
auto const rc_blinky = xTaskCreate
2451
(
2552
blinky_thread_func,
2653
static_cast<const char*>("Blinky Thread"),
27-
512 / 4, /* usStackDepth in words */
28-
nullptr, /* pvParameters */
29-
1, /* uxPriority */
30-
&blinky_thread_hdl /* pxCreatedTask */
54+
512 / 4, /* usStackDepth in words */
55+
nullptr, /* pvParameters */
56+
1, /* uxPriority */
57+
&blinky_task /* pxCreatedTask */
3158
);
3259

33-
if (rc != pdPASS)
60+
if (rc_blinky != pdPASS) {
61+
Serial.println("Failed to create 'loop' thread");
3462
return;
35-
63+
}
64+
#endif
3665
Serial.println("Starting scheduler ...");
3766
/* Start the scheduler. */
38-
vTaskStartScheduler ();
67+
vTaskStartScheduler();
68+
/* We'll never get here. */
69+
for( ;; );
3970
}
4071

4172
void loop()
4273
{
74+
digitalWrite(LEDR, !digitalRead(LEDR));
75+
vTaskDelay(configTICK_RATE_HZ/4);
76+
}
4377

78+
void loop_thread_func(void *pvParameters)
79+
{
80+
for(;;)
81+
{
82+
loop();
83+
taskYIELD();
84+
}
4485
}
4586

4687
void blinky_thread_func(void *pvParameters)
4788
{
48-
/* blinky_thread.setup() */
89+
/* setup() */
4990
pinMode(LED_BUILTIN, OUTPUT);
5091
digitalWrite(LED_BUILTIN, LOW);
5192

52-
/* blinky_thread.loop() */
93+
/* loop() */
5394
for(;;)
5495
{
5596
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));

0 commit comments

Comments
 (0)