Skip to content

Commit 16925e8

Browse files
committed
Added some examples
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 797d3cd commit 16925e8

File tree

4 files changed

+434
-0
lines changed

4 files changed

+434
-0
lines changed

examples/frBlink/frBlink.ino

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Example to demonstrate thread definition, semaphores, and thread sleep.
3+
*/
4+
#include <STM32FreeRTOS.h>
5+
6+
// Define the LED pin is attached
7+
const uint8_t LED_PIN = LED_BUILTIN;
8+
9+
// Declare a semaphore handle.
10+
SemaphoreHandle_t sem;
11+
//------------------------------------------------------------------------------
12+
/*
13+
* Thread 1, turn the LED off when signalled by thread 2.
14+
*/
15+
// Declare the thread function for thread 1.
16+
static void Thread1(void* arg) {
17+
UNUSED(arg);
18+
while (1) {
19+
20+
// Wait for signal from thread 2.
21+
xSemaphoreTake(sem, portMAX_DELAY);
22+
23+
// Turn LED off.
24+
digitalWrite(LED_PIN, LOW);
25+
}
26+
}
27+
//------------------------------------------------------------------------------
28+
/*
29+
* Thread 2, turn the LED on and signal thread 1 to turn the LED off.
30+
*/
31+
// Declare the thread function for thread 2.
32+
static void Thread2(void* arg) {
33+
UNUSED(arg);
34+
pinMode(LED_PIN, OUTPUT);
35+
36+
while (1) {
37+
// Turn LED on.
38+
digitalWrite(LED_PIN, HIGH);
39+
40+
// Sleep for 200 milliseconds.
41+
vTaskDelay((200L * configTICK_RATE_HZ) / 1000L);
42+
43+
// Signal thread 1 to turn LED off.
44+
xSemaphoreGive(sem);
45+
46+
// Sleep for 200 milliseconds.
47+
vTaskDelay((200L * configTICK_RATE_HZ) / 1000L);
48+
}
49+
}
50+
//------------------------------------------------------------------------------
51+
void setup() {
52+
portBASE_TYPE s1, s2;
53+
54+
Serial.begin(9600);
55+
56+
// initialize semaphore
57+
sem = xSemaphoreCreateCounting(1, 0);
58+
59+
// create task at priority two
60+
s1 = xTaskCreate(Thread1, NULL, configMINIMAL_STACK_SIZE, NULL, 2, NULL);
61+
62+
// create task at priority one
63+
s2 = xTaskCreate(Thread2, NULL, configMINIMAL_STACK_SIZE, NULL, 1, NULL);
64+
65+
// check for creation errors
66+
if (sem== NULL || s1 != pdPASS || s2 != pdPASS ) {
67+
Serial.println(F("Creation problem"));
68+
while(1);
69+
}
70+
71+
// start scheduler
72+
vTaskStartScheduler();
73+
Serial.println("Insufficient RAM");
74+
while(1);
75+
}
76+
77+
//------------------------------------------------------------------------------
78+
// WARNING idle loop has a very small stack (configMINIMAL_STACK_SIZE)
79+
// loop must never block
80+
void loop() {
81+
// Not used.
82+
}
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Simple demo of three threads
2+
// LED blink thread, print thread, and idle loop
3+
#include <STM32FreeRTOS.h>
4+
5+
const uint8_t LED_PIN = LED_BUILTIN;
6+
7+
volatile uint32_t count = 0;
8+
9+
// handle for blink task
10+
TaskHandle_t blink;
11+
12+
//------------------------------------------------------------------------------
13+
// high priority for blinking LED
14+
static void vLEDFlashTask(void *pvParameters) {
15+
pinMode(LED_PIN, OUTPUT);
16+
17+
// Flash led every 200 ms.
18+
for (;;) {
19+
// Turn LED on.
20+
digitalWrite(LED_PIN, HIGH);
21+
22+
// Sleep for 50 milliseconds.
23+
vTaskDelay((50L * configTICK_RATE_HZ) / 1000L);
24+
25+
// Turn LED off.
26+
digitalWrite(LED_PIN, LOW);
27+
28+
// Sleep for 150 milliseconds.
29+
vTaskDelay((150L * configTICK_RATE_HZ) / 1000L);
30+
}
31+
}
32+
//------------------------------------------------------------------------------
33+
static void vPrintTask(void *pvParameters) {
34+
while (1) {
35+
// Sleep for one second.
36+
vTaskDelay(configTICK_RATE_HZ);
37+
38+
// Print count for previous second.
39+
Serial.print(F("Count: "));
40+
Serial.print(count);
41+
42+
// Print unused stack for threads.
43+
Serial.print(F(", Unused Stack: "));
44+
Serial.print(uxTaskGetStackHighWaterMark(blink));
45+
Serial.print(' ');
46+
Serial.print(uxTaskGetStackHighWaterMark(0));
47+
Serial.print(' ');
48+
Serial.println(uxTaskGetStackHighWaterMark(xTaskGetIdleTaskHandle()));
49+
50+
// Zero count.
51+
count = 0;
52+
}
53+
}
54+
//------------------------------------------------------------------------------
55+
void setup() {
56+
Serial.begin(9600);
57+
// wait for Leonardo
58+
while(!Serial) {}
59+
60+
// create blink task
61+
xTaskCreate(vLEDFlashTask,
62+
"Task1",
63+
configMINIMAL_STACK_SIZE + 50,
64+
NULL,
65+
tskIDLE_PRIORITY + 2,
66+
&blink);
67+
68+
// create print task
69+
xTaskCreate(vPrintTask,
70+
"Task2",
71+
configMINIMAL_STACK_SIZE + 100,
72+
NULL,
73+
tskIDLE_PRIORITY + 1,
74+
NULL);
75+
76+
// start FreeRTOS
77+
vTaskStartScheduler();
78+
79+
// should never return
80+
Serial.println(F("Die"));
81+
while(1);
82+
}
83+
//------------------------------------------------------------------------------
84+
// WARNING idle loop has a very small stack (configMINIMAL_STACK_SIZE)
85+
// loop must never block
86+
void loop() {
87+
while(1) {
88+
// must insure increment is atomic
89+
// in case of context switch for print
90+
noInterrupts();
91+
count++;
92+
interrupts();
93+
}
94+
}

