nrf_bitmask.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright (c) 2016 - 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. #ifndef NRF_BITMASK_H
  41. #define NRF_BITMASK_H
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_bitmask Bitmask module
  48. * @{
  49. * @ingroup nrfx
  50. * @brief Bitmask managing module.
  51. */
  52. /** @brief Macro for getting index of byte in byte stream where @c abs_bit is put. */
  53. #define BITMASK_BYTE_GET(abs_bit) ((abs_bit)/8)
  54. /** @brief Macro for getting relative index of bit in byte. */
  55. #define BITMASK_RELBIT_GET(abs_bit) ((abs_bit) & 0x00000007)
  56. /**
  57. * @brief Function for checking if bit in the multi-byte bit mask is set.
  58. *
  59. * @param[in] bit Bit index.
  60. * @param[in] p_mask Pointer to mask with bit fields.
  61. *
  62. * @return 0 if bit is not set, positive value otherwise.
  63. */
  64. __STATIC_INLINE uint32_t nrf_bitmask_bit_is_set(uint32_t bit, void const * p_mask)
  65. {
  66. uint8_t const * p_mask8 = (uint8_t const *)p_mask;
  67. uint32_t byte_idx = BITMASK_BYTE_GET(bit);
  68. bit = BITMASK_RELBIT_GET(bit);
  69. return (1 << bit) & p_mask8[byte_idx];
  70. }
  71. /**
  72. * @brief Function for setting a bit in the multi-byte bit mask.
  73. *
  74. * @param[in] bit Bit index.
  75. * @param[in] p_mask Pointer to mask with bit fields.
  76. */
  77. __STATIC_INLINE void nrf_bitmask_bit_set(uint32_t bit, void * p_mask)
  78. {
  79. uint8_t * p_mask8 = (uint8_t *)p_mask;
  80. uint32_t byte_idx = BITMASK_BYTE_GET(bit);
  81. bit = BITMASK_RELBIT_GET(bit);
  82. p_mask8[byte_idx] |= (1 << bit);
  83. }
  84. /**
  85. * @brief Function for clearing a bit in the multi-byte bit mask.
  86. *
  87. * @param[in] bit Bit index.
  88. * @param[in] p_mask Pointer to mask with bit fields.
  89. */
  90. __STATIC_INLINE void nrf_bitmask_bit_clear(uint32_t bit, void * p_mask)
  91. {
  92. uint8_t * p_mask8 = (uint8_t *)p_mask;
  93. uint32_t byte_idx = BITMASK_BYTE_GET(bit);
  94. bit = BITMASK_RELBIT_GET(bit);
  95. p_mask8[byte_idx] &= ~(1 << bit);
  96. }
  97. /**
  98. * @brief Function for performing bitwise OR operation on two multi-byte bit masks.
  99. *
  100. * @param[in] p_mask1 Pointer to the first bit mask.
  101. * @param[in] p_mask2 Pointer to the second bit mask.
  102. * @param[in] p_out_mask Pointer to the output bit mask.
  103. * @param[in] length Length of output mask in bytes.
  104. */
  105. __STATIC_INLINE void nrf_bitmask_masks_or(void const * p_mask1,
  106. void const * p_mask2,
  107. void * p_out_mask,
  108. uint32_t length)
  109. {
  110. uint8_t const * p_mask8_1 = (uint8_t const *)p_mask1;
  111. uint8_t const * p_mask8_2 = (uint8_t const *)p_mask2;
  112. uint8_t * p_mask8_out = (uint8_t *)p_out_mask;
  113. uint32_t i;
  114. for (i = 0; i < length; i++)
  115. {
  116. p_mask8_out[i] = p_mask8_1[i] | p_mask8_2[i];
  117. }
  118. }
  119. /**
  120. * @brief Function for performing bitwise AND operation on two multi-byte bit masks.
  121. *
  122. * @param[in] p_mask1 Pointer to the first bit mask.
  123. * @param[in] p_mask2 Pointer to the second bit mask.
  124. * @param[in] p_out_mask Pointer to the output bit mask.
  125. * @param[in] length Length of output mask in bytes.
  126. */
  127. __STATIC_INLINE void nrf_bitmask_masks_and(void const * p_mask1,
  128. void const * p_mask2,
  129. void * p_out_mask,
  130. uint32_t length)
  131. {
  132. uint8_t const * p_mask8_1 = (uint8_t const *)p_mask1;
  133. uint8_t const * p_mask8_2 = (uint8_t const *)p_mask2;
  134. uint8_t * p_mask8_out = (uint8_t *)p_out_mask;
  135. uint32_t i;
  136. for (i = 0; i < length; i++)
  137. {
  138. p_mask8_out[i] = p_mask8_1[i] & p_mask8_2[i];
  139. }
  140. }
  141. /** @} */
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif // NRF_BITMASK_H