]> begriffs open source - cmsis-freertos/blob - CMSIS/RTOS2/FreeRTOS/Examples/Native_Blinky/Blinky.c
Added Event Recorder configuration and enhanced debug capabilities
[cmsis-freertos] / CMSIS / RTOS2 / FreeRTOS / Examples / Native_Blinky / Blinky.c
1 /* -------------------------------------------------------------------------- 
2  * Copyright (c) 2013-2016 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: Example program
20  *
21  *---------------------------------------------------------------------------*/
22
23 #include <stdio.h>
24
25 #include "RTE_Components.h"             // Component selection
26 #include CMSIS_device_header
27
28 #include "FreeRTOS.h"                   // Keil::RTOS:FreeRTOS:Core
29 #include "task.h"                       // Keil::RTOS:FreeRTOS:Core
30
31 TaskHandle_t tid_phaseA;                /* Thread id of thread: phase_a      */
32 TaskHandle_t tid_phaseB;                /* Thread id of thread: phase_b      */
33 TaskHandle_t tid_phaseC;                /* Thread id of thread: phase_c      */
34 TaskHandle_t tid_phaseD;                /* Thread id of thread: phase_d      */
35 TaskHandle_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 (TaskHandle_t tid)  {
49   xTaskNotify (tid_clock, 0x0100, eSetBits);/* set signal to clock thread    */
50   vTaskDelay (500);                         /* delay 500ms                   */
51   xTaskNotify (tid_clock, 0x0100, eSetBits);/* set signal to clock thread    */
52   vTaskDelay (500);                         /* delay 500ms                   */
53   xTaskNotify (tid, 0x0001, eSetBits);      /* set signal to thread 'thread' */
54   vTaskDelay (500);                         /* delay 500ms                   */
55 }
56
57 /*----------------------------------------------------------------------------
58  *      Thread 1 'phaseA': Phase A output
59  *---------------------------------------------------------------------------*/
60 void phaseA (void *argument) {
61   for (;;) {
62     xTaskNotifyWait (0, 0x0001, NULL, portMAX_DELAY); /* wait for an thread flag */
63     g_phases.phaseA = 1;
64     signal_func(tid_phaseB);                          /* call 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     xTaskNotifyWait (0, 0x0001, NULL, portMAX_DELAY); /* wait for an thread flag */
75     g_phases.phaseB = 1;
76     signal_func(tid_phaseC);                          /* call 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     xTaskNotifyWait (0, 0x0001, NULL, portMAX_DELAY); /* wait for an thread flag */
87     g_phases.phaseC = 1;
88     signal_func(tid_phaseD);                          /* call 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     xTaskNotifyWait (0, 0x0001, NULL, portMAX_DELAY); /* wait for an thread flag */
99     g_phases.phaseD = 1;
100     signal_func(tid_phaseA);                          /* call 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     xTaskNotifyWait (0, 0x0100, NULL, portMAX_DELAY); /* wait for an thread flag */
111     vTaskDelay(80);                                   /* delay 80ms              */
112   }
113 }
114
115 /*----------------------------------------------------------------------------
116  *      Initialize and start threads
117  *---------------------------------------------------------------------------*/
118 void app_main (void *argument) {
119
120   xTaskCreate (phaseA, "PhaseA", 64, NULL, tskIDLE_PRIORITY+1, &tid_phaseA);
121   xTaskCreate (phaseB, "PhaseB", 64, NULL, tskIDLE_PRIORITY+1, &tid_phaseB);
122   xTaskCreate (phaseC, "PhaseC", 64, NULL, tskIDLE_PRIORITY+1, &tid_phaseC);
123   xTaskCreate (phaseD, "PhaseD", 64, NULL, tskIDLE_PRIORITY+1, &tid_phaseD);
124   xTaskCreate (clock,  "Clock",  64, NULL, tskIDLE_PRIORITY+1, &tid_clock);
125
126   xTaskNotify (tid_phaseA, 0x0001, eSetBits); /* set signal to phaseA thread  */
127
128   vTaskDelay (portMAX_DELAY);
129   while(1);
130 }
131
132 int main (void) {
133   SystemCoreClockUpdate();
134
135   xTaskCreate (app_main, "app_main", 64, NULL, tskIDLE_PRIORITY+1, NULL);
136
137   vTaskStartScheduler();
138
139   while(1);
140 }