embassy-rs/embassy · error

Incorrect Vrs setting!

Error message

Incorrect Vrs setting!

What it means

The VREFBUF (voltage reference buffer) driver panics in get_refbuf_trim when the requested Vrs voltage scale is not one of the four supported Vref0..Vref3 settings. The trim value is looked up from a fixed table, and any other Vrs variant has no trim constant, so the driver refuses to construct rather than program wrong calibration data.

Solutions

  1. Pass only Vrs::Vref0 through Vrs::Vref3 to VoltageReferenceBuffer::new.
  2. Update embassy-stm32 to the latest version so newer Vrs variants have trim values.
  3. If you need an unsupported scale, disable the VREFBUF (bypass mode) instead of constructing the driver.

Example fix

// before
let vref = VoltageReferenceBuffer::new(p.VREFBUF, Vrs::Vref5, Impedance::High);
// after
let vref = VoltageReferenceBuffer::new(p.VREFBUF, Vrs::Vref2, Impedance::High);
Defensive patterns

Strategy: validation

Validate before calling

use embassy_stm32::vrefbuf::Vrs;
fn is_supported_vrs(v: Vrs) -> bool {
    matches!(v, Vrs::Vref0 | Vrs::Vref1 | Vrs::Vref2 | Vrs::Vref3)
}
assert!(is_supported_vrs(scale), "Vrs not supported by this HAL trim table");

Type guard

fn is_supported_vrs(v: Vrs) -> bool {
    matches!(v, Vrs::Vref0 | Vrs::Vref1 | Vrs::Vref2 | Vrs::Vref3)
}

Prevention

When it happens

Trigger: Calling VoltageReferenceBuffer::new with a voltage_scale value outside Vrs::Vref0, Vrs::Vref1, Vrs::Vref2, or Vrs::Vref3 — typically a non-exhaustive Vrs variant (e.g. a newer/scaled enum value) unsupported by the current HAL.

Common situations: Targeting a newer STM32 family whose Vrs enum has extra variants while using an embassy-stm32 version whose trim table predates them; copying example code that used Vrs for another chip; typo'ing or casting an arbitrary integer into Vrs.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of embassy-rs/embassy@463a07b963 (2026-09-10). Data as JSON: /api/errors/e30df3e83723dd55. Report an issue: GitHub.

Appendix: source

Thrown at embassy-stm32/src/vrefbuf/mod.rs:21

use embassy_hal_internal::PeripheralType;
use stm32_metapac::vrefbuf::vals::*;

use crate::Peri;

/// Voltage Reference (VREFBUF) driver.
pub struct VoltageReferenceBuffer<'d, T: Instance> {
    vrefbuf: PhantomData<&'d mut T>,
}

#[cfg(rcc_wba)]
fn get_refbuf_trim(voltage_scale: Vrs) -> usize {
    match voltage_scale {
        Vrs::Vref0 => 0x0BFA_07ABusize,
        Vrs::Vref1 => 0x0BFA_07AAusize,
        Vrs::Vref2 => 0x0BFA_07A9usize,
        Vrs::Vref3 => 0x0BFA_07A8usize,
        _ => panic!("Incorrect Vrs setting!"),
    }
}

impl<'d, T: Instance> VoltageReferenceBuffer<'d, T> {
    /// Creates an VREFBUF (Voltage Reference) instance with a voltage scale and impedance mode.
    ///
    /// [Self] has to be started with [Self::new()].
    pub fn new(_instance: Peri<'d, T>, voltage_scale: Vrs, impedance_mode: Hiz) -> Self {
        #[cfg(rcc_wba)]
        {
            use crate::pac::RCC;
            RCC.apb7enr().modify(|w| w.set_vrefen(true));
            // This is an errata for WBA6 devices. VREFBUF_TRIM value isn't set correctly
            // [Link explaining it](https://www.st.com/resource/en/errata_sheet/es0644-stm32wba6xxx-device-errata-stmicroelectronics.pdf)
            unsafe {
                use crate::pac::VREFBUF;
                let addr = get_refbuf_trim(voltage_scale);
                let buf_trim_ptr = core::ptr::with_exposed_provenance::<u32>(addr);

View on GitHub (pinned to 463a07b963)