foxBMS  1.5.0
The foxBMS Battery Management System API Documentation
fstartup.c
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 fstartup.c
44  * @author foxBMS Team
45  * @date 2020-07-09 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup GENERAL
49  * @prefix STU
50  *
51  * @brief Startup code
52  *
53  * @details This file contains startup code mostly identical to TI code.
54  * Function "_c_int00" is extracted from file "HL_sys_startup.c" and
55  * function "STU_GetResetSourceWithoutFlagReset" is taken from file
56  * "HL_system.c". Both files are generated by TI HALCoGen under the
57  * following license:
58  *
59  * Copyright (C) 2009-2018 Texas Instruments Incorporated - www.ti.com
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * Redistributions of source code must retain the above copyright
66  * notice, this list of conditions and the following disclaimer.
67  *
68  * Redistributions in binary form must reproduce the above copyright
69  * notice, this list of conditions and the following disclaimer in the
70  * documentation and/or other materials provided with the
71  * distribution.
72  *
73  * Neither the name of Texas Instruments Incorporated nor the names of
74  * its contributors may be used to endorse or promote products derived
75  * from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  *
89  */
90 
91 /*========== Includes =======================================================*/
92 /* clang-format off */
93 /* keep include order as suggested by TI HALCoGen */
94 #include "HL_sys_common.h"
95 #include "HL_system.h"
96 #include "HL_sys_vim.h"
97 #include "HL_sys_core.h"
98 #include "HL_esm.h"
99 #include "HL_sys_mpu.h"
100 #include "HL_errata_SSWF021_45.h"
101 
102 #include "fassert.h"
103 #include "fstartup.h"
104 #include "main.h"
105 
106 #include <stdlib.h>
107 /* clang-format on */
108 
109 #include <stdint.h>
110 
111 /*========== Macros and Definitions =========================================*/
112 /** number of retries for the PLL to come up */
113 #define STU_PLL_RETRIES (5u)
114 
115 /*========== Static Constant and Variable Definitions =======================*/
116 
117 /*========== Extern Constant and Variable Definitions =======================*/
118 
119 /*========== Static Function Prototypes =====================================*/
120 /**
121  * @brief Handler for a failed PLL lock
122  * @details If the PLL can not be locked the, this function shall be called to
123  * ensure that the application no further starts.
124  * @return This function never returns */
125 static void STU_HandlePllLockFail(void);
126 
127 /**
128  * @brief Get reset flag
129  * @details Get reset source without reseting respective the flag in SYSESR
130  * register
131  * @return returns reset reason
132  */
133 static resetSource_t STU_GetResetSourceWithoutFlagReset(void);
134 
135 /*========== Static Function Implementations ================================*/
136 
139 }
141  register resetSource_t rst_source;
142 
143  if ((SYS_EXCEPTION & (uint32)POWERON_RESET) != 0U) {
144  /* power-on reset condition */
145  rst_source = POWERON_RESET;
146  } else if ((SYS_EXCEPTION & (uint32)EXT_RESET) != 0U) {
147  /*** Check for other causes of EXT_RESET that would take precedence **/
148  if ((SYS_EXCEPTION & (uint32)OSC_FAILURE_RESET) != 0U) {
149  /* Reset caused due to oscillator failure. Add user code here to handle oscillator failure */
150  rst_source = OSC_FAILURE_RESET;
151  } else if ((SYS_EXCEPTION & (uint32)WATCHDOG_RESET) != 0U) {
152  /* Reset caused due watchdog violation */
153  rst_source = WATCHDOG_RESET;
154  } else if ((SYS_EXCEPTION & (uint32)WATCHDOG2_RESET) != 0U) {
155  /* Reset caused due watchdog violation */
156  rst_source = WATCHDOG2_RESET;
157  } else if ((SYS_EXCEPTION & (uint32)SW_RESET) != 0U) {
158  /* Reset caused due to software reset. */
159  rst_source = SW_RESET;
160  } else {
161  /* Reset caused due to External reset. */
162  rst_source = EXT_RESET;
163  }
164  } else if ((SYS_EXCEPTION & (uint32)DEBUG_RESET) != 0U) {
165  /* Reset caused due Debug reset request */
166  rst_source = DEBUG_RESET;
167  } else if ((SYS_EXCEPTION & (uint32)CPU0_RESET) != 0U) {
168  /* Reset caused due to CPU0 reset. CPU reset can be caused by CPU self-test completion, or by toggling the "CPU RESET" bit of the CPU Reset Control Register. */
169  rst_source = CPU0_RESET;
170  } else {
171  /* No_reset occurred. */
172  rst_source = NO_RESET;
173  }
174  return rst_source;
175 }
176 
177 /*========== Extern Function Implementations ================================*/
178 /** system entry point */
179 #pragma CODE_STATE(_c_int00, 32)
180 #pragma INTERRUPT(_c_int00, RESET)
181 /* SourceId : STARTUP_SourceId_001 */
182 /* DesignId : STARTUP_DesignId_001 */
183 /* Requirements : HL_CONQ_STARTUP_SR1 */
184 void _c_int00(void) {
185  register resetSource_t rstSrc;
186 
187  /* Initialize Core Registers to avoid CCM Error */
188  _coreInitRegisters_();
189 
190  /* Initialize Stack Pointers */
191  _coreInitStackPointer_();
192 
193  /* Reset handler: the following instructions read from the system exception status register
194  * to identify the cause of the CPU reset.
195  */
196  /* Changed in comparison to TI _c_int00 implementation. Readout flags but do NOT clear them! */
198  switch (rstSrc) {
199  case POWERON_RESET:
200  /* Initialize L2RAM to avoid ECC errors right after power on */
201  _memInit_();
202 
203  /* Add condition to check whether PLL can be started successfully */
204  if (_errata_SSWF021_45_both_plls(STU_PLL_RETRIES) != 0U) {
205  /* Put system in a safe state */
207  }
208 
209  case DEBUG_RESET:
210  case EXT_RESET:
211 
212  /* Initialize L2RAM to avoid ECC errors right after power on */
213  if (rstSrc != POWERON_RESET) {
214  _memInit_();
215  }
216 
217  /* Enable CPU Event Export */
218  /* This allows the CPU to signal any single-bit or double-bit errors detected
219  * by its ECC logic for accesses to program flash or data RAM.
220  */
221  _coreEnableEventBusExport_();
222 
223  /* Check if there were ESM group3 errors during power-up.
224  * These could occur during eFuse auto-load or during reads from flash OTP
225  * during power-up. Device operation is not reliable and not recommended
226  * in this case. */
227  if ((esmREG->SR1[2]) != 0U) {
228  esmGroup3Notification(esmREG, esmREG->SR1[2]);
229  }
230 
231  /* Initialize System - Clock, Flash settings with Efuse self check */
232  systemInit();
233 
234  /* Enable IRQ offset via Vic controller */
235  _coreEnableIrqVicOffset_();
236 
237  /* Initialize VIM table */
238  vimInit();
239 
240  /* Configure system response to error conditions signaled to the ESM group1 */
241  /* This function can be configured from the ESM tab of HALCoGen */
242  esmInit();
243  break;
244 
245  case OSC_FAILURE_RESET:
246  break;
247 
248  case WATCHDOG_RESET:
249  case WATCHDOG2_RESET:
250 
251  break;
252 
253  case CPU0_RESET:
254  /* Enable CPU Event Export */
255  /* This allows the CPU to signal any single-bit or double-bit errors detected
256  * by its ECC logic for accesses to program flash or data RAM.
257  */
258  _coreEnableEventBusExport_();
259  break;
260 
261  case SW_RESET:
262  break;
263 
264  default:
265  break;
266  }
267 
268  _mpuInit_();
269 
270  /* initialize global variable and constructors */
271  __TI_auto_init();
272 
273  /* call the application */
274  main();
275 
276  /* AXIVION Next Codeline Style MisraC2012-21.8: exit is called as in generated code by TI */
277  exit(0);
278 }
279 
280 /*========== Externalized Static Function Implementations (Unit Test) =======*/
281 #ifdef UNITY_UNIT_TEST
282 #endif
Assert macro implementation.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:254
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:129
void _c_int00(void)
Startup Routine.
Definition: fstartup.c:184
#define STU_PLL_RETRIES
Definition: fstartup.c:113
static void STU_HandlePllLockFail(void)
Handler for a failed PLL lock.
Definition: fstartup.c:137
static resetSource_t STU_GetResetSourceWithoutFlagReset(void)
Get reset flag.
Definition: fstartup.c:140
Startup code.
void __TI_auto_init(void)
initialize global variable and constructors
Main function header.
int main(void)
main function of foxBMS
Definition: main.c:91