main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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: main.c
  19. * Purpose: RTX example program
  20. *
  21. *---------------------------------------------------------------------------*/
  22. #include <stdio.h>
  23. #include "RTE_Components.h"
  24. #include CMSIS_device_header
  25. #include "cmsis_os2.h"
  26. #ifdef RTE_Compiler_EventRecorder
  27. #include "EventRecorder.h"
  28. #endif
  29. int main (void);
  30. void app_main (void *argument);
  31. void app_msg (void *argument);
  32. typedef struct msg_s {
  33. uint8_t cmd;
  34. uint8_t len;
  35. uint8_t data[8];
  36. } msg_t;
  37. static osMessageQueueId_t msgQueue;
  38. static const osThreadAttr_t msgAttr = {
  39. .stack_size = 400U
  40. };
  41. /*----------------------------------------------------------------------------
  42. * Application main thread
  43. *---------------------------------------------------------------------------*/
  44. void app_main (void *argument) {
  45. (void)argument;
  46. osStatus_t status;
  47. uint32_t cnt = 0UL;
  48. msg_t msg = {
  49. .cmd = 1U,
  50. .len = 4U,
  51. .data = { 0 }
  52. };
  53. while(1) {
  54. // Produce a new message and put it to the queue
  55. ++cnt;
  56. *((uint32_t*)msg.data) = cnt;
  57. status = osMessageQueuePut(msgQueue, &msg, 0U, osWaitForever);
  58. if (status != osOK) {
  59. printf("app_main: osMessageQueuePut failed.\n");
  60. }
  61. // Defer message creation
  62. status = osDelay(osMessageQueueGetCount(msgQueue)*100U);
  63. if (status != osOK) {
  64. printf("app_main: osDelay failed.\n");
  65. }
  66. }
  67. }
  68. /*----------------------------------------------------------------------------
  69. * Application message receiver thread
  70. *---------------------------------------------------------------------------*/
  71. void app_msg (void *argument) {
  72. (void)argument;
  73. osStatus_t status;
  74. uint32_t cnt;
  75. msg_t msg;
  76. while(1) {
  77. // Defer message processing
  78. status = osDelay(osMessageQueueGetSpace(msgQueue)*100U);
  79. if (status != osOK) {
  80. printf("app_msg: osDelay failed.\n");
  81. }
  82. // Wait forever until a message could be received
  83. status = osMessageQueueGet(msgQueue, &msg, NULL, osWaitForever);
  84. if (status != osOK) {
  85. printf("app_msg: osMessageQueuePut failed.\n");
  86. } else {
  87. if (msg.len == 4U) {
  88. cnt = *((uint32_t*)msg.data);
  89. }
  90. printf("app_msg: received [cmd = %d, data = 0x%0X]\n", msg.cmd, cnt);
  91. }
  92. }
  93. }
  94. /*----------------------------------------------------------------------------
  95. * Main entry
  96. *---------------------------------------------------------------------------*/
  97. int main (void) {
  98. // System Initialization
  99. SystemCoreClockUpdate();
  100. #ifdef RTE_Compiler_EventRecorder
  101. // Initialize and start Event Recorder
  102. EventRecorderInitialize(EventRecordError, 1U);
  103. EventRecorderEnable (EventRecordAll, 0xFE, 0xFE);
  104. #endif
  105. // ...
  106. osKernelInitialize(); // Initialize CMSIS-RTOS
  107. osThreadNew(app_main, NULL, NULL); // Create application main thread
  108. osThreadNew(app_msg, NULL, &msgAttr); // Create message receiver thread
  109. msgQueue = osMessageQueueNew(10, sizeof(msg_t), NULL); // Create message queue for up to 10 messages of type msg_t
  110. osKernelStart(); // Start thread execution
  111. for (;;) {}
  112. }