Skip to content

Commit

Permalink
Use ESP_HAL to read mac address (esp-rs#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed May 24, 2024
1 parent 943904f commit 97bee31
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 324 deletions.
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ unsafe extern "C" fn coex_schm_status_bit_clear(_typ: i32, status: i32) {

#[ram]
unsafe extern "C" fn read_efuse_mac(mac: *const ()) -> i32 {
crate::common_adapter::chip_specific::read_mac(mac as *mut _, 2)
crate::common_adapter::read_mac(mac as *mut _, 2)
}

#[cfg(feature = "esp32")]
Expand Down
3 changes: 1 addition & 2 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,8 @@ pub(crate) fn ble_init() {
#[cfg(coex)]
crate::binary::include::coex_enable();

// let mac: [u8; 6] = [0x6a, 0x9b, 0x4d, 0x6f, 0x64, 0x80]; // TODO better not hardcode :)
let mut mac = [0u8; 6];
crate::common_adapter::chip_specific::read_mac(mac.as_mut_ptr(), 2);
crate::common_adapter::read_mac(mac.as_mut_ptr(), 2);
mac.reverse();

esp_ble_ll_set_public_addr(&mac as *const u8);
Expand Down
51 changes: 0 additions & 51 deletions esp-wifi/src/common_adapter/common_adapter_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,57 +130,6 @@ pub(crate) unsafe fn phy_disable_clock() {
}
}

#[ram]
pub(crate) unsafe extern "C" fn read_mac(
mac: *mut u8,
type_: u32,
) -> crate::binary::c_types::c_int {
trace!("read_mac {:p} {}", mac, type_);

let mut regval = [0u32; 2];
let data = &regval as *const _ as *const u8;
regval[0] = (0x3ff5a004 as *const u32).read_volatile();
regval[1] = (0x3ff5a008 as *const u32).read_volatile();

for i in 0..6 {
mac.offset(i)
.write_volatile(data.offset(5 - i).read_volatile());
}

/* ESP_MAC_WIFI_SOFTAP */
if type_ == 1 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}
}

// ESP_MAC_BT
if type_ == 2 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}

mac.offset(5)
.write_volatile(mac.offset(5).read_volatile() + 1);
}

0
}

#[ram]
pub(crate) fn init_clocks() {
unsafe {
Expand Down
50 changes: 0 additions & 50 deletions esp-wifi/src/common_adapter/common_adapter_esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,56 +132,6 @@ pub(crate) unsafe fn phy_disable_clock() {
trace!("phy_enable_clock done!");
}

pub(crate) unsafe extern "C" fn read_mac(
mac: *mut u8,
type_: u32,
) -> crate::binary::c_types::c_int {
trace!("read_mac {:p} {}", mac, type_);

let mut regval = [0u32; 2];
let data = &regval as *const _ as *const u8;
regval[0] = ((0x60008800 + 0x40) as *const u32).read_volatile();
regval[1] = ((0x60008800 + 0x44) as *const u32).read_volatile();

for i in 0..6 {
mac.offset(i)
.write_volatile(data.offset(5 - i).read_volatile());
}

/* ESP_MAC_WIFI_SOFTAP */
if type_ == 1 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}
}

// ESP_MAC_BT
if type_ == 2 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}

mac.offset(5)
.write_volatile(mac.offset(5).read_volatile() + 1);
}

0
}

pub(crate) fn init_clocks() {
unsafe {
// PERIP_CLK_EN0
Expand Down
50 changes: 0 additions & 50 deletions esp-wifi/src/common_adapter/common_adapter_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,56 +132,6 @@ pub(crate) unsafe fn phy_disable_clock() {
trace!("phy_enable_clock done!");
}

pub(crate) unsafe extern "C" fn read_mac(
mac: *mut u8,
type_: u32,
) -> crate::binary::c_types::c_int {
trace!("read_mac {:p} {}", mac, type_);

let mut regval = [0u32; 2];
let data = &regval as *const _ as *const u8;
regval[0] = ((0x60008800 + 0x44) as *const u32).read_volatile();
regval[1] = ((0x60008800 + 0x48) as *const u32).read_volatile();

for i in 0..6 {
mac.offset(i)
.write_volatile(data.offset(5 - i).read_volatile());
}

/* ESP_MAC_WIFI_SOFTAP */
if type_ == 1 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}
}

// ESP_MAC_BT
if type_ == 2 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}

mac.offset(5)
.write_volatile(mac.offset(5).read_volatile() + 1);
}

0
}

pub(crate) fn init_clocks() {
unsafe {
// PERIP_CLK_EN0
Expand Down
50 changes: 0 additions & 50 deletions esp-wifi/src/common_adapter/common_adapter_esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,56 +159,6 @@ pub(crate) unsafe fn phy_disable_clock() {
trace!("phy_enable_clock done!");
}

pub(crate) unsafe extern "C" fn read_mac(
mac: *mut u8,
type_: u32,
) -> crate::binary::c_types::c_int {
trace!("read_mac {:p} {}", mac, type_);

let mut regval = [0u32; 2];
let data = &regval as *const _ as *const u8;
regval[0] = ((0x600b0800 + 0x44) as *const u32).read_volatile();
regval[1] = ((0x600b0800 + 0x48) as *const u32).read_volatile();

for i in 0..6 {
mac.offset(i)
.write_volatile(data.offset(5 - i).read_volatile());
}

/* ESP_MAC_WIFI_SOFTAP */
if type_ == 1 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}
}

// ESP_MAC_BT
if type_ == 2 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}

mac.offset(5)
.write_volatile(mac.offset(5).read_volatile() + 1);
}

0
}

pub(crate) fn init_clocks() {
unsafe {
let pmu = &*esp32c6::PMU::PTR;
Expand Down
50 changes: 0 additions & 50 deletions esp-wifi/src/common_adapter/common_adapter_esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,56 +136,6 @@ pub(crate) fn wifi_reset_mac() {
}
}

pub(crate) unsafe extern "C" fn read_mac(
mac: *mut u8,
type_: u32,
) -> crate::binary::c_types::c_int {
trace!("read_mac {:p} {}", mac, type_);

let mut regval = [0u32; 2];
let data = &regval as *const _ as *const u8;
regval[0] = ((0x3f41A000 + 0x44) as *const u32).read_volatile();
regval[1] = ((0x3f41A000 + 0x48) as *const u32).read_volatile();

for i in 0..6 {
mac.offset(i)
.write_volatile(data.offset(5 - i).read_volatile());
}

/* ESP_MAC_WIFI_SOFTAP */
if type_ == 1 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}
}

// ESP_MAC_BT
if type_ == 2 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}

mac.offset(5)
.write_volatile(mac.offset(5).read_volatile() + 1);
}

0
}

pub(crate) fn init_clocks() {
log::trace!("init clocks");

Expand Down
50 changes: 0 additions & 50 deletions esp-wifi/src/common_adapter/common_adapter_esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,56 +124,6 @@ pub(crate) unsafe fn phy_disable_clock() {
trace!("phy_enable_clock done!");
}

pub(crate) unsafe extern "C" fn read_mac(
mac: *mut u8,
type_: u32,
) -> crate::binary::c_types::c_int {
trace!("read_mac {:p} {}", mac, type_);

let mut regval = [0u32; 2];
let data = &regval as *const _ as *const u8;
regval[0] = ((0x60007000 + 0x44) as *const u32).read_volatile();
regval[1] = ((0x60007000 + 0x48) as *const u32).read_volatile();

for i in 0..6 {
mac.offset(i)
.write_volatile(data.offset(5 - i).read_volatile());
}

/* ESP_MAC_WIFI_SOFTAP */
if type_ == 1 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}
}

// ESP_MAC_BT
if type_ == 2 {
let tmp = mac.offset(0).read_volatile();
for i in 0..64 {
mac.offset(0).write_volatile(tmp | 0x02);
mac.offset(0)
.write_volatile(mac.offset(0).read_volatile() ^ (i << 2));

if mac.offset(0).read_volatile() != tmp {
break;
}
}

mac.offset(5)
.write_volatile(mac.offset(5).read_volatile() + 1);
}

0
}

pub(crate) fn init_clocks() {
unsafe {
// PERIP_CLK_EN0
Expand Down
Loading

0 comments on commit 97bee31

Please sign in to comment.