проект запущен шим идет, нужно запустить с пид регулятором вывести все измерения в структуру

This commit is contained in:
2026-05-23 19:07:50 +03:00
parent b85718ccf2
commit 223841301e
16 changed files with 2066 additions and 1319 deletions

View File

@@ -4,76 +4,84 @@
#include <stdint.h>
/*
* B-G474E-DPOW1 quick configuration.
*
* SAFETY:
* - The project configures the buck half-bridge on PB12/PB13.
* - Keep DCDC_POWER_STAGE_ENABLE at 0 until VIN, load and oscilloscope
* probes are ready. With 0 the firmware prints ADC values and prepares
* HRTIM, but does not enable HRTIM outputs.
* - If VIN comes from USB-C/USB-PD through JP1, set
* DCDC_CONNECT_USBPD_INPUT to 1 only after checking the board jumpers.
*/
## B-G474E-DPOW1 build-time defaults
These macros are now **boot defaults**. Runtime values live in
`g_dcdc_config` and can be edited while the firmware is running.
### Safety
- `DCDC_POWER_STAGE_ENABLE = 0` keeps the real closed-loop power stage off.
- `DCDC_PWM_TEST_ENABLE = 1` starts the current HRTIM analyzer test.
- `DCDC_CONNECT_USBPD_INPUT = 0` keeps the USB-PD-to-VIN switch off.
*/
#define DCDC_POWER_STAGE_ENABLE 0U
#define DCDC_PWM_TEST_ENABLE 1U
#define DCDC_GPIO_PIN_TEST_ENABLE 0U
#define DCDC_CONNECT_USBPD_INPUT 0U
/*
* Clock startup.
* 0 = safest rescue mode: run directly from HSI16, no PLL switch.
* 1 = normal performance mode: HSI16 -> PLL -> 170 MHz.
*
* If the debugger hangs inside SystemClock_Config(), keep this at 0 until
* the board is recovered and ST-LINK connects reliably.
*/
## Clock startup
- `0`: rescue mode, direct HSI16, no PLL switch.
- `1`: normal mode, HSI16 -> PLL -> 170 MHz.
*/
#define CLOCK_USE_PLL_170MHZ 1U
#define SYSCLK_HZ 170000000UL
#define HSI16_HZ 16000000UL
#define USART3_BAUDRATE 115200UL
#define DEBUG_ATTACH_DELAY_LOOPS 8000000UL
/*
* HRTIM Timer C drives the onboard buck leg:
* PB12 = BUCKBOOST_P1_DRIVE = HRTIM1_CHC1, high-side buck MOSFET
* PB13 = BUCKBOOST_N1_DRIVE = HRTIM1_CHC2, low-side buck MOSFET
*
* fHRCK = fHRTIM * 32. The firmware calculates the actual period from
* SystemCoreClock, so rescue HSI16 mode still works.
*/
## HRTIM Timer C defaults
- `PB12`: `HRTIM1_CHC1`
- `PB13`: `HRTIM1_CHC2`
- `fHRCK = fHRTIM * 32`
Live edits:
- `g_dcdc_config.pwm.frequency_hz`
- `g_dcdc_config.pwm.test_duty_permille`
- `g_dcdc_config.pwm.deadtime_rising_ticks`
- `g_dcdc_config.pwm.deadtime_falling_ticks`
*/
#define DCDC_HRTIM_PERIOD_TICKS 27200U
#define DCDC_PWM_FREQUENCY_HZ 200000UL
#define DCDC_PWM_TEST_DUTY_PERMILLE 100U
#define DCDC_PWM_TEST_DEADTIME_ENABLE 1U
#define DCDC_PWM_TEST_DEADTIME_TICKS 34U
#define DCDC_GPIO_PIN_TEST_HALF_PERIOD_MS 1U
#define DCDC_MIN_DUTY_TICKS 0U
#define DCDC_MAX_DUTY_TICKS 24000U
#define DCDC_START_DUTY_TICKS 400U
/*
* Dead-time values are expressed in HRTIM dead-time ticks.
* They are intentionally conservative starter values; verify PB12/PB13
* with an oscilloscope before enabling the power stage.
*/
## Closed-loop dead-time defaults
These are used when the real power stage mode is enabled. The current analyzer
test uses `g_dcdc_config.pwm.deadtime_*_ticks`.
*/
#define DCDC_DEADTIME_RISING_TICKS 110U
#define DCDC_DEADTIME_FALLING_TICKS 350U
/*
* ADC measurement pins on B-G474E-DPOW1:
* PA1 = BUCKBOOST_VIN (ADC1_IN2)
* PA2 = BUCKBOOST_I_IN_AVG (ADC1_IN3)
* PA3 = BUCKBOOST_VOUT (ADC1_IN4)
*
* ST X-CUBE-DPower reference values for this board use:
* Vout_Scaling = 0.198795180 -> ADC pin voltage = VOUT * 0.198795180
* Iin_Scaling = 0.721543408 -> ADC pin voltage = IIN * 0.721543408 V/A
*
* VIN uses the same divider value by default here. If your hardware revision
* or solder bridge setup differs, adjust DCDC_VIN_SCALE_PPM after measuring.
*/
## ADC measurement defaults
- `PA1 / ADC1_IN2`: `BUCKBOOST_VIN`
- `PA2 / ADC1_IN3`: `BUCKBOOST_I_IN_AVG`
- `PA3 / ADC1_IN4`: `BUCKBOOST_VOUT`
Scaling starts from ST X-CUBE-DPower board values and can be adjusted after
multimeter comparison.
*/
#define ADC_REFERENCE_MV 3300U
#define ADC_FULL_SCALE_COUNTS 4095U
#define DCDC_VOUT_SCALE_PPM 198795UL
#define DCDC_VIN_SCALE_PPM 198795UL
#define DCDC_IIN_UV_PER_MA 721UL
/* Starter setpoints and protection thresholds. */
/* ## Starter setpoints and protection thresholds */
#define DCDC_TARGET_VOUT_MV 3300U
#define DCDC_MIN_VIN_MV 4500U
#define DCDC_MAX_VOUT_MV 3800U
@@ -81,10 +89,14 @@
#define DCDC_HARD_INPUT_CURRENT_MA 450U
/*
* Simple voltage loop gains for the starter firmware.
* Duty command = feed-forward + proportional + integral - current limiting.
* Tune these gently with a current-limited supply and a scope.
*/
## Starter loop gains
Duty command:
`feed-forward + proportional + integral - current-limit correction`
Tune with a current-limited supply and an oscilloscope.
*/
#define DCDC_KP_TICKS_PER_100MV 90
#define DCDC_KI_TICKS_PER_100MV 3
#define DCDC_CURRENT_LIMIT_KP 60

