Skip to content

Commit

Permalink
Merge pull request #202 from bytedream/more-exp32-examples
Browse files Browse the repository at this point in the history
Add more esp32 examples
  • Loading branch information
lulf authored Dec 19, 2024
2 parents aaf2d02 + 4c57d3b commit 19b02ee
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmt = [
"embedded-io/defmt-03",
"embedded-hal/defmt-03"
]
esp = []
log = [
"dep:log",
"trouble-host/log",
Expand Down
4 changes: 4 additions & 0 deletions examples/apps/src/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use embassy_time::{Duration, Timer};
use trouble_host::prelude::*;

/// Size of L2CAP packets
#[cfg(not(feature = "esp"))]
const L2CAP_MTU: usize = 128;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
const L2CAP_MTU: usize = 1017;

/// Max number of connections
const CONNECTIONS_MAX: usize = 1;
Expand Down
6 changes: 5 additions & 1 deletion examples/apps/src/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const L2CAP_TXQ: u8 = 20;
const L2CAP_RXQ: u8 = 20;

/// Size of L2CAP packets
const L2CAP_MTU: usize = 27;
#[cfg(not(feature = "esp"))]
const L2CAP_MTU: usize = 128;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
const L2CAP_MTU: usize = 1017;

/// Max number of connections
const CONNECTIONS_MAX: usize = 1;
Expand Down
6 changes: 5 additions & 1 deletion examples/apps/src/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const L2CAP_TXQ: u8 = 20;
const L2CAP_RXQ: u8 = 20;

/// Size of L2CAP packets
const L2CAP_MTU: usize = 27;
#[cfg(not(feature = "esp"))]
const L2CAP_MTU: usize = 128;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
const L2CAP_MTU: usize = 1017;

/// Max number of connections
const CONNECTIONS_MAX: usize = 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ esp32c6 = "run --release --no-default-features --features=esp32c6 --target=riscv
esp32h2 = "run --release --no-default-features --features=esp32h2 --target=riscv32imac-unknown-none-elf"
esp32s3 = "run --release --no-default-features --features=esp32s3 --target=xtensa-esp32s3-none-elf"

[target.'cfg(all(any(target_arch = "riscv32imc", target_arch = "riscv32imac", target_arch = "xtensa"), target_os = "none"))']
[target.'cfg(all(any(target_arch = "riscv32", target_arch = "xtensa"), target_os = "none"))']
runner = "espflash flash --monitor"

[build]
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ esp-wifi = { version = "0.10.1", features = [
"ble",
] }
heapless = { version = "0.8.0", default-features = false }
trouble-example-apps = { version = "0.1.0", path = "../apps", features = ["log"] }
trouble-example-apps = { version = "0.1.0", path = "../apps", features = ["esp", "log"] }
bt-hci = { version = "0.1.1" }
embassy-futures = "0.1.1"
embassy-time = { version = "0.3", features = ["generic-queue-8"] }
Expand Down
46 changes: 46 additions & 0 deletions examples/esp32/src/bin/ble_bas_central.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![no_std]
#![no_main]

use bt_hci::controller::ExternalController;
use embassy_executor::Spawner;
use esp_hal::prelude::*;
use esp_hal::timer::timg::TimerGroup;
use esp_wifi::ble::controller::BleConnector;
use trouble_example_apps::ble_bas_central;
use {esp_alloc as _, esp_backtrace as _};

#[esp_hal_embassy::main]
async fn main(_s: Spawner) {
esp_println::logger::init_logger_from_env();
let peripherals = esp_hal::init({
let mut config = esp_hal::Config::default();
config.cpu_clock = CpuClock::max();
config
});
esp_alloc::heap_allocator!(72 * 1024);
let timg0 = TimerGroup::new(peripherals.TIMG0);

let init = esp_wifi::init(
timg0.timer0,
esp_hal::rng::Rng::new(peripherals.RNG),
peripherals.RADIO_CLK,
)
.unwrap();

#[cfg(not(feature = "esp32"))]
{
let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER)
.split::<esp_hal::timer::systimer::Target>();
esp_hal_embassy::init(systimer.alarm0);
}
#[cfg(feature = "esp32")]
{
esp_hal_embassy::init(timg0.timer1);
}

let bluetooth = peripherals.BT;
let connector = BleConnector::new(&init, bluetooth);
let controller: ExternalController<_, 20> = ExternalController::new(connector);

ble_bas_central::run(controller).await;
}
46 changes: 46 additions & 0 deletions examples/esp32/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![no_std]
#![no_main]

use bt_hci::controller::ExternalController;
use embassy_executor::Spawner;
use esp_hal::prelude::*;
use esp_hal::timer::timg::TimerGroup;
use esp_wifi::ble::controller::BleConnector;
use trouble_example_apps::ble_l2cap_central;
use {esp_alloc as _, esp_backtrace as _};

#[esp_hal_embassy::main]
async fn main(_s: Spawner) {
esp_println::logger::init_logger_from_env();
let peripherals = esp_hal::init({
let mut config = esp_hal::Config::default();
config.cpu_clock = CpuClock::max();
config
});
esp_alloc::heap_allocator!(72 * 1024);
let timg0 = TimerGroup::new(peripherals.TIMG0);

let init = esp_wifi::init(
timg0.timer0,
esp_hal::rng::Rng::new(peripherals.RNG),
peripherals.RADIO_CLK,
)
.unwrap();

#[cfg(not(feature = "esp32"))]
{
let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER)
.split::<esp_hal::timer::systimer::Target>();
esp_hal_embassy::init(systimer.alarm0);
}
#[cfg(feature = "esp32")]
{
esp_hal_embassy::init(timg0.timer1);
}

let bluetooth = peripherals.BT;
let connector = BleConnector::new(&init, bluetooth);
let controller: ExternalController<_, 20> = ExternalController::new(connector);

ble_l2cap_central::run(controller).await;
}
46 changes: 46 additions & 0 deletions examples/esp32/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![no_std]
#![no_main]

use bt_hci::controller::ExternalController;
use embassy_executor::Spawner;
use esp_hal::prelude::*;
use esp_hal::timer::timg::TimerGroup;
use esp_wifi::ble::controller::BleConnector;
use trouble_example_apps::ble_l2cap_peripheral;
use {esp_alloc as _, esp_backtrace as _};

#[esp_hal_embassy::main]
async fn main(_s: Spawner) {
esp_println::logger::init_logger_from_env();
let peripherals = esp_hal::init({
let mut config = esp_hal::Config::default();
config.cpu_clock = CpuClock::max();
config
});
esp_alloc::heap_allocator!(72 * 1024);
let timg0 = TimerGroup::new(peripherals.TIMG0);

let init = esp_wifi::init(
timg0.timer0,
esp_hal::rng::Rng::new(peripherals.RNG),
peripherals.RADIO_CLK,
)
.unwrap();

#[cfg(not(feature = "esp32"))]
{
let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER)
.split::<esp_hal::timer::systimer::Target>();
esp_hal_embassy::init(systimer.alarm0);
}
#[cfg(feature = "esp32")]
{
esp_hal_embassy::init(timg0.timer1);
}

let bluetooth = peripherals.BT;
let connector = BleConnector::new(&init, bluetooth);
let controller: ExternalController<_, 20> = ExternalController::new(connector);

ble_l2cap_peripheral::run(controller).await;
}

0 comments on commit 19b02ee

Please sign in to comment.