diff options
| author | xoviat <[email protected]> | 2023-06-23 02:09:52 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-06-23 02:09:52 +0000 |
| commit | 6f17286c7525050a3acfdbb889915730913518b0 (patch) | |
| tree | 8f62f023c207ffa9203541433f600702cbb5ea1a /examples | |
| parent | 70907d84f197d1c5d5d112ae1172e9070d41b730 (diff) | |
| parent | caf63b9e7336ed3ddb0dc997d431f15a26ae7693 (diff) | |
Merge pull request #1576 from OueslatiGhaith/hci_v2
wpan: add BLE HCI
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32wb/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32wb/Cargo.toml | 9 | ||||
| -rw-r--r-- | examples/stm32wb/src/bin/eddystone_beacon.rs | 249 | ||||
| -rw-r--r-- | examples/stm32wb/src/bin/tl_mbox_ble.rs | 4 | ||||
| -rw-r--r-- | examples/stm32wb/src/bin/tl_mbox_mac.rs | 4 |
5 files changed, 262 insertions, 6 deletions
diff --git a/examples/stm32wb/.cargo/config.toml b/examples/stm32wb/.cargo/config.toml index d23fdc513..35317a297 100644 --- a/examples/stm32wb/.cargo/config.toml +++ b/examples/stm32wb/.cargo/config.toml | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] |
| 2 | # replace STM32WB55CCUx with your chip as listed in `probe-rs-cli chip list` | 2 | # replace STM32WB55CCUx with your chip as listed in `probe-rs-cli chip list` |
| 3 | # runner = "probe-rs-cli run --chip STM32WB55CCUx --speed 1000 --connect-under-reset" | 3 | # runner = "probe-rs-cli run --chip STM32WB55RGVx --speed 1000 --connect-under-reset" |
| 4 | runner = "teleprobe local run --chip STM32WB55RG --elf" | 4 | runner = "teleprobe local run --chip STM32WB55RG --elf" |
| 5 | 5 | ||
| 6 | [build] | 6 | [build] |
diff --git a/examples/stm32wb/Cargo.toml b/examples/stm32wb/Cargo.toml index e41424aad..fbb2d918b 100644 --- a/examples/stm32wb/Cargo.toml +++ b/examples/stm32wb/Cargo.toml | |||
| @@ -33,4 +33,11 @@ required-features = ["ble"] | |||
| 33 | 33 | ||
| 34 | [[bin]] | 34 | [[bin]] |
| 35 | name = "tl_mbox_mac" | 35 | name = "tl_mbox_mac" |
| 36 | required-features = ["mac"] \ No newline at end of file | 36 | required-features = ["mac"] |
| 37 | |||
| 38 | [[bin]] | ||
| 39 | name = "eddystone_beacon" | ||
| 40 | required-features = ["ble"] | ||
| 41 | |||
| 42 | [patch.crates-io] | ||
| 43 | stm32wb-hci = { git = "https://github.com/OueslatiGhaith/stm32wb-hci", rev = "9f663be"} \ No newline at end of file | ||
diff --git a/examples/stm32wb/src/bin/eddystone_beacon.rs b/examples/stm32wb/src/bin/eddystone_beacon.rs new file mode 100644 index 000000000..fdd5be4a2 --- /dev/null +++ b/examples/stm32wb/src/bin/eddystone_beacon.rs | |||
| @@ -0,0 +1,249 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use core::time::Duration; | ||
| 6 | |||
| 7 | use defmt::*; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_stm32::bind_interrupts; | ||
| 10 | use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler}; | ||
| 11 | use embassy_stm32_wpan::ble::hci::host::uart::UartHci; | ||
| 12 | use embassy_stm32_wpan::ble::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType}; | ||
| 13 | use embassy_stm32_wpan::ble::hci::types::AdvertisingType; | ||
| 14 | use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gap::{ | ||
| 15 | AdvertisingDataType, DiscoverableParameters, GapCommands, Role, | ||
| 16 | }; | ||
| 17 | use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gatt::GattCommands; | ||
| 18 | use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel}; | ||
| 19 | use embassy_stm32_wpan::ble::hci::BdAddr; | ||
| 20 | use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp; | ||
| 21 | use embassy_stm32_wpan::TlMbox; | ||
| 22 | use {defmt_rtt as _, panic_probe as _}; | ||
| 23 | |||
| 24 | bind_interrupts!(struct Irqs{ | ||
| 25 | IPCC_C1_RX => ReceiveInterruptHandler; | ||
| 26 | IPCC_C1_TX => TransmitInterruptHandler; | ||
| 27 | }); | ||
| 28 | |||
| 29 | const BLE_GAP_DEVICE_NAME_LENGTH: u8 = 7; | ||
| 30 | |||
| 31 | #[embassy_executor::main] | ||
| 32 | async fn main(_spawner: Spawner) { | ||
| 33 | /* | ||
| 34 | How to make this work: | ||
| 35 | |||
| 36 | - Obtain a NUCLEO-STM32WB55 from your preferred supplier. | ||
| 37 | - Download and Install STM32CubeProgrammer. | ||
| 38 | - Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from | ||
| 39 | gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x | ||
| 40 | - Open STM32CubeProgrammer | ||
| 41 | - On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware. | ||
| 42 | - Once complete, click connect to connect to the device. | ||
| 43 | - On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services". | ||
| 44 | - In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file | ||
| 45 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 46 | - Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the | ||
| 47 | stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address. | ||
| 48 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 49 | - Select "Start Wireless Stack". | ||
| 50 | - Disconnect from the device. | ||
| 51 | - In the examples folder for stm32wb, modify the memory.x file to match your target device. | ||
| 52 | - Run this example. | ||
| 53 | |||
| 54 | Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name. | ||
| 55 | */ | ||
| 56 | |||
| 57 | let p = embassy_stm32::init(Default::default()); | ||
| 58 | info!("Hello World!"); | ||
| 59 | |||
| 60 | let config = Config::default(); | ||
| 61 | let mut mbox = TlMbox::init(p.IPCC, Irqs, config); | ||
| 62 | |||
| 63 | let sys_event = mbox.sys_subsystem.read().await; | ||
| 64 | info!("sys event: {}", sys_event.payload()); | ||
| 65 | |||
| 66 | mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await; | ||
| 67 | |||
| 68 | info!("resetting BLE..."); | ||
| 69 | mbox.ble_subsystem.reset().await; | ||
| 70 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 71 | defmt::info!("{}", response); | ||
| 72 | |||
| 73 | info!("config public address..."); | ||
| 74 | mbox.ble_subsystem | ||
| 75 | .write_config_data(&ConfigData::public_address(get_bd_addr()).build()) | ||
| 76 | .await; | ||
| 77 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 78 | defmt::info!("{}", response); | ||
| 79 | |||
| 80 | info!("config random address..."); | ||
| 81 | mbox.ble_subsystem | ||
| 82 | .write_config_data(&ConfigData::random_address(get_random_addr()).build()) | ||
| 83 | .await; | ||
| 84 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 85 | defmt::info!("{}", response); | ||
| 86 | |||
| 87 | info!("config identity root..."); | ||
| 88 | mbox.ble_subsystem | ||
| 89 | .write_config_data(&ConfigData::identity_root(&get_irk()).build()) | ||
| 90 | .await; | ||
| 91 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 92 | defmt::info!("{}", response); | ||
| 93 | |||
| 94 | info!("config encryption root..."); | ||
| 95 | mbox.ble_subsystem | ||
| 96 | .write_config_data(&ConfigData::encryption_root(&get_erk()).build()) | ||
| 97 | .await; | ||
| 98 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 99 | defmt::info!("{}", response); | ||
| 100 | |||
| 101 | info!("config tx power level..."); | ||
| 102 | mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await; | ||
| 103 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 104 | defmt::info!("{}", response); | ||
| 105 | |||
| 106 | info!("GATT init..."); | ||
| 107 | mbox.ble_subsystem.init_gatt().await; | ||
| 108 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 109 | defmt::info!("{}", response); | ||
| 110 | |||
| 111 | info!("GAP init..."); | ||
| 112 | mbox.ble_subsystem | ||
| 113 | .init_gap(Role::PERIPHERAL, false, BLE_GAP_DEVICE_NAME_LENGTH) | ||
| 114 | .await; | ||
| 115 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 116 | defmt::info!("{}", response); | ||
| 117 | |||
| 118 | // info!("set scan response..."); | ||
| 119 | // mbox.ble_subsystem.le_set_scan_response_data(&[]).await.unwrap(); | ||
| 120 | // let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 121 | // defmt::info!("{}", response); | ||
| 122 | |||
| 123 | info!("set discoverable..."); | ||
| 124 | mbox.ble_subsystem | ||
| 125 | .set_discoverable(&DiscoverableParameters { | ||
| 126 | advertising_type: AdvertisingType::NonConnectableUndirected, | ||
| 127 | advertising_interval: Some((Duration::from_millis(250), Duration::from_millis(250))), | ||
| 128 | address_type: OwnAddressType::Public, | ||
| 129 | filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan, | ||
| 130 | local_name: None, | ||
| 131 | advertising_data: &[], | ||
| 132 | conn_interval: (None, None), | ||
| 133 | }) | ||
| 134 | .await | ||
| 135 | .unwrap(); | ||
| 136 | |||
| 137 | let response = mbox.ble_subsystem.read().await; | ||
| 138 | defmt::info!("{}", response); | ||
| 139 | |||
| 140 | // remove some advertisement to decrease the packet size | ||
| 141 | info!("delete tx power ad type..."); | ||
| 142 | mbox.ble_subsystem | ||
| 143 | .delete_ad_type(AdvertisingDataType::TxPowerLevel) | ||
| 144 | .await; | ||
| 145 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 146 | defmt::info!("{}", response); | ||
| 147 | |||
| 148 | info!("delete conn interval ad type..."); | ||
| 149 | mbox.ble_subsystem | ||
| 150 | .delete_ad_type(AdvertisingDataType::PeripheralConnectionInterval) | ||
| 151 | .await; | ||
| 152 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 153 | defmt::info!("{}", response); | ||
| 154 | |||
| 155 | info!("update advertising data..."); | ||
| 156 | mbox.ble_subsystem | ||
| 157 | .update_advertising_data(&eddystone_advertising_data()) | ||
| 158 | .await | ||
| 159 | .unwrap(); | ||
| 160 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 161 | defmt::info!("{}", response); | ||
| 162 | |||
| 163 | info!("update advertising data type..."); | ||
| 164 | mbox.ble_subsystem | ||
| 165 | .update_advertising_data(&[3, AdvertisingDataType::UuidCompleteList16 as u8, 0xaa, 0xfe]) | ||
| 166 | .await | ||
| 167 | .unwrap(); | ||
| 168 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 169 | defmt::info!("{}", response); | ||
| 170 | |||
| 171 | info!("update advertising data flags..."); | ||
| 172 | mbox.ble_subsystem | ||
| 173 | .update_advertising_data(&[ | ||
| 174 | 2, | ||
| 175 | AdvertisingDataType::Flags as u8, | ||
| 176 | (0x02 | 0x04) as u8, // BLE general discoverable, without BR/EDR support | ||
| 177 | ]) | ||
| 178 | .await | ||
| 179 | .unwrap(); | ||
| 180 | let response = mbox.ble_subsystem.read().await.unwrap(); | ||
| 181 | defmt::info!("{}", response); | ||
| 182 | |||
| 183 | cortex_m::asm::wfi(); | ||
| 184 | } | ||
| 185 | |||
| 186 | fn get_bd_addr() -> BdAddr { | ||
| 187 | let mut bytes = [0u8; 6]; | ||
| 188 | |||
| 189 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 190 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 191 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 192 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 193 | bytes[3] = lhci_info.device_type_id; | ||
| 194 | bytes[4] = (lhci_info.st_company_id & 0xff) as u8; | ||
| 195 | bytes[5] = (lhci_info.st_company_id >> 8 & 0xff) as u8; | ||
| 196 | |||
| 197 | BdAddr(bytes) | ||
| 198 | } | ||
| 199 | |||
| 200 | fn get_random_addr() -> BdAddr { | ||
| 201 | let mut bytes = [0u8; 6]; | ||
| 202 | |||
| 203 | let lhci_info = LhciC1DeviceInformationCcrp::new(); | ||
| 204 | bytes[0] = (lhci_info.uid64 & 0xff) as u8; | ||
| 205 | bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8; | ||
| 206 | bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8; | ||
| 207 | bytes[3] = 0; | ||
| 208 | bytes[4] = 0x6E; | ||
| 209 | bytes[5] = 0xED; | ||
| 210 | |||
| 211 | BdAddr(bytes) | ||
| 212 | } | ||
| 213 | |||
| 214 | const BLE_CFG_IRK: [u8; 16] = [ | ||
| 215 | 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, | ||
| 216 | ]; | ||
| 217 | const BLE_CFG_ERK: [u8; 16] = [ | ||
| 218 | 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, | ||
| 219 | ]; | ||
| 220 | |||
| 221 | fn get_irk() -> EncryptionKey { | ||
| 222 | EncryptionKey(BLE_CFG_IRK) | ||
| 223 | } | ||
| 224 | |||
| 225 | fn get_erk() -> EncryptionKey { | ||
| 226 | EncryptionKey(BLE_CFG_ERK) | ||
| 227 | } | ||
| 228 | |||
| 229 | fn eddystone_advertising_data() -> [u8; 24] { | ||
| 230 | const EDDYSTONE_URL: &[u8] = b"www.rust-lang.com"; | ||
| 231 | |||
| 232 | let mut service_data = [0u8; 24]; | ||
| 233 | let url_len = EDDYSTONE_URL.len(); | ||
| 234 | |||
| 235 | service_data[0] = 6 + url_len as u8; | ||
| 236 | service_data[1] = AdvertisingDataType::ServiceData as u8; | ||
| 237 | |||
| 238 | // 16-bit eddystone uuid | ||
| 239 | service_data[2] = 0xaa; | ||
| 240 | service_data[3] = 0xFE; | ||
| 241 | |||
| 242 | service_data[4] = 0x10; // URL frame type | ||
| 243 | service_data[5] = 22_i8 as u8; // calibrated TX power at 0m | ||
| 244 | service_data[6] = 0x03; // eddystone url prefix = https | ||
| 245 | |||
| 246 | service_data[7..(7 + url_len)].copy_from_slice(EDDYSTONE_URL); | ||
| 247 | |||
| 248 | service_data | ||
| 249 | } | ||
diff --git a/examples/stm32wb/src/bin/tl_mbox_ble.rs b/examples/stm32wb/src/bin/tl_mbox_ble.rs index 439bd01ac..a511e89aa 100644 --- a/examples/stm32wb/src/bin/tl_mbox_ble.rs +++ b/examples/stm32wb/src/bin/tl_mbox_ble.rs | |||
| @@ -52,10 +52,10 @@ async fn main(_spawner: Spawner) { | |||
| 52 | mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await; | 52 | mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await; |
| 53 | 53 | ||
| 54 | info!("starting ble..."); | 54 | info!("starting ble..."); |
| 55 | mbox.ble_subsystem.write(0x0c, &[]).await; | 55 | mbox.ble_subsystem.tl_write(0x0c, &[]).await; |
| 56 | 56 | ||
| 57 | info!("waiting for ble..."); | 57 | info!("waiting for ble..."); |
| 58 | let ble_event = mbox.ble_subsystem.read().await; | 58 | let ble_event = mbox.ble_subsystem.tl_read().await; |
| 59 | 59 | ||
| 60 | info!("ble event: {}", ble_event.payload()); | 60 | info!("ble event: {}", ble_event.payload()); |
| 61 | 61 | ||
diff --git a/examples/stm32wb/src/bin/tl_mbox_mac.rs b/examples/stm32wb/src/bin/tl_mbox_mac.rs index a42939bbd..afd319a41 100644 --- a/examples/stm32wb/src/bin/tl_mbox_mac.rs +++ b/examples/stm32wb/src/bin/tl_mbox_mac.rs | |||
| @@ -52,10 +52,10 @@ async fn main(_spawner: Spawner) { | |||
| 52 | mbox.sys_subsystem.shci_c2_mac_802_15_4_init().await; | 52 | mbox.sys_subsystem.shci_c2_mac_802_15_4_init().await; |
| 53 | // | 53 | // |
| 54 | // info!("starting ble..."); | 54 | // info!("starting ble..."); |
| 55 | // mbox.ble_subsystem.write(0x0c, &[]).await; | 55 | // mbox.ble_subsystem.t_write(0x0c, &[]).await; |
| 56 | // | 56 | // |
| 57 | // info!("waiting for ble..."); | 57 | // info!("waiting for ble..."); |
| 58 | // let ble_event = mbox.ble_subsystem.read().await; | 58 | // let ble_event = mbox.ble_subsystem.tl_read().await; |
| 59 | // | 59 | // |
| 60 | // info!("ble event: {}", ble_event.payload()); | 60 | // info!("ble event: {}", ble_event.payload()); |
| 61 | 61 | ||
