foxBMS  1.0.0
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
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
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 2020-01-21 (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_SIZE configMINIMAL_STACK_SIZE
63 
64 /*========== Static Constant and Variable Definitions =======================*/
65 
66 /*========== Extern Constant and Variable Definitions =======================*/
67 /** boot state of the OS */
69 /** system timer variable */
70 volatile OS_TIMER_s os_timer = {0, 0, 0, 0, 0, 0, 0};
71 /** timestamp of the scheduler start */
72 uint32_t os_schedulerStartTime = 0;
73 
74 /** Buffer for the Idle Task's structure */
75 static StaticTask_t os_idleTaskTcbBuffer;
76 
77 /** @brief Stack for the Idle task */
78 static StackType_t os_idleStack[OS_IDLE_TASK_SIZE];
79 
80 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
81 /** Buffer for the Timer Task's structure */
82 static StaticTask_t os_timerTaskTcbBuffer;
83 #endif /* configUSE_TIMERS */
84 
85 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
86 /** Stack for the Timer Task */
87 static StackType_t os_timerStack[configTIMER_TASK_STACK_DEPTH];
88 #endif /* configUSE_TIMERS */
89 
90 /*========== Static Function Prototypes =====================================*/
91 
92 /*========== Static Function Implementations ================================*/
93 
94 /*========== Extern Function Implementations ================================*/
95 
96 void OS_StartScheduler(void) {
97  vTaskStartScheduler();
98 }
99 
100 void OS_InitializeTasks(void) {
101  /* operating system configuration (Queues, Mutexes, Events, Tasks */
111 }
112 
114  StaticTask_t **ppxIdleTaskTCBBuffer,
115  StackType_t **ppxIdleTaskStackBuffer,
116  uint32_t *pulIdleTaskStackSize) {
117  *ppxIdleTaskTCBBuffer = &os_idleTaskTcbBuffer;
118  *ppxIdleTaskStackBuffer = &os_idleStack[0];
119  *pulIdleTaskStackSize = OS_IDLE_TASK_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_timerStack[0];
129  *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
130 }
131 #endif /* configUSE_TIMERS */
132 
135 }
136 
137 void vApplicationStackOverflowHook(TaskHandle_t xTask, signed 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) =======*/
os.h
Implementation of the tasks used by the system, headers.
OS_StartScheduler
void OS_StartScheduler(void)
Starts the operating system scheduler.
Definition: os.c:96
os_timer
volatile OS_TIMER_s os_timer
Definition: os.c:70
OS_IDLE_TASK_SIZE
#define OS_IDLE_TASK_SIZE
Definition: os.c:62
OS_DelayTaskUntil
void OS_DelayTaskUntil(uint32_t *pPreviousWakeTime, uint32_t milliseconds)
Delay a task until a specified time.
Definition: os.c:196
OS_OFF
@ OS_OFF
Definition: os.h:91
vApplicationGetIdleTaskMemory
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
Supplies the memory for the idle task.
Definition: os.c:113
OS_InitializeTasks
void OS_InitializeTasks(void)
Initialization the RTOS interface.
Definition: os.c:100
os_schedulerStartTime
uint32_t os_schedulerStartTime
Scheduler "zero" time for task phase control.
Definition: os.c:72
OS_CREATE_EVENT
@ OS_CREATE_EVENT
Definition: os.h:94
OS_TIMER::timer_sec
uint8_t timer_sec
Definition: os.h:111
vApplicationIdleHook
void vApplicationIdleHook(void)
Hook function for the idle task.
Definition: os.c:133
FTSK_CreateEvents
void FTSK_CreateEvents(void)
Creates all events of the group.
Definition: ftask.c:135
OS_TriggerTimer
void OS_TriggerTimer(volatile OS_TIMER_s *timer)
Increments the system timer os_timer.
Definition: os.c:141
OS_DelayTask
void OS_DelayTask(uint32_t delay_ms)
Delays a task in milliseconds.
Definition: os.c:186
OS_TIMER::timer_1ms
uint8_t timer_1ms
Definition: os.h:108
FAS_ASSERT
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
OS_ExitTaskCritical
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:178
OS_TIMER::timer_10ms
uint8_t timer_10ms
Definition: os.h:109
OS_TIMER::timer_h
uint8_t timer_h
Definition: os.h:113
OS_INIT_PRE_OS
@ OS_INIT_PRE_OS
Definition: os.h:96
OS_GetTickCount
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os.c:182
ftask.h
Header of task driver implementation.
FTSK_CreateMutexes
void FTSK_CreateMutexes(void)
Creates all mutexes.
Definition: ftask.c:132
os_idleStack
static StackType_t os_idleStack[OS_IDLE_TASK_SIZE]
Stack for the Idle task.
Definition: os.c:78
os_idleTaskTcbBuffer
static StaticTask_t os_idleTaskTcbBuffer
Definition: os.c:75
OS_EnterTaskCritical
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os.c:174
OS_TIMER::timer_d
uint16_t timer_d
Definition: os.h:114
OS_CREATE_MUTEX
@ OS_CREATE_MUTEX
Definition: os.h:93
OS_SystemTickHandler
void OS_SystemTickHandler(void)
Handles the tick increment of operating systick timer.
Definition: os.c:206
OS_TIMER::timer_min
uint8_t timer_min
Definition: os.h:112
OS_TIMER::timer_100ms
uint8_t timer_100ms
Definition: os.h:110
OS_TIMER
OS timer.
Definition: os.h:107
OS_BOOT_STATE_e
enum OS_BOOT_STATE OS_BOOT_STATE_e
enum of OS boot states
os_boot
volatile OS_BOOT_STATE_e os_boot
Definition: os.c:68
OS_CREATE_QUEUES
@ OS_CREATE_QUEUES
Definition: os.h:92
FTSK_CreateTasks
void FTSK_CreateTasks(void)
Creates all tasks of the group.
Definition: ftask.c:138
vApplicationStackOverflowHook
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName)
Hook function for StackOverflowHandling.
Definition: os.c:137
FTSK_CreateQueues
void FTSK_CreateQueues(void)
Creates all queues.
Definition: ftask.c:129
OS_CREATE_TASKS
@ OS_CREATE_TASKS
Definition: os.h:95
FAS_TRAP
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:108
FTSK_UserCodeIdle
void FTSK_UserCodeIdle(void)
Idle task.
Definition: ftask_cfg.c:220