View File

@@ -4,6 +4,63 @@
#include <stdbool.h>
#include <stdint.h>
/*
## Runtime configuration model
`g_dcdc_config` is intentionally a global `volatile` structure so it can be
edited from Keil **Watch**, **Memory**, or a future UART command parser while
the firmware is running.
Typical live edits during the current HRTIM test:
- `g_dcdc_config.mode = DCDC_MODE_PWM_TEST`
- `g_dcdc_config.pwm.frequency_hz = 200000`
- `g_dcdc_config.pwm.test_duty_permille = 100`
- `g_dcdc_config.pwm.deadtime_rising_ticks = 34`
- `g_dcdc_config.pwm.deadtime_falling_ticks = 34`
*/
typedef enum
{
DCDC_MODE_MONITOR = 0,
DCDC_MODE_PWM_TEST,
DCDC_MODE_CLOSED_LOOP
} DCDC_Mode;
typedef struct
{
uint32_t frequency_hz;
uint32_t test_duty_permille;
uint32_t max_duty_permille;
uint32_t deadtime_enable;
uint32_t deadtime_rising_ticks;
uint32_t deadtime_falling_ticks;
} DCDC_PwmRuntimeConfig;
typedef struct
{
uint32_t target_vout_mv;
uint32_t min_vin_mv;
uint32_t max_vout_mv;
uint32_t current_limit_ma;
uint32_t hard_current_ma;
} DCDC_LimitsRuntimeConfig;
typedef struct
{
int32_t kp_ticks_per_100mv;
int32_t ki_ticks_per_100mv;
int32_t current_limit_kp;
} DCDC_LoopRuntimeConfig;
typedef struct
{
uint32_t mode;
uint32_t connect_usbpd_input;
DCDC_PwmRuntimeConfig pwm;
DCDC_LimitsRuntimeConfig limits;
DCDC_LoopRuntimeConfig loop;
} DCDC_RuntimeConfig;
typedef struct
{
uint16_t vin_raw;
@@ -18,6 +75,7 @@ typedef enum
{
DCDC_STATE_STOPPED = 0,
DCDC_STATE_READY,
DCDC_STATE_PWM_TEST,
DCDC_STATE_RUNNING,
DCDC_STATE_FAULT
} DCDC_State;
@@ -31,17 +89,24 @@ typedef enum
DCDC_FAULT_HRTIM
} DCDC_Fault;
extern volatile DCDC_RuntimeConfig g_dcdc_config;
void DCDC_Init(void);
void DCDC_Start(void);
void DCDC_StartPwmTest(void);
void DCDC_Stop(void);
void DCDC_Service1ms(void);
void DCDC_ApplyRuntimeConfig(void);
void DCDC_ControlStep(void);
void DCDC_ReadMeasurements(DCDC_Measurements *out);
DCDC_Mode DCDC_GetMode(void);
DCDC_State DCDC_GetState(void);
DCDC_Fault DCDC_GetFault(void);
bool DCDC_IsHrtimReady(void);
uint32_t DCDC_GetDutyTicks(void);
uint32_t DCDC_GetPeriodTicks(void);
const char *DCDC_ModeText(DCDC_Mode mode);
const char *DCDC_StateText(DCDC_State state);
const char *DCDC_FaultText(DCDC_Fault fault);