foxBMS  1.5.0
The foxBMS Battery Management System API Documentation
os.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 os.c
44  * @author foxBMS Team
45  * @date 2019-08-27 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup OS
49  * @prefix OS
50  *
51  * @brief Implementation of the tasks and resources used by the system
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "os.h"
57 
58 #include "fstd_types.h"
59 #include "ftask.h"
60 #include "rtc.h"
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Static Constant and Variable Definitions =======================*/
68 
69 /** timer counter since the 1ms task has started */
70 static OS_TIMER_s os_timer = {0u, 0u, 0u, 0u, 0u, 0u, 0u};
71 
72 /*========== Extern Constant and Variable Definitions =======================*/
73 /** boot state of the OS */
75 /** timestamp of the scheduler start */
76 uint32_t os_schedulerStartTime = 0u;
77 
78 /*========== Static Function Prototypes =====================================*/
79 
80 /*========== Static Function Implementations ================================*/
81 
82 /*========== Extern Function Implementations ================================*/
83 
85  /* Initialize the scheduler */
88  /* operating system configuration (Queues, Tasks) */
94 }
95 
96 /* AXIVION Disable Style Generic-MaxNesting: The program flow is simple
97  although it exceeds the maximum nesting level and changing the
98  implementation would not be beneficial for understanding the function. */
99 extern void OS_IncrementTimer(void) {
101  if (++os_timer.timer_1ms > 9u) { /* 10ms */
102  os_timer.timer_1ms = 0u;
103  if (++os_timer.timer_10ms > 9u) { /* 100ms */
104  os_timer.timer_10ms = 0u;
105  if (++os_timer.timer_100ms > 9u) { /* 1s */
106  os_timer.timer_100ms = 0u;
107  if (++os_timer.timer_sec > 59u) { /* 1min */
108  os_timer.timer_sec = 0u;
109  if (++os_timer.timer_min > 59u) { /* 1h */
110  os_timer.timer_min = 0u;
111  if (++os_timer.timer_h > 23u) { /* 1d */
112  os_timer.timer_h = 0u;
113  ++os_timer.timer_d;
114  }
115  }
116  }
117  }
118  }
119  }
120 }
121 /* AXIVION Enable Style Generic-MaxNesting: */
122 
123 /* AXIVION Next Codeline Style Generic-MissingParameterAssert: The function is designed and tested for full range */
125  uint32_t oldTimeStamp_ms,
126  uint32_t currentTimeStamp_ms,
127  uint32_t timeToPass_ms) {
128  /* AXIVION Next Codeline Style MisraC2012Directive-4.1: correct handling of underflows is checked with unit tests */
129  const uint32_t timeDifference_ms = currentTimeStamp_ms - oldTimeStamp_ms;
130  bool timeHasPassed = false;
131 
132  if (timeToPass_ms == 0u) {
133  timeHasPassed = true;
134  } else if (timeDifference_ms == 0u) {
135  /* case timeToPass_ms is 0u has already been extracted
136  therefore, only the cases where full UINT32_MAX time has passed
137  remain. By definition we will assume in this case that no time has
138  passed between these two timestamps and will therefore return false.
139  */
140  } else if (timeDifference_ms >= timeToPass_ms) {
141  timeHasPassed = true;
142  } else {
143  /* timeHasPassed is already false, nothing to do */
144  }
145 
146  return timeHasPassed;
147 }
148 
149 /* AXIVION Next Codeline Style Generic-MissingParameterAssert: The function is designed and tested for full range */
150 extern bool OS_CheckTimeHasPassed(uint32_t oldTimeStamp_ms, uint32_t timeToPass_ms) {
151  return OS_CheckTimeHasPassedWithTimestamp(oldTimeStamp_ms, OS_GetTickCount(), timeToPass_ms);
152 }
153 
155  STD_RETURN_TYPE_e selfCheckReturnValue = STD_OK;
156 
157  /* AXIVION Next Codeline Style MisraC2012-2.2 MisraC2012-14.3: If the code works as expected, this self test
158  function is expected to always return the same value */
159  if (OS_CheckTimeHasPassedWithTimestamp(0u, 0u, 0u) != true) {
160  /* AXIVION Next Codeline Style FaultDetection-UnusedAssignments: All cases collected, using each not
161  necessary. */
162  selfCheckReturnValue = STD_NOT_OK;
163  }
164 
165  if (OS_CheckTimeHasPassedWithTimestamp(0u, 1u, 1u) != true) {
166  /* AXIVION Next Codeline Style FaultDetection-UnusedAssignments: All cases collected, using each not
167  necessary. */
168  selfCheckReturnValue = STD_NOT_OK;
169  }
170 
171  if (OS_CheckTimeHasPassedWithTimestamp(1u, 2u, 2u) != false) {
172  /* AXIVION Next Codeline Style FaultDetection-UnusedAssignments: All cases collected, using each not
173  necessary. */
174  selfCheckReturnValue = STD_NOT_OK;
175  }
176 
177  if (OS_CheckTimeHasPassedWithTimestamp(0u, 1u, 0u) != true) {
178  /* AXIVION Next Codeline Style FaultDetection-UnusedAssignments: All cases collected, using each not
179  necessary. */
180  selfCheckReturnValue = STD_NOT_OK;
181  }
182 
183  if (OS_CheckTimeHasPassedWithTimestamp(1u, 0u, 1u) != true) {
184  selfCheckReturnValue = STD_NOT_OK;
185  }
186 
187  return selfCheckReturnValue;
188 }
189 
190 /*========== Externalized Static Function Implementations (Unit Test) =======*/
191 #ifdef UNITY_UNIT_TEST
192 extern OS_TIMER_s *TEST_OS_GetOsTimer() {
193  return &os_timer;
194 }
195 #endif
Definition of foxBMS standard types.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
Header of task driver implementation.
void FTSK_CreateTasks(void)
Creates all tasks of the group.
void FTSK_CreateQueues(void)
Creates all queues.
uint32_t os_schedulerStartTime
Scheduler "zero" time for task phase control.
Definition: os.c:76
bool OS_CheckTimeHasPassed(uint32_t oldTimeStamp_ms, uint32_t timeToPass_ms)
This function checks if timeToPass has passed since the last timestamp to now.
Definition: os.c:150
STD_RETURN_TYPE_e OS_CheckTimeHasPassedSelfTest(void)
Does a self check if the OS_CheckTimeHasPassedWithTimestamp works as expected.
Definition: os.c:154
bool OS_CheckTimeHasPassedWithTimestamp(uint32_t oldTimeStamp_ms, uint32_t currentTimeStamp_ms, uint32_t timeToPass_ms)
This function checks if timeToPass has passed since the last timestamp to now.
Definition: os.c:124
void OS_InitializeOperatingSystem(void)
Initialization the RTOS interface.
Definition: os.c:84
void OS_IncrementTimer(void)
Increments the system timer os_timer.
Definition: os.c:99
static OS_TIMER_s os_timer
Definition: os.c:70
volatile OS_BOOT_STATE_e os_boot
Definition: os.c:74
Declaration of the OS wrapper interface.
OS_BOOT_STATE_e
enum of OS boot states
Definition: os.h:105
@ OS_CREATE_TASKS
Definition: os.h:109
@ OS_INITIALIZE_SCHEDULER
Definition: os.h:107
@ OS_CREATE_QUEUES
Definition: os.h:108
@ OS_INIT_PRE_OS
Definition: os.h:110
@ OS_OFF
Definition: os.h:106
void OS_InitializeScheduler(void)
Initialization function for the scheduler.
Definition: os_freertos.c:75
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:142
void RTC_IncrementSystemTime(void)
increment the RTC system timer.
Definition: rtc.c:586
Header file of the RTC driver.
OS timer.
Definition: os.h:121
uint8_t timer_min
Definition: os.h:126
uint8_t timer_1ms
Definition: os.h:122
uint8_t timer_sec
Definition: os.h:125
uint8_t timer_100ms
Definition: os.h:124
uint8_t timer_10ms
Definition: os.h:123
uint8_t timer_h
Definition: os.h:127
uint16_t timer_d
Definition: os.h:128