Blinky.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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: RTX example program
  20. *
  21. *---------------------------------------------------------------------------*/
  22. #include <stdio.h>
  23. #include "ARMCM3.h" // Device header
  24. #include "cmsis_os.h" // ARM::CMSIS:RTOS:Keil RTX5
  25. #include "cmsis_os2.h" // ARM::CMSIS:RTOS2:Keil RTX5
  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. g_phases.phaseA = 1;
  67. signal_func(tid_phaseB); /* call common signal function */
  68. g_phases.phaseA = 0;
  69. }
  70. }
  71. /*----------------------------------------------------------------------------
  72. * Thread 2 'phaseB': Phase B output
  73. *---------------------------------------------------------------------------*/
  74. void phaseB (void *argument) {
  75. for (;;) {
  76. osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
  77. g_phases.phaseB = 1;
  78. signal_func(tid_phaseC); /* call common signal function */
  79. g_phases.phaseB = 0;
  80. }
  81. }
  82. /*----------------------------------------------------------------------------
  83. * Thread 3 'phaseC': Phase C output
  84. *---------------------------------------------------------------------------*/
  85. void phaseC (void *argument) {
  86. for (;;) {
  87. osThreadFlagsWait(0x0001, osFlagsWaitAny, osWaitForever); /* wait for an event flag 0x0001 */
  88. g_phases.phaseC = 1;
  89. signal_func(tid_phaseD); /* call common signal function */
  90. g_phases.phaseC = 0;
  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. * Thread 5 'clock': Signal Clock
  106. *---------------------------------------------------------------------------*/
  107. void clock (void const *argument) {
  108. for (;;) {
  109. osSignalWait(0x0100, osWaitForever); /* wait for an event flag 0x0100 */
  110. osDelay(80); /* delay 80ms */
  111. }
  112. }
  113. /* Define the API v1 thread */
  114. osThreadDef(clock, osPriorityNormal, 1, 0);
  115. /*----------------------------------------------------------------------------
  116. * Main: Initialize and start RTX Kernel
  117. *---------------------------------------------------------------------------*/
  118. void app_main (void *argument) {
  119. tid_phaseA = osThreadNew(phaseA, NULL, NULL);
  120. tid_phaseB = osThreadNew(phaseB, NULL, NULL);
  121. tid_phaseC = osThreadNew(phaseC, NULL, NULL);
  122. tid_phaseD = osThreadNew(phaseD, NULL, NULL);
  123. tid_clock = osThreadCreate(osThread(clock), NULL);
  124. osThreadFlagsSet(tid_phaseA, 0x0001); /* set signal to phaseA thread */
  125. osDelay(osWaitForever);
  126. while(1);
  127. }
  128. int main (void) {
  129. // System Initialization
  130. SystemCoreClockUpdate();
  131. // ...
  132. osKernelInitialize(); // Initialize CMSIS-RTOS
  133. osThreadNew(app_main, NULL, NULL); // Create application main thread
  134. if (osKernelGetState() == osKernelReady) {
  135. osKernelStart(); // Start thread execution
  136. }
  137. while(1);
  138. }