1 /*
2  * Copyright (c) 2019 - 2024, 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 NRF_MUTEX_H__
35 #define NRF_MUTEX_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_mutex_hal MUTEX HAL
45  * @{
46  * @ingroup nrf_mutex
47  * @brief   Hardware access layer for managing the MUTEX peripheral.
48  */
49 
50 /**
51  * @brief Function for locking the specified mutex.
52  *
53  * If the specified mutex is already locked, its state remains unchanged.
54  *
55  * @note Faults are not managed by the MUTEX peripheral.
56  *       One consequence is that if a mutex is locked and a fault happens,
57  *       it is the responsibility of the fault handler to release the mutex.
58  *       If a fault handler is not managing the mutex release, the mutex will remain locked.
59  *
60  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
61  * @param[in] mutex Index of the mutex to be locked.
62  *
63  * @retval true  Mutex is successfully locked.
64  * @retval false Mutex was already locked.
65  */
66 NRF_STATIC_INLINE bool nrf_mutex_lock(NRF_MUTEX_Type * p_reg, uint8_t mutex);
67 
68 /**
69  * @brief Function for unlocking the specified mutex.
70  *
71  * If the specified mutex is already unlocked, its state remains unchanged.
72  *
73  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
74  * @param[in] mutex Index of the mutex to be locked.
75  */
76 NRF_STATIC_INLINE void nrf_mutex_unlock(NRF_MUTEX_Type * p_reg, uint8_t mutex);
77 
78 #ifndef NRF_DECLARE_ONLY
79 
nrf_mutex_lock(NRF_MUTEX_Type * p_reg,uint8_t mutex)80 NRF_STATIC_INLINE bool nrf_mutex_lock(NRF_MUTEX_Type * p_reg, uint8_t mutex)
81 {
82     return (p_reg->MUTEX[mutex] == MUTEX_MUTEX_MUTEX_Unlocked);
83 }
84 
nrf_mutex_unlock(NRF_MUTEX_Type * p_reg,uint8_t mutex)85 NRF_STATIC_INLINE void nrf_mutex_unlock(NRF_MUTEX_Type * p_reg, uint8_t mutex)
86 {
87     p_reg->MUTEX[mutex] = MUTEX_MUTEX_MUTEX_Unlocked;
88 }
89 
90 #endif // NRF_DECLARE_ONLY
91 
92 /** @} */
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif // NRF_MUTEX_H__
99