Merge branch 'embassy-rs:main' into can-split

This commit is contained in:
schphil
2023-06-23 10:19:30 +02:00
committed by GitHub
33 changed files with 3723 additions and 294 deletions

View File

@ -6,8 +6,24 @@ license = "MIT OR Apache-2.0"
[features]
default = ["nightly"]
nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embassy-net/nightly", "embassy-nrf/unstable-traits", "embassy-time/nightly", "embassy-time/unstable-traits", "static_cell/nightly",
"embassy-usb", "embedded-io/async", "embassy-net", "embassy-lora", "lora-phy", "lorawan-device", "lorawan"]
nightly = [
"embedded-hal-async",
"embassy-executor/nightly",
"embassy-nrf/nightly",
"embassy-net/nightly",
"embassy-net-esp-hosted",
"embassy-nrf/unstable-traits",
"embassy-time/nightly",
"embassy-time/unstable-traits",
"static_cell/nightly",
"embassy-usb",
"embedded-io/async",
"embassy-net",
"embassy-lora",
"lora-phy",
"lorawan-device",
"lorawan",
]
[dependencies]
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
@ -22,6 +38,7 @@ embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["ti
lora-phy = { version = "1", optional = true }
lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"], optional = true }
lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"], optional = true }
embassy-net-esp-hosted = { version = "0.1.0", path = "../../embassy-net-esp-hosted", features = ["defmt"], optional = true }
defmt = "0.3"
defmt-rtt = "0.4"
@ -35,3 +52,4 @@ rand = { version = "0.8.4", default-features = false }
embedded-storage = "0.3.0"
usbd-hid = "0.6.0"
serde = { version = "1.0.136", default-features = false }
embedded-hal-async = { version = "0.2.0-alpha.1", optional = true }

View File

@ -0,0 +1,139 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::{info, unwrap, warn};
use embassy_executor::Spawner;
use embassy_net::tcp::TcpSocket;
use embassy_net::{Stack, StackResources};
use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pin, Pull};
use embassy_nrf::rng::Rng;
use embassy_nrf::spim::{self, Spim};
use embassy_nrf::{bind_interrupts, peripherals};
use embedded_hal_async::spi::ExclusiveDevice;
use embedded_io::asynch::Write;
use static_cell::make_static;
use {defmt_rtt as _, embassy_net_esp_hosted as hosted, panic_probe as _};
bind_interrupts!(struct Irqs {
SPIM3 => spim::InterruptHandler<peripherals::SPI3>;
RNG => embassy_nrf::rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::task]
async fn wifi_task(
runner: hosted::Runner<
'static,
ExclusiveDevice<Spim<'static, peripherals::SPI3>, Output<'static, peripherals::P0_31>>,
Input<'static, AnyPin>,
Output<'static, peripherals::P1_05>,
>,
) -> ! {
runner.run().await
}
#[embassy_executor::task]
async fn net_task(stack: &'static Stack<hosted::NetDriver<'static>>) -> ! {
stack.run().await
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
info!("Hello World!");
let p = embassy_nrf::init(Default::default());
let miso = p.P0_28;
let sck = p.P0_29;
let mosi = p.P0_30;
let cs = Output::new(p.P0_31, Level::High, OutputDrive::HighDrive);
let handshake = Input::new(p.P1_01.degrade(), Pull::Up);
let ready = Input::new(p.P1_04.degrade(), Pull::None);
let reset = Output::new(p.P1_05, Level::Low, OutputDrive::Standard);
let mut config = spim::Config::default();
config.frequency = spim::Frequency::M32;
config.mode = spim::MODE_2; // !!!
let spi = spim::Spim::new(p.SPI3, Irqs, sck, miso, mosi, config);
let spi = ExclusiveDevice::new(spi, cs);
let (device, mut control, runner) = embassy_net_esp_hosted::new(
make_static!(embassy_net_esp_hosted::State::new()),
spi,
handshake,
ready,
reset,
)
.await;
unwrap!(spawner.spawn(wifi_task(runner)));
control.init().await;
control.join(env!("WIFI_NETWORK"), env!("WIFI_PASSWORD")).await;
let config = embassy_net::Config::dhcpv4(Default::default());
// let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
// address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
// dns_servers: Vec::new(),
// gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
// });
// Generate random seed
let mut rng = Rng::new(p.RNG, Irqs);
let mut seed = [0; 8];
rng.blocking_fill_bytes(&mut seed);
let seed = u64::from_le_bytes(seed);
// Init network stack
let stack = &*make_static!(Stack::new(
device,
config,
make_static!(StackResources::<2>::new()),
seed
));
unwrap!(spawner.spawn(net_task(stack)));
// And now we can use it!
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut buf = [0; 4096];
loop {
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
info!("Listening on TCP:1234...");
if let Err(e) = socket.accept(1234).await {
warn!("accept error: {:?}", e);
continue;
}
info!("Received connection from {:?}", socket.remote_endpoint());
loop {
let n = match socket.read(&mut buf).await {
Ok(0) => {
warn!("read EOF");
break;
}
Ok(n) => n,
Err(e) => {
warn!("read error: {:?}", e);
break;
}
};
info!("rxd {:02x}", &buf[..n]);
match socket.write_all(&buf[..n]).await {
Ok(()) => {}
Err(e) => {
warn!("write error: {:?}", e);
break;
}
};
}
}
}

View File

@ -1,6 +1,6 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# replace STM32WB55CCUx with your chip as listed in `probe-rs-cli chip list`
# runner = "probe-rs-cli run --chip STM32WB55CCUx --speed 1000 --connect-under-reset"
# runner = "probe-rs-cli run --chip STM32WB55RGVx --speed 1000 --connect-under-reset"
runner = "teleprobe local run --chip STM32WB55RG --elf"
[build]

View File

@ -20,3 +20,24 @@ embedded-hal = "0.2.6"
panic-probe = { version = "0.3", features = ["print-defmt"] }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
heapless = { version = "0.7.5", default-features = false }
[features]
default = ["ble"]
mac = ["embassy-stm32-wpan/mac"]
ble = ["embassy-stm32-wpan/ble"]
[[bin]]
name = "tl_mbox_ble"
required-features = ["ble"]
[[bin]]
name = "tl_mbox_mac"
required-features = ["mac"]
[[bin]]
name = "eddystone_beacon"
required-features = ["ble"]
[patch.crates-io]
stm32wb-hci = { git = "https://github.com/OueslatiGhaith/stm32wb-hci", rev = "9f663be"}

View File

@ -0,0 +1,249 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use core::time::Duration;
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::bind_interrupts;
use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler};
use embassy_stm32_wpan::ble::hci::host::uart::UartHci;
use embassy_stm32_wpan::ble::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType};
use embassy_stm32_wpan::ble::hci::types::AdvertisingType;
use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gap::{
AdvertisingDataType, DiscoverableParameters, GapCommands, Role,
};
use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gatt::GattCommands;
use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel};
use embassy_stm32_wpan::ble::hci::BdAddr;
use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp;
use embassy_stm32_wpan::TlMbox;
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs{
IPCC_C1_RX => ReceiveInterruptHandler;
IPCC_C1_TX => TransmitInterruptHandler;
});
const BLE_GAP_DEVICE_NAME_LENGTH: u8 = 7;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
/*
How to make this work:
- Obtain a NUCLEO-STM32WB55 from your preferred supplier.
- Download and Install STM32CubeProgrammer.
- Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from
gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x
- Open STM32CubeProgrammer
- On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware.
- Once complete, click connect to connect to the device.
- On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services".
- In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file
- Select that file, the memory address, "verify download", and then "Firmware Upgrade".
- Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the
stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address.
- Select that file, the memory address, "verify download", and then "Firmware Upgrade".
- Select "Start Wireless Stack".
- Disconnect from the device.
- In the examples folder for stm32wb, modify the memory.x file to match your target device.
- Run this example.
Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name.
*/
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
let config = Config::default();
let mut mbox = TlMbox::init(p.IPCC, Irqs, config);
let sys_event = mbox.sys_subsystem.read().await;
info!("sys event: {}", sys_event.payload());
mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await;
info!("resetting BLE...");
mbox.ble_subsystem.reset().await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("config public address...");
mbox.ble_subsystem
.write_config_data(&ConfigData::public_address(get_bd_addr()).build())
.await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("config random address...");
mbox.ble_subsystem
.write_config_data(&ConfigData::random_address(get_random_addr()).build())
.await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("config identity root...");
mbox.ble_subsystem
.write_config_data(&ConfigData::identity_root(&get_irk()).build())
.await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("config encryption root...");
mbox.ble_subsystem
.write_config_data(&ConfigData::encryption_root(&get_erk()).build())
.await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("config tx power level...");
mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("GATT init...");
mbox.ble_subsystem.init_gatt().await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("GAP init...");
mbox.ble_subsystem
.init_gap(Role::PERIPHERAL, false, BLE_GAP_DEVICE_NAME_LENGTH)
.await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
// info!("set scan response...");
// mbox.ble_subsystem.le_set_scan_response_data(&[]).await.unwrap();
// let response = mbox.ble_subsystem.read().await.unwrap();
// defmt::info!("{}", response);
info!("set discoverable...");
mbox.ble_subsystem
.set_discoverable(&DiscoverableParameters {
advertising_type: AdvertisingType::NonConnectableUndirected,
advertising_interval: Some((Duration::from_millis(250), Duration::from_millis(250))),
address_type: OwnAddressType::Public,
filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan,
local_name: None,
advertising_data: &[],
conn_interval: (None, None),
})
.await
.unwrap();
let response = mbox.ble_subsystem.read().await;
defmt::info!("{}", response);
// remove some advertisement to decrease the packet size
info!("delete tx power ad type...");
mbox.ble_subsystem
.delete_ad_type(AdvertisingDataType::TxPowerLevel)
.await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("delete conn interval ad type...");
mbox.ble_subsystem
.delete_ad_type(AdvertisingDataType::PeripheralConnectionInterval)
.await;
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("update advertising data...");
mbox.ble_subsystem
.update_advertising_data(&eddystone_advertising_data())
.await
.unwrap();
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("update advertising data type...");
mbox.ble_subsystem
.update_advertising_data(&[3, AdvertisingDataType::UuidCompleteList16 as u8, 0xaa, 0xfe])
.await
.unwrap();
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
info!("update advertising data flags...");
mbox.ble_subsystem
.update_advertising_data(&[
2,
AdvertisingDataType::Flags as u8,
(0x02 | 0x04) as u8, // BLE general discoverable, without BR/EDR support
])
.await
.unwrap();
let response = mbox.ble_subsystem.read().await.unwrap();
defmt::info!("{}", response);
cortex_m::asm::wfi();
}
fn get_bd_addr() -> BdAddr {
let mut bytes = [0u8; 6];
let lhci_info = LhciC1DeviceInformationCcrp::new();
bytes[0] = (lhci_info.uid64 & 0xff) as u8;
bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8;
bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8;
bytes[3] = lhci_info.device_type_id;
bytes[4] = (lhci_info.st_company_id & 0xff) as u8;
bytes[5] = (lhci_info.st_company_id >> 8 & 0xff) as u8;
BdAddr(bytes)
}
fn get_random_addr() -> BdAddr {
let mut bytes = [0u8; 6];
let lhci_info = LhciC1DeviceInformationCcrp::new();
bytes[0] = (lhci_info.uid64 & 0xff) as u8;
bytes[1] = ((lhci_info.uid64 >> 8) & 0xff) as u8;
bytes[2] = ((lhci_info.uid64 >> 16) & 0xff) as u8;
bytes[3] = 0;
bytes[4] = 0x6E;
bytes[5] = 0xED;
BdAddr(bytes)
}
const BLE_CFG_IRK: [u8; 16] = [
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
];
const BLE_CFG_ERK: [u8; 16] = [
0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21,
];
fn get_irk() -> EncryptionKey {
EncryptionKey(BLE_CFG_IRK)
}
fn get_erk() -> EncryptionKey {
EncryptionKey(BLE_CFG_ERK)
}
fn eddystone_advertising_data() -> [u8; 24] {
const EDDYSTONE_URL: &[u8] = b"www.rust-lang.com";
let mut service_data = [0u8; 24];
let url_len = EDDYSTONE_URL.len();
service_data[0] = 6 + url_len as u8;
service_data[1] = AdvertisingDataType::ServiceData as u8;
// 16-bit eddystone uuid
service_data[2] = 0xaa;
service_data[3] = 0xFE;
service_data[4] = 0x10; // URL frame type
service_data[5] = 22_i8 as u8; // calibrated TX power at 0m
service_data[6] = 0x03; // eddystone url prefix = https
service_data[7..(7 + url_len)].copy_from_slice(EDDYSTONE_URL);
service_data
}

