From 61c8d173a1dad59fccf7c47e7b0fce17ec31f87e Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 23 May 2024 19:08:23 +0300 Subject: [PATCH 01/38] added state_machine.c --- Core/Inc/state_machine.h | 71 +++++++++++++++++++ Core/Src/main.c | 10 --- Core/Src/state_machine.c | 146 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+), 10 deletions(-) create mode 100644 Core/Inc/state_machine.h create mode 100644 Core/Src/state_machine.c diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h new file mode 100644 index 0000000..478c6a6 --- /dev/null +++ b/Core/Inc/state_machine.h @@ -0,0 +1,71 @@ +//int errorcode[2] = {0,0}; 1 Bit per error +#include +#include + +// Minimum vehicle side voltage to exit precharge +#define MIN_VEHICLE_SIDE_VOLTAGE 150000 // mV +// Time to wait after reaching 95% of battery voltage before exiting precharge +// Set this to 1000 in scruti to demonstrate the voltage on the multimeter +#define PRECHARGE_95_DURATION 0 // ms +// Time to wait for discharge +#define DISCHARGE_DURATION 5000 // ms +// Time to wait after there is no more error condition before exiting TS_ERROR +#define NO_ERROR_TIME 1000 // ms +// Time to wait for charger voltage before going to TS_ERROR +#define MAX_CHARGING_CHECK_DURATION 2000 // ms +// Time to wait between closing relays +#define RELAY_CLOSE_WAIT 10 // ms + +typedef enum { // valid transitions: (all could transition to error) + INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR + PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR + READY, // READY -> ACTIVE, DISCHARGE, ERROR + ACTIVE, // ACTIVE -> READY, DISCHARGE, ERROR + DISCHARGE, // DISCHARGE -> INACTIVE, PRECHARGE, ERROR + CHARGING, // CHARGING -> INACTIVE, DISCHARGE, ERROR + ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR +} State; + +typedef enum { + ERRORKIND_NONE = 0x0, + ERRORKIND_BMS_TIMEOUT = 0x1, + ERRORKIND_OVERTEMPTURE = 0x2, + ERRORKIND_OVERCURRENT = 0x3, + ERRORKIND_OVERVOLTAGE = 0x4, + ERRORKIND_MISSING_CURRENT_READING = 0x5, + ERRORKIND_MISSING_VOLTAGE_READING = 0x6, + ERRORKIND_MISSING_TEMP_READING = 0x7, + ERRORKIND_PRECHARGE_FAIL = 0x8, + ERRORKIND_POWERGROUND_FAIL = 0x9 +} ErrorKind; + +//typedef enum {} WarningKind; + +typedef struct { + State current_state; + State target_state; + uint16_t error_source; // TSErrorSource (bitmask) + uint16_t error_type; // TSErrorKind +} StateHandle; + +extern StateHandle state; + +void sm_init(); +void sm_update(); + +State sm_update_inactive(); +State sm_update_active(); +State sm_update_precharge(); +State sm_update_discharge(); +State sm_update_error(); +State sm_update_charging_check(); +State sm_update_charging(); + +typedef enum { RELAY_POS, RELAY_PRECHARGE } Relay; +void sm_set_relay_positions(State state); +void sm_set_relay_position(Relay relay, int closed); +void sm_check_close_wait(int *is_closed, int should_close); + +void sm_handle_ams_in(const uint8_t *data); + +void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 4cd65ab..523bd73 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -121,9 +121,6 @@ int main(void) AMS_Init(&hspi1); ams_can_init(&hcan); - bool relay_actual = false; - bool relay_target = true; - uint32_t target_time = HAL_GetTick() + 10000; /* USER CODE END 2 */ @@ -142,13 +139,6 @@ int main(void) ams_can_send_error(); } ams_can_send_log(); - - if (HAL_GetTick() >= target_time) { - HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, relay_target ? GPIO_PIN_SET : GPIO_PIN_RESET); - HAL_GPIO_WritePin(STATUS_LED_R_GPIO_Port, STATUS_LED_R_Pin, relay_target ? GPIO_PIN_RESET : GPIO_PIN_SET); - target_time = HAL_GetTick() + 10000; - relay_target = !relay_target; - } } /* USER CODE END 3 */ } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c new file mode 100644 index 0000000..3aa8808 --- /dev/null +++ b/Core/Src/state_machine.c @@ -0,0 +1,146 @@ +#include "state_machine.h" + +StateHandle state; + +void sm_init(){ + state.current_state = INACTIVE; + state.target_state = INACTIVE; + state.error_source = 0; +} + +void sm_update(){ + + if (state.error_source) { + state.current_state = ERROR; + } + + switch (state.current_state) { + case INACTIVE: + //state.current_state = sm_update_inactive(); moniter only + break; + case PRECHARGE: + //state.current_state = sm_update_precharge(); set PRECHARGE and turn on cooling at 50% or such + break; + case READY: + //state.current_state = ; keep cooling at 50%, get ready to turn on powerground + break; + case ACTIVE: + //state.current_state = sm_update_precharge(); set PRECHARGE and turn on cooling at 50% or such + break; + case DISCHARGE: + //state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed + break; + case CHARGING: + //state.current_state = sm_update_charging(); // turn on cooling if needed, + break; + case ERROR: + //state.current_state = sm_update_error(); // enter the correct ERROR state + break; + case WARNING: + //state.current_state = sm_update_error(); // send warnings through CAN + break; + } + + sm_set_relay_positions(state.current_state); + status_led_state(state.current_state, (ErrorKind) state.error_type); +} + +State sm_update_inactive(){ + switch (state.target_state) { + case PRECHARGE: + if () + break; + case CHARGING: + break; + } + return INACTIVE; +} + +State sm_update_precharge(){ + switch (state.target_state) { + case INACTIVE: + break; + case READY: + break; + case DISCHARGE: + break; + } + return PRECHARGE; +} + +State sm_update_ready(){ + switch (state.target_state) { + case ACTIVE: + break; + case DISCHARGE: + break; + } + return READY; +} + +State sm_update_active(){ + switch (state.target_state) { + case READY: + break; + case DISCHARGE: + break; + } + return ACTIVE; +} + +State sm_update_discharge(){ + switch (state.target_state) { + case INACTIVE: + break; + case PRECHARGE: + break; + } + return DISCHARGE; +} + +State sm_update_charging(){ + switch (state.target_state) { + case INACTIVE: + break; + case DISCHARGE: + break; + } + return CHARGING; +} + +State sm_update_error(){ + switch (state.target_state) { + case INACTIVE: + break; + case DISCHARGE: + break; + } + return ERROR; +} + +void sm_set_relay_positions(State state){ + return; +} + +void sm_set_relay(Relay relay, int closed){ + static int closed = 0; + static int precharge_closed = 0; + + GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET; + switch (relay) { + case RELAY_POS: + ts_sm_check_close_wait(&closed, closed); + HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state); + break; + case RELAY_PRECHARGE: + ts_sm_check_close_wait(&precharge_closed, closed); + HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state); + break; + } +} + +//void sm_check_close_wait(int *is_closed, int should_close); + +//void sm_handle_ams_in(const uint8_t *data); + +//void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file From 1642627dd2e36a98832478ae16dd69cfb77f9f27 Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 23 May 2024 19:08:46 +0300 Subject: [PATCH 02/38] updated the state machine --- Core/Inc/state_machine.h | 6 +++--- Core/Src/state_machine.c | 23 ++++++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index 478c6a6..a7d422e 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -54,12 +54,12 @@ void sm_init(); void sm_update(); State sm_update_inactive(); -State sm_update_active(); State sm_update_precharge(); +State sm_update_ready(); +State sm_update_active(); State sm_update_discharge(); -State sm_update_error(); -State sm_update_charging_check(); State sm_update_charging(); +State sm_update_error(); typedef enum { RELAY_POS, RELAY_PRECHARGE } Relay; void sm_set_relay_positions(State state); diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 3aa8808..24a48dc 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -16,39 +16,35 @@ void sm_update(){ switch (state.current_state) { case INACTIVE: - //state.current_state = sm_update_inactive(); moniter only + state.current_state = sm_update_inactive(); // moniter only break; case PRECHARGE: - //state.current_state = sm_update_precharge(); set PRECHARGE and turn on cooling at 50% or such + state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such break; case READY: - //state.current_state = ; keep cooling at 50%, get ready to turn on powerground + state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground break; case ACTIVE: - //state.current_state = sm_update_precharge(); set PRECHARGE and turn on cooling at 50% or such + state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such break; case DISCHARGE: - //state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed + state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed break; case CHARGING: - //state.current_state = sm_update_charging(); // turn on cooling if needed, + state.current_state = sm_update_charging(); // turn on cooling if needed, break; case ERROR: - //state.current_state = sm_update_error(); // enter the correct ERROR state - break; - case WARNING: - //state.current_state = sm_update_error(); // send warnings through CAN + state.current_state = sm_update_error(); // enter the correct ERROR state break; } sm_set_relay_positions(state.current_state); - status_led_state(state.current_state, (ErrorKind) state.error_type); + //status_led_state(state.current_state, (ErrorKind) state.error_type); } State sm_update_inactive(){ switch (state.target_state) { case PRECHARGE: - if () break; case CHARGING: break; @@ -121,7 +117,7 @@ State sm_update_error(){ void sm_set_relay_positions(State state){ return; } - +/* void sm_set_relay(Relay relay, int closed){ static int closed = 0; static int precharge_closed = 0; @@ -138,6 +134,7 @@ void sm_set_relay(Relay relay, int closed){ break; } } +*/ //void sm_check_close_wait(int *is_closed, int should_close); From d2478b5ff87661b56b2b23eb21cb9c810414f19e Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 23 May 2024 19:09:32 +0300 Subject: [PATCH 03/38] removed aux_in aux_out, corrected CAN timings. ioc --- mvbms.ioc | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 mvbms.ioc diff --git a/mvbms.ioc b/mvbms.ioc new file mode 100644 index 0000000..80b07a5 --- /dev/null +++ b/mvbms.ioc @@ -0,0 +1,261 @@ +#MicroXplorer Configuration settings - do not modify +CAD.formats= +CAD.pinconfig= +CAD.provider= +CAN.ABOM=ENABLE +CAN.BS1=CAN_BS1_13TQ +CAN.BS2=CAN_BS2_2TQ +CAN.CalculateBaudRate=500000 +CAN.CalculateTimeBit=2000 +CAN.CalculateTimeQuantum=125.0 +CAN.IPParameters=CalculateTimeQuantum,CalculateTimeBit,CalculateBaudRate,BS1,BS2,ABOM,Prescaler,NART +CAN.NART=ENABLE +CAN.Prescaler=2 +File.Version=6 +GPIO.groupedBy=Group By Peripherals +KeepUserPlacement=true +Mcu.CPN=STM32F302CBT6 +Mcu.Family=STM32F3 +Mcu.IP0=CAN +Mcu.IP1=I2C1 +Mcu.IP2=NVIC +Mcu.IP3=RCC +Mcu.IP4=SPI1 +Mcu.IP5=SYS +Mcu.IP6=TIM1 +Mcu.IP7=TIM15 +Mcu.IP8=USART1 +Mcu.IPNb=9 +Mcu.Name=STM32F302C(B-C)Tx +Mcu.Package=LQFP48 +Mcu.Pin0=PF0-OSC_IN +Mcu.Pin1=PF1-OSC_OUT +Mcu.Pin10=PB0 +Mcu.Pin11=PB1 +Mcu.Pin12=PB2 +Mcu.Pin13=PB11 +Mcu.Pin14=PB15 +Mcu.Pin15=PA8 +Mcu.Pin16=PA9 +Mcu.Pin17=PA10 +Mcu.Pin18=PA11 +Mcu.Pin19=PA12 +Mcu.Pin2=PA0 +Mcu.Pin20=PA13 +Mcu.Pin21=PA14 +Mcu.Pin22=PA15 +Mcu.Pin23=PB3 +Mcu.Pin24=PB6 +Mcu.Pin25=PB7 +Mcu.Pin26=PB9 +Mcu.Pin27=VP_SYS_VS_Systick +Mcu.Pin3=PA1 +Mcu.Pin4=PA2 +Mcu.Pin5=PA3 +Mcu.Pin6=PA4 +Mcu.Pin7=PA5 +Mcu.Pin8=PA6 +Mcu.Pin9=PA7 +Mcu.PinsNb=28 +Mcu.ThirdPartyNb=0 +Mcu.UserConstants= +Mcu.UserName=STM32F302CBTx +MxCube.Version=6.11.1 +MxDb.Version=DB.6.0.111 +NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.ForceEnableDMAVector=true +NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 +NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false +NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +PA0.GPIOParameters=PinState,GPIO_Label +PA0.GPIO_Label=RELAY_EN +PA0.Locked=true +PA0.PinState=GPIO_PIN_RESET +PA0.Signal=GPIO_Output +PA1.GPIOParameters=PinState,GPIO_Label +PA1.GPIO_Label=_60V_EN +PA1.Locked=true +PA1.PinState=GPIO_PIN_RESET +PA1.Signal=GPIO_Output +PA10.GPIOParameters=GPIO_Label +PA10.GPIO_Label=CURRENT_SENSOR_ON +PA10.Locked=true +PA10.Signal=GPIO_Input +PA11.Locked=true +PA11.Mode=CAN_Activate +PA11.Signal=CAN_RX +PA12.Locked=true +PA12.Mode=CAN_Activate +PA12.Signal=CAN_TX +PA13.Locked=true +PA13.Mode=Trace_Asynchronous_SW +PA13.Signal=SYS_JTMS-SWDIO +PA14.Locked=true +PA14.Mode=Trace_Asynchronous_SW +PA14.Signal=SYS_JTCK-SWCLK +PA15.Locked=true +PA15.Mode=I2C +PA15.Signal=I2C1_SCL +PA2.GPIOParameters=GPIO_Label +PA2.GPIO_Label=PWM_PG_FAN1 +PA2.Locked=true +PA2.Signal=S_TIM15_CH1 +PA3.GPIOParameters=GPIO_Label +PA3.GPIO_Label=PWM_PG_FAN2 +PA3.Locked=true +PA3.Signal=S_TIM15_CH2 +PA4.GPIOParameters=GPIO_Label +PA4.GPIO_Label=CSB +PA4.Locked=true +PA4.Signal=GPIO_Output +PA5.Locked=true +PA5.Mode=Full_Duplex_Master +PA5.Signal=SPI1_SCK +PA6.Locked=true +PA6.Mode=Full_Duplex_Master +PA6.Signal=SPI1_MISO +PA7.Locked=true +PA7.Mode=Full_Duplex_Master +PA7.Signal=SPI1_MOSI +PA8.GPIOParameters=GPIO_Label +PA8.GPIO_Label=RELAY_BATT_SIDE_ON +PA8.Locked=true +PA8.Signal=GPIO_Input +PA9.GPIOParameters=GPIO_Label +PA9.GPIO_Label=RELAY_ESC_SIDE_ON +PA9.Locked=true +PA9.Signal=GPIO_Input +PB0.GPIOParameters=PinState,GPIO_Label +PB0.GPIO_Label=STATUS_LED_R +PB0.Locked=true +PB0.PinState=GPIO_PIN_SET +PB0.Signal=GPIO_Output +PB1.GPIOParameters=PinState,GPIO_Label +PB1.GPIO_Label=STATUS_LED_B +PB1.Locked=true +PB1.PinState=GPIO_PIN_SET +PB1.Signal=GPIO_Output +PB11.GPIOParameters=PinState,GPIO_Label +PB11.GPIO_Label=PRECHARGE_EN +PB11.Locked=true +PB11.PinState=GPIO_PIN_RESET +PB11.Signal=GPIO_Output +PB15.Locked=true +PB15.Mode=PWM Generation3 CH3N +PB15.Signal=TIM1_CH3N +PB2.GPIOParameters=PinState,GPIO_Label +PB2.GPIO_Label=STATUS_LED_G +PB2.Locked=true +PB2.PinState=GPIO_PIN_SET +PB2.Signal=GPIO_Output +PB3.Locked=true +PB3.Mode=Trace_Asynchronous_SW +PB3.Signal=SYS_JTDO-TRACESWO +PB6.Locked=true +PB6.Mode=Asynchronous +PB6.Signal=USART1_TX +PB7.Locked=true +PB7.Mode=Asynchronous +PB7.Signal=USART1_RX +PB9.Locked=true +PB9.Mode=I2C +PB9.Signal=I2C1_SDA +PF0-OSC_IN.Locked=true +PF0-OSC_IN.Mode=HSE-External-Oscillator +PF0-OSC_IN.Signal=RCC_OSC_IN +PF1-OSC_OUT.Locked=true +PF1-OSC_OUT.Mode=HSE-External-Oscillator +PF1-OSC_OUT.Signal=RCC_OSC_OUT +PinOutPanel.RotationAngle=0 +ProjectManager.AskForMigrate=true +ProjectManager.BackupPrevious=false +ProjectManager.CompilerOptimize=6 +ProjectManager.ComputerToolchain=false +ProjectManager.CoupleFile=false +ProjectManager.CustomerFirmwarePackage= +ProjectManager.DefaultFWLocation=true +ProjectManager.DeletePrevious=true +ProjectManager.DeviceId=STM32F302CBTx +ProjectManager.FirmwarePackage=STM32Cube FW_F3 V1.11.4 +ProjectManager.FreePins=true +ProjectManager.HalAssertFull=false +ProjectManager.HeapSize=0x200 +ProjectManager.KeepUserCode=true +ProjectManager.LastFirmware=true +ProjectManager.LibraryCopy=1 +ProjectManager.MainLocation=Core/Src +ProjectManager.NoMain=false +ProjectManager.PreviousToolchain= +ProjectManager.ProjectBuild=false +ProjectManager.ProjectFileName=mvbms.ioc +ProjectManager.ProjectName=mvbms +ProjectManager.ProjectStructure= +ProjectManager.RegisterCallBack= +ProjectManager.StackSize=0x400 +ProjectManager.TargetToolchain=Makefile +ProjectManager.ToolChainLocation= +ProjectManager.UAScriptAfterPath= +ProjectManager.UAScriptBeforePath= +ProjectManager.UnderRoot=false +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM15_Init-TIM15-false-HAL-true,7-MX_USART1_UART_Init-USART1-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true +RCC.ADC12outputFreq_Value=16000000 +RCC.AHBFreq_Value=16000000 +RCC.APB1Freq_Value=16000000 +RCC.APB1TimFreq_Value=16000000 +RCC.APB2Freq_Value=16000000 +RCC.APB2TimFreq_Value=16000000 +RCC.CortexFreq_Value=16000000 +RCC.FCLKCortexFreq_Value=16000000 +RCC.FamilyName=M +RCC.HCLKFreq_Value=16000000 +RCC.HSEPLLFreq_Value=16000000 +RCC.HSE_VALUE=16000000 +RCC.HSIPLLFreq_Value=4000000 +RCC.HSI_VALUE=8000000 +RCC.I2C1Freq_Value=8000000 +RCC.I2C2Freq_Value=8000000 +RCC.IPParameters=ADC12outputFreq_Value,AHBFreq_Value,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HSEPLLFreq_Value,HSE_VALUE,HSIPLLFreq_Value,HSI_VALUE,I2C1Freq_Value,I2C2Freq_Value,LSE_VALUE,LSI_VALUE,MCOFreq_Value,PLLCLKFreq_Value,PLLMCOFreq_Value,PLLMUL,RTCFreq_Value,RTCHSEDivFreq_Value,SYSCLKFreq_VALUE,SYSCLKSourceVirtual,TIM1Freq_Value,TIM2Freq_Value,USART1Freq_Value,USART2Freq_Value,USART3Freq_Value,USBFreq_Value,VCOOutput2Freq_Value +RCC.LSE_VALUE=32768 +RCC.LSI_VALUE=40000 +RCC.MCOFreq_Value=16000000 +RCC.PLLCLKFreq_Value=16000000 +RCC.PLLMCOFreq_Value=8000000 +RCC.PLLMUL=RCC_PLL_MUL4 +RCC.RTCFreq_Value=40000 +RCC.RTCHSEDivFreq_Value=500000 +RCC.SYSCLKFreq_VALUE=16000000 +RCC.SYSCLKSourceVirtual=RCC_SYSCLKSOURCE_HSE +RCC.TIM1Freq_Value=16000000 +RCC.TIM2Freq_Value=16000000 +RCC.USART1Freq_Value=16000000 +RCC.USART2Freq_Value=16000000 +RCC.USART3Freq_Value=16000000 +RCC.USBFreq_Value=16000000 +RCC.VCOOutput2Freq_Value=4000000 +SH.S_TIM15_CH1.0=TIM15_CH1,PWM Generation1 CH1 +SH.S_TIM15_CH1.ConfNb=1 +SH.S_TIM15_CH2.0=TIM15_CH2,PWM Generation2 CH2 +SH.S_TIM15_CH2.ConfNb=1 +SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32 +SPI1.CalculateBaudRate=500.0 KBits/s +SPI1.DataSize=SPI_DATASIZE_8BIT +SPI1.Direction=SPI_DIRECTION_2LINES +SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,BaudRatePrescaler +SPI1.Mode=SPI_MODE_MASTER +SPI1.VirtualType=VM_MASTER +TIM1.Channel-PWM\ Generation3\ CH3N=TIM_CHANNEL_3 +TIM1.IPParameters=Channel-PWM Generation3 CH3N +TIM15.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 +TIM15.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 +TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2 +USART1.IPParameters=VirtualMode-Asynchronous +USART1.VirtualMode-Asynchronous=VM_ASYNC +VP_SYS_VS_Systick.Mode=SysTick +VP_SYS_VS_Systick.Signal=SYS_VS_Systick +board=custom From d7012665094f06e5c7b0fb5b7222a16d193efa98 Mon Sep 17 00:00:00 2001 From: kbracher Date: Thu, 23 May 2024 18:27:51 +0200 Subject: [PATCH 04/38] switch errorkind to bitfield --- Core/Inc/state_machine.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index a7d422e..2240194 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -26,17 +26,16 @@ typedef enum { // valid transitions: (all could transition to error) ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR } State; -typedef enum { - ERRORKIND_NONE = 0x0, - ERRORKIND_BMS_TIMEOUT = 0x1, - ERRORKIND_OVERTEMPTURE = 0x2, - ERRORKIND_OVERCURRENT = 0x3, - ERRORKIND_OVERVOLTAGE = 0x4, - ERRORKIND_MISSING_CURRENT_READING = 0x5, - ERRORKIND_MISSING_VOLTAGE_READING = 0x6, - ERRORKIND_MISSING_TEMP_READING = 0x7, - ERRORKIND_PRECHARGE_FAIL = 0x8, - ERRORKIND_POWERGROUND_FAIL = 0x9 +typedef struct { + uint16_t bms_timeout : 1; + uint16_t overtemp : 1; + uint16_t overcurrent : 1; + uint16_t overvoltage : 1; + uint16_t missing_current_reading : 1; + uint16_t missing_voltage_reading : 1; + uint16_t missing_temp_reading : 1; + uint16_t precharge_fail : 1; + uint16_t powerground_fail : 1; } ErrorKind; //typedef enum {} WarningKind; @@ -45,7 +44,7 @@ typedef struct { State current_state; State target_state; uint16_t error_source; // TSErrorSource (bitmask) - uint16_t error_type; // TSErrorKind + ErrorKind error_type; // TSErrorKind } StateHandle; extern StateHandle state; From 665d1ffed6d0083a977ed9d0b3f2a267399d9e15 Mon Sep 17 00:00:00 2001 From: kbracher Date: Thu, 23 May 2024 19:19:46 +0200 Subject: [PATCH 05/38] clean up can code --- Core/Inc/AMS_CAN.h | 7 ------- Core/Src/AMS_CAN.c | 43 ++----------------------------------------- Core/Src/main.c | 5 ----- 3 files changed, 2 insertions(+), 53 deletions(-) diff --git a/Core/Inc/AMS_CAN.h b/Core/Inc/AMS_CAN.h index 103db6c..d814d38 100644 --- a/Core/Inc/AMS_CAN.h +++ b/Core/Inc/AMS_CAN.h @@ -21,16 +21,9 @@ void ams_can_init(CAN_HandleTypeDef* hcan); void ams_can_handle_ams_msg(CAN_RxHeaderTypeDef* header, uint8_t* data); void ams_can_send_status(); -/** - * @brief Send an AMS Error via CAN. - * - * Data is taken from error_data - */ -void ams_can_send_error(); HAL_StatusTypeDef ams_can_wait_for_free_mailboxes(CAN_HandleTypeDef* handle, int num_mailboxes, uint32_t timeout); -void ams_can_send_log(); #endif /* INC_AMS_CAN_H_ */ diff --git a/Core/Src/AMS_CAN.c b/Core/Src/AMS_CAN.c index 99ca38e..4ff7b40 100644 --- a/Core/Src/AMS_CAN.c +++ b/Core/Src/AMS_CAN.c @@ -25,9 +25,7 @@ #include #include -#define CAN_ID_SLAVE_PANIC 0x009 -#define CAN_ID_SLAVE_STATUS_BASE 0x080 -#define CAN_ID_SLAVE_LOG_BASE 0x600 +#define CAN_ID_MV_BMS 0x501 void ams_can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } @@ -70,43 +68,6 @@ void ams_can_send_status() { } } ftcan_marshal_unsigned(ptr, max_temp, 2); - uint16_t id = CAN_ID_SLAVE_STATUS_BASE | eeprom_config.id; + uint16_t id = CAN_ID_MV_BMS | eeprom_config.id; ftcan_transmit(id, data, sizeof(data)); } - -void ams_can_send_error() { - static uint8_t data[6]; - data[0] = eeprom_config.id; - data[1] = error_data.data_kind; - memcpy(&data[2], error_data.data, 4); - ftcan_transmit(CAN_ID_SLAVE_PANIC, data, sizeof(data)); -} - -void ams_can_send_log() { - static uint8_t call_count = 0; - static uint8_t data[8]; - - uint16_t can_addr = - CAN_ID_SLAVE_LOG_BASE | (eeprom_config.id << 4) | call_count; - uint8_t* ptr = &data[0]; - - if (call_count < N_CELLS / 4) { - for (size_t i = 0; i < 4; i++) { - size_t offset = call_count * 4; - ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[offset + i], 2); - } - ftcan_transmit(can_addr, data, sizeof(data)); - } else if (call_count == N_CELLS / 4) { - // Send last cell & failed temperature sensors - ptr = ftcan_marshal_unsigned(ptr, module.cellVoltages[N_CELLS - 1], 2); - ptr = ftcan_marshal_unsigned(ptr, tmp1075_failed_sensors, 4); - ftcan_transmit(can_addr, data, 6); - } else { - size_t offset = (call_count - N_CELLS / 4 - 1) * 8; - for (size_t i = 0; i < 8; i++) { - data[i] = tmp1075_temps[offset + i] >> 4; - } - ftcan_transmit(can_addr, data, sizeof(data)); - } - call_count = (call_count + 1) % (N_CELLS / 4 + 1 + N_TEMP_SENSORS / 8); -} diff --git a/Core/Src/main.c b/Core/Src/main.c index 523bd73..5891d50 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -134,11 +134,6 @@ int main(void) AMS_Loop(); ams_can_send_status(); - if (error_data.error_sources && - HAL_GetTick() - error_data.errors_since >= ERROR_TIME_THRESH) { - ams_can_send_error(); - } - ams_can_send_log(); } /* USER CODE END 3 */ } From fe3bcb6ebb897e81ab6661cda864eedd7463f3ba Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 23 May 2024 22:41:21 +0300 Subject: [PATCH 06/38] updated the state_machine and added it to main --- Core/Inc/state_machine.h | 20 ++--- Core/Src/state_machine.c | 172 ++++++++++++++++++++++++--------------- 2 files changed, 118 insertions(+), 74 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index 2240194..a0a5214 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -17,13 +17,13 @@ #define RELAY_CLOSE_WAIT 10 // ms typedef enum { // valid transitions: (all could transition to error) - INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR - PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR - READY, // READY -> ACTIVE, DISCHARGE, ERROR - ACTIVE, // ACTIVE -> READY, DISCHARGE, ERROR - DISCHARGE, // DISCHARGE -> INACTIVE, PRECHARGE, ERROR - CHARGING, // CHARGING -> INACTIVE, DISCHARGE, ERROR - ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR + STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR + STATE_PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR + STATE_READY, // READY -> ACTIVE, DISCHARGE, ERROR + STATE_ACTIVE, // ACTIVE -> READY, DISCHARGE, ERROR + STATE_DISCHARGE, // DISCHARGE -> INACTIVE, PRECHARGE, ERROR + STATE_CHARGING, // CHARGING -> INACTIVE, DISCHARGE, ERROR + STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR } State; typedef struct { @@ -60,10 +60,10 @@ State sm_update_discharge(); State sm_update_charging(); State sm_update_error(); -typedef enum { RELAY_POS, RELAY_PRECHARGE } Relay; +typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; void sm_set_relay_positions(State state); -void sm_set_relay_position(Relay relay, int closed); -void sm_check_close_wait(int *is_closed, int should_close); +void sm_set_relay(Relay relay, bool closed); +void sm_check_precharge_discharge(int *is_closed, int should_close); void sm_handle_ams_in(const uint8_t *data); diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 24a48dc..661f54c 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -1,39 +1,48 @@ #include "state_machine.h" +#include "ADBMS_LL_Driver.h" +#include "AMS_HighLevel.h" +#include "stm32f3xx_hal.h" +#include "ADBMS_Abstraction.h" +#include "main.h" StateHandle state; +static bool relay_closed = 0; +static bool precharge_closed = 0; +static int16_t RELAY_BAT_SIDE; +static int16_t RELAY_ESC_SIDE; +static int16_t CURRENT_MEASUREMENT; void sm_init(){ - state.current_state = INACTIVE; - state.target_state = INACTIVE; + state.current_state = STATE_INACTIVE; + state.target_state = STATE_INACTIVE; state.error_source = 0; + RELAY_BAT_SIDE = 0; + RELAY_ESC_SIDE = 0; + CURRENT_MEASUREMENT = 0; } void sm_update(){ - if (state.error_source) { - state.current_state = ERROR; - } - switch (state.current_state) { - case INACTIVE: + case STATE_INACTIVE: state.current_state = sm_update_inactive(); // moniter only break; - case PRECHARGE: + case STATE_PRECHARGE: state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such break; - case READY: + case STATE_READY: state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground break; - case ACTIVE: + case STATE_ACTIVE: state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such break; - case DISCHARGE: + case STATE_DISCHARGE: state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed break; - case CHARGING: - state.current_state = sm_update_charging(); // turn on cooling if needed, + case STATE_CHARGING: + state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. break; - case ERROR: + case STATE_ERROR: state.current_state = sm_update_error(); // enter the correct ERROR state break; } @@ -44,100 +53,135 @@ void sm_update(){ State sm_update_inactive(){ switch (state.target_state) { - case PRECHARGE: - break; - case CHARGING: - break; + case STATE_PRECHARGE: + //close precharge relay, wait until both sides are similar + return STATE_PRECHARGE; + case STATE_CHARGING: + return STATE_CHARGING; + default: + return STATE_INACTIVE; } - return INACTIVE; } State sm_update_precharge(){ switch (state.target_state) { - case INACTIVE: - break; - case READY: - break; - case DISCHARGE: - break; + case STATE_INACTIVE: + return STATE_INACTIVE; + case STATE_READY: + return STATE_READY; + case STATE_DISCHARGE: + return STATE_DISCHARGE; + default: + return STATE_PRECHARGE; } - return PRECHARGE; } State sm_update_ready(){ switch (state.target_state) { - case ACTIVE: - break; - case DISCHARGE: - break; + case STATE_ACTIVE: + return STATE_ACTIVE; + case STATE_DISCHARGE: + return STATE_DISCHARGE; + default: + return STATE_READY; } - return READY; } State sm_update_active(){ switch (state.target_state) { - case READY: - break; - case DISCHARGE: - break; + case STATE_READY: + return STATE_READY; + case STATE_DISCHARGE: + return STATE_DISCHARGE; + default: + return STATE_ACTIVE; } - return ACTIVE; } State sm_update_discharge(){ switch (state.target_state) { - case INACTIVE: - break; - case PRECHARGE: - break; + case STATE_INACTIVE: + return STATE_INACTIVE; + case STATE_PRECHARGE: + return STATE_PRECHARGE; + default: + return STATE_DISCHARGE; } - return DISCHARGE; } State sm_update_charging(){ switch (state.target_state) { - case INACTIVE: - break; - case DISCHARGE: - break; + case STATE_INACTIVE: + return STATE_INACTIVE; + case STATE_DISCHARGE: + return STATE_DISCHARGE; + default: + return STATE_CHARGING; } - return CHARGING; } State sm_update_error(){ switch (state.target_state) { - case INACTIVE: + case STATE_INACTIVE: + return STATE_INACTIVE; + case STATE_DISCHARGE: + return STATE_DISCHARGE; + default: + return STATE_ERROR; + } +} + +void sm_set_relay_positions(State current_state){ + switch (state.target_state) { + case STATE_INACTIVE: + sm_set_relay(RELAY_MAIN, 0); + sm_set_relay(RELAY_PRECHARGE, 0); break; - case DISCHARGE: + case STATE_PRECHARGE: + sm_set_relay(RELAY_MAIN, 0); + sm_set_relay(RELAY_PRECHARGE, 1); + break; + case STATE_READY: + sm_set_relay(RELAY_MAIN, 1); + sm_set_relay(RELAY_PRECHARGE, 0); + break; + case STATE_ACTIVE: + sm_set_relay(RELAY_MAIN, 1); + sm_set_relay(RELAY_PRECHARGE, 0); + case STATE_DISCHARGE: + sm_set_relay(RELAY_MAIN, 0); + sm_set_relay(RELAY_PRECHARGE, 1); + break; + case STATE_CHARGING: + sm_set_relay(RELAY_MAIN, 1); + sm_set_relay(RELAY_PRECHARGE, 1); + break; + case STATE_ERROR: + sm_set_relay(RELAY_MAIN, 0); break; } - return ERROR; } -void sm_set_relay_positions(State state){ - return; -} -/* -void sm_set_relay(Relay relay, int closed){ - static int closed = 0; - static int precharge_closed = 0; - +void sm_set_relay(Relay relay, bool closed){ GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET; switch (relay) { - case RELAY_POS: - ts_sm_check_close_wait(&closed, closed); + case RELAY_MAIN: HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state); + relay_closed = closed; break; case RELAY_PRECHARGE: - ts_sm_check_close_wait(&precharge_closed, closed); HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state); + precharge_closed = closed; break; } } -*/ -//void sm_check_close_wait(int *is_closed, int should_close); -//void sm_handle_ams_in(const uint8_t *data); +void sm_check_precharge_discharge(int *is_closed, int should_close){} + // compare RELAY_BATT_SIDE and RELAY_ESC_SIDE + // if (state.current_state == STATE_PRECHARGE && (RELAY_ESC_SIDE < RELAY_BAT_SIDE)) //-> don't switch from PRECHARGE to READY + // if (state.current_state == STATE_DISCHARGE && (RELAY_ESC_SIDE > 12V)) -> don't switch from DISCHARGE to INACTIVE -//void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file +void sm_handle_ams_in(const uint8_t *data){} + +void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file From 6f26e46e518a3d9aeb9ef2ac1a3f0980344c4b28 Mon Sep 17 00:00:00 2001 From: Hamza Date: Fri, 24 May 2024 15:20:23 +0200 Subject: [PATCH 07/38] added sm_get_state_code(), added more ErrorKinds --- Core/Inc/state_machine.h | 30 ++++++++++++++++++++---------- Core/Src/state_machine.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index a0a5214..ce0356f 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -16,7 +16,7 @@ // Time to wait between closing relays #define RELAY_CLOSE_WAIT 10 // ms -typedef enum { // valid transitions: (all could transition to error) +typedef enum { // 7 states -> 3 bit. valid transitions: (all could transition to error) STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR STATE_PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR STATE_READY, // READY -> ACTIVE, DISCHARGE, ERROR @@ -26,16 +26,23 @@ typedef enum { // valid transitions: (all could transition to error) STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR } State; -typedef struct { +typedef struct { // 13 errors -> 4 bit uint16_t bms_timeout : 1; - uint16_t overtemp : 1; - uint16_t overcurrent : 1; - uint16_t overvoltage : 1; - uint16_t missing_current_reading : 1; - uint16_t missing_voltage_reading : 1; - uint16_t missing_temp_reading : 1; - uint16_t precharge_fail : 1; - uint16_t powerground_fail : 1; + uint16_t bms_checksum_fail : 1; + uint16_t bms_overtemp : 1; + uint16_t bms_fault : 1; + + uint16_t temperature_error : 1; + uint16_t current_error : 1; + uint16_t voltage_error : 1; + + uint16_t temperature_sensor_missing : 1; + uint16_t current_sensor_missing : 1; + uint16_t voltage_missing : 1; + uint16_t relay_missing : 1; + + uint16_t state_fail : 1; + uint16_t state_transition_fail : 1; } ErrorKind; //typedef enum {} WarningKind; @@ -51,6 +58,7 @@ extern StateHandle state; void sm_init(); void sm_update(); +int sm_get_state_code(); State sm_update_inactive(); State sm_update_precharge(); @@ -65,6 +73,8 @@ void sm_set_relay_positions(State state); void sm_set_relay(Relay relay, bool closed); void sm_check_precharge_discharge(int *is_closed, int should_close); +void sm_check_errors(); + void sm_handle_ams_in(const uint8_t *data); void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 661f54c..86afba4 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -8,17 +8,14 @@ StateHandle state; static bool relay_closed = 0; static bool precharge_closed = 0; -static int16_t RELAY_BAT_SIDE; -static int16_t RELAY_ESC_SIDE; -static int16_t CURRENT_MEASUREMENT; +static int16_t RELAY_BAT_SIDE = 0; +static int16_t RELAY_ESC_SIDE = 0; +static int16_t CURRENT_MEASUREMENT = 0; void sm_init(){ state.current_state = STATE_INACTIVE; state.target_state = STATE_INACTIVE; state.error_source = 0; - RELAY_BAT_SIDE = 0; - RELAY_ESC_SIDE = 0; - CURRENT_MEASUREMENT = 0; } void sm_update(){ @@ -51,10 +48,30 @@ void sm_update(){ //status_led_state(state.current_state, (ErrorKind) state.error_type); } +int sm_get_state_code(){ + switch (state.current_state) { + case STATE_INACTIVE: + return 0; + case STATE_PRECHARGE: + return 1; + case STATE_READY: + return 2; + case STATE_ACTIVE: + return 3; + case STATE_DISCHARGE: + return 4; + case STATE_CHARGING: + return 5; + default: // either STATE_ERROR or something went severly wrong + return 6; + } +} + State sm_update_inactive(){ switch (state.target_state) { case STATE_PRECHARGE: //close precharge relay, wait until both sides are similar + sm_set_relay_positions(STATE_PRECHARGE); return STATE_PRECHARGE; case STATE_CHARGING: return STATE_CHARGING; @@ -184,4 +201,6 @@ void sm_check_precharge_discharge(int *is_closed, int should_close){} void sm_handle_ams_in(const uint8_t *data){} -void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file +void sm_set_error(ErrorKind error_kind, bool is_errored); + +void sm_check_errors(){} \ No newline at end of file From 30b3aa4dd13f5c80e05cac7af89101ca6211d3a8 Mon Sep 17 00:00:00 2001 From: Hamza Date: Fri, 24 May 2024 15:21:29 +0200 Subject: [PATCH 08/38] changed AMS_CAN.h -> can.h --- Core/Inc/AMS_CAN.h | 29 ---------------- Core/Inc/AMS_HighLevel.h | 2 +- Core/Inc/TMP1075.h | 2 +- Core/Inc/can.h | 16 +++++++++ Core/Src/AMS_CAN.c | 73 ---------------------------------------- Core/Src/AMS_HighLevel.c | 2 +- Core/Src/can.c | 27 +++++++++++++++ Core/Src/main.c | 6 ++-- 8 files changed, 49 insertions(+), 108 deletions(-) delete mode 100644 Core/Inc/AMS_CAN.h create mode 100644 Core/Inc/can.h delete mode 100644 Core/Src/AMS_CAN.c create mode 100644 Core/Src/can.c diff --git a/Core/Inc/AMS_CAN.h b/Core/Inc/AMS_CAN.h deleted file mode 100644 index d814d38..0000000 --- a/Core/Inc/AMS_CAN.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * AMS_CAN.h - * - * Created on: Mar 19, 2022 - * Author: jasper - */ - -#ifndef INC_AMS_CAN_H_ -#define INC_AMS_CAN_H_ - -#include "main.h" - -#include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_can.h" -#include "stm32f3xx_hal_def.h" - -#include - -void ams_can_init(CAN_HandleTypeDef* hcan); - -void ams_can_handle_ams_msg(CAN_RxHeaderTypeDef* header, uint8_t* data); - -void ams_can_send_status(); - -HAL_StatusTypeDef ams_can_wait_for_free_mailboxes(CAN_HandleTypeDef* handle, - int num_mailboxes, - uint32_t timeout); - -#endif /* INC_AMS_CAN_H_ */ diff --git a/Core/Inc/AMS_HighLevel.h b/Core/Inc/AMS_HighLevel.h index 78a8609..870007a 100644 --- a/Core/Inc/AMS_HighLevel.h +++ b/Core/Inc/AMS_HighLevel.h @@ -11,7 +11,7 @@ #include "ADBMS_Abstraction.h" #include "ADBMS_CMD_MAKROS.h" #include "ADBMS_LL_Driver.h" -#include "AMS_CAN.h" +#include "can.h" typedef enum { AMSDEACTIVE, diff --git a/Core/Inc/TMP1075.h b/Core/Inc/TMP1075.h index 9c7dd5c..1e9e680 100644 --- a/Core/Inc/TMP1075.h +++ b/Core/Inc/TMP1075.h @@ -1,7 +1,7 @@ #ifndef INC_TMP1075_H_ #define INC_TMP1075_H_ -#include "AMS_CAN.h" +#include "can.h" #include "common_defs.h" #include "stm32f3xx_hal.h" #include "stm32f3xx_hal_def.h" diff --git a/Core/Inc/can.h b/Core/Inc/can.h new file mode 100644 index 0000000..4b2b471 --- /dev/null +++ b/Core/Inc/can.h @@ -0,0 +1,16 @@ + +#include "stm32f3xx_hal.h" +#include "stm32f3xx_hal_can.h" +#include "stm32f3xx_hal_def.h" +#include "state_machine.h" +#include + +#define CAN_ID_IN 0x501 +#define CAN_ID_OUT 0x502 + +void can_init(CAN_HandleTypeDef* hcan); + +void can_handle_send_status(); + +void can_handle_recieve_command(CAN_RxHeaderTypeDef* header, uint8_t* data); + diff --git a/Core/Src/AMS_CAN.c b/Core/Src/AMS_CAN.c deleted file mode 100644 index 4ff7b40..0000000 --- a/Core/Src/AMS_CAN.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * AMS_CAN.c - * - * Created on: Mar 19, 2022 - * Author: jasper - */ - -#include "AMS_CAN.h" - -#include "ADBMS_Abstraction.h" - -#include "AMS_HighLevel.h" -#include "TMP1075.h" -#include "common_defs.h" -#include "eeprom.h" -#include "errors.h" -#include "main.h" -#include "stm32f3xx.h" -#include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_can.h" - -#include "can-halal.h" -#include "stm32f3xx_hal_gpio.h" - -#include -#include - -#define CAN_ID_MV_BMS 0x501 - -void ams_can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } - -#define ITER_COUNT 10 -static uint8_t count = 0; -static bool isOn = false; - -void ams_can_send_status() { - if (count == ITER_COUNT) { - HAL_GPIO_WritePin(STATUS_LED_G_GPIO_Port, STATUS_LED_G_Pin, isOn ? GPIO_PIN_SET : GPIO_PIN_RESET); - - count = 0; - isOn = !isOn; - } else { - count++; - } - - static uint8_t data[8]; - int error = error_data.error_sources != 0; - data[0] = eeprom_config.id | (error << 7); - data[1] = stateofcharge; - uint8_t* ptr = &data[2]; - uint16_t min_volt = 0xFFFF; - uint16_t max_volt = 0; - for (size_t i = 0; i < numberofCells; i++) { - if (module.cellVoltages[i] < min_volt) { - min_volt = module.cellVoltages[i]; - } - if (module.cellVoltages[i] > max_volt) { - max_volt = module.cellVoltages[i]; - } - } - ptr = ftcan_marshal_unsigned(ptr, min_volt, 2); - ptr = ftcan_marshal_unsigned(ptr, max_volt, 2); - int16_t max_temp = -0x8000; - for (size_t i = 0; i < N_TEMP_SENSORS; i++) { - if (tmp1075_temps[i] > max_temp && - (tmp1075_failed_sensors & (1 << i)) == 0) { - max_temp = tmp1075_temps[i]; - } - } - ftcan_marshal_unsigned(ptr, max_temp, 2); - uint16_t id = CAN_ID_MV_BMS | eeprom_config.id; - ftcan_transmit(id, data, sizeof(data)); -} diff --git a/Core/Src/AMS_HighLevel.c b/Core/Src/AMS_HighLevel.c index 8008693..2eeb096 100644 --- a/Core/Src/AMS_HighLevel.c +++ b/Core/Src/AMS_HighLevel.c @@ -8,7 +8,7 @@ #include "AMS_HighLevel.h" #include "ADBMS_Abstraction.h" #include "ADBMS_LL_Driver.h" -#include "AMS_CAN.h" +#include "can.h" #include "TMP1075.h" #include "can-halal.h" #include "errors.h" diff --git a/Core/Src/can.c b/Core/Src/can.c new file mode 100644 index 0000000..0aaf81b --- /dev/null +++ b/Core/Src/can.c @@ -0,0 +1,27 @@ +/* + * can.c + * Created on: Mai 23, 2024 + * Author: Hamza + */ + +#include "can.h" + +#include "AMS_HighLevel.h" +#include "TMP1075.h" +#include "can-halal.h" +#include + +void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } + +/* +This function sends the status of the mvbms, the battery and of powerground. +once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR. +once every 0.5s in states: READY, ACTIVE. +with format of: + +*/ +void can_handle_send_status() { + static uint8_t data[8]; + data[0] = state. (sm_get_state_code() << 5); //save 5 bit since codes are from 0-6 + //ftcan_transmit(id, data, sizeof(data)); +} diff --git a/Core/Src/main.c b/Core/Src/main.c index 5891d50..9a5a862 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -23,7 +23,7 @@ /* USER CODE BEGIN Includes */ #include "ADBMS_Abstraction.h" #include "ADBMS_CMD_MAKROS.h" -#include "AMS_CAN.h" +#include "can.h" #include "AMS_HighLevel.h" #include "TMP1075.h" #include "eeprom.h" @@ -119,7 +119,7 @@ int main(void) /* USER CODE BEGIN 2 */ tmp1075_init(&hi2c1); AMS_Init(&hspi1); - ams_can_init(&hcan); + can_init(&hcan); uint32_t target_time = HAL_GetTick() + 10000; /* USER CODE END 2 */ @@ -133,7 +133,7 @@ int main(void) /* USER CODE BEGIN 3 */ AMS_Loop(); - ams_can_send_status(); + can_handle_send_status(); } /* USER CODE END 3 */ } From 572749b2023873bd52c273c3f48818f1aece6240 Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 24 May 2024 18:49:37 +0300 Subject: [PATCH 09/38] updated the state_machine --- Core/Src/main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 9a5a862..e574010 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -25,6 +25,7 @@ #include "ADBMS_CMD_MAKROS.h" #include "can.h" #include "AMS_HighLevel.h" +#include "state_machine.h" #include "TMP1075.h" #include "eeprom.h" #include "errors.h" @@ -117,9 +118,10 @@ int main(void) MX_USART1_UART_Init(); MX_TIM1_Init(); /* USER CODE BEGIN 2 */ + sm_init(); tmp1075_init(&hi2c1); AMS_Init(&hspi1); - can_init(&hcan); + ams_can_init(&hcan); uint32_t target_time = HAL_GetTick() + 10000; /* USER CODE END 2 */ @@ -133,7 +135,7 @@ int main(void) /* USER CODE BEGIN 3 */ AMS_Loop(); - can_handle_send_status(); + ams_can_send_status(); } /* USER CODE END 3 */ } From c8047a9017a6512e341638614dc6960c856abcd4 Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 24 May 2024 18:56:49 +0300 Subject: [PATCH 10/38] fixed a build issue --- Core/Inc/can.h | 3 --- Core/Src/can.c | 5 ++--- Core/Src/main.c | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Core/Inc/can.h b/Core/Inc/can.h index 4b2b471..aecdb06 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -1,8 +1,5 @@ #include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_can.h" -#include "stm32f3xx_hal_def.h" -#include "state_machine.h" #include #define CAN_ID_IN 0x501 diff --git a/Core/Src/can.c b/Core/Src/can.c index 0aaf81b..a8e239a 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -7,9 +7,8 @@ #include "can.h" #include "AMS_HighLevel.h" -#include "TMP1075.h" +//#include "TMP1075.h" #include "can-halal.h" -#include void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } @@ -22,6 +21,6 @@ with format of: */ void can_handle_send_status() { static uint8_t data[8]; - data[0] = state. (sm_get_state_code() << 5); //save 5 bit since codes are from 0-6 + //data[0] = state. (sm_get_state_code() << 5); //save 5 bit since codes are from 0-6 //ftcan_transmit(id, data, sizeof(data)); } diff --git a/Core/Src/main.c b/Core/Src/main.c index e574010..024678b 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -121,7 +121,7 @@ int main(void) sm_init(); tmp1075_init(&hi2c1); AMS_Init(&hspi1); - ams_can_init(&hcan); + can_init(&hcan); uint32_t target_time = HAL_GetTick() + 10000; /* USER CODE END 2 */ @@ -135,7 +135,7 @@ int main(void) /* USER CODE BEGIN 3 */ AMS_Loop(); - ams_can_send_status(); + can_handle_send_status(); } /* USER CODE END 3 */ } From 61e455dfcdb7f0f289af4d3269745d1c05b6772b Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 24 May 2024 19:03:52 +0300 Subject: [PATCH 11/38] removed eeprom.h .c --- Core/Inc/eeprom.h | 17 --------------- Core/Src/eeprom.c | 55 ----------------------------------------------- Core/Src/main.c | 1 - 3 files changed, 73 deletions(-) delete mode 100644 Core/Inc/eeprom.h delete mode 100644 Core/Src/eeprom.c diff --git a/Core/Inc/eeprom.h b/Core/Inc/eeprom.h deleted file mode 100644 index 3d21c7a..0000000 --- a/Core/Inc/eeprom.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef INC_EEPROM_H_ -#define INC_EEPROM_H_ - -#include "stm32f3xx_hal.h" - -#include - -__attribute__((packed)) typedef struct { - uint8_t id; -} EEPROMConfig; - -extern EEPROMConfig eeprom_config; - -void eeprom_init(I2C_HandleTypeDef* hi2c); -void eeprom_config_save(); - -#endif // INC_EEPROM_H_ diff --git a/Core/Src/eeprom.c b/Core/Src/eeprom.c deleted file mode 100644 index e119142..0000000 --- a/Core/Src/eeprom.c +++ /dev/null @@ -1,55 +0,0 @@ -#include "eeprom.h" -#include "errors.h" -#include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_exti.h" -#include "stm32f3xx_hal_i2c.h" - -#include - -#define EEPROM_I2C_ADDR 0xA0 -// Don't use the beginning of the EEPROM, since the testbench writes there -#define EEPROM_CONFIG_BASE 0x0100 -#define EEPROM_CONFIG_ECC_OFFSET 0x20 - -EEPROMConfig eeprom_config; - -static I2C_HandleTypeDef* hi2c; - -void eeprom_init(I2C_HandleTypeDef* handle) { - hi2c = handle; - uint8_t buf[sizeof(EEPROMConfig) * 3]; - // Read 3 EEPROM config buffers at 32 byte offsets - for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) { - HAL_I2C_Mem_Read(hi2c, EEPROM_I2C_ADDR, - EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2, - buf + sizeof(EEPROMConfig) * ecc_i, sizeof(EEPROMConfig), - 100); - } - // ECC - for (size_t i = 0; i < sizeof(EEPROMConfig); i++) { - uint8_t a = buf[i + sizeof(EEPROMConfig) * 0]; - uint8_t b = buf[i + sizeof(EEPROMConfig) * 1]; - uint8_t c = buf[i + sizeof(EEPROMConfig) * 2]; - if (a == b || a == c) { - buf[i] = a; - } else if (b == c) { - buf[i] = b; - } else { - set_error_source(ERROR_SOURCE_EEPROM); - } - } - - memcpy(&eeprom_config, buf, sizeof(EEPROMConfig)); - // Write back config - eeprom_config.id = 10; - eeprom_config_save(); -} - -void eeprom_config_save() { - for (size_t ecc_i = 0; ecc_i < 3; ecc_i++) { - HAL_I2C_Mem_Write(hi2c, EEPROM_I2C_ADDR, - EEPROM_CONFIG_BASE + EEPROM_CONFIG_ECC_OFFSET * ecc_i, 2, - (uint8_t*)&eeprom_config, sizeof(eeprom_config), 100); - HAL_Delay(100); - } -} diff --git a/Core/Src/main.c b/Core/Src/main.c index 024678b..09ea26f 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -27,7 +27,6 @@ #include "AMS_HighLevel.h" #include "state_machine.h" #include "TMP1075.h" -#include "eeprom.h" #include "errors.h" #include "stm32f3xx_hal.h" #include "stm32f3xx_hal_gpio.h" From b87a0ca55c40aa7f4878b062ab13f28482d886c7 Mon Sep 17 00:00:00 2001 From: kbracher Date: Fri, 24 May 2024 19:06:34 +0200 Subject: [PATCH 12/38] clean up --- Core/Inc/state_machine.h | 5 +-- Core/Src/can.c | 7 ++-- Core/Src/state_machine.c | 79 +++++++++++++++------------------------- 3 files changed, 36 insertions(+), 55 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index ce0356f..e1326f2 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -26,7 +26,7 @@ typedef enum { // 7 states -> 3 bit. valid transitions: (all could transition STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR } State; -typedef struct { // 13 errors -> 4 bit +typedef struct { uint16_t bms_timeout : 1; uint16_t bms_checksum_fail : 1; uint16_t bms_overtemp : 1; @@ -58,7 +58,6 @@ extern StateHandle state; void sm_init(); void sm_update(); -int sm_get_state_code(); State sm_update_inactive(); State sm_update_precharge(); @@ -71,7 +70,7 @@ State sm_update_error(); typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; void sm_set_relay_positions(State state); void sm_set_relay(Relay relay, bool closed); -void sm_check_precharge_discharge(int *is_closed, int should_close); +void sm_check_precharge_discharge(bool *is_closed, bool should_close); void sm_check_errors(); diff --git a/Core/Src/can.c b/Core/Src/can.c index a8e239a..963c179 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -9,6 +9,7 @@ #include "AMS_HighLevel.h" //#include "TMP1075.h" #include "can-halal.h" +#include "state_machine.h" void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } @@ -20,7 +21,7 @@ with format of: */ void can_handle_send_status() { - static uint8_t data[8]; - //data[0] = state. (sm_get_state_code() << 5); //save 5 bit since codes are from 0-6 - //ftcan_transmit(id, data, sizeof(data)); + static uint8_t data[8] = {}; + data[0] = (state.current_state << 5); //save 5 bit since codes are from 0-6 + ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 86afba4..b79f3e0 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -21,52 +21,33 @@ void sm_init(){ void sm_update(){ switch (state.current_state) { - case STATE_INACTIVE: - state.current_state = sm_update_inactive(); // moniter only - break; - case STATE_PRECHARGE: - state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such - break; - case STATE_READY: - state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground - break; - case STATE_ACTIVE: - state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such - break; - case STATE_DISCHARGE: - state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed - break; - case STATE_CHARGING: - state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. - break; - case STATE_ERROR: - state.current_state = sm_update_error(); // enter the correct ERROR state - break; + case STATE_INACTIVE: + state.current_state = sm_update_inactive(); // monitor only + break; + case STATE_PRECHARGE: + state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such + break; + case STATE_READY: + state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground + break; + case STATE_ACTIVE: + state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such + break; + case STATE_DISCHARGE: + state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed + break; + case STATE_CHARGING: + state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. + break; + case STATE_ERROR: + state.current_state = sm_update_error(); // enter the correct ERROR state + break; } sm_set_relay_positions(state.current_state); //status_led_state(state.current_state, (ErrorKind) state.error_type); } -int sm_get_state_code(){ - switch (state.current_state) { - case STATE_INACTIVE: - return 0; - case STATE_PRECHARGE: - return 1; - case STATE_READY: - return 2; - case STATE_ACTIVE: - return 3; - case STATE_DISCHARGE: - return 4; - case STATE_CHARGING: - return 5; - default: // either STATE_ERROR or something went severly wrong - return 6; - } -} - State sm_update_inactive(){ switch (state.target_state) { case STATE_PRECHARGE: @@ -182,19 +163,19 @@ void sm_set_relay_positions(State current_state){ void sm_set_relay(Relay relay, bool closed){ GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET; switch (relay) { - case RELAY_MAIN: - HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state); - relay_closed = closed; - break; - case RELAY_PRECHARGE: - HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state); - precharge_closed = closed; - break; + case RELAY_MAIN: + HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state); + relay_closed = closed; + break; + case RELAY_PRECHARGE: + HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state); + precharge_closed = closed; + break; } } -void sm_check_precharge_discharge(int *is_closed, int should_close){} +void sm_check_precharge_discharge(bool *is_closed, bool should_close){} // compare RELAY_BATT_SIDE and RELAY_ESC_SIDE // if (state.current_state == STATE_PRECHARGE && (RELAY_ESC_SIDE < RELAY_BAT_SIDE)) //-> don't switch from PRECHARGE to READY // if (state.current_state == STATE_DISCHARGE && (RELAY_ESC_SIDE > 12V)) -> don't switch from DISCHARGE to INACTIVE From 77f9b364a09619410fa0270c183ed619ba2c96a6 Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 24 May 2024 20:12:42 +0300 Subject: [PATCH 13/38] changed the ioc file --- .mxproject | 22 ++++---- Core/Inc/main.h | 6 +-- Core/Inc/stm32f3xx_hal_conf.h | 2 +- Core/Src/main.c | 97 ++++++++++++++++++++++++----------- Core/Src/stm32f3xx_hal_msp.c | 54 ++++++++++++++++--- Makefile | 27 ++-------- mvbms.ioc | 22 ++++---- 7 files changed, 146 insertions(+), 84 deletions(-) diff --git a/.mxproject b/.mxproject index 0d4d2e0..ce6f6d0 100644 --- a/.mxproject +++ b/.mxproject @@ -1,25 +1,25 @@ [PreviousLibFiles] -LibFiles=Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_can.h;Drivers\STM32F3xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_def.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_rcc.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_rcc_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_bus.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_rcc.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_crs.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_system.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_utils.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_gpio.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_gpio_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_gpio.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_dma_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_dma.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_dma.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_cortex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_cortex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_pwr.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_pwr_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_pwr.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_flash.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_flash_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_i2c.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_i2c_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_exti.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_exti.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_i2c.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_spi.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_spi.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_spi_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_tim.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_tim_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_tim.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_uart.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_usart.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_uart_ex.h;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_can.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_rcc.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_rcc_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_gpio.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_dma.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_cortex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_pwr.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_pwr_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_flash.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_flash_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_i2c.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_i2c_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_exti.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_spi.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_spi_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_tim.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_tim_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_uart.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_uart_ex.c;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_can.h;Drivers\STM32F3xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_def.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_rcc.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_rcc_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_bus.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_rcc.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_crs.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_system.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_utils.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_gpio.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_gpio_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_gpio.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_dma_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_dma.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_dma.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_cortex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_cortex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_pwr.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_pwr_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_pwr.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_flash.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_flash_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_i2c.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_i2c_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_exti.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_exti.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_i2c.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_spi.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_spi.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_spi_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_tim.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_tim_ex.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_tim.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_uart.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_ll_usart.h;Drivers\STM32F3xx_HAL_Driver\Inc\stm32f3xx_hal_uart_ex.h;Drivers\CMSIS\Device\ST\STM32F3xx\Include\stm32f302xc.h;Drivers\CMSIS\Device\ST\STM32F3xx\Include\stm32f3xx.h;Drivers\CMSIS\Device\ST\STM32F3xx\Include\system_stm32f3xx.h;Drivers\CMSIS\Device\ST\STM32F3xx\Source\Templates\system_stm32f3xx.c;Drivers\CMSIS\Include\cmsis_armcc.h;Drivers\CMSIS\Include\cmsis_armclang.h;Drivers\CMSIS\Include\cmsis_compiler.h;Drivers\CMSIS\Include\cmsis_gcc.h;Drivers\CMSIS\Include\cmsis_iccarm.h;Drivers\CMSIS\Include\cmsis_version.h;Drivers\CMSIS\Include\core_armv8mbl.h;Drivers\CMSIS\Include\core_armv8mml.h;Drivers\CMSIS\Include\core_cm0.h;Drivers\CMSIS\Include\core_cm0plus.h;Drivers\CMSIS\Include\core_cm1.h;Drivers\CMSIS\Include\core_cm23.h;Drivers\CMSIS\Include\core_cm3.h;Drivers\CMSIS\Include\core_cm33.h;Drivers\CMSIS\Include\core_cm4.h;Drivers\CMSIS\Include\core_cm7.h;Drivers\CMSIS\Include\core_sc000.h;Drivers\CMSIS\Include\core_sc300.h;Drivers\CMSIS\Include\mpu_armv7.h;Drivers\CMSIS\Include\mpu_armv8.h;Drivers\CMSIS\Include\tz_context.h; +LibFiles=Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_can.h;Drivers/STM32F3xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_def.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_bus.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_crs.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_system.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_utils.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_usart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart_ex.h;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_can.h;Drivers/STM32F3xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_def.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_bus.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_crs.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_system.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_utils.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_usart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart_ex.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/stm32f302xc.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/stm32f3xx.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/system_stm32f3xx.h;Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/cmsis_armcc.h; [PreviousUsedMakefileFiles] -SourceFiles=Core\Src\main.c;Core\Src\stm32f3xx_it.c;Core\Src\stm32f3xx_hal_msp.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_can.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_rcc.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_rcc_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_gpio.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_dma.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_cortex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_pwr.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_pwr_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_flash.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_flash_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_i2c.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_i2c_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_exti.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_spi.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_spi_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_tim.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_tim_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_uart.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_uart_ex.c;Drivers\CMSIS\Device\ST\STM32F3xx\Source\Templates\system_stm32f3xx.c;Core\Src\system_stm32f3xx.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_can.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_rcc.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_rcc_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_gpio.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_dma.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_cortex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_pwr.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_pwr_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_flash.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_flash_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_i2c.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_i2c_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_exti.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_spi.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_spi_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_tim.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_tim_ex.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_uart.c;Drivers\STM32F3xx_HAL_Driver\Src\stm32f3xx_hal_uart_ex.c;Drivers\CMSIS\Device\ST\STM32F3xx\Source\Templates\system_stm32f3xx.c;Core\Src\system_stm32f3xx.c;;; -HeaderPath=Drivers\STM32F3xx_HAL_Driver\Inc;Drivers\STM32F3xx_HAL_Driver\Inc\Legacy;Drivers\CMSIS\Device\ST\STM32F3xx\Include;Drivers\CMSIS\Include;Core\Inc; +SourceFiles=Core/Src/main.c;Core/Src/stm32f3xx_it.c;Core/Src/stm32f3xx_hal_msp.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c;Core/Src/system_stm32f3xx.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c;Core/Src/system_stm32f3xx.c;;; +HeaderPath=Drivers/STM32F3xx_HAL_Driver/Inc;Drivers/STM32F3xx_HAL_Driver/Inc/Legacy;Drivers/CMSIS/Device/ST/STM32F3xx/Include;Drivers/CMSIS/Include;Core/Inc; CDefines=USE_HAL_DRIVER;STM32F302xC;USE_HAL_DRIVER;USE_HAL_DRIVER; [PreviousGenFiles] AdvancedFolderStructure=true HeaderFileListSize=3 -HeaderFiles#0=..\Core\Inc\stm32f3xx_it.h -HeaderFiles#1=..\Core\Inc\stm32f3xx_hal_conf.h -HeaderFiles#2=..\Core\Inc\main.h +HeaderFiles#0=../Core/Inc/stm32f3xx_it.h +HeaderFiles#1=../Core/Inc/stm32f3xx_hal_conf.h +HeaderFiles#2=../Core/Inc/main.h HeaderFolderListSize=1 -HeaderPath#0=..\Core\Inc +HeaderPath#0=../Core/Inc HeaderFiles=; SourceFileListSize=3 -SourceFiles#0=..\Core\Src\stm32f3xx_it.c -SourceFiles#1=..\Core\Src\stm32f3xx_hal_msp.c -SourceFiles#2=..\Core\Src\main.c +SourceFiles#0=../Core/Src/stm32f3xx_it.c +SourceFiles#1=../Core/Src/stm32f3xx_hal_msp.c +SourceFiles#2=../Core/Src/main.c SourceFolderListSize=1 -SourcePath#0=..\Core\Src +SourcePath#0=../Core/Src SourceFiles=; diff --git a/Core/Inc/main.h b/Core/Inc/main.h index d02a49d..7dea7f3 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -77,10 +77,8 @@ void Error_Handler(void); #define STATUS_LED_G_GPIO_Port GPIOB #define PRECHARGE_EN_Pin GPIO_PIN_11 #define PRECHARGE_EN_GPIO_Port GPIOB -#define AUX_IN_Pin GPIO_PIN_13 -#define AUX_IN_GPIO_Port GPIOB -#define AUX_OUT_Pin GPIO_PIN_14 -#define AUX_OUT_GPIO_Port GPIOB +#define PWM_Battery_Cooling_Pin GPIO_PIN_15 +#define PWM_Battery_Cooling_GPIO_Port GPIOB #define RELAY_BATT_SIDE_ON_Pin GPIO_PIN_8 #define RELAY_BATT_SIDE_ON_GPIO_Port GPIOA #define RELAY_ESC_SIDE_ON_Pin GPIO_PIN_9 diff --git a/Core/Inc/stm32f3xx_hal_conf.h b/Core/Inc/stm32f3xx_hal_conf.h index 23d581a..f70f764 100644 --- a/Core/Inc/stm32f3xx_hal_conf.h +++ b/Core/Inc/stm32f3xx_hal_conf.h @@ -81,7 +81,7 @@ * (when HSE is used as system clock source, directly or through the PLL). */ #if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ + #define HSE_VALUE ((uint32_t)16000000) /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */ /** diff --git a/Core/Src/main.c b/Core/Src/main.c index 09ea26f..669c751 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -55,6 +55,7 @@ I2C_HandleTypeDef hi2c1; SPI_HandleTypeDef hspi1; TIM_HandleTypeDef htim1; +TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim15; UART_HandleTypeDef huart1; @@ -72,6 +73,7 @@ static void MX_SPI1_Init(void); static void MX_TIM15_Init(void); static void MX_USART1_UART_Init(void); static void MX_TIM1_Init(void); +static void MX_TIM2_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ @@ -116,6 +118,7 @@ int main(void) MX_TIM15_Init(); MX_USART1_UART_Init(); MX_TIM1_Init(); + MX_TIM2_Init(); /* USER CODE BEGIN 2 */ sm_init(); tmp1075_init(&hi2c1); @@ -152,12 +155,11 @@ void SystemClock_Config(void) /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); @@ -167,7 +169,7 @@ void SystemClock_Config(void) */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; @@ -203,15 +205,15 @@ static void MX_CAN_Init(void) /* USER CODE END CAN_Init 1 */ hcan.Instance = CAN; - hcan.Init.Prescaler = 16; + hcan.Init.Prescaler = 2; hcan.Init.Mode = CAN_MODE_NORMAL; hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; - hcan.Init.TimeSeg1 = CAN_BS1_1TQ; - hcan.Init.TimeSeg2 = CAN_BS2_1TQ; + hcan.Init.TimeSeg1 = CAN_BS1_13TQ; + hcan.Init.TimeSeg2 = CAN_BS2_2TQ; hcan.Init.TimeTriggeredMode = DISABLE; - hcan.Init.AutoBusOff = DISABLE; + hcan.Init.AutoBusOff = ENABLE; hcan.Init.AutoWakeUp = DISABLE; - hcan.Init.AutoRetransmission = DISABLE; + hcan.Init.AutoRetransmission = ENABLE; hcan.Init.ReceiveFifoLocked = DISABLE; hcan.Init.TransmitFifoPriority = DISABLE; if (HAL_CAN_Init(&hcan) != HAL_OK) @@ -382,6 +384,55 @@ static void MX_TIM1_Init(void) } +/** + * @brief TIM2 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM2_Init(void) +{ + + /* USER CODE BEGIN TIM2_Init 0 */ + + /* USER CODE END TIM2_Init 0 */ + + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + + /* USER CODE BEGIN TIM2_Init 1 */ + + /* USER CODE END TIM2_Init 1 */ + htim2.Instance = TIM2; + htim2.Init.Prescaler = 0; + htim2.Init.CounterMode = TIM_COUNTERMODE_UP; + htim2.Init.Period = 4294967295; + htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM2_Init 2 */ + + /* USER CODE END TIM2_Init 2 */ + HAL_TIM_MspPostInit(&htim2); + +} + /** * @brief TIM15 Initialization Function * @param None @@ -429,10 +480,6 @@ static void MX_TIM15_Init(void) { Error_Handler(); } - if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) - { - Error_Handler(); - } sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; @@ -511,7 +558,7 @@ static void MX_GPIO_Init(void) HAL_GPIO_WritePin(GPIOB, STATUS_LED_R_Pin|STATUS_LED_B_Pin|STATUS_LED_G_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOB, PRECHARGE_EN_Pin|AUX_IN_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, GPIO_PIN_RESET); /*Configure GPIO pins : PC13 PC14 PC15 */ GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15; @@ -526,29 +573,21 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /*Configure GPIO pins : STATUS_LED_R_Pin STATUS_LED_B_Pin STATUS_LED_G_Pin PRECHARGE_EN_Pin - AUX_IN_Pin */ - GPIO_InitStruct.Pin = STATUS_LED_R_Pin|STATUS_LED_B_Pin|STATUS_LED_G_Pin|PRECHARGE_EN_Pin - |AUX_IN_Pin; + /*Configure GPIO pins : STATUS_LED_R_Pin STATUS_LED_B_Pin STATUS_LED_G_Pin PRECHARGE_EN_Pin */ + GPIO_InitStruct.Pin = STATUS_LED_R_Pin|STATUS_LED_B_Pin|STATUS_LED_G_Pin|PRECHARGE_EN_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /*Configure GPIO pins : PB10 PB12 PB4 PB5 - PB8 */ - GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_4|GPIO_PIN_5 - |GPIO_PIN_8; + /*Configure GPIO pins : PB10 PB12 PB13 PB14 + PB4 PB5 PB8 */ + GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14 + |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /*Configure GPIO pin : AUX_OUT_Pin */ - GPIO_InitStruct.Pin = AUX_OUT_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(AUX_OUT_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pins : RELAY_BATT_SIDE_ON_Pin RELAY_ESC_SIDE_ON_Pin CURRENT_SENSOR_ON_Pin */ GPIO_InitStruct.Pin = RELAY_BATT_SIDE_ON_Pin|RELAY_ESC_SIDE_ON_Pin|CURRENT_SENSOR_ON_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; diff --git a/Core/Src/stm32f3xx_hal_msp.c b/Core/Src/stm32f3xx_hal_msp.c index f407261..fe17f67 100644 --- a/Core/Src/stm32f3xx_hal_msp.c +++ b/Core/Src/stm32f3xx_hal_msp.c @@ -59,7 +59,7 @@ /* USER CODE END 0 */ void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); - /** + /** * Initializes the Global MSP. */ void HAL_MspInit(void) @@ -305,6 +305,17 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm) /* USER CODE END TIM1_MspInit 1 */ } + else if(htim_pwm->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspInit 0 */ + + /* USER CODE END TIM2_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_TIM2_CLK_ENABLE(); + /* USER CODE BEGIN TIM2_MspInit 1 */ + + /* USER CODE END TIM2_MspInit 1 */ + } else if(htim_pwm->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspInit 0 */ @@ -331,17 +342,38 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) /**TIM1 GPIO Configuration PB15 ------> TIM1_CH3N */ - GPIO_InitStruct.Pin = GPIO_PIN_15; + GPIO_InitStruct.Pin = PWM_Battery_Cooling_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF4_TIM1; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + HAL_GPIO_Init(PWM_Battery_Cooling_GPIO_Port, &GPIO_InitStruct); /* USER CODE BEGIN TIM1_MspPostInit 1 */ /* USER CODE END TIM1_MspPostInit 1 */ } + else if(htim->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspPostInit 0 */ + + /* USER CODE END TIM2_MspPostInit 0 */ + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**TIM2 GPIO Configuration + PA3 ------> TIM2_CH4 + */ + GPIO_InitStruct.Pin = PWM_PG_FAN2_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; + HAL_GPIO_Init(PWM_PG_FAN2_GPIO_Port, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM2_MspPostInit 1 */ + + /* USER CODE END TIM2_MspPostInit 1 */ + } else if(htim->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspPostInit 0 */ @@ -351,14 +383,13 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) __HAL_RCC_GPIOA_CLK_ENABLE(); /**TIM15 GPIO Configuration PA2 ------> TIM15_CH1 - PA3 ------> TIM15_CH2 */ - GPIO_InitStruct.Pin = PWM_PG_FAN1_Pin|PWM_PG_FAN2_Pin; + GPIO_InitStruct.Pin = PWM_PG_FAN1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF9_TIM15; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + HAL_GPIO_Init(PWM_PG_FAN1_GPIO_Port, &GPIO_InitStruct); /* USER CODE BEGIN TIM15_MspPostInit 1 */ @@ -385,6 +416,17 @@ void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm) /* USER CODE END TIM1_MspDeInit 1 */ } + else if(htim_pwm->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspDeInit 0 */ + + /* USER CODE END TIM2_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM2_CLK_DISABLE(); + /* USER CODE BEGIN TIM2_MspDeInit 1 */ + + /* USER CODE END TIM2_MspDeInit 1 */ + } else if(htim_pwm->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspDeInit 0 */ diff --git a/Makefile b/Makefile index 8f815f8..a020bde 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri May 17 19:29:11 CEST 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri May 24 17:06:04 GMT 2024] ########################################################################################################################## # ------------------------------------------------ @@ -13,7 +13,7 @@ ###################################### # target ###################################### -TARGET = mvbms-test-24 +TARGET = mvbms ###################################### @@ -40,28 +40,7 @@ Core/Src/main.c \ Core/Src/stm32f3xx_it.c \ Core/Src/stm32f3xx_hal_msp.c \ Core/Src/sysmem.c \ -Core/Src/syscalls.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c \ -Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c \ -Core/Src/system_stm32f3xx.c +Core/Src/syscalls.c # ASM sources ASM_SOURCES = \ diff --git a/mvbms.ioc b/mvbms.ioc index 80b07a5..5c75f5e 100644 --- a/mvbms.ioc +++ b/mvbms.ioc @@ -23,9 +23,10 @@ Mcu.IP3=RCC Mcu.IP4=SPI1 Mcu.IP5=SYS Mcu.IP6=TIM1 -Mcu.IP7=TIM15 -Mcu.IP8=USART1 -Mcu.IPNb=9 +Mcu.IP7=TIM2 +Mcu.IP8=TIM15 +Mcu.IP9=USART1 +Mcu.IPNb=10 Mcu.Name=STM32F302C(B-C)Tx Mcu.Package=LQFP48 Mcu.Pin0=PF0-OSC_IN @@ -109,7 +110,7 @@ PA2.Signal=S_TIM15_CH1 PA3.GPIOParameters=GPIO_Label PA3.GPIO_Label=PWM_PG_FAN2 PA3.Locked=true -PA3.Signal=S_TIM15_CH2 +PA3.Signal=S_TIM2_CH4 PA4.GPIOParameters=GPIO_Label PA4.GPIO_Label=CSB PA4.Locked=true @@ -146,6 +147,8 @@ PB11.GPIO_Label=PRECHARGE_EN PB11.Locked=true PB11.PinState=GPIO_PIN_RESET PB11.Signal=GPIO_Output +PB15.GPIOParameters=GPIO_Label +PB15.GPIO_Label=PWM_Battery_Cooling PB15.Locked=true PB15.Mode=PWM Generation3 CH3N PB15.Signal=TIM1_CH3N @@ -203,7 +206,7 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM15_Init-TIM15-false-HAL-true,7-MX_USART1_UART_Init-USART1-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM15_Init-TIM15-false-HAL-true,7-MX_USART1_UART_Init-USART1-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true,9-MX_TIM2_Init-TIM2-false-HAL-true RCC.ADC12outputFreq_Value=16000000 RCC.AHBFreq_Value=16000000 RCC.APB1Freq_Value=16000000 @@ -240,8 +243,8 @@ RCC.USBFreq_Value=16000000 RCC.VCOOutput2Freq_Value=4000000 SH.S_TIM15_CH1.0=TIM15_CH1,PWM Generation1 CH1 SH.S_TIM15_CH1.ConfNb=1 -SH.S_TIM15_CH2.0=TIM15_CH2,PWM Generation2 CH2 -SH.S_TIM15_CH2.ConfNb=1 +SH.S_TIM2_CH4.0=TIM2_CH4,PWM Generation4 CH4 +SH.S_TIM2_CH4.ConfNb=1 SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32 SPI1.CalculateBaudRate=500.0 KBits/s SPI1.DataSize=SPI_DATASIZE_8BIT @@ -252,8 +255,9 @@ SPI1.VirtualType=VM_MASTER TIM1.Channel-PWM\ Generation3\ CH3N=TIM_CHANNEL_3 TIM1.IPParameters=Channel-PWM Generation3 CH3N TIM15.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 -TIM15.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 -TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2 +TIM15.IPParameters=Channel-PWM Generation1 CH1 +TIM2.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 +TIM2.IPParameters=Channel-PWM Generation4 CH4 USART1.IPParameters=VirtualMode-Asynchronous USART1.VirtualMode-Asynchronous=VM_ASYNC VP_SYS_VS_Systick.Mode=SysTick From 600d55f29289043216bf1fbbc157fa9924c92209 Mon Sep 17 00:00:00 2001 From: kbracher Date: Fri, 24 May 2024 19:38:26 +0200 Subject: [PATCH 14/38] fix build error with HAL --- Core/Inc/TMP1075.h | 2 -- Core/Src/TMP1075.c | 2 -- Core/Src/main.c | 1 - STM32-for-VSCode.config.yaml | 1 + 4 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Core/Inc/TMP1075.h b/Core/Inc/TMP1075.h index 1e9e680..d52088c 100644 --- a/Core/Inc/TMP1075.h +++ b/Core/Inc/TMP1075.h @@ -4,8 +4,6 @@ #include "can.h" #include "common_defs.h" #include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_def.h" -#include "stm32f3xx_hal_i2c.h" #include extern uint32_t tmp1075_failed_sensors; diff --git a/Core/Src/TMP1075.c b/Core/Src/TMP1075.c index cf9a1e0..0d5f566 100644 --- a/Core/Src/TMP1075.c +++ b/Core/Src/TMP1075.c @@ -3,8 +3,6 @@ #include "can-halal.h" #include "errors.h" -#include "stm32f3xx_hal_def.h" -#include "stm32f3xx_hal_i2c.h" #define MAX_TEMP ((int16_t)(59 / 0.0625f)) #define MAX_FAILED_TEMP 12 //TODO: change value for compliance with the actual number of sensors diff --git a/Core/Src/main.c b/Core/Src/main.c index 669c751..2cd7160 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -29,7 +29,6 @@ #include "TMP1075.h" #include "errors.h" #include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_gpio.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ diff --git a/STM32-for-VSCode.config.yaml b/STM32-for-VSCode.config.yaml index d854716..e4488d0 100644 --- a/STM32-for-VSCode.config.yaml +++ b/STM32-for-VSCode.config.yaml @@ -96,6 +96,7 @@ sourceFiles: - Src/** - Core/Src/** - Core/Lib/** + - Drivers/STM32F3xx_HAL_Driver/Src/** # When no makefile is present it will show a warning pop-up. From 71335167214621a9712dec1a2a5f3fb5eaa10c4c Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 24 May 2024 21:13:02 +0300 Subject: [PATCH 15/38] added PWM_control --- Core/Inc/PWM_control.h | 13 +++++++++++++ Core/Src/PWM_control.c | 30 ++++++++++++++++++++++++++++++ Makefile | 25 +++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 Core/Inc/PWM_control.h create mode 100644 Core/Src/PWM_control.c diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h new file mode 100644 index 0000000..590b0ef --- /dev/null +++ b/Core/Inc/PWM_control.h @@ -0,0 +1,13 @@ +#ifndef INC_PWM_CONTROL_H_ +#define INC_PWM_CONTROL_H_ + +#include "stm32f3xx_hal.h" +#include "ADBMS_LL_Driver.h" +#include "main.h" + +void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling); + +void PWM_powerground_control(uint8_t percent); +void PWM_battery_cooling_control(uint8_t percent); + +#endif /* INC_CHANNEL_CONTROL_H_ */ diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c new file mode 100644 index 0000000..74c8bc3 --- /dev/null +++ b/Core/Src/PWM_control.c @@ -0,0 +1,30 @@ +#include "PWM_control.h" +#include "stm32f3xx_hal_tim.h" +#include + +uint8_t powerground_status; +uint8_t battery_cooling_status; + +TIM_HandleTypeDef powerground1, powerground2, battery_cooling; + +void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling){ + powerground_status = 0; + battery_cooling = 0; + + HAL_TIM_PWM_Start(&powerground1, 1); + HAL_TIM_PWM_Start(&powerground2, 4); + HAL_TIM_PWM_Start(&battery_cooling, 3); + + //__HAL_TIM_SET_COMPARE(&powerground1, 1, 0); + //__HAL_TIM_SET_COMPARE(&powerground2, 4, 0); + //__HAL_TIM_SET_COMPARE(&battery_cooling, 3, 0); +} + +void PWM_powerground_control(uint8_t percent){ + //uint8_t duty_cycle = (percent/100) * 65536; + //__HAL_TIM_SET_COMPARE(&powerground1, 1, 0); +} + +void PWM_battery_cooling_control(uint8_t percent){ + +} \ No newline at end of file diff --git a/Makefile b/Makefile index a020bde..4f8b9c9 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri May 24 17:06:04 GMT 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri May 24 17:29:49 GMT 2024] ########################################################################################################################## # ------------------------------------------------ @@ -40,7 +40,28 @@ Core/Src/main.c \ Core/Src/stm32f3xx_it.c \ Core/Src/stm32f3xx_hal_msp.c \ Core/Src/sysmem.c \ -Core/Src/syscalls.c +Core/Src/syscalls.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c \ +Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c \ +Core/Src/system_stm32f3xx.c # ASM sources ASM_SOURCES = \ From f6ba786e1b4ac5e17eb0263bb8f51bd05a4af0bb Mon Sep 17 00:00:00 2001 From: Hamza Date: Sat, 25 May 2024 18:41:12 +0200 Subject: [PATCH 16/38] added PWM_powerground_control() moved pg2 to TIM15CH2 --- Core/Inc/PWM_control.h | 18 ++++++++++++++++++ Core/Src/PWM_control.c | 33 +++++++++++++++++++++++---------- mvbms.ioc | 20 +++++++++----------- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index 590b0ef..8e67096 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -5,6 +5,24 @@ #include "ADBMS_LL_Driver.h" #include "main.h" +/* The PWM period (1/FPWM) is defined by the following parameters: +ARR value, the Prescaler value, and the internal clock itself which drives the timer module FCLK. +F_PWM = (F_CLK)/((ARR + 1) * (PSC + 1)) + +F_CLK = 16 MHz + +POWERGROUND ESC Signal: pulse every 20 ms, 1 ms = 0%, 2 ms = 100% +FREQ = 50 Hz -> 16 MHz/50 Hz = 320000 = ((9999 + 1) * (31 + 1)) +DUTY CYCLE = 1/20 -> 0%, DUTY CYCLE = 2/20 -> 100% +CCR * DUTY_CYCLE +CCR: 1/20 -> 500, 2/20 -> 1000 +*/ +#define POWERGROUND_FREQ 50 +#define POWERGROUND_MAX_DUTY_CYCLE 0.05 +#define POWERGROUND_MIN_DUTY_CYCLE 0.1 + +//#define BATTERY_COOLING_FREQ 20000 + void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling); void PWM_powerground_control(uint8_t percent); diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index 74c8bc3..5df4da1 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -1,28 +1,41 @@ #include "PWM_control.h" -#include "stm32f3xx_hal_tim.h" #include uint8_t powerground_status; uint8_t battery_cooling_status; +uint8_t powerground_percent; +uint8_t battery_cooling_percent; +//uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR; TIM_HandleTypeDef powerground1, powerground2, battery_cooling; +/* + Pulse width modulation mode allows for generating a signal with a frequency determined by + the value of the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register. +*/ + void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling){ powerground_status = 0; - battery_cooling = 0; + battery_cooling_status = 0; - HAL_TIM_PWM_Start(&powerground1, 1); - HAL_TIM_PWM_Start(&powerground2, 4); - HAL_TIM_PWM_Start(&battery_cooling, 3); + HAL_TIM_PWM_Start(&powerground1, 1); //TIM15CH1 + HAL_TIM_PWM_Start(&powerground2, 2); //TIM15CH2 + HAL_TIM_PWM_Start(&battery_cooling, 3); //TIM1CH3 - //__HAL_TIM_SET_COMPARE(&powerground1, 1, 0); - //__HAL_TIM_SET_COMPARE(&powerground2, 4, 0); - //__HAL_TIM_SET_COMPARE(&battery_cooling, 3, 0); + powerground_percent = 0; + battery_cooling_percent = 0; } +/* + controls the duty cycle of the fans by setting the CCR of the channel percent/100 = x/ARR +*/ void PWM_powerground_control(uint8_t percent){ - //uint8_t duty_cycle = (percent/100) * 65536; - //__HAL_TIM_SET_COMPARE(&powerground1, 1, 0); + if (percent > 100 || percent < 0) + return; + + powerground_percent = percent/100; + TIM15->CCR1 = (POWERGROUND_MAX_DUTY_CYCLE-POWERGROUND_MIN_DUTY_CYCLE) * (percent/100) + POWERGROUND_MIN_DUTY_CYCLE; + TIM15->CCR2 = TIM15->CCR1; /* *1.01 or *0.99 if the speeds of the fans are different*/ } void PWM_battery_cooling_control(uint8_t percent){ diff --git a/mvbms.ioc b/mvbms.ioc index 5c75f5e..4c362af 100644 --- a/mvbms.ioc +++ b/mvbms.ioc @@ -23,10 +23,9 @@ Mcu.IP3=RCC Mcu.IP4=SPI1 Mcu.IP5=SYS Mcu.IP6=TIM1 -Mcu.IP7=TIM2 -Mcu.IP8=TIM15 -Mcu.IP9=USART1 -Mcu.IPNb=10 +Mcu.IP7=TIM15 +Mcu.IP8=USART1 +Mcu.IPNb=9 Mcu.Name=STM32F302C(B-C)Tx Mcu.Package=LQFP48 Mcu.Pin0=PF0-OSC_IN @@ -110,7 +109,7 @@ PA2.Signal=S_TIM15_CH1 PA3.GPIOParameters=GPIO_Label PA3.GPIO_Label=PWM_PG_FAN2 PA3.Locked=true -PA3.Signal=S_TIM2_CH4 +PA3.Signal=S_TIM15_CH2 PA4.GPIOParameters=GPIO_Label PA4.GPIO_Label=CSB PA4.Locked=true @@ -190,7 +189,7 @@ ProjectManager.FreePins=true ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=true +ProjectManager.LastFirmware=false ProjectManager.LibraryCopy=1 ProjectManager.MainLocation=Core/Src ProjectManager.NoMain=false @@ -243,8 +242,8 @@ RCC.USBFreq_Value=16000000 RCC.VCOOutput2Freq_Value=4000000 SH.S_TIM15_CH1.0=TIM15_CH1,PWM Generation1 CH1 SH.S_TIM15_CH1.ConfNb=1 -SH.S_TIM2_CH4.0=TIM2_CH4,PWM Generation4 CH4 -SH.S_TIM2_CH4.ConfNb=1 +SH.S_TIM15_CH2.0=TIM15_CH2,PWM Generation2 CH2 +SH.S_TIM15_CH2.ConfNb=1 SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32 SPI1.CalculateBaudRate=500.0 KBits/s SPI1.DataSize=SPI_DATASIZE_8BIT @@ -255,9 +254,8 @@ SPI1.VirtualType=VM_MASTER TIM1.Channel-PWM\ Generation3\ CH3N=TIM_CHANNEL_3 TIM1.IPParameters=Channel-PWM Generation3 CH3N TIM15.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 -TIM15.IPParameters=Channel-PWM Generation1 CH1 -TIM2.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 -TIM2.IPParameters=Channel-PWM Generation4 CH4 +TIM15.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 +TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2 USART1.IPParameters=VirtualMode-Asynchronous USART1.VirtualMode-Asynchronous=VM_ASYNC VP_SYS_VS_Systick.Mode=SysTick From 5404cc929868c13d88986496e6f7047c22ad5577 Mon Sep 17 00:00:00 2001 From: Hamza Date: Sun, 26 May 2024 19:57:14 +0200 Subject: [PATCH 17/38] PWM_control is complete --- Core/Inc/PWM_control.h | 4 ++-- Core/Src/PWM_control.c | 14 ++++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index 8e67096..50f0c9f 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -18,8 +18,8 @@ CCR * DUTY_CYCLE CCR: 1/20 -> 500, 2/20 -> 1000 */ #define POWERGROUND_FREQ 50 -#define POWERGROUND_MAX_DUTY_CYCLE 0.05 -#define POWERGROUND_MIN_DUTY_CYCLE 0.1 +#define POWERGROUND_MAX_DUTY_CYCLE 0.1 +#define POWERGROUND_MIN_DUTY_CYCLE 0.05 //#define BATTERY_COOLING_FREQ 20000 diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index 5df4da1..a6be4ae 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -3,8 +3,6 @@ uint8_t powerground_status; uint8_t battery_cooling_status; -uint8_t powerground_percent; -uint8_t battery_cooling_percent; //uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR; TIM_HandleTypeDef powerground1, powerground2, battery_cooling; @@ -21,9 +19,6 @@ void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powergro HAL_TIM_PWM_Start(&powerground1, 1); //TIM15CH1 HAL_TIM_PWM_Start(&powerground2, 2); //TIM15CH2 HAL_TIM_PWM_Start(&battery_cooling, 3); //TIM1CH3 - - powerground_percent = 0; - battery_cooling_percent = 0; } /* @@ -33,11 +28,10 @@ void PWM_powerground_control(uint8_t percent){ if (percent > 100 || percent < 0) return; - powerground_percent = percent/100; - TIM15->CCR1 = (POWERGROUND_MAX_DUTY_CYCLE-POWERGROUND_MIN_DUTY_CYCLE) * (percent/100) + POWERGROUND_MIN_DUTY_CYCLE; + powerground_status = percent/100; + TIM15->CCR1 = (TIM15->ARR*POWERGROUND_MAX_DUTY_CYCLE-TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE) + * (percent/100) + TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE; TIM15->CCR2 = TIM15->CCR1; /* *1.01 or *0.99 if the speeds of the fans are different*/ } -void PWM_battery_cooling_control(uint8_t percent){ - -} \ No newline at end of file +void PWM_battery_cooling_control(uint8_t percent){} \ No newline at end of file From f86d8af20813805627908920157ace2e5188f50f Mon Sep 17 00:00:00 2001 From: Hamza Date: Sun, 26 May 2024 20:59:05 +0200 Subject: [PATCH 18/38] added a diagram for the state machine --- Documentation/State machine diagram.drawio | 170 +++++++++++++++++++++ Documentation/State machine diagram.svg | 4 + 2 files changed, 174 insertions(+) create mode 100644 Documentation/State machine diagram.drawio create mode 100644 Documentation/State machine diagram.svg diff --git a/Documentation/State machine diagram.drawio b/Documentation/State machine diagram.drawio new file mode 100644 index 0000000..822ad37 --- /dev/null +++ b/Documentation/State machine diagram.drawio @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Documentation/State machine diagram.svg b/Documentation/State machine diagram.svg new file mode 100644 index 0000000..2e3cfbb --- /dev/null +++ b/Documentation/State machine diagram.svg @@ -0,0 +1,4 @@ + + + +CAN Signal: 1000 0000RELAY_BAT_SIDE > RELAY_ESC_SIDEfor longer than 5 seconds
INACTIVE
INACTIVE
Allows quick shutdown. Could be done with ERRORRELAY_BAT_SIDE = RELAY_ESC_SIDEif it is around 90% done
PRECHARGE
PRECHARGE
Allows quick reactivation instead of shutdownRELAY_ESC_SIDE == 0
DISCHARGE
DISCHARGE
CAN Signal = 1100 0000
READY
READY
CAN Signal: 0000 0000 ORBattery out of charge
CAN_Signal: 1000 0000
CAN_Signal: 1000 0000
ACTIVE
ACTIVE
PRECHARGE is done
CHARGING
PRECHAGE
CHARGING...
CHARGING is done
CHARGING
CHARGING
CAN_Signal: 0000 0000
Text is not SVG - cannot display
\ No newline at end of file From 863b085d1c0c0a96e0090295384501d48b381bfd Mon Sep 17 00:00:00 2001 From: Hamza Date: Sun, 26 May 2024 21:03:26 +0200 Subject: [PATCH 19/38] removed old code --- Core/Src/main.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 2cd7160..8b9b011 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -123,8 +123,7 @@ int main(void) tmp1075_init(&hi2c1); AMS_Init(&hspi1); can_init(&hcan); - - uint32_t target_time = HAL_GetTick() + 10000; + HAL_Delay(100); /* USER CODE END 2 */ /* Infinite loop */ @@ -134,8 +133,7 @@ int main(void) /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - AMS_Loop(); - + sm_update(); can_handle_send_status(); } /* USER CODE END 3 */ From 9042ceb02c1d31e29fb28eddd7edbdbffae95155 Mon Sep 17 00:00:00 2001 From: Hamza Date: Sun, 26 May 2024 21:04:14 +0200 Subject: [PATCH 20/38] updated can.c --- Core/Inc/can.h | 6 ++++-- Core/Src/can.c | 11 ++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Core/Inc/can.h b/Core/Inc/can.h index aecdb06..c355b19 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -1,6 +1,9 @@ #include "stm32f3xx_hal.h" #include +#include "AMS_HighLevel.h" +#include "can-halal.h" +#include "state_machine.h" #define CAN_ID_IN 0x501 #define CAN_ID_OUT 0x502 @@ -9,5 +12,4 @@ void can_init(CAN_HandleTypeDef* hcan); void can_handle_send_status(); -void can_handle_recieve_command(CAN_RxHeaderTypeDef* header, uint8_t* data); - +void can_handle_recieve_command(const uint8_t *data); \ No newline at end of file diff --git a/Core/Src/can.c b/Core/Src/can.c index 963c179..747ff35 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -6,11 +6,8 @@ #include "can.h" -#include "AMS_HighLevel.h" -//#include "TMP1075.h" -#include "can-halal.h" -#include "state_machine.h" - +//#define CAN_ID_IN 0x501 +//#define CAN_ID_OUT 0x502 void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } /* @@ -25,3 +22,7 @@ void can_handle_send_status() { data[0] = (state.current_state << 5); //save 5 bit since codes are from 0-6 ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); } + +void can_handle_recieve_command(const uint8_t *data){ + ftcan_msg_received_cb(0x501, 8, data); +} From fc24a34740b857c9428998e3a16e74f9f5008b67 Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 27 May 2024 13:29:53 +0200 Subject: [PATCH 21/38] updated state machine --- Core/Inc/state_machine.h | 41 ++++--- Core/Src/can.c | 2 +- Core/Src/state_machine.c | 134 +++++++++++++-------- Documentation/State machine diagram.drawio | 86 ++++++++----- Documentation/State machine diagram.svg | 2 +- 5 files changed, 169 insertions(+), 96 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index e1326f2..64f657d 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -1,7 +1,17 @@ -//int errorcode[2] = {0,0}; 1 Bit per error +#ifndef INC_STATE_MACHINE_H +#define INC_STATE_MACHINE_H + #include #include +#include "ADBMS_LL_Driver.h" +#include "AMS_HighLevel.h" +#include "PWM_control.h" +#include "stm32f3xx_hal.h" +#include "ADBMS_Abstraction.h" +#include "main.h" +#include "can.h" + // Minimum vehicle side voltage to exit precharge #define MIN_VEHICLE_SIDE_VOLTAGE 150000 // mV // Time to wait after reaching 95% of battery voltage before exiting precharge @@ -16,19 +26,19 @@ // Time to wait between closing relays #define RELAY_CLOSE_WAIT 10 // ms -typedef enum { // 7 states -> 3 bit. valid transitions: (all could transition to error) - STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR - STATE_PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR - STATE_READY, // READY -> ACTIVE, DISCHARGE, ERROR - STATE_ACTIVE, // ACTIVE -> READY, DISCHARGE, ERROR - STATE_DISCHARGE, // DISCHARGE -> INACTIVE, PRECHARGE, ERROR - STATE_CHARGING, // CHARGING -> INACTIVE, DISCHARGE, ERROR - STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR +typedef enum { // states -> 3 bit. valid transitions: (all could transition to error) + STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR + STATE_PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR + STATE_READY, // READY -> ACTIVE, DISCHARGE, ERROR + STATE_ACTIVE, // ACTIVE -> READY, DISCHARGE, ERROR + STATE_DISCHARGE, // DISCHARGE -> INACTIVE, PRECHARGE, ERROR + STATE_CHARGING_PRECHARGE, + STATE_CHARGING, // CHARGING -> INACTIVE, DISCHARGE, ERROR + STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR } State; typedef struct { uint16_t bms_timeout : 1; - uint16_t bms_checksum_fail : 1; uint16_t bms_overtemp : 1; uint16_t bms_fault : 1; @@ -39,9 +49,9 @@ typedef struct { uint16_t temperature_sensor_missing : 1; uint16_t current_sensor_missing : 1; uint16_t voltage_missing : 1; + uint16_t battery_missing : 1; uint16_t relay_missing : 1; - uint16_t state_fail : 1; uint16_t state_transition_fail : 1; } ErrorKind; @@ -64,16 +74,17 @@ State sm_update_precharge(); State sm_update_ready(); State sm_update_active(); State sm_update_discharge(); +State sm_update_charging_precharge(); State sm_update_charging(); State sm_update_error(); typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; void sm_set_relay_positions(State state); void sm_set_relay(Relay relay, bool closed); -void sm_check_precharge_discharge(bool *is_closed, bool should_close); +void sm_charging_check(); +void sm_handle_ams_in(); void sm_check_errors(); +void sm_set_error(ErrorKind error_kind, bool is_errored); -void sm_handle_ams_in(const uint8_t *data); - -void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file +#endif /* "INC_STATE_MACHINE_H" */ \ No newline at end of file diff --git a/Core/Src/can.c b/Core/Src/can.c index 747ff35..ab1eadb 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -24,5 +24,5 @@ void can_handle_send_status() { } void can_handle_recieve_command(const uint8_t *data){ - ftcan_msg_received_cb(0x501, 8, data); + ftcan_msg_received_cb(0x501, 16, data); } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index b79f3e0..2a2322a 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -1,74 +1,72 @@ #include "state_machine.h" -#include "ADBMS_LL_Driver.h" -#include "AMS_HighLevel.h" -#include "stm32f3xx_hal.h" -#include "ADBMS_Abstraction.h" -#include "main.h" StateHandle state; static bool relay_closed = 0; static bool precharge_closed = 0; -static int16_t RELAY_BAT_SIDE = 0; -static int16_t RELAY_ESC_SIDE = 0; -static int16_t CURRENT_MEASUREMENT = 0; +static int16_t RELAY_BAT_SIDE_VOLTAGE = 0; +static int16_t RELAY_ESC_SIDE_VOLTAGE = 0; +static int16_t CURRENT_MEASUREMENT_VOLTAGE = 0; +static int16_t timestamp; void sm_init(){ state.current_state = STATE_INACTIVE; state.target_state = STATE_INACTIVE; state.error_source = 0; + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0]; + RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1]; + CURRENT_MEASUREMENT_VOLTAGE = module.auxVoltages[2]; } void sm_update(){ - + sm_handle_ams_in(); switch (state.current_state) { case STATE_INACTIVE: - state.current_state = sm_update_inactive(); // monitor only + state.current_state = sm_update_inactive(); // monitor only break; case STATE_PRECHARGE: - state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such + state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such break; case STATE_READY: - state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground + state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground break; case STATE_ACTIVE: - state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such + state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such break; case STATE_DISCHARGE: - state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed + state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed + break; + case STATE_CHARGING_PRECHARGE: + state.current_state = sm_update_charging_precharge(); break; case STATE_CHARGING: - state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. + state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. break; case STATE_ERROR: - state.current_state = sm_update_error(); // enter the correct ERROR state + state.current_state = sm_update_error(); // enter the correct ERROR state break; } sm_set_relay_positions(state.current_state); - //status_led_state(state.current_state, (ErrorKind) state.error_type); } State sm_update_inactive(){ switch (state.target_state) { case STATE_PRECHARGE: - //close precharge relay, wait until both sides are similar - sm_set_relay_positions(STATE_PRECHARGE); return STATE_PRECHARGE; - case STATE_CHARGING: - return STATE_CHARGING; - default: + case STATE_CHARGING_PRECHARGE: + return STATE_CHARGING_PRECHARGE; + default: return STATE_INACTIVE; } } State sm_update_precharge(){ switch (state.target_state) { - case STATE_INACTIVE: - return STATE_INACTIVE; - case STATE_READY: - return STATE_READY; - case STATE_DISCHARGE: + case STATE_INACTIVE: // if CAN Signal 0000 0000 then immidiete shutdown return STATE_DISCHARGE; + case STATE_READY: + if (RELAY_BAT_SIDE_VOLTAGE == RELAY_ESC_SIDE_VOLTAGE) + return STATE_READY; default: return STATE_PRECHARGE; } @@ -76,9 +74,9 @@ State sm_update_precharge(){ State sm_update_ready(){ switch (state.target_state) { - case STATE_ACTIVE: + case STATE_ACTIVE: // if CAN Signal 1100 0000 then turn on powerground return STATE_ACTIVE; - case STATE_DISCHARGE: + case STATE_DISCHARGE: // if CAN Signal 0000 0000 then shutdown return STATE_DISCHARGE; default: return STATE_READY; @@ -87,9 +85,9 @@ State sm_update_ready(){ State sm_update_active(){ switch (state.target_state) { - case STATE_READY: - return STATE_READY; - case STATE_DISCHARGE: + case STATE_READY: // if CAN Signal 1000 0000 then turn oof powerground but stay ready + return STATE_READY; + case STATE_DISCHARGE: // if CAN Signal 0000 0000 then shutdown return STATE_DISCHARGE; default: return STATE_ACTIVE; @@ -99,18 +97,28 @@ State sm_update_active(){ State sm_update_discharge(){ switch (state.target_state) { case STATE_INACTIVE: - return STATE_INACTIVE; - case STATE_PRECHARGE: + if (RELAY_ESC_SIDE_VOLTAGE == 0) + return STATE_INACTIVE; + case STATE_PRECHARGE: // if CAN Signal 1000 0000 then get ready return STATE_PRECHARGE; default: return STATE_DISCHARGE; } } +State sm_update_charging_precharge(){ + switch (state.target_state) { + case STATE_CHARGING: + return STATE_CHARGING; + case STATE_DISCHARGE: + return STATE_DISCHARGE; + default: + return STATE_CHARGING_PRECHARGE; + } +} + State sm_update_charging(){ switch (state.target_state) { - case STATE_INACTIVE: - return STATE_INACTIVE; case STATE_DISCHARGE: return STATE_DISCHARGE; default: @@ -118,10 +126,9 @@ State sm_update_charging(){ } } + State sm_update_error(){ switch (state.target_state) { - case STATE_INACTIVE: - return STATE_INACTIVE; case STATE_DISCHARGE: return STATE_DISCHARGE; default: @@ -148,7 +155,7 @@ void sm_set_relay_positions(State current_state){ sm_set_relay(RELAY_PRECHARGE, 0); case STATE_DISCHARGE: sm_set_relay(RELAY_MAIN, 0); - sm_set_relay(RELAY_PRECHARGE, 1); + sm_set_relay(RELAY_PRECHARGE, 0); break; case STATE_CHARGING: sm_set_relay(RELAY_MAIN, 1); @@ -156,6 +163,7 @@ void sm_set_relay_positions(State current_state){ break; case STATE_ERROR: sm_set_relay(RELAY_MAIN, 0); + sm_set_relay(RELAY_PRECHARGE, 0); break; } } @@ -174,14 +182,46 @@ void sm_set_relay(Relay relay, bool closed){ } } +void sm_handle_ams_in(){ + uint8_t data[2] = {}; + can_handle_recieve_command(&data); + switch (data[0]) { + case 0b00000000: + if (state.current_state != STATE_INACTIVE){ + PWM_powerground_control(0); + state.target_state = STATE_DISCHARGE; + } + break; + case 0b10000000: + if (state.target_state == STATE_INACTIVE || state.target_state == STATE_DISCHARGE){ + PWM_powerground_control(0); + state.target_state = STATE_PRECHARGE; + } else if (state.target_state == STATE_ACTIVE){ + PWM_powerground_control(0); + state.target_state = STATE_READY; + } + break; + case 0b11000000: + PWM_powerground_control(data[1]); + state.target_state = STATE_ACTIVE; // READY -> ACTIVE + break; + } +} -void sm_check_precharge_discharge(bool *is_closed, bool should_close){} - // compare RELAY_BATT_SIDE and RELAY_ESC_SIDE - // if (state.current_state == STATE_PRECHARGE && (RELAY_ESC_SIDE < RELAY_BAT_SIDE)) //-> don't switch from PRECHARGE to READY - // if (state.current_state == STATE_DISCHARGE && (RELAY_ESC_SIDE > 12V)) -> don't switch from DISCHARGE to INACTIVE +void sm_set_error(ErrorKind error_kind, bool is_errored){} -void sm_handle_ams_in(const uint8_t *data){} +void sm_check_errors(){ + if (module.status.THSD == 1) { + state.error_type.bms_overtemp = 1; + } + if (RELAY_BAT_SIDE_VOLTAGE < 40){ + state.error_source = (1 << 10); + } +} -void sm_set_error(ErrorKind error_kind, bool is_errored); - -void sm_check_errors(){} \ No newline at end of file +void sm_charging_check(){ + if (RELAY_BAT_SIDE_VOLTAGE < RELAY_ESC_SIDE_VOLTAGE && timestamp == 0) + timestamp = HAL_GetTick() + 5000; + if (timestamp < HAL_GetTick()) + state.target_state = STATE_CHARGING_PRECHARGE; +} \ No newline at end of file diff --git a/Documentation/State machine diagram.drawio b/Documentation/State machine diagram.drawio index 822ad37..69a3678 100644 --- a/Documentation/State machine diagram.drawio +++ b/Documentation/State machine diagram.drawio @@ -1,15 +1,15 @@ - + - + - + - + @@ -20,7 +20,7 @@ - + @@ -30,14 +30,14 @@ - + - + - - + + @@ -49,7 +49,7 @@ - + @@ -59,13 +59,13 @@ - + - - + + @@ -76,21 +76,20 @@ - + - - - + + - + - + @@ -98,8 +97,8 @@ - - + + @@ -109,10 +108,17 @@ - + - - + + + + + + + + + @@ -122,8 +128,8 @@ - - + + @@ -133,7 +139,7 @@ - + @@ -143,12 +149,12 @@ - - + + - + @@ -157,13 +163,29 @@ - + - + + + + + + + + + + + + + + + + + diff --git a/Documentation/State machine diagram.svg b/Documentation/State machine diagram.svg index 2e3cfbb..58f8d46 100644 --- a/Documentation/State machine diagram.svg +++ b/Documentation/State machine diagram.svg @@ -1,4 +1,4 @@ -CAN Signal: 1000 0000RELAY_BAT_SIDE > RELAY_ESC_SIDEfor longer than 5 seconds
INACTIVE
INACTIVE
Allows quick shutdown. Could be done with ERRORRELAY_BAT_SIDE = RELAY_ESC_SIDEif it is around 90% done
PRECHARGE
PRECHARGE
Allows quick reactivation instead of shutdownRELAY_ESC_SIDE == 0
DISCHARGE
DISCHARGE
CAN Signal = 1100 0000
READY
READY
CAN Signal: 0000 0000 ORBattery out of charge
CAN_Signal: 1000 0000
CAN_Signal: 1000 0000
ACTIVE
ACTIVE
PRECHARGE is done
CHARGING
PRECHAGE
CHARGING...
CHARGING is done
CHARGING
CHARGING
CAN_Signal: 0000 0000
Text is not SVG - cannot display
\ No newline at end of file +CAN Signal: 1000 0000RELAY_BAT_SIDE > RELAY_ESC_SIDEfor longer than 5 seconds
INACTIVE
INACTIVE
Allows quick shutdown. Could be done with ERRORRELAY_BAT_SIDE = RELAY_ESC_SIDEif it is around 90% done
PRECHARGE
PRECHARGE
Allows quick reactivation instead of shutdownRELAY_ESC_SIDE == 0
DISCHARGE
DISCHARGE
CAN Signal = 1100 0000
READY
READY
CAN Signal: 0000 0000 ORBattery out of charge
CAN_Signal: 1000 0000
CAN_Signal: 1000 0000
ACTIVE
ACTIVE
PRECHARGE is done
CHARGING
PRECHAGE
CHARGING...
CHARGING is done
CHARGING
CHARGING
CAN_Signal: 0000 0000
PRECHARGE failure
PRECHARGE failure
Text is not SVG - cannot display
\ No newline at end of file From 07f11db3f0820ca9b88cb0804a11b402d720cde3 Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 27 May 2024 13:46:49 +0200 Subject: [PATCH 22/38] fixed includes --- Core/Inc/AMS_HighLevel.h | 1 - Core/Inc/PWM_control.h | 6 +++--- Core/Inc/can.h | 9 +++++++-- Core/Src/PWM_control.c | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Core/Inc/AMS_HighLevel.h b/Core/Inc/AMS_HighLevel.h index 870007a..70c6134 100644 --- a/Core/Inc/AMS_HighLevel.h +++ b/Core/Inc/AMS_HighLevel.h @@ -11,7 +11,6 @@ #include "ADBMS_Abstraction.h" #include "ADBMS_CMD_MAKROS.h" #include "ADBMS_LL_Driver.h" -#include "can.h" typedef enum { AMSDEACTIVE, diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index 50f0c9f..4c83bbb 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -1,5 +1,5 @@ -#ifndef INC_PWM_CONTROL_H_ -#define INC_PWM_CONTROL_H_ +#ifndef INC_PWM_CONTROL_H +#define INC_PWM_CONTROL_H #include "stm32f3xx_hal.h" #include "ADBMS_LL_Driver.h" @@ -28,4 +28,4 @@ void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powergro void PWM_powerground_control(uint8_t percent); void PWM_battery_cooling_control(uint8_t percent); -#endif /* INC_CHANNEL_CONTROL_H_ */ +#endif /* INC_CHANNEL_CONTROL_H */ diff --git a/Core/Inc/can.h b/Core/Inc/can.h index c355b19..7f6fcae 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -1,9 +1,12 @@ +#ifndef INC_CAN_H +#define INC_CAN_H #include "stm32f3xx_hal.h" #include +#include "main.h" #include "AMS_HighLevel.h" -#include "can-halal.h" #include "state_machine.h" +#include "can-halal.h" #define CAN_ID_IN 0x501 #define CAN_ID_OUT 0x502 @@ -12,4 +15,6 @@ void can_init(CAN_HandleTypeDef* hcan); void can_handle_send_status(); -void can_handle_recieve_command(const uint8_t *data); \ No newline at end of file +void can_handle_recieve_command(const uint8_t *data); + +#endif /* "INC_CAN_H" */ \ No newline at end of file diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index a6be4ae..7ad7c19 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -25,7 +25,7 @@ void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powergro controls the duty cycle of the fans by setting the CCR of the channel percent/100 = x/ARR */ void PWM_powerground_control(uint8_t percent){ - if (percent > 100 || percent < 0) + if (percent > 100) //something went wrong return; powerground_status = percent/100; From fcc8f54fe0eeddbf627f5c4286a6c4d7c9ba2bc9 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 28 May 2024 18:39:35 +0200 Subject: [PATCH 23/38] removed stdint.h --- Core/Inc/PWM_control.h | 1 + Core/Src/PWM_control.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index 4c83bbb..e3b63e7 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -3,6 +3,7 @@ #include "stm32f3xx_hal.h" #include "ADBMS_LL_Driver.h" +#include #include "main.h" /* The PWM period (1/FPWM) is defined by the following parameters: diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index 7ad7c19..a5fde64 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -1,5 +1,4 @@ #include "PWM_control.h" -#include uint8_t powerground_status; uint8_t battery_cooling_status; From 2c1171e8307a8d1f26ab87c2088f17a93386b7b5 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 28 May 2024 18:40:05 +0200 Subject: [PATCH 24/38] moved libraries to .h file --- Core/Inc/TMP1075.h | 4 ++++ Core/Src/TMP1075.c | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Core/Inc/TMP1075.h b/Core/Inc/TMP1075.h index d52088c..345b2fe 100644 --- a/Core/Inc/TMP1075.h +++ b/Core/Inc/TMP1075.h @@ -4,10 +4,14 @@ #include "can.h" #include "common_defs.h" #include "stm32f3xx_hal.h" +#include "TMP1075.h" +#include "can-halal.h" +#include "errors.h" #include extern uint32_t tmp1075_failed_sensors; extern int16_t tmp1075_temps[N_TEMP_SENSORS]; + HAL_StatusTypeDef tmp1075_init(I2C_HandleTypeDef* hi2c); HAL_StatusTypeDef tmp1075_measure(); HAL_StatusTypeDef tmp1075_sensor_init(int n); diff --git a/Core/Src/TMP1075.c b/Core/Src/TMP1075.c index 0d5f566..9fd4aae 100644 --- a/Core/Src/TMP1075.c +++ b/Core/Src/TMP1075.c @@ -1,9 +1,5 @@ #include "TMP1075.h" -#include "can-halal.h" - -#include "errors.h" - #define MAX_TEMP ((int16_t)(59 / 0.0625f)) #define MAX_FAILED_TEMP 12 //TODO: change value for compliance with the actual number of sensors #warning "change value for compliance with the actual number of sensors" From a0e55c0f444971d57bb635666a90861664e36c31 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 28 May 2024 19:01:01 +0200 Subject: [PATCH 25/38] implemented can_hanlde_recieve_command correctly changed can_handle_send_status --- Core/Inc/can.h | 3 ++- Core/Inc/state_machine.h | 16 ++++++------- Core/Src/can.c | 48 ++++++++++++++++++++++++++++++++++++--- Core/Src/state_machine.c | 49 +++++++++++++++++++++++----------------- 4 files changed, 83 insertions(+), 33 deletions(-) diff --git a/Core/Inc/can.h b/Core/Inc/can.h index 7f6fcae..8b6651f 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -4,7 +4,6 @@ #include "stm32f3xx_hal.h" #include #include "main.h" -#include "AMS_HighLevel.h" #include "state_machine.h" #include "can-halal.h" @@ -17,4 +16,6 @@ void can_handle_send_status(); void can_handle_recieve_command(const uint8_t *data); +void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data); + #endif /* "INC_CAN_H" */ \ No newline at end of file diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index 64f657d..9442090 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -3,14 +3,8 @@ #include #include - #include "ADBMS_LL_Driver.h" #include "AMS_HighLevel.h" -#include "PWM_control.h" -#include "stm32f3xx_hal.h" -#include "ADBMS_Abstraction.h" -#include "main.h" -#include "can.h" // Minimum vehicle side voltage to exit precharge #define MIN_VEHICLE_SIDE_VOLTAGE 150000 // mV @@ -65,6 +59,11 @@ typedef struct { } StateHandle; extern StateHandle state; +static bool relay_closed = 0; +static bool precharge_closed = 0; +static int16_t RELAY_BAT_SIDE_VOLTAGE = 0; +static int16_t RELAY_ESC_SIDE_VOLTAGE = 0; +static int16_t CURRENT_MEASUREMENT = 0; void sm_init(); void sm_update(); @@ -81,9 +80,10 @@ State sm_update_error(); typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; void sm_set_relay_positions(State state); void sm_set_relay(Relay relay, bool closed); -void sm_charging_check(); +void sm_check_charging(); +void sm_check_cell_temps(int8_t* id, int16_t* temp); -void sm_handle_ams_in(); +void sm_handle_ams_in(const uint8 *data); void sm_check_errors(); void sm_set_error(ErrorKind error_kind, bool is_errored); diff --git a/Core/Src/can.c b/Core/Src/can.c index ab1eadb..9b71da5 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -5,6 +5,8 @@ */ #include "can.h" +#include "ADBMS_Abstraction.h" +#include "state_machine.h" //#define CAN_ID_IN 0x501 //#define CAN_ID_OUT 0x502 @@ -15,14 +17,54 @@ This function sends the status of the mvbms, the battery and of powerground. once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR. once every 0.5s in states: READY, ACTIVE. with format of: +CAN Messages: +- MVBMS Status (1B), Powerground Status 0-100% (1 bit) +- Battery: SoC (1B), Pack Voltage (2B), Current (1B), + Min/Max. Cell Temp (ID, Min Temp, ID, Max Temp)(4B), + Min/Max Cell Voltage (ID, Min Voltage, ID, Max Voltage)(4B) +bit 0-2: status +bit 0-7: +bit 8-15: State of Charge from 0-100% +bit 16-31: Battery voltage +bit 32-47: Current measurement +bit 48-50: id of cell with highest temperature +bit 51-62: temperature of the cell with highest temperature (12 bits moved 4 bit to the left) */ + void can_handle_send_status() { - static uint8_t data[8] = {}; - data[0] = (state.current_state << 5); //save 5 bit since codes are from 0-6 + uint8_t data[8] = {}; + data[0] = (state.current_state << 5); // save 5 bit since codes are from 0-7 61 bits left + //data[1] = // in 8 bits from 0-100% + data[2] = mV_from_ADBMS6830(RELAY_BAT_SIDE_VOLTAGE); // Battery voltage 16 bit 45 bit + data[4] = CURRENT_MEASUREMENT; // 16 bit measurement + + int8_t id = -1; + int16_t temp = INT16_MIN; + sm_check_cell_temps(&id, &temp); + data[6] = (id << 5) | (temp >> 4); // there are only 7 TMP1075 ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); } +/* +can_handle_recieve_command() should only check if the message is valid and then hand it +to the sm_handle_ams_in() which handles the state machine transition. +*/ void can_handle_recieve_command(const uint8_t *data){ - ftcan_msg_received_cb(0x501, 16, data); + if (data[0] == 0x00 && data[1] == 0x00){ + sm_handle_ams_in(data); + } else if (data[0] == 0b1000000 && data[1] == 0x00){ + sm_handle_ams_in(data); + } else if (data[0] == 0b1100000 && data[1] <= 100) { + sm_handle_ams_in(data); + } +} +/* +implements the _weak method ftcan_msg_recieved_cb() which throws an interrupt when a CAN message is recieved. +it only checks if the id is and datalen is correct thans hands data over to can_handle_recieve_command(). +*/ +void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data){ + if (id == 0x501 && datalen == 16){ + can_handle_recieve_command(data); + } } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 2a2322a..8f232df 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -1,24 +1,23 @@ #include "state_machine.h" +#include "ADBMS_LL_Driver.h" +#include "TMP1075.h" +#include "common_defs.h" +#include StateHandle state; -static bool relay_closed = 0; -static bool precharge_closed = 0; -static int16_t RELAY_BAT_SIDE_VOLTAGE = 0; -static int16_t RELAY_ESC_SIDE_VOLTAGE = 0; -static int16_t CURRENT_MEASUREMENT_VOLTAGE = 0; static int16_t timestamp; void sm_init(){ state.current_state = STATE_INACTIVE; state.target_state = STATE_INACTIVE; state.error_source = 0; - RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0]; - RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1]; - CURRENT_MEASUREMENT_VOLTAGE = module.auxVoltages[2]; } void sm_update(){ - sm_handle_ams_in(); + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0]; + RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1]; + CURRENT_MEASUREMENT_VOLTAGE = module.auxVoltages[2]; + switch (state.current_state) { case STATE_INACTIVE: state.current_state = sm_update_inactive(); // monitor only @@ -65,7 +64,7 @@ State sm_update_precharge(){ case STATE_INACTIVE: // if CAN Signal 0000 0000 then immidiete shutdown return STATE_DISCHARGE; case STATE_READY: - if (RELAY_BAT_SIDE_VOLTAGE == RELAY_ESC_SIDE_VOLTAGE) + if (roundf(RELAY_BAT_SIDE_VOLTAGE) == roundf(RELAY_ESC_SIDE_VOLTAGE)) return STATE_READY; default: return STATE_PRECHARGE; @@ -97,7 +96,7 @@ State sm_update_active(){ State sm_update_discharge(){ switch (state.target_state) { case STATE_INACTIVE: - if (RELAY_ESC_SIDE_VOLTAGE == 0) + if (RELAY_ESC_SIDE_VOLTAGE < 12) return STATE_INACTIVE; case STATE_PRECHARGE: // if CAN Signal 1000 0000 then get ready return STATE_PRECHARGE; @@ -182,9 +181,23 @@ void sm_set_relay(Relay relay, bool closed){ } } -void sm_handle_ams_in(){ - uint8_t data[2] = {}; - can_handle_recieve_command(&data); + +void sm_check_charging(){ + if (RELAY_BAT_SIDE_VOLTAGE < RELAY_ESC_SIDE_VOLTAGE && timestamp == 0) + timestamp = HAL_GetTick() + 5000; + if (timestamp < HAL_GetTick()) + state.target_state = STATE_CHARGING_PRECHARGE; +} + +void sm_check_cell_temps(int8_t *id, int16_t *temp){ + for (int i = 0; i < N_TEMP_SENSORS; i++) { + if (tmp1075_temps[i] > *temp){ + *id = i; + } + } +} + +void sm_handle_ams_in(const uint8_t *data){ switch (data[0]) { case 0b00000000: if (state.current_state != STATE_INACTIVE){ @@ -210,6 +223,7 @@ void sm_handle_ams_in(){ void sm_set_error(ErrorKind error_kind, bool is_errored){} +#warning TODO: add error checking for everything here void sm_check_errors(){ if (module.status.THSD == 1) { state.error_type.bms_overtemp = 1; @@ -218,10 +232,3 @@ void sm_check_errors(){ state.error_source = (1 << 10); } } - -void sm_charging_check(){ - if (RELAY_BAT_SIDE_VOLTAGE < RELAY_ESC_SIDE_VOLTAGE && timestamp == 0) - timestamp = HAL_GetTick() + 5000; - if (timestamp < HAL_GetTick()) - state.target_state = STATE_CHARGING_PRECHARGE; -} \ No newline at end of file From cb4b5cb53efb98b5832e7082ce921be7d91de654 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 28 May 2024 19:43:24 +0200 Subject: [PATCH 26/38] added sm_test_cycle_states(); --- Core/Inc/state_machine.h | 5 +++++ Core/Src/state_machine.c | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index 9442090..6e85d96 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -5,6 +5,9 @@ #include #include "ADBMS_LL_Driver.h" #include "AMS_HighLevel.h" +#include "PWM_control.h" +#include "TMP1075.h" +#include // Minimum vehicle side voltage to exit precharge #define MIN_VEHICLE_SIDE_VOLTAGE 150000 // mV @@ -87,4 +90,6 @@ void sm_handle_ams_in(const uint8 *data); void sm_check_errors(); void sm_set_error(ErrorKind error_kind, bool is_errored); +void sm_test_cycle_states(); + #endif /* "INC_STATE_MACHINE_H" */ \ No newline at end of file diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 8f232df..5360fb5 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -1,11 +1,8 @@ #include "state_machine.h" -#include "ADBMS_LL_Driver.h" -#include "TMP1075.h" -#include "common_defs.h" -#include +#include "stm32f3xx_hal.h" StateHandle state; -static int16_t timestamp; +static uint32_t timestamp; void sm_init(){ state.current_state = STATE_INACTIVE; @@ -16,7 +13,7 @@ void sm_init(){ void sm_update(){ RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0]; RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1]; - CURRENT_MEASUREMENT_VOLTAGE = module.auxVoltages[2]; + CURRENT_MEASUREMENT = module.auxVoltages[2]; switch (state.current_state) { case STATE_INACTIVE: @@ -232,3 +229,30 @@ void sm_check_errors(){ state.error_source = (1 << 10); } } + +void sm_test_cycle_states(){ + if (timestamp > HAL_GetTick()) + return; + switch (state.current_state) { + case STATE_INACTIVE: + state.current_state = STATE_PRECHARGE; + timestamp = HAL_GetTick() + 10000; + return; + case STATE_PRECHARGE: + state.current_state = STATE_READY; + timestamp = HAL_GetTick() + 10000; + return; + case STATE_READY: + state.current_state = STATE_ACTIVE; + timestamp = HAL_GetTick() + 10000; + return; + case STATE_ACTIVE: + state.current_state = STATE_DISCHARGE; + timestamp = HAL_GetTick() + 10000; + return; + case STATE_DISCHARGE: + state.current_state = STATE_INACTIVE; + timestamp = HAL_GetTick() + 10000; + return; + } +} \ No newline at end of file From ad21577cbb02412536153a78b37c552482120157 Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 31 May 2024 02:03:44 +0300 Subject: [PATCH 27/38] added some testing tools --- .mxproject | 2 +- Core/Inc/PWM_control.h | 4 +- Core/Inc/state_machine.h | 6 +-- Core/Src/PWM_control.c | 27 +++++++----- Core/Src/main.c | 83 +++++++++++------------------------- Core/Src/state_machine.c | 27 ++++++++---- Core/Src/stm32f3xx_hal_msp.c | 50 ++-------------------- Makefile | 2 +- mvbms.ioc | 7 ++- 9 files changed, 77 insertions(+), 131 deletions(-) diff --git a/.mxproject b/.mxproject index ce6f6d0..95b5290 100644 --- a/.mxproject +++ b/.mxproject @@ -1,5 +1,5 @@ [PreviousLibFiles] -LibFiles=Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_can.h;Drivers/STM32F3xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_def.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_bus.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_crs.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_system.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_utils.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_usart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart_ex.h;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_can.h;Drivers/STM32F3xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_def.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_bus.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_crs.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_system.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_utils.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_usart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart_ex.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/stm32f302xc.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/stm32f3xx.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/system_stm32f3xx.h;Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/cmsis_armcc.h; +LibFiles=Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_can.h;Drivers/STM32F3xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_def.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_bus.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_crs.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_system.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_utils.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_usart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart_ex.h;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_can.h;Drivers/STM32F3xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_def.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_rcc_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_bus.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_rcc.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_crs.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_system.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_utils.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_gpio_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_gpio.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_dma.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_cortex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_pwr_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_pwr.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_flash_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_i2c_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_exti.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_i2c.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_spi.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_spi_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_tim_ex.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_tim.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_ll_usart.h;Drivers/STM32F3xx_HAL_Driver/Inc/stm32f3xx_hal_uart_ex.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/stm32f302xc.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/stm32f3xx.h;Drivers/CMSIS/Device/ST/STM32F3xx/Include/system_stm32f3xx.h;Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_sc300.h; [PreviousUsedMakefileFiles] SourceFiles=Core/Src/main.c;Core/Src/stm32f3xx_it.c;Core/Src/stm32f3xx_hal_msp.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c;Core/Src/system_stm32f3xx.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_can.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_rcc_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_gpio.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_dma.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_cortex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_pwr_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_flash_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_i2c_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_tim_ex.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart.c;Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c;Core/Src/system_stm32f3xx.c;;; diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index e3b63e7..640b277 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -13,7 +13,7 @@ F_PWM = (F_CLK)/((ARR + 1) * (PSC + 1)) F_CLK = 16 MHz POWERGROUND ESC Signal: pulse every 20 ms, 1 ms = 0%, 2 ms = 100% -FREQ = 50 Hz -> 16 MHz/50 Hz = 320000 = ((9999 + 1) * (31 + 1)) +FREQ = 50 Hz -> 16 MHz/50 Hz = 320000 = ((39999 + 1) * (7 + 1)) DUTY CYCLE = 1/20 -> 0%, DUTY CYCLE = 2/20 -> 100% CCR * DUTY_CYCLE CCR: 1/20 -> 500, 2/20 -> 1000 @@ -24,7 +24,7 @@ CCR: 1/20 -> 500, 2/20 -> 1000 //#define BATTERY_COOLING_FREQ 20000 -void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling); +void PWM_control_init(TIM_HandleTypeDef* powerground1, TIM_HandleTypeDef* powerground2, TIM_HandleTypeDef* battery_cooling); void PWM_powerground_control(uint8_t percent); void PWM_battery_cooling_control(uint8_t percent); diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index 6e85d96..716ad3b 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -64,9 +64,9 @@ typedef struct { extern StateHandle state; static bool relay_closed = 0; static bool precharge_closed = 0; -static int16_t RELAY_BAT_SIDE_VOLTAGE = 0; -static int16_t RELAY_ESC_SIDE_VOLTAGE = 0; -static int16_t CURRENT_MEASUREMENT = 0; +static int16_t RELAY_BAT_SIDE_VOLTAGE; +static int16_t RELAY_ESC_SIDE_VOLTAGE; +static int16_t CURRENT_MEASUREMENT; void sm_init(); void sm_update(); diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index a5fde64..364b709 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -1,23 +1,30 @@ #include "PWM_control.h" +#include "stm32f3xx_hal_tim.h" uint8_t powerground_status; uint8_t battery_cooling_status; //uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR; -TIM_HandleTypeDef powerground1, powerground2, battery_cooling; +TIM_HandleTypeDef* powerground, *battery_cooling; /* Pulse width modulation mode allows for generating a signal with a frequency determined by the value of the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register. */ -void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling){ +void PWM_control_init(TIM_HandleTypeDef* pg1, TIM_HandleTypeDef* pg2, TIM_HandleTypeDef* bat_cool){ powerground_status = 0; battery_cooling_status = 0; - HAL_TIM_PWM_Start(&powerground1, 1); //TIM15CH1 - HAL_TIM_PWM_Start(&powerground2, 2); //TIM15CH2 - HAL_TIM_PWM_Start(&battery_cooling, 3); //TIM1CH3 + HAL_TIM_PWM_Start(pg1, TIM_CHANNEL_1); //TIM15CH1 + HAL_TIM_PWM_Start(pg2, TIM_CHANNEL_2); //TIM15CH2 + HAL_TIM_PWM_Start(bat_cool, TIM_CHANNEL_3); //TIM1CH3 + + powerground = pg1; + battery_cooling = bat_cool; + __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 2000); + __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000); + //__HAL_TIM_SET_COMPARE(battery_cooling, TIM_CHANNEL_3, 2000); } /* @@ -26,11 +33,11 @@ void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powergro void PWM_powerground_control(uint8_t percent){ if (percent > 100) //something went wrong return; - - powerground_status = percent/100; - TIM15->CCR1 = (TIM15->ARR*POWERGROUND_MAX_DUTY_CYCLE-TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE) - * (percent/100) + TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE; - TIM15->CCR2 = TIM15->CCR1; /* *1.01 or *0.99 if the speeds of the fans are different*/ + powerground_status = percent; + int ccr = 2000 + ((2000) * (percent/100.0)); + __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, ccr); + __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000 + ((2000) * (percent/100.0))); + //TIM15->CCR1 = (TIM15->ARR*POWERGROUND_MAX_DUTY_CYCLE-TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE) * (percent/100.0) + TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE; } void PWM_battery_cooling_control(uint8_t percent){} \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 8b9b011..07e81b8 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -23,12 +23,15 @@ /* USER CODE BEGIN Includes */ #include "ADBMS_Abstraction.h" #include "ADBMS_CMD_MAKROS.h" +#include "PWM_control.h" #include "can.h" #include "AMS_HighLevel.h" #include "state_machine.h" #include "TMP1075.h" #include "errors.h" +#include "stm32f302xc.h" #include "stm32f3xx_hal.h" +#include "stm32f3xx_hal_tim.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -54,7 +57,6 @@ I2C_HandleTypeDef hi2c1; SPI_HandleTypeDef hspi1; TIM_HandleTypeDef htim1; -TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim15; UART_HandleTypeDef huart1; @@ -72,7 +74,6 @@ static void MX_SPI1_Init(void); static void MX_TIM15_Init(void); static void MX_USART1_UART_Init(void); static void MX_TIM1_Init(void); -static void MX_TIM2_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ @@ -117,13 +118,13 @@ int main(void) MX_TIM15_Init(); MX_USART1_UART_Init(); MX_TIM1_Init(); - MX_TIM2_Init(); /* USER CODE BEGIN 2 */ sm_init(); tmp1075_init(&hi2c1); AMS_Init(&hspi1); can_init(&hcan); - HAL_Delay(100); + PWM_control_init(&htim15, &htim15, &htim1); + HAL_Delay(10); /* USER CODE END 2 */ /* Infinite loop */ @@ -133,8 +134,20 @@ int main(void) /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ + int ttt = HAL_GetTick() + 45000; + PWM_powerground_control(0); + while (HAL_GetTick() < ttt){} + + state.current_state = STATE_ACTIVE; sm_update(); - can_handle_send_status(); + PWM_powerground_control(15); + ttt = HAL_GetTick() + 5000; + while (HAL_GetTick() < ttt){} + + //AMS_Loop(); + //sm_update(); + //sm_test_cycle_states(); + //can_handle_send_status(); } /* USER CODE END 3 */ } @@ -381,55 +394,6 @@ static void MX_TIM1_Init(void) } -/** - * @brief TIM2 Initialization Function - * @param None - * @retval None - */ -static void MX_TIM2_Init(void) -{ - - /* USER CODE BEGIN TIM2_Init 0 */ - - /* USER CODE END TIM2_Init 0 */ - - TIM_MasterConfigTypeDef sMasterConfig = {0}; - TIM_OC_InitTypeDef sConfigOC = {0}; - - /* USER CODE BEGIN TIM2_Init 1 */ - - /* USER CODE END TIM2_Init 1 */ - htim2.Instance = TIM2; - htim2.Init.Prescaler = 0; - htim2.Init.CounterMode = TIM_COUNTERMODE_UP; - htim2.Init.Period = 4294967295; - htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) - { - Error_Handler(); - } - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) - { - Error_Handler(); - } - sConfigOC.OCMode = TIM_OCMODE_PWM1; - sConfigOC.Pulse = 0; - sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; - if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN TIM2_Init 2 */ - - /* USER CODE END TIM2_Init 2 */ - HAL_TIM_MspPostInit(&htim2); - -} - /** * @brief TIM15 Initialization Function * @param None @@ -450,9 +414,9 @@ static void MX_TIM15_Init(void) /* USER CODE END TIM15_Init 1 */ htim15.Instance = TIM15; - htim15.Init.Prescaler = 0; + htim15.Init.Prescaler = 7; htim15.Init.CounterMode = TIM_COUNTERMODE_UP; - htim15.Init.Period = 65535; + htim15.Init.Period = 39999; htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim15.Init.RepetitionCounter = 0; htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; @@ -467,7 +431,7 @@ static void MX_TIM15_Init(void) Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; - sConfigOC.Pulse = 0; + sConfigOC.Pulse = 10000; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; @@ -477,6 +441,11 @@ static void MX_TIM15_Init(void) { Error_Handler(); } + sConfigOC.Pulse = 0; + if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) + { + Error_Handler(); + } sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 5360fb5..1558250 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -11,9 +11,9 @@ void sm_init(){ } void sm_update(){ - RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0]; - RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1]; - CURRENT_MEASUREMENT = module.auxVoltages[2]; + RELAY_BAT_SIDE_VOLTAGE = mV_from_ADBMS6830(module.auxVoltages[0]); + RELAY_ESC_SIDE_VOLTAGE = mV_from_ADBMS6830(module.auxVoltages[1]); + CURRENT_MEASUREMENT = mV_from_ADBMS6830(module.auxVoltages[2]); switch (state.current_state) { case STATE_INACTIVE: @@ -231,28 +231,37 @@ void sm_check_errors(){ } void sm_test_cycle_states(){ + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0]; + RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1]; + CURRENT_MEASUREMENT = module.auxVoltages[2]; + sm_set_relay_positions(state.current_state); + if (timestamp > HAL_GetTick()) return; switch (state.current_state) { case STATE_INACTIVE: state.current_state = STATE_PRECHARGE; - timestamp = HAL_GetTick() + 10000; - return; + timestamp = HAL_GetTick() + 30000; + PWM_powerground_control(0); + break; case STATE_PRECHARGE: state.current_state = STATE_READY; timestamp = HAL_GetTick() + 10000; - return; + break; case STATE_READY: state.current_state = STATE_ACTIVE; timestamp = HAL_GetTick() + 10000; - return; + break; case STATE_ACTIVE: state.current_state = STATE_DISCHARGE; timestamp = HAL_GetTick() + 10000; - return; + PWM_powerground_control(1); + break; case STATE_DISCHARGE: state.current_state = STATE_INACTIVE; timestamp = HAL_GetTick() + 10000; - return; + break; } + + state.target_state = state.current_state; } \ No newline at end of file diff --git a/Core/Src/stm32f3xx_hal_msp.c b/Core/Src/stm32f3xx_hal_msp.c index fe17f67..ed1bb30 100644 --- a/Core/Src/stm32f3xx_hal_msp.c +++ b/Core/Src/stm32f3xx_hal_msp.c @@ -59,7 +59,7 @@ /* USER CODE END 0 */ void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); - /** + /** * Initializes the Global MSP. */ void HAL_MspInit(void) @@ -305,17 +305,6 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm) /* USER CODE END TIM1_MspInit 1 */ } - else if(htim_pwm->Instance==TIM2) - { - /* USER CODE BEGIN TIM2_MspInit 0 */ - - /* USER CODE END TIM2_MspInit 0 */ - /* Peripheral clock enable */ - __HAL_RCC_TIM2_CLK_ENABLE(); - /* USER CODE BEGIN TIM2_MspInit 1 */ - - /* USER CODE END TIM2_MspInit 1 */ - } else if(htim_pwm->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspInit 0 */ @@ -353,27 +342,6 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) /* USER CODE END TIM1_MspPostInit 1 */ } - else if(htim->Instance==TIM2) - { - /* USER CODE BEGIN TIM2_MspPostInit 0 */ - - /* USER CODE END TIM2_MspPostInit 0 */ - - __HAL_RCC_GPIOA_CLK_ENABLE(); - /**TIM2 GPIO Configuration - PA3 ------> TIM2_CH4 - */ - GPIO_InitStruct.Pin = PWM_PG_FAN2_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; - HAL_GPIO_Init(PWM_PG_FAN2_GPIO_Port, &GPIO_InitStruct); - - /* USER CODE BEGIN TIM2_MspPostInit 1 */ - - /* USER CODE END TIM2_MspPostInit 1 */ - } else if(htim->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspPostInit 0 */ @@ -383,13 +351,14 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) __HAL_RCC_GPIOA_CLK_ENABLE(); /**TIM15 GPIO Configuration PA2 ------> TIM15_CH1 + PA3 ------> TIM15_CH2 */ - GPIO_InitStruct.Pin = PWM_PG_FAN1_Pin; + GPIO_InitStruct.Pin = PWM_PG_FAN1_Pin|PWM_PG_FAN2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF9_TIM15; - HAL_GPIO_Init(PWM_PG_FAN1_GPIO_Port, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN TIM15_MspPostInit 1 */ @@ -416,17 +385,6 @@ void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm) /* USER CODE END TIM1_MspDeInit 1 */ } - else if(htim_pwm->Instance==TIM2) - { - /* USER CODE BEGIN TIM2_MspDeInit 0 */ - - /* USER CODE END TIM2_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_TIM2_CLK_DISABLE(); - /* USER CODE BEGIN TIM2_MspDeInit 1 */ - - /* USER CODE END TIM2_MspDeInit 1 */ - } else if(htim_pwm->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspDeInit 0 */ diff --git a/Makefile b/Makefile index 4f8b9c9..20bcacd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri May 24 17:29:49 GMT 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Thu May 30 20:42:23 EEST 2024] ########################################################################################################################## # ------------------------------------------------ diff --git a/mvbms.ioc b/mvbms.ioc index 4c362af..e3451ef 100644 --- a/mvbms.ioc +++ b/mvbms.ioc @@ -205,7 +205,7 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM15_Init-TIM15-false-HAL-true,7-MX_USART1_UART_Init-USART1-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true,9-MX_TIM2_Init-TIM2-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_CAN_Init-CAN-false-HAL-true,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_TIM15_Init-TIM15-false-HAL-true,7-MX_USART1_UART_Init-USART1-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true RCC.ADC12outputFreq_Value=16000000 RCC.AHBFreq_Value=16000000 RCC.APB1Freq_Value=16000000 @@ -255,7 +255,10 @@ TIM1.Channel-PWM\ Generation3\ CH3N=TIM_CHANNEL_3 TIM1.IPParameters=Channel-PWM Generation3 CH3N TIM15.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM15.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 -TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2 +TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Prescaler,Period,Pulse-PWM Generation1 CH1 +TIM15.Period=39999 +TIM15.Prescaler=7 +TIM15.Pulse-PWM\ Generation1\ CH1=0 USART1.IPParameters=VirtualMode-Asynchronous USART1.VirtualMode-Asynchronous=VM_ASYNC VP_SYS_VS_Systick.Mode=SysTick From e48b87a11ecb7fcd854eea17e0757aa44abc3a3e Mon Sep 17 00:00:00 2001 From: hamza Date: Sun, 2 Jun 2024 23:06:07 +0300 Subject: [PATCH 28/38] added delay for CAN --- Core/Inc/can.h | 2 +- Core/Src/can.c | 7 +++++++ Core/Src/main.c | 8 +++++--- Core/Src/state_machine.c | 6 +++--- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Core/Inc/can.h b/Core/Inc/can.h index 8b6651f..82e2b64 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -9,7 +9,7 @@ #define CAN_ID_IN 0x501 #define CAN_ID_OUT 0x502 - +#define CAN_STATUS_FREQ 1000 void can_init(CAN_HandleTypeDef* hcan); void can_handle_send_status(); diff --git a/Core/Src/can.c b/Core/Src/can.c index 9b71da5..5b46127 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -7,9 +7,11 @@ #include "can.h" #include "ADBMS_Abstraction.h" #include "state_machine.h" +#include "stm32f3xx_hal.h" //#define CAN_ID_IN 0x501 //#define CAN_ID_OUT 0x502 +int can_delay_manager = 0; void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } /* @@ -33,6 +35,11 @@ bit 51-62: temperature of the cell with highest temperature (12 bits moved 4 bit */ void can_handle_send_status() { + if (can_delay_manager > HAL_GetTick()) + return; + else + can_delay_manager = HAL_GetTick() + CAN_STATUS_FREQ; + uint8_t data[8] = {}; data[0] = (state.current_state << 5); // save 5 bit since codes are from 0-7 61 bits left //data[1] = // in 8 bits from 0-100% diff --git a/Core/Src/main.c b/Core/Src/main.c index 07e81b8..5360001 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -134,6 +134,7 @@ int main(void) /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ + /* int ttt = HAL_GetTick() + 45000; PWM_powerground_control(0); while (HAL_GetTick() < ttt){} @@ -143,11 +144,12 @@ int main(void) PWM_powerground_control(15); ttt = HAL_GetTick() + 5000; while (HAL_GetTick() < ttt){} + */ - //AMS_Loop(); - //sm_update(); + AMS_Loop(); + sm_update(); //sm_test_cycle_states(); - //can_handle_send_status(); + can_handle_send_status(); } /* USER CODE END 3 */ } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 1558250..9b44794 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -11,9 +11,9 @@ void sm_init(){ } void sm_update(){ - RELAY_BAT_SIDE_VOLTAGE = mV_from_ADBMS6830(module.auxVoltages[0]); - RELAY_ESC_SIDE_VOLTAGE = mV_from_ADBMS6830(module.auxVoltages[1]); - CURRENT_MEASUREMENT = mV_from_ADBMS6830(module.auxVoltages[2]); + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0] * 11.989; + RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1] * 11.989; + CURRENT_MEASUREMENT = module.auxVoltages[2] ; switch (state.current_state) { case STATE_INACTIVE: From cdf15b1860d505e8df25ddbbe2fe41ca22ce4985 Mon Sep 17 00:00:00 2001 From: hamza Date: Mon, 3 Jun 2024 01:30:56 +0300 Subject: [PATCH 29/38] disabled gcc optimization --- STM32-for-VSCode.config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STM32-for-VSCode.config.yaml b/STM32-for-VSCode.config.yaml index e4488d0..1caed20 100644 --- a/STM32-for-VSCode.config.yaml +++ b/STM32-for-VSCode.config.yaml @@ -10,7 +10,7 @@ target: mvbms-test-24 # Can be C or C++ language: C -optimization: Og +optimization: O0 # MCU settings targetMCU: stm32f3x From f52abb8ef0d7ce3953b4da4d976ffa9ddb3d63db Mon Sep 17 00:00:00 2001 From: hamza Date: Mon, 3 Jun 2024 01:31:54 +0300 Subject: [PATCH 30/38] moved libraries to .h --- Core/Inc/AMS_HighLevel.h | 6 ++++++ Core/Inc/can.h | 6 +++++- Core/Src/AMS_HighLevel.c | 9 +-------- Core/Src/can.c | 18 +++++++++--------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Core/Inc/AMS_HighLevel.h b/Core/Inc/AMS_HighLevel.h index 70c6134..5ac241a 100644 --- a/Core/Inc/AMS_HighLevel.h +++ b/Core/Inc/AMS_HighLevel.h @@ -11,6 +11,12 @@ #include "ADBMS_Abstraction.h" #include "ADBMS_CMD_MAKROS.h" #include "ADBMS_LL_Driver.h" +#include "can.h" +#include "TMP1075.h" +#include "can-halal.h" +#include "errors.h" +#include "stm32f3xx_hal.h" +#include typedef enum { AMSDEACTIVE, diff --git a/Core/Inc/can.h b/Core/Inc/can.h index 82e2b64..42a0831 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -1,8 +1,12 @@ #ifndef INC_CAN_H #define INC_CAN_H -#include "stm32f3xx_hal.h" +#include #include +#include "stm32f3xx_hal.h" +#include "stm32f3xx_hal_can.h" +#include "stm32f3xx_hal_def.h" +#include "ADBMS_Abstraction.h" #include "main.h" #include "state_machine.h" #include "can-halal.h" diff --git a/Core/Src/AMS_HighLevel.c b/Core/Src/AMS_HighLevel.c index 2eeb096..0ea74de 100644 --- a/Core/Src/AMS_HighLevel.c +++ b/Core/Src/AMS_HighLevel.c @@ -6,14 +6,7 @@ */ #include "AMS_HighLevel.h" -#include "ADBMS_Abstraction.h" -#include "ADBMS_LL_Driver.h" -#include "can.h" -#include "TMP1075.h" -#include "can-halal.h" -#include "errors.h" -#include "stm32f3xx_hal.h" -#include + Cell_Module module = {}; uint32_t balancedCells = 0; diff --git a/Core/Src/can.c b/Core/Src/can.c index 5b46127..2543ddf 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -5,15 +5,14 @@ */ #include "can.h" -#include "ADBMS_Abstraction.h" -#include "state_machine.h" -#include "stm32f3xx_hal.h" //#define CAN_ID_IN 0x501 //#define CAN_ID_OUT 0x502 int can_delay_manager = 0; -void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } - +void can_init(CAN_HandleTypeDef* hcan) { + ftcan_init(hcan); + ftcan_add_filter(CAN_ID_IN, 0xFFF); +} /* This function sends the status of the mvbms, the battery and of powerground. once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR. @@ -43,8 +42,8 @@ void can_handle_send_status() { uint8_t data[8] = {}; data[0] = (state.current_state << 5); // save 5 bit since codes are from 0-7 61 bits left //data[1] = // in 8 bits from 0-100% - data[2] = mV_from_ADBMS6830(RELAY_BAT_SIDE_VOLTAGE); // Battery voltage 16 bit 45 bit - data[4] = CURRENT_MEASUREMENT; // 16 bit measurement + ftcan_marshal_unsigned(&data[2], RELAY_BAT_SIDE_VOLTAGE, 2); // Battery voltage 16 bit 45 bit + ftcan_marshal_unsigned(&data[4], CURRENT_MEASUREMENT, 2); // 16 bit measurement int8_t id = -1; int16_t temp = INT16_MIN; @@ -60,12 +59,13 @@ to the sm_handle_ams_in() which handles the state machine transition. void can_handle_recieve_command(const uint8_t *data){ if (data[0] == 0x00 && data[1] == 0x00){ sm_handle_ams_in(data); - } else if (data[0] == 0b1000000 && data[1] == 0x00){ + } else if (data[0] == 0b10000000 && data[1] == 0x00){ sm_handle_ams_in(data); - } else if (data[0] == 0b1100000 && data[1] <= 100) { + } else if (data[0] == 0b11000000 && data[1] <= 100) { sm_handle_ams_in(data); } } + /* implements the _weak method ftcan_msg_recieved_cb() which throws an interrupt when a CAN message is recieved. it only checks if the id is and datalen is correct thans hands data over to can_handle_recieve_command(). From 757165c9bff307ff0731facdd7f6e66c13fdc6ad Mon Sep 17 00:00:00 2001 From: hamza Date: Mon, 3 Jun 2024 01:32:33 +0300 Subject: [PATCH 31/38] changed variables from static to extern for use in other functions --- Core/Inc/state_machine.h | 6 +++--- Core/Src/state_machine.c | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index 716ad3b..b7726f3 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -64,9 +64,9 @@ typedef struct { extern StateHandle state; static bool relay_closed = 0; static bool precharge_closed = 0; -static int16_t RELAY_BAT_SIDE_VOLTAGE; -static int16_t RELAY_ESC_SIDE_VOLTAGE; -static int16_t CURRENT_MEASUREMENT; +extern int16_t RELAY_BAT_SIDE_VOLTAGE; +extern int16_t RELAY_ESC_SIDE_VOLTAGE; +extern int16_t CURRENT_MEASUREMENT; void sm_init(); void sm_update(); diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 9b44794..144ae14 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -2,7 +2,10 @@ #include "stm32f3xx_hal.h" StateHandle state; -static uint32_t timestamp; +int16_t RELAY_BAT_SIDE_VOLTAGE; +int16_t RELAY_ESC_SIDE_VOLTAGE; +int16_t CURRENT_MEASUREMENT; +uint32_t timestamp; void sm_init(){ state.current_state = STATE_INACTIVE; @@ -11,9 +14,9 @@ void sm_init(){ } void sm_update(){ - RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0] * 11.989; - RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1] * 11.989; - CURRENT_MEASUREMENT = module.auxVoltages[2] ; + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0] * 12.42; // the calculation says the factor is 11.989. 12.42 yields the better result + RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1] * 12.42; + CURRENT_MEASUREMENT = module.auxVoltages[2] / 2.2; switch (state.current_state) { case STATE_INACTIVE: From 8983097b877098a5b4d23f4ac8c12ebd393823ba Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 6 Jun 2024 16:46:10 +0300 Subject: [PATCH 32/38] enabled can support and cleaned up the code for it --- Core/Src/can.c | 24 +++++++++++++++--------- Core/Src/stm32f3xx_hal_msp.c | 8 ++++++++ Core/Src/stm32f3xx_it.c | 30 +++++++++++++++++++++++++++++- Makefile | 2 +- mvbms.ioc | 2 ++ 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/Core/Src/can.c b/Core/Src/can.c index 2543ddf..4ebcb4e 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -19,13 +19,14 @@ once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR. once every 0.5s in states: READY, ACTIVE. with format of: CAN Messages: -- MVBMS Status (1B), Powerground Status 0-100% (1 bit) -- Battery: SoC (1B), Pack Voltage (2B), Current (1B), - Min/Max. Cell Temp (ID, Min Temp, ID, Max Temp)(4B), - Min/Max Cell Voltage (ID, Min Voltage, ID, Max Voltage)(4B) + MVBMS Status (1B), Powerground Status 0-100% (1B) + Battery: SoC (1B), Pack Voltage (2B), Current (1B), + + Min/Max. Cell Temp (ID, Min Temp, ID, Max Temp)(3B), + Min/Max Cell Voltage (ID, Min Voltage, ID, Max Voltage)(3B) bit 0-2: status -bit 0-7: +bit 3-7: bit 8-15: State of Charge from 0-100% bit 16-31: Battery voltage bit 32-47: Current measurement @@ -48,8 +49,11 @@ void can_handle_send_status() { int8_t id = -1; int16_t temp = INT16_MIN; sm_check_cell_temps(&id, &temp); - data[6] = (id << 5) | (temp >> 4); // there are only 7 TMP1075 + data[6] = (id << 4) | (temp >> 4); // there are only 7 TMP1075 ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); + + ; + } /* @@ -59,9 +63,9 @@ to the sm_handle_ams_in() which handles the state machine transition. void can_handle_recieve_command(const uint8_t *data){ if (data[0] == 0x00 && data[1] == 0x00){ sm_handle_ams_in(data); - } else if (data[0] == 0b10000000 && data[1] == 0x00){ + } else if (data[0] == 0x01 && data[1] == 0x00){ sm_handle_ams_in(data); - } else if (data[0] == 0b11000000 && data[1] <= 100) { + } else if (data[0] == 0x02 && data[1] <= 100) { sm_handle_ams_in(data); } } @@ -69,9 +73,11 @@ void can_handle_recieve_command(const uint8_t *data){ /* implements the _weak method ftcan_msg_recieved_cb() which throws an interrupt when a CAN message is recieved. it only checks if the id is and datalen is correct thans hands data over to can_handle_recieve_command(). + +in MXCUBE under CAN NVIC settings "USB low priority or CAN_RX0 interrupts" has to be on */ void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data){ - if (id == 0x501 && datalen == 16){ + if (id == 0x501 && datalen == 2){ can_handle_recieve_command(data); } } diff --git a/Core/Src/stm32f3xx_hal_msp.c b/Core/Src/stm32f3xx_hal_msp.c index ed1bb30..16cdd64 100644 --- a/Core/Src/stm32f3xx_hal_msp.c +++ b/Core/Src/stm32f3xx_hal_msp.c @@ -108,6 +108,11 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) GPIO_InitStruct.Alternate = GPIO_AF9_CAN; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + /* CAN interrupt Init */ + HAL_NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn); + HAL_NVIC_SetPriority(CAN_RX1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(CAN_RX1_IRQn); /* USER CODE BEGIN CAN_MspInit 1 */ /* USER CODE END CAN_MspInit 1 */ @@ -137,6 +142,9 @@ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan) */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); + /* CAN interrupt DeInit */ + HAL_NVIC_DisableIRQ(USB_LP_CAN_RX0_IRQn); + HAL_NVIC_DisableIRQ(CAN_RX1_IRQn); /* USER CODE BEGIN CAN_MspDeInit 1 */ /* USER CODE END CAN_MspDeInit 1 */ diff --git a/Core/Src/stm32f3xx_it.c b/Core/Src/stm32f3xx_it.c index f45aa76..71d43b5 100644 --- a/Core/Src/stm32f3xx_it.c +++ b/Core/Src/stm32f3xx_it.c @@ -55,7 +55,7 @@ /* USER CODE END 0 */ /* External variables --------------------------------------------------------*/ - +extern CAN_HandleTypeDef hcan; /* USER CODE BEGIN EV */ /* USER CODE END EV */ @@ -198,6 +198,34 @@ void SysTick_Handler(void) /* please refer to the startup file (startup_stm32f3xx.s). */ /******************************************************************************/ +/** + * @brief This function handles USB low priority or CAN_RX0 interrupts. + */ +void USB_LP_CAN_RX0_IRQHandler(void) +{ + /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 0 */ + + /* USER CODE END USB_LP_CAN_RX0_IRQn 0 */ + HAL_CAN_IRQHandler(&hcan); + /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 1 */ + + /* USER CODE END USB_LP_CAN_RX0_IRQn 1 */ +} + +/** + * @brief This function handles CAN RX1 interrupt. + */ +void CAN_RX1_IRQHandler(void) +{ + /* USER CODE BEGIN CAN_RX1_IRQn 0 */ + + /* USER CODE END CAN_RX1_IRQn 0 */ + HAL_CAN_IRQHandler(&hcan); + /* USER CODE BEGIN CAN_RX1_IRQn 1 */ + + /* USER CODE END CAN_RX1_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ diff --git a/Makefile b/Makefile index 20bcacd..9e893cd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Thu May 30 20:42:23 EEST 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Mon Jun 03 16:11:34 EEST 2024] ########################################################################################################################## # ------------------------------------------------ diff --git a/mvbms.ioc b/mvbms.ioc index e3451ef..e918736 100644 --- a/mvbms.ioc +++ b/mvbms.ioc @@ -63,6 +63,7 @@ Mcu.UserName=STM32F302CBTx MxCube.Version=6.11.1 MxDb.Version=DB.6.0.111 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.CAN_RX1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false @@ -72,6 +73,7 @@ NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false +NVIC.USB_LP_CAN_RX0_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false PA0.GPIOParameters=PinState,GPIO_Label PA0.GPIO_Label=RELAY_EN From 5ed35b605bfab30d0b18b9aa58dade4872f7f836 Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 6 Jun 2024 16:46:57 +0300 Subject: [PATCH 33/38] changed the code for the PWM signals --- Core/Inc/PWM_control.h | 2 +- Core/Inc/common_defs.h | 4 ++-- Core/Inc/stm32f3xx_it.h | 2 ++ Core/Src/PWM_control.c | 8 ++++---- Core/Src/main.c | 17 ++--------------- Core/Src/state_machine.c | 34 ++++++++++++++++++++++------------ 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index 640b277..ebdbaf7 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -24,7 +24,7 @@ CCR: 1/20 -> 500, 2/20 -> 1000 //#define BATTERY_COOLING_FREQ 20000 -void PWM_control_init(TIM_HandleTypeDef* powerground1, TIM_HandleTypeDef* powerground2, TIM_HandleTypeDef* battery_cooling); +void PWM_control_init(TIM_HandleTypeDef* powerground, TIM_HandleTypeDef* battery_cooling); void PWM_powerground_control(uint8_t percent); void PWM_battery_cooling_control(uint8_t percent); diff --git a/Core/Inc/common_defs.h b/Core/Inc/common_defs.h index 316cb67..faff2dd 100644 --- a/Core/Inc/common_defs.h +++ b/Core/Inc/common_defs.h @@ -8,7 +8,7 @@ #ifndef INC_COMMON_DEFS_H_ #define INC_COMMON_DEFS_H_ -#define N_CELLS 14 -#define N_TEMP_SENSORS 14 +#define N_CELLS 12 +#define N_TEMP_SENSORS 12 #endif /* INC_COMMON_DEFS_H_ */ diff --git a/Core/Inc/stm32f3xx_it.h b/Core/Inc/stm32f3xx_it.h index 3c19d5a..7e2721d 100644 --- a/Core/Inc/stm32f3xx_it.h +++ b/Core/Inc/stm32f3xx_it.h @@ -55,6 +55,8 @@ void SVC_Handler(void); void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); +void USB_LP_CAN_RX0_IRQHandler(void); +void CAN_RX1_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index 364b709..ce0f317 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -12,15 +12,15 @@ TIM_HandleTypeDef* powerground, *battery_cooling; the value of the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register. */ -void PWM_control_init(TIM_HandleTypeDef* pg1, TIM_HandleTypeDef* pg2, TIM_HandleTypeDef* bat_cool){ +void PWM_control_init(TIM_HandleTypeDef* pg, TIM_HandleTypeDef* bat_cool){ powerground_status = 0; battery_cooling_status = 0; - HAL_TIM_PWM_Start(pg1, TIM_CHANNEL_1); //TIM15CH1 - HAL_TIM_PWM_Start(pg2, TIM_CHANNEL_2); //TIM15CH2 + HAL_TIM_PWM_Start(pg, TIM_CHANNEL_1); //TIM15CH1 + HAL_TIM_PWM_Start(pg, TIM_CHANNEL_2); //TIM15CH2 HAL_TIM_PWM_Start(bat_cool, TIM_CHANNEL_3); //TIM1CH3 - powerground = pg1; + powerground = pg; battery_cooling = bat_cool; __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 2000); __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000); diff --git a/Core/Src/main.c b/Core/Src/main.c index 5360001..5e91e46 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -123,7 +123,7 @@ int main(void) tmp1075_init(&hi2c1); AMS_Init(&hspi1); can_init(&hcan); - PWM_control_init(&htim15, &htim15, &htim1); + PWM_control_init(&htim15, &htim1); HAL_Delay(10); /* USER CODE END 2 */ @@ -134,18 +134,6 @@ int main(void) /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - /* - int ttt = HAL_GetTick() + 45000; - PWM_powerground_control(0); - while (HAL_GetTick() < ttt){} - - state.current_state = STATE_ACTIVE; - sm_update(); - PWM_powerground_control(15); - ttt = HAL_GetTick() + 5000; - while (HAL_GetTick() < ttt){} - */ - AMS_Loop(); sm_update(); //sm_test_cycle_states(); @@ -433,7 +421,7 @@ static void MX_TIM15_Init(void) Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; - sConfigOC.Pulse = 10000; + sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; @@ -443,7 +431,6 @@ static void MX_TIM15_Init(void) { Error_Handler(); } - sConfigOC.Pulse = 0; if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 144ae14..60856a5 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -46,6 +46,7 @@ void sm_update(){ } sm_set_relay_positions(state.current_state); + state.target_state = state.current_state; } State sm_update_inactive(){ @@ -63,9 +64,12 @@ State sm_update_precharge(){ switch (state.target_state) { case STATE_INACTIVE: // if CAN Signal 0000 0000 then immidiete shutdown return STATE_DISCHARGE; - case STATE_READY: - if (roundf(RELAY_BAT_SIDE_VOLTAGE) == roundf(RELAY_ESC_SIDE_VOLTAGE)) + case STATE_PRECHARGE: + if (RELAY_BAT_SIDE_VOLTAGE-RELAY_ESC_SIDE_VOLTAGE < 100) return STATE_READY; + break; + case STATE_DISCHARGE: + return STATE_DISCHARGE; default: return STATE_PRECHARGE; } @@ -95,8 +99,8 @@ State sm_update_active(){ State sm_update_discharge(){ switch (state.target_state) { - case STATE_INACTIVE: - if (RELAY_ESC_SIDE_VOLTAGE < 12) + case STATE_DISCHARGE: + if (RELAY_ESC_SIDE_VOLTAGE < 5000) return STATE_INACTIVE; case STATE_PRECHARGE: // if CAN Signal 1000 0000 then get ready return STATE_PRECHARGE; @@ -136,7 +140,7 @@ State sm_update_error(){ } void sm_set_relay_positions(State current_state){ - switch (state.target_state) { + switch (state.current_state) { case STATE_INACTIVE: sm_set_relay(RELAY_MAIN, 0); sm_set_relay(RELAY_PRECHARGE, 0); @@ -152,13 +156,18 @@ void sm_set_relay_positions(State current_state){ case STATE_ACTIVE: sm_set_relay(RELAY_MAIN, 1); sm_set_relay(RELAY_PRECHARGE, 0); + break; case STATE_DISCHARGE: sm_set_relay(RELAY_MAIN, 0); sm_set_relay(RELAY_PRECHARGE, 0); break; + case STATE_CHARGING_PRECHARGE: + sm_set_relay(RELAY_MAIN, 0); + sm_set_relay(RELAY_PRECHARGE, 1); + break; case STATE_CHARGING: sm_set_relay(RELAY_MAIN, 1); - sm_set_relay(RELAY_PRECHARGE, 1); + sm_set_relay(RELAY_PRECHARGE, 0); break; case STATE_ERROR: sm_set_relay(RELAY_MAIN, 0); @@ -181,7 +190,6 @@ void sm_set_relay(Relay relay, bool closed){ } } - void sm_check_charging(){ if (RELAY_BAT_SIDE_VOLTAGE < RELAY_ESC_SIDE_VOLTAGE && timestamp == 0) timestamp = HAL_GetTick() + 5000; @@ -199,13 +207,13 @@ void sm_check_cell_temps(int8_t *id, int16_t *temp){ void sm_handle_ams_in(const uint8_t *data){ switch (data[0]) { - case 0b00000000: + case 0x00: if (state.current_state != STATE_INACTIVE){ PWM_powerground_control(0); state.target_state = STATE_DISCHARGE; } break; - case 0b10000000: + case 0x01: if (state.target_state == STATE_INACTIVE || state.target_state == STATE_DISCHARGE){ PWM_powerground_control(0); state.target_state = STATE_PRECHARGE; @@ -214,9 +222,11 @@ void sm_handle_ams_in(const uint8_t *data){ state.target_state = STATE_READY; } break; - case 0b11000000: - PWM_powerground_control(data[1]); - state.target_state = STATE_ACTIVE; // READY -> ACTIVE + case 0x02: + if (state.current_state == STATE_READY){ + PWM_powerground_control(data[1]); + state.target_state = STATE_ACTIVE; // READY -> ACTIVE + } break; } } From d3ec4a7f2faa5efbfbc85d523e625d001e71a744 Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 6 Jun 2024 23:12:15 +0300 Subject: [PATCH 34/38] - modified the CAN class with a specific messages - add documentation --- Core/Inc/can.h | 8 ++-- Core/Src/can.c | 113 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 98 insertions(+), 23 deletions(-) diff --git a/Core/Inc/can.h b/Core/Inc/can.h index 42a0831..ee48dec 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -1,15 +1,13 @@ #ifndef INC_CAN_H #define INC_CAN_H -#include -#include #include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_can.h" -#include "stm32f3xx_hal_def.h" #include "ADBMS_Abstraction.h" #include "main.h" -#include "state_machine.h" #include "can-halal.h" +#include "AMS_HighLevel.h" +#include "state_machine.h" +#include #define CAN_ID_IN 0x501 #define CAN_ID_OUT 0x502 diff --git a/Core/Src/can.c b/Core/Src/can.c index 4ebcb4e..7a5f6af 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -13,25 +13,43 @@ void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); ftcan_add_filter(CAN_ID_IN, 0xFFF); } + /* This function sends the status of the mvbms, the battery and of powerground. once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR. once every 0.5s in states: READY, ACTIVE. with format of: CAN Messages: - MVBMS Status (1B), Powerground Status 0-100% (1B) - Battery: SoC (1B), Pack Voltage (2B), Current (1B), + Error bit + MVBMS state + Powerground Status 0-100% + Errors + Battery state of charge + Pack Voltage + Current + Battery temperature (12 bit) Min/Max. Cell Temp (ID, Min Temp, ID, Max Temp)(3B), Min/Max Cell Voltage (ID, Min Voltage, ID, Max Voltage)(3B) -bit 0-2: status -bit 3-7: -bit 8-15: State of Charge from 0-100% -bit 16-31: Battery voltage -bit 32-47: Current measurement -bit 48-50: id of cell with highest temperature -bit 51-62: temperature of the cell with highest temperature (12 bits moved 4 bit to the left) +bit 0 (1b): empty +bit 1-3 (3b): state +bit 4-11 (8b): powerground status +bit 12-19 (8b): error +bit 20-27 (8b): state of charge from 0-100% +bit 28-39 (12b): battery voltage +bit 40-51 (12b): current measurement +bit 52-63 (12b): temperature of the cell with highest temperature + + +bit 0-3 (4b): ID of the sensor with highest temperature +bit 4-7 (4b): ID of the sensor with lowest temperataure +bit 8-19 (12b): temperature of the coldest cell +bit 20-23 (4b): ID of the cell with the lowest voltage +bit 24-35 (12b): lowest cell voltage +bit 36-39 (4b): ID of the cell the the highest voltage +bit 40-51 (12b): highest cell voltage +bit 52-63 (12b): empty */ void can_handle_send_status() { @@ -41,24 +59,83 @@ void can_handle_send_status() { can_delay_manager = HAL_GetTick() + CAN_STATUS_FREQ; uint8_t data[8] = {}; - data[0] = (state.current_state << 5); // save 5 bit since codes are from 0-7 61 bits left - //data[1] = // in 8 bits from 0-100% - ftcan_marshal_unsigned(&data[2], RELAY_BAT_SIDE_VOLTAGE, 2); // Battery voltage 16 bit 45 bit - ftcan_marshal_unsigned(&data[4], CURRENT_MEASUREMENT, 2); // 16 bit measurement + int8_t id_highest_temp = -1; + int16_t highest_temp = INT16_MIN; + sm_check_battery_temperature(&id_highest_temp, &highest_temp); + + data[0] = ((state.current_state << 4) | (powerground_status >> 4)); // 1 bit emptyy | 3 bit state | 4 bit powerground + data[1] = ((powerground_status << 4) | (state.error_source >> 4)); // 4 bit powerground | 4 bit error + data[2] = ((state.error_source << 4) | (0)); // 4 bit error | 4 bit state of charge + data[3] = ((0) + (RELAY_BAT_SIDE_VOLTAGE >> 12)); // 4 bit state of charge | 4 bit battery voltage + data[4] = ((RELAY_BAT_SIDE_VOLTAGE >> 4)); + data[5] = ((CURRENT_MEASUREMENT >> 8)); + data[6] = ((CURRENT_MEASUREMENT & 0x00F0) | (highest_temp >> 12)); + data[7] = ((highest_temp) >> 4); - int8_t id = -1; - int16_t temp = INT16_MIN; - sm_check_cell_temps(&id, &temp); - data[6] = (id << 4) | (temp >> 4); // there are only 7 TMP1075 ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); - ; + int8_t id_lowest_temp = -1; + int16_t lowest_temp = INT16_MIN; + for (int i = 0; i < N_TEMP_SENSORS; i++) { + if (tmp1075_temps[i] < lowest_temp){ + id_lowest_temp = i; + lowest_temp = tmp1075_temps[i]; + } + } + + int8_t id_lowest_volt = -1; + int16_t lowest_volt = INT16_MIN; + int8_t id_highest_volt = -1; + int16_t highest_volt = INT16_MIN; + + for (int i = 0; i < module.sumOfCellMeasurements; i++) { + if (sm_return_cell_voltage(i) < lowest_temp){ + id_lowest_volt = i; + lowest_volt = sm_return_cell_voltage(i); + } + if (sm_return_cell_voltage(i) > highest_temp){ + id_highest_volt = i; + highest_volt = sm_return_cell_voltage(i); + } + } + + data[0] = ((id_highest_temp & 0x0F) << 4 | (id_lowest_temp & 0x0F)); + data[1] = ((lowest_temp) >> 8); + data[2] = ((lowest_temp & 0x00F0) | (id_lowest_volt & 0x0F)); + data[3] = (lowest_volt >> 8); + data[4] = ((lowest_volt & 0x00F0) | (id_highest_volt & 0x0F)); + data[5] = ((highest_volt >> 8)); + data[6] = ((highest_volt & 0x00F0)); + data[7] = 0; + ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); } +/* +bit 0-3 (4b): ID of the sensor with highest temperature +bit 4-7 (4b): ID of the sensor with lowest temperataure +bit 8-19 (12b): temperature of the coldest cell +bit 20-23 (4b): ID of the cell with the lowest voltage +bit 24-35 (12b): lowest cell voltage +bit 36-39 (4b): ID of the cell the the highest voltage +bit 40-51 (12b): highest cell voltage +bit 52-63 (12b): empty +*/ /* can_handle_recieve_command() should only check if the message is valid and then hand it to the sm_handle_ams_in() which handles the state machine transition. + +This function recieves a command from the Autobox with the CAN ID of 0x501. +with format of: +data[0] = target state + 0x0 STATE_INACTIVE | disconnect power to the ESC of powerground. Send it to return the mvbms to idle/monitoring mode. If data[1] != 0 -> assume bad CAN message. + 0x1 STATE_READY | conneect power to the ESC of powerground and but with no PWM signal. If data[1] != 0 -> assume bad CAN message. + 0x2 STATE_ACTIVE | activate powerground at (data[1]) percent. If data[1] > 100 -> assume bad CAN message. + +allowed transitions: + STATE_INACTIVE -> STATE_READY + STATE_READY -> STATE_INACTIVE, STATE_ACTIVE + STATE_ACTIVE -> STATE_INACTIVE, STATE_READY */ void can_handle_recieve_command(const uint8_t *data){ if (data[0] == 0x00 && data[1] == 0x00){ From b4ee2b1b3b570f17ca04f9ef8f0b4ec2d6466c96 Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 6 Jun 2024 23:13:11 +0300 Subject: [PATCH 35/38] modified includes --- Core/Inc/PWM_control.h | 1 + Core/Src/PWM_control.c | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index ebdbaf7..d88cdf8 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -3,6 +3,7 @@ #include "stm32f3xx_hal.h" #include "ADBMS_LL_Driver.h" +#include "state_machine.h" #include #include "main.h" diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index ce0f317..09adf92 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -1,7 +1,5 @@ #include "PWM_control.h" -#include "stm32f3xx_hal_tim.h" -uint8_t powerground_status; uint8_t battery_cooling_status; //uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR; From 48854dc80afe25e6feb947bac5605a038da8d4ee Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 6 Jun 2024 23:13:55 +0300 Subject: [PATCH 36/38] added sm_check_errors --- Core/Inc/state_machine.h | 23 +++++++++-------- Core/Src/state_machine.c | 55 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index b7726f3..b08e0ce 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -5,6 +5,7 @@ #include #include "ADBMS_LL_Driver.h" #include "AMS_HighLevel.h" +#include "errors.h" #include "PWM_control.h" #include "TMP1075.h" #include @@ -23,6 +24,7 @@ // Time to wait between closing relays #define RELAY_CLOSE_WAIT 10 // ms +#warning typedef enum { // states -> 3 bit. valid transitions: (all could transition to error) STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR STATE_PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR @@ -35,21 +37,16 @@ typedef enum { // states -> 3 bit. valid transitions: (all could t } State; typedef struct { - uint16_t bms_timeout : 1; - uint16_t bms_overtemp : 1; - uint16_t bms_fault : 1; + uint16_t bms_timeout : 1; + uint16_t bms_fault : 1; uint16_t temperature_error : 1; uint16_t current_error : 1; - uint16_t voltage_error : 1; - - uint16_t temperature_sensor_missing : 1; uint16_t current_sensor_missing : 1; + uint16_t voltage_error : 1; uint16_t voltage_missing : 1; - uint16_t battery_missing : 1; - uint16_t relay_missing : 1; - uint16_t state_transition_fail : 1; + } ErrorKind; //typedef enum {} WarningKind; @@ -67,6 +64,8 @@ static bool precharge_closed = 0; extern int16_t RELAY_BAT_SIDE_VOLTAGE; extern int16_t RELAY_ESC_SIDE_VOLTAGE; extern int16_t CURRENT_MEASUREMENT; +extern uint8_t powerground_status; + void sm_init(); void sm_update(); @@ -84,12 +83,14 @@ typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; void sm_set_relay_positions(State state); void sm_set_relay(Relay relay, bool closed); void sm_check_charging(); -void sm_check_cell_temps(int8_t* id, int16_t* temp); +void sm_check_battery_temperature(int8_t* id, int16_t* temp); + +int16_t sm_return_cell_temperature(int id); +int16_t sm_return_cell_voltage(int id); void sm_handle_ams_in(const uint8 *data); void sm_check_errors(); void sm_set_error(ErrorKind error_kind, bool is_errored); - void sm_test_cycle_states(); #endif /* "INC_STATE_MACHINE_H" */ \ No newline at end of file diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 60856a5..aacb4d2 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -1,10 +1,16 @@ #include "state_machine.h" +#include "AMS_HighLevel.h" +#include "TMP1075.h" +#include "errors.h" #include "stm32f3xx_hal.h" +#include StateHandle state; int16_t RELAY_BAT_SIDE_VOLTAGE; int16_t RELAY_ESC_SIDE_VOLTAGE; int16_t CURRENT_MEASUREMENT; +uint8_t powerground_status; + uint32_t timestamp; void sm_init(){ @@ -14,6 +20,8 @@ void sm_init(){ } void sm_update(){ + sm_check_errors(); + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0] * 12.42; // the calculation says the factor is 11.989. 12.42 yields the better result RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1] * 12.42; CURRENT_MEASUREMENT = module.auxVoltages[2] / 2.2; @@ -197,14 +205,24 @@ void sm_check_charging(){ state.target_state = STATE_CHARGING_PRECHARGE; } -void sm_check_cell_temps(int8_t *id, int16_t *temp){ +/* returns the ID and temperature of the hottest cell */ +void sm_check_battery_temperature(int8_t *id, int16_t *temp){ for (int i = 0; i < N_TEMP_SENSORS; i++) { if (tmp1075_temps[i] > *temp){ *id = i; + *temp = tmp1075_temps[i]; } } } +int16_t sm_return_cell_temperature(int id){ + return tmp1075_temps[id]; +} + +int16_t sm_return_cell_voltage(int id){ + return module.cellVoltages[id]; +} + void sm_handle_ams_in(const uint8_t *data){ switch (data[0]) { case 0x00: @@ -235,11 +253,38 @@ void sm_set_error(ErrorKind error_kind, bool is_errored){} #warning TODO: add error checking for everything here void sm_check_errors(){ - if (module.status.THSD == 1) { - state.error_type.bms_overtemp = 1; + switch (error_data.data_kind) { + case SEK_OVERTEMP: + case SEK_UNDERTEMP: + case SEK_TOO_FEW_TEMPS: + state.error_type.temperature_error = 1; + case SEK_OVERVOLT: + case SEK_UNDERVOLT: + case SEK_OPENWIRE: + case SEK_EEPROM_ERR: + case SEK_INTERNAL_BMS_TIMEOUT: + state.error_type.bms_timeout = 1; + case SEK_INTERNAL_BMS_CHECKSUM_FAIL: + case SEK_INTERNAL_BMS_OVERTEMP: + case SEK_INTERNAL_BMS_FAULT: + state.error_type.bms_fault = 1; + break; } - if (RELAY_BAT_SIDE_VOLTAGE < 40){ - state.error_source = (1 << 10); + + if (1){ + state.error_type.current_error = 1; + } + + if (1){ + state.error_type.current_sensor_missing = 1; + } + + if (RELAY_BAT_SIDE_VOLTAGE < 30000){ + state.error_type.voltage_error = 1; + } + + if (1){ + state.error_type.voltage_missing = 1; } } From 5213a4a2e0ff574113b195bdb9294567858e9f96 Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 7 Jun 2024 00:42:34 +0300 Subject: [PATCH 37/38] removed a comment --- Core/Src/can.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Core/Src/can.c b/Core/Src/can.c index 7a5f6af..0e4aebe 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -110,16 +110,6 @@ void can_handle_send_status() { ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); } -/* -bit 0-3 (4b): ID of the sensor with highest temperature -bit 4-7 (4b): ID of the sensor with lowest temperataure -bit 8-19 (12b): temperature of the coldest cell -bit 20-23 (4b): ID of the cell with the lowest voltage -bit 24-35 (12b): lowest cell voltage -bit 36-39 (4b): ID of the cell the the highest voltage -bit 40-51 (12b): highest cell voltage -bit 52-63 (12b): empty -*/ /* can_handle_recieve_command() should only check if the message is valid and then hand it From 7a4494f5efeeeadb29794b02fd52f0d7e7036b45 Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 7 Jun 2024 00:43:00 +0300 Subject: [PATCH 38/38] addeed the correct scaling for the current sensor --- Core/Src/state_machine.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index aacb4d2..7008496 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -4,6 +4,7 @@ #include "errors.h" #include "stm32f3xx_hal.h" #include +#include StateHandle state; int16_t RELAY_BAT_SIDE_VOLTAGE; @@ -24,7 +25,7 @@ void sm_update(){ RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0] * 12.42; // the calculation says the factor is 11.989. 12.42 yields the better result RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1] * 12.42; - CURRENT_MEASUREMENT = module.auxVoltages[2] / 2.2; + CURRENT_MEASUREMENT = (module.auxVoltages[2] - 2496) * 300; switch (state.current_state) { case STATE_INACTIVE: