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