esp_wifi/common_adapter/
mod.rsuse core::ptr::addr_of;
use esp_wifi_sys::include::timeval;
use hal::macros::ram;
use crate::{
binary::include::{esp_event_base_t, esp_timer_get_time},
compat::{common::*, timer_compat::*},
hal,
};
#[cfg_attr(esp32c3, path = "common_adapter_esp32c3.rs")]
#[cfg_attr(esp32c2, path = "common_adapter_esp32c2.rs")]
#[cfg_attr(esp32c6, path = "common_adapter_esp32c6.rs")]
#[cfg_attr(esp32h2, path = "common_adapter_esp32h2.rs")]
#[cfg_attr(esp32, path = "common_adapter_esp32.rs")]
#[cfg_attr(esp32s3, path = "common_adapter_esp32s3.rs")]
#[cfg_attr(esp32s2, path = "common_adapter_esp32s2.rs")]
pub(crate) mod chip_specific;
#[cfg_attr(esp32c3, path = "phy_init_data_esp32c3.rs")]
#[cfg_attr(esp32c2, path = "phy_init_data_esp32c2.rs")]
#[cfg_attr(esp32c6, path = "phy_init_data_esp32c6.rs")]
#[cfg_attr(esp32h2, path = "phy_init_data_esp32h2.rs")]
#[cfg_attr(esp32, path = "phy_init_data_esp32.rs")]
#[cfg_attr(esp32s3, path = "phy_init_data_esp32s3.rs")]
#[cfg_attr(esp32s2, path = "phy_init_data_esp32s2.rs")]
pub(crate) mod phy_init_data;
#[allow(unused)]
pub unsafe extern "C" fn semphr_create(max: u32, init: u32) -> *mut crate::binary::c_types::c_void {
trace!("semphr_create - max {} init {}", max, init);
sem_create(max, init)
}
#[allow(unused)]
pub unsafe extern "C" fn semphr_delete(semphr: *mut crate::binary::c_types::c_void) {
trace!("semphr_delete {:?}", semphr);
sem_delete(semphr);
}
#[ram]
pub unsafe extern "C" fn semphr_take(
semphr: *mut crate::binary::c_types::c_void,
tick: u32,
) -> i32 {
sem_take(semphr, tick)
}
#[ram]
pub unsafe extern "C" fn semphr_give(semphr: *mut crate::binary::c_types::c_void) -> i32 {
sem_give(semphr)
}
#[allow(unused)]
#[ram]
#[no_mangle]
pub unsafe extern "C" fn random() -> crate::binary::c_types::c_ulong {
trace!("random");
let mut rng = esp_hal::rng::Rng::new(unsafe { esp_hal::peripherals::RNG::steal() });
rng.random()
}
pub unsafe extern "C" fn read_mac(mac: *mut u8, type_: u32) -> crate::binary::c_types::c_int {
trace!("read_mac {:?} {}", mac, type_);
let base_mac = crate::hal::efuse::Efuse::mac_address();
for (i, &byte) in base_mac.iter().enumerate() {
mac.add(i).write_volatile(byte);
}
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;
}
}
}
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
}
#[allow(unused)]
#[ram]
pub(crate) unsafe extern "C" fn semphr_take_from_isr(sem: *const (), hptw: *const ()) -> i32 {
trace!("sem take from isr");
(hptw as *mut u32).write_volatile(0);
crate::common_adapter::semphr_take(sem as *mut crate::binary::c_types::c_void, 0)
}
#[allow(unused)]
#[ram]
pub(crate) unsafe extern "C" fn semphr_give_from_isr(sem: *const (), hptw: *const ()) -> i32 {
trace!("sem give from isr");
(hptw as *mut u32).write_volatile(0);
crate::common_adapter::semphr_give(sem as *mut crate::binary::c_types::c_void)
}
#[no_mangle]
pub unsafe extern "C" fn puts(s: *const u8) {
let cstr = str_from_c(s);
info!("{}", cstr);
}
static mut EVT: i8 = 0;
#[no_mangle]
#[allow(unused_unsafe)]
static mut WIFI_EVENT: esp_event_base_t = unsafe { addr_of!(EVT) };
#[no_mangle]
pub unsafe extern "C" fn __assert_func(
file: *const u8,
line: u32,
func: *const u8,
failed_expr: *const u8,
) {
let file = str_from_c(file);
let (func_pre, func) = if func.is_null() {
("", "")
} else {
(", function: ", str_from_c(func))
};
let expr = str_from_c(failed_expr);
panic!(
"assertion \"{}\" failed: file \"{}\", line {}{}{}",
expr, file, line, func_pre, func
);
}
#[no_mangle]
pub unsafe extern "C" fn ets_timer_disarm(timer: *mut crate::binary::c_types::c_void) {
compat_timer_disarm(timer.cast());
}
#[no_mangle]
pub unsafe extern "C" fn ets_timer_done(timer: *mut crate::binary::c_types::c_void) {
compat_timer_done(timer.cast());
}
#[no_mangle]
pub unsafe extern "C" fn ets_timer_setfn(
ptimer: *mut crate::binary::c_types::c_void,
pfunction: *mut crate::binary::c_types::c_void,
parg: *mut crate::binary::c_types::c_void,
) {
compat_timer_setfn(
ptimer.cast(),
core::mem::transmute::<
*mut crate::binary::c_types::c_void,
unsafe extern "C" fn(*mut crate::binary::c_types::c_void),
>(pfunction),
parg,
);
}
#[no_mangle]
pub unsafe extern "C" fn ets_timer_arm(
timer: *mut crate::binary::c_types::c_void,
tmout: u32,
repeat: bool,
) {
compat_timer_arm(timer.cast(), tmout, repeat);
}
#[no_mangle]
pub unsafe extern "C" fn ets_timer_arm_us(
timer: *mut crate::binary::c_types::c_void,
tmout: u32,
repeat: bool,
) {
compat_timer_arm_us(timer.cast(), tmout, repeat);
}
#[no_mangle]
pub unsafe extern "C" fn gettimeofday(tv: *mut timeval, _tz: *mut ()) -> i32 {
if !tv.is_null() {
unsafe {
let microseconds = esp_timer_get_time();
(*tv).tv_sec = (microseconds / 1_000_000) as u64;
(*tv).tv_usec = (microseconds % 1_000_000) as u32;
}
}
0
}
#[no_mangle]
pub unsafe extern "C" fn esp_fill_random(dst: *mut u8, len: u32) {
trace!("esp_fill_random");
let dst = core::slice::from_raw_parts_mut(dst, len as usize);
let mut rng = esp_hal::rng::Rng::new(unsafe { esp_hal::peripherals::RNG::steal() });
for chunk in dst.chunks_mut(4) {
let bytes = rng.random().to_le_bytes();
chunk.copy_from_slice(&bytes[..chunk.len()]);
}
}
#[no_mangle]
pub unsafe extern "C" fn strrchr(_s: *const (), _c: u32) -> *const u8 {
todo!("strrchr");
}
#[cfg(feature = "esp32c6")]
#[no_mangle]
pub unsafe extern "C" fn floor(v: f64) -> f64 {
libm::floor(v)
}