atsamd_hal/peripherals/calibration/d5x.rs
1//! NVM Software Calibration Area Mapping
2// See 9.5 NVM Software Calibration Area Mapping, page 57
3
4use core::ptr;
5
6// "The NVM Software Calibration Area can be read at address 0x00800080."
7const ADDR: u32 = 0x00800080;
8
9// Calculations for this from the datasheet values:
10// Example: Bit position: 41:37
11//
12// addr_offset = int(37/8) = 4
13// bit_shift = 37 % 8 = 5
14// Mask length = 41-37+1 = 5 bits (0b11111)
15fn cal(addr_offset: u32, bit_shift: u32, bit_mask: u32) -> u32 {
16 unsafe {
17 let addr: *const u32 = (ADDR + addr_offset) as *const _;
18 let value = ptr::read_unaligned(addr);
19
20 (value >> bit_shift) & bit_mask
21 }
22}
23
24/// USB TRANSN calibration value. Should be written to USB PADCAL register.
25pub fn usb_transn_cal() -> u8 {
26 cal(4, 0, 0b11111) as u8
27}
28
29/// USB TRANSP calibration value. Should be written to USB PADCAL register.
30pub fn usb_transp_cal() -> u8 {
31 cal(4, 5, 0b11111) as u8
32}
33
34/// USB TRIM calibration value. Should be written to USB PADCAL register.
35pub fn usb_trim_cal() -> u8 {
36 cal(5, 2, 0b111) as u8
37}
38
39/// ADC0 BIASCOMP calibration value. Should be written to ADC0 CALIB register.
40pub fn adc0_biascomp_scale_cal() -> u8 {
41 cal(0, 2, 0b111) as u8
42}
43
44/// ADC0 BIASREFBUF calibration value. Should be written to ADC0 CALIB register.
45pub fn adc0_biasref_scale_cal() -> u8 {
46 cal(0, 5, 0b111) as u8
47}
48
49/// ADC0 BIASR2R calibration value. Should be written to ADC0 CALIB register.
50pub fn adc0_biasr2r_scale_cal() -> u8 {
51 cal(1, 0, 0b111) as u8
52}
53
54/// ADC1 BIASCOMP calibration value. Should be written to ADC1 CALIB register.
55pub fn adc1_biascomp_scale_cal() -> u8 {
56 cal(2, 0, 0b111) as u8
57}
58
59/// ADC1 BIASREFBUF calibration value. Should be written to ADC1 CALIB register.
60pub fn adc1_biasref_scale_cal() -> u8 {
61 cal(2, 3, 0b111) as u8
62}
63
64/// ADC1 BIASR2R calibration value. Should be written to ADC1 CALIB register.
65pub fn adc1_biasr2r_scale_cal() -> u8 {
66 cal(2, 6, 0b111) as u8
67}
68
69/// Calibration temperature parameter 'TL', formed by TLI and TLD (TL'Integer', TL'Decimal')
70pub fn tl() -> f32 {
71 tli() as f32 + (tld() as f32 / 10.0)
72}
73
74/// Calibration temperature parameter 'TH', formed by THI and THD (TH'Integer', TH'Decimal')
75pub fn th() -> f32 {
76 thi() as f32 + (thd() as f32 / 10.0)
77}
78
79/// Temperature calibration - Integer part of calibration temperature TL
80pub fn tli() -> u32 {
81 cal(0x80, 0, 0b11111111)
82}
83
84/// Temperature calibration - Decimal part of calibration temperature TL
85pub fn tld() -> u32 {
86 cal(0x80 + 1, 0, 0b1111)
87}
88
89/// Temperature calibration - Integer part of calibration temperature TH
90pub fn thi() -> u32 {
91 cal(0x80 + 1, 4, 0b11111111)
92}
93
94/// Temperature calibration - Decimal part of calibration temperature TH
95pub fn thd() -> u32 {
96 cal(0x80 + 2, 4, 0b1111)
97}
98
99/// Temperature calibration - Parameter VPL
100pub fn vpl() -> u16 {
101 cal(0x80 + 5, 0, 0b111111111111) as u16
102}
103
104/// Temperature calibration - Parameter VPH
105pub fn vph() -> u16 {
106 cal(0x80 + 6, 4, 0b111111111111) as u16
107}
108
109/// Temperature calibration - Parameter VCL
110pub fn vcl() -> u16 {
111 cal(0x80 + 8, 0, 0b111111111111) as u16
112}
113
114/// Temperature calibration - Parameter VCH
115pub fn vch() -> u16 {
116 cal(0x80 + 9, 4, 0b111111111111) as u16
117}