test_queue_dynamic.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 dynamically 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 TaskHandle_t TaskHandle1 = NULL;
  26. static TaskHandle_t TaskHandle2 = NULL;
  27. static void vTask1Code(void *pvParameters)
  28. {
  29. BaseType_t xReturn;
  30. uint32_t num = 0;
  31. while (1)
  32. {
  33. xReturn = xQueueReceive(xQueue, &num, portMAX_DELAY);
  34. if (xReturn != pdPASS)
  35. {
  36. rt_kprintf("Task 1 receive from queue failed\n");
  37. }
  38. else
  39. {
  40. rt_kprintf("Task 1 receive data %d from queue\n", num);
  41. }
  42. if (num >= 100)
  43. {
  44. return;
  45. }
  46. }
  47. }
  48. static void vTask2Code(void * pvParameters)
  49. {
  50. BaseType_t xReturn;
  51. uint32_t num = 0;
  52. while (1)
  53. {
  54. xReturn = xQueueSendToBack(xQueue, &num, 0);
  55. if (xReturn != pdPASS)
  56. {
  57. rt_kprintf("Task 2 send to queue failed\n");
  58. }
  59. num += 1;
  60. if (num >= 100)
  61. {
  62. return;
  63. }
  64. }
  65. }
  66. int queue_dynamic(void)
  67. {
  68. /* Create a queue dynamically */
  69. xQueue = xQueueCreate(QUEUE_LENGTH, ITEM_SIZE);
  70. if (xQueue == NULL)
  71. {
  72. rt_kprintf("create dynamic queue failed.\n");
  73. return -1;
  74. }
  75. xTaskCreate( vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY + 1, &TaskHandle1 );
  76. if (TaskHandle1 == NULL)
  77. {
  78. rt_kprintf("Create task 1 failed\n");
  79. return -1;
  80. }
  81. xTaskCreate( vTask2Code, "Task2", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle2 );
  82. if (TaskHandle2 == NULL)
  83. {
  84. rt_kprintf("Create task 2 failed\n");
  85. return -1;
  86. }
  87. return 0;
  88. }
  89. MSH_CMD_EXPORT(queue_dynamic, queue sample);