{"record":{"id":"309d41797110a23b","repo":"embassy-rs/embassy","slug":"the-top-value-cannot-be-changed-after-the-initiali","errorCode":null,"errorMessage":"The top value cannot be changed after the initialization.","messagePattern":"The top value cannot be changed after the initialization\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-nxp/src/pwm/lpc55.rs","lineNumber":164,"sourceCode":"        SCT0.config().modify(|w| {\n            w.set_unify(vals::Unify::UnifiedCounter);\n            w.set_clkmode(vals::Clkmode::SystemClockMode);\n            w.set_noreload_l(true);\n            w.set_autolimit_l(true);\n        });\n\n        // Before setting the match registers, we have to make sure that `compare` is lower or equal to `top`,\n        // otherwise the counter will not reach the match and, therefore, no events will happen.\n        assert!(config.compare <= config.top);\n\n        if TOP_VALUE.load(Ordering::Relaxed) == 0 {\n            // Match 0 will reset the timer using TOP value\n            SCT0.match_(0).modify(|w| {\n                w.set_matc_hn_l((config.top & 0xFFFF) as u16);\n                w.set_matc_hn_h((config.top >> 16) as u16);\n            });\n        } else {\n            panic!(\"The top value cannot be changed after the initialization.\");\n        }\n        // The actual matches that are used for event logic\n        SCT0.match_(output_number + 1).modify(|w| {\n            w.set_matc_hn_l((config.compare & 0xFFFF) as u16);\n            w.set_matc_hn_h((config.compare >> 16) as u16);\n        });\n\n        SCT0.match_(15).modify(|w| {\n            w.set_matc_hn_l(0);\n            w.set_matc_hn_h(0);\n        });\n\n        // Event configuration\n        critical_section::with(|_cs| {\n            // If it is already set, don't change\n            if SCT0.ev(0).ev_ctrl().read().matchsel() != 15 {\n                SCT0.ev(0).ev_ctrl().modify(|w| {\n                    w.set_matchsel(15);","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-nxp/src/pwm/lpc55.rs#L146-L182","documentation":"The LPC55 PWM driver only allows setting the timer TOP (period) value during initialization; once the PWM is initialized with a TOP value, calling `configure` with a different `config.top` panics. This is because the match registers that reset the counter are latched at init and changing them at runtime is not supported by this driver implementation.","triggerScenarios":"Calling `Pwm::new(...)` first with one `config.top`, then calling `configure()` (or constructing again) with a `config.top` value different from the initial one on the same SCT instance.","commonSituations":"Dynamically changing PWM frequency/period at runtime for motor control or LED breathing effects; reusing a PWM instance across modules with conflicting period settings; copying example config values without realizing `top` must stay constant.","solutions":["Keep `config.top` identical to the value used at initialization for all subsequent `configure()` calls","Only vary `config.compare` (duty cycle) at runtime, not the period","De-initialize and re-create the PWM instance if the period genuinely must change","Precompute all needed frequencies at init and allocate one PWM instance per period"],"exampleFix":"// before\npwm.configure(channel, &Config { top: 10_000, compare: 5_000, .. }); // top differs from init\n// after\npwm.configure(channel, &Config { top: INITIAL_TOP, compare: 5_000, .. }); // same top as at init\n","handlingStrategy":"validation","validationCode":"fn assert_same_top(init_top: u32, cfg: &PwmConfig) -> Result<(), &'static str> {\n    if cfg.top != init_top { return Err(\"top must stay fixed after PWM init\"); }\n    Ok(())\n}\n","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat config.top as immutable after Pwm::new; only change compare values at runtime","Store the initialized top in a const and reference it in all subsequent configs","Wrap configure() calls in a helper that enforces the invariant"],"tags":["rust","embedded","pwm","lpc55","unsupported-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}