{"record":{"id":"d14808b46e1141d1","repo":"embassy-rs/embassy","slug":"cannot-get-cc-register-of-timer-with-cc-regi","errorCode":null,"errorMessage":"Cannot get CC register {} of timer with {} CC registers.","messagePattern":"Cannot get CC register (.+?) of timer with (.+?) CC registers\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-nrf/src/timer.rs","lineNumber":225,"sourceCode":"    /// This will stop the timer if it isn't already stopped,\n    /// because the timer may exhibit 'unpredictable behaviour' if it's frequency is changed while it's running.\n    pub fn set_frequency(&self, frequency: Frequency) {\n        self.stop();\n\n        self.r\n            .prescaler()\n            // SAFETY: `frequency` is a variant of `Frequency`,\n            // whose values are all in the range of 0-9 (the valid range of `prescaler`).\n            .write(|w| w.set_prescaler(frequency as u8))\n    }\n\n    /// Returns this timer's `n`th CC register.\n    ///\n    /// # Panics\n    /// Panics if `n` >= the number of CC registers this timer has (4 for a normal timer, 6 for an extended timer).\n    pub fn cc(&self, n: usize) -> Cc<'d> {\n        if n >= self.ccs {\n            panic!(\"Cannot get CC register {} of timer with {} CC registers.\", n, self.ccs);\n        }\n        Cc {\n            n,\n            r: self.r,\n            _p: PhantomData,\n        }\n    }\n}\n\nimpl Timer<'static> {\n    /// Persist the timer's configuration for the rest of the program's lifetime. This method\n    /// should be preferred over [`core::mem::forget()`] because the `'static` bound prevents\n    /// accidental reuse of the underlying peripheral.\n    pub fn persist(self) {\n        core::mem::forget(self);\n    }\n}\n","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-nrf/src/timer.rs#L207-L243","documentation":"Timer::cc panics when asked for a CC (capture/compare) register index n that is greater than or equal to the number of CC registers the timer instance actually has — 4 for a normal timer, 6 for an extended timer (e.g. TIMER0 on some SoCs). This guards against out-of-bounds register access on hardware.","triggerScenarios":"Calling timer.cc(n) with n >= self.ccs, e.g. timer.cc(4) on a 4-channel timer, or looping 0..6 over every timer instance instead of only extended ones; also occurs when code assuming extended-timer behavior runs against a normal timer peripheral.","commonSituations":"Shared timer-driver code written for an extended timer reused with a plain TIMER instance; hardcoding cc(5) or cc(4); iterating a fixed count of CC channels for PWM capture/compare setup on multiple timers.","solutions":["Use the timer's actual channel count — iterate with 0..timer.cc_count() (or the ccs value) instead of a hardcoded bound.","Assign the code needing 6 CC channels to an extended timer instance (e.g. one with 6 CC registers on your SoC).","Check the embassy-nrf timer bindings for your chip to confirm which instances are extended before indexing."],"exampleFix":"// before\nfor i in 0..6 {\n    let cc = timer.cc(i); // panics on 4-CC timers\n}\n// after\nlet ccs = timer.cc_count();\nfor i in 0..ccs {\n    let cc = timer.cc(i);\n}","handlingStrategy":"validation","validationCode":"let ccs: usize = timer.cc_count();\nif n < ccs {\n    let cc = timer.cc(n);\n}","typeGuard":"fn has_cc(timer: &Timer<'_, T>, n: usize) -> bool { n < timer.cc_count() }","tryCatchPattern":null,"preventionTips":["Never hardcode CC indices; derive loop bounds from the timer's channel count.","Only use CC channels 4-5 on timer instances that are extended (6 CC registers).","Check your SoC's embassy-nrf peripheral bindings to know which timers are extended."],"tags":["embedded","nrf","timer","index-out-of-bounds","panic"],"backgroundTag":"index-out-of-range","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"}