/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
*
© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TIMEBASE 1000
#define PWM_MINIMUM 20
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
const uint16_t GPU_STAGES[] = {900, 1400, 1800, 2200};
const uint16_t CPU_STAGES[] = {850, 900, 1000, 1200};
uint32_t freq_count_gpu;
uint32_t freq_count_cpu;
uint32_t freq_count_case1;
uint32_t freq_count_case2;
uint32_t last_gpu;
uint32_t last_cpu;
uint32_t last_case1;
uint32_t last_case2;
uint16_t rpm_gpu;
uint16_t rpm_cpu;
uint16_t rpm_case1;
uint16_t rpm_case2;
uint32_t pwm_fan1;
uint32_t pwm_fan2;
uint8_t gpu_stage;
uint8_t cpu_stage;
uint8_t current_stage;
char msg[256];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int max(uint8_t a, uint8_t b) {
if (a > b) {
return a;
}
return b;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (HAL_GPIO_ReadPin(Switch_GPIO_Port, Switch_Pin) == GPIO_PIN_SET) {
// do stuff
}
if (rpm_gpu < GPU_STAGES[0]) {
gpu_stage = 1;
} else if (rpm_gpu < GPU_STAGES[1]) {
gpu_stage = 2;
} else if (rpm_gpu < GPU_STAGES[2]) {
gpu_stage = 3;
} else if (rpm_gpu < GPU_STAGES[3]) {
gpu_stage = 4;
} else {
gpu_stage = 5;
}
if (rpm_cpu < CPU_STAGES[0]) {
cpu_stage = 1;
} else if (rpm_cpu < CPU_STAGES[1]) {
cpu_stage = 2;
} else if (rpm_cpu < CPU_STAGES[2]) {
cpu_stage = 3;
} else if (rpm_cpu < CPU_STAGES[3]) {
cpu_stage = 4;
} else {
cpu_stage = 5;
}
current_stage = max(gpu_stage, cpu_stage);
if (current_stage == 1) {
pwm_fan1 = PWM_MINIMUM;
} else if (current_stage == 2) {
pwm_fan1 = 35;
} else if (current_stage == 3) {
pwm_fan1 = 50;
} else if (current_stage == 4) {
pwm_fan1 = 80;
} else {
pwm_fan1 = 100;
}
// For now
pwm_fan2 = pwm_fan1;
// Write the value after converting from duty cycle to raw value.
TIM2->CCR1 = htim2.Init.Period * pwm_fan1 / 100;
TIM2->CCR2 = htim2.Init.Period * pwm_fan2 / 100;
// If the fans don't spin, the rpm won't be updated as no
// interrupts are called. This resets those values every two seconds.
if (HAL_GetTick() - last_gpu >= TIMEBASE * 2) {
rpm_gpu = 0;
freq_count_gpu = 0;
}
if (HAL_GetTick() - last_cpu >= TIMEBASE * 2) {
rpm_cpu = 0;
freq_count_cpu = 0;
}
if (HAL_GetTick() - last_case1 >= TIMEBASE * 2) {
rpm_case1 = 0;
freq_count_case1 = 0;
}
if (HAL_GetTick() - last_case2 >= TIMEBASE * 2) {
rpm_case2 = 0;
freq_count_case2 = 0;
}
sprintf(msg, "GPU: %u RPM\nCPU: %u RPM\nCase 1: %u RPM\nCase2: %u RPM\n", rpm_gpu, rpm_cpu, rpm_case1, rpm_case2);
HAL_UART_Transmit(&huart2, (uint8_t *) msg, strlen(msg), 100);
sprintf(msg, "PWM Case 1: %lu %%\nPWM Case 2: %lu %%\n\n", pwm_fan1, pwm_fan2);
HAL_UART_Transmit(&huart2, (uint8_t *) msg, strlen(msg), 100);
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @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_ClockConfigTypeDef sClockSourceConfig = {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 = 640;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
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_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&htim2);
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pins : Case1_Pin Case2_Pin CPU_Pin GPU_Pin */
GPIO_InitStruct.Pin = Case1_Pin|Case2_Pin|CPU_Pin|GPU_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : Switch_Pin */
GPIO_InitStruct.Pin = Switch_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(Switch_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == GPU_Pin) {
if ((HAL_GetTick() - last_gpu) <= TIMEBASE) {
freq_count_gpu++;
} else {
rpm_gpu = freq_count_gpu * 60 / 2;
last_gpu = HAL_GetTick();
freq_count_gpu = 0;
}
}
if (GPIO_Pin == CPU_Pin) {
if ((HAL_GetTick() - last_cpu) <= TIMEBASE) {
freq_count_cpu++;
} else {
rpm_cpu = freq_count_cpu * 60 / 2;
last_cpu = HAL_GetTick();
freq_count_cpu = 0;
}
}
if (GPIO_Pin == Case1_Pin) {
if ((HAL_GetTick() - last_case1) <= TIMEBASE) {
freq_count_case1++;
} else {
rpm_case1 = freq_count_case1 * 60 / 2;
last_case1 = HAL_GetTick();
freq_count_case1 = 0;
}
}
if (GPIO_Pin == Case2_Pin) {
if ((HAL_GetTick() - last_case2) <= TIMEBASE) {
freq_count_case2++;
} else {
rpm_case2 = freq_count_case2 * 60 / 2;
last_case2 = HAL_GetTick();
freq_count_case2 = 0;
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/