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