nrf_bitmask.h 5.3 KB

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