Skip to content

Commit 1d873e8

Browse files
committed
feat(example): add bonding example to esp32
Signed-off-by: Haobo Gu <[email protected]>
1 parent 2f8e936 commit 1d873e8

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

examples/apps/src/ble_bas_peripheral_bonding.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,35 @@ const L2CAP_CHANNELS_MAX: usize = 2; // Signal + att
1818
#[gatt_server]
1919
struct Server {
2020
battery_service: BatteryService,
21+
hid_service: HidService,
2122
}
2223

24+
static DESC: [u8; 67] = [
25+
5u8, 1u8, 9u8, 6u8, 161u8, 1u8, 5u8, 7u8, 25u8, 224u8, 41u8, 231u8, 21u8, 0u8, 37u8, 1u8, 117u8, 1u8, 149u8, 8u8,
26+
129u8, 2u8, 21u8, 0u8, 38u8, 255u8, 0u8, 117u8, 8u8, 149u8, 1u8, 129u8, 3u8, 5u8, 8u8, 25u8, 1u8, 41u8, 5u8, 37u8,
27+
1u8, 117u8, 1u8, 149u8, 5u8, 145u8, 2u8, 149u8, 3u8, 145u8, 3u8, 5u8, 7u8, 25u8, 0u8, 41u8, 221u8, 38u8, 255u8,
28+
0u8, 117u8, 8u8, 149u8, 6u8, 129u8, 0u8, 192u8,
29+
];
30+
31+
#[gatt_service(uuid = service::HUMAN_INTERFACE_DEVICE)]
32+
pub(crate) struct HidService {
33+
#[characteristic(uuid = "2a4a", read, value = [0x01, 0x01, 0x00, 0x03])]
34+
pub(crate) hid_info: [u8; 4],
35+
#[characteristic(uuid = "2a4b", read, value = DESC)]
36+
pub(crate) report_map: [u8; 67],
37+
#[characteristic(uuid = "2a4c", write_without_response)]
38+
pub(crate) hid_control_point: u8,
39+
#[characteristic(uuid = "2a4e", read, write_without_response, value = 1)]
40+
pub(crate) protocol_mode: u8,
41+
#[descriptor(uuid = "2908", read, value = [0u8, 1u8])]
42+
#[characteristic(uuid = "2a4d", read, notify)]
43+
pub(crate) input_keyboard: [u8; 8],
44+
#[descriptor(uuid = "2908", read, value = [0u8, 2u8])]
45+
#[characteristic(uuid = "2a4d", read, write, write_without_response)]
46+
pub(crate) output_keyboard: [u8; 1],
47+
}
48+
49+
2350
/// Battery service
2451
#[gatt_service(uuid = service::BATTERY)]
2552
struct BatteryService {
@@ -160,7 +187,7 @@ where
160187
info!("Starting advertising and GATT service");
161188
let server = Server::new_with_config(GapConfig::Peripheral(PeripheralConfig {
162189
name: "TrouBLE",
163-
appearance: &appearance::power_device::GENERIC_POWER_DEVICE,
190+
appearance: &appearance::human_interface_device::GENERIC_HUMAN_INTERFACE_DEVICE,
164191
}))
165192
.unwrap();
166193

@@ -294,7 +321,7 @@ async fn advertise<'values, 'server, C: Controller>(
294321
let len = AdStructure::encode_slice(
295322
&[
296323
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
297-
AdStructure::ServiceUuids16(&[[0x0f, 0x18]]),
324+
AdStructure::ServiceUuids16(&[service::BATTERY.to_le_bytes(), service::HUMAN_INTERFACE_DEVICE.to_le_bytes()]),
298325
AdStructure::CompleteLocalName(name.as_bytes()),
299326
],
300327
&mut advertiser_data[..],

examples/esp32/Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/esp32/Cargo.toml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ license = "MIT OR Apache-2.0"
66

77
[dependencies]
88
embassy-executor = "0.9.0"
9+
embassy-embedded-hal = "0.5"
910
esp-bootloader-esp-idf = "0.2.0"
1011
esp-backtrace = { version = "0.17.0", features = [ "panic-handler", "println" ] }
1112
esp-hal = { version = "1.0.0-rc.0", features = [ "unstable" ] }
1213
esp-hal-embassy = { version = "0.9.0" }
1314
esp-alloc = { version = "0.8.0" }
15+
esp-storage = { version = "0.7.0" }
1416
esp-println = { version = "0.15.0", features = ["log-04"] }
1517
esp-radio = { version = "0.15.0", features = [ "unstable", "ble" ] }
1618
esp-preempt = { version = "0.0.1", features = ["log-04"] }
@@ -21,12 +23,12 @@ static_cell = "2"
2123
[features]
2224
default = ["esp32c3"]
2325

24-
esp32 = ["esp-hal/esp32", "esp-backtrace/esp32", "esp-hal-embassy/esp32", "esp-println/esp32", "esp-radio/esp32", "esp-preempt/esp32", "esp-bootloader-esp-idf/esp32"]
25-
esp32c2 = ["esp-hal/esp32c2", "esp-backtrace/esp32c2", "esp-hal-embassy/esp32c2", "esp-println/esp32c2", "esp-preempt/esp32c2", "esp-radio/esp32c2", "esp-bootloader-esp-idf/esp32c2"]
26-
esp32c3 = ["esp-hal/esp32c3", "esp-backtrace/esp32c3", "esp-hal-embassy/esp32c3", "esp-println/esp32c3", "esp-preempt/esp32c3", "esp-radio/esp32c3", "esp-bootloader-esp-idf/esp32c3"]
27-
esp32c6 = ["esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-hal-embassy/esp32c6", "esp-println/esp32c6", "esp-preempt/esp32c6", "esp-radio/esp32c6", "esp-bootloader-esp-idf/esp32c6"]
28-
esp32h2 = ["esp-hal/esp32h2", "esp-backtrace/esp32h2", "esp-hal-embassy/esp32h2", "esp-println/esp32h2", "esp-preempt/esp32h2", "esp-radio/esp32h2", "esp-bootloader-esp-idf/esp32h2"]
29-
esp32s3 = ["esp-hal/esp32s3", "esp-backtrace/esp32s3", "esp-hal-embassy/esp32s3", "esp-println/esp32s3", "esp-preempt/esp32s3", "esp-radio/esp32s3", "esp-bootloader-esp-idf/esp32s3"]
26+
esp32 = ["esp-hal/esp32", "esp-backtrace/esp32", "esp-hal-embassy/esp32", "esp-println/esp32", "esp-storage/esp32", "esp-radio/esp32", "esp-preempt/esp32", "esp-bootloader-esp-idf/esp32"]
27+
esp32c2 = ["esp-hal/esp32c2", "esp-backtrace/esp32c2", "esp-hal-embassy/esp32c2", "esp-println/esp32c2", "esp-storage/esp32c2", "esp-preempt/esp32c2", "esp-radio/esp32c2", "esp-bootloader-esp-idf/esp32c2"]
28+
esp32c3 = ["esp-hal/esp32c3", "esp-backtrace/esp32c3", "esp-hal-embassy/esp32c3", "esp-println/esp32c3", "esp-storage/esp32c3", "esp-preempt/esp32c3", "esp-radio/esp32c3", "esp-bootloader-esp-idf/esp32c3"]
29+
esp32c6 = ["esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-hal-embassy/esp32c6", "esp-println/esp32c6", "esp-storage/esp32c6", "esp-preempt/esp32c6", "esp-radio/esp32c6", "esp-bootloader-esp-idf/esp32c6"]
30+
esp32h2 = ["esp-hal/esp32h2", "esp-backtrace/esp32h2", "esp-hal-embassy/esp32h2", "esp-println/esp32h2", "esp-storage/esp32h2", "esp-preempt/esp32h2", "esp-radio/esp32h2", "esp-bootloader-esp-idf/esp32h2"]
31+
esp32s3 = ["esp-hal/esp32s3", "esp-backtrace/esp32s3", "esp-hal-embassy/esp32s3", "esp-println/esp32s3", "esp-storage/esp32s3", "esp-preempt/esp32s3", "esp-radio/esp32s3", "esp-bootloader-esp-idf/esp32s3"]
3032

3133
security = [
3234
"trouble-example-apps/security",
@@ -57,6 +59,7 @@ required-features = ["security"]
5759
[patch.crates-io]
5860
esp-radio = {git = "https://github.com/lulf/esp-hal.git", branch = "bt-hci-fix"}
5961
esp-preempt = {git = "https://github.com/lulf/esp-hal.git", branch = "bt-hci-fix"}
62+
esp-storage = {git = "https://github.com/lulf/esp-hal.git", branch = "bt-hci-fix"}
6063
esp-backtrace = {git = "https://github.com/lulf/esp-hal.git", branch = "bt-hci-fix"}
6164
esp-hal = {git = "https://github.com/lulf/esp-hal.git", branch = "bt-hci-fix"}
6265
esp-bootloader-esp-idf = {git = "https://github.com/lulf/esp-hal.git", branch = "bt-hci-fix"}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use embassy_executor::Spawner;
5+
use esp_hal::clock::CpuClock;
6+
use esp_hal::rng::{Trng, TrngSource};
7+
use esp_hal::timer::timg::TimerGroup;
8+
use esp_radio::Controller;
9+
use esp_radio::ble::controller::BleConnector;
10+
use esp_storage::FlashStorage;
11+
use static_cell::StaticCell;
12+
use trouble_example_apps::ble_bas_peripheral_bonding;
13+
use trouble_host::prelude::ExternalController;
14+
use {esp_alloc as _, esp_backtrace as _};
15+
16+
esp_bootloader_esp_idf::esp_app_desc!();
17+
18+
#[esp_hal_embassy::main]
19+
async fn main(_s: Spawner) {
20+
esp_println::logger::init_logger_from_env();
21+
let peripherals = esp_hal::init(esp_hal::Config::default().with_cpu_clock(CpuClock::max()));
22+
esp_alloc::heap_allocator!(size: 72 * 1024);
23+
let timg0 = TimerGroup::new(peripherals.TIMG0);
24+
#[cfg(target_arch = "riscv32")]
25+
let software_interrupt = esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
26+
27+
esp_preempt::start(
28+
timg0.timer0,
29+
#[cfg(target_arch = "riscv32")]
30+
software_interrupt.software_interrupt0,
31+
);
32+
33+
let _trng_source = TrngSource::new(peripherals.RNG, peripherals.ADC1);
34+
let mut trng = Trng::try_new().unwrap(); // Ok when there's a TrngSource accessible
35+
36+
static RADIO: StaticCell<Controller<'static>> = StaticCell::new();
37+
let radio = RADIO.init(esp_radio::init().unwrap());
38+
39+
#[cfg(not(feature = "esp32"))]
40+
{
41+
let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER);
42+
esp_hal_embassy::init(systimer.alarm0);
43+
}
44+
#[cfg(feature = "esp32")]
45+
{
46+
esp_hal_embassy::init(timg0.timer1);
47+
}
48+
49+
let bluetooth = peripherals.BT;
50+
let connector = BleConnector::new(radio, bluetooth);
51+
let controller: ExternalController<_, 20> = ExternalController::new(connector);
52+
53+
// Initialize the flash
54+
let mut flash = embassy_embedded_hal::adapter::BlockingAsync::new(FlashStorage::new(peripherals.FLASH));
55+
56+
// ble_bas_peripheral_sec::run(controller, &mut trng).await;
57+
ble_bas_peripheral_bonding::run(controller, &mut trng, &mut flash).await;
58+
}

0 commit comments

Comments
 (0)