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.rs150
1 files changed, 0 insertions, 150 deletions
diff --git a/examples/rp/src/bin/bluetooth.rs b/examples/rp/src/bin/bluetooth.rs
deleted file mode 100644
index 7524e7929..000000000
--- a/examples/rp/src/bin/bluetooth.rs
+++ /dev/null
@@ -1,150 +0,0 @@
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 // probe-rs download 43439A0_btfw.bin --format bin --chip RP2040 --base-address 0x10141400
47 //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) };
48 //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
49 //let btfw = unsafe { core::slice::from_raw_parts(0x10141400 as *const u8, 6164) };
50
51 let pwr = Output::new(p.PIN_23, Level::Low);
52 let cs = Output::new(p.PIN_25, Level::High);
53 let mut pio = Pio::new(p.PIO0, Irqs);
54 let spi = PioSpi::new(&mut pio.common, pio.sm0, pio.irq0, cs, p.PIN_24, p.PIN_29, p.DMA_CH0);
55
56 static STATE: StaticCell<cyw43::State> = StaticCell::new();
57 let state = STATE.init(cyw43::State::new());
58 let (_net_device, bt_device, mut control, runner) = cyw43::new_with_bluetooth(state, pwr, spi, fw, btfw).await;
59 unwrap!(spawner.spawn(cyw43_task(runner)));
60 control.init(clm).await;
61
62 let controller: ExternalController<_, 10> = ExternalController::new(bt_device);
63 static HOST_RESOURCES: StaticCell<BleHostResources<4, 32, 27>> = StaticCell::new();
64 let host_resources = HOST_RESOURCES.init(BleHostResources::new(PacketQos::None));
65
66 let mut ble: BleHost<'_, _> = BleHost::new(controller, host_resources);
67
68 ble.set_random_address(Address::random([0xff, 0x9f, 0x1a, 0x05, 0xe4, 0xff]));
69 let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new();
70
71 // Generic Access Service (mandatory)
72 let id = b"Pico W Bluetooth";
73 let appearance = [0x80, 0x07];
74 let mut bat_level = [0; 1];
75 let handle = {
76 let mut svc = table.add_service(Service::new(0x1800));
77 let _ = svc.add_characteristic_ro(0x2a00, id);
78 let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
79 svc.build();
80
81 // Generic attribute service (mandatory)
82 table.add_service(Service::new(0x1801));
83
84 // Battery service
85 let mut svc = table.add_service(Service::new(0x180f));
86
87 svc.add_characteristic(
88 0x2a19,
89 &[CharacteristicProp::Read, CharacteristicProp::Notify],
90 &mut bat_level,
91 )
92 .build()
93 };
94
95 let mut adv_data = [0; 31];
96 AdStructure::encode_slice(
97 &[
98 AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
99 AdStructure::ServiceUuids16(&[Uuid::Uuid16([0x0f, 0x18])]),
100 AdStructure::CompleteLocalName(b"Pico W Bluetooth"),
101 ],
102 &mut adv_data[..],
103 )
104 .unwrap();
105
106 let server = ble.gatt_server(&table);
107
108 info!("Starting advertising and GATT service");
109 let _ = join3(
110 ble.run(),
111 async {
112 loop {
113 match server.next().await {
114 Ok(GattEvent::Write { handle, connection: _ }) => {
115 let _ = table.get(handle, |value| {
116 info!("Write event. Value written: {:?}", value);
117 });
118 }
119 Ok(GattEvent::Read { .. }) => {
120 info!("Read event");
121 }
122 Err(e) => {
123 error!("Error processing GATT events: {:?}", e);
124 }
125 }
126 }
127 },
128 async {
129 let mut advertiser = ble
130 .advertise(
131 &Default::default(),
132 Advertisement::ConnectableScannableUndirected {
133 adv_data: &adv_data[..],
134 scan_data: &[],
135 },
136 )
137 .await
138 .unwrap();
139 let conn = advertiser.accept().await.unwrap();
140 // Keep connection alive
141 let mut tick: u8 = 0;
142 loop {
143 Timer::after(Duration::from_secs(10)).await;
144 tick += 1;
145 server.notify(handle, &conn, &[tick]).await.unwrap();
146 }
147 },
148 )
149 .await;
150}