main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: 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. void app_main (void *argument);
  27. void app_msg (void *argument);
  28. typedef struct msg_s {
  29. uint8_t cmd;
  30. uint8_t len;
  31. uint8_t data[8];
  32. } msg_t;
  33. static osMessageQueueId_t msgQueue;
  34. static osMemoryPoolId_t memPool;
  35. static const osThreadAttr_t msgAttr = {
  36. .stack_size = 400U
  37. };
  38. /*----------------------------------------------------------------------------
  39. * Application main thread
  40. *---------------------------------------------------------------------------*/
  41. void app_main (void *argument) {
  42. (void)argument;
  43. osStatus_t status;
  44. uint32_t cnt = 0UL;
  45. msg_t* msg;
  46. while (1) {
  47. // Allocate memory for the message
  48. msg = osMemoryPoolAlloc(memPool, osWaitForever);
  49. if (msg == NULL) {
  50. printf("app_msg: osMemoryPoolAlloc failed.\n");
  51. continue;
  52. }
  53. // Produce a new message and put it to the queue
  54. ++cnt;
  55. msg->cmd = 1U;
  56. msg->len = 4U;
  57. *((uint32_t*)(msg->data)) = cnt;
  58. status = osMessageQueuePut(msgQueue, &msg, 0U, osWaitForever);
  59. if (status != osOK) {
  60. printf("app_main: osMessageQueuePut failed.\n");
  61. }
  62. // Defer message creation
  63. osDelay(osMessageQueueGetCount(msgQueue)*100U);
  64. }
  65. }
  66. /*----------------------------------------------------------------------------
  67. * Application message receiver thread
  68. *---------------------------------------------------------------------------*/
  69. void app_msg (void *argument) {
  70. (void)argument;
  71. osStatus_t status;
  72. uint32_t cnt;
  73. msg_t* msg;
  74. while (1) {
  75. // Defer message processing
  76. osDelay(osMessageQueueGetSpace(msgQueue)*100U);
  77. // Wait forever until a message could be received
  78. status = osMessageQueueGet(msgQueue, &msg, NULL, osWaitForever);
  79. if (status != osOK) {
  80. printf("app_msg: osMessageQueueGet failed.\n");
  81. } else {
  82. if (msg->len == 4U) {
  83. cnt = *((uint32_t*)(msg->data));
  84. }
  85. printf("app_msg: received [cmd = %d, data = 0x%0X]\n", msg->cmd, cnt);
  86. // Free memory of the message
  87. status = osMemoryPoolFree(memPool, msg);
  88. if (status != osOK) {
  89. printf("app_msg: osMemoryPoolFree failed.\n");
  90. }
  91. }
  92. }
  93. }
  94. /*----------------------------------------------------------------------------
  95. * Main entry
  96. *---------------------------------------------------------------------------*/
  97. int main (void) {
  98. // System Initialization
  99. SystemCoreClockUpdate();
  100. osKernelInitialize(); // Initialize CMSIS-RTOS
  101. osThreadNew(app_main, NULL, NULL); // Create application main thread
  102. osThreadNew(app_msg, NULL, &msgAttr); // Create message receiver thread
  103. // Create message queue used to pass pointers to msg_t
  104. msgQueue = osMessageQueueNew(10U, sizeof(msg_t*), NULL);
  105. // Create memory pool for actual message objects
  106. memPool = osMemoryPoolNew(10U, sizeof(msg_t), NULL);
  107. osKernelStart(); // Start thread execution
  108. for (;;) {}
  109. }