summaryrefslogtreecommitdiffstats
path: root/libopencm3/include/libopencm3/cm3/cortex.h
blob: eb9cb091b411ec9c8b563a8afc84fb32e345925e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/** @defgroup CM3_cortex_defines Cortex Core Defines
 *
 * @brief <b>libopencm3 Defined Constants and Types for the Cortex Core </b>
 *
 * @ingroup CM3_defines
 *
 * @version 1.0.0
 *
 * LGPL License Terms @ref lgpl_license
 */
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2013 Ben Gamari <bgamari@gmail.com>
 * Copyright (C) 2013 Frantisek Burian <BuFran@seznam.cz>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LIBOPENCM3_CORTEX_H
#define LIBOPENCM3_CORTEX_H

/**@{*/

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Enable interrupts
 *
 * Disable the interrupt mask and enable interrupts globally
 */
static inline void cm_enable_interrupts(void)
{
	__asm__("CPSIE I\n");
}

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Disable interrupts
 *
 * Mask all interrupts globally
 */
static inline void cm_disable_interrupts(void)
{
	__asm__("CPSID I\n");
}

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Enable faults
 *
 * Disable the HardFault mask and enable fault interrupt globally
 */
static inline void cm_enable_faults(void)
{
	__asm__("CPSIE F\n");
}

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Disable faults
 *
 * Mask the HardFault interrupt globally
 */
static inline void cm_disable_faults(void)
{
	__asm__("CPSID F\n");
}

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Check if interrupts are masked
 *
 * Checks, if interrupts are masked (disabled).
 *
 * @returns true, if interrupts are disabled.
 */
__attribute__((always_inline))
static inline bool cm_is_masked_interrupts(void)
{
	register uint32_t result;
	__asm__ ("MRS %0, PRIMASK"  : "=r" (result));
	return result;
}

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Check if Fault interrupt is masked
 *
 * Checks, if HardFault interrupt is masked (disabled).
 *
 * @returns bool true, if HardFault interrupt is disabled.
 */
__attribute__((always_inline))
static inline bool cm_is_masked_faults(void)
{
	register uint32_t result;
	__asm__ ("MRS %0, FAULTMASK"  : "=r" (result));
	return result;
}

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Mask interrupts
 *
 * This function switches the mask of the interrupts. If mask is true, the
 * interrupts will be disabled. The result of this function can be used for
 * restoring previous state of the mask.
 *
 * @param[in] mask bool New state of the interrupt mask
 * @returns bool old state of the interrupt mask
 */
__attribute__((always_inline))
static inline bool cm_mask_interrupts(bool mask)
{
	register bool old;
	__asm__ __volatile__("MRS %0, PRIMASK"  : "=r" (old));
	__asm__ __volatile__(""  : : : "memory");
	__asm__ __volatile__("MSR PRIMASK, %0" : : "r" (mask));
	return old;
}

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Mask HardFault interrupt
 *
 * This function switches the mask of the HardFault interrupt. If mask is true,
 * the HardFault interrupt will be disabled. The result of this function can be
 * used for restoring previous state of the mask.
 *
 * @param[in] mask bool New state of the HardFault interrupt mask
 * @returns bool old state of the HardFault interrupt mask
 */
__attribute__((always_inline))
static inline bool cm_mask_faults(bool mask)
{
	register bool old;
	__asm__ __volatile__ ("MRS %0, FAULTMASK"  : "=r" (old));
	__asm__ __volatile__ (""  : : : "memory");
	__asm__ __volatile__ ("MSR FAULTMASK, %0" : : "r" (mask));
	return old;
}

/**@}*/

/*===========================================================================*/
/** @defgroup CM3_cortex_atomic_defines Cortex Core Atomic support Defines
 *
 * @brief Atomic operation support
 *
 * @ingroup CM3_cortex_defines
 */
/**@{*/

#if !defined(__DOXYGEN__)
/* Do not populate this definition outside */
static inline bool __cm_atomic_set(bool *val)
{
	return cm_mask_interrupts(*val);
}

#define __CM_SAVER(state)					\
	__val = state,						\
	__save __attribute__((__cleanup__(__cm_atomic_set))) =	\
	__cm_atomic_set(&__val)

#endif /* !defined(__DOXYGEN) */


/*---------------------------------------------------------------------------*/
/** @brief Cortex M Atomic Declare block
 *
 * This macro disables interrupts for the next command or block of code. The
 * interrupt mask is automatically restored after exit of the boundary of the
 * code block. Therefore restore of interrupt is done automatically after call
 * of return or goto control sentence jumping outside of the block.
 *
 * @warning The usage of sentences break or continue is prohibited in the block
 * due to implementation of this macro!
 *
 * @note It is safe to use this block inside normal code and in interrupt
 * routine.
 *
 * @example 1: Basic usage of atomic block
 *
 * @code
 * uint64_t value;		// This value is used somewhere in interrupt
 *
 * ...
 *
 * CM_ATOMIC_BLOCK() {			// interrupts are masked in this block
 *     value = value * 1024 + 651;	// access value as atomic
 * }					// interrupts is restored automatically
 * @endcode
 *
 * @example 2: Use of return inside block:
 *
 * @code
 * uint64_t value;		// This value is used somewhere in interrupt
 *
 * ...
 *
 * uint64_t allocval(void)
 * {
 *     CM_ATOMIC_BLOCK() {		// interrupts are masked in this block
 *         value = value * 1024 + 651;	// do long atomic operation
 *         return value;		// interrupts is restored automatically
 *     }
 * }
 * @endcode
 */
#if defined(__DOXYGEN__)
#define CM_ATOMIC_BLOCK()
#else /* defined(__DOXYGEN__) */
#define CM_ATOMIC_BLOCK()						\
	for (bool ___CM_SAVER(true), __my = true; __my; __my = false)
#endif /* defined(__DOXYGEN__) */

/*---------------------------------------------------------------------------*/
/** @brief Cortex M Atomic Declare context
 *
 * This macro disables interrupts in the current block of code from the place
 * where it is defined to the end of the block. The interrupt mask is
 * automatically restored after exit of the boundary of the code block.
 * Therefore restore of interrupt is done automatically after call of return,
 * continue, break, or goto control sentence jumping outside of the block.
 *
 * @note This function is intended for use in for- cycles to enable the use of
 * break and contine sentences inside the block, and for securing the atomic
 * reader-like functions.
 *
 * @note It is safe to use this block inside normal code and in interrupt
 * routine.
 *
 * @example 1: Basic usage of atomic context
 *
 * @code
 * uint64_t value;		// This value is used somewhere in interrupt
 *
 * ...
 *
 * for (int i=0;i < 100; i++) {
 *     CM_ATOMIC_CONTEXT();		// interrupts are masked in this block
 *     value += 100;			// access value as atomic
 *     if ((value % 16) == 0) {
 *         break;			// restore interrupts and break cycle
 *     }
 * }					// interrupts is restored automatically
 * @endcode
 *
 * @example 2: Usage of atomic context inside atomic reader fcn.
 *
 * @code
 * uint64_t value;		// This value is used somewhere in interrupt
 *
 * ...
 *
 * uint64_t getnextval(void)
 * {
 *     CM_ATOMIC_CONTEXT();	// interrupts are masked in this block
 *     value = value + 3;	// do long atomic operation
 *     return value;		// interrupts is restored automatically
 * }
 * @endcode
 */
#if defined(__DOXYGEN__)
#define CM_ATOMIC_CONTEXT()
#else /* defined(__DOXYGEN__) */
#define CM_ATOMIC_CONTEXT()	bool __CM_SAVER(true)
#endif /* defined(__DOXYGEN__) */

/**@}*/



#endif