From 705b9da8218db98ca03eafe13ceb5d646942941e Mon Sep 17 00:00:00 2001 From: jazzpi Date: Tue, 14 Jun 2022 00:44:46 +0200 Subject: [PATCH] Implement fan PWM --- Core/Inc/FanControl.h | 15 +++++++ Core/Inc/main.h | 2 + Core/Inc/stm32f4xx_hal_conf.h | 2 +- Core/Src/FanControl.c | 27 +++++++++++++ Core/Src/main.c | 60 ++++++++++++++++++++++++---- Core/Src/stm32f4xx_hal_msp.c | 74 ++++++++++++++++++++++++++++++++++- Makefile | 2 +- STM32Make.make | 1 + ams-slave.ioc | 17 +++++--- 9 files changed, 183 insertions(+), 17 deletions(-) create mode 100644 Core/Inc/FanControl.h create mode 100644 Core/Src/FanControl.c diff --git a/Core/Inc/FanControl.h b/Core/Inc/FanControl.h new file mode 100644 index 0000000..6d0eef0 --- /dev/null +++ b/Core/Inc/FanControl.h @@ -0,0 +1,15 @@ +#ifndef INC_FAN_CONTROL_H +#define INC_FAN_CONTROL_H + +#include "stm32f4xx_hal.h" +#include "stm32f4xx_hal_def.h" +#include "stm32f4xx_hal_tim.h" + +HAL_StatusTypeDef fan_ctrl_init(TIM_HandleTypeDef* handle, uint32_t channel); + +/** + * @brief Set the power of the fan (0 -> off, 100 -> full power). + */ +void fan_ctrl_set_power(uint32_t percent); + +#endif // INC_FAN_CONTROL_H diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 6412d63..4d993bd 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -48,6 +48,8 @@ extern "C" { #define MAIN_LOOP_PERIOD 100 /* ms */ /* USER CODE END EM */ +void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim); + /* Exported functions prototypes ---------------------------------------------*/ void Error_Handler(void); diff --git a/Core/Inc/stm32f4xx_hal_conf.h b/Core/Inc/stm32f4xx_hal_conf.h index 6e3313b..d6e618d 100644 --- a/Core/Inc/stm32f4xx_hal_conf.h +++ b/Core/Inc/stm32f4xx_hal_conf.h @@ -63,7 +63,7 @@ /* #define HAL_SD_MODULE_ENABLED */ /* #define HAL_MMC_MODULE_ENABLED */ /* #define HAL_SPI_MODULE_ENABLED */ -/* #define HAL_TIM_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED #define HAL_UART_MODULE_ENABLED /* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */ diff --git a/Core/Src/FanControl.c b/Core/Src/FanControl.c new file mode 100644 index 0000000..7258526 --- /dev/null +++ b/Core/Src/FanControl.c @@ -0,0 +1,27 @@ +#include "FanControl.h" + +#include "stm32f4xx_hal_def.h" +#include "stm32f4xx_hal_tim.h" + +TIM_HandleTypeDef* timer; +uint32_t channel; + +HAL_StatusTypeDef fan_ctrl_init(TIM_HandleTypeDef* handle, uint32_t channel_) { + timer = handle; + channel = channel_; + + return HAL_TIM_PWM_Start(timer, channel); +} + +void fan_ctrl_set_power(uint32_t percent) { + if (percent > 100) { + percent = 100; + } + + // The PWM signal switches a low side switch, so we need to invert the duty + // cycle. + percent = 100 - percent; + + uint32_t counter = timer->Init.Period * percent / 100; + __HAL_TIM_SET_COMPARE(timer, channel, counter); +} diff --git a/Core/Src/main.c b/Core/Src/main.c index c4b6e45..4bb7c76 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -23,9 +23,11 @@ /* USER CODE BEGIN Includes */ #include "BQ_Abstraction_Layer.h" #include "EEPROM.h" +#include "FanControl.h" #include "stm32f4xx_hal.h" #include "stm32f4xx_hal_gpio.h" +#include "stm32f4xx_hal_tim.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -48,6 +50,8 @@ CAN_HandleTypeDef hcan2; I2C_HandleTypeDef hi2c1; +TIM_HandleTypeDef htim3; + UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; UART_HandleTypeDef huart3; @@ -67,6 +71,7 @@ static void MX_USART1_UART_Init(void); static void MX_USART2_UART_Init(void); static void MX_USART3_UART_Init(void); static void MX_USART6_UART_Init(void); +static void MX_TIM3_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ @@ -136,6 +141,7 @@ int main(void) { MX_USART2_UART_Init(); MX_USART3_UART_Init(); MX_USART6_UART_Init(); + MX_TIM3_Init(); /* USER CODE BEGIN 2 */ HAL_GPIO_WritePin(DCDC_CTRL_GPIO_Port, DCDC_CTRL_Pin, GPIO_PIN_SET); HAL_Delay(100); @@ -145,6 +151,7 @@ int main(void) { if (eeprom_read_random(EEPROM_ADDR_SLAVE_ID, &id) == HAL_OK) { HAL_GPIO_WritePin(STAT_LED4_GPIO_Port, STAT_LED4_Pin, GPIO_PIN_SET); } + fan_ctrl_init(&htim3, TIM_CHANNEL_4); /* USER CODE END 2 */ /* Infinite loop */ @@ -156,6 +163,7 @@ int main(void) { update_status_leds(); afe_measure(); delay_period(); + fan_ctrl_set_power((HAL_GetTick() / 100) % 100); } /* USER CODE END 3 */ } @@ -297,6 +305,50 @@ static void MX_I2C1_Init(void) { /* USER CODE END I2C1_Init 2 */ } +/** + * @brief TIM3 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM3_Init(void) { + + /* USER CODE BEGIN TIM3_Init 0 */ + + /* USER CODE END TIM3_Init 0 */ + + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + + /* USER CODE BEGIN TIM3_Init 1 */ + + /* USER CODE END TIM3_Init 1 */ + htim3.Instance = TIM3; + htim3.Init.Prescaler = 0; + htim3.Init.CounterMode = TIM_COUNTERMODE_UP; + htim3.Init.Period = 500; + htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_PWM_Init(&htim3) != HAL_OK) { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &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(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN TIM3_Init 2 */ + + /* USER CODE END TIM3_Init 2 */ + HAL_TIM_MspPostInit(&htim3); +} + /** * @brief USART1 Initialization Function * @param None @@ -459,14 +511,6 @@ static void MX_GPIO_Init(void) { GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(BQ_FAULT_N_GPIO_Port, &GPIO_InitStruct); - - /*Configure GPIO pin : FAN_PWM_Pin */ - GPIO_InitStruct.Pin = FAN_PWM_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; - HAL_GPIO_Init(FAN_PWM_GPIO_Port, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ diff --git a/Core/Src/stm32f4xx_hal_msp.c b/Core/Src/stm32f4xx_hal_msp.c index dcb5876..76660ad 100644 --- a/Core/Src/stm32f4xx_hal_msp.c +++ b/Core/Src/stm32f4xx_hal_msp.c @@ -57,7 +57,9 @@ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ -/** + +void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); + /** * Initializes the Global MSP. */ void HAL_MspInit(void) @@ -289,6 +291,76 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c) } +/** +* @brief TIM_PWM MSP Initialization +* This function configures the hardware resources used in this example +* @param htim_pwm: TIM_PWM handle pointer +* @retval None +*/ +void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm) +{ + if(htim_pwm->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspInit 0 */ + + /* USER CODE END TIM3_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_TIM3_CLK_ENABLE(); + /* USER CODE BEGIN TIM3_MspInit 1 */ + + /* USER CODE END TIM3_MspInit 1 */ + } + +} + +void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(htim->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspPostInit 0 */ + + /* USER CODE END TIM3_MspPostInit 0 */ + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**TIM3 GPIO Configuration + PC9 ------> TIM3_CH4 + */ + GPIO_InitStruct.Pin = FAN_PWM_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; + HAL_GPIO_Init(FAN_PWM_GPIO_Port, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM3_MspPostInit 1 */ + + /* USER CODE END TIM3_MspPostInit 1 */ + } + +} +/** +* @brief TIM_PWM MSP De-Initialization +* This function freeze the hardware resources used in this example +* @param htim_pwm: TIM_PWM handle pointer +* @retval None +*/ +void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm) +{ + if(htim_pwm->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspDeInit 0 */ + + /* USER CODE END TIM3_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM3_CLK_DISABLE(); + /* USER CODE BEGIN TIM3_MspDeInit 1 */ + + /* USER CODE END TIM3_MspDeInit 1 */ + } + +} + /** * @brief UART MSP Initialization * This function configures the hardware resources used in this example diff --git a/Makefile b/Makefile index 7c8cf52..43caf09 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Sun Jun 12 16:56:47 CEST 2022] +# File automatically-generated by tool: [projectgenerator] version: [3.16.0] date: [Mon Jun 13 23:59:18 CEST 2022] ########################################################################################################################## # ------------------------------------------------ diff --git a/STM32Make.make b/STM32Make.make index 239b573..350ab2a 100644 --- a/STM32Make.make +++ b/STM32Make.make @@ -41,6 +41,7 @@ Core/Src/BQ_Abstraction_Layer.c \ Core/Src/BQ_Communication.c \ Core/Src/BatteryManagement.c \ Core/Src/EEPROM.c \ +Core/Src/FanControl.c \ Core/Src/SoftI2C.c \ Core/Src/TMP144.c \ Core/Src/main.c \ diff --git a/ams-slave.ioc b/ams-slave.ioc index 0141ebf..ad0d993 100644 --- a/ams-slave.ioc +++ b/ams-slave.ioc @@ -21,15 +21,16 @@ Mcu.CPN=STM32F412RET6 Mcu.Family=STM32F4 Mcu.IP0=CAN1 Mcu.IP1=CAN2 +Mcu.IP10=USART6 Mcu.IP2=I2C1 Mcu.IP3=NVIC Mcu.IP4=RCC Mcu.IP5=SYS -Mcu.IP6=USART1 -Mcu.IP7=USART2 -Mcu.IP8=USART3 -Mcu.IP9=USART6 -Mcu.IPNb=10 +Mcu.IP6=TIM3 +Mcu.IP7=USART1 +Mcu.IP8=USART2 +Mcu.IP9=USART3 +Mcu.IPNb=11 Mcu.Name=STM32F412R(E-G)Tx Mcu.Package=LQFP64 Mcu.Pin0=PC0 @@ -205,8 +206,12 @@ RCC.VCOI2SInputFreq_Value=1000000 RCC.VCOI2SOutputFreq_Value=192000000 RCC.VCOInputFreq_Value=2000000 RCC.VCOOutputFreq_Value=100000000 -SH.S_TIM3_CH4.0=TIM3_CH4 +SH.S_TIM3_CH4.0=TIM3_CH4,PWM Generation4 CH4 SH.S_TIM3_CH4.ConfNb=1 +TIM3.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 +TIM3.IPParameters=Channel-PWM Generation4 CH4,Period,Pulse-PWM Generation4 CH4 +TIM3.Period=500 +TIM3.Pulse-PWM\ Generation4\ CH4=0 USART1.BaudRate=115200 USART1.IPParameters=VirtualMode,BaudRate USART1.VirtualMode=VM_ASYNC