task_notification_binary_semaphore.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. *
  12. * Demo: task notification
  13. * This program demonstrates using task notification as a light weight binary semaphore
  14. *
  15. */
  16. #include <rtthread.h>
  17. #include <FreeRTOS.h>
  18. #include <task.h>
  19. #define TASK_PRIORITY 25
  20. static TaskHandle_t xHandle = NULL;
  21. static rt_timer_t timer1;
  22. static void vTask1Code(void * pvParameters)
  23. {
  24. while (1)
  25. {
  26. uint32_t ulNotificationValue;
  27. ulNotificationValue = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  28. if (ulNotificationValue == 1)
  29. {
  30. rt_kprintf("Task received notification\n");
  31. }
  32. }
  33. }
  34. static void timeout(void *parameter)
  35. {
  36. rt_kprintf("Timer interrupt generated\n");
  37. vTaskNotifyGiveFromISR(xHandle, NULL);
  38. }
  39. int task_notification_binary_semaphore()
  40. {
  41. xTaskCreate(vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &xHandle);
  42. if (xHandle == NULL)
  43. {
  44. rt_kprintf("Create task failed\n");
  45. return -1;
  46. }
  47. /* Create a hard timer with period of 1 second */
  48. timer1 = rt_timer_create("timer1", timeout, RT_NULL, rt_tick_from_millisecond(1000), RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER);
  49. if (timer1 != RT_NULL)
  50. {
  51. rt_timer_start(timer1);
  52. }
  53. return 0;
  54. }
  55. MSH_CMD_EXPORT(task_notification_binary_semaphore, task notification sample);