foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
fsystem.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 2023-10-12 (date of last update)
47  * @version v1.6.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 #include <stdint.h>
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /* AXIVION Disable Style Generic-DoxygenCommentInHeader: Function has a doxygen
64  * comment, but since the '#pragma' AXIVION does not detect it, we annotate it
65  */
66 /* AXIVION Disable Style MisraC2012Directive-1.1: 'pragma' required to tell the
67  * TI ARM CGT compiler, that this is an interrupt function
68  * (see SPNU151V-January1998-RevisedFebruary2020: 5.11.29 The SWI_ALIAS Pragma)
69  */
70 /* AXIVION Disable Style MisraC2012-1.2: Function is implemented in assembler
71  * and this is the way to tell it the TI compiler (see
72  * src\os\freertos\portable\ccs\arm_cortex-r5\portasm.asm::swiRaisePrivilege)
73  */
74 /* AXIVION Disable Style MisraC2012-8.6: Function defintion is in assembler
75  * (see
76  * src\os\freertos\portable\ccs\arm_cortex-r5\portasm.asm::swiRaisePrivilege)
77  */
78 /**
79  * @brief Raise privilege
80  * @details This alias is mapped to an ASM function and raises to a privileged
81  * processor state if the system is currently in user mode.
82  * This is done by the following ASM code:
83  *
84  * * Copy the contents of a CP14 or CP15 coprocessor register (in this
85  * case SPSR -- Saved Program Status Register) into local register r12:
86  * \verbatim mrs r12, spsr \endverbatim
87  *
88  * * Perform bitwise AND on the contents of r12 and 0x0F
89  * * write to r0
90  * * update condition code flag on result of operation
91  * \verbatim ands r0, r12, #0x0F ; return value \endverbatim
92  *
93  * * Perform a logical OR on the contents of r12 and 0x1F, if equal to
94  * condition code flag from previous instruction
95  * * write to r12
96  * \verbatim orreq r12, r12, #0x1F \endverbatim
97  *
98  * * Conditionally (equal on previous instruction condition code flag)
99  * load immediate result from r12 into control field mask byte,
100  * PSR[7:0] (privileged software execution) from SPSR
101  * \verbatim msreq spsr_c, r12 \endverbatim
102  *
103  * * Branch and exchange instruction set
104  * * Target address is contained in r14
105  * \verbatim bx r14 \endverbatim
106  *
107  * It is important to issue #FSYS_SwitchToUserMode() after the
108  * privileged mode is no longer needed. Otherwise the system would
109  * stay in privileged mode.
110  *
111  * The datatype of the return value has to be reevaluated when changing
112  * platform and compiler.
113  * @return return value is zero if caller was in user mode (type: long)
114  */
115 #pragma SWI_ALIAS(FSYS_RaisePrivilege, 1);
116 extern long FSYS_RaisePrivilege(void);
117 /* AXIVION Enable Style MisraC2012-8.6: */
118 /* AXIVION Enable Style MisraC2012-1.2: */
119 /* AXIVION Enable Style MisraC2012Directive-1.1: */
120 /* AXIVION Enable Style Generic-DoxygenCommentInHeader: */
121 
122 /**
123  * @brief Switch back to user mode
124  * @details This macro is used after raising the privileges with
125  * #FSYS_RaisePrivilege(). Failure to call this macro may lead to
126  * unintended system behavior.
127  */
128 #ifndef UNITY_UNIT_TEST
129 #pragma FUNC_ALWAYS_INLINE(FSYS_SwitchToUserMode)
130 static inline void FSYS_SwitchToUserMode(void) {
131  __asm(" CPS #0x10");
132 }
133 
134 #else
135 #define FSYS_SwitchToUserMode()
136 #endif
137 
138 /*========== Extern Constant and Variable Declarations ======================*/
139 
140 /*========== Extern Function Prototypes =====================================*/
141 
142 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
143 #ifdef UNITY_UNIT_TEST
144 #endif
145 
146 #endif /* FOXBMS__FSYSTEM_H_ */
#define FSYS_SwitchToUserMode()
Switch back to user mode.
Definition: fsystem.h:135
long FSYS_RaisePrivilege(void)
Raise privilege.
Definition: test_htsensor.c:71