Merge branch 'master' into mac-3
This commit is contained in:
@ -1,3 +1,6 @@
|
||||
//! This example test the ADC (Analog to Digital Conversion) of the RS2040 pin 26, 27 and 28.
|
||||
//! It also reads the temperature sensor in the chip.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
@ -38,5 +41,8 @@ async fn main(_spawner: Spawner) {
|
||||
|
||||
fn convert_to_celsius(raw_temp: u16) -> f32 {
|
||||
// According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet
|
||||
27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721 as f32
|
||||
let temp = 27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721;
|
||||
let sign = if temp < 0.0 { -1.0 } else { 1.0 };
|
||||
let rounded_temp_x10: i16 = ((temp * 10.0) + 0.5 * sign) as i16;
|
||||
(rounded_temp_x10 as f32) / 10.0
|
||||
}
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example test the RP Pico on board LED.
|
||||
//!
|
||||
//! It does not work with the RP Pico W board. See wifi_blinky.rs.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example uses the RP Pico on board LED to test input pin 28. This is not the button on the board.
|
||||
//!
|
||||
//! It does not work with the RP Pico W board. Use wifi_blinky.rs and add input pin.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,5 @@
|
||||
//! This example test the flash connected to the RP2040 chip.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how async gpio can be used with a RP2040.
|
||||
//!
|
||||
//! The LED on the RP Pico W board is connected differently. See wifi_blinky.rs.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
@ -9,8 +13,6 @@ use embassy_time::{Duration, Timer};
|
||||
use gpio::{Input, Level, Output, Pull};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
/// This example shows how async gpio can be used with a RP2040.
|
||||
///
|
||||
/// It requires an external signal to be manually triggered on PIN 16. For
|
||||
/// example, this could be accomplished using an external power source with a
|
||||
/// button so that it is possible to toggle the signal from low to high.
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how GPOUT (General purpose clock outputs) can toggle a output pin.
|
||||
//!
|
||||
//! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,8 @@
|
||||
//! This example shows how to communicate asynchronous using i2c with external chips.
|
||||
//!
|
||||
//! Example written for the [`MCP23017 16-Bit I2C I/O Expander with Serial Interface`] chip.
|
||||
//! (https://www.microchip.com/en-us/product/mcp23017)
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,8 @@
|
||||
//! This example shows how to communicate using i2c with external chips.
|
||||
//!
|
||||
//! Example written for the [`MCP23017 16-Bit I2C I/O Expander with Serial Interface`] chip.
|
||||
//! (https://www.microchip.com/en-us/product/mcp23017)
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,5 +1,6 @@
|
||||
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
|
||||
//! It demonstrates LoRaWAN join functionality.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![macro_use]
|
||||
|
@ -1,5 +1,6 @@
|
||||
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
|
||||
//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![macro_use]
|
||||
|
@ -1,5 +1,6 @@
|
||||
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
|
||||
//! It demonstrates LORA P2P send functionality.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![macro_use]
|
||||
|
@ -1,5 +1,6 @@
|
||||
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
|
||||
//! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![macro_use]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how to send messages between the two cores in the RP2040 chip.
|
||||
//!
|
||||
//! The LED on the RP Pico W board is connected differently. See wifi_blinky.rs.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,5 @@
|
||||
//! This example shows powerful PIO module in the RP2040 chip.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
@ -54,7 +56,14 @@ fn setup_pio_task_sm1<'a>(pio: &mut Common<'a, PIO0>, sm: &mut StateMachine<'a,
|
||||
// Setupm sm1
|
||||
|
||||
// Read 0b10101 repeatedly until ISR is full
|
||||
let prg = pio_proc::pio_asm!(".origin 8", "set x, 0x15", ".wrap_target", "in x, 5 [31]", ".wrap",);
|
||||
let prg = pio_proc::pio_asm!(
|
||||
//
|
||||
".origin 8",
|
||||
"set x, 0x15",
|
||||
".wrap_target",
|
||||
"in x, 5 [31]",
|
||||
".wrap",
|
||||
);
|
||||
|
||||
let relocated = RelocatedProgram::new(&prg.program);
|
||||
let mut cfg = Config::default();
|
||||
|
@ -1,3 +1,5 @@
|
||||
//! This example shows powerful PIO module in the RP2040 chip.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,6 @@
|
||||
//! This example shows powerful PIO module in the RP2040 chip to communicate with a HD44780 display.
|
||||
//! See (https://www.sparkfun.com/datasheets/LCD/HD44780.pdf)
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,6 @@
|
||||
//! This example shows powerful PIO module in the RP2040 chip to communicate with WS2812 LED modules.
|
||||
//! See (https://www.sparkfun.com/categories/tags/ws2812)
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip.
|
||||
//!
|
||||
//! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
46
examples/rp/src/bin/rtc.rs
Normal file
46
examples/rp/src/bin/rtc.rs
Normal file
@ -0,0 +1,46 @@
|
||||
//! This example shows how to use RTC (Real Time Clock) in the RP2040 chip.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_rp::rtc::{DateTime, DayOfWeek, Rtc};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = embassy_rp::init(Default::default());
|
||||
info!("Wait for 20s");
|
||||
|
||||
let mut rtc = Rtc::new(p.RTC);
|
||||
|
||||
if !rtc.is_running() {
|
||||
info!("Start RTC");
|
||||
let now = DateTime {
|
||||
year: 2000,
|
||||
month: 1,
|
||||
day: 1,
|
||||
day_of_week: DayOfWeek::Saturday,
|
||||
hour: 0,
|
||||
minute: 0,
|
||||
second: 0,
|
||||
};
|
||||
rtc.set_datetime(now).unwrap();
|
||||
}
|
||||
|
||||
Timer::after(Duration::from_millis(20000)).await;
|
||||
|
||||
if let Ok(dt) = rtc.now() {
|
||||
info!(
|
||||
"Now: {}-{:02}-{:02} {}:{:02}:{:02}",
|
||||
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
|
||||
);
|
||||
}
|
||||
|
||||
info!("Reboot.");
|
||||
Timer::after(Duration::from_millis(200)).await;
|
||||
cortex_m::peripheral::SCB::sys_reset();
|
||||
}
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip.
|
||||
//!
|
||||
//! Example for resistive touch sensor in Waveshare Pico-ResTouch
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,6 @@
|
||||
//! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip.
|
||||
//! No specific hardware is specified in this example. If you connect pin 11 and 12 you should get the same data back.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,8 @@
|
||||
//! This example shows how to use SPI (Serial Peripheral Interface) in the RP2040 chip.
|
||||
//!
|
||||
//! Example written for a display using the ST7789 chip. Possibly the Waveshare Pico-ResTouch
|
||||
//! (https://www.waveshare.com/wiki/Pico-ResTouch-LCD-2.8)
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,9 @@
|
||||
//! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip.
|
||||
//!
|
||||
//! No specific hardware is specified in this example. Only output on pin 0 is tested.
|
||||
//! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used
|
||||
//! with its UART port.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,9 @@
|
||||
//! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip.
|
||||
//!
|
||||
//! No specific hardware is specified in this example. If you connect pin 0 and 1 you should get the same data back.
|
||||
//! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used
|
||||
//! with its UART port.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,5 +1,9 @@
|
||||
//! test TX-only and RX-only UARTs. You need to connect GPIO0 to GPIO5 for
|
||||
//! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip.
|
||||
//!
|
||||
//! Test TX-only and RX-only on two different UARTs. You need to connect GPIO0 to GPIO5 for
|
||||
//! this to work
|
||||
//! The Raspberry Pi Debug Probe (https://www.raspberrypi.com/products/debug-probe/) could be used
|
||||
//! with its UART port.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip.
|
||||
//!
|
||||
//! This is a CDC-NCM class implementation, aka Ethernet over USB.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip.
|
||||
//!
|
||||
//! This creates the possibility to send log::info/warn/error/debug! to USB serial port.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip.
|
||||
//!
|
||||
//! This creates a USB serial port that echos.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example shows how to use Watchdog in the RP2040 chip.
|
||||
//!
|
||||
//! It does not work with the RP Pico W board. See wifi_blinky.rs or connect external LED and resistor.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,6 @@
|
||||
//! This example uses the RP Pico W board Wifi chip (cyw43).
|
||||
//! Creates an Access point Wifi network and creates a TCP endpoint on port 1234.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,7 @@
|
||||
//! This example test the RP Pico W on board LED.
|
||||
//!
|
||||
//! It does not work with the RP Pico board. See blinky.rs.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,6 @@
|
||||
//! This example uses the RP Pico W board Wifi chip (cyw43).
|
||||
//! Scans Wifi for ssid names.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -1,3 +1,6 @@
|
||||
//! This example uses the RP Pico W board Wifi chip (cyw43).
|
||||
//! Connects to specified Wifi network and creates a TCP endpoint on port 1234.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
@ -43,5 +43,6 @@ required-features = ["mac"]
|
||||
name = "eddystone_beacon"
|
||||
required-features = ["ble"]
|
||||
|
||||
[patch.crates-io]
|
||||
stm32wb-hci = { git = "https://github.com/OueslatiGhaith/stm32wb-hci", rev = "9f663be"}
|
||||
[[bin]]
|
||||
name = "gatt_server"
|
||||
required-features = ["ble"]
|
397
examples/stm32wb/src/bin/gatt_server.rs
Normal file
397
examples/stm32wb/src/bin/gatt_server.rs
Normal file
@ -0,0 +1,397 @@
|
||||
#![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::hci::event::command::{CommandComplete, ReturnParameters};
|
||||
use embassy_stm32_wpan::hci::host::uart::{Packet, UartHci};
|
||||
use embassy_stm32_wpan::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType};
|
||||
use embassy_stm32_wpan::hci::types::AdvertisingType;
|
||||
use embassy_stm32_wpan::hci::vendor::stm32wb::command::gap::{
|
||||
AddressType, AuthenticationRequirements, DiscoverableParameters, GapCommands, IoCapability, LocalName, Pin, Role,
|
||||
SecureConnectionSupport,
|
||||
};
|
||||
use embassy_stm32_wpan::hci::vendor::stm32wb::command::gatt::{
|
||||
AddCharacteristicParameters, AddServiceParameters, CharacteristicEvent, CharacteristicPermission,
|
||||
CharacteristicProperty, EncryptionKeySize, GattCommands, ServiceType, UpdateCharacteristicValueParameters, Uuid,
|
||||
WriteResponseParameters,
|
||||
};
|
||||
use embassy_stm32_wpan::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel};
|
||||
use embassy_stm32_wpan::hci::vendor::stm32wb::event::{self, AttributeHandle, Stm32Wb5xEvent};
|
||||
use embassy_stm32_wpan::hci::{BdAddr, Event};
|
||||
use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp;
|
||||
use embassy_stm32_wpan::sub::ble::Ble;
|
||||
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;
|
||||
defmt::debug!("{}", 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;
|
||||
defmt::debug!("{}", 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;
|
||||
defmt::debug!("{}", 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;
|
||||
defmt::debug!("{}", 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;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
info!("config tx power level...");
|
||||
mbox.ble_subsystem.set_tx_power_level(PowerLevel::ZerodBm).await;
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
info!("GATT init...");
|
||||
mbox.ble_subsystem.init_gatt().await;
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", 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;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
info!("set IO capabilities...");
|
||||
mbox.ble_subsystem.set_io_capability(IoCapability::DisplayConfirm).await;
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
info!("set authentication requirements...");
|
||||
mbox.ble_subsystem
|
||||
.set_authentication_requirement(&AuthenticationRequirements {
|
||||
bonding_required: false,
|
||||
keypress_notification_support: false,
|
||||
mitm_protection_required: false,
|
||||
encryption_key_size_range: (8, 16),
|
||||
fixed_pin: Pin::Requested,
|
||||
identity_address_type: AddressType::Public,
|
||||
secure_connection_support: SecureConnectionSupport::Optional,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
info!("set scan response data...");
|
||||
mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap();
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
info!("set scan response data...");
|
||||
mbox.ble_subsystem.le_set_scan_response_data(b"TXTX").await.unwrap();
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
defmt::info!("initializing services and characteristics...");
|
||||
let mut ble_context = init_gatt_services(&mut mbox.ble_subsystem).await.unwrap();
|
||||
defmt::info!("{}", ble_context);
|
||||
|
||||
let discovery_params = DiscoverableParameters {
|
||||
advertising_type: AdvertisingType::ConnectableUndirected,
|
||||
advertising_interval: Some((Duration::from_millis(100), Duration::from_millis(100))),
|
||||
address_type: OwnAddressType::Public,
|
||||
filter_policy: AdvertisingFilterPolicy::AllowConnectionAndScan,
|
||||
local_name: Some(LocalName::Complete(b"TXTX")),
|
||||
advertising_data: &[],
|
||||
conn_interval: (None, None),
|
||||
};
|
||||
|
||||
info!("set discoverable...");
|
||||
mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap();
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
loop {
|
||||
let response = mbox.ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
if let Ok(Packet::Event(event)) = response {
|
||||
match event {
|
||||
Event::LeConnectionComplete(_) => {
|
||||
defmt::info!("connected");
|
||||
}
|
||||
Event::DisconnectionComplete(_) => {
|
||||
defmt::info!("disconnected");
|
||||
ble_context.is_subscribed = false;
|
||||
mbox.ble_subsystem.set_discoverable(&discovery_params).await.unwrap();
|
||||
}
|
||||
Event::Vendor(vendor_event) => match vendor_event {
|
||||
Stm32Wb5xEvent::AttReadPermitRequest(read_req) => {
|
||||
defmt::info!("read request received {}, allowing", read_req);
|
||||
mbox.ble_subsystem.allow_read(read_req.conn_handle).await
|
||||
}
|
||||
Stm32Wb5xEvent::AttWritePermitRequest(write_req) => {
|
||||
defmt::info!("write request received {}, allowing", write_req);
|
||||
mbox.ble_subsystem
|
||||
.write_response(&WriteResponseParameters {
|
||||
conn_handle: write_req.conn_handle,
|
||||
attribute_handle: write_req.attribute_handle,
|
||||
status: Ok(()),
|
||||
value: write_req.value(),
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
Stm32Wb5xEvent::GattAttributeModified(attribute) => {
|
||||
defmt::info!("{}", ble_context);
|
||||
if attribute.attr_handle.0 == ble_context.chars.notify.0 + 2 {
|
||||
if attribute.data()[0] == 0x01 {
|
||||
defmt::info!("subscribed");
|
||||
ble_context.is_subscribed = true;
|
||||
} else {
|
||||
defmt::info!("unsubscribed");
|
||||
ble_context.is_subscribed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
#[derive(defmt::Format)]
|
||||
pub struct BleContext {
|
||||
pub service_handle: AttributeHandle,
|
||||
pub chars: CharHandles,
|
||||
pub is_subscribed: bool,
|
||||
}
|
||||
|
||||
#[derive(defmt::Format)]
|
||||
pub struct CharHandles {
|
||||
pub read: AttributeHandle,
|
||||
pub write: AttributeHandle,
|
||||
pub notify: AttributeHandle,
|
||||
}
|
||||
|
||||
pub async fn init_gatt_services(ble_subsystem: &mut Ble) -> Result<BleContext, ()> {
|
||||
let service_handle = gatt_add_service(ble_subsystem, Uuid::Uuid16(0x500)).await?;
|
||||
|
||||
let read = gatt_add_char(
|
||||
ble_subsystem,
|
||||
service_handle,
|
||||
Uuid::Uuid16(0x501),
|
||||
CharacteristicProperty::READ,
|
||||
Some(b"Hello from embassy!"),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let write = gatt_add_char(
|
||||
ble_subsystem,
|
||||
service_handle,
|
||||
Uuid::Uuid16(0x502),
|
||||
CharacteristicProperty::WRITE_WITHOUT_RESPONSE | CharacteristicProperty::WRITE | CharacteristicProperty::READ,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let notify = gatt_add_char(
|
||||
ble_subsystem,
|
||||
service_handle,
|
||||
Uuid::Uuid16(0x503),
|
||||
CharacteristicProperty::NOTIFY | CharacteristicProperty::READ,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(BleContext {
|
||||
service_handle,
|
||||
is_subscribed: false,
|
||||
chars: CharHandles { read, write, notify },
|
||||
})
|
||||
}
|
||||
|
||||
async fn gatt_add_service(ble_subsystem: &mut Ble, uuid: Uuid) -> Result<AttributeHandle, ()> {
|
||||
ble_subsystem
|
||||
.add_service(&AddServiceParameters {
|
||||
uuid,
|
||||
service_type: ServiceType::Primary,
|
||||
max_attribute_records: 8,
|
||||
})
|
||||
.await;
|
||||
let response = ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
if let Ok(Packet::Event(Event::CommandComplete(CommandComplete {
|
||||
return_params:
|
||||
ReturnParameters::Vendor(event::command::ReturnParameters::GattAddService(event::command::GattService {
|
||||
service_handle,
|
||||
..
|
||||
})),
|
||||
..
|
||||
}))) = response
|
||||
{
|
||||
Ok(service_handle)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn gatt_add_char(
|
||||
ble_subsystem: &mut Ble,
|
||||
service_handle: AttributeHandle,
|
||||
characteristic_uuid: Uuid,
|
||||
characteristic_properties: CharacteristicProperty,
|
||||
default_value: Option<&[u8]>,
|
||||
) -> Result<AttributeHandle, ()> {
|
||||
ble_subsystem
|
||||
.add_characteristic(&AddCharacteristicParameters {
|
||||
service_handle,
|
||||
characteristic_uuid,
|
||||
characteristic_properties,
|
||||
characteristic_value_len: 32,
|
||||
security_permissions: CharacteristicPermission::empty(),
|
||||
gatt_event_mask: CharacteristicEvent::all(),
|
||||
encryption_key_size: EncryptionKeySize::with_value(7).unwrap(),
|
||||
is_variable: true,
|
||||
})
|
||||
.await;
|
||||
let response = ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
|
||||
if let Ok(Packet::Event(Event::CommandComplete(CommandComplete {
|
||||
return_params:
|
||||
ReturnParameters::Vendor(event::command::ReturnParameters::GattAddCharacteristic(
|
||||
event::command::GattCharacteristic {
|
||||
characteristic_handle, ..
|
||||
},
|
||||
)),
|
||||
..
|
||||
}))) = response
|
||||
{
|
||||
if let Some(value) = default_value {
|
||||
ble_subsystem
|
||||
.update_characteristic_value(&UpdateCharacteristicValueParameters {
|
||||
service_handle,
|
||||
characteristic_handle,
|
||||
offset: 0,
|
||||
value,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let response = ble_subsystem.read().await;
|
||||
defmt::debug!("{}", response);
|
||||
}
|
||||
Ok(characteristic_handle)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@
|
||||
runner = "probe-rs run --chip STM32WLE5JCIx"
|
||||
|
||||
[build]
|
||||
target = "thumbv7em-none-eabihf"
|
||||
target = "thumbv7em-none-eabi"
|
||||
|
||||
[env]
|
||||
DEFMT_LOG = "trace"
|
||||
|
@ -8,8 +8,8 @@ license = "MIT OR Apache-2.0"
|
||||
embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
|
||||
embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
|
||||
embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] }
|
||||
embassy-embedded-hal = {version = "0.1.0", path = "../../embassy-embedded-hal" }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti", "chrono"] }
|
||||
embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" }
|
||||
embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] }
|
||||
lora-phy = { version = "1" }
|
||||
lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] }
|
||||
@ -25,6 +25,7 @@ embedded-storage = "0.3.0"
|
||||
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 }
|
||||
chrono = { version = "^0.4", default-features = false }
|
||||
|
||||
[patch.crates-io]
|
||||
lora-phy = { git = "https://github.com/embassy-rs/lora-phy", rev = "ad289428fd44b02788e2fa2116445cc8f640a265" }
|
||||
|
43
examples/stm32wl/src/bin/rtc.rs
Normal file
43
examples/stm32wl/src/bin/rtc.rs
Normal file
@ -0,0 +1,43 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::rcc::{self, ClockSrc};
|
||||
use embassy_stm32::rtc::{Rtc, RtcConfig};
|
||||
use embassy_stm32::Config;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let p = {
|
||||
let mut config = Config::default();
|
||||
config.rcc.mux = ClockSrc::HSE32;
|
||||
config.rcc.rtc_mux = rcc::RtcClockSource::LSE32;
|
||||
config.rcc.enable_rtc_apb = true;
|
||||
embassy_stm32::init(config)
|
||||
};
|
||||
info!("Hello World!");
|
||||
|
||||
let now = NaiveDate::from_ymd_opt(2020, 5, 15)
|
||||
.unwrap()
|
||||
.and_hms_opt(10, 30, 15)
|
||||
.unwrap();
|
||||
|
||||
let mut rtc = Rtc::new(
|
||||
p.RTC,
|
||||
RtcConfig::default().clock_config(embassy_stm32::rtc::RtcClockSource::LSE),
|
||||
);
|
||||
info!("Got RTC! {:?}", now.timestamp());
|
||||
|
||||
rtc.set_datetime(now.into()).expect("datetime not set");
|
||||
|
||||
// In reality the delay would be much longer
|
||||
Timer::after(Duration::from_millis(20000)).await;
|
||||
|
||||
let then: NaiveDateTime = rtc.now().unwrap().into();
|
||||
info!("Got RTC! {:?}", then.timestamp());
|
||||
}
|
Reference in New Issue
Block a user