foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
os_freertos.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_freertos.c
44  * @author foxBMS Team
45  * @date 2021-11-18 (date of creation)
46  * @updated 2021-12-01 (date of last update)
47  * @ingroup OS
48  * @prefix OS
49  *
50  * @brief FreeRTOS specific implementation of the tasks and resources used by
51  * the system
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 /*========== Extern Constant and Variable Definitions =======================*/
64 
65 /*========== Static Function Prototypes =====================================*/
66 
67 /*========== Static Function Implementations ================================*/
68 
69 /*========== Extern Function Implementations ================================*/
70 
71 void OS_StartScheduler(void) {
72  vTaskStartScheduler();
73 }
74 
76  StaticTask_t **ppxIdleTaskTCBBuffer,
77  StackType_t **ppxIdleTaskStackBuffer,
78  uint32_t *pulIdleTaskStackSize) {
79  /** Buffer for the Idle Task's structure */
80  static StaticTask_t os_idleTask = {0};
81  /** @brief Stack for the Idle task */
82  static StackType_t os_stackSizeIdle[OS_IDLE_TASK_STACK_SIZE] = {0};
83  FAS_ASSERT(ppxIdleTaskTCBBuffer != NULL_PTR);
84  FAS_ASSERT(ppxIdleTaskStackBuffer != NULL_PTR);
85  FAS_ASSERT(pulIdleTaskStackSize != NULL_PTR);
86  *ppxIdleTaskTCBBuffer = &os_idleTask;
87  *ppxIdleTaskStackBuffer = &os_stackSizeIdle[0];
88  *pulIdleTaskStackSize = OS_IDLE_TASK_STACK_SIZE;
89 }
90 
91 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
92 void vApplicationGetTimerTaskMemory(
93  StaticTask_t **ppxTimerTaskTCBBuffer,
94  StackType_t **ppxTimerTaskStackBuffer,
95  uint32_t *pulTimerTaskStackSize) {
96 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
97  /** Buffer for the Timer Task's structure */
98  static StaticTask_t os_timerTask;
99 #endif /* configUSE_TIMERS */
100 
101 #if (configUSE_TIMERS > 0) && (configSUPPORT_STATIC_ALLOCATION == 1)
102  /** Stack for the Timer Task */
103  static StackType_t os_stackSizeTimer[OS_TIMER_TASK_STACK_SIZE];
104 #endif /* configUSE_TIMERS */
105  *ppxTimerTaskTCBBuffer = &os_timerTask;
106  *ppxTimerTaskStackBuffer = &os_stackSizeTimer[0];
107  *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
108 }
109 #endif /* configUSE_TIMERS */
110 
113 }
114 
115 #if (configCHECK_FOR_STACK_OVERFLOW > 0)
116 void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
118 }
119 #endif /* configCHECK_FOR_STACK_OVERFLOW */
120 
122  taskENTER_CRITICAL();
123 }
124 
126  taskEXIT_CRITICAL();
127 }
128 
129 uint32_t OS_GetTickCount(void) {
130  return xTaskGetTickCount(); /*TMS570 does not support nested interrupts*/
131 }
132 
133 void OS_DelayTaskUntil(uint32_t *pPreviousWakeTime, uint32_t milliseconds) {
134 #if INCLUDE_vTaskDelayUntil
135  FAS_ASSERT(pPreviousWakeTime != NULL_PTR);
136  FAS_ASSERT(milliseconds > 0u);
137  TickType_t ticks = ((TickType_t)milliseconds / portTICK_PERIOD_MS);
138  if ((uint32_t)ticks < 1u) {
139  ticks = 1u; /* Minimum delay is 1 tick */
140  }
141  vTaskDelayUntil((TickType_t *)pPreviousWakeTime, ticks);
142 
143 #else
144 #error "Can't use OS_taskDelayUntil."
145 #endif
146 }
147 
149 #if (INCLUDE_xTaskGetSchedulerState == 1)
150  /* Only increment operating systick timer if scheduler started */
151  if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
152  xTaskIncrementTick();
153  }
154 #else
155  xTaskIncrementTick();
156 #endif /* INCLUDE_xTaskGetSchedulerState */
157 }
158 
160  vPortTaskUsesFPU();
161 }
162 
163 extern OS_STD_RETURN_e OS_ReceiveFromQueue(OS_QUEUE xQueue, void *const pvBuffer, uint32_t ticksToWait) {
164  FAS_ASSERT(pvBuffer != NULL_PTR);
165 
166  OS_STD_RETURN_e queueReceiveSucessfull = OS_FAIL;
167  /* FreeRTOS: This function must not be used in an interrupt service routine. */
168  BaseType_t xQueueReceiveSuccess = xQueueReceive(xQueue, pvBuffer, (TickType_t)ticksToWait);
169  /* FreeRTOS:xQueueReceive returns pdTRUE if an item was successfully received from the queue (otherwise pdFALSE). */
170  if (xQueueReceiveSuccess == pdTRUE) {
171  queueReceiveSucessfull = OS_SUCCESS;
172  }
173  return queueReceiveSucessfull;
174 }
175 
176 extern OS_STD_RETURN_e OS_SendToBackOfQueue(OS_QUEUE xQueue, const void *const pvItemToQueue, TickType_t ticksToWait) {
177  FAS_ASSERT(pvItemToQueue != NULL_PTR);
178 
179  OS_STD_RETURN_e queueSendSucessfull = OS_FAIL;
180  BaseType_t xQueueSendSuccess = xQueueSendToBack(xQueue, pvItemToQueue, ticksToWait);
181  /* FreeRTOS:xQueueSendToBack returns pdTRUE if the item was successfully posted (otherwise errQUEUE_FULL). */
182  if (xQueueSendSuccess == pdTRUE) {
183  queueSendSucessfull = OS_SUCCESS;
184  }
185  return queueSendSucessfull;
186 }
187 
189  OS_QUEUE xQueue,
190  const void *const pvItemToQueue,
191  long *const pxHigherPriorityTaskWoken) {
192  FAS_ASSERT(pvItemToQueue != NULL_PTR);
193 
194  OS_STD_RETURN_e queueSendSucessfull = OS_FAIL;
195  BaseType_t xQueueSendSuccess =
196  xQueueSendToBackFromISR(xQueue, pvItemToQueue, (BaseType_t *)pxHigherPriorityTaskWoken);
197  /* FreeRTOS:xQueueSendToBackFromISR returns pdTRUE if the item was successfully posted (otherwise errQUEUE_FULL). */
198  if (xQueueSendSuccess == pdTRUE) {
199  queueSendSucessfull = OS_SUCCESS;
200  }
201  return queueSendSucessfull;
202 }
203 
204 /*========== Externalized Static Function Implementations (Unit Test) =======*/
205 #ifdef UNITY_UNIT_TEST
206 
207 #endif
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:239
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:110
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
Header of task driver implementation.
void FTSK_RunUserCodeIdle(void)
Idle task.
Definition: ftask_cfg.c:267
Declaration of the OS wrapper interface.
enum OS_STD_RETURN OS_STD_RETURN_e
#define OS_IDLE_TASK_STACK_SIZE
Definition: os.h:69
@ OS_SUCCESS
Definition: os.h:77
@ OS_FAIL
Definition: os.h:78
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
Definition: os_freertos.c:75
OS_STD_RETURN_e OS_ReceiveFromQueue(OS_QUEUE xQueue, void *const pvBuffer, uint32_t ticksToWait)
Receive an item from a queue.
Definition: os_freertos.c:163
void OS_StartScheduler(void)
Starts the operating system scheduler.
Definition: os_freertos.c:71
void OS_SystemTickHandler(void)
Handles the tick increment of operating systick timer.
Definition: os_freertos.c:148
void OS_DelayTaskUntil(uint32_t *pPreviousWakeTime, uint32_t milliseconds)
Delay a task until a specified time.
Definition: os_freertos.c:133
OS_STD_RETURN_e OS_SendToBackOfQueueFromIsr(OS_QUEUE xQueue, const void *const pvItemToQueue, long *const pxHigherPriorityTaskWoken)
Post an item to the back the provided queue during an ISR.
Definition: os_freertos.c:188
void OS_MarkTaskAsRequiringFpuContext(void)
Marks the current task as requiring FPU context.
Definition: os_freertos.c:159
OS_STD_RETURN_e OS_SendToBackOfQueue(OS_QUEUE xQueue, const void *const pvItemToQueue, TickType_t ticksToWait)
Post an item to the back the provided queue.
Definition: os_freertos.c:176
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:125
void vApplicationIdleHook(void)
Hook function for the idle task.
Definition: os_freertos.c:111
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:121
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:129