hal_task_scheduler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  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. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef HAL_TASK_H_INCLUDED
  43. #define HAL_TASK_H_INCLUDED
  44. #include <stdint.h>
  45. #include "hal_atomic.h"
  46. #include "sys_utils.h"
  47. #include "sys_task_scheduler.h"
  48. /**
  49. * @defgroup hal_task HAL Tasks
  50. * @ingroup hal_15_4
  51. * @{
  52. * @brief Module to declare HAL tasks library
  53. */
  54. /**@brief Identifiers for registered HAL handlers.
  55. *
  56. * @details enumeration with identifiers of registered HAL handlers.
  57. * HAL handlers will be called from the HAL task.
  58. */
  59. typedef enum
  60. {
  61. HAL_TIMER_TASK_ID,
  62. HAL_UART_TASK_ID,
  63. HAL_TIMER_CRITICAL_MANUAL_TASK,
  64. HAL_TASKS_AMOUNT,
  65. } hal_task_id_t;
  66. /**@brief Prototype of a HAL task handler.
  67. *
  68. * @details Handler which will be called from HAL task.
  69. */
  70. typedef void (* hal_task_handler_t)(void);
  71. void hal_task_handler(void);
  72. void hal_timer_task_handler(void);
  73. void hal_uart_task_handler(void);
  74. void hal_timer_critical_manual_handler(void);
  75. /**@brief Pending HAL tasks.
  76. *
  77. * @details Variable which includes markers of pending HAL tasks.
  78. */
  79. extern volatile uint_fast16_t g_hal_tasks;
  80. /**@brief Notify task scheduler to add a HAL task for execution.
  81. *
  82. * @details The function sets a marker for the HAL task for execution.
  83. *
  84. * @param[in] hal_task_id HAL task identifier (see \ref hal_task_id_t enumeration).
  85. */
  86. static inline void hal_task_post(hal_task_id_t hal_task_id)
  87. {
  88. atomic_t atomic = 0;
  89. hal_atomic_start(&atomic);
  90. g_hal_tasks |= BIT(hal_task_id);
  91. hal_atomic_end(&atomic);
  92. sys_task_post(HAL_TASK_ID);
  93. }
  94. /**@brief Removes a task from pending list in HAL task scheduler.
  95. *
  96. * @details The function removes a marker from the HAL execution list.
  97. *
  98. * @param[in] hal_task_id HAL task identifier (see \ref hal_task_id_t enumeration).
  99. */
  100. static inline void hal_task_unpost(hal_task_id_t hal_task_id)
  101. {
  102. atomic_t atomic = 0;
  103. hal_atomic_start(&atomic);
  104. g_hal_tasks &= ~BIT(hal_task_id);
  105. hal_atomic_end(&atomic);
  106. }
  107. /** @} */
  108. #endif // HAL_TASK_H_INCLUDED