Skip to content

Commit

Permalink
fix: Allocate more stack memory to tasks. (#181)
Browse files Browse the repository at this point in the history
* fix: Allocate more stack memory to tasks.

While testing TMS, I came across a strange bug where Update() would hang when the OS was used. Replacing the OS with manual timer & function call avoided the problem. The cause was the large `RawCanMsg[100]` array in `CanBase::ReadQueue()` which exceeded the memory limits of FreeRTOS. This was a very strange error as the memory limit was only slightly exceeded, so calling ReadQueue (even with all internal code commented out) exceeded the memory, but then removing `virtual` from `ReadQueue` removed enough memory to call the function.

* bring up tms to new bindings structure
  • Loading branch information
BlakeFreer authored Sep 9, 2024
1 parent 8247f1a commit 2559c2f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 116 deletions.
24 changes: 24 additions & 0 deletions firmware/projects/EV5/TMS/bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "shared/periph/adc.h"
#include "shared/periph/can.h"
#include "shared/periph/gpio.h"
#include "shared/periph/pwm.h"

namespace bindings {
extern shared::periph::ADCInput& temp_sensor_adc_1;
extern shared::periph::ADCInput& temp_sensor_adc_2;
extern shared::periph::ADCInput& temp_sensor_adc_3;
extern shared::periph::ADCInput& temp_sensor_adc_4;
extern shared::periph::ADCInput& temp_sensor_adc_5;
extern shared::periph::ADCInput& temp_sensor_adc_6;

extern shared::periph::PWMOutput& fan_controller_pwm;

extern shared::periph::DigitalOutput& debug_led_green;
extern shared::periph::DigitalOutput& debug_led_red;

extern shared::periph::CanBase& veh_can_base;

extern void Initialize();
} // namespace bindings
36 changes: 9 additions & 27 deletions firmware/projects/EV5/TMS/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>

#include "app.h"
#include "bindings.h"
#include "shared/comms/can/can_bus.h"
#include "shared/os/tick.h"
#include "shared/periph/adc.h"
Expand All @@ -16,25 +17,6 @@
#include "veh_can_messages.h"
#include "veh_msg_registry.h"

namespace bindings {
extern shared::periph::ADCInput& temp_sensor_adc_1;
extern shared::periph::ADCInput& temp_sensor_adc_2;
extern shared::periph::ADCInput& temp_sensor_adc_3;
extern shared::periph::ADCInput& temp_sensor_adc_4;
extern shared::periph::ADCInput& temp_sensor_adc_5;
extern shared::periph::ADCInput& temp_sensor_adc_6;

extern shared::periph::PWMOutput& fan_controller_pwm;

extern shared::periph::DigitalOutput& debug_do_blue;
extern shared::periph::DigitalOutput& debug_do_red;

extern shared::periph::CanBase& veh_can_base;

extern void Initialize();
extern void Log(std::string);
} // namespace bindings

namespace os {
extern void Tick(uint32_t ticks);
extern void InitializeKernel();
Expand Down Expand Up @@ -84,11 +66,9 @@ const float temp_lut_data[][2] = {
};

const float fan_lut_data[][2] = {
// clang-format off
{-1, 0},
{ 0, 30},
{50, 100}
// clang-format on
{-1, 0},
{0, 30},
{50, 100},
};

constexpr int temp_lut_length =
Expand All @@ -115,8 +95,8 @@ shared::can::CanBus veh_can_bus{
***************************************************************/
FanContoller fan_controller{bindings::fan_controller_pwm, fan_temp_lut, 2.0f};

DebugIndicator debug_blue{bindings::debug_do_blue};
DebugIndicator debug_red{bindings::debug_do_red};
DebugIndicator debug_green{bindings::debug_led_green};
DebugIndicator debug_red{bindings::debug_led_red};

TempSensor temp_sensors[] = {
TempSensor{bindings::temp_sensor_adc_1, temp_adc_lut},
Expand All @@ -141,6 +121,9 @@ void Update() {
static uint8_t low_thermistor_idx;
static uint8_t high_thermistor_idx;

debug_green.Toggle();
debug_red.Toggle();

veh_can_bus.Update();
ts_manager.Update();
ts_manager.GetTemperatures(temperature_buffer);
Expand Down Expand Up @@ -168,7 +151,6 @@ void UpdateTask(void* argument) {
while (true) {
uint32_t start_time_ms = os::GetTickCount();
Update();
debug_blue.Toggle(); // toggling indicates the loop is running
os::TickUntil(start_time_ms + kTaskPeriodMs);
}
}
Expand Down
2 changes: 0 additions & 2 deletions firmware/projects/EV5/TMS/platforms/cli/CMakeLists.txt

This file was deleted.

50 changes: 0 additions & 50 deletions firmware/projects/EV5/TMS/platforms/cli/bindings.cc

This file was deleted.

3 changes: 0 additions & 3 deletions firmware/projects/EV5/TMS/platforms/cli/mcal_conf.cmake

This file was deleted.

3 changes: 2 additions & 1 deletion firmware/projects/EV5/TMS/platforms/stm32f767/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

add_subdirectory(cubemx)

target_sources(bindings PUBLIC bindings.cc os.cc)
target_sources(bindings PUBLIC bindings.cc)
target_include_directories(bindings PUBLIC ${DIR_PROJECT})
target_link_libraries(bindings PUBLIC driver mcal-stm32f767)

add_library(os)
Expand Down
31 changes: 15 additions & 16 deletions firmware/projects/EV5/TMS/platforms/stm32f767/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "tim.h"

// fw imports
#include "bindings.h"
#include "mcal/stm32f767/periph/adc.h"
#include "mcal/stm32f767/periph/gpio.h"
#include "mcal/stm32f767/periph/pwm.h"
Expand All @@ -39,26 +40,28 @@ ADCInput temp_sensor_adc_5{&hadc1, SENS_5_UC_IN_CHANNEL};
ADCInput temp_sensor_adc_6{&hadc1, SENS_6_UC_IN_CHANNEL};

PWMOutput fan_controller_pwm{&htim4, TIM_CHANNEL_1};
DigitalOutput debug_do_blue{NUCLEO_BLUE_LED_GPIO_Port, NUCLEO_BLUE_LED_Pin};
DigitalOutput debug_do_red{NUCLEO_RED_LED_GPIO_Port, NUCLEO_RED_LED_Pin};
DigitalOutput debug_led_green{DEBUG_LED1_GPIO_Port, DEBUG_LED1_Pin};
DigitalOutput debug_led_red{DEBUG_LED2_GPIO_Port, DEBUG_LED2_Pin};
DigitalOutput debug_led_nucleo{NUCLEO_RED_LED_GPIO_Port, NUCLEO_RED_LED_Pin};

CanBase veh_can_base{&hcan2};

} // namespace mcal

namespace bindings {
const shared::periph::ADCInput& temp_sensor_adc_1 = mcal::temp_sensor_adc_1;
const shared::periph::ADCInput& temp_sensor_adc_2 = mcal::temp_sensor_adc_2;
const shared::periph::ADCInput& temp_sensor_adc_3 = mcal::temp_sensor_adc_3;
const shared::periph::ADCInput& temp_sensor_adc_4 = mcal::temp_sensor_adc_4;
const shared::periph::ADCInput& temp_sensor_adc_5 = mcal::temp_sensor_adc_5;
const shared::periph::ADCInput& temp_sensor_adc_6 = mcal::temp_sensor_adc_6;
shared::periph::ADCInput& temp_sensor_adc_1 = mcal::temp_sensor_adc_1;
shared::periph::ADCInput& temp_sensor_adc_2 = mcal::temp_sensor_adc_2;
shared::periph::ADCInput& temp_sensor_adc_3 = mcal::temp_sensor_adc_3;
shared::periph::ADCInput& temp_sensor_adc_4 = mcal::temp_sensor_adc_4;
shared::periph::ADCInput& temp_sensor_adc_5 = mcal::temp_sensor_adc_5;
shared::periph::ADCInput& temp_sensor_adc_6 = mcal::temp_sensor_adc_6;

const shared::periph::PWMOutput& fan_controller_pwm = mcal::fan_controller_pwm;
const shared::periph::DigitalOutput& debug_do_blue = mcal::debug_do_blue;
const shared::periph::DigitalOutput& debug_do_red = mcal::debug_do_red;
shared::periph::PWMOutput& fan_controller_pwm = mcal::fan_controller_pwm;
shared::periph::DigitalOutput& debug_led_green = mcal::debug_led_green;
shared::periph::DigitalOutput& debug_led_red = mcal::debug_led_red;
// shared::periph::DigitalOutput& debug_led_red = mcal::debug_led_nucleo;

const shared::periph::CanBase& veh_can_base = mcal::veh_can_base;
shared::periph::CanBase& veh_can_base = mcal::veh_can_base;

void Initialize() {
SystemClock_Config();
Expand All @@ -69,8 +72,4 @@ void Initialize() {
MX_CAN2_Init();
}

void Log(std::string s) {
return;
}

} // namespace bindings
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ CAN2.CalculateTimeQuantum=125.0
CAN2.IPParameters=CalculateTimeQuantum,CalculateBaudRate,Prescaler,CalculateTimeBit,BS1,BS2,ABOM
CAN2.Prescaler=6
FREERTOS.FootprintOK=true
FREERTOS.IPParameters=Tasks01,FootprintOK
FREERTOS.Tasks01=Update,24,128,UpdateTask,As weak,NULL,Static,UpdateBuffer,UpdateControlBlock
FREERTOS.IPParameters=Tasks01,FootprintOK,configTOTAL_HEAP_SIZE
FREERTOS.Tasks01=Update,24,4096,UpdateTask,As weak,NULL,Static,UpdateBuffer,UpdateControlBlock
FREERTOS.configTOTAL_HEAP_SIZE=102400
File.Version=6
GPIO.groupedBy=Group By Peripherals
KeepUserPlacement=false
Expand All @@ -35,21 +36,23 @@ Mcu.IP7=TIM4
Mcu.IPNb=8
Mcu.Name=STM32F767ZITx
Mcu.Package=LQFP144
Mcu.Pin0=PC0
Mcu.Pin1=PC1
Mcu.Pin10=PB7
Mcu.Pin11=VP_FREERTOS_VS_CMSIS_V2
Mcu.Pin12=VP_SYS_VS_tim3
Mcu.Pin13=VP_TIM4_VS_ClockSourceINT
Mcu.Pin2=PC2
Mcu.Pin3=PC3
Mcu.Pin4=PC4
Mcu.Pin5=PC5
Mcu.Pin6=PB13
Mcu.Pin7=PB14
Mcu.Pin8=PB5
Mcu.Pin9=PB6
Mcu.PinsNb=14
Mcu.Pin0=PE2
Mcu.Pin1=PC0
Mcu.Pin10=PB5
Mcu.Pin11=PB6
Mcu.Pin12=PB7
Mcu.Pin13=VP_FREERTOS_VS_CMSIS_V2
Mcu.Pin14=VP_SYS_VS_tim3
Mcu.Pin15=VP_TIM4_VS_ClockSourceINT
Mcu.Pin2=PC1
Mcu.Pin3=PC2
Mcu.Pin4=PC3
Mcu.Pin5=PA3
Mcu.Pin6=PC4
Mcu.Pin7=PC5
Mcu.Pin8=PB13
Mcu.Pin9=PB14
Mcu.PinsNb=16
Mcu.ThirdPartyNb=0
Mcu.UserConstants=SENS_2_UC_IN_CHANNEL,ADC_CHANNEL_11;SENS_6_UC_IN_CHANNEL,ADC_CHANNEL_15;SENS_3_UC_IN_CHANNEL,ADC_CHANNEL_12;SENS_1_UC_IN_CHANNEL,ADC_CHANNEL_10;SENS_4_UC_IN_CHANNEL,ADC_CHANNEL_13;SENS_5_UC_IN_CHANNEL,ADC_CHANNEL_14
Mcu.UserName=STM32F767ZITx
Expand All @@ -73,6 +76,10 @@ NVIC.TIM3_IRQn=true\:15\:0\:false\:false\:true\:false\:false\:true\:true
NVIC.TimeBase=TIM3_IRQn
NVIC.TimeBaseIP=TIM3
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:false\:false
PA3.GPIOParameters=GPIO_Label
PA3.GPIO_Label=DEBUG_LED2
PA3.Locked=true
PA3.Signal=GPIO_Output
PB13.Locked=true
PB13.Mode=CAN_Activate
PB13.Signal=CAN2_TX
Expand Down Expand Up @@ -115,6 +122,10 @@ PC5.GPIOParameters=GPIO_Label
PC5.GPIO_Label=SENS_6_UC_IN
PC5.Locked=true
PC5.Signal=ADCx_IN15
PE2.GPIOParameters=GPIO_Label
PE2.GPIO_Label=DEBUG_LED1
PE2.Locked=true
PE2.Signal=GPIO_Output
PinOutPanel.RotationAngle=0
ProjectManager.AskForMigrate=true
ProjectManager.BackupPrevious=false
Expand Down

0 comments on commit 2559c2f

Please sign in to comment.