From 8983097b877098a5b4d23f4ac8c12ebd393823ba Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 6 Jun 2024 16:46:10 +0300 Subject: [PATCH] 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