foxBMS  1.4.0
The foxBMS Battery Management System 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-07-28 (date of last update)
47  * @version v1.4.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) / GEN_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 /** @brief Definition of task handle for the AFE task */
94 TaskHandle_t ftsk_taskHandleAfe;
95 
96 volatile bool ftsk_allQueuesCreated = false;
97 
98 QueueHandle_t ftsk_databaseQueue = NULL_PTR;
99 
101 
102 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-handle-start-include */
103 QueueHandle_t ftsk_canRxQueue = NULL_PTR;
104 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-handle-stop-include */
105 
106 /*========== Static Function Prototypes =====================================*/
107 
108 /*========== Static Function Implementations ================================*/
109 
110 /*========== Extern Function Implementations ================================*/
111 
112 extern void FTSK_CreateQueues(void) {
113  /* structure and array for static database queue */
114  static uint8_t ftsk_databaseQueueStorageArea[FTSK_DATABASE_QUEUE_STORAGE_AREA] = {0};
115  static StaticQueue_t ftsk_databaseQueueStructure = {0};
116 
117  /* Create a queue capable of containing a pointer of type DATA_QUEUE_MESSAGE_s
118  Data of Messages are passed by pointer as they contain a lot of data. */
119  ftsk_databaseQueue = xQueueCreateStatic(
122  ftsk_databaseQueueStorageArea,
123  &ftsk_databaseQueueStructure);
125  vQueueAddToRegistry(ftsk_databaseQueue, "Database Queue");
126 
127  /* structure and array for static IMD queue */
128  static uint8_t ftsk_imdQueueStorageArea[FTSK_IMD_QUEUE_STORAGE_AREA] = {0};
129  static StaticQueue_t ftsk_imdQueueStructure = {0};
130 
131  ftsk_imdCanDataQueue = xQueueCreateStatic(
132  FTSK_IMD_QUEUE_LENGTH, FTSK_IMD_QUEUE_ITEM_SIZE_IN_BYTES, ftsk_imdQueueStorageArea, &ftsk_imdQueueStructure);
133  vQueueAddToRegistry(ftsk_imdCanDataQueue, "IMD CAN Data Queue");
135 
136  /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-vars-start-include */
137  /* structure and array for static CAN RX queue */
138  static uint8_t ftsk_canRxQueueStorageArea[FTSK_CAN_RX_QUEUE_STORAGE_AREA] = {0};
139  static StaticQueue_t ftsk_canRxQueueStructure = {0};
140  /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE can-documentation-rx-queue-vars-stop-include */
141 
142  ftsk_canRxQueue = xQueueCreateStatic(
145  ftsk_canRxQueueStorageArea,
146  &ftsk_canRxQueueStructure);
147  vQueueAddToRegistry(ftsk_canRxQueue, "CAN Receive Queue");
149 
151  ftsk_allQueuesCreated = true;
153 }
154 
155 extern void FTSK_CreateTasks(void) {
156  /* Engine Task */
157  static StaticTask_t ftsk_taskEngine = {0};
158  static StackType_t ftsk_stackEngine[FTSK_TASK_ENGINE_STACK_SIZE_IN_WORDS] = {0};
159 
160  const TaskHandle_t ftsk_taskHandleEngine = xTaskCreateStatic(
161  (TaskFunction_t)FTSK_CreateTaskEngine,
162  (const portCHAR *)"TaskEngine",
165  (UBaseType_t)ftsk_taskDefinitionEngine.priority,
166  ftsk_stackEngine,
167  &ftsk_taskEngine);
168  FAS_ASSERT(ftsk_taskHandleEngine != NULL); /* Trap if initialization failed */
169 
170  /* Cyclic Task 1ms */
171  static StaticTask_t ftsk_taskCyclic1ms = {0};
172  static StackType_t ftsk_stackCyclic1ms[FTSK_TASK_CYCLIC_1MS_STACK_SIZE_IN_WORDS] = {0};
173 
174  const TaskHandle_t ftsk_taskHandleCyclic1ms = xTaskCreateStatic(
175  (TaskFunction_t)FTSK_CreateTaskCyclic1ms,
176  (const portCHAR *)"TaskCyclic1ms",
180  ftsk_stackCyclic1ms,
181  &ftsk_taskCyclic1ms);
182  FAS_ASSERT(ftsk_taskHandleCyclic1ms != NULL); /* Trap if initialization failed */
183 
184  /* Cyclic Task 10ms */
185  static StaticTask_t ftsk_taskCyclic10ms = {0};
186  static StackType_t ftsk_stackCyclic10ms[FTSK_TASK_CYCLIC_10MS_STACK_SIZE_IN_WORDS] = {0};
187 
188  const TaskHandle_t ftsk_taskHandleCyclic10ms = xTaskCreateStatic(
189  (TaskFunction_t)FTSK_CreateTaskCyclic10ms,
190  (const portCHAR *)"TaskCyclic10ms",
194  ftsk_stackCyclic10ms,
195  &ftsk_taskCyclic10ms);
196  FAS_ASSERT(ftsk_taskHandleCyclic10ms != NULL); /* Trap if initialization failed */
197 
198  /* Cyclic Task 100ms */
199  static StaticTask_t ftsk_taskCyclic100ms = {0};
200  static StackType_t ftsk_stackCyclic100ms[FTSK_TASK_CYCLIC_100MS_STACK_SIZE_IN_WORDS] = {0};
201 
202  const TaskHandle_t ftsk_taskHandleCyclic100ms = xTaskCreateStatic(
203  (TaskFunction_t)FTSK_CreateTaskCyclic100ms,
204  (const portCHAR *)"TaskCyclic100ms",
208  ftsk_stackCyclic100ms,
209  &ftsk_taskCyclic100ms);
210  FAS_ASSERT(ftsk_taskHandleCyclic100ms != NULL); /* Trap if initialization failed */
211 
212  /* Cyclic Task 100ms for algorithms */
213  static StaticTask_t ftsk_taskCyclicAlgorithm100ms = {0};
214  static StackType_t ftsk_stackCyclicAlgorithm100ms[FTSK_TASK_CYCLIC_ALGORITHM_100MS_STACK_SIZE_IN_WORDS] = {0};
215 
216  const TaskHandle_t ftsk_taskHandleCyclicAlgorithm100ms = xTaskCreateStatic(
217  (TaskFunction_t)FTSK_CreateTaskCyclicAlgorithm100ms,
218  (const portCHAR *)"TaskCyclicAlgorithm100ms",
222  ftsk_stackCyclicAlgorithm100ms,
223  &ftsk_taskCyclicAlgorithm100ms);
224  FAS_ASSERT(ftsk_taskHandleCyclicAlgorithm100ms != NULL); /* Trap if initialization failed */
225 
226  /* Continuously running Task for AFE */
227  static StaticTask_t ftsk_taskAfe = {0};
228  static StackType_t ftsk_stackSizeAfe[FTSK_TASK_AFE_STACK_SIZE_IN_WORDS] = {0};
229 
230  ftsk_taskHandleAfe = xTaskCreateStatic(
231  (TaskFunction_t)FTSK_CreateTaskAfe,
232  (const portCHAR *)"TaskAfe",
235  (UBaseType_t)ftsk_taskDefinitionAfe.priority,
236  ftsk_stackSizeAfe,
237  &ftsk_taskAfe);
238  FAS_ASSERT(ftsk_taskHandleAfe != NULL); /* Trap if initialization failed */
239 }
240 
241 /*========== 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:252
#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:186
void FTSK_CreateTaskCyclic100ms(void *const pvParameters)
Creation of cyclic 100 ms task.
Definition: ftask.c:157
void FTSK_CreateTaskCyclic10ms(void *const pvParameters)
Creation of cyclic 10 ms task.
Definition: ftask.c:128
void FTSK_CreateTaskAfe(void *const pvParameters)
Creation of continuously running task for AFEs.
Definition: ftask.c:217
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 handle for the AFE task.
#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