foxBMS  1.3.0
The foxBMS Battery Management System API Documentation
fstartup.c
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 fstartup.c
44  * @author foxBMS Team
45  * @date 2020-07-09 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.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 "fstartup.h"
103 #include "main.h"
104 /* clang-format on */
105 
106 /*========== Macros and Definitions =========================================*/
107 /** number of retries for the PLL to come up */
108 #define STU_PLL_RETRIES 5U
109 
110 /*========== Static Constant and Variable Definitions =======================*/
111 
112 /*========== Extern Constant and Variable Definitions =======================*/
113 
114 /*========== Static Function Prototypes =====================================*/
115 /**
116  * @brief Handler for a failed PLL lock
117  * @details If the PLL can not be locked the, this function shall be called to
118  * ensure that the application no further starts.
119  * @return This function never returns */
120 static void STU_HandlePllLockFail(void);
121 
122 /**
123  * @brief Get reset flag
124  * @details Get reset source without reseting respective the flag in SYSESR
125  * register
126  * @return returns reset reason
127  */
128 static resetSource_t STU_GetResetSourceWithoutFlagReset(void);
129 
130 /*========== Static Function Implementations ================================*/
131 
133  while (1) {
134  }
135 }
137  register resetSource_t rst_source;
138 
139  if ((SYS_EXCEPTION & (uint32)POWERON_RESET) != 0U) {
140  /* power-on reset condition */
141  rst_source = POWERON_RESET;
142  } else if ((SYS_EXCEPTION & (uint32)EXT_RESET) != 0U) {
143  /*** Check for other causes of EXT_RESET that would take precedence **/
144  if ((SYS_EXCEPTION & (uint32)OSC_FAILURE_RESET) != 0U) {
145  /* Reset caused due to oscillator failure. Add user code here to handle oscillator failure */
146  rst_source = OSC_FAILURE_RESET;
147  } else if ((SYS_EXCEPTION & (uint32)WATCHDOG_RESET) != 0U) {
148  /* Reset caused due watchdog violation */
149  rst_source = WATCHDOG_RESET;
150  } else if ((SYS_EXCEPTION & (uint32)WATCHDOG2_RESET) != 0U) {
151  /* Reset caused due watchdog violation */
152  rst_source = WATCHDOG2_RESET;
153  } else if ((SYS_EXCEPTION & (uint32)SW_RESET) != 0U) {
154  /* Reset caused due to software reset. */
155  rst_source = SW_RESET;
156  } else {
157  /* Reset caused due to External reset. */
158  rst_source = EXT_RESET;
159  }
160  } else if ((SYS_EXCEPTION & (uint32)DEBUG_RESET) != 0U) {
161  /* Reset caused due Debug reset request */
162  rst_source = DEBUG_RESET;
163  } else if ((SYS_EXCEPTION & (uint32)CPU0_RESET) != 0U) {
164  /* 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. */
165  rst_source = CPU0_RESET;
166  } else {
167  /* No_reset occurred. */
168  rst_source = NO_RESET;
169  }
170  return rst_source;
171 }
172 
173 /*========== Extern Function Implementations ================================*/
174 /** system entry point */
175 #pragma CODE_STATE(_c_int00, 32)
176 #pragma INTERRUPT(_c_int00, RESET)
177 /* SourceId : STARTUP_SourceId_001 */
178 /* DesignId : STARTUP_DesignId_001 */
179 /* Requirements : HL_CONQ_STARTUP_SR1 */
180 void _c_int00(void) {
181  register resetSource_t rstSrc;
182 
183  /* Initialize Core Registers to avoid CCM Error */
184  _coreInitRegisters_();
185 
186  /* Initialize Stack Pointers */
187  _coreInitStackPointer_();
188 
189  /* Reset handler: the following instructions read from the system exception status register
190  * to identify the cause of the CPU reset.
191  */
192  /* Changed in comparison to TI _c_int00 implementation. Readout flags but do NOT clear them! */
194  switch (rstSrc) {
195  case POWERON_RESET:
196  /* Initialize L2RAM to avoid ECC errors right after power on */
197  _memInit_();
198 
199  /* Add condition to check whether PLL can be started successfully */
200  if (_errata_SSWF021_45_both_plls(STU_PLL_RETRIES) != 0U) {
201  /* Put system in a safe state */
203  }
204 
205  case DEBUG_RESET:
206  case EXT_RESET:
207 
208  /* Initialize L2RAM to avoid ECC errors right after power on */
209  if (rstSrc != POWERON_RESET) {
210  _memInit_();
211  }
212 
213  /* Enable CPU Event Export */
214  /* This allows the CPU to signal any single-bit or double-bit errors detected
215  * by its ECC logic for accesses to program flash or data RAM.
216  */
217  _coreEnableEventBusExport_();
218 
219  /* Check if there were ESM group3 errors during power-up.
220  * These could occur during eFuse auto-load or during reads from flash OTP
221  * during power-up. Device operation is not reliable and not recommended
222  * in this case. */
223  if ((esmREG->SR1[2]) != 0U) {
224  esmGroup3Notification(esmREG, esmREG->SR1[2]);
225  }
226 
227  /* Initialize System - Clock, Flash settings with Efuse self check */
228  systemInit();
229 
230  /* Enable IRQ offset via Vic controller */
231  _coreEnableIrqVicOffset_();
232 
233  /* Initialize VIM table */
234  vimInit();
235 
236  /* Configure system response to error conditions signaled to the ESM group1 */
237  /* This function can be configured from the ESM tab of HALCoGen */
238  esmInit();
239  break;
240 
241  case OSC_FAILURE_RESET:
242  break;
243 
244  case WATCHDOG_RESET:
245  case WATCHDOG2_RESET:
246 
247  break;
248 
249  case CPU0_RESET:
250  /* Enable CPU Event Export */
251  /* This allows the CPU to signal any single-bit or double-bit errors detected
252  * by its ECC logic for accesses to program flash or data RAM.
253  */
254  _coreEnableEventBusExport_();
255  break;
256 
257  case SW_RESET:
258  break;
259 
260  default:
261  break;
262  }
263 
264  _mpuInit_();
265 
266  /* initialize global variable and constructors */
267  __TI_auto_init();
268 
269  /* call the application */
270  main();
271 
272  /* AXIVION Next Line Style MisraC2012-21.8: exit is called as in generated code by TI */
273  exit(0);
274 }
275 
276 /*========== Externalized Static Function Implementations (Unit Test) =======*/
void _c_int00(void)
Startup Routine.
Definition: fstartup.c:180
#define STU_PLL_RETRIES
Definition: fstartup.c:108
static void STU_HandlePllLockFail(void)
Handler for a failed PLL lock.
Definition: fstartup.c:132
static resetSource_t STU_GetResetSourceWithoutFlagReset(void)
Get reset flag.
Definition: fstartup.c:136
Startup code.
void __TI_auto_init(void)
initialize global variable and constructors
void exit(int _status)
exit application
Main function header.
int main(void)
main function of foxBMS
Definition: main.c:90