1 /***************************************************************************//**
2  * @file
3  * @brief Interrupt enable/disable unit API
4  * @author Energy Micro AS
5  * @version 3.0.0
6  *******************************************************************************
7  * @section License
8  * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
9  *******************************************************************************
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute it
13  * freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must not
16  *    claim that you wrote the original software.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  *    misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
22  * obligation to support this Software. Energy Micro AS is providing the
23  * Software "AS IS", with no express or implied warranties of any kind,
24  * including, but not limited to, any implied warranties of merchantability
25  * or fitness for any particular purpose or warranties against infringement
26  * of any proprietary rights of a third party.
27  *
28  * Energy Micro AS will not be liable for any consequential, incidental, or
29  * special damages, or any other relief, or for any claim by any third party,
30  * arising from your use of this Software.
31  *
32  ******************************************************************************/
33 #ifndef __EM_INT_H
34 #define __EM_INT_H
35 
36 #include "em_part.h"
37 
38 extern uint32_t INT_LockCnt;
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /***************************************************************************//**
45  * @addtogroup EM_Library
46  * @{
47  ******************************************************************************/
48 
49 /***************************************************************************//**
50  * @addtogroup INT
51  * @{
52  ******************************************************************************/
53 
54 /***************************************************************************//**
55  * @brief
56  *   Disable interrupts.
57  *
58  * @return
59  *   The resulting interrupt nesting level.
60  *
61  * @details
62  *   Disable interrupts and increment lock level counter.
63  *
64  ******************************************************************************/
INT_Disable(void)65 __STATIC_INLINE uint32_t INT_Disable(void)
66 {
67   __disable_irq();
68   if (INT_LockCnt < UINT32_MAX)
69   {
70     INT_LockCnt++;
71   }
72 
73   return INT_LockCnt;
74 }
75 
76 /***************************************************************************//**
77  * @brief
78  *   Enable interrupts.
79  *
80  * @return
81  *   The resulting interrupt nesting level.
82  *
83  * @details
84  *   Decrement interrupt lock level counter and enable interrupts if counter
85  *   reached zero.
86  *
87  ******************************************************************************/
INT_Enable(void)88 __STATIC_INLINE uint32_t INT_Enable(void)
89 {
90   uint32_t retVal;
91 
92   if (INT_LockCnt > 0)
93   {
94     INT_LockCnt--;
95     retVal = INT_LockCnt;
96     if (retVal == 0)
97     {
98       __enable_irq();
99     }
100     return retVal;
101   }
102   else
103   {
104     return 0;
105   }
106 }
107 
108 /** @} (end addtogroup INT) */
109 /** @} (end addtogroup EM_Library) */
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* __EM_INT_H */
116