aboutsummaryrefslogtreecommitdiff
path: root/examples/rp/src/bin/bluetooth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rp/src/bin/bluetooth.rs')
-rw-r--r--examples/rp/src/bin/bluetooth.rs148
1 files changed, 148 insertions, 0 deletions
diff --git a/examples/rp/src/bin/bluetooth.rs b/examples/rp/src/bin/bluetooth.rs
new file mode 100644
index 000000000..901521b60
--- /dev/null
+++ b/examples/rp/src/bin/bluetooth.rs
@@ -0,0 +1,148 @@
1//! This example test the RP Pico W on board LED.
2//!
3//! It does not work with the RP Pico board. See blinky.rs.
4
5#![no_std]
6#![no_main]
7
8use bt_hci::controller::ExternalController;
9use cyw43_pio::PioSpi;
10use defmt::*;
11use embassy_executor::Spawner;
12use embassy_futures::join::join3;
13use embassy_rp::bind_interrupts;
14use embassy_rp::gpio::{Level, Output};
15use embassy_rp::peripherals::{DMA_CH0, PIO0};
16use embassy_rp::pio::{InterruptHandler, Pio};
17use embassy_sync::blocking_mutex::raw::NoopRawMutex;
18use embassy_time::{Duration, Timer};
19use static_cell::StaticCell;
20use trouble_host::advertise::{AdStructure, Advertisement, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE};
21use trouble_host::attribute::{AttributeTable, CharacteristicProp, Service, Uuid};
22use trouble_host::gatt::GattEvent;
23use trouble_host::{Address, BleHost, BleHostResources, PacketQos};
24use {defmt_rtt as _, embassy_time as _, panic_probe as _};
25
26bind_interrupts!(struct Irqs {
27 PIO0_IRQ_0 => InterruptHandler<PIO0>;
28});
29
30#[embassy_executor::task]
31async fn cyw43_task(runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>) -> ! {
32 runner.run().await
33}
34
35#[embassy_executor::main]
36async fn main(spawner: Spawner) {
37 let p = embassy_rp::init(Default::default());
38 let fw = include_bytes!("../../../../cyw43-firmware/43439A0.bin");
39 let clm = include_bytes!("../../../../cyw43-firmware/43439A0_clm.bin");
40 let btfw = include_bytes!("../../../../cyw43-firmware/43439A0_btfw.bin");
41
42 // To make flashing faster for development, you may want to flash the firmwares independently
43 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
44 // probe-rs download 43439A0.bin --format bin --chip RP2040 --base-address 0x10100000
45 // probe-rs download 43439A0_clm.bin --format bin --chip RP2040 --base-address 0x10140000
46 //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) };
47 //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
48
49 let pwr = Output::new(p.PIN_23, Level::Low);
50 let cs = Output::new(p.PIN_25, Level::High);
51 let mut pio = Pio::new(p.PIO0, Irqs);
52 let spi = PioSpi::new(&mut pio.common, pio.sm0, pio.irq0, cs, p.PIN_24, p.PIN_29, p.DMA_CH0);
53
54 static STATE: StaticCell<cyw43::State> = StaticCell::new();
55 let state = STATE.init(cyw43::State::new());
56 let (_net_device, bt_device, mut control, runner) = cyw43::new_with_bluetooth(state, pwr, spi, fw, btfw).await;
57 unwrap!(spawner.spawn(cyw43_task(runner)));
58 control.init(clm).await;
59
60 let controller: ExternalController<_, 10> = ExternalController::new(bt_device);
61 static HOST_RESOURCES: StaticCell<BleHostResources<4, 32, 27>> = StaticCell::new();
62 let host_resources = HOST_RESOURCES.init(BleHostResources::new(PacketQos::None));
63
64 let mut ble: BleHost<'_, _> = BleHost::new(controller, host_resources);
65
66 ble.set_random_address(Address::random([0xff, 0x9f, 0x1a, 0x05, 0xe4, 0xff]));
67 let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new();
68
69 // Generic Access Service (mandatory)
70 let id = b"Pico W Bluetooth";
71 let appearance = [0x80, 0x07];
72 let mut bat_level = [0; 1];
73 let handle = {
74 let mut svc = table.add_service(Service::new(0x1800));
75 let _ = svc.add_characteristic_ro(0x2a00, id);
76 let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
77 svc.build();
78
79 // Generic attribute service (mandatory)
80 table.add_service(Service::new(0x1801));
81
82 // Battery service
83 let mut svc = table.add_service(Service::new(0x180f));
84
85 svc.add_characteristic(
86 0x2a19,
87 &[CharacteristicProp::Read, CharacteristicProp::Notify],
88 &mut bat_level,
89 )
90 .build()
91 };
92
93 let mut adv_data = [0; 31];
94 AdStructure::encode_slice(
95 &[
96 AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
97 AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
98 AdStructure::CompleteLocalName(b"Pico W Bluetooth"),
99 ],
100 &mut adv_data[..],
101 )
102 .unwrap();
103
104 let server = ble.gatt_server(&table);
105
106 info!("Starting advertising and GATT service");
107 let _ = join3(
108 ble.run(),
109 async {
110 loop {
111 match server.next().await {
112 Ok(GattEvent::Write { handle, connection: _ }) => {
113 let _ = table.get(handle, |value| {
114 info!("Write event. Value written: {:?}", value);
115 });
116 }
117 Ok(GattEvent::Read { .. }) => {
118 info!("Read event");
119 }
120 Err(e) => {
121 error!("Error processing GATT events: {:?}", e);
122 }
123 }
124 }
125 },
126 async {
127 let mut advertiser = ble
128 .advertise(
129 &Default::default(),
130 Advertisement::ConnectableScannableUndirected {
131 adv_data: &adv_data[..],
132 scan_data: &[],
133 },
134 )
135 .await
136 .unwrap();
137 let conn = advertiser.accept().await.unwrap();
138 // Keep connection alive
139 let mut tick: u8 = 0;
140 loop {
141 Timer::after(Duration::from_secs(10)).await;
142 tick += 1;
143 server.notify(handle, &conn, &[tick]).await.unwrap();
144 }
145 },
146 )
147 .await;
148}