atsamd_hal/peripherals/calibration/
d11.rs

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
4
5use atsamd_hal_macros::hal_cfg;
6use core::ptr;
7
8const ADDR: u32 = 0x806020u32;
9
10fn cal(addr_offset: u32, bit_shift: u32, bit_mask: u32) -> u32 {
11    unsafe {
12        let addr: *const u32 = (ADDR + addr_offset) as *const _;
13        let value = ptr::read(addr);
14
15        (value >> bit_shift) & bit_mask
16    }
17}
18
19fn 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 {
26    let 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
29    if val == bad_val {
30        def_val
31    } else {
32        val
33    }
34}
35
36/// Returns the osc32k calibration value from the NVM calibration area
37pub fn osc32k_cal() -> u8 {
38    cal(4, 6, 0x7f) as u8
39}
40
41/// Returns the dfll48m coarse calibration value
42pub fn dfll48m_coarse_cal() -> u8 {
43    cal_with_errata(4, 26, 0x3f, 0x3f, 0x1f) as u8
44}
45
46/// 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}
50
51/// 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}
55
56/// 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}
61
62/// 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}