foxBMS - Unit Tests  1.1.0
The foxBMS Unit Tests 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-07-23 (date of last update)
47  * @ingroup OS
48  * @prefix OS
49  *
50  * @brief Implementation of the tasks 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 /** stack size of the idle task */
62 #define OS_IDLE_TASK_STACK_SIZE configMINIMAL_STACK_SIZE
63 
64 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
65 #define OS_TIMER_TASK_STACK_SIZE configTIMER_TASK_STACK_DEPTH
66 #endif /* configUSE_TIMERS */
67 
68 /*========== Static Constant and Variable Definitions =======================*/
69 
70 /*========== Extern Constant and Variable Definitions =======================*/
71 /** boot state of the OS */
73 /** system timer variable */
74 volatile OS_TIMER_s os_timer = {0, 0, 0, 0, 0, 0, 0};
75 /** timestamp of the scheduler start */
76 uint32_t os_schedulerStartTime = 0;
77 
78 /** Buffer for the Idle Task's structure */
79 static StaticTask_t os_idleTaskTcbBuffer;
80 
81 /** @brief Stack for the Idle task */
83 
84 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
85 /** Buffer for the Timer Task's structure */
86 static StaticTask_t os_timerTaskTcbBuffer;
87 #endif /* configUSE_TIMERS */
88 
89 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
90 /** Stack for the Timer Task */
91 static StackType_t os_stackSizeTimer[OS_TIMER_TASK_STACK_SIZE];
92 #endif /* configUSE_TIMERS */
93 
94 /*========== Static Function Prototypes =====================================*/
95 
96 /*========== Static Function Implementations ================================*/
97 
98 /*========== Extern Function Implementations ================================*/
99 
100 void OS_StartScheduler(void) {
101  vTaskStartScheduler();
102 }
103 
105  /* operating system configuration (Queues, Tasks) */
111 }
112 
114  StaticTask_t **ppxIdleTaskTCBBuffer,
115  StackType_t **ppxIdleTaskStackBuffer,
116  uint32_t *pulIdleTaskStackSize) {
117  *ppxIdleTaskTCBBuffer = &os_idleTaskTcbBuffer;
118  *ppxIdleTaskStackBuffer = &os_stackSizeIdle[0];
119  *pulIdleTaskStackSize = OS_IDLE_TASK_STACK_SIZE;
120 }
121 
122 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
123 void vApplicationGetTimerTaskMemory(
124  StaticTask_t **ppxTimerTaskTCBBuffer,
125  StackType_t **ppxTimerTaskStackBuffer,
126  uint32_t *pulTimerTaskStackSize) {
127  *ppxTimerTaskTCBBuffer = &os_timerTaskTcbBuffer;
128  *ppxTimerTaskStackBuffer = &os_stackSizeTimer[0];
129  *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
130 }
131 #endif /* configUSE_TIMERS */
132 
135 }
136 
137 void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
139 }
140 
141 void OS_TriggerTimer(volatile OS_TIMER_s *timer) {
142  if (++timer->timer_1ms > 9) {
143  /* 10ms */
144  timer->timer_1ms = 0;
145 
146  if (++timer->timer_10ms > 9) {
147  /* 100ms */
148  timer->timer_10ms = 0;
149 
150  if (++timer->timer_100ms > 9) {
151  /* 1s */
152  timer->timer_100ms = 0;
153 
154  if (++timer->timer_sec > 59) {
155  /* 1min */
156  timer->timer_sec = 0;
157 
158  if (++timer->timer_min > 59) {
159  /* 1h */
160  timer->timer_min = 0;
161 
162  if (++timer->timer_h > 23) {
163  /* 1d */
164  timer->timer_h = 0;
165  ++timer->timer_d;
166  }
167  }
168  }
169  }
170  }
171  }
172 }
173 
175  taskENTER_CRITICAL();
176 }
177 
179  taskEXIT_CRITICAL();
180 }
181 
182 uint32_t OS_GetTickCount(void) {
183  return xTaskGetTickCount(); /*TMS570 does not support nested interrupts*/
184 }
185 
186 void OS_DelayTask(uint32_t delay_ms) {
187 #if INCLUDE_vTaskDelay
188  TickType_t ticks = delay_ms / portTICK_PERIOD_MS;
189 
190  vTaskDelay(ticks ? ticks : 1); /* Minimum delay = 1 tick */
191 #else
192 #error "Can't use OS_taskDelay."
193 #endif
194 }
195 
196 void OS_DelayTaskUntil(uint32_t *pPreviousWakeTime, uint32_t milliseconds) {
197 #if INCLUDE_vTaskDelayUntil
198  TickType_t ticks = (milliseconds / portTICK_PERIOD_MS);
199  vTaskDelayUntil((TickType_t *)pPreviousWakeTime, ticks ? ticks : 1);
200 
201 #else
202 #error "Can't use OS_taskDelayUntil."
203 #endif
204 }
205 
207 #if (INCLUDE_xTaskGetSchedulerState == 1)
208  /* Only increment operating systick timer if scheduler started */
209  if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
210  xTaskIncrementTick();
211  }
212 #else
213  xTaskIncrementTick();
214 #endif /* INCLUDE_xTaskGetSchedulerState */
215 }
216 
217 /*========== Externalized Static Function Implementations (Unit Test) =======*/
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:233
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:108
void FTSK_CreateTasks(void)
Creates all tasks of the group.
Definition: ftask.c:363
void FTSK_CreateQueues(void)
Creates all queues.
Definition: ftask.c:340
Header of task driver implementation.
void FTSK_RunUserCodeIdle(void)
Idle task.
Definition: ftask_cfg.c:234
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
Supplies the memory for the idle task.
Definition: os.c:113
void OS_StartScheduler(void)
Starts the operating system scheduler.
Definition: os.c:100
void OS_SystemTickHandler(void)
Handles the tick increment of operating systick timer.
Definition: os.c:206
void OS_DelayTaskUntil(uint32_t *pPreviousWakeTime, uint32_t milliseconds)
Delay a task until a specified time.
Definition: os.c:196
uint32_t os_schedulerStartTime
Scheduler "zero" time for task phase control.
Definition: os.c:76
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
Hook function for StackOverflowHandling.
Definition: os.c:137
static StackType_t os_stackSizeIdle[OS_IDLE_TASK_STACK_SIZE]
Stack for the Idle task.
Definition: os.c:82
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:178
void OS_InitializeOperatingSystem(void)
Initialization the RTOS interface.
Definition: os.c:104
void vApplicationIdleHook(void)
Hook function for the idle task.
Definition: os.c:133
volatile OS_TIMER_s os_timer
Definition: os.c:74
#define OS_IDLE_TASK_STACK_SIZE
Definition: os.c:62
void OS_TriggerTimer(volatile OS_TIMER_s *timer)
Increments the system timer os_timer.
Definition: os.c:141
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:174
void OS_DelayTask(uint32_t delay_ms)
Delays a task in milliseconds.
Definition: os.c:186
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os.c:182
volatile OS_BOOT_STATE_e os_boot
Definition: os.c:72
static StaticTask_t os_idleTaskTcbBuffer
Definition: os.c:79
Implementation of the tasks used by the system, headers.
enum OS_BOOT_STATE OS_BOOT_STATE_e
enum of OS boot states
@ OS_CREATE_TASKS
Definition: os.h:93
@ OS_CREATE_QUEUES
Definition: os.h:92
@ OS_INIT_PRE_OS
Definition: os.h:94
@ OS_OFF
Definition: os.h:91
OS timer.
Definition: os.h:105
uint8_t timer_min
Definition: os.h:110
uint16_t timer_d
Definition: os.h:112
uint8_t timer_h
Definition: os.h:111
uint8_t timer_100ms
Definition: os.h:108
uint8_t timer_sec
Definition: os.h:109
uint8_t timer_1ms
Definition: os.h:106
uint8_t timer_10ms
Definition: os.h:107