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