main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS 'main' function template
  3. *---------------------------------------------------------------------------*/
  4. #include "RTE_Components.h"
  5. #include CMSIS_device_header
  6. #include "cmsis_os2.h"
  7. #ifdef RTE_Compiler_EventRecorder
  8. #include "EventRecorder.h"
  9. #endif
  10. #include "stdio.h"
  11. int main (void);
  12. void app_main (void *argument);
  13. void app_msg (void *argument);
  14. typedef struct msg_s {
  15. uint8_t cmd;
  16. uint8_t len;
  17. uint8_t data[8];
  18. } msg_t;
  19. static osMessageQueueId_t msgQueue;
  20. static osMemoryPoolId_t memPool;
  21. static const osThreadAttr_t msgAttr = {
  22. .stack_size = 400U
  23. };
  24. /*----------------------------------------------------------------------------
  25. * Application main thread
  26. *---------------------------------------------------------------------------*/
  27. void app_main (void *argument) {
  28. (void)argument;
  29. msg_t* msg;
  30. uint32_t cnt = 0UL;
  31. osStatus_t status;
  32. while(1) {
  33. ++cnt;
  34. msg = osMemoryPoolAlloc(memPool, osWaitForever);
  35. if (msg == NULL) {
  36. printf("app_msg: osMemoryPoolAlloc failed.\n");
  37. continue;
  38. }
  39. msg->cmd = 1U;
  40. msg->len = 4U;
  41. *((uint32_t*)(msg->data)) = cnt;
  42. status = osMessageQueuePut(msgQueue, &msg, 0U, osWaitForever);
  43. if (status != osOK) {
  44. printf("app_main: osMessageQueuePut failed.\n");
  45. }
  46. status = osDelay(osMessageQueueGetCount(msgQueue)*100U);
  47. if (status != osOK) {
  48. printf("app_main: osDelay failed.\n");
  49. }
  50. }
  51. }
  52. void app_msg (void *argument) {
  53. (void)argument;
  54. uint32_t cnt;
  55. msg_t* msg;
  56. osStatus_t status;
  57. while(1) {
  58. status = osDelay(osMessageQueueGetSpace(msgQueue)*100U);
  59. if (status != osOK) {
  60. printf("app_msg: osDelay failed.\n");
  61. }
  62. msg = NULL;
  63. status = osMessageQueueGet(msgQueue, &msg, NULL, osWaitForever);
  64. if ((status != osOK) || (msg == NULL)) {
  65. printf("app_msg: osMessageQueueGet failed.\n");
  66. } else {
  67. if (msg->len == 4U) {
  68. cnt = *((uint32_t*)(msg->data));
  69. }
  70. printf("app_msg: received [cmd = %d, data = 0x%0X]\n", msg->cmd, cnt);
  71. status = osMemoryPoolFree(memPool, msg);
  72. if (status != osOK) {
  73. printf("app_msg: osMemoryPoolFree failed.\n");
  74. }
  75. }
  76. }
  77. }
  78. int main (void) {
  79. // System Initialization
  80. SystemCoreClockUpdate();
  81. #ifdef RTE_Compiler_EventRecorder
  82. // Initialize and start Event Recorder
  83. EventRecorderInitialize(EventRecordError, 1U);
  84. EventRecorderEnable (EventRecordAll, 0xFE, 0xFE);
  85. #endif
  86. osKernelInitialize(); // Initialize CMSIS-RTOS
  87. osThreadNew(app_main, NULL, NULL); // Create application main thread
  88. osThreadNew(app_msg, NULL, &msgAttr); // Create message receiver thread
  89. // Message queue used to pass pointers to msg_t
  90. msgQueue = osMessageQueueNew(10U, sizeof(msg_t*), NULL);
  91. // Memory pool for actual message objects
  92. memPool = osMemoryPoolNew(10U, sizeof(msg_t), NULL);
  93. osKernelStart(); // Start thread execution
  94. for (;;) {}
  95. }