nrf_atflags.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "nrf.h"
  41. #include "nrf_atomic.h"
  42. #include "nrf_atflags.h"
  43. #include "sdk_common.h"
  44. /**@brief Macro for getting the index inside the flag array where a flag can be found.
  45. *
  46. * @param flag_index Index of the flag.
  47. *
  48. * @return Index of the @ref nrf_atflags_t the flag can be found in.
  49. */
  50. #define FLAG_BASE(flag_index) ((flag_index) / NRF_ATFLAGS_FLAGS_PER_ELEMENT)
  51. /**@brief Macro for getting the mask representing the flag within the flag array member.
  52. *
  53. * @param flag_index ID of the flag.
  54. *
  55. * @return Mask representing the flag within a single @ref nrf_atflags_t.
  56. */
  57. #define FLAG_MASK(flag_index) (1UL << ((flag_index) % NRF_ATFLAGS_FLAGS_PER_ELEMENT))
  58. void nrf_atflags_set(nrf_atflags_t * p_flags, uint32_t flag_index)
  59. {
  60. uint32_t new_value = nrf_atomic_u32_or(&p_flags[FLAG_BASE(flag_index)], FLAG_MASK(flag_index));
  61. UNUSED_RETURN_VALUE(new_value);
  62. }
  63. bool nrf_atflags_fetch_set(nrf_atflags_t * p_flags, uint32_t flag_index)
  64. {
  65. return (nrf_atomic_u32_fetch_or(&p_flags[FLAG_BASE(flag_index)], FLAG_MASK(flag_index))
  66. & FLAG_MASK(flag_index)) != 0;
  67. }
  68. void nrf_atflags_clear(nrf_atflags_t * p_flags, uint32_t flag_index)
  69. {
  70. uint32_t new_value = nrf_atomic_u32_and(&p_flags[FLAG_BASE(flag_index)], ~FLAG_MASK(flag_index));
  71. UNUSED_RETURN_VALUE(new_value);
  72. }
  73. bool nrf_atflags_fetch_clear(nrf_atflags_t * p_flags, uint32_t flag_index)
  74. {
  75. return (nrf_atomic_u32_fetch_and(&p_flags[FLAG_BASE(flag_index)], ~FLAG_MASK(flag_index))
  76. & FLAG_MASK(flag_index)) != 0;
  77. }
  78. bool nrf_atflags_get(nrf_atflags_t const * p_flags, uint32_t flag_index)
  79. {
  80. return (p_flags[FLAG_BASE(flag_index)] & FLAG_MASK(flag_index)) != 0;
  81. }
  82. uint32_t nrf_atflags_init(nrf_atflags_t * p_flags, uint32_t flags_array_len, uint32_t flag_count)
  83. {
  84. uint32_t required_flags_array_len = NRF_ATFLAGS_ARRAY_LEN(flag_count);
  85. if (required_flags_array_len <= flags_array_len)
  86. {
  87. for (uint32_t i = 0; i < required_flags_array_len; i++)
  88. {
  89. p_flags[i] = 0;
  90. }
  91. return required_flags_array_len;
  92. }
  93. return 0;
  94. }
  95. uint32_t nrf_atflags_find_and_set_flag(nrf_atflags_t * p_flags, uint32_t flag_count)
  96. {
  97. for (uint32_t i = 0; i < NRF_ATFLAGS_ARRAY_LEN(flag_count); i++)
  98. {
  99. // Using __RBIT to make the order of flags more traditional.
  100. uint32_t first_zero = __CLZ(__RBIT(~p_flags[i]));
  101. while (first_zero < 32)
  102. {
  103. uint32_t first_zero_global = first_zero + (i * 32);
  104. if (first_zero_global >= flag_count)
  105. {
  106. break;
  107. }
  108. if (!nrf_atflags_fetch_set(p_flags, first_zero_global))
  109. {
  110. return first_zero_global;
  111. }
  112. first_zero = __CLZ(__RBIT(~p_flags[i]));
  113. }
  114. }
  115. return flag_count;
  116. }
  117. uint32_t nrf_atflags_find_and_clear_flag(nrf_atflags_t * p_flags, uint32_t flag_count)
  118. {
  119. for (uint32_t i = 0; i < NRF_ATFLAGS_ARRAY_LEN(flag_count); i++)
  120. {
  121. // Using __RBIT to make the order of flags more traditional.
  122. uint32_t first_one = __CLZ(__RBIT(p_flags[i]));
  123. while (first_one < 32)
  124. {
  125. uint32_t first_one_global = first_one + (i * 32);
  126. if (first_one_global >= flag_count)
  127. {
  128. break;
  129. }
  130. if (nrf_atflags_fetch_clear(p_flags, first_one_global))
  131. {
  132. return first_one_global;
  133. }
  134. first_one = __CLZ(__RBIT(p_flags[i]));
  135. }
  136. }
  137. return flag_count;
  138. }