Skip to content

Implement regulator firmware per NLSpec v0.1.2d, CubeMX-regeneration safe#5

Draft
Copilot wants to merge 14 commits intospecfrom
copilot/implement-code-nlspec-compatibility
Draft

Implement regulator firmware per NLSpec v0.1.2d, CubeMX-regeneration safe#5
Copilot wants to merge 14 commits intospecfrom
copilot/implement-code-nlspec-compatibility

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 10, 2026

Implements the full buck-boost regulator control firmware as specified in buck-boost-nlspec-v0_1_2d.md. All application code lives in new files or existing /* USER CODE */ sections — CubeMX regeneration cannot overwrite any of it.

New files

  • regulator_config.h — Single source of truth for all hardware constants (§2.1). Pin mappings, HRTIM timing, ADC/DAC scaling, PID limits, OVP/UVP thresholds, NVIC priorities — each with units, derivation, and valid-range _Static_assert.

  • power_stage.h — Public API: PS_State_t (INIT/IDLE/RUNNING/FAULT), PS_Mode_t (BUCK/BOOST), §9.1 shared state (target_voltage_mv, regulator_ready, regulator_fault), and the §9.2 PD↔Regulator function API.

  • power_stage.c — Full regulator engine:

    • HRTIM Set/Reset sources for buck (Timer A, EEV4 + CMP1 backstop) and boost (Timer B, inverted polarity)
    • Static-leg bootstrap refresh via CMP2 at end-of-period (§5.4)
    • Hardware backstop at 85% duty (CMP1); 3 consecutive violations → FAULT (§10.3–10.4)
    • TIM6 slope-compensation staircase at 2 MHz; step precomputed at init, never in ISR (§7.5)
    • TIM7 PID outer loop at 20 kHz with back-calculation anti-windup (§8.4)
    • Software OVP/UVP/V_in checks including 52.8 V compile-time hard cap (§10.2)
    • Soft-start ramp and mode auto-selection with hysteresis (§5.1, §11.2)
    • All ISRs implemented as __weak HAL callback overrides — no changes to stm32g4xx_it.c required
/* PD stack calls these; safe from any context */
void regulator_set_target_voltage(uint32_t voltage_mv);
void regulator_stop(void);
void regulator_clear_fault(void);

Modified files (USER CODE sections only)

  • state_machine.c — Replaced broken C++ PID.calculate() stub with delegation to power_stage; now a thin application coordinator.
  • usbpd_pdo_defs.h — Expanded from 1 stub PDO to 5 SPR PDOs: 5 V/9 V/12 V/15 V @ 3 A, 20 V @ 5 A (§9.4).
  • usbpd_pwr_user.c — Wired VBUSOn, VBUSOff, VBUSSetVoltage_Fixed, VBUSIsOn, VBUSGetVoltage to power_stage.
  • usbpd_dpm_user.c — Enabled USBPD_NOTIFY_POWER_EXPLICIT_CONTRACT handler; added power_stage.h include.
  • main.c — Added #include "power_stage.h" in USER CODE section.

Note: power_stage.c references HAL handles (hhrtim1, hdac3, hadc1, hadc2, hcomp1, htim6, htim7) that will be generated by CubeMX once the IOC is updated with HRTIM1, DAC3, COMP1, ADC1, ADC2, TIM6, and TIM7.

Original prompt

Implement code as per the nlspec that is compatible with cube mx code regeneration


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits March 10, 2026 16:00
…power_stage.c; fix state_machine.c; expand PDO table; wire USBPD callbacks

Co-authored-by: electricalhog <15219301+electricalhog@users.noreply.github.com>
… comment, clean HRTIM constant names

Co-authored-by: electricalhog <15219301+electricalhog@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement code for NLSPEC compatibility with CubeMX Implement regulator firmware per NLSpec v0.1.2d, CubeMX-regeneration safe Mar 10, 2026
Comment on lines +204 to +208
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, NVIC_PRIO_SLOPE_COMP, 0u);
HAL_NVIC_SetPriority(TIM7_IRQn, NVIC_PRIO_PID, 0u);
HAL_NVIC_SetPriority(HRTIM1_TIMA_IRQn, NVIC_PRIO_HRTIM, 0u);
HAL_NVIC_SetPriority(HRTIM1_TIMB_IRQn, NVIC_PRIO_HRTIM, 0u);
HAL_NVIC_SetPriority(HRTIM1_FLT_IRQn, NVIC_PRIO_HRTIM, 0u);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unclear of priority order; RTOS priority should be ≥ 5 to allow for full indexing of IRQs

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unclear of priority order; RTOS priority should be ≥ 5 to allow for full indexing of IRQs

Updated CubeMX to add overhead in NVIC priority change commit to allow for discrete IRQ interrupt priorities.

Comment on lines +255 to +261
if (mode == PS_MODE_BUCK) {
ps_configure_hrtim_buck();
ps_configure_static_leg_buck();
} else {
ps_configure_hrtim_boost();
ps_configure_static_leg_boost();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be switch for future proofing with buck-boost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants