|
| 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 | +} |
0 commit comments