1 /* --------------------------------------------------------------------------
\r
2 * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
\r
4 * SPDX-License-Identifier: Apache-2.0
\r
6 * Licensed under the Apache License, Version 2.0 (the License); you may
\r
7 * not use this file except in compliance with the License.
\r
8 * You may obtain a copy of the License at
\r
10 * www.apache.org/licenses/LICENSE-2.0
\r
12 * Unless required by applicable law or agreed to in writing, software
\r
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
\r
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
15 * See the License for the specific language governing permissions and
\r
16 * limitations under the License.
\r
19 * Purpose: RTOS2 example program
\r
21 *---------------------------------------------------------------------------*/
\r
25 #include "RTE_Components.h"
\r
26 #include CMSIS_device_header
\r
28 #include "cmsis_os2.h"
\r
31 osThreadId_t tid_phaseA; /* Thread id of thread: phase_a */
\r
32 osThreadId_t tid_phaseB; /* Thread id of thread: phase_b */
\r
33 osThreadId_t tid_phaseC; /* Thread id of thread: phase_c */
\r
34 osThreadId_t tid_phaseD; /* Thread id of thread: phase_d */
\r
35 osThreadId_t tid_clock; /* Thread id of thread: clock */
\r
45 /*----------------------------------------------------------------------------
\r
46 * Function 'signal_func' called from multiple threads
\r
47 *---------------------------------------------------------------------------*/
\r
48 void signal_func (osThreadId_t tid) {
\r
49 osThreadFlagsSet(tid_clock, 0x0100); /* set signal to clock thread */
\r
50 osDelay(500); /* delay 500ms */
\r
51 osThreadFlagsSet(tid_clock, 0x0100); /* set signal to clock thread */
\r
52 osDelay(500); /* delay 500ms */
\r
53 osThreadFlagsSet(tid, 0x0001); /* set signal to thread 'thread' */
\r
54 osDelay(500); /* delay 500ms */
\r
57 /*----------------------------------------------------------------------------
\r
58 * Thread 1 'phaseA': Phase A output
\r
59 *---------------------------------------------------------------------------*/
\r
60 void phaseA (void *argument) {
\r
62 osThreadFlagsWait(0x0001, osFlagsWaitAny ,osWaitForever); /* wait for an event flag 0x0001 */
\r
63 g_phases.phaseA = 1;
\r
64 signal_func(tid_phaseB); /* call common signal function */
\r
65 g_phases.phaseA = 0;
\r
69 /*----------------------------------------------------------------------------
\r
70 * Thread 2 'phaseB': Phase B output
\r
71 *---------------------------------------------------------------------------*/
\r
72 void phaseB (void *argument) {
\r
74 osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
\r
75 g_phases.phaseB = 1;
\r
76 signal_func(tid_phaseC); /* call common signal function */
\r
77 g_phases.phaseB = 0;
\r
81 /*----------------------------------------------------------------------------
\r
82 * Thread 3 'phaseC': Phase C output
\r
83 *---------------------------------------------------------------------------*/
\r
84 void phaseC (void *argument) {
\r
86 osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
\r
87 g_phases.phaseC = 1;
\r
88 signal_func(tid_phaseD); /* call common signal function */
\r
89 g_phases.phaseC = 0;
\r
93 /*----------------------------------------------------------------------------
\r
94 * Thread 4 'phaseD': Phase D output
\r
95 *---------------------------------------------------------------------------*/
\r
96 void phaseD (void *argument) {
\r
98 osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
\r
99 g_phases.phaseD = 1;
\r
100 signal_func(tid_phaseA); /* call common signal function */
\r
101 g_phases.phaseD = 0;
\r
105 /*----------------------------------------------------------------------------
\r
106 * Thread 5 'clock': Signal Clock
\r
107 *---------------------------------------------------------------------------*/
\r
108 void clock (void *argument) {
\r
110 osThreadFlagsWait(0x0100, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0100 */
\r
111 osDelay(80); /* delay 80ms */
\r
115 /*----------------------------------------------------------------------------
\r
116 * Main: Initialize and start the application
\r
117 *---------------------------------------------------------------------------*/
\r
118 void app_main (void *argument) {
\r
120 tid_phaseA = osThreadNew(phaseA, NULL, NULL);
\r
121 tid_phaseB = osThreadNew(phaseB, NULL, NULL);
\r
122 tid_phaseC = osThreadNew(phaseC, NULL, NULL);
\r
123 tid_phaseD = osThreadNew(phaseD, NULL, NULL);
\r
124 tid_clock = osThreadNew(clock, NULL, NULL);
\r
126 osThreadFlagsSet(tid_phaseA, 0x0001); /* set signal to phaseA thread */
\r
128 osDelay(osWaitForever);
\r
132 /*----------------------------------------------------------------------------
\r
133 * Main: Initialize and start the RTOS2 Kernel
\r
134 *---------------------------------------------------------------------------*/
\r
137 // System Initialization
\r
138 SystemCoreClockUpdate();
\r
140 osKernelInitialize(); // Initialize CMSIS-RTOS
\r
141 osThreadNew(app_main, NULL, NULL); // Create application main thread
\r
142 if (osKernelGetState() == osKernelReady) {
\r
143 osKernelStart(); // Start thread execution
\r