task_notification_event_group.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 event group
  14. *
  15. */
  16. #include <rtthread.h>
  17. #include <FreeRTOS.h>
  18. #include <task.h>
  19. #define TASK_PRIORITY 25
  20. #define BIT_0 ( 1 << 0 )
  21. #define BIT_1 ( 1 << 1 )
  22. static TaskHandle_t TaskHandle1 = NULL;
  23. static TaskHandle_t TaskHandle2 = NULL;
  24. static TaskHandle_t TaskHandle3 = NULL;
  25. static void vTask1Code(void * pvParameters)
  26. {
  27. BaseType_t xResult;
  28. uint32_t ulNotificationValue;
  29. while (1)
  30. {
  31. xResult = xTaskNotifyWait( pdFALSE, BIT_0 | BIT_1, &ulNotificationValue, portMAX_DELAY);
  32. if (xResult == pdTRUE)
  33. {
  34. if (ulNotificationValue & BIT_0)
  35. {
  36. rt_kprintf("Task 1 received notification from task 2\n");
  37. }
  38. if (ulNotificationValue & BIT_1)
  39. {
  40. rt_kprintf("Task 1 received notification from task 3\n");
  41. }
  42. }
  43. }
  44. }
  45. void vTask2Code(void * pvParameters)
  46. {
  47. while (1)
  48. {
  49. vTaskDelay(pdMS_TO_TICKS(1000));
  50. rt_kprintf("Task 2 send notification\n");
  51. xTaskNotify(TaskHandle1, BIT_0, eSetBits);
  52. }
  53. }
  54. void vTask3Code(void * pvParameters)
  55. {
  56. while (1)
  57. {
  58. vTaskDelay(pdMS_TO_TICKS(2000));
  59. rt_kprintf("Task 3 send notification\n");
  60. xTaskNotify(TaskHandle1, BIT_1, eSetBits);
  61. }
  62. }
  63. int task_notification_event_group()
  64. {
  65. xTaskCreate(vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY + 1, &TaskHandle1);
  66. if (TaskHandle1 == NULL)
  67. {
  68. rt_kprintf("Create task failed\n");
  69. return -1;
  70. }
  71. xTaskCreate(vTask2Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle2);
  72. if (TaskHandle2 == NULL)
  73. {
  74. rt_kprintf("Create task failed\n");
  75. return -1;
  76. }
  77. xTaskCreate(vTask3Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle3);
  78. if (TaskHandle3 == NULL)
  79. {
  80. rt_kprintf("Create task failed\n");
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. MSH_CMD_EXPORT(task_notification_event_group, task notification sample);