nrf_bitmask.h 5.2 KB

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