main
TylerK Replace discrete-analog board with MCU/IC redesign: STM32G031 P&O MPPT + CC/CV firmware, LM5175 buck-boost, INA180 curr… e48ba30 1mo ago
#include "pwm_ref.h"
#include "board_config.h"
#include "main.h"   /* CubeMX-generated: htim14 handle */

void pwm_ref_init(void) {
    HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);
    __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, 0);
}

void pwm_ref_set_vbatt_setpoint(float vbatt_setpoint) {
    float mcu_ref_v = vbatt_setpoint_to_mcu_ref(vbatt_setpoint);
    float duty = mcu_ref_to_pwm_duty(mcu_ref_v);

    uint32_t arr = __HAL_TIM_GET_AUTORELOAD(&htim14);
    uint32_t compare = (uint32_t)(duty * (float)(arr + 1));
    if (compare > arr) compare = arr;

    __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, compare);
}