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