xref: /nrf52832-nimble/nordic/nrfx/drivers/include/nrf_bitmask.h (revision 150812a83cab50279bd772ef6db1bfaf255f2c5b)
1 /*
2  * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_BITMASK_H
33 #define NRF_BITMASK_H
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_bitmask Bitmask module
43  * @{
44  * @ingroup nrfx
45  * @brief   Bitmask managing module.
46  */
47 
48 #define BITMASK_BYTE_GET(abs_bit) ((abs_bit)/8)
49 #define BITMASK_RELBIT_GET(abs_bit) ((abs_bit) & 0x00000007)
50 
51 /**
52  * Function for checking if bit in the multi-byte bit mask is set.
53  *
54  * @param bit    Bit index.
55  * @param p_mask A pointer to mask with bit fields.
56  *
57  * @return 0 if bit is not set, positive value otherwise.
58  */
nrf_bitmask_bit_is_set(uint32_t bit,void const * p_mask)59 __STATIC_INLINE uint32_t nrf_bitmask_bit_is_set(uint32_t bit, void const * p_mask)
60 {
61     uint8_t const * p_mask8 = (uint8_t const *)p_mask;
62     uint32_t byte_idx = BITMASK_BYTE_GET(bit);
63     bit = BITMASK_RELBIT_GET(bit);
64     return (1 << bit) & p_mask8[byte_idx];
65 }
66 
67 /**
68  * Function for setting a bit in the multi-byte bit mask.
69  *
70  * @param bit    Bit index.
71  * @param p_mask A pointer to mask with bit fields.
72  */
nrf_bitmask_bit_set(uint32_t bit,void * p_mask)73 __STATIC_INLINE void nrf_bitmask_bit_set(uint32_t bit, void * p_mask)
74 {
75     uint8_t * p_mask8 = (uint8_t *)p_mask;
76     uint32_t byte_idx = BITMASK_BYTE_GET(bit);
77     bit = BITMASK_RELBIT_GET(bit);
78     p_mask8[byte_idx] |= (1 << bit);
79 }
80 
81 /**
82  * Function for clearing a bit in the multi-byte bit mask.
83  *
84  * @param bit    Bit index.
85  * @param p_mask A pointer to mask with bit fields.
86  */
nrf_bitmask_bit_clear(uint32_t bit,void * p_mask)87 __STATIC_INLINE void nrf_bitmask_bit_clear(uint32_t bit, void * p_mask)
88 {
89     uint8_t * p_mask8 = (uint8_t *)p_mask;
90     uint32_t byte_idx = BITMASK_BYTE_GET(bit);
91     bit = BITMASK_RELBIT_GET(bit);
92     p_mask8[byte_idx] &= ~(1 << bit);
93 }
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  */
nrf_bitmask_masks_or(void const * p_mask1,void const * p_mask2,void * p_out_mask,uint32_t length)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 /**
119  * Function for performing bitwise AND operation on two multi-byte bit masks.
120  *
121  * @param p_mask1    A pointer to the first bit mask.
122  * @param p_mask2    A pointer to the second bit mask.
123  * @param p_out_mask A pointer to the output bit mask.
124  * @param length     Length of output mask in bytes.
125  */
nrf_bitmask_masks_and(void const * p_mask1,void const * p_mask2,void * p_out_mask,uint32_t length)126 __STATIC_INLINE void nrf_bitmask_masks_and(void const *   p_mask1,
127                                            void const *   p_mask2,
128                                            void *         p_out_mask,
129                                            uint32_t       length)
130 {
131     uint8_t const * p_mask8_1 = (uint8_t const *)p_mask1;
132     uint8_t const * p_mask8_2 = (uint8_t const *)p_mask2;
133     uint8_t * p_mask8_out = (uint8_t *)p_out_mask;
134     uint32_t i;
135     for (i = 0; i < length; i++)
136     {
137         p_mask8_out[i] = p_mask8_1[i] & p_mask8_2[i];
138     }
139 }
140 
141 /** @} */
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif // NRF_BITMASK_H
148