use core::mem::offset_of;
use crate::hwcore::addr::MmioAddr;
use crate::hwcore::regs::access::Rw;
#[repr(C)]
pub struct TimBasicRegs {
pub cr1: Rw<u32>,
pub cr2: Rw<u32>,
_reserved_08: u32,
pub dier: Rw<u32>,
pub sr: Rw<u32>,
pub egr: Rw<u32>,
_reserved_18: [u32; 3],
pub cnt: Rw<u32>,
pub psc: Rw<u32>,
pub arr: Rw<u32>,
}
const _: () = assert!(offset_of!(TimBasicRegs, cr1) == 0x00);
const _: () = assert!(offset_of!(TimBasicRegs, cr2) == 0x04);
const _: () = assert!(offset_of!(TimBasicRegs, dier) == 0x0C);
const _: () = assert!(offset_of!(TimBasicRegs, sr) == 0x10);
const _: () = assert!(offset_of!(TimBasicRegs, egr) == 0x14);
const _: () = assert!(offset_of!(TimBasicRegs, cnt) == 0x24);
const _: () = assert!(offset_of!(TimBasicRegs, psc) == 0x28);
const _: () = assert!(offset_of!(TimBasicRegs, arr) == 0x2C);
pub const TIM6_BASE: usize = 0x4000_1000;
pub const TIM7_BASE: usize = 0x4000_1400;
pub struct TimBasic {
base: MmioAddr<TimBasicRegs>,
}
impl TimBasic {
pub const unsafe fn new(base: usize) -> Self {
Self {
base: unsafe { MmioAddr::new(base) },
}
}
pub const unsafe fn tim6() -> Self {
unsafe { Self::new(TIM6_BASE) }
}
pub const unsafe fn tim7() -> Self {
unsafe { Self::new(TIM7_BASE) }
}
#[inline]
pub fn regs(&self) -> &TimBasicRegs {
unsafe { &*self.base.as_ptr() }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tim6_sr_matches_legacy_constant() {
assert_eq!(TIM6_BASE + offset_of!(TimBasicRegs, sr), 0x4000_1010);
}
#[test]
fn tim7_layout_matches_freertos_entry_constants() {
assert_eq!(TIM7_BASE + offset_of!(TimBasicRegs, cr1), 0x4000_1400);
assert_eq!(TIM7_BASE + offset_of!(TimBasicRegs, dier), 0x4000_140C);
assert_eq!(TIM7_BASE + offset_of!(TimBasicRegs, sr), 0x4000_1410);
assert_eq!(TIM7_BASE + offset_of!(TimBasicRegs, egr), 0x4000_1414);
assert_eq!(TIM7_BASE + offset_of!(TimBasicRegs, cnt), 0x4000_1424);
assert_eq!(TIM7_BASE + offset_of!(TimBasicRegs, psc), 0x4000_1428);
assert_eq!(TIM7_BASE + offset_of!(TimBasicRegs, arr), 0x4000_142C);
}
}