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