examples/frJitter/frJitter.ino

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// test of jitter in sleep for one tick
2+
//
3+
// idle loop prints min and max time between sleep calls
4+
//
5+
// Note: access to shared variables tmin and tmax is not atomic
6+
// so glitches are possible if context switch happens during idle loop
7+
// access to these variables.
8+
//
9+
#include <STM32FreeRTOS.h>
10+
11+
volatile uint16_t tmax = 0;
12+
volatile uint16_t tmin = 0XFFFF;
13+
//------------------------------------------------------------------------------
14+
static void vJitter(void *pvParameters) {
15+
// initialize tlast
16+
vTaskDelay(1);
17+
uint32_t tlast = micros();
18+
while (true) {
19+
vTaskDelay(1);
20+
// get wake time
21+
uint32_t tmp = micros();
22+
uint16_t diff = tmp - tlast;
23+
if (diff < tmin) tmin = diff;
24+
if (diff > tmax) tmax = diff;
25+
tlast = tmp;
26+
}
27+
}
28+
//------------------------------------------------------------------------------
29+
uint8_t np = 10;
30+
static void vPrintTask(void *pvParameters) {
31+
for (;;) {
32+
// delay one second
33+
vTaskDelay(configTICK_RATE_HZ);
34+
Serial.print(tmin);
35+
Serial.write(',');
36+
Serial.println(tmax);
37+
if (np++ >= 10) {
38+
np = 0;
39+
tmin = 0XFFFF;
40+
tmax = 0;
41+
Serial.write("clear\r\n");
42+
}
43+
}
44+
}
45+
//------------------------------------------------------------------------------
46+
void setup() {
47+
Serial.begin(9600);
48+
while (!Serial);
49+
delay(1000);
50+
51+
// create high priority thread
52+
xTaskCreate(vJitter,
53+
"Task1",
54+
configMINIMAL_STACK_SIZE,
55+
NULL,
56+
tskIDLE_PRIORITY + 2,
57+
NULL);
58+
59+
// create print task
60+
xTaskCreate(vPrintTask,
61+
"Task2",
62+
configMINIMAL_STACK_SIZE + 100,
63+
NULL,
64+
tskIDLE_PRIORITY + 1,
65+
NULL);
66+
67+
// start FreeRTOS
68+
vTaskStartScheduler();
69+
70+
// should never return
71+
Serial.println("Die");
72+
while(1);
73+
}
74+
//------------------------------------------------------------------------------
75+
void loop() {
76+
// Not used - idle loop has a very small, configMINIMAL_STACK_SIZE, stack
77+
// loop must never block
78+
}

0 commit comments

Comments
 (0)