forked from trimchess/portenta_basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportenta_basic_mbed_1.txt
115 lines (102 loc) · 3.14 KB
/
portenta_basic_mbed_1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <Arduino.h>
#include "mbed.h" // mbed 1.2.2
/*
* Simple example on how to synchronoze threads
* This code builds on portenta_basic_mbed_0.ino
* Board: Arduino Portenta H7 (M7 core)
*
* In this scetch, we expand the LED switching with some synchronisation
* blinkerThread1, immediatly after the start() switches its LED, then sleeps, wakes up,
* switches its LED OFF and - that is new! - starts the following thread
* and waits for an event from an other thread to restart.
* The thread is paused until it is triggered by its start event.
* When blinkerThread2 and blinkerThread3 starts, they just wait on their start event.
* This is the synchronisation between the two sketch versions. portenta_basic_mbed_0.ino and portenta_basic_mbed_1.ino
* You will see the difference when you observe the 3 LED!
* Synchronization is done with the mbed EventFlags Class https://os.mbed.com/docs/mbed-os/v6.2/apis/eventflags.html
* Create an EventFlags object
* EventFlags event_flags;
*
* A thread can wait until an other thread sets a trigger signal.
* event_flags.wait_all(1) This is a wait point until an other thread sets flag 1 of the 32 bit event flags
* To set a specific flag, you have to read the flags and then to mask your flag in the flags:
* int actFlag
* actFlag = event_flags.get();
* event_flags.set(actFlag | 1); // mask the desired flag in the flags
*
* and now you can wait until your flag is set from an other thread.
* event_flags.wait_all(4);
*
* Why has blinkerThread1 an other structure than the two other threads?
* If blinkerR() starts also with
* event_flags.wait_all();
* Then each thread wait after its start on an event of an other thread.
* But the other thread is also waiting for a trigger.
* Each thread waits for an other thread
* This ends in a classic deadlock situation.
* So, be carefull when you synchronize your threads!
*
* The loop function is unused
*
*
*/
using namespace mbed;
using namespace rtos;
DigitalOut LD1(PK_5); // red
DigitalOut LD2(PK_6); // green
DigitalOut LD3(PK_7); // blue
Thread blinkerThread1;
Thread blinkerThread2;
Thread blinkerThread3;
EventFlags event_flags;
void blinkerR()
{
while (true)
{
LD1 = !LD1; // red on
ThisThread::sleep_for(std::chrono::milliseconds(250));
LD1 = 1;
// read the flags
int actFlag = event_flags.get();
// mask the desired flag; it is the flag, blinkerThread3 waits on
event_flags.set(actFlag | 1);
// wait for the flag set by blinkerThread3
event_flags.wait_all(4);
}
}
void blinkerG()
{
while (true)
{
event_flags.wait_all(1);
LD2 = !LD2; // red on
ThisThread::sleep_for(std::chrono::milliseconds(500));
LD2 = 1;
int actFlag = event_flags.get();
event_flags.set(actFlag | 2);
}
}
void blinkerB()
{
while (true)
{
event_flags.wait_all(2);
LD3 = !LD3; // red on
ThisThread::sleep_for(std::chrono::milliseconds(1000));
LD3 = 1;
int actFlag = event_flags.get();
event_flags.set(actFlag | 4);
}
}
void setup()
{
LD1 = 1;
LD2 = 1;
LD3 = 1;
blinkerThread1.start(blinkerR);
blinkerThread2.start(blinkerG);
blinkerThread3.start(blinkerB);
}
void loop()
{
}