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