nrf_atflags.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Copyright (c) 2017 - 2020, 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. /**@file
  41. *
  42. * @defgroup nrf_atflags Atomic flags (bitmaps)
  43. * @ingroup app_common
  44. * @{
  45. *
  46. * @brief @tagAPI52 This module implements atomic flags as bitmaps.
  47. *
  48. * Operations on the individual flags are atomic, meaning that you are always sure that the flag is
  49. * set to the desired value, and you always know what value was there before. You also know that no
  50. * other flags were affected by the operation.
  51. *
  52. * Operations on the entire flag collection are NOT atomic. This essentially means that you can't
  53. * know the order in which operations on different flags happened, and you can't know the state of
  54. * the entire flag collection at any instant. These limitations can be overcome by protecting
  55. * operations with a mutex.
  56. */
  57. #ifndef NRF_ATFLAGS_H__
  58. #define NRF_ATFLAGS_H__
  59. #include <stdint.h>
  60. #include <stdbool.h>
  61. /**
  62. * @brief Array of atomic flags.
  63. * */
  64. typedef volatile uint32_t nrf_atflags_t;
  65. /**
  66. * @brief Number of flags per @ref nrf_atflags_t.
  67. * */
  68. #define NRF_ATFLAGS_FLAGS_PER_ELEMENT (sizeof(nrf_atflags_t) * 8)
  69. /**@brief Macro for the length of an array of @ref nrf_atflags_t needed to keep \p flag_count flags.
  70. *
  71. * @param flag_count Number of flags to keep in a flag array.
  72. *
  73. * @return Length of the array needed to house flag_count flags.
  74. */
  75. #define NRF_ATFLAGS_ARRAY_LEN(flag_count) ((flag_count - 1) / NRF_ATFLAGS_FLAGS_PER_ELEMENT) + 1
  76. /**@brief Macro for declaring a flag array with the right size and initial value.
  77. *
  78. * @note When using this macro, no call to @ref nrf_atflags_init is necessary for this array.
  79. *
  80. * @param _name Name to be given to the array.
  81. * @param flag_count Number of flags to be kept in the flag array.
  82. *
  83. * @return Flag array definition.
  84. */
  85. #define NRF_ATFLAGS_DEF(_name, flag_count) \
  86. nrf_atflags_t _name[NRF_ATFLAGS_ARRAY_LEN((flag_count))] = {0}
  87. /**@brief Macro for defining a flag array inside a struct.
  88. *
  89. * @note When using this macro, make sure to set the array to 0 or use @ref nrf_atflags_init
  90. *
  91. * @param _name Name to be given to the array.
  92. * @param flag_count Number of flags to be kept in the flag array.
  93. *
  94. * @return The flag array definition.
  95. */
  96. #define NRF_ATFLAGS_DEF_MEMBER(_name, flag_count) \
  97. nrf_atflags_t _name[NRF_ATFLAGS_ARRAY_LEN((flag_count))]
  98. /**@brief Function for safely initializing a flag array to 0.
  99. *
  100. * @param p_flags Flag array to initialize.
  101. * @param flags_array_len Length of \p p_flags.
  102. * @param flag_count Number of flags to be kept in the flag array.
  103. *
  104. * @retval 0 if the given length is not sufficient to house \p flag_count flags in the array.
  105. * @return If successful: The actual length required.
  106. */
  107. uint32_t nrf_atflags_init(nrf_atflags_t * p_flags, uint32_t flags_array_len, uint32_t flag_count);
  108. /**@brief Function for atomically setting a flag to 1.
  109. *
  110. * @param[in] p_flags Atomic flag array.
  111. * @param[in] flag_index Index of the flag in the array.
  112. */
  113. void nrf_atflags_set(nrf_atflags_t * p_flags, uint32_t flag_index);
  114. /**@brief Function for atomically setting a flag to 1, returning the previous value of the flag.
  115. *
  116. * @param[in] p_flags Atomic flag array.
  117. * @param[in] flag_index Index of the flag in the array.
  118. *
  119. * @return Old flag value.
  120. */
  121. bool nrf_atflags_fetch_set(nrf_atflags_t * p_flags, uint32_t flag_index);
  122. /**@brief Function for atomically setting a flag to 0.
  123. *
  124. * @param[in] p_flags Atomic flag array.
  125. * @param[in] flag_index Index of the flag in the array.
  126. */
  127. void nrf_atflags_clear(nrf_atflags_t * p_flags, uint32_t flag_index);
  128. /**@brief Function for atomically setting a flag to 0, returning the previous value of the flag.
  129. *
  130. * @param[in] p_flags Atomic flag array.
  131. * @param[in] flag_index Index of the flag in the array.
  132. *
  133. * @return Old flag value.
  134. */
  135. bool nrf_atflags_fetch_clear(nrf_atflags_t * p_flags, uint32_t flag_index);
  136. /**@brief Function for getting the value of a flag in a flag array.
  137. *
  138. * @param[in] p_flags Atomic flag array.
  139. * @param[in] flag_index Index of the flag in the array.
  140. *
  141. * @return Flag value.
  142. */
  143. bool nrf_atflags_get(nrf_atflags_t const * p_flags, uint32_t flag_index);
  144. /**@brief Function for finding a flag with value 0, and atomically setting it to one.
  145. *
  146. * @param[in] p_flags Atomic flag array.
  147. * @param[in] flag_count Number of flags in the array.
  148. *
  149. * @return Index of the cleared flag that has been set.
  150. */
  151. uint32_t nrf_atflags_find_and_set_flag(nrf_atflags_t * p_flags, uint32_t flag_count);
  152. /**@brief Function for finding a flag with value 1, and atomically clearing it to 0.
  153. *
  154. * @param[in] p_flags Atomic flag array.
  155. * @param[in] flag_count The number of flags in the array.
  156. *
  157. * @return The index of the set flag that has been cleared.
  158. */
  159. uint32_t nrf_atflags_find_and_clear_flag(nrf_atflags_t * p_flags, uint32_t flag_count);
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* NRF_ATFLAGS_H__ */
  164. /** @} */