Skip to content

Commit b7057f4

Browse files
committed
Add CMSIS-RTOSv2 examples
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 31cd9c2 commit b7057f4

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* --------------------------------------------------------------------------
2+
* Original code from:
3+
* https://github.com/ARM-software/CMSIS-FreeRTOS/tree/develop/CMSIS/RTOS2/FreeRTOS/Examples/Blinky
4+
*
5+
* Copyright (c) 2013-2017 ARM Limited. All rights reserved.
6+
*
7+
* SPDX-License-Identifier: Apache-2.0
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the License); you may
10+
* not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
* Original name: Blinky.c
22+
* OriginalPurpose: RTOS2 example program
23+
************************************************
24+
* Abstract.txt:
25+
* The Blinky project is a simple CMSIS RTOS2 Kernel based example
26+
* for a simulated Cortex-M3 device.
27+
* The example simulates the step-motor driver. Four phase variables are
28+
* simulating the activation of the four output driver stages. The state
29+
* changes are output on the Watch window variable g_phases:
30+
* - phase A
31+
* - phase B
32+
* - phase C
33+
* - phase D
34+
*
35+
* This example simulates Half step driver mode and CW rotation direction.
36+
*
37+
* The Blinky example program is available for one target:
38+
*
39+
* Simulation: configured for a simulated on-chip Flash
40+
************************************************
41+
* Updated to STM32FreeRTOS example
42+
*---------------------------------------------------------------------------*/
43+
44+
#include <STM32FreeRTOS.h>
45+
46+
osThreadId_t tid_phaseA; /* Thread id of thread: phase_a */
47+
osThreadId_t tid_phaseB; /* Thread id of thread: phase_b */
48+
osThreadId_t tid_phaseC; /* Thread id of thread: phase_c */
49+
osThreadId_t tid_phaseD; /* Thread id of thread: phase_d */
50+
osThreadId_t tid_clock; /* Thread id of thread: clock */
51+
52+
struct phases_t {
53+
int_fast8_t phaseA;
54+
int_fast8_t phaseB;
55+
int_fast8_t phaseC;
56+
int_fast8_t phaseD;
57+
} g_phases;
58+
59+
60+
/*----------------------------------------------------------------------------
61+
* Function 'signal_func' called from multiple threads
62+
*---------------------------------------------------------------------------*/
63+
void signal_func (osThreadId_t tid) {
64+
osThreadFlagsSet(tid_clock, 0x0100); /* set signal to clock thread */
65+
osDelay(500); /* delay 500ms */
66+
osThreadFlagsSet(tid_clock, 0x0100); /* set signal to clock thread */
67+
osDelay(500); /* delay 500ms */
68+
osThreadFlagsSet(tid, 0x0001); /* set signal to thread 'thread' */
69+
osDelay(500); /* delay 500ms */
70+
}
71+
72+
/*----------------------------------------------------------------------------
73+
* Thread 1 'phaseA': Phase A output
74+
*---------------------------------------------------------------------------*/
75+
void phaseA (void */*argument*/) {
76+
for (;;) {
77+
osThreadFlagsWait(0x0001, osFlagsWaitAny ,osWaitForever); /* wait for an event flag 0x0001 */
78+
g_phases.phaseA = 1;
79+
digitalToggle(LED_BUILTIN);
80+
signal_func(tid_phaseB); /* call common signal function */
81+
g_phases.phaseA = 0;
82+
}
83+
}
84+
85+
/*----------------------------------------------------------------------------
86+
* Thread 2 'phaseB': Phase B output
87+
*---------------------------------------------------------------------------*/
88+
void phaseB (void */*argument*/) {
89+
for (;;) {
90+
osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
91+
g_phases.phaseB = 1;
92+
digitalToggle(LED_BUILTIN);
93+
signal_func(tid_phaseC); /* call common signal function */
94+
g_phases.phaseB = 0;
95+
}
96+
}
97+
98+
/*----------------------------------------------------------------------------
99+
* Thread 3 'phaseC': Phase C output
100+
*---------------------------------------------------------------------------*/
101+
void phaseC (void */*argument*/) {
102+
for (;;) {
103+
osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
104+
g_phases.phaseC = 1;
105+
digitalToggle(LED_BUILTIN);
106+
signal_func(tid_phaseD); /* call common signal function */
107+
g_phases.phaseC = 0;
108+
}
109+
}
110+
111+
/*----------------------------------------------------------------------------
112+
* Thread 4 'phaseD': Phase D output
113+
*---------------------------------------------------------------------------*/
114+
void phaseD (void */*argument*/) {
115+
for (;;) {
116+
osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
117+
g_phases.phaseD = 1;
118+
digitalToggle(LED_BUILTIN);
119+
signal_func(tid_phaseA); /* call common signal function */
120+
g_phases.phaseD = 0;
121+
}
122+
}
123+
124+
/*----------------------------------------------------------------------------
125+
* Thread 5 'clock': Signal Clock
126+
*---------------------------------------------------------------------------*/
127+
void clock (void */*argument*/) {
128+
for (;;) {
129+
osThreadFlagsWait(0x0100, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0100 */
130+
osDelay(80); /* delay 80ms */
131+
}
132+
}
133+
134+
/*----------------------------------------------------------------------------
135+
* Main: Initialize and start the application
136+
*---------------------------------------------------------------------------*/
137+
void app_main (void */*argument*/) {
138+
139+
tid_phaseA = osThreadNew(phaseA, NULL, NULL);
140+
tid_phaseB = osThreadNew(phaseB, NULL, NULL);
141+
tid_phaseC = osThreadNew(phaseC, NULL, NULL);
142+
tid_phaseD = osThreadNew(phaseD, NULL, NULL);
143+
tid_clock = osThreadNew(clock, NULL, NULL);
144+
145+
osThreadFlagsSet(tid_phaseA, 0x0001); /* set signal to phaseA thread */
146+
147+
osDelay(osWaitForever);
148+
while(1);
149+
}
150+
151+
/*----------------------------------------------------------------------------
152+
* Main: Initialize and start the RTOS2 Kernel
153+
*---------------------------------------------------------------------------*/
154+
void setup() {
155+
pinMode(LED_BUILTIN, OUTPUT);
156+
157+
osKernelInitialize(); // Initialize CMSIS-RTOS
158+
osThreadNew(app_main, NULL, NULL); // Create application main thread
159+
if (osKernelGetState() == osKernelReady) {
160+
osKernelStart(); // Start thread execution
161+
}
162+
163+
while(1);
164+
}
165+
166+
//------------------------------------------------------------------------------
167+
// loop must never block
168+
void loop() {
169+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define configUSE_CMSIS_RTOS_V2 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define configUSE_CMSIS_RTOS_V2 1
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* --------------------------------------------------------------------------
2+
Simple LED blink example using CMS-RTOSv2
3+
---------------------------------------------------------------------------*/
4+
5+
#include <STM32FreeRTOS.h>
6+
7+
osThreadId_t tid_ledOn; /* Thread id of thread: ledOn */
8+
osThreadId_t tid_ledOff; /* Thread id of thread: ledOff */
9+
10+
//Thread 1
11+
void ledOn(void */*argument*/) {
12+
for (;;) {
13+
digitalToggle(LED_BUILTIN);
14+
osThreadFlagsSet(tid_ledOff, 0x0001);
15+
//signal ledOffthread
16+
osDelay(500);
17+
}
18+
}
19+
20+
// Thread 2
21+
void ledOff(void */*argument*/) {
22+
for (;;) {
23+
// wait for signal from ledOnthread
24+
osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever) ;
25+
osDelay(1000);
26+
digitalToggle(LED_BUILTIN);
27+
}
28+
}
29+
30+
/*----------------------------------------------------------------------------
31+
Main: Initialize and start the application
32+
---------------------------------------------------------------------------*/
33+
void app_main (void */*argument*/) {
34+
35+
tid_ledOn = osThreadNew(ledOn, NULL, NULL);
36+
tid_ledOff = osThreadNew(ledOff, NULL, NULL);
37+
38+
osDelay(osWaitForever);
39+
while (1);
40+
}
41+
42+
/*----------------------------------------------------------------------------
43+
Main: Initialize and start the RTOS2 Kernel
44+
---------------------------------------------------------------------------*/
45+
void setup() {
46+
pinMode(LED_BUILTIN, OUTPUT);
47+
48+
osKernelInitialize(); // Initialize CMSIS-RTOS
49+
osThreadNew(app_main, NULL, NULL); // Create application main thread
50+
if (osKernelGetState() == osKernelReady) {
51+
osKernelStart(); // Start thread execution
52+
}
53+
54+
while (1);
55+
}
56+
57+
//------------------------------------------------------------------------------
58+
// loop must never block
59+
void loop() {
60+
}

0 commit comments

Comments
 (0)