View File

@ -52,10 +52,10 @@ async fn main(_spawner: Spawner) {
mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await;
info!("starting ble...");
mbox.ble_subsystem.write(0x0c, &[]).await;
mbox.ble_subsystem.tl_write(0x0c, &[]).await;
info!("waiting for ble...");
let ble_event = mbox.ble_subsystem.read().await;
let ble_event = mbox.ble_subsystem.tl_read().await;
info!("ble event: {}", ble_event.payload());

View File

@ -0,0 +1,64 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::bind_interrupts;
use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler};
use embassy_stm32_wpan::TlMbox;
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs{
IPCC_C1_RX => ReceiveInterruptHandler;
IPCC_C1_TX => TransmitInterruptHandler;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
/*
How to make this work:
- Obtain a NUCLEO-STM32WB55 from your preferred supplier.
- Download and Install STM32CubeProgrammer.
- Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from
gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x
- Open STM32CubeProgrammer
- On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware.
- Once complete, click connect to connect to the device.
- On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services".
- In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file
- Select that file, the memory address, "verify download", and then "Firmware Upgrade".
- Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the
stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address.
- Select that file, the memory address, "verify download", and then "Firmware Upgrade".
- Select "Start Wireless Stack".
- Disconnect from the device.
- In the examples folder for stm32wb, modify the memory.x file to match your target device.
- Run this example.
Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name.
*/
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
let config = Config::default();
let mbox = TlMbox::init(p.IPCC, Irqs, config);
let sys_event = mbox.sys_subsystem.read().await;
info!("sys event: {}", sys_event.payload());
mbox.sys_subsystem.shci_c2_mac_802_15_4_init().await;
//
// info!("starting ble...");
// mbox.ble_subsystem.t_write(0x0c, &[]).await;
//
// info!("waiting for ble...");
// let ble_event = mbox.ble_subsystem.tl_read().await;
//
// info!("ble event: {}", ble_event.payload());
info!("Test OK");
cortex_m::asm::bkpt();
}