1 /**
2  * \file
3  *
4  * \brief Critical sections related functionality declaration.
5  *
6  * Copyright (C) 2014 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 
44 #ifndef _HAL_ATOMIC_H_INCLUDED
45 #define _HAL_ATOMIC_H_INCLUDED
46 
47 #include <compiler.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /**
54  * \addtogroup doc_driver_hal_helper_atomic
55  *
56  *@{
57  */
58 
59 /**
60  * \brief Type for the register holding global interrupt enable flag
61  */
62 typedef uint32_t hal_atomic_t;
63 
64 /**
65  * \brief Helper macro for entering critical sections
66  *
67  * This macro is recommended to be used instead of a direct call
68  * hal_enterCritical() function to enter critical
69  * sections. No semicolon is required after the macro.
70  *
71  * \section atomic_usage Usage Example
72  * \code
73  * CRITICAL_SECTION_ENTER()
74  * Critical code
75  * CRITICAL_SECTION_LEAVE()
76  * \endcode
77  */
78 #define CRITICAL_SECTION_ENTER()                                                                                       \
79 	{                                                                                                                  \
80 		volatile hal_atomic_t __atomic;                                                                                \
81 		atomic_enter_critical(&__atomic);
82 
83 /**
84  * \brief Helper macro for leaving critical sections
85  *
86  * This macro is recommended to be used instead of a direct call
87  * hal_leaveCritical() function to leave critical
88  * sections. No semicolon is required after the macro.
89  *
90  * \section atomic_usage Usage Example
91  * \code
92  * CRITICAL_SECTION_ENTER()
93  * Some critical code
94  * CRITICAL_SECTION_LEAVE()
95  * \endcode
96  */
97 #define CRITICAL_SECTION_LEAVE()                                                                                       \
98 	atomic_leave_critical(&__atomic);                                                                                  \
99 	}
100 
101 /**
102  * \brief Disable interrupts, enter critical section
103  *
104  * Disables global interrupts. Supports nested critical sections,
105  * so that global interrupts are only re-enabled
106  * upon leaving the outermost nested critical section.
107  *
108  * \param[out] atomic The pointer to a variable to store the value of global
109  * interrupt enable flag
110  */
111 void atomic_enter_critical(hal_atomic_t volatile *atomic);
112 
113 /**
114  * \brief Exit atomic section
115  *
116  * Enables global interrupts. Supports nested critical sections,
117  * so that global interrupts are only re-enabled
118  * upon leaving the outermost nested critical section.
119  *
120  * \param[in] atomic The pointer to a variable, which stores the latest stored
121  * value of the global interrupt enable flag
122  */
123 void atomic_leave_critical(hal_atomic_t volatile *atomic);
124 
125 /**
126  * \brief Retrieve the current driver version
127  *
128  * \return Current driver version.
129  */
130 uint32_t atomic_get_version(void);
131 /**@}*/
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 #endif /* _HAL_ATOMIC_H_INCLUDED */
138