Skip to main content

oxstack_cardputer/
radio.rs

1//! Wi-Fi and Bluetooth LE through Espressif's `esp-radio`.
2//!
3//! The radio's MAC/PHY firmware is only available from Espressif as binary
4//! libraries, which `esp-radio` wraps. This board crate's job is ownership
5//! and memory: with the `radio` feature, [`crate::init`] gives the global
6//! heap the ~72 KB of RAM the second-stage bootloader used, which is free
7//! once the app runs. The radio's buffers therefore don't compete with the
8//! framebuffer or audio streams.
9//!
10//! Enable `wifi` and/or `ble` on this crate for the helpers in
11//! [`crate::net`] and [`crate::ble`], or use `esp_radio` directly
12//! (re-exported here). Running both at once (coexistence) needs more heap
13//! than the reclaimed region provides; add a second region with
14//! [`esp_alloc::heap_allocator!`] in the app.
15
16use esp_hal::peripherals::{BT, WIFI};
17
18#[cfg(feature = "radio")]
19pub use {esp_alloc, esp_radio};
20
21/// Bytes of reclaimed bootloader RAM handed to the heap (the value
22/// `esp-generate` uses for the ESP32-S3).
23pub const RECLAIMED_HEAP_BYTES: usize = 73_744;
24
25/// The radio peripherals.
26#[allow(missing_docs)]
27pub struct RadioResources<'d> {
28    pub wifi: WIFI<'d>,
29    pub bt: BT<'d>,
30}
31reborrow_group!(RadioResources { wifi, bt });
32
33#[cfg(feature = "radio")]
34pub(crate) fn init_heap() {
35    esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: RECLAIMED_HEAP_BYTES);
36}
37
38/// Heap usage in bytes: `(used, free)`. Available with the `radio` feature.
39#[cfg(feature = "radio")]
40pub fn heap_usage() -> (usize, usize) {
41    (esp_alloc::HEAP.used(), esp_alloc::HEAP.free())
42}