Blinky.c 6.3 KB

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