foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
sys_mon.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 sys_mon.c
44  * @author foxBMS Team
45  * @date 2019-11-28 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup ENGINE
49  * @prefix SYSM
50  *
51  * @brief system monitoring module
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "sys_mon.h"
56 
57 #include "diag.h"
58 #include "fram.h"
59 #include "os.h"
60 
61 #include <stdint.h>
62 
63 /*========== Macros and Definitions =========================================*/
64 
65 /*========== Static Constant and Variable Definitions =======================*/
66 /** tracking variable for System monitoring notifications */
68 
69 /** local shadow copy of the FRAM entry */
71 
72 /** flag, indicating that the FRAM entry has been changed and should be written */
73 static volatile bool sysm_flagFramCopyHasChanges = false;
74 
75 /*========== Extern Constant and Variable Definitions =======================*/
76 
77 /*========== Static Function Prototypes =====================================*/
78 /**
79  * @brief Records the violation of a timing in non volatile memory
80  *
81  * @param[in] taskId id of the violating task
82  * @param[in] taskDuration duration of the task when recording the violation
83  * @param[in] timestampEnter timestamp that the task has entered
84  */
85 static void SYSM_RecordTimingViolation(SYSM_TASK_ID_e taskId, uint32_t taskDuration, uint32_t timestampEnter);
86 
87 /**
88  * @brief Convert recorded timings into a decision (bool) whether the timing has been violated or not
89  * @details Timing has been violated if both duration or timestamp are not equal to zero
90  * @param[in] duration duration in ticks
91  * @param[in] timestampEnter entry timestamp in ticks
92  * @returns bool describing whether this is a violation (true) or not (false)
93  */
94 static bool SYSM_ConvertRecordedTimingsToViolation(uint32_t duration, uint32_t timestampEnter);
95 
96 /*========== Static Function Implementations ================================*/
97 static void SYSM_RecordTimingViolation(SYSM_TASK_ID_e taskId, uint32_t taskDuration, uint32_t timestampEnter) {
98  FAS_ASSERT(taskId < SYSM_TASK_ID_MAX);
99 
102 
103  switch (taskId) {
104  case SYSM_TASK_ID_ENGINE:
107  break;
110  sysm_localFramCopy.task1msEnterTimestamp = timestampEnter;
111  break;
115  break;
119  break;
123  break;
124  default:
125  /* this should not be possible to happen */
127  break;
128  }
129 }
130 
131 static bool SYSM_ConvertRecordedTimingsToViolation(uint32_t duration, uint32_t timestampEnter) {
132  bool returnValue = false;
133  if ((duration != 0u) || (timestampEnter != 0u)) {
134  returnValue = true;
135  }
136  return returnValue;
137 }
138 
139 /*========== Extern Function Implementations ================================*/
141  /* no need to check for the configuration as it is already checked with a
142  static assert */
143 
144  /* read from FRAM interface the sys mon violations */
146  /* copy FRAM into local shadow copy */
148 
149  return STD_OK;
150 }
151 
153  static uint32_t sysm_timestamp = 0;
154 
155  /** early exit if nothing to do */
156  uint32_t local_timer = OS_GetTickCount();
157  if (sysm_timestamp == local_timer) {
158  return;
159  }
160  sysm_timestamp = local_timer;
161 
162  uint32_t time_since_last_call = 0;
163  uint32_t max_allowed_jitter = 0;
164 
165  for (SYSM_TASK_ID_e taskId = (SYSM_TASK_ID_e)0; taskId < SYSM_TASK_ID_MAX; taskId++) {
166  if (sysm_ch_cfg[taskId].enable == SYSM_ENABLED) {
167  /* check that the task gets called within its timing threshold plus jitter and
168  then check that the tasks duration is shorter than tasks cycle time */
169  time_since_last_call = local_timer - sysm_notifications[taskId].timestampEnter;
170  max_allowed_jitter = sysm_ch_cfg[taskId].cycleTime + sysm_ch_cfg[taskId].maxJitter;
171  if ((time_since_last_call > max_allowed_jitter) &&
172  (sysm_notifications[taskId].duration > sysm_ch_cfg[taskId].cycleTime)) {
173  /* module not running within its timed limits */
175  if (sysm_ch_cfg[taskId].enableRecording == SYSM_RECORDING_ENABLED) {
177  taskId, sysm_notifications[taskId].duration, sysm_notifications[taskId].timestampEnter);
178  }
179  sysm_ch_cfg[taskId].callbackFunction(taskId);
180  }
181  }
182  }
183 }
184 
185 void SYSM_Notify(SYSM_TASK_ID_e taskId, SYSM_NOTIFY_TYPE_e state, uint32_t timestamp) {
186  FAS_ASSERT(taskId < SYSM_TASK_ID_MAX);
187  FAS_ASSERT((state == SYSM_NOTIFY_ENTER) || (state == SYSM_NOTIFY_EXIT));
189  sysm_notifications[taskId].state = state;
190  if (state == SYSM_NOTIFY_ENTER) {
191  sysm_notifications[taskId].timestampEnter = timestamp;
192  } else if (state == SYSM_NOTIFY_EXIT) {
193  sysm_notifications[taskId].timestampExit = timestamp;
194  sysm_notifications[taskId].duration = timestamp - sysm_notifications[taskId].timestampEnter;
195  } else {
196  /* state has an illegal value */
198  }
200 }
201 
203  FAS_ASSERT(pAnswer != NULL_PTR);
204 
206  FRAM_SYS_MON_RECORD_s localSysMonRecord = {0};
207  SYSM_CopyFramStruct(&fram_sys_mon_record, &localSysMonRecord);
209 
210  if (localSysMonRecord.anyTimingIssueOccurred == false) {
211  /* no timing violations recorded, abort */
212  pAnswer->recordedViolationAny = false;
213  pAnswer->recordedViolationEngine = false;
214  pAnswer->recordedViolation1ms = false;
215  pAnswer->recordedViolation10ms = false;
216  pAnswer->recordedViolation100ms = false;
217  pAnswer->recordedViolation100msAlgo = false;
218  } else {
219  /* timing violation, find it */
220  pAnswer->recordedViolationAny = true;
222  localSysMonRecord.taskEngineViolatingDuration, localSysMonRecord.taskEngineEnterTimestamp);
224  localSysMonRecord.task1msViolatingDuration, localSysMonRecord.task1msEnterTimestamp);
226  localSysMonRecord.task10msViolatingDuration, localSysMonRecord.task10msEnterTimestamp);
228  localSysMonRecord.task100msViolatingDuration, localSysMonRecord.task100msEnterTimestamp);
230  localSysMonRecord.task100msAlgorithmViolatingDuration, localSysMonRecord.task100msAlgorithmEnterTimestamp);
231  }
232 }
233 
234 extern void SYSM_ClearAllTimingViolations(void) {
235  /* clear all diag entries */
236  for (SYSM_TASK_ID_e taskId = (SYSM_TASK_ID_e)0; taskId < SYSM_TASK_ID_MAX; taskId++) {
238  }
239  /* clear local FRAM copy */
254  /* commit to FRAM */
256 }
257 
258 extern void SYSM_UpdateFramData(void) {
260  const bool updateNecessary = sysm_flagFramCopyHasChanges;
262  if (updateNecessary == true) {
267 
269  }
270 }
271 
272 extern void SYSM_CopyFramStruct(const FRAM_SYS_MON_RECORD_s *const kpkFrom, FRAM_SYS_MON_RECORD_s *pTo) {
273  FAS_ASSERT(kpkFrom != NULL_PTR);
274  FAS_ASSERT(pTo != NULL_PTR);
275 
276  /* copy all elements in order to make sure that we are always compatible (independently of C-version) */
288 }
289 
290 /*========== Getter for static Variables (Unit Test) ========================*/
291 #ifdef UNITY_UNIT_TEST
292 extern SYSM_NOTIFICATION_s *TEST_SYSM_GetNotifications(void) {
293  return sysm_notifications;
294 }
295 #endif
296 
297 /*========== Externalized Static Function Implementations (Unit Test) =======*/
298 #ifdef UNITY_UNIT_TEST
299 #endif
DIAG_RETURNTYPE_e DIAG_Handler(DIAG_ID_e diagId, DIAG_EVENT_e event, DIAG_IMPACT_LEVEL_e impact, uint32_t data)
DIAG_Handler provides generic error handling, based on diagnosis group.
Definition: diag.c:246
Diagnosis driver header.
@ DIAG_EVENT_NOT_OK
Definition: diag_cfg.h:268
@ DIAG_EVENT_OK
Definition: diag_cfg.h:267
@ DIAG_SYSTEM
Definition: diag_cfg.h:280
@ DIAG_ID_SYSTEM_MONITORING
Definition: diag_cfg.h:178
#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
FRAM_RETURN_TYPE_e FRAM_ReadData(FRAM_BLOCK_ID_e blockId)
Reads a variable from the FRAM.
Definition: fram.c:211
FRAM_RETURN_TYPE_e FRAM_WriteData(FRAM_BLOCK_ID_e blockId)
Writes a variable to the FRAM.
Definition: fram.c:133
Header for the driver for the FRAM module.
FRAM_SYS_MON_RECORD_s fram_sys_mon_record
Definition: fram_cfg.c:78
@ FRAM_BLOCK_ID_SYS_MON_RECORD
Definition: fram_cfg.h:110
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
Declaration of the OS wrapper interface.
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:154
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:150
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:158
struct that stores for each task the last violation of timing
Definition: fram_cfg.h:170
uint32_t taskEngineViolatingDuration
Definition: fram_cfg.h:174
uint32_t taskEngineEnterTimestamp
Definition: fram_cfg.h:176
uint32_t task10msViolatingDuration
Definition: fram_cfg.h:182
uint32_t task100msViolatingDuration
Definition: fram_cfg.h:186
uint32_t task100msEnterTimestamp
Definition: fram_cfg.h:188
uint32_t task1msViolatingDuration
Definition: fram_cfg.h:178
uint32_t task100msAlgorithmEnterTimestamp
Definition: fram_cfg.h:192
uint32_t task10msEnterTimestamp
Definition: fram_cfg.h:184
uint32_t task1msEnterTimestamp
Definition: fram_cfg.h:180
uint32_t task100msAlgorithmViolatingDuration
Definition: fram_cfg.h:190
void(* callbackFunction)(SYSM_TASK_ID_e taskId)
Definition: sys_mon_cfg.h:103
uint32_t duration
Definition: sys_mon.h:76
uint32_t timestampEnter
Definition: sys_mon.h:74
uint32_t timestampExit
Definition: sys_mon.h:75
SYSM_NOTIFY_TYPE_e state
Definition: sys_mon.h:73
void SYSM_GetRecordedTimingViolations(SYSM_TIMING_VIOLATION_RESPONSE_s *pAnswer)
Returns the timing violation flags determined from fram state.
Definition: sys_mon.c:202
void SYSM_ClearAllTimingViolations(void)
Clears all timing violations (both current and recorded)
Definition: sys_mon.c:234
void SYSM_CheckNotifications(void)
overall system monitoring
Definition: sys_mon.c:152
void SYSM_Notify(SYSM_TASK_ID_e taskId, SYSM_NOTIFY_TYPE_e state, uint32_t timestamp)
Sets needed bits to indicate that a task is running.
Definition: sys_mon.c:185
void SYSM_UpdateFramData(void)
Commits the stored changes to FRAM if necessary.
Definition: sys_mon.c:258
static void SYSM_RecordTimingViolation(SYSM_TASK_ID_e taskId, uint32_t taskDuration, uint32_t timestampEnter)
Records the violation of a timing in non volatile memory.
Definition: sys_mon.c:97
STD_RETURN_TYPE_e SYSM_Init(void)
initialization function for system monitoring
Definition: sys_mon.c:140
static volatile bool sysm_flagFramCopyHasChanges
Definition: sys_mon.c:73
static SYSM_NOTIFICATION_s sysm_notifications[SYSM_TASK_ID_MAX]
Definition: sys_mon.c:67
static bool SYSM_ConvertRecordedTimingsToViolation(uint32_t duration, uint32_t timestampEnter)
Convert recorded timings into a decision (bool) whether the timing has been violated or not.
Definition: sys_mon.c:131
static FRAM_SYS_MON_RECORD_s sysm_localFramCopy
Definition: sys_mon.c:70
void SYSM_CopyFramStruct(const FRAM_SYS_MON_RECORD_s *const kpkFrom, FRAM_SYS_MON_RECORD_s *pTo)
Copy from the from entry to the to entry.
Definition: sys_mon.c:272
system monitoring module
SYSM_NOTIFY_TYPE_e
Definition: sys_mon.h:66
@ SYSM_NOTIFY_ENTER
Definition: sys_mon.h:67
@ SYSM_NOTIFY_EXIT
Definition: sys_mon.h:68
SYSM_MONITORING_CFG_s sysm_ch_cfg[]
Definition: sys_mon_cfg.c:72
@ SYSM_RECORDING_ENABLED
Definition: sys_mon_cfg.h:85
@ SYSM_ENABLED
Definition: sys_mon_cfg.h:91
SYSM_TASK_ID_e
Definition: sys_mon_cfg.h:74
@ SYSM_TASK_ID_CYCLIC_10ms
Definition: sys_mon_cfg.h:77
@ SYSM_TASK_ID_MAX
Definition: sys_mon_cfg.h:80
@ SYSM_TASK_ID_CYCLIC_ALGORITHM_100ms
Definition: sys_mon_cfg.h:79
@ SYSM_TASK_ID_CYCLIC_100ms
Definition: sys_mon_cfg.h:78
@ SYSM_TASK_ID_CYCLIC_1ms
Definition: sys_mon_cfg.h:76
@ SYSM_TASK_ID_ENGINE
Definition: sys_mon_cfg.h:75