Skip to content

Commit 10637da

Browse files
committed
addng bms runtime stats for testing
1 parent 62a458d commit 10637da

File tree

11 files changed

+72
-26
lines changed

11 files changed

+72
-26
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"hw_runtimestats.h": "c",
1919
"stm32f4xx_hal_exti.h": "c",
2020
"cmsis_os.h": "c",
21-
"app_utils.h": "c"
21+
"app_utils.h": "c",
22+
"app_cantx.h": "c",
23+
"stm32f4xx_hal_tim.h": "c"
2224
}
2325
}

firmware/quintuna/BMS/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ list(APPEND HW_SRCS
5050
"${SHARED_HW_INCLUDE_DIR}/hw_hardFaultHandler.c"
5151
"${SHARED_HW_INCLUDE_DIR}/hw_freeRtosConfigs.c"
5252
"${SHARED_HW_INCLUDE_DIR}/hw_bootup.c"
53+
"${SHARED_HW_INCLUDE_DIR}/hw_runTimeStat.c"
5354
)
5455
set(HW_INCLUDE_DIRS
5556
"${CMAKE_CURRENT_SOURCE_DIR}/src/hw"

firmware/quintuna/BMS/src/cubemx/BMS.ioc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ FDCAN2.StdFiltersNbr=1
138138
FDCAN2.TxElmtSize=FDCAN_DATA_BYTES_64
139139
FDCAN2.TxFifoQueueElmtsNbr=32
140140
FREERTOS.FootprintOK=true
141-
FREERTOS.IPParameters=Tasks01,configENABLE_FPU,configUSE_NEWLIB_REENTRANT,FootprintOK
141+
FREERTOS.IPParameters=Tasks01,configENABLE_FPU,configUSE_NEWLIB_REENTRANT,FootprintOK,configGENERATE_RUN_TIME_STATS
142142
FREERTOS.Tasks01=Task100Hz,40,512,RunTask100Hz,Default,NULL,Static,Task100HzBuffer,Task100HzControlBlock;TaskCanRx,16,512,RunTaskCanRx,Default,NULL,Static,TaskCanRxBuffer,TaskCanRxControlBlock;TaskCanTx,16,512,RunTaskCanTx,Default,NULL,Static,TaskCanTxBuffer,TaskCanTxControlBlock;Task1kHz,48,512,RunTask1kHz,Default,NULL,Static,Task1kHzBuffer,Task1kHzControlBlock;Task1Hz,32,512,RunTask1Hz,Default,NULL,Static,Task1HzBuffer,Task1HzControlBlock;TaskChimera,40,512,RunTaskChimera,Default,NULL,Static,TaskChimeraBuffer,TaskChimeraControlBlock;TaskLtcVoltages,24,512,RunTaskLtcVoltages,Default,NULL,Static,TaskLtcVoltagesBuffer,TaskLtcVoltagesControlBlock;TaskLtcTemps,24,512,RunTaskLtcTemps,Default,NULL,Static,TaskLtcTempsBuffer,TaskLtcTempsControlBlock;TaskLtcDiag,24,512,RunTaskLtcDiag,Default,NULL,Static,TaskLtcDiagBuffer,TaskLtcDiagControlBlock;TaskInit,48,512,RunTaskInit,Default,NULL,Static,TaskInitBuffer,TaskInitControlBlock
143143
FREERTOS.configENABLE_FPU=1
144+
FREERTOS.configGENERATE_RUN_TIME_STATS=1
144145
FREERTOS.configUSE_NEWLIB_REENTRANT=1
145146
File.Version=6
146147
GPIO.groupedBy=Group By Peripherals
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a0b28da69efc77dea2b4d2b3360a0e7a
1+
1fc734b4451a9760aaed5edce1bf8c39

firmware/quintuna/BMS/src/cubemx/Inc/FreeRTOSConfig.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
5252
#include <stdint.h>
5353
extern uint32_t SystemCoreClock;
54+
/* USER CODE BEGIN 0 */
55+
extern void configureTimerForRunTimeStats(void);
56+
extern unsigned long getRunTimeCounterValue(void);
57+
/* USER CODE END 0 */
5458
#endif
5559
#ifndef CMSIS_device_header
5660
#define CMSIS_device_header "stm32h7xx.h"
@@ -70,6 +74,7 @@ extern uint32_t SystemCoreClock;
7074
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
7175
#define configTOTAL_HEAP_SIZE ((size_t)15360)
7276
#define configMAX_TASK_NAME_LEN (16)
77+
#define configGENERATE_RUN_TIME_STATS 1
7378
#define configUSE_TRACE_FACILITY 1
7479
#define configUSE_16_BIT_TICKS 0
7580
#define configUSE_MUTEXES 1
@@ -173,6 +178,12 @@ standard names. */
173178

174179
#define USE_CUSTOM_SYSTICK_HANDLER_IMPLEMENTATION 0
175180

181+
/* USER CODE BEGIN 2 */
182+
/* Definitions needed when configGENERATE_RUN_TIME_STATS is on */
183+
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS configureTimerForRunTimeStats
184+
#define portGET_RUN_TIME_COUNTER_VALUE getRunTimeCounterValue
185+
/* USER CODE END 2 */
186+
176187
/* USER CODE BEGIN Defines */
177188
/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
178189
#include "hw_freeRtosConfigs.h"

firmware/quintuna/BMS/src/cubemx/Src/freertos.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@
5252

5353
/* USER CODE END FunctionPrototypes */
5454

55+
/* Hook prototypes */
56+
void configureTimerForRunTimeStats(void);
57+
unsigned long getRunTimeCounterValue(void);
58+
59+
/* USER CODE BEGIN 1 */
60+
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
61+
__weak void configureTimerForRunTimeStats(void) {}
62+
63+
__weak unsigned long getRunTimeCounterValue(void)
64+
{
65+
return 0;
66+
}
67+
/* USER CODE END 1 */
68+
5569
/* Private application code --------------------------------------------------*/
5670
/* USER CODE BEGIN Application */
5771

firmware/quintuna/BMS/src/tasks.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "hw_cans.h"
2727
#include "hw_adcs.h"
2828
#include "hw_pwms.h"
29+
#include "hw_runTimeStat.h"
2930
#include "hw_hardFaultHandler.h"
3031

3132
// chimera

firmware/quintuna/BMS/src/tasks.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#pragma once
22

3+
4+
typedef enum TaskIndex_e
5+
{
6+
TASK_RUN100HZ = 0,
7+
TASK_RUNCANTX = 1,
8+
TASK_RUNCANRX = 2,
9+
TASK_RUN10HZ = 3,
10+
TASK_RUN1HZ = 4,
11+
TASK_RUNCHIMERA = 5,
12+
TASK_RUNLTCVOLTAGES = 6,
13+
TASK_RUNLTCTEMPS = 7,
14+
TASK_RUNLTCDIAGNOSTICS = 8,
15+
NUM_OF_TASKS = 9
16+
} TaskIndex_e;
17+
318
void tasks_preInit(void);
419
void tasks_init(void);
520
_Noreturn void tasks_run1Hz(void);

firmware/quintuna/FSM/src/tasks.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ void tasks_init(void)
8282
hw_bootup_setBootRequest(boot_request);
8383
}
8484

85-
CpuRunTimeStats cpu_info = {
86-
.cpu_usage_max_setter = app_canTx_FSM_CoreCpuUsage_set,
87-
.cpu_usage_setter = app_canTx_FSM_CoreCpuUsageMax_set
88-
};
89-
85+
CpuRunTimeStats cpu_info = { .cpu_usage_max_setter = app_canTx_FSM_CoreCpuUsage_set,
86+
.cpu_usage_setter = app_canTx_FSM_CoreCpuUsageMax_set };
87+
9088
hw_runtimeStat_registerCpu(&cpu_info);
9189
jobs_init();
9290

firmware/shared/src/hw/hw_runTimeStat.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Needed to ensure we are not running this does not compile for the bootloadeer binary
2+
13
#ifndef BOOTLOADER
24
#include "hw_runTimeStat.h"
35
#include "main.h"
@@ -9,10 +11,10 @@
911
/*
1012
* Software defines
1113
*/
12-
#define IDLE_TASK_INDEX 0U
13-
#define TMR_SVC_INDEX 1U
14-
#define NUM_FT_TASKS 2U
15-
#define NUM_TOTAL_TASKS (NUM_FT_TASKS + NUM_OF_TASKS)
14+
#define IDLE_TASK_INDEX 0U
15+
#define TMR_SVC_INDEX 1U
16+
#define NUM_FT_TASKS 2U
17+
#define NUM_TOTAL_TASKS (NUM_FT_TASKS + NUM_OF_TASKS)
1618

1719
/*
1820
* Global Variables
@@ -21,10 +23,8 @@ static TIM_HandleTypeDef *runTimeCounter;
2123

2224
volatile unsigned long ulHighFrequencyTimerTick = 0;
2325

24-
static TaskRuntimeStats *tasks_runtime_stat[NUM_TOTAL_TASKS] = { [IDLE_TASK_INDEX] =
25-
&(TaskRuntimeStats){ 0 },
26-
[TMR_SVC_INDEX] =
27-
&(TaskRuntimeStats){ 0 } };
26+
static TaskRuntimeStats *tasks_runtime_stat
27+
[NUM_TOTAL_TASKS] = { [IDLE_TASK_INDEX] = &(TaskRuntimeStats){ 0 }, [TMR_SVC_INDEX] = &(TaskRuntimeStats){ 0 } };
2828
// We are going to assume that from here forth all our processors are going
2929
// single core but if not make the following an array
3030

@@ -36,7 +36,7 @@ static CpuRunTimeStats *cpu_runtime_stat;
3636

3737
ExitCode hw_runTimeStat_init(TIM_HandleTypeDef *htim)
3838
{
39-
if (NULL == htim)
39+
if (htim == NULL)
4040
{
4141
return EXIT_CODE_INVALID_ARGS;
4242
}
@@ -48,7 +48,7 @@ ExitCode hw_runTimeStat_init(TIM_HandleTypeDef *htim)
4848

4949
ExitCode hw_runtimeStat_registerCpu(CpuRunTimeStats *cpu_info)
5050
{
51-
if (NULL == cpu_info->cpu_usage_max_setter || NULL == cpu_info->cpu_usage_setter)
51+
if (cpu_info->cpu_usage_max_setter == NULL || cpu_info->cpu_usage_setter == NULL)
5252
{
5353
return EXIT_CODE_INVALID_ARGS;
5454
}
@@ -60,8 +60,8 @@ ExitCode hw_runtimeStat_registerCpu(CpuRunTimeStats *cpu_info)
6060

6161
ExitCode hw_runTimeStat_registerTask(TaskRuntimeStats *task_info)
6262
{
63-
if (NULL == task_info->cpu_usage_max_setter || NULL == task_info->cpu_usage_setter ||
64-
NULL == task_info->stack_usage_max_setter || 0 != task_info->stack_size)
63+
if (task_info->cpu_usage_max_setter == NULL || task_info->cpu_usage_setter == NULL ||
64+
task_info->stack_usage_max_setter == NULL || task_info->stack_size != 0)
6565
{
6666
return EXIT_CODE_INVALID_ARGS;
6767
}
@@ -79,12 +79,15 @@ void hw_runTimeStat_hookCallBack(void)
7979
{
8080
TaskStatus_t runTimeStats[NUM_TOTAL_TASKS];
8181

82+
/* Get the task IDLE handle for processing the time spend outside of idle task*/
8283
TaskHandle_t idleHandle = xTaskGetIdleTaskHandle();
8384

8485
uint32_t arraySize = uxTaskGetSystemState(runTimeStats, (UBaseType_t)NUM_TOTAL_TASKS, NULL);
8586

86-
// given each task that we get from the following getsystemstate call we are gonna calcualte the
87-
// cpu usage and stack usage
87+
/*
88+
* Given each task that we get from the following getsystemstate call we are gonna calcualte the
89+
* cpu usage and stack usage
90+
*/
8891

8992
uint32_t idle_counter = 0;
9093

@@ -95,7 +98,7 @@ void hw_runTimeStat_hookCallBack(void)
9598
idle_counter = runTimeStats[task].ulRunTimeCounter;
9699

97100
// Calculate total current cpu usage and max cpu usage
98-
cpu_runtime_stat->cpu_curr_usage = (float)idle_counter / (float)ulHighFrequencyTimerTick;
101+
cpu_runtime_stat->cpu_curr_usage = 1.0f - ((float)idle_counter / (float)ulHighFrequencyTimerTick);
99102

100103
cpu_runtime_stat->cpu_max_usage = MAX(cpu_runtime_stat->cpu_curr_usage, cpu_runtime_stat->cpu_max_usage);
101104
break;
@@ -105,7 +108,7 @@ void hw_runTimeStat_hookCallBack(void)
105108
for (uint32_t task = 0; task < arraySize; task++)
106109
{
107110
// get the idle time that we need to calculate the cpu usage associated
108-
if (NULL != tasks_runtime_stat[task])
111+
if (tasks_runtime_stat[task] != NULL)
109112
{
110113
if ((idle_counter + runTimeStats[task].ulRunTimeCounter) != 0)
111114
{
@@ -116,7 +119,7 @@ void hw_runTimeStat_hookCallBack(void)
116119
tasks_runtime_stat[task]->cpu_max_usage =
117120
MAX(tasks_runtime_stat[task]->cpu_max_usage, tasks_runtime_stat[task]->cpu_curr_usage);
118121
}
119-
122+
120123
// Calculate max stack usage
121124
tasks_runtime_stat[task]->stack_usage_max =
122125
(float)runTimeStats[task].usStackHighWaterMark / (float)tasks_runtime_stat[task]->stack_size;

0 commit comments

Comments
 (0)