main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 const osThreadAttr_t msgAttr = {
  35. .stack_size = 400U
  36. };
  37. /*----------------------------------------------------------------------------
  38. * Application main thread
  39. *---------------------------------------------------------------------------*/
  40. void app_main (void *argument) {
  41. (void)argument;
  42. osStatus_t status;
  43. uint32_t cnt = 0UL;
  44. msg_t msg = {
  45. .cmd = 1U,
  46. .len = 4U,
  47. .data = { 0U }
  48. };
  49. while (1) {
  50. // Produce a new message and put it to the queue
  51. ++cnt;
  52. *((uint32_t*)msg.data) = cnt;
  53. status = osMessageQueuePut(msgQueue, &msg, 0U, osWaitForever);
  54. if (status != osOK) {
  55. printf("app_main: osMessageQueuePut failed.\n");
  56. }
  57. // Defer message creation
  58. osDelay(osMessageQueueGetCount(msgQueue)*100U);
  59. }
  60. }
  61. /*----------------------------------------------------------------------------
  62. * Application message receiver thread
  63. *---------------------------------------------------------------------------*/
  64. void app_msg (void *argument) {
  65. (void)argument;
  66. osStatus_t status;
  67. uint32_t cnt;
  68. msg_t msg;
  69. while (1) {
  70. // Defer message processing
  71. osDelay(osMessageQueueGetSpace(msgQueue)*100U);
  72. // Wait forever until a message could be received
  73. status = osMessageQueueGet(msgQueue, &msg, NULL, osWaitForever);
  74. if (status != osOK) {
  75. printf("app_msg: osMessageQueueGet failed.\n");
  76. } else {
  77. if (msg.len == 4U) {
  78. cnt = *((uint32_t*)msg.data);
  79. }
  80. printf("app_msg: received [cmd = %d, data = 0x%0X]\n", msg.cmd, cnt);
  81. }
  82. }
  83. }
  84. /*----------------------------------------------------------------------------
  85. * Main entry
  86. *---------------------------------------------------------------------------*/
  87. int main (void) {
  88. // System Initialization
  89. SystemCoreClockUpdate();
  90. osKernelInitialize(); // Initialize CMSIS-RTOS
  91. osThreadNew(app_main, NULL, NULL); // Create application main thread
  92. osThreadNew(app_msg, NULL, &msgAttr); // Create message receiver thread
  93. // Create message queue for up to 10 messages of type msg_t
  94. msgQueue = osMessageQueueNew(10, sizeof(msg_t), NULL);
  95. osKernelStart(); // Start thread execution
  96. for (;;) {}
  97. }