1 /*
2 * Copyright (c) 2021 - 2023, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef NRFX_FLAG32_ALLOCATOR_H__
35 #define NRFX_FLAG32_ALLOCATOR_H__
36
37 #include <nrfx.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44 * @defgroup nrfx_flag32_allocator Generic flag allocator
45 * @{
46 * @ingroup nrfx
47 * @brief Generic flag allocator.
48 */
49
50 /**
51 * @brief Function for initializing allocator mask.
52 *
53 * Initialization value contains mask where each bit indicates availablility of
54 * a given flag, e.g. init value 0x0000000A indicates that flag 3 and 1 (counting
55 * from 0) can be allocated.
56 *
57 * Alternatively, mask can be set to init value by direct assignment.
58 *
59 * @param[out] p_mask Mask to be initilized.
60 * @param[in] init_mask Mask with pool of available flags where bit being set means that
61 * flag is free and can be allocated.
62 */
nrfx_flag32_init(nrfx_atomic_t * p_mask,uint32_t init_mask)63 __STATIC_INLINE void nrfx_flag32_init(nrfx_atomic_t * p_mask, uint32_t init_mask)
64 {
65 *p_mask = init_mask;
66 }
67
68 /**
69 * @brief Function for checking if given flag is allocated.
70 *
71 * @note This check may not be valid if context is preempted and state is changed.
72 *
73 * @param[in] mask Mask.
74 * @param[in] bitpos Flag bit position.
75 *
76 * @return True if specified flag is allocated, false otherwise.
77 */
78 bool nrfx_flag32_is_allocated(nrfx_atomic_t mask, uint8_t bitpos);
79
80 /**
81 * @brief Function for allocating a flag in the mask.
82 *
83 * @note Function is thread safe, it uses @ref NRFX_ATOMIC_CAS macro. No further
84 * synchronization mechanism is needed, provided the macro is properly implemented
85 * (see @ref nrfx_glue).
86 *
87 * Mask must be initialized before first allocation. Flags are allocated from the
88 * highest bit position, e.g. if mask is set to 0x0000000A, 3 is returned and bit 3
89 * is cleared in the mask. Mask is set to 0x00000002 on return after successful allocation.
90 *
91 * @param[in,out] p_mask Mask with available flags set. On successful allocation flag is cleared.
92 * @param[out] p_flag Index of the allocated flag.
93 *
94 * @retval NRFX_SUCCESS Allocation was successful.
95 * @retval NRFX_ERROR_NO_MEM No resource available.
96 */
97 nrfx_err_t nrfx_flag32_alloc(nrfx_atomic_t * p_mask, uint8_t * p_flag);
98
99 /**
100 * @brief Function for freeing a flag allocated with @ref nrfx_flag32_alloc.
101 *
102 * @note Function is thread safe, it uses @ref NRFX_ATOMIC_CAS macro. No further
103 * synchronization mechanism is needed, provided the macro is properly implemented
104 * (see @ref nrfx_glue).
105 *
106 * @param[in,out] p_mask Mask with available flags set. On successful allocation flag is set.
107 * @param[in] flag Flag index.
108 *
109 * @retval NRFX_SUCCESS Freeing was successful.
110 * @retval NRFX_ERROR_INVALID_PARAM Flag was not allocated.
111 */
112 nrfx_err_t nrfx_flag32_free(nrfx_atomic_t * p_mask, uint8_t flag);
113
114 /** @} */
115
116 #ifdef __cplusplus
117 }
118 #endif
119
120 #endif // NRFX_FLAG32_ALLOCATOR_H__
121