{"record":{"id":"d861779b231482cf","repo":"embassy-rs/embassy","slug":"invalid-prescaler-for-timer-frequency-hz","errorCode":null,"errorMessage":"Invalid prescaler: {} for timer frequency: {}Hz","messagePattern":"Invalid prescaler: (.+?) for timer frequency: (.+?)Hz","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-stm32/src/time_driver/lptim.rs","lineNumber":147,"sourceCode":"        rcc::enable_and_reset_with_cs::<T>(cs);\n\n        let timer_freq = T::frequency();\n\n        r.cnt().write(|w| w.set_cnt(0));\n\n        // let psc = timer_freq.0 / TICK_HZ as u32 - 1;\n        let psc = timer_freq.0 / TICK_HZ as u32;\n        let psc = match psc {\n            128 => vals::Presc::Div128,\n            64 => vals::Presc::Div64,\n            32 => vals::Presc::Div32,\n            16 => vals::Presc::Div16,\n            8 => vals::Presc::Div8,\n            4 => vals::Presc::Div4,\n            2 => vals::Presc::Div2,\n            1 => vals::Presc::Div1,\n            // TODO: we could compute the valid TICK_HZ for the valid prescalers to include in the panic message\n            _ => panic!(\"Invalid prescaler: {} for timer frequency: {}Hz\", psc, timer_freq.0),\n        };\n\n        trace!(\n            \"init: setting presc: {} timer_freq: {}Hz TICK_HZ: {}\",\n            psc, timer_freq, TICK_HZ\n        );\n        r.cfgr().modify(|w| w.set_presc(psc));\n\n        // RM says timer must be enabled before setting arr or cmp\n        r.cr().modify(|w| w.set_enable(true));\n        trace!(\"init: arr: {:?}\", r.arr().read());\n        // TRM says this is updated immediately if the timer is not started so no need to check for arrok! (stm32wl5 & stm32wle)\n        r.arr().write(|w| w.set_arr(u16::MAX));\n\n        // Enable overflow interrupts\n        ier_set_ueie(T::regs(), true);\n\n        <T as crate::lptim::SealedBasicInstance>::GlobalInterrupt::unpend();","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-stm32/src/time_driver/lptim.rs#L129-L165","documentation":"The LPTIM-based time driver computes the prescaler needed so timer ticks match TICK_HZ; it only supports division factors 1,2,4,8,16 (mapped to LPTIM Presc values). If the required division for the given timer frequency isn't one of these, init_timer panics with the prescaler and timer frequency.","triggerScenarios":"Setting TICK_HZ such that timer_freq / TICK_HZ is not exactly 1, 2, 4, 8, or 16 (e.g. timer clock 32768 Hz with TICK_HZ=1000 → ratio not a supported power of two).","commonSituations":"Custom TICK_HZ in a build.rs/feature config that doesn't divide the LPTIM clock; boards using an unusual LPTIM clock source (e.g. LSE 32768 Hz) with a millisecond tick.","solutions":["Choose a TICK_HZ that divides timer_freq by exactly 1/2/4/8/16 (e.g. with 32 kHz LSE use TICK_HZ 1024 or 2048).","Change the LPTIM clock source/frequency so the ratio becomes a supported power of two.","Adjust the TICK_HZ feature/configuration at build time to match your timer clock."],"exampleFix":"// before (LSE 32768 Hz)\ntick_hz = 1000\n// after\ntick_hz = 1024 // 32768 / 32 is not supported; 32768/32 no — use power-of-two ratio ≤16:\n// e.g. tick_hz = 2048 (32768/16) or raise LPTIM clock to 16 MHz and keep 1000 only if ratio fits","handlingStrategy":"validation","validationCode":"const TICK_HZ: u32 = 1024; // ensure timer_freq / TICK_HZ is exactly 1, 2, 4, 8, or 16\nfn prescaler_valid(timer_freq: u32, tick_hz: u32) -> bool {\n    matches!(timer_freq / tick_hz, 1 | 2 | 4 | 8 | 16)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pick TICK_HZ as timer_freq divided by a supported power of two (≤16).","For 32 kHz LSE clocks, use TICK_HZ like 2048 or 4096, not 1000.","Set TICK_HZ at build time consistently with the LPTIM clock source."],"tags":["lptim","time-driver","embedded","panic"],"backgroundTag":"invalid-config-value","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"}