foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
ftask_cfg.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 ftask_cfg.c
44  * @author foxBMS Team
45  * @date 2019-08-26 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup TASK_CONFIGURATION
49  * @prefix FTSK
50  *
51  * @brief Task configuration
52  * @details
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "ftask_cfg.h"
57 
58 #include "HL_gio.h"
59 #include "HL_het.h"
60 
61 #include "adc.h"
62 #include "algorithm.h"
63 #include "bal.h"
64 #include "bms.h"
65 #include "can.h"
66 #include "contactor.h"
67 #include "database.h"
68 #include "diag.h"
69 #include "dma.h"
70 #include "fram.h"
71 #include "htsensor.h"
72 #include "i2c.h"
73 #include "imd.h"
74 #include "interlock.h"
75 #include "led.h"
76 #include "meas.h"
77 #include "pex.h"
78 #include "redundancy.h"
79 #include "rtc.h"
80 #include "sbc.h"
81 #include "sof_trapezoid.h"
82 #include "spi.h"
83 #include "sps.h"
84 #include "state_estimation.h"
85 #include "sys.h"
86 #include "sys_mon.h"
87 
88 #include <stdint.h>
89 
90 /*========== Macros and Definitions =========================================*/
91 
92 /** counter value for 50ms in 10ms task */
93 #define TASK_10MS_COUNTER_FOR_50MS (5u)
94 
95 /** counter value for 1s in 100ms task */
96 #define TASK_100MS_COUNTER_FOR_1S (10u)
97 
98 /*========== Static Constant and Variable Definitions =======================*/
99 
100 /*========== Extern Constant and Variable Definitions =======================*/
101 
102 /**
103  * @brief Definition of the engine task
104  * @details Task is not delayed after the scheduler starts. This task must
105  * have the highest priority.
106  * @warning Do not change the configuration of this task. This will very
107  * likely break the system.
108  */
151 
152 /*========== Static Function Prototypes =====================================*/
153 
154 /*========== Static Function Implementations ================================*/
155 
156 /*========== Extern Function Implementations ================================*/
157 extern void FTSK_InitializeUserCodeEngine(void) {
158  /* Warning: Do not change the content of this function */
159  /* See function definition doxygen comment for details */
161 
162  if (retval == STD_NOT_OK) {
164  }
165 
166  /* Suspend AFE task if unused, otherwise it will preempt all lower priority tasks */
167 #if (FOXBMS_AFE_DRIVER_TYPE_FSM == 1)
168  vTaskSuspend(ftsk_taskHandleAfe);
169 #endif
170 
171  /* Init FRAM */
172  FRAM_Initialize();
173 
174  retval = SYSM_Init();
175 
176  if (retval == STD_NOT_OK) {
178  }
179 
180  /* Warning: Do not change the content of this function */
181  /* See function definition doxygen comment for details */
182 }
183 
184 extern void FTSK_RunUserCodeEngine(void) {
185  /* Warning: Do not change the content of this function */
186  /* See function definition doxygen comment for details */
187  DATA_Task(); /* Call database manager */
188  SYSM_CheckNotifications(); /* Check notifications from tasks */
189  /* Warning: Do not change the content of this function */
190  /* See function definition doxygen comment for details */
191 }
192 
194  /* user code */
196 
197  /* Init Sys */
199 
200  /* Init port expander */
201  PEX_Initialize();
202 
203  /* Set 3rd PE pin to activate temperature/humidity sensor */
206 
207  CONT_Initialize();
208  SPS_Initialize();
209  (void)MEAS_Initialize(); /* cast to void as the return value is unused */
210 
211  /* Initialize redundancy module */
212  (void)MRC_Initialize();
213 
214  /* This function operates under the assumption that it is called when
215  * the operating system is not yet running.
216  * In this state the return value of #SYS_SetStateRequest should
217  * always be #SYS_OK. Therefore we trap otherwise.
218  */
219  FAS_ASSERT(sys_retVal == SYS_OK);
220 
221  /* System started correctly -> Start toggling of debug LED */
223 }
224 
225 extern void FTSK_RunUserCodeCyclic1ms(void) {
226  /* Increment of operating system timer */
227  /* This must not be changed, add user code only below */
230  /* user code */
231 #if (FOXBMS_AFE_DRIVER_TYPE_FSM == 1)
232  MEAS_Control();
233 #endif
235 }
236 
237 extern void FTSK_RunUserCodeCyclic10ms(void) {
238  static uint8_t ftsk_cyclic10msCounter = 0;
239  /* user code */
242  ILCK_Trigger();
243  ADC_Control();
244  SPS_Ctrl();
246  SOF_Calculation();
249 
250  if (ftsk_cyclic10msCounter == TASK_10MS_COUNTER_FOR_50MS) {
253  ftsk_cyclic10msCounter = 0;
254  }
255  /* Call BMS_Trigger function at the end of the 10ms task to allow previously
256  * called modules in this task to update respectively evaluate their new.
257  * This minimizes the delay between data evaluation and the reaction from
258  * the BMS module */
259  BMS_Trigger();
260  ftsk_cyclic10msCounter++;
261 }
262 
263 extern void FTSK_RunUserCodeCyclic100ms(void) {
264  /* user code */
265  static uint8_t ftsk_cyclic100msCounter = 0;
266 
267  /** Perform SOC and SOE calculations only every 1s. Not suited if analog
268  * integration of current sensor is NOT used. Manual integration of current
269  * requires a higher frequency.
270  */
271  if (ftsk_cyclic100msCounter == TASK_100MS_COUNTER_FOR_1S) {
273  ftsk_cyclic100msCounter = 0;
274  }
275 
276  BAL_Trigger();
277  IMD_Trigger();
278  LED_Trigger();
279 
280  ftsk_cyclic100msCounter++;
281 }
282 
284  /* user code */
285  static uint8_t ftsk_cyclicAlgorithm100msCounter = 0;
286 
288 
289  ftsk_cyclicAlgorithm100msCounter++;
290 }
291 
293  /* user code */
294  PEX_Trigger();
295  HTSEN_Trigger();
296  RTC_Trigger();
297  uint32_t current_time = OS_GetTickCount();
298  OS_DelayTaskUntil(&current_time, 2u);
299 }
300 
302  /* user code */
303 #if (FOXBMS_AFE_DRIVER_TYPE_NO_FSM == 1)
304  MEAS_Control();
305 #endif
306 }
307 
308 extern void FTSK_RunUserCodeIdle(void) {
309  /* user code */
310 }
311 
312 /*========== Externalized Static Function Implementations (Unit Test) =======*/
313 #ifdef UNITY_UNIT_TEST
314 #endif
void ADC_Control(void)
controls ADC measurement sequence.
Definition: adc.c:104
Headers for the driver for the ADC module.
void ALGO_MainFunction(void)
handles the call of different algorithm functions when cycle time has expired
Definition: algorithm.c:116
void ALGO_MonitorExecutionTime(void)
monitors the calculation duration of the different algorithms
Definition: algorithm.c:155
Headers for the driver for the storage in the EEPROM memory.
Header for the driver for balancing.
void BAL_Trigger(void)
trigger function for the BAL driver state machine.
void BMS_Trigger(void)
trigger function for the BMS driver state machine.
Definition: bms.c:825
bms driver header
void CAN_MainFunction(void)
Calls the functions to drive the CAN interface. Makes the CAN timing checks and sends the periodic me...
Definition: can.c:981
void CAN_ReadRxBuffer(void)
Checks the data received per CAN. A receive buffer is used because CAN frames are received in an inte...
Definition: can.c:988
Header for the driver for the CAN module.
void CONT_Initialize(void)
initializes the contactor module
Definition: contactor.c:251
Headers for the driver for the contactors.
void DATA_Task(void)
trigger of database manager
Definition: database.c:279
STD_RETURN_TYPE_e DATA_Initialize(void)
Initialization of database manager.
Definition: database.c:229
Database module header.
Diagnosis driver header.
void DIAG_UpdateFlags(void)
update function for diagnosis flags
Definition: diag_cfg.c:250
Headers for the driver for the DMA module.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:129
void FRAM_Initialize(void)
Initializes the addresses to be written in the FRAM.
Definition: fram.c:96
Header for the driver for the FRAM module.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_NOT_OK
Definition: fstd_types.h:84
OS_TASK_DEFINITION_s ftsk_taskDefinitionAfe
Task configuration of the continuously running task for AFEs.
Definition: ftask_cfg.c:145
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclic100ms
Task configuration of the cyclic 100 ms task.
Definition: ftask_cfg.c:127
void FTSK_RunUserCodeAfe(void)
Continuously running task for AFEs.
Definition: ftask_cfg.c:301
void FTSK_InitializeUserCodeEngine(void)
Initializes the database.
Definition: ftask_cfg.c:157
void FTSK_RunUserCodeEngine(void)
Engine task for the database and the system monitoring module.
Definition: ftask_cfg.c:184
OS_TASK_DEFINITION_s ftsk_taskDefinitionEngine
Definition of the engine task.
Definition: ftask_cfg.c:109
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclic1ms
Task configuration of the cyclic 1 ms task.
Definition: ftask_cfg.c:115
#define TASK_100MS_COUNTER_FOR_1S
Definition: ftask_cfg.c:96
void FTSK_RunUserCodeIdle(void)
Idle task.
Definition: ftask_cfg.c:308
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclicAlgorithm100ms
Task configuration of the cyclic 100 ms task for algorithms.
Definition: ftask_cfg.c:133
void FTSK_RunUserCodeCyclic100ms(void)
Cyclic 100 ms task.
Definition: ftask_cfg.c:263
void FTSK_RunUserCodeI2c(void)
Continuously running task for I2C.
Definition: ftask_cfg.c:292
void FTSK_InitializeUserCodePreCyclicTasks(void)
Initialization function before all tasks started.
Definition: ftask_cfg.c:193
void FTSK_RunUserCodeCyclic10ms(void)
Cyclic 10 ms task.
Definition: ftask_cfg.c:237
OS_TASK_DEFINITION_s ftsk_taskDefinitionI2c
Task configuration of the continuously running task for MCU I2C communication.
Definition: ftask_cfg.c:139
#define TASK_10MS_COUNTER_FOR_50MS
Definition: ftask_cfg.c:93
void FTSK_RunUserCodeCyclic1ms(void)
Cyclic 1 ms task.
Definition: ftask_cfg.c:225
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclic10ms
Task configuration of the cyclic 10 ms task.
Definition: ftask_cfg.c:121
void FTSK_RunUserCodeCyclicAlgorithm100ms(void)
Cyclic 100 ms task for algorithms.
Definition: ftask_cfg.c:283
Task configuration header.
#define FTSK_TASK_CYCLIC_1MS_STACK_SIZE_IN_BYTES
Stack size of cyclic 1 ms task.
Definition: ftask_cfg.h:84
#define FTSK_TASK_CYCLIC_10MS_PRIORITY
Priority of cyclic 10 ms task.
Definition: ftask_cfg.h:105
#define FTSK_TASK_I2C_PV_PARAMETERS
pvParameters of the continuously running task for I2C
Definition: ftask_cfg.h:168
#define FTSK_TASK_ENGINE_STACK_SIZE_IN_BYTES
Stack size of engine task.
Definition: ftask_cfg.h:66
#define FTSK_TASK_I2C_CYCLE_TIME
Cycle time of continuously running task for I2C.
Definition: ftask_cfg.h:165
#define FTSK_TASK_CYCLIC_1MS_PRIORITY
Priority of cyclic 1ms task.
Definition: ftask_cfg.h:87
#define FTSK_TASK_CYCLIC_100MS_STACK_SIZE_IN_BYTES
Stack size of cyclic 100 ms task.
Definition: ftask_cfg.h:120
#define FTSK_TASK_CYCLIC_10MS_PV_PARAMETERS
pvParameters of the 10ms task
Definition: ftask_cfg.h:117
#define FTSK_TASK_CYCLIC_1MS_CYCLE_TIME
Cycle time of 1ms task.
Definition: ftask_cfg.h:93
#define FTSK_TASK_AFE_STACK_SIZE_IN_BYTES
Stack size of continuously running task for AFEs.
Definition: ftask_cfg.h:171
#define FTSK_TASK_I2C_STACK_SIZE_IN_BYTES
Stack size of continuously running task for I2C.
Definition: ftask_cfg.h:156
#define FTSK_TASK_CYCLIC_10MS_STACK_SIZE_IN_BYTES
Stack size of cyclic 10 ms task.
Definition: ftask_cfg.h:102
#define FTSK_TASK_I2C_PRIORITY
Priority of continuously running task for I2C.
Definition: ftask_cfg.h:159
#define FTSK_TASK_ENGINE_CYCLE_TIME
Cycle time of engine task.
Definition: ftask_cfg.h:75
#define FTSK_TASK_CYCLIC_ALGORITHM_100MS_PRIORITY
Priority of cyclic 100 ms task for algorithms.
Definition: ftask_cfg.h:141
#define FTSK_TASK_CYCLIC_ALGORITHM_100MS_CYCLE_TIME
Cycle time of 100ms task for algorithms.
Definition: ftask_cfg.h:147
#define FTSK_TASK_CYCLIC_100MS_PV_PARAMETERS
pvParameters of the 100ms task
Definition: ftask_cfg.h:135
#define FTSK_TASK_CYCLIC_10MS_CYCLE_TIME
Cycle time of 10 ms task.
Definition: ftask_cfg.h:111
#define FTSK_TASK_CYCLIC_ALGORITHM_100MS_PV_PARAMETERS
pvParameters of the 100ms task for algorithms
Definition: ftask_cfg.h:153
#define FTSK_TASK_I2C_PHASE
Phase of continuously running task for I2C.
Definition: ftask_cfg.h:162
OS_TASK_HANDLE ftsk_taskHandleAfe
Definition of task handles.
#define FTSK_TASK_ENGINE_PRIORITY
Phase of engine task.
Definition: ftask_cfg.h:69
#define FTSK_TASK_CYCLIC_100MS_PRIORITY
Priority of cyclic 100 ms task.
Definition: ftask_cfg.h:123
#define FTSK_TASK_AFE_CYCLE_TIME
Cycle time of continuously running task for AFEs.
Definition: ftask_cfg.h:180
#define FTSK_TASK_AFE_PHASE
Phase of continuously running task for AFEs.
Definition: ftask_cfg.h:177
#define FTSK_TASK_ENGINE_PV_PARAMETERS
pvParameters of the engine task
Definition: ftask_cfg.h:81
#define FTSK_TASK_CYCLIC_1MS_PV_PARAMETERS
pvParameters of the 1ms task
Definition: ftask_cfg.h:99
#define FTSK_TASK_CYCLIC_100MS_CYCLE_TIME
Cycle time of 100ms task.
Definition: ftask_cfg.h:129
#define FTSK_TASK_AFE_PV_PARAMETERS
pvParameters of the continuously running task for AFEs
Definition: ftask_cfg.h:183
#define FTSK_TASK_CYCLIC_100MS_PHASE
Phase of cyclic 100 ms task.
Definition: ftask_cfg.h:126
#define FTSK_TASK_CYCLIC_ALGORITHM_100MS_PHASE
Phase of cyclic 100 ms task for algorithms.
Definition: ftask_cfg.h:144
#define FTSK_TASK_CYCLIC_1MS_PHASE
Phase of cyclic 1ms task.
Definition: ftask_cfg.h:90
#define FTSK_TASK_CYCLIC_ALGORITHM_100MS_STACK_SIZE_IN_BYTES
Stack size of cyclic 100 ms task for algorithms.
Definition: ftask_cfg.h:138
#define FTSK_TASK_AFE_PRIORITY
Priority of continuously running task for AFEs.
Definition: ftask_cfg.h:174
#define FTSK_TASK_CYCLIC_10MS_PHASE
Phase of cyclic 10 ms task.
Definition: ftask_cfg.h:108
#define FTSK_TASK_ENGINE_PHASE
Phase of engine task.
Definition: ftask_cfg.h:72
void HTSEN_Trigger(void)
triggers a measurement of the I2C humidity/temperature sensor.
Definition: htsensor.c:223
Header for the driver for the Sensirion SHT35-DIS I2C humidity/temperature sensor.
Header for the driver for the I2C module.
STD_RETURN_TYPE_e IMD_Trigger(void)
trigger function for the IMD driver state machine.
Definition: imd.c:528
API header for the insulation monitoring device.
void ILCK_Trigger(void)
trigger function for the ILCK driver state machine.
Definition: interlock.c:268
Headers for the driver for the interlock.
void LED_SetToggleTime(uint32_t onOffTime_ms)
Set debug LED with defined frequency.
Definition: led.c:114
void LED_Trigger(void)
Trigger function to periodically toggle debug LED. Frequency can be set via function LED_SetToggleTim...
Definition: led.c:91
Header file of the debug LED driver.
#define LED_NORMAL_OPERATION_ON_OFF_TIME_ms
Definition: led.h:74
void MEAS_Control(void)
Control function that is called from the task every 1ms.
Definition: meas.c:79
STD_RETURN_TYPE_e MEAS_Initialize(void)
Definition: meas.c:121
Headers for the driver for the measurements needed by the BMS (e.g., I,V,T).
void OS_IncrementTimer(void)
Increments the system timer os_timer.
Definition: os.c:99
void OS_DelayTaskUntil(uint32_t *pPreviousWakeTime, uint32_t milliseconds)
Delay a task until a specified time.
Definition: os_freertos.c:162
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:158
void PEX_SetPin(uint8_t portExpander, uint8_t pin)
sets pin to high.
Definition: pex.c:336
void PEX_SetPinDirectionOutput(uint8_t portExpander, uint8_t pin)
sets pin to input.
Definition: pex.c:409
void PEX_Initialize(void)
initialize local variable containing state of port expander.
Definition: pex.c:290
void PEX_Trigger(void)
implements reading/writing to the port expander registers.
Definition: pex.c:312
Header for the driver for the NXP PCA9539 port expander module.
#define PEX_PIN00
Definition: pex_cfg.h:82
#define PEX_PORT_EXPANDER3
Definition: pex_cfg.h:77
STD_RETURN_TYPE_e MRC_Initialize(void)
Function to initalize redundancy module.
Definition: redundancy.c:1078
STD_RETURN_TYPE_e MRC_ValidateAfeMeasurement(void)
Function to validate the measurement between redundant measurement values for cell voltage and cell t...
Definition: redundancy.c:1103
STD_RETURN_TYPE_e MRC_ValidatePackMeasurement(void)
Function to validate the measurements of pack values (string values, pack values)
Definition: redundancy.c:1144
Header fileS for handling redundancy between redundant cell voltage and cell temperature measurements...
void RTC_Trigger(void)
trigger function for the RTC driver.
Definition: rtc.c:414
Header file of the RTC driver.
void SBC_Trigger(SBC_STATE_s *pInstance)
trigger function for the SYS driver state machine.
Definition: sbc.c:275
SBC_STATE_s sbc_stateMcuSupervisor
Definition: sbc.c:78
Header for the driver for the SBC module.
void SOF_Calculation(void)
triggers SOF calculation
Header for SOX module, responsible for current derating calculation.
Headers for the driver for the SPI module.
void SPS_Initialize(void)
Initialize IOs for the SPS driver.
Definition: sps.c:598
void SPS_Ctrl(void)
Control function for the CONT driver state machine.
Definition: sps.c:481
Headers for the driver for the smart power switches.
void SE_RunStateEstimations(void)
Main function to perform state estimations.
Header for state-estimation module responsible for the estimation of state-of-charge (SOC),...
struct for FreeRTOS task definition
Definition: os.h:135
SYS_RETURN_TYPE_e SYS_SetStateRequest(SYS_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable sys_state.
Definition: sys.c:569
SYS_STATE_s sys_state
Definition: sys.c:98
STD_RETURN_TYPE_e SYS_Trigger(SYS_STATE_s *pSystemState)
tick function, call this to advance the state machine
Definition: sys.c:626
Sys driver header.
SYS_RETURN_TYPE_e
Definition: sys.h:157
@ SYS_OK
Definition: sys.h:158
@ SYS_ILLEGAL_REQUEST
Definition: sys.h:161
@ SYS_STATE_INITIALIZATION_REQUEST
Definition: sys.h:151
void SYSM_CheckNotifications(void)
overall system monitoring
Definition: sys_mon.c:152
void SYSM_UpdateFramData(void)
Commits the stored changes to FRAM if necessary.
Definition: sys_mon.c:258
STD_RETURN_TYPE_e SYSM_Init(void)
initialization function for system monitoring
Definition: sys_mon.c:140
system monitoring module