1//! NVM Software Calibration Area Mapping
2// For samd11, see 9.5 NVM Software Calibration Area Mapping, page 24
3// For samd21, see 10.3.2 NVM Software Calibration Area Mapping, page 46
45use atsamd_hal_macros::hal_cfg;
6use core::ptr;
78const ADDR: u32 = 0x806020u32;
910fn cal(addr_offset: u32, bit_shift: u32, bit_mask: u32) -> u32 {
11unsafe {
12let addr: *const u32 = (ADDR + addr_offset) as *const _;
13let value = ptr::read(addr);
1415 (value >> bit_shift) & bit_mask
16 }
17}
1819fn cal_with_errata(
20 addr_offset: u32,
21 bit_shift: u32,
22 bit_mask: u32,
23 bad_val: u32,
24 def_val: u32,
25) -> u32 {
26let val = cal(addr_offset, bit_shift, bit_mask);
27// if the value matches the bad value, use an alternative value
28 // specified in the the errata section of the datasheet
29if val == bad_val {
30 def_val
31 } else {
32 val
33 }
34}
3536/// Returns the osc32k calibration value from the NVM calibration area
37pub fn osc32k_cal() -> u8 {
38 cal(4, 6, 0x7f) as u8
39}
4041/// Returns the dfll48m coarse calibration value
42pub fn dfll48m_coarse_cal() -> u8 {
43 cal_with_errata(4, 26, 0x3f, 0x3f, 0x1f) as u8
44}
4546/// USB TRANSN calibration value. Should be written to USB PADCAL register.
47pub fn usb_transn_cal() -> u8 {
48 cal_with_errata(4, 13, 0x1f, 0x1f, 5) as u8
49}
5051/// USB TRANSP calibration value. Should be written to USB PADCAL register.
52pub fn usb_transp_cal() -> u8 {
53 cal_with_errata(4, 18, 0x1f, 0x1f, 29) as u8
54}
5556/// USB TRIM calibration value. Should be written to USB PADCAL register.
57#[hal_cfg("nvmctrl-d11")]
58pub fn usb_trim_cal() -> u8 {
59 cal_with_errata(4, 23, 7, 7, 5) as u8
60}
6162/// USB TRIM calibration value. Should be written to USB PADCAL register.
63#[hal_cfg("nvmctrl-d21")]
64pub fn usb_trim_cal() -> u8 {
65 cal_with_errata(4, 23, 7, 7, 3) as u8
66}