Blinky.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* --------------------------------------------------------------------------
  2. * Copyright (c) 2013-2019 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: RTX example program
  20. *
  21. *---------------------------------------------------------------------------*/
  22. #include <stdio.h>
  23. #include "cmsis_os2.h" // ARM::CMSIS:RTOS2:Keil RTX5
  24. #include "RTE_Components.h"
  25. #include CMSIS_device_header
  26. osThreadId_t tid_phaseA; /* Thread id of thread: phase_a */
  27. osThreadId_t tid_phaseB; /* Thread id of thread: phase_b */
  28. osThreadId_t tid_phaseC; /* Thread id of thread: phase_c */
  29. osThreadId_t tid_phaseD; /* Thread id of thread: phase_d */
  30. osThreadId_t tid_clock; /* Thread id of thread: clock */
  31. struct phases_t {
  32. int_fast8_t phaseA;
  33. int_fast8_t phaseB;
  34. int_fast8_t phaseC;
  35. int_fast8_t phaseD;
  36. } g_phases;
  37. /*----------------------------------------------------------------------------
  38. * Switch LED on
  39. *---------------------------------------------------------------------------*/
  40. void Switch_On (unsigned char led) {
  41. printf("LED On: #%d\n\r", led);
  42. }
  43. /*----------------------------------------------------------------------------
  44. * Switch LED off
  45. *---------------------------------------------------------------------------*/
  46. void Switch_Off (unsigned char led) {
  47. printf("LED Off: #%d\n\r", led);
  48. }
  49. /*----------------------------------------------------------------------------
  50. * Function 'signal_func' called from multiple threads
  51. *---------------------------------------------------------------------------*/
  52. void signal_func (osThreadId_t tid) {
  53. osThreadFlagsSet(tid_clock, 0x0100); /* set signal to clock thread */
  54. osDelay(500); /* delay 500ms */
  55. osThreadFlagsSet(tid_clock, 0x0100); /* set signal to clock thread */
  56. osDelay(500); /* delay 500ms */
  57. osThreadFlagsSet(tid, 0x0001); /* set signal to thread 'thread' */
  58. osDelay(500); /* delay 500ms */
  59. }
  60. /*----------------------------------------------------------------------------
  61. * Thread 1 'phaseA': Phase A output
  62. *---------------------------------------------------------------------------*/
  63. void phaseA (void *argument) {
  64. for (;;) {
  65. osThreadFlagsWait(0x0001, osFlagsWaitAny ,osWaitForever); /* wait for an event flag 0x0001 */
  66. Switch_On(0);
  67. g_phases.phaseA = 1;
  68. signal_func(tid_phaseB); /* call common signal function */
  69. g_phases.phaseA = 0;
  70. Switch_Off(0);
  71. }
  72. }
  73. /*----------------------------------------------------------------------------
  74. * Thread 2 'phaseB': Phase B output
  75. *---------------------------------------------------------------------------*/
  76. void phaseB (void *argument) {
  77. for (;;) {
  78. osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
  79. Switch_On(1);
  80. g_phases.phaseB = 1;
  81. signal_func(tid_phaseC); /* call common signal function */
  82. g_phases.phaseB = 0;
  83. Switch_Off(1);
  84. }
  85. }
  86. /*----------------------------------------------------------------------------
  87. * Thread 3 'phaseC': Phase C output
  88. *---------------------------------------------------------------------------*/
  89. void phaseC (void *argument) {
  90. for (;;) {
  91. osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
  92. Switch_On(2);
  93. g_phases.phaseC = 1;
  94. signal_func(tid_phaseD); /* call common signal function */
  95. g_phases.phaseC = 0;
  96. Switch_Off(2);
  97. }
  98. }
  99. /*----------------------------------------------------------------------------
  100. * Thread 4 'phaseD': Phase D output
  101. *---------------------------------------------------------------------------*/
  102. void phaseD (void *argument) {
  103. for (;;) {
  104. osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
  105. Switch_On(3);
  106. g_phases.phaseD = 1;
  107. signal_func(tid_phaseA); /* call common signal function */
  108. g_phases.phaseD = 0;
  109. Switch_Off(3);
  110. }
  111. }
  112. /*----------------------------------------------------------------------------
  113. * Thread 5 'clock': Signal Clock
  114. *---------------------------------------------------------------------------*/
  115. void clock (void *argument) {
  116. for (;;) {
  117. osThreadFlagsWait(0x0100, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0100 */
  118. osDelay(80); /* delay 80ms */
  119. }
  120. }
  121. /*----------------------------------------------------------------------------
  122. * Main: Initialize and start RTX Kernel
  123. *---------------------------------------------------------------------------*/
  124. void app_main (void *argument) {
  125. tid_phaseA = osThreadNew(phaseA, NULL, NULL);
  126. tid_phaseB = osThreadNew(phaseB, NULL, NULL);
  127. tid_phaseC = osThreadNew(phaseC, NULL, NULL);
  128. tid_phaseD = osThreadNew(phaseD, NULL, NULL);
  129. tid_clock = osThreadNew(clock, NULL, NULL);
  130. osThreadFlagsSet(tid_phaseA, 0x0001); /* set signal to phaseA thread */
  131. osDelay(osWaitForever);
  132. }
  133. int main (void) {
  134. // System Initialization
  135. SystemCoreClockUpdate();
  136. // ...
  137. osKernelInitialize(); // Initialize CMSIS-RTOS
  138. osThreadNew(app_main, NULL, NULL); // Create application main thread
  139. if (osKernelGetState() == osKernelReady) {
  140. osKernelStart(); // Start thread execution
  141. }
  142. while(1);
  143. }