task_manager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef __TASK_MANAGER_H__
  41. #define __TASK_MANAGER_H__
  42. /**
  43. * @defgroup task_manager Task manager (Cooperative Scheduler)
  44. * @{
  45. * @ingroup app_common
  46. * @brief Functions for managing tasks
  47. */
  48. #include <stdbool.h>
  49. #include <stdint.h>
  50. #include "nrf.h"
  51. #include "sdk_errors.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**@brief Main function of the task. */
  56. typedef void (* task_main_t)(void * p_context);
  57. /**@brief Task ID */
  58. typedef uint8_t task_id_t;
  59. /**@brief Invalid task ID */
  60. #define TASK_ID_INVALID ((task_id_t)(-1))
  61. /**@brief Start task manager.
  62. *
  63. * @details This function starts the task manager and configures given function as idle task.
  64. * This function never returns.
  65. *
  66. * @param[in] idle_task Main function of the task scheduled when no other tasks could be run.
  67. * @param[in] p_idle_task_context Context passed to idle task.
  68. */
  69. void task_manager_start(task_main_t idle_task, void * p_idle_task_context);
  70. /**@brief Create new task.
  71. *
  72. * @param[in] task Function which become main procedure of new task.
  73. * @param[in] p_task_name Task name.
  74. * @param[in] p_context Context passed to task procedure.
  75. *
  76. * @return ID of the task on success, otherwise TASK_ID_INVALID.
  77. */
  78. task_id_t task_create(task_main_t task, char const * p_task_name, void * p_context);
  79. /**@brief Yield CPU to other tasks.
  80. */
  81. void task_yield(void);
  82. /**@brief Complete current task.
  83. *
  84. * Task stack returns to the pool of available stacks.
  85. */
  86. void task_exit(void);
  87. /**@brief Wait for events. Set events are cleared after this function returns.
  88. *
  89. * @param[in] evt_mask Mask of events to wait
  90. *
  91. * @return Mask with set events (can be a subset of evt_mask).
  92. */
  93. uint32_t task_events_wait(uint32_t evt_mask);
  94. /**@brief Set events for given task.
  95. *
  96. * @param[in] task_id Id of the task which shall receive events.
  97. * @param[in] evt_mask Events for the task.
  98. *
  99. */
  100. void task_events_set(task_id_t task_id, uint32_t evt_mask);
  101. /**@brief Returns maximum depth of task stack.
  102. *
  103. * @param[in] task_id Id of the task (use @ref TASK_ID_INVALID for current task).
  104. * @return Number of bytes ever used on task stack.
  105. */
  106. uint32_t task_stack_max_usage_get(task_id_t task_id);
  107. /**@brief Returns ID of currently running task.
  108. *
  109. * @return ID of active task.
  110. */
  111. task_id_t task_id_get(void);
  112. /**@brief Set events for given task.
  113. *
  114. * @param[in] task_id Id of the task which name will be returned.
  115. * @return Task name
  116. *
  117. */
  118. char const * task_name_get(task_id_t task_id);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif /* __TASK_MANAGER_H__ */
  123. /** @} */