foxBMS-UnitTests  1.0.0
The foxBMS Unit Tests API Documentation
fsystem.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
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  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
38  * ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file fsystem.h
44  * @author foxBMS Team
45  * @date 2020-07-21 (date of creation)
46  * @updated 2020-07-21 (date of last update)
47  * @ingroup SYSTEM
48  * @prefix FSYS
49  *
50  * @brief Function to switch between user mode and privilege mode
51  *
52  */
53 
54 #ifndef FOXBMS__FSYSTEM_H_
55 #define FOXBMS__FSYSTEM_H_
56 
57 /*========== Includes =======================================================*/
58 
59 /*========== Macros and Definitions =========================================*/
60 /**
61  * @brief raise privilege
62  * @details This alias is mapped to an ASM function and raises to a privileged
63  * processor state if the system is currently in user mode.
64  * This is done by the following ASM code:
65  *
66  * * Copy the contents of a CP14 or CP15 coprocessor register (in this
67  * case SPSR -- Saved Program Status Register) into local register r12:
68  * \verbatim mrs r12, spsr \endverbatim
69  *
70  * * Perform bitwise AND on the contents of r12 and 0x0F
71  * * write to r0
72  * * update condition code flag on result of operation
73  * \verbatim ands r0, r12, #0x0F ; return value \endverbatim
74  *
75  * * Perform a logical OR on the contents of r12 and 0x1F, if equal to
76  * condition code flag from previous instruction
77  * * write to r12
78  * \verbatim orreq r12, r12, #0x1F \endverbatim
79  *
80  * * Conditionally (equal on previous instruction condition code flag)
81  * load immediate result from r12 into control field mask byte,
82  * PSR[7:0] (privileged software execution) from SPSR
83  * \verbatim msreq spsr_c, r12 \endverbatim
84  *
85  * * Branch and exchange instruction set
86  * * Target address is contained in r14
87  * \verbatim bx r14 \endverbatim
88  *
89  * It is important to issue #FSYS_SwitchToUserMode() after the
90  * privileged mode is no longer needed. Otherwise the system would
91  * stay in privileged mode.
92  *
93  * The datatype of the return value has to be reevaluated when changing
94  * platform and compiler.
95  * @return return value is zero if caller was in user mode (type: long)
96  */
97 #pragma SWI_ALIAS(FSYS_RaisePrivilege, 1);
98 extern long FSYS_RaisePrivilege(void);
99 
100 /**
101  * @def FSYS_SwitchToUserMode()
102  * @brief Switch back to user mode
103  * @details This macro is used after raising the privileges with
104  * #FSYS_RaisePrivilege(). Failure to call this macro may lead to
105  * unintended system behavior.
106  */
107 #ifndef UNITY_UNIT_TEST
108 #define FSYS_SwitchToUserMode() \
109  { __asm(" CPS #0x10"); }
110 #else
111 #define FSYS_SwitchToUserMode()
112 #endif
113 
114 /*========== Extern Constant and Variable Declarations ======================*/
115 
116 /*========== Extern Function Prototypes =====================================*/
117 
118 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
119 
120 #endif /* FOXBMS__FSYSTEM_H_ */
FSYS_RaisePrivilege
long FSYS_RaisePrivilege(void)
raise privilege
Definition: test_nxpfs85xx.c:94