]> begriffs open source - cmsis-freertos/blob - CMSIS/RTOS2/FreeRTOS/Examples/Blinky/Blinky.c
Added Event Recorder configuration and enhanced debug capabilities
[cmsis-freertos] / CMSIS / RTOS2 / FreeRTOS / Examples / Blinky / Blinky.c
1 /* -------------------------------------------------------------------------- 
2  * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
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
9  *
10  * www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *      Name:    Blinky.c
19  *      Purpose: RTOS2 example program
20  *
21  *---------------------------------------------------------------------------*/
22
23 #include <stdio.h>
24
25 #include "RTE_Components.h"
26 #include CMSIS_device_header
27
28 #include "cmsis_os2.h"
29
30
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        */
36
37 struct phases_t {
38   int_fast8_t phaseA;
39   int_fast8_t phaseB;
40   int_fast8_t phaseC;
41   int_fast8_t phaseD;
42 } g_phases;
43
44
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                   */
55 }
56
57 /*----------------------------------------------------------------------------
58  *      Thread 1 'phaseA': Phase A output
59  *---------------------------------------------------------------------------*/
60 void phaseA (void *argument) {
61   for (;;) {
62     osThreadFlagsWait(0x0001, osFlagsWaitAny ,osWaitForever);   /* wait for an event flag 0x0001 */
63     g_phases.phaseA = 1;
64     signal_func(tid_phaseB);                                    /* call common signal function   */
65     g_phases.phaseA = 0;
66   }
67 }
68
69 /*----------------------------------------------------------------------------
70  *      Thread 2 'phaseB': Phase B output
71  *---------------------------------------------------------------------------*/
72 void phaseB (void *argument) {
73   for (;;) {
74     osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever);   /* wait for an event flag 0x0001 */
75     g_phases.phaseB = 1;
76     signal_func(tid_phaseC);                                    /* call common signal function   */
77     g_phases.phaseB = 0;
78   }
79 }
80
81 /*----------------------------------------------------------------------------
82  *      Thread 3 'phaseC': Phase C output
83  *---------------------------------------------------------------------------*/
84 void phaseC (void *argument) {
85   for (;;) {
86     osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever);   /* wait for an event flag 0x0001 */
87     g_phases.phaseC = 1;
88     signal_func(tid_phaseD);                                    /* call common signal function   */
89     g_phases.phaseC = 0;
90   }
91 }
92
93 /*----------------------------------------------------------------------------
94  *      Thread 4 'phaseD': Phase D output
95  *---------------------------------------------------------------------------*/
96 void phaseD (void *argument) {
97   for (;;) {
98     osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever);   /* wait for an event flag 0x0001 */
99     g_phases.phaseD = 1;
100     signal_func(tid_phaseA);                                    /* call common signal function   */
101     g_phases.phaseD = 0;
102   }
103 }
104
105 /*----------------------------------------------------------------------------
106  *      Thread 5 'clock': Signal Clock
107  *---------------------------------------------------------------------------*/
108 void clock (void *argument) {
109   for (;;) {
110     osThreadFlagsWait(0x0100, osFlagsWaitAny, osWaitForever);   /* wait for an event flag 0x0100 */
111     osDelay(80);                                                /* delay  80ms                   */
112   }
113 }
114
115 /*----------------------------------------------------------------------------
116  *      Main: Initialize and start the application
117  *---------------------------------------------------------------------------*/
118 void app_main (void *argument) {
119
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);
125
126   osThreadFlagsSet(tid_phaseA, 0x0001); /* set signal to phaseA thread   */
127
128   osDelay(osWaitForever);
129   while(1);
130 }
131
132 /*----------------------------------------------------------------------------
133  *      Main: Initialize and start the RTOS2 Kernel
134  *---------------------------------------------------------------------------*/
135 int main (void) {
136
137   // System Initialization
138   SystemCoreClockUpdate();
139
140   osKernelInitialize();                 // Initialize CMSIS-RTOS
141   osThreadNew(app_main, NULL, NULL);    // Create application main thread
142   if (osKernelGetState() == osKernelReady) {
143     osKernelStart();                    // Start thread execution
144   }
145
146   while(1);
147 }