MCU-Common
Modules useful for embedded (MCU) programming
critical.h
Go to the documentation of this file.
1 /*
2  * This file is part of MCU-Common.
3  *
4  * Copyright (C) 2017 Adam Heinrich <adam@adamh.cz>
5  *
6  * MCU-Common is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * MCU-Common is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MCU-Common. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  */
31 
32 #ifndef MCU_COMMON_CRITICAL_H
33 #define MCU_COMMON_CRITICAL_H
34 
45 #include <stdint.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
53 #if defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \
54  defined(__ARM_ARCH_7EM__)
55 
60 #define CRITICAL_ENTER() \
61  do { \
62  uint32_t _prm; \
63  __asm__ volatile ("mrs %0, primask" : "=r" (_prm)); \
64  __asm__ volatile ("cpsid i" ::: "memory")
65 
70 #define CRITICAL_EXIT() \
71  __asm__ volatile ("msr primask, %0" :: "r" (_prm) : "memory"); \
72  } while(0)
73 
74 #else
75 
76 #pragma message "Using empty stubs for CRITICAL_*() macros. "\
77  "Please define them for your architecture."
78 
79 #define CRITICAL_ENTER() do {
80 #define CRITICAL_EXIT() } while(0)
81 
82 #endif
83 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #endif /* MCU_COMMON_CRITICAL_H */