{"record":{"id":"39a178ec7b4117e3","repo":"embassy-rs/embassy","slug":"hertz-cannot-be-0","errorCode":null,"errorMessage":"Hertz cannot be 0","messagePattern":"Hertz cannot be 0","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-stm32/src/time.rs","lineNumber":143,"sourceCode":"#[cfg_attr(feature = \"defmt\", derive(defmt::Format))]\n/// A variant on [Hertz] that acts as an `Option<Hertz>` that is smaller and repr C.\n///\n/// An `Option<Hertz>` can be `.into()`'d into this type and back.\n/// The only restriction is that that [Hertz] cannot have the value 0 since that's\n/// seen as the `None` variant.\npub struct MaybeHertz(u32);\n\nimpl MaybeHertz {\n    /// Same as calling the `.into()` function, but without type inference.\n    pub fn to_hertz(self) -> Option<Hertz> {\n        self.into()\n    }\n}\n\nimpl From<Option<Hertz>> for MaybeHertz {\n    fn from(value: Option<Hertz>) -> Self {\n        match value {\n            Some(Hertz(0)) => panic!(\"Hertz cannot be 0\"),\n            Some(Hertz(val)) => Self(val),\n            None => Self(0),\n        }\n    }\n}\n\nimpl From<MaybeHertz> for Option<Hertz> {\n    fn from(value: MaybeHertz) -> Self {\n        match value {\n            MaybeHertz(0) => None,\n            MaybeHertz(val) => Some(Hertz(val)),\n        }\n    }\n}\n","sourceCodeStart":125,"sourceCodeEnd":158,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-stm32/src/time.rs#L125-L158","documentation":"MaybeHertz's From<Option<Hertz>> impl panics when converting Some(Hertz(0)). A zero frequency is meaningless for clock configuration and typically indicates an unset or miscalculated clock, so the driver rejects it eagerly.","triggerScenarios":"Passing Some(Hertz(0)) anywhere the API accepts Option<Hertz> (e.g. optional clock frequencies in RCC/peripheral configs), usually because a computed frequency was 0 (disabled source, division by zero elsewhere, or uninitialized config).","commonSituations":"Clock frequency computed from an HSE value that is None/0; typo in a board file yielding 0 Hz; passing 0.mhz() wrapped in Some instead of None.","solutions":["Pass None for the optional frequency instead of Some(Hertz(0)).","Fix the frequency computation so a real nonzero value is produced (check HSE crystal value / divisor math).","Add an early debug assertion on your computed frequencies before constructing the config."],"exampleFix":"// before\nlet freq: Option<Hertz> = Some(Hertz(0));\n// after\nlet freq: Option<Hertz> = None; // or Some(Hertz::mhz(16))","handlingStrategy":"validation","validationCode":"fn checked_maybe(h: Option<Hertz>) -> Option<Hertz> { match h { Some(Hertz(0)) | None => None, other => other } }","typeGuard":"fn is_valid_freq(f: Option<Hertz>) -> bool { !matches!(f, Some(Hertz(0))) }","tryCatchPattern":null,"preventionTips":["Map zero/absent frequencies to None instead of Some(Hertz(0)).","Validate computed clock values are nonzero before building configs.","Check HSE crystal constants and divisor math in board files."],"tags":["time","clock","panic","stm32"],"backgroundTag":"invalid-argument-value","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}