foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
fsystem.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, 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 2022-05-30 (date of last update)
47  * @version v1.3.0
48  * @ingroup SYSTEM
49  * @prefix FSYS
50  *
51  * @brief Function to switch between user mode and privilege mode
52  *
53  */
54 
55 #ifndef FOXBMS__FSYSTEM_H_
56 #define FOXBMS__FSYSTEM_H_
57 
58 /*========== Includes =======================================================*/
59 
60 /*========== Macros and Definitions =========================================*/
61 
62 /* AXIVION Disable Style MisraC2012-1.2 MisraC2012Directive-4.1: Function is
63  * implemented in Assembler (see swiRaisePrivilege) and this is the way to tell
64  * it the TI compiler */
65 /**
66  * @brief raise privilege
67  * @details This alias is mapped to an ASM function and raises to a privileged
68  * processor state if the system is currently in user mode.
69  * This is done by the following ASM code:
70  *
71  * * Copy the contents of a CP14 or CP15 coprocessor register (in this
72  * case SPSR -- Saved Program Status Register) into local register r12:
73  * \verbatim mrs r12, spsr \endverbatim
74  *
75  * * Perform bitwise AND on the contents of r12 and 0x0F
76  * * write to r0
77  * * update condition code flag on result of operation
78  * \verbatim ands r0, r12, #0x0F ; return value \endverbatim
79  *
80  * * Perform a logical OR on the contents of r12 and 0x1F, if equal to
81  * condition code flag from previous instruction
82  * * write to r12
83  * \verbatim orreq r12, r12, #0x1F \endverbatim
84  *
85  * * Conditionally (equal on previous instruction condition code flag)
86  * load immediate result from r12 into control field mask byte,
87  * PSR[7:0] (privileged software execution) from SPSR
88  * \verbatim msreq spsr_c, r12 \endverbatim
89  *
90  * * Branch and exchange instruction set
91  * * Target address is contained in r14
92  * \verbatim bx r14 \endverbatim
93  *
94  * It is important to issue #FSYS_SwitchToUserMode() after the
95  * privileged mode is no longer needed. Otherwise the system would
96  * stay in privileged mode.
97  *
98  * The datatype of the return value has to be reevaluated when changing
99  * platform and compiler.
100  * @return return value is zero if caller was in user mode (type: long)
101  */
102 #pragma SWI_ALIAS(FSYS_RaisePrivilege, 1);
103 extern long FSYS_RaisePrivilege(void);
104 /* AXIVION Enable Style MisraC2012-1.2 MisraC2012Directive-4.1: */
105 
106 /**
107  * @def FSYS_SwitchToUserMode()
108  * @brief Switch back to user mode
109  * @details This macro is used after raising the privileges with
110  * #FSYS_RaisePrivilege(). Failure to call this macro may lead to
111  * unintended system behavior.
112  */
113 #ifndef UNITY_UNIT_TEST
114 #define FSYS_SwitchToUserMode() \
115  { __asm(" CPS #0x10"); }
116 #else
117 #define FSYS_SwitchToUserMode()
118 #endif
119 
120 /*========== Extern Constant and Variable Declarations ======================*/
121 
122 /*========== Extern Function Prototypes =====================================*/
123 
124 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
125 
126 #endif /* FOXBMS__FSYSTEM_H_ */
long FSYS_RaisePrivilege(void)
raise privilege
Definition: test_htsensor.c:67