1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![allow(non_camel_case_types)]
use crate::prelude::*;
use crate::target::EFUSE;
pub struct Efuse;
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum ChipType {
ESP32_D0WDQ6,
ESP32_D0WDQ5,
ESP32_D2WDQ5,
ESP32_PICOD2,
ESP32_PICOD4,
Unknown,
}
impl Efuse {
pub fn get_mac_address() -> [u8; 6] {
let efuse = unsafe { &*EFUSE::ptr() };
let mac_low: u32 = efuse.blk0_rdata1.read().rd_wifi_mac_crc_low().bits();
let mac_high: u32 = efuse.blk0_rdata2.read().rd_wifi_mac_crc_high().bits();
let mac_low_bytes = mac_low.to_be_bytes();
let mac_high_bytes = mac_high.to_be_bytes();
[
mac_high_bytes[2],
mac_high_bytes[3],
mac_low_bytes[0],
mac_low_bytes[1],
mac_low_bytes[2],
mac_low_bytes[3],
]
}
pub fn get_core_count() -> u32 {
let efuse = unsafe { &*EFUSE::ptr() };
let cpu_disabled = efuse.blk0_rdata3.read().rd_chip_ver_dis_app_cpu().bit();
if cpu_disabled {
1
} else {
2
}
}
pub fn get_max_cpu_fequency() -> Hertz {
let efuse = unsafe { &*EFUSE::ptr() };
let has_rating = efuse.blk0_rdata3.read().rd_chip_cpu_freq_rated().bit();
let has_low_rating = efuse.blk0_rdata3.read().rd_chip_cpu_freq_low().bit();
if has_rating && has_low_rating {
Hertz(160_000_000)
} else {
Hertz(240_000_000)
}
}
pub fn is_bluetooth_enabled() -> bool {
let efuse = unsafe { &*EFUSE::ptr() };
!efuse.blk0_rdata3.read().rd_chip_ver_dis_bt().bit()
}
pub fn get_chip_type() -> ChipType {
let efuse = unsafe { &*EFUSE::ptr() };
match efuse.blk0_rdata3.read().rd_chip_ver_pkg().bits() {
0 => ChipType::ESP32_D0WDQ6,
1 => ChipType::ESP32_D0WDQ5,
2 => ChipType::ESP32_D2WDQ5,
4 => ChipType::ESP32_PICOD2,
5 => ChipType::ESP32_PICOD4,
_ => ChipType::Unknown,
}
}
pub fn get_adc_vref() -> Option<i32> {
let efuse = unsafe { &*EFUSE::ptr() };
let base_voltage: i32 = 1100;
let step: i32 = 7;
let calibration = efuse.blk0_rdata4.read().rd_adc_vref().bits();
if calibration == 0 {
return None;
}
let (sign, offset) = ((calibration >> 4) as i32, (calibration & 0x0F) as i32);
if sign == 0 {
Some(base_voltage + (offset * step))
} else {
Some(base_voltage - (offset * step))
}
}
pub fn get_adc1_two_point_cal() -> Option<(i32, i32)> {
let efuse = unsafe { &*EFUSE::ptr() };
let adc1_low_base: i32 = 278;
let adc1_high_base: i32 = 3265;
let adc1_step: i32 = 4;
let adc2_low_bit_size = 7;
let adc2_high_bit_size = 9;
let adc1_low = efuse.blk3_rdata3.read().rd_adc1_tp_low().bits() as u16;
let adc1_high = efuse.blk3_rdata3.read().rd_adc1_tp_high().bits() as u16;
if adc1_low == 0 || adc1_high == 0 {
None
} else {
Some((
adc1_low_base
+ Efuse::from_twos_complement(adc1_low, adc2_low_bit_size) * adc1_step,
adc1_high_base
+ Efuse::from_twos_complement(adc1_high, adc2_high_bit_size) * adc1_step,
))
}
}
pub fn get_adc2_two_point_cal() -> Option<(i32, i32)> {
let efuse = unsafe { &*EFUSE::ptr() };
let adc2_low_base: i32 = 421;
let adc2_high_base: i32 = 3406;
let adc2_step: i32 = 4;
let adc2_low_bit_size = 7;
let adc2_high_bit_size = 9;
let adc2_low = efuse.blk3_rdata3.read().rd_adc2_tp_low().bits() as u16;
let adc2_high = efuse.blk3_rdata3.read().rd_adc2_tp_high().bits() as u16;
if adc2_low == 0 || adc2_high == 0 {
None
} else {
Some((
adc2_low_base
+ Efuse::from_twos_complement(adc2_low, adc2_low_bit_size) * adc2_step,
adc2_high_base
+ Efuse::from_twos_complement(adc2_high, adc2_high_bit_size) * adc2_step,
))
}
}
fn from_twos_complement(value: u16, bits: u8) -> i32 {
let mask = 2_u16.pow(bits as u32 - 1) - 1;
let complement_value = (value & mask) as i32;
let sign_bit = (value >> bits - 1) & 0x01;
if sign_bit == 0 {
complement_value
} else {
complement_value - 2_i32.pow(bits as u32 - 1)
}
}
}