foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
ftask_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 ftask_freertos.c
44  * @author foxBMS Team
45  * @date 2019-08-27 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.0
48  * @ingroup TASK
49  * @prefix FTSK
50  *
51  * @brief OS specific, i.e., FreeRTOS specfific, creation of the tasks
52  * @details TODO
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "can_cfg.h"
57 
58 #include "FreeRTOS.h"
59 #include "task.h"
60 
61 #include "database.h"
62 #include "ftask.h"
63 
64 /*========== Macros and Definitions =========================================*/
65 /** helper macro to translate the stack sizes from bytes into words as FreeRTOS requires words and not bytes */
66 #define FTSK_BYTES_TO_WORDS(VARIABALE_IN_BYTES) ((VARIABALE_IN_BYTES) / BYTES_PER_WORD)
67 /** Stack size of engine task in words */
68 #define FTSK_TASK_ENGINE_STACK_SIZE_IN_WORDS FTSK_BYTES_TO_WORDS(FTSK_TASK_ENGINE_STACK_SIZE_IN_BYTES)
69 /** @brief Stack size of cyclic 1 ms task in words */
70 #define FTSK_TASK_CYCLIC_1MS_STACK_SIZE_IN_WORDS FTSK_BYTES_TO_WORDS(FTSK_TASK_CYCLIC_1MS_STACK_SIZE_IN_BYTES)
71 /** @brief Stack size of cyclic 10 ms task in words */
72 #define FTSK_TASK_CYCLIC_10MS_STACK_SIZE_IN_WORDS FTSK_BYTES_TO_WORDS(FTSK_TASK_CYCLIC_10MS_STACK_SIZE_IN_BYTES)
73 /** @brief Stack size of cyclic 100 ms task in words */
74 #define FTSK_TASK_CYCLIC_100MS_STACK_SIZE_IN_WORDS FTSK_BYTES_TO_WORDS(FTSK_TASK_CYCLIC_100MS_STACK_SIZE_IN_BYTES)
75 /** @brief Stack size of cyclic 100 ms task for algorithms in words */
76 #define FTSK_TASK_CYCLIC_ALGORITHM_100MS_STACK_SIZE_IN_WORDS \
77  FTSK_BYTES_TO_WORDS(FTSK_TASK_CYCLIC_ALGORITHM_100MS_STACK_SIZE_IN_BYTES)
78 /** @brief Stack size of continuously running task for AFEs */
79 #define FTSK_TASK_AFE_STACK_SIZE_IN_WORDS FTSK_BYTES_TO_WORDS(FTSK_TASK_AFE_STACK_SIZE_IN_BYTES)
80 
81 /** size of storage area for the database queue */
82 #define FTSK_DATABASE_QUEUE_STORAGE_AREA (FTSK_DATABASE_QUEUE_LENGTH * FTSK_DATABASE_QUEUE_ITEM_SIZE_IN_BYTES)
83 
84 /** size of storage area for the IMD queue*/
85 #define FTSK_IMD_QUEUE_STORAGE_AREA (FTSK_IMD_QUEUE_LENGTH * FTSK_IMD_QUEUE_ITEM_SIZE_IN_BYTES)
86 
87 /** size of storage area for the CAN Rx queue*/
88 #define FTSK_CAN_RX_QUEUE_STORAGE_AREA (FTSK_CAN_RX_QUEUE_LENGTH * FTSK_CAN_RX_QUEUE_ITEM_SIZE_IN_BYTES)
89 
90 /*========== Static Constant and Variable Definitions =======================*/
91 
92 /*========== Extern Constant and Variable Definitions =======================*/
93 TaskHandle_t ftsk_taskHandleAfe;
94 
95 volatile bool ftsk_allQueuesCreated = false;
96 
97 QueueHandle_t ftsk_databaseQueue = NULL_PTR;
98 
100 
101 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-handle-start-include */
102 QueueHandle_t ftsk_canRxQueue = NULL_PTR;
103 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-handle-stop-include */
104 
105 /*========== Static Function Prototypes =====================================*/
106 
107 /*========== Static Function Implementations ================================*/
108 
109 /*========== Extern Function Implementations ================================*/
110 
111 extern void FTSK_CreateQueues(void) {
112  /* structure and array for static database queue */
113  static uint8_t ftsk_databaseQueueStorageArea[FTSK_DATABASE_QUEUE_STORAGE_AREA] = {0};
114  static StaticQueue_t ftsk_databaseQueueStructure = {0};
115 
116  /* Create a queue capable of containing a pointer of type DATA_QUEUE_MESSAGE_s
117  Data of Messages are passed by pointer as they contain a lot of data. */
118  ftsk_databaseQueue = xQueueCreateStatic(
121  ftsk_databaseQueueStorageArea,
122  &ftsk_databaseQueueStructure);
124  vQueueAddToRegistry(ftsk_databaseQueue, "Database Queue");
125 
126  /* structure and array for static IMD queue */
128  static StaticQueue_t ftsk_imdQueueStructure = {0};
129 
130  ftsk_imdCanDataQueue = xQueueCreateStatic(
132  vQueueAddToRegistry(ftsk_imdCanDataQueue, "IMD CAN Data Queue");
134 
135  /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-vars-start-include */
136  /* structure and array for static CAN RX queue */
138  static StaticQueue_t ftsk_canRxQueueStructure = {0};
139  /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-vars-stop-include */
140 
141  ftsk_canRxQueue = xQueueCreateStatic(
146  vQueueAddToRegistry(ftsk_canRxQueue, "CAN Receive Queue");
148 
150  ftsk_allQueuesCreated = true;
152 }
153 
154 extern void FTSK_CreateTasks(void) {
155  /* Engine Task */
156  static StaticTask_t ftsk_taskEngine = {0};
157  static StackType_t ftsk_stackEngine[FTSK_TASK_ENGINE_STACK_SIZE_IN_WORDS] = {0};
158 
159  const TaskHandle_t ftsk_taskHandleEngine = xTaskCreateStatic(
160  (TaskFunction_t)FTSK_CreateTaskEngine,
161  (const portCHAR *)"TaskEngine",
164  (UBaseType_t)ftsk_taskDefinitionEngine.priority,
165  ftsk_stackEngine,
166  &ftsk_taskEngine);
167  FAS_ASSERT(ftsk_taskHandleEngine != NULL); /* Trap if initialization failed */
168 
169  /* Cyclic Task 1ms */
170  static StaticTask_t ftsk_taskCyclic1ms = {0};
171  static StackType_t ftsk_stackCyclic1ms[FTSK_TASK_CYCLIC_1MS_STACK_SIZE_IN_WORDS] = {0};
172 
173  const TaskHandle_t ftsk_taskHandleCyclic1ms = xTaskCreateStatic(
174  (TaskFunction_t)FTSK_CreateTaskCyclic1ms,
175  (const portCHAR *)"TaskCyclic1ms",
179  ftsk_stackCyclic1ms,
180  &ftsk_taskCyclic1ms);
181  FAS_ASSERT(ftsk_taskHandleCyclic1ms != NULL); /* Trap if initialization failed */
182 
183  /* Cyclic Task 10ms */
184  static StaticTask_t ftsk_taskCyclic10ms = {0};
185  static StackType_t ftsk_stackCyclic10ms[FTSK_TASK_CYCLIC_10MS_STACK_SIZE_IN_WORDS] = {0};
186 
187  const TaskHandle_t ftsk_taskHandleCyclic10ms = xTaskCreateStatic(
188  (TaskFunction_t)FTSK_CreateTaskCyclic10ms,
189  (const portCHAR *)"TaskCyclic10ms",
193  ftsk_stackCyclic10ms,
194  &ftsk_taskCyclic10ms);
195  FAS_ASSERT(ftsk_taskHandleCyclic10ms != NULL); /* Trap if initialization failed */
196 
197  /* Cyclic Task 100ms */
198  static StaticTask_t ftsk_taskCyclic100ms = {0};
199  static StackType_t ftsk_stackCyclic100ms[FTSK_TASK_CYCLIC_100MS_STACK_SIZE_IN_WORDS] = {0};
200 
201  const TaskHandle_t ftsk_taskHandleCyclic100ms = xTaskCreateStatic(
202  (TaskFunction_t)FTSK_CreateTaskCyclic100ms,
203  (const portCHAR *)"TaskCyclic100ms",
207  ftsk_stackCyclic100ms,
208  &ftsk_taskCyclic100ms);
209  FAS_ASSERT(ftsk_taskHandleCyclic100ms != NULL); /* Trap if initialization failed */
210 
211  /* Cyclic Task 100ms for algorithms */
212  static StaticTask_t ftsk_taskCyclicAlgorithm100ms = {0};
213  static StackType_t ftsk_stackCyclicAlgorithm100ms[FTSK_TASK_CYCLIC_ALGORITHM_100MS_STACK_SIZE_IN_WORDS] = {0};
214 
215  const TaskHandle_t ftsk_taskHandleCyclicAlgorithm100ms = xTaskCreateStatic(
216  (TaskFunction_t)FTSK_CreateTaskCyclicAlgorithm100ms,
217  (const portCHAR *)"TaskCyclicAlgorithm100ms",
221  ftsk_stackCyclicAlgorithm100ms,
222  &ftsk_taskCyclicAlgorithm100ms);
223  FAS_ASSERT(ftsk_taskHandleCyclicAlgorithm100ms != NULL); /* Trap if initialization failed */
224 
225  /* Continuously running Task for AFE */
226  static StaticTask_t ftsk_taskAfe = {0};
227  static StackType_t ftsk_stackSizeAfe[FTSK_TASK_AFE_STACK_SIZE_IN_WORDS] = {0};
228 
229  ftsk_taskHandleAfe = xTaskCreateStatic(
230  (TaskFunction_t)FTSK_CreateTaskAfe,
231  (const portCHAR *)"TaskAfe",
234  (UBaseType_t)ftsk_taskDefinitionAfe.priority,
235  ftsk_stackSizeAfe,
236  &ftsk_taskAfe);
237  FAS_ASSERT(ftsk_taskHandleAfe != NULL); /* Trap if initialization failed */
238 }
239 
240 /*========== Externalized Static Function Implementations (Unit Test) =======*/
Headers for the configuration for the CAN module.
Database module header.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
#define NULL
NULL definition.
Definition: fstd_types.h:66
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
void FTSK_CreateTaskEngine(void *const pvParameters)
Database-Task.
Definition: ftask.c:74
void FTSK_CreateTaskCyclicAlgorithm100ms(void *const pvParameters)
Creation of cyclic 100 ms algorithm task.
Definition: ftask.c:181
void FTSK_CreateTaskCyclic100ms(void *const pvParameters)
Creation of cyclic 100 ms task.
Definition: ftask.c:154
void FTSK_CreateTaskCyclic10ms(void *const pvParameters)
Creation of cyclic 10 ms task.
Definition: ftask.c:127
void FTSK_CreateTaskAfe(void *const pvParameters)
Creation of continuously running task for AFEs.
Definition: ftask.c:210
void FTSK_CreateTaskCyclic1ms(void *const pvParameters)
Creation of cyclic 1 ms task.
Definition: ftask.c:96
Header of task driver implementation.
#define FTSK_DATABASE_QUEUE_LENGTH
Definition: ftask.h:66
#define FTSK_DATABASE_QUEUE_ITEM_SIZE_IN_BYTES
Definition: ftask.h:69
#define FTSK_CAN_RX_QUEUE_ITEM_SIZE_IN_BYTES
Definition: ftask.h:79
#define FTSK_CAN_RX_QUEUE_LENGTH
Definition: ftask.h:77
#define FTSK_IMD_QUEUE_ITEM_SIZE_IN_BYTES
Definition: ftask.h:74
#define FTSK_IMD_QUEUE_LENGTH
Definition: ftask.h:72
OS_TASK_DEFINITION_s ftsk_taskDefinitionAfe
Task configuration of the continuously running task for AFEs.
Definition: ftask_cfg.c:136
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclic100ms
Task configuration of the cyclic 100 ms task.
Definition: ftask_cfg.c:124
OS_TASK_DEFINITION_s ftsk_taskDefinitionEngine
Definition of the engine task.
Definition: ftask_cfg.c:106
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclic1ms
Task configuration of the cyclic 1 ms task.
Definition: ftask_cfg.c:112
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclicAlgorithm100ms
Task configuration of the cyclic 100 ms task for algorithms.
Definition: ftask_cfg.c:130
OS_TASK_DEFINITION_s ftsk_taskDefinitionCyclic10ms
Task configuration of the cyclic 10 ms task.
Definition: ftask_cfg.c:118
#define FTSK_TASK_CYCLIC_1MS_STACK_SIZE_IN_WORDS
Stack size of cyclic 1 ms task in words.
#define FTSK_IMD_QUEUE_STORAGE_AREA
#define FTSK_CAN_RX_QUEUE_STORAGE_AREA
volatile bool ftsk_allQueuesCreated
QueueHandle_t ftsk_databaseQueue
#define FTSK_TASK_ENGINE_STACK_SIZE_IN_WORDS
#define FTSK_TASK_AFE_STACK_SIZE_IN_WORDS
Stack size of continuously running task for AFEs.
#define FTSK_TASK_CYCLIC_ALGORITHM_100MS_STACK_SIZE_IN_WORDS
Stack size of cyclic 100 ms task for algorithms in words.
TaskHandle_t ftsk_taskHandleAfe
Definition of task handles.
#define FTSK_BYTES_TO_WORDS(VARIABALE_IN_BYTES)
#define FTSK_TASK_CYCLIC_10MS_STACK_SIZE_IN_WORDS
Stack size of cyclic 10 ms task in words.
void FTSK_CreateTasks(void)
Creates all tasks of the group.
#define FTSK_DATABASE_QUEUE_STORAGE_AREA
#define FTSK_TASK_CYCLIC_100MS_STACK_SIZE_IN_WORDS
Stack size of cyclic 100 ms task in words.
QueueHandle_t ftsk_canRxQueue
void FTSK_CreateQueues(void)
Creates all queues.
QueueHandle_t ftsk_imdCanDataQueue
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:135
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:131
uint32_t stackSize_B
Definition: os.h:131
void * pvParameters
Definition: os.h:132
OS_PRIORITY_e priority
Definition: os.h:128
static uint8_t ftsk_imdQueueStorageArea[FTSK_IMD_QUEUE_LENGTH *FTSK_IMD_QUEUE_ITEM_SIZE]
static StaticQueue_t ftsk_canRxQueueStructure
static StaticQueue_t ftsk_imdQueueStructure
static uint8_t ftsk_canRxQueueStorageArea[FTSK_CAN_RX_QUEUE_LENGTH *FTSK_CAN_RX_QUEUE_ITEM_SIZE]