174 lines
5.2 KiB
C++
174 lines
5.2 KiB
C++
/*
|
|
* FanControl.cpp
|
|
*
|
|
* Created on: 27.07.2023
|
|
* Author: Carst
|
|
*/
|
|
|
|
#include <stm32g0xx_ll_bus.h>
|
|
#include <stm32g0xx_ll_gpio.h>
|
|
#include <stm32g0xx_ll_tim.h>
|
|
#include "FanControl.hpp"
|
|
|
|
#define TIMER_RELOAD_VALUE 2559UL
|
|
#define DUTY_TO_TIM_VALUE(x) TIMER_RELOAD_VALUE * (x) / 100U
|
|
|
|
static std::uint32_t fan_speed;
|
|
static std::uint32_t duty;
|
|
extern "C" void TIM14_IRQHandler(void)
|
|
{
|
|
LL_TIM_ClearFlag_UPDATE(TIM14);
|
|
|
|
fan_speed = LL_TIM_GetCounter(TIM2);
|
|
LL_TIM_SetCounter(TIM2, 0UL);
|
|
}
|
|
extern "C" void FanControl_SetDuty(uint32_t d)
|
|
{
|
|
duty = DUTY_TO_TIM_VALUE(d);
|
|
}
|
|
namespace ElektronischeLast
|
|
{
|
|
static const std::uint16_t stuetzpunkte [5][2] =
|
|
{
|
|
{ 25U, DUTY_TO_TIM_VALUE(0U) },
|
|
{ 40U, DUTY_TO_TIM_VALUE(30U) },
|
|
{ 50U, DUTY_TO_TIM_VALUE(50U) },
|
|
{ 65U, DUTY_TO_TIM_VALUE(75U) },
|
|
{ 80U, DUTY_TO_TIM_VALUE(100U) }
|
|
};
|
|
|
|
FanControl::FanControl(void)
|
|
{
|
|
/* TIM3 PWM-Output
|
|
* 25kHz (Vorgabe Intel). Geschwindigkeit wird über Tastverhältnis geregelt.
|
|
*/
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {
|
|
.Prescaler = 0UL,
|
|
.CounterMode = LL_TIM_COUNTERMODE_UP,
|
|
.Autoreload = TIMER_RELOAD_VALUE,
|
|
.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1,
|
|
};
|
|
LL_TIM_Init(TIM3, &TIM_InitStruct);
|
|
|
|
LL_TIM_DisableARRPreload(TIM3);
|
|
LL_TIM_OC_EnablePreload(TIM3, LL_TIM_CHANNEL_CH1);
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {
|
|
.OCMode = LL_TIM_OCMODE_PWM1,
|
|
.OCState = LL_TIM_OCSTATE_ENABLE,
|
|
.OCNState = LL_TIM_OCSTATE_DISABLE,
|
|
.CompareValue = 0UL,
|
|
.OCPolarity = LL_TIM_OCPOLARITY_HIGH,
|
|
};
|
|
LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
|
|
LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH1);
|
|
LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);
|
|
LL_TIM_DisableMasterSlaveMode(TIM3);
|
|
|
|
/* TIM2 Input Capture zur Messung der Geschwindigkeit.
|
|
*/
|
|
TIM_InitStruct.Prescaler = 0;
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
TIM_InitStruct.Autoreload = UINT32_MAX;
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
|
LL_TIM_DisableARRPreload(TIM2);
|
|
LL_TIM_ConfigETR(TIM2, LL_TIM_ETR_POLARITY_NONINVERTED, LL_TIM_ETR_PRESCALER_DIV1, LL_TIM_ETR_FILTER_FDIV1);
|
|
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_EXT_MODE2);
|
|
LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
|
|
LL_TIM_DisableMasterSlaveMode(TIM2);
|
|
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
|
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
|
|
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
|
|
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
|
|
LL_TIM_SetRemap(TIM2, LL_TIM_TIM2_TI1_RMP_COMP1);
|
|
|
|
/* TIM14 1s ISR */
|
|
TIM_InitStruct.Prescaler = 15999;
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
TIM_InitStruct.Autoreload = 3999;
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
LL_TIM_Init(TIM14, &TIM_InitStruct);
|
|
LL_TIM_DisableARRPreload(TIM14);
|
|
LL_TIM_EnableIT_UPDATE(TIM14);
|
|
|
|
/* TIM3 GPIO Configuration
|
|
* PC6 ------> TIM3_CH1
|
|
*/
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {
|
|
.Pin = LL_GPIO_PIN_6,
|
|
.Mode = LL_GPIO_MODE_ALTERNATE,
|
|
.Speed = LL_GPIO_SPEED_FREQ_LOW,
|
|
.OutputType = LL_GPIO_OUTPUT_PUSHPULL,
|
|
.Pull = LL_GPIO_PULL_NO,
|
|
.Alternate = LL_GPIO_AF_1,
|
|
};
|
|
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
/*T IM2 GPIO Configuration
|
|
* PA15 ------> TIM2_CH1
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
LL_TIM_EnableCounter(TIM2);
|
|
LL_TIM_EnableCounter(TIM3);
|
|
LL_TIM_EnableCounter(TIM14);
|
|
|
|
this->last_compare = 0UL;
|
|
this->last_temp = 0UL;
|
|
|
|
NVIC_SetPriority(TIM14_IRQn, 0);
|
|
NVIC_EnableIRQ(TIM14_IRQn);
|
|
}
|
|
|
|
void FanControl::run(std::uint32_t temp)
|
|
{
|
|
if(duty != 0U)
|
|
{
|
|
LL_TIM_OC_SetCompareCH1(TIM3, duty);
|
|
}
|
|
else
|
|
{
|
|
if(this->last_temp != temp)
|
|
{
|
|
std::uint16_t cv = this->get_compare_value(temp);
|
|
if(this->last_compare != cv)
|
|
{
|
|
cv = (this->last_compare + cv) / 2U;
|
|
LL_TIM_OC_SetCompareCH1(TIM3, cv);
|
|
this->last_compare = cv;
|
|
}
|
|
this->last_temp = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
std::uint16_t FanControl::get_compare_value(std::uint16_t temp)
|
|
{
|
|
std::uint16_t cv = TIMER_RELOAD_VALUE;
|
|
for(std::uint32_t i = 0UL; i < sizeof(stuetzpunkte) / sizeof(stuetzpunkte[0]); i++)
|
|
{
|
|
if(temp < stuetzpunkte[i][0])
|
|
{
|
|
cv = stuetzpunkte[i][1];
|
|
break;
|
|
}
|
|
}
|
|
return cv;
|
|
}
|
|
|
|
/**
|
|
* @brief Lüftergeschwindigkeit in Umdrehungen pro Minute.
|
|
* @details Das Tachosignal gibt zwei Impule pro Umdrehung heraus.
|
|
* Die Messdauer der Pulsanzahl beträgt eine Miunute.
|
|
* v = pulse / 2 * 60 => erst mal 60, damit es ganuer wird
|
|
* wegen Datentyp ungenauigkeit beim Teilen.
|
|
* @return
|
|
*/
|
|
std::uint32_t FanControl::get_speed(void)
|
|
{
|
|
return fan_speed * 60UL / 2UL;
|
|
}
|
|
}
|