test_queue_static.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo: queue
  13. *
  14. * This demo demonstrates statically creating a queue and using it to send data between two tasks.
  15. *
  16. */
  17. #include <FreeRTOS.h>
  18. #include <queue.h>
  19. #include <task.h>
  20. #define TASK_PRIORITY (FINSH_THREAD_PRIORITY + 1)
  21. #define QUEUE_LENGTH 10
  22. #define ITEM_SIZE sizeof( uint32_t )
  23. /* queue handler */
  24. static QueueHandle_t xQueue = NULL;
  25. static StaticQueue_t xQueueBuffer;
  26. static uint8_t ucQueueStorage[ QUEUE_BUFFER_SIZE( QUEUE_LENGTH, ITEM_SIZE) ];
  27. static TaskHandle_t TaskHandle1 = NULL;
  28. static TaskHandle_t TaskHandle2 = NULL;
  29. static void vTask1Code(void *pvParameters)
  30. {
  31. BaseType_t xReturn;
  32. uint32_t num = 0;
  33. while (1)
  34. {
  35. xReturn = xQueueReceive(xQueue, &num, portMAX_DELAY);
  36. if (xReturn != pdPASS)
  37. {
  38. rt_kprintf("Task 1 receive from queue failed\n");
  39. }
  40. else
  41. {
  42. rt_kprintf("Task 1 receive data %d from queue\n", num);
  43. }
  44. if (num >= 100)
  45. {
  46. return;
  47. }
  48. }
  49. }
  50. static void vTask2Code(void * pvParameters)
  51. {
  52. BaseType_t xReturn;
  53. uint32_t num = 0;
  54. while (1)
  55. {
  56. xReturn = xQueueSendToBack(xQueue, &num, 0);
  57. if (xReturn != pdPASS)
  58. {
  59. rt_kprintf("Task 2 send to queue failed\n");
  60. }
  61. num += 1;
  62. if (num >= 100)
  63. {
  64. return;
  65. }
  66. }
  67. }
  68. int queue_static(void)
  69. {
  70. /* Create a queue statically */
  71. xQueue = xQueueCreateStatic( QUEUE_LENGTH, ITEM_SIZE, &( ucQueueStorage[ 0 ] ), &xQueueBuffer);
  72. if (xQueue == NULL)
  73. {
  74. rt_kprintf("create static queue failed.\n");
  75. return -1;
  76. }
  77. xTaskCreate( vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY + 1, &TaskHandle1 );
  78. if (TaskHandle1 == NULL)
  79. {
  80. rt_kprintf("Create task 1 failed\n");
  81. return -1;
  82. }
  83. xTaskCreate( vTask2Code, "Task2", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle2 );
  84. if (TaskHandle2 == NULL)
  85. {
  86. rt_kprintf("Create task 2 failed\n");
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. MSH_CMD_EXPORT(queue_static, queue sample);