test_semaphore_counting_static.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. */
  10. /*
  11. * Demo: semaphore
  12. * This demo creates one counting semaphore statically
  13. * It creates two tasks:
  14. * 1) task #1: take the semaphore until its value reaches 0
  15. * 2) task #2: give the semaphore until its value reaches maximum
  16. *
  17. */
  18. #include <FreeRTOS.h>
  19. #include <semphr.h>
  20. #include <task.h>
  21. #include <task.h>
  22. #define TASK_PRIORITY (FINSH_THREAD_PRIORITY + 1)
  23. /* Semaphore handle */
  24. static SemaphoreHandle_t static_sem = RT_NULL;
  25. /* Buffer to store semaphore structure */
  26. static StaticSemaphore_t xMutexBuffer;
  27. static TaskHandle_t TaskHandle1 = NULL;
  28. static TaskHandle_t TaskHandle2 = NULL;
  29. static void vTask1Code(void *pvParameters)
  30. {
  31. static BaseType_t result;
  32. static BaseType_t number = 0;
  33. while (1)
  34. {
  35. /* Task1 starts when task2 is delayed. Semaphore value is 5. Should take it successfully for five times */
  36. for (number = 0; number < 5; number++)
  37. {
  38. result = xSemaphoreTake(static_sem, portMAX_DELAY);
  39. if (result != pdPASS)
  40. {
  41. rt_kprintf("task1 take a static semaphore, failed.\n");
  42. return;
  43. }
  44. else
  45. {
  46. rt_kprintf("task1 take a static semaphore. number = %d\n", number);
  47. }
  48. }
  49. /* Cannot take the semaphore for the sixth time because the value is 0 */
  50. result = xSemaphoreTake(static_sem, 0);
  51. if (result != errQUEUE_EMPTY)
  52. {
  53. rt_kprintf("task1 take a static semaphore. number = %d. Should not succeed.\n", number);
  54. }
  55. vTaskDelay(pdMS_TO_TICKS(5000));
  56. }
  57. }
  58. static void vTask2Code(void * pvParameters)
  59. {
  60. static BaseType_t result;
  61. static BaseType_t number = 0;
  62. while (1)
  63. {
  64. /* Task2 runs before task1. The semaphore value is 0. Should give the semaphore 5 times successfully */
  65. for (number = 0; number < 5; number++)
  66. {
  67. result = xSemaphoreGive(static_sem);
  68. if (result != pdPASS)
  69. {
  70. rt_kprintf("task2 release a static semaphore, failed.\n");
  71. return;
  72. }
  73. else
  74. {
  75. rt_kprintf("task2 release a static semaphore. number = %d\n", number);
  76. }
  77. }
  78. /* Cannot give the semaphore for the sixth time because the max value is reached */
  79. result = xSemaphoreGive(static_sem);
  80. if (result != errQUEUE_FULL)
  81. {
  82. rt_kprintf("task2 release a static semaphore. number = %d. Should not succeed.\n", number);
  83. }
  84. vTaskDelay(pdMS_TO_TICKS(5000));
  85. }
  86. }
  87. int semaphore_counting_static()
  88. {
  89. /* Create a counting semaphore statically. Max value is 5. Initial value is 0. */
  90. static_sem = xSemaphoreCreateCountingStatic(5, 0, &xMutexBuffer);
  91. if (static_sem == NULL)
  92. {
  93. rt_kprintf("create static semaphore failed.\n");
  94. return -1;
  95. }
  96. xTaskCreate( vTask2Code, "Task2", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY + 1, &TaskHandle2 );
  97. if (TaskHandle2 == NULL)
  98. {
  99. rt_kprintf("Create task 2 failed\n");
  100. return -1;
  101. }
  102. xTaskCreate( vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle1 );
  103. if (TaskHandle1 == NULL)
  104. {
  105. rt_kprintf("Create task 1 failed\n");
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. MSH_CMD_EXPORT(semaphore_counting_static, semaphore sample);