foxBMS  1.2.0
The foxBMS Battery Management System API Documentation
fsystem.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
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_ */
long FSYS_RaisePrivilege(void)
raise privilege