Skip to content

Commit

Permalink
Replace a panic with a compile error (esp-rs#321)
Browse files Browse the repository at this point in the history
* Fix fn pointer signatures

* Clean up scoping a bit

* Simplify clock validation

* Make WifiBle option only available when coex is enabled

* Check for wifi and ble if coex is enabled

* Simplify BTDM osi funcs setup
  • Loading branch information
bugadani authored and bjoernQ committed May 23, 2024
1 parent af97e42 commit 5e485a5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 56 deletions.
11 changes: 10 additions & 1 deletion esp-wifi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ fn main() -> Result<(), String> {
println!("cargo:rustc-cfg=esp32s3");

#[cfg(feature = "coex")]
println!("cargo:rustc-cfg=coex");
{
#[cfg(all(feature = "wifi", feature = "ble"))]
println!("cargo:rustc-cfg=coex");

#[cfg(not(feature = "wifi"))]
println!("cargo:warning=coex is enabled but wifi is not");

#[cfg(not(feature = "ble"))]
println!("cargo:warning=coex is enabled but wifi is not");
}

validate_config();

Expand Down
8 changes: 4 additions & 4 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::cell::RefCell;
use core::{cell::RefCell, ptr::addr_of};

use critical_section::Mutex;

use crate::ble::btdm::ble_os_adapter_chip_specific::G_OSI_FUNCS;
use crate::ble::btdm::ble_os_adapter_chip_specific::{osi_funcs_s, G_OSI_FUNCS};
use crate::ble::HciOutCollector;
use crate::ble::HCI_OUT_COLLECTOR;
use crate::hal::macros::ram;
Expand Down Expand Up @@ -38,7 +38,7 @@ struct vhci_host_callback_s {
}

extern "C" {
fn btdm_osi_funcs_register(osi_funcs: *const ()) -> i32;
fn btdm_osi_funcs_register(osi_funcs: *const osi_funcs_s) -> i32;
fn btdm_controller_get_compile_version() -> *const u8;

#[cfg(any(esp32c3, esp32s3))]
Expand Down Expand Up @@ -446,7 +446,7 @@ pub(crate) fn ble_init() {

let mut cfg = ble_os_adapter_chip_specific::create_ble_config();

let res = btdm_osi_funcs_register(&G_OSI_FUNCS as *const _ as *const ());
let res = btdm_osi_funcs_register(addr_of!(G_OSI_FUNCS));
if res != 0 {
panic!("btdm_osi_funcs_register returned {}", res);
}
Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/src/ble/os_adapter_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(super) struct osi_funcs_s {
version: u32,
interrupt_set: Option<unsafe extern "C" fn(i32, i32, i32, i32)>,
interrupt_clear: Option<unsafe extern "C" fn(i32, i32)>,
interrupt_handler_set: Option<unsafe extern "C" fn(i32, extern "C" fn(), *const ())>,
interrupt_handler_set: Option<unsafe extern "C" fn(i32, extern "C" fn(*const ()), *const ())>,
interrupt_disable: Option<unsafe extern "C" fn()>,
interrupt_enable: Option<unsafe extern "C" fn()>,
task_yield: Option<unsafe extern "C" fn()>,
Expand Down Expand Up @@ -228,7 +228,7 @@ pub(crate) unsafe extern "C" fn interrupt_clear(_interrupt_source: i32, _interru

pub(crate) unsafe extern "C" fn interrupt_handler_set(
interrupt_no: i32,
func: extern "C" fn(),
func: extern "C" fn(*const ()),
arg: *const (),
) {
trace!(
Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/src/ble/os_adapter_esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(super) struct osi_funcs_s {
version: u32,
interrupt_set: Option<unsafe extern "C" fn(i32, i32, i32, i32)>,
interrupt_clear: Option<unsafe extern "C" fn(i32, i32)>,
interrupt_handler_set: Option<unsafe extern "C" fn(i32, extern "C" fn(), *const ())>,
interrupt_handler_set: Option<unsafe extern "C" fn(i32, extern "C" fn(*const ()), *const ())>,
interrupt_disable: Option<unsafe extern "C" fn()>,
interrupt_enable: Option<unsafe extern "C" fn()>,
task_yield: Option<unsafe extern "C" fn()>,
Expand Down Expand Up @@ -222,7 +222,7 @@ pub(crate) unsafe extern "C" fn interrupt_clear(_interrupt_source: i32, _interru

pub(crate) unsafe extern "C" fn interrupt_handler_set(
interrupt_no: i32,
func: extern "C" fn(),
func: extern "C" fn(*const ()),
arg: *const (),
) {
trace!(
Expand Down
70 changes: 26 additions & 44 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub enum EspWifiInitialization {
Wifi(EspWifiInitializationInternal),
#[cfg(feature = "ble")]
Ble(EspWifiInitializationInternal),
#[cfg(all(feature = "wifi", feature = "ble"))]
#[cfg(coex)]
WifiBle(EspWifiInitializationInternal),
}

Expand Down Expand Up @@ -215,7 +215,7 @@ pub enum EspWifiInitFor {
Wifi,
#[cfg(feature = "ble")]
Ble,
#[cfg(all(feature = "wifi", feature = "ble"))]
#[cfg(coex)]
WifiBle,
}

Expand Down Expand Up @@ -247,28 +247,16 @@ pub fn initialize(
radio_clocks: hal::system::RadioClockControl,
clocks: &Clocks,
) -> Result<EspWifiInitialization, InitializationError> {
#[cfg(all(not(coex), feature = "wifi", feature = "ble"))]
if init_for == EspWifiInitFor::WifiBle {
panic!("Trying to use Wifi and BLE without COEX feature");
}

#[cfg(any(esp32, esp32s3, esp32s2))]
if clocks.cpu_clock != MegahertzU32::MHz(240) {
return Err(InitializationError::WrongClockConfig);
}
const MAX_CLOCK: u32 = 240;

#[cfg(esp32c6)]
if clocks.cpu_clock != MegahertzU32::MHz(160) {
return Err(InitializationError::WrongClockConfig);
}

#[cfg(esp32c3)]
if clocks.cpu_clock != MegahertzU32::MHz(160) {
return Err(InitializationError::WrongClockConfig);
}
#[cfg(any(esp32c3, esp32c6))]
const MAX_CLOCK: u32 = 160;

#[cfg(esp32c2)]
if clocks.cpu_clock != MegahertzU32::MHz(120) {
const MAX_CLOCK: u32 = 120;

if clocks.cpu_clock != MegahertzU32::MHz(MAX_CLOCK) {
return Err(InitializationError::WrongClockConfig);
}

Expand Down Expand Up @@ -298,40 +286,33 @@ pub fn initialize(
init_clocks();

#[cfg(coex)]
{
debug!("coex init");
let res = crate::wifi::coex_initialize();
if res != 0 {
return Err(InitializationError::General(res));
}
match crate::wifi::coex_initialize() {
0 => {}
error => return Err(InitializationError::General(error)),
}

#[cfg(feature = "wifi")]
{
if init_for.is_wifi() {
debug!("wifi init");
// wifi init
crate::wifi::wifi_init()?;
}
if init_for.is_wifi() {
debug!("wifi init");
// wifi init
crate::wifi::wifi_init()?;
}

#[cfg(feature = "ble")]
{
if init_for.is_ble() {
// ble init
// for some reason things don't work when initializing things the other way around
// while the original implementation in NuttX does it like that
debug!("ble init");
crate::ble::ble_init();
}
if init_for.is_ble() {
// ble init
// for some reason things don't work when initializing things the other way around
// while the original implementation in NuttX does it like that
debug!("ble init");
crate::ble::ble_init();
}

match init_for {
#[cfg(feature = "wifi")]
EspWifiInitFor::Wifi => Ok(EspWifiInitialization::Wifi(EspWifiInitializationInternal)),
#[cfg(feature = "ble")]
EspWifiInitFor::Ble => Ok(EspWifiInitialization::Ble(EspWifiInitializationInternal)),
#[cfg(all(feature = "wifi", feature = "ble"))]
#[cfg(coex)]
EspWifiInitFor::WifiBle => Ok(EspWifiInitialization::WifiBle(
EspWifiInitializationInternal,
)),
Expand Down Expand Up @@ -360,10 +341,11 @@ impl From<WifiError> for InitializationError {
pub fn wifi_set_log_verbose() {
#[cfg(feature = "wifi-logs")]
unsafe {
use crate::binary::include::{esp_wifi_internal_set_log_level, wifi_log_level_t};
use crate::binary::include::{
esp_wifi_internal_set_log_level, wifi_log_level_t_WIFI_LOG_VERBOSE,
};

let level: wifi_log_level_t = crate::binary::include::wifi_log_level_t_WIFI_LOG_VERBOSE;
esp_wifi_internal_set_log_level(level);
esp_wifi_internal_set_log_level(wifi_log_level_t_WIFI_LOG_VERBOSE);
}
}

Expand Down
4 changes: 1 addition & 3 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,7 @@ pub fn wifi_init() -> Result<(), WifiError> {
G_CONFIG.feature_caps = g_wifi_feature_caps;

#[cfg(coex)]
{
esp_wifi_result!(coex_init())?;
}
esp_wifi_result!(coex_init())?;

esp_wifi_result!(esp_wifi_init_internal(&G_CONFIG))?;
esp_wifi_result!(esp_wifi_set_mode(wifi_mode_t_WIFI_MODE_NULL))?;
Expand Down

0 comments on commit 5e485a5

Please sign in to comment.