Files
d_power/B-G474E-DPOW1_Keil/Core/Src/main.c

128 lines
4.0 KiB
C

#include <stdio.h>
#include "app_config.h"
#include "board.h"
#include "dcdc.h"
#include "retarget.h"
#include "stm32g474xx.h"
/*
## Main loop
The application is intentionally small:
- board clocks and UART start first;
- `DCDC_Init()` prepares ADC and HRTIM;
- `DCDC_Service1ms()` applies live edits from `g_dcdc_config`;
- terminal output prints the current measurements and active runtime mode.
*/
#if DCDC_GPIO_PIN_TEST_ENABLE
static void GpioPinTest_Init(void);
static void GpioPinTest_SetPhase(bool phase);
#endif
int main(void)
{
uint32_t last_control_ms = 0U;
uint32_t last_print_ms = 0U;
#if DCDC_GPIO_PIN_TEST_ENABLE
uint32_t last_gpio_test_ms = 0U;
bool gpio_test_phase = false;
#endif
Board_Init();
Retarget_Init(USART3_BAUDRATE);
DCDC_Init();
printf("\nB-G474E-DPOW1 DCDC starter project\n");
printf("USART3 retarget printf: %lu baud\n", (unsigned long)USART3_BAUDRATE);
printf("SystemCoreClock: %lu Hz\n", (unsigned long)SystemCoreClock);
printf("PWM: %lu Hz, period=%lu HRTIM ticks\n",
(unsigned long)DCDC_PWM_FREQUENCY_HZ,
(unsigned long)DCDC_GetPeriodTicks());
printf("HRTIM DLL: %s\n", DCDC_IsHrtimReady() ? "ready" : "not ready");
printf("Runtime mode: %s; edit g_dcdc_config in Keil Watch.\n",
DCDC_ModeText(DCDC_GetMode()));
printf("Runtime PWM: freq=%luHz duty=%lu/1000 deadtime=%lu rise=%lu fall=%lu ticks\n",
(unsigned long)g_dcdc_config.pwm.frequency_hz,
(unsigned long)g_dcdc_config.pwm.test_duty_permille,
(unsigned long)g_dcdc_config.pwm.deadtime_enable,
(unsigned long)g_dcdc_config.pwm.deadtime_rising_ticks,
(unsigned long)g_dcdc_config.pwm.deadtime_falling_ticks);
#if DCDC_GPIO_PIN_TEST_ENABLE
printf("GPIO pin test: PB12/PB13 toggle as ordinary outputs.\n");
GpioPinTest_Init();
#else
printf("Runtime service active. mode=0 monitor, 1 pwm-test, 2 closed-loop.\n");
#endif
while (1)
{
uint32_t now = Board_Millis();
#if DCDC_GPIO_PIN_TEST_ENABLE
if ((now - last_gpio_test_ms) >= DCDC_GPIO_PIN_TEST_HALF_PERIOD_MS)
{
last_gpio_test_ms = now;
gpio_test_phase = !gpio_test_phase;
GpioPinTest_SetPhase(gpio_test_phase);
}
#else
if ((now - last_control_ms) >= 1U)
{
last_control_ms = now;
DCDC_Service1ms();
}
#endif
if ((now - last_print_ms) >= 500U)
{
DCDC_Measurements m;
last_print_ms = now;
DCDC_ReadMeasurements(&m);
printf("mode=%s state=%s fault=%s hrtim=%s vin=%lumV iin=%lumA vout=%lumV duty=%lu/%lu dt=%lu/%lu\n",
DCDC_ModeText(DCDC_GetMode()),
DCDC_StateText(DCDC_GetState()),
DCDC_FaultText(DCDC_GetFault()),
DCDC_IsHrtimReady() ? "ready" : "not-ready",
(unsigned long)m.vin_mv,
(unsigned long)m.iin_ma,
(unsigned long)m.vout_mv,
(unsigned long)DCDC_GetDutyTicks(),
(unsigned long)DCDC_GetPeriodTicks(),
(unsigned long)g_dcdc_config.pwm.deadtime_rising_ticks,
(unsigned long)g_dcdc_config.pwm.deadtime_falling_ticks);
}
}
}
#if DCDC_GPIO_PIN_TEST_ENABLE
static void GpioPinTest_Init(void)
{
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
(void)RCC->AHB2ENR;
GPIOB->MODER &= ~((3UL << (12U * 2U)) | (3UL << (13U * 2U)));
GPIOB->MODER |= ((1UL << (12U * 2U)) | (1UL << (13U * 2U)));
GPIOB->OTYPER &= ~((1UL << 12U) | (1UL << 13U));
GPIOB->OSPEEDR |= ((3UL << (12U * 2U)) | (3UL << (13U * 2U)));
GPIOB->PUPDR &= ~((3UL << (12U * 2U)) | (3UL << (13U * 2U)));
GpioPinTest_SetPhase(false);
}
static void GpioPinTest_SetPhase(bool phase)
{
if (phase)
{
GPIOB->BSRR = (1UL << 12U) | (1UL << (13U + 16U));
}
else
{
GPIOB->BSRR = (1UL << 13U) | (1UL << (12U + 16U));
}
}
#endif