| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /*
- * Copyright (c) 2006-2022, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-08-24 yangjie the first version
- */
- /*
- * Demo: semaphore
- * This demo creates one counting semaphore statically
- * It creates two tasks:
- * 1) task #1: take the semaphore until its value reaches 0
- * 2) task #2: give the semaphore until its value reaches maximum
- *
- */
- #include <FreeRTOS.h>
- #include <semphr.h>
- #include <task.h>
- #include <task.h>
- #define TASK_PRIORITY (FINSH_THREAD_PRIORITY + 1)
- /* Semaphore handle */
- static SemaphoreHandle_t static_sem = RT_NULL;
- /* Buffer to store semaphore structure */
- static StaticSemaphore_t xMutexBuffer;
- static TaskHandle_t TaskHandle1 = NULL;
- static TaskHandle_t TaskHandle2 = NULL;
- static void vTask1Code(void *pvParameters)
- {
- static BaseType_t result;
- static BaseType_t number = 0;
- while (1)
- {
- /* Task1 starts when task2 is delayed. Semaphore value is 5. Should take it successfully for five times */
- for (number = 0; number < 5; number++)
- {
- result = xSemaphoreTake(static_sem, portMAX_DELAY);
- if (result != pdPASS)
- {
- rt_kprintf("task1 take a static semaphore, failed.\n");
- return;
- }
- else
- {
- rt_kprintf("task1 take a static semaphore. number = %d\n", number);
- }
- }
- /* Cannot take the semaphore for the sixth time because the value is 0 */
- result = xSemaphoreTake(static_sem, 0);
- if (result != errQUEUE_EMPTY)
- {
- rt_kprintf("task1 take a static semaphore. number = %d. Should not succeed.\n", number);
- }
- vTaskDelay(pdMS_TO_TICKS(5000));
- }
- }
- static void vTask2Code(void * pvParameters)
- {
- static BaseType_t result;
- static BaseType_t number = 0;
- while (1)
- {
- /* Task2 runs before task1. The semaphore value is 0. Should give the semaphore 5 times successfully */
- for (number = 0; number < 5; number++)
- {
- result = xSemaphoreGive(static_sem);
- if (result != pdPASS)
- {
- rt_kprintf("task2 release a static semaphore, failed.\n");
- return;
- }
- else
- {
- rt_kprintf("task2 release a static semaphore. number = %d\n", number);
- }
- }
- /* Cannot give the semaphore for the sixth time because the max value is reached */
- result = xSemaphoreGive(static_sem);
- if (result != errQUEUE_FULL)
- {
- rt_kprintf("task2 release a static semaphore. number = %d. Should not succeed.\n", number);
- }
- vTaskDelay(pdMS_TO_TICKS(5000));
- }
- }
- int semaphore_counting_static()
- {
- /* Create a counting semaphore statically. Max value is 5. Initial value is 0. */
- static_sem = xSemaphoreCreateCountingStatic(5, 0, &xMutexBuffer);
- if (static_sem == NULL)
- {
- rt_kprintf("create static semaphore failed.\n");
- return -1;
- }
- xTaskCreate( vTask2Code, "Task2", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY + 1, &TaskHandle2 );
- if (TaskHandle2 == NULL)
- {
- rt_kprintf("Create task 2 failed\n");
- return -1;
- }
- xTaskCreate( vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle1 );
- if (TaskHandle1 == NULL)
- {
- rt_kprintf("Create task 1 failed\n");
- return -1;
- }
- return 0;
- }
- MSH_CMD_EXPORT(semaphore_counting_static, semaphore sample);
|