diff options
| author | goueslati <[email protected]> | 2023-07-10 16:54:48 +0100 |
|---|---|---|
| committer | goueslati <[email protected]> | 2023-07-10 16:54:48 +0100 |
| commit | 4aca7c8811b70e420280893784cdad2acbe326f9 (patch) | |
| tree | 7d7b1d989b2533d536725b2b7a6d0e63b9c4c2ae | |
| parent | 8a811cfcf75b25fe81168134bf0cf8a8d387c391 (diff) | |
wip
| -rw-r--r-- | embassy-stm32-wpan/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/sub/mac/commands.rs | 83 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/sub/mac/mod.rs (renamed from embassy-stm32-wpan/src/sub/mac.rs) | 18 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/sub/mac/opcodes.rs | 27 | ||||
| -rw-r--r-- | examples/stm32wb/.cargo/config.toml | 4 | ||||
| -rw-r--r-- | examples/stm32wb/Cargo.toml | 6 | ||||
| -rw-r--r-- | examples/stm32wb/src/bin/tl_mbox_mac_2.rs | 117 | ||||
| -rw-r--r-- | rust-toolchain.toml | 4 |
8 files changed, 255 insertions, 5 deletions
diff --git a/embassy-stm32-wpan/Cargo.toml b/embassy-stm32-wpan/Cargo.toml index 5141f9bd2..4b5dcdd29 100644 --- a/embassy-stm32-wpan/Cargo.toml +++ b/embassy-stm32-wpan/Cargo.toml | |||
| @@ -28,6 +28,7 @@ stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] } | |||
| 28 | stm32wb-hci = { version = "0.1.2", features = ["version-5-0"], optional = true } | 28 | stm32wb-hci = { version = "0.1.2", features = ["version-5-0"], optional = true } |
| 29 | 29 | ||
| 30 | [features] | 30 | [features] |
| 31 | default = ["stm32wb55rg", "mac", "ble"] | ||
| 31 | defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt"] | 32 | defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt"] |
| 32 | 33 | ||
| 33 | ble = ["dep:stm32wb-hci"] | 34 | ble = ["dep:stm32wb-hci"] |
diff --git a/embassy-stm32-wpan/src/sub/mac/commands.rs b/embassy-stm32-wpan/src/sub/mac/commands.rs new file mode 100644 index 000000000..75a31d2fc --- /dev/null +++ b/embassy-stm32-wpan/src/sub/mac/commands.rs | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | use bit_field::BitField; | ||
| 2 | |||
| 3 | use super::opcodes::OpcodeM4ToM0; | ||
| 4 | |||
| 5 | pub trait MacCommand { | ||
| 6 | type Response; | ||
| 7 | const OPCODE: OpcodeM4ToM0; | ||
| 8 | const SIZE: usize; | ||
| 9 | |||
| 10 | fn copy_into_slice(&self, buf: &mut [u8]); | ||
| 11 | } | ||
| 12 | |||
| 13 | pub struct ResetRequest { | ||
| 14 | /// MAC PIB attributes are set to their default values or not during reset | ||
| 15 | pub set_default_pib: bool, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl MacCommand for ResetRequest { | ||
| 19 | type Response = (); | ||
| 20 | const OPCODE: OpcodeM4ToM0 = OpcodeM4ToM0::MlmeResetReq; | ||
| 21 | const SIZE: usize = 4; | ||
| 22 | |||
| 23 | fn copy_into_slice(&self, buf: &mut [u8]) { | ||
| 24 | buf[0] = self.set_default_pib as u8; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | #[repr(C)] | ||
| 29 | pub struct SetRequest { | ||
| 30 | pub pib_attribute_ptr: *const u8, | ||
| 31 | pub pib_attribute: u8, | ||
| 32 | pub stuffing: [u8; 3], | ||
| 33 | } | ||
| 34 | |||
| 35 | impl MacCommand for SetRequest { | ||
| 36 | type Response = (); | ||
| 37 | const OPCODE: OpcodeM4ToM0 = OpcodeM4ToM0::MlmeSetReq; | ||
| 38 | const SIZE: usize = 8; | ||
| 39 | |||
| 40 | fn copy_into_slice(&self, buf: &mut [u8]) { | ||
| 41 | let address = self.pib_attribute_ptr as usize; | ||
| 42 | |||
| 43 | // 68 ff 2 20 6f | ||
| 44 | |||
| 45 | let a = unsafe { core::slice::from_raw_parts(&self as *const _ as *const u8, Self::SIZE) }; | ||
| 46 | debug!("{:#04x}", a); | ||
| 47 | |||
| 48 | unsafe { core::ptr::copy(self as *const _ as *const u8, buf as *mut _ as *mut u8, 8) }; | ||
| 49 | |||
| 50 | // buf[0] = self.pib_attribute_ptr as u8; | ||
| 51 | // buf[1] = self.pib_attribute; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | pub struct AssociateRequest { | ||
| 56 | pub channel_number: u8, | ||
| 57 | pub channel_page: u8, | ||
| 58 | pub coord_addr_mode: u8, | ||
| 59 | pub capability_information: u8, | ||
| 60 | pub coord_pan_id: [u8; 2], | ||
| 61 | pub security_level: u8, | ||
| 62 | pub key_id_mode: u8, | ||
| 63 | pub key_source: [u8; 8], | ||
| 64 | pub coord_address: MacAddress, | ||
| 65 | pub key_index: u8, | ||
| 66 | } | ||
| 67 | |||
| 68 | impl MacCommand for AssociateRequest { | ||
| 69 | const OPCODE: OpcodeM4ToM0 = OpcodeM4ToM0::MlmeAssociateReq; | ||
| 70 | const SIZE: usize = 25; | ||
| 71 | type Response = (); | ||
| 72 | |||
| 73 | fn copy_into_slice(&self, buf: &mut [u8]) { | ||
| 74 | let a = unsafe { core::slice::from_raw_parts(&self as *const _ as *const u8, core::mem::size_of::<Self>()) }; | ||
| 75 | |||
| 76 | buf[..a.len()].copy_from_slice(a); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | pub union MacAddress { | ||
| 81 | pub short: [u8; 2], | ||
| 82 | pub extended: [u8; 8], | ||
| 83 | } | ||
diff --git a/embassy-stm32-wpan/src/sub/mac.rs b/embassy-stm32-wpan/src/sub/mac/mod.rs index f7a59b0e4..83970a881 100644 --- a/embassy-stm32-wpan/src/sub/mac.rs +++ b/embassy-stm32-wpan/src/sub/mac/mod.rs | |||
| @@ -8,12 +8,16 @@ use embassy_futures::poll_once; | |||
| 8 | use embassy_stm32::ipcc::Ipcc; | 8 | use embassy_stm32::ipcc::Ipcc; |
| 9 | use embassy_sync::waitqueue::AtomicWaker; | 9 | use embassy_sync::waitqueue::AtomicWaker; |
| 10 | 10 | ||
| 11 | use self::commands::MacCommand; | ||
| 11 | use crate::cmd::CmdPacket; | 12 | use crate::cmd::CmdPacket; |
| 12 | use crate::consts::TlPacketType; | 13 | use crate::consts::TlPacketType; |
| 13 | use crate::evt::{EvtBox, EvtPacket}; | 14 | use crate::evt::{EvtBox, EvtPacket}; |
| 14 | use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER}; | 15 | use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER}; |
| 15 | use crate::{channels, evt}; | 16 | use crate::{channels, evt}; |
| 16 | 17 | ||
| 18 | pub mod commands; | ||
| 19 | mod opcodes; | ||
| 20 | |||
| 17 | static MAC_WAKER: AtomicWaker = AtomicWaker::new(); | 21 | static MAC_WAKER: AtomicWaker = AtomicWaker::new(); |
| 18 | static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false); | 22 | static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false); |
| 19 | 23 | ||
| @@ -77,8 +81,22 @@ impl Mac { | |||
| 77 | }) | 81 | }) |
| 78 | .await; | 82 | .await; |
| 79 | } | 83 | } |
| 84 | |||
| 85 | pub async fn send_command<T>(&self, cmd: T) -> u8 | ||
| 86 | where | ||
| 87 | T: MacCommand, | ||
| 88 | { | ||
| 89 | let mut payload = [0u8; MAX_PACKET_SIZE]; | ||
| 90 | cmd.copy_into_slice(&mut payload); | ||
| 91 | |||
| 92 | debug!("sending {:#x}", payload[..T::SIZE]); | ||
| 93 | |||
| 94 | self.write_and_get_response(T::OPCODE as u16, &payload[..T::SIZE]).await | ||
| 95 | } | ||
| 80 | } | 96 | } |
| 81 | 97 | ||
| 98 | const MAX_PACKET_SIZE: usize = 255; | ||
| 99 | |||
| 82 | impl evt::MemoryManager for Mac { | 100 | impl evt::MemoryManager for Mac { |
| 83 | /// SAFETY: passing a pointer to something other than a managed event packet is UB | 101 | /// SAFETY: passing a pointer to something other than a managed event packet is UB |
| 84 | unsafe fn drop_event_packet(_: *mut EvtPacket) { | 102 | unsafe fn drop_event_packet(_: *mut EvtPacket) { |
diff --git a/embassy-stm32-wpan/src/sub/mac/opcodes.rs b/embassy-stm32-wpan/src/sub/mac/opcodes.rs new file mode 100644 index 000000000..511b78157 --- /dev/null +++ b/embassy-stm32-wpan/src/sub/mac/opcodes.rs | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | const ST_VENDOR_OGF: u16 = 0x3F; | ||
| 2 | const MAC_802_15_4_CMD_OPCODE_OFFSET: u16 = 0x280; | ||
| 3 | |||
| 4 | const fn opcode(ocf: u16) -> isize { | ||
| 5 | ((ST_VENDOR_OGF << 9) | (MAC_802_15_4_CMD_OPCODE_OFFSET + ocf)) as isize | ||
| 6 | } | ||
| 7 | |||
| 8 | pub enum OpcodeM4ToM0 { | ||
| 9 | MlmeAssociateReq = opcode(0x00), | ||
| 10 | MlmeAssociateRes = opcode(0x01), | ||
| 11 | MlmeDisassociateReq = opcode(0x02), | ||
| 12 | MlmeGetReq = opcode(0x03), | ||
| 13 | MlmeGtsReq = opcode(0x04), | ||
| 14 | MlmeOrphanRes = opcode(0x05), | ||
| 15 | MlmeResetReq = opcode(0x06), | ||
| 16 | MlmeRxEnableReq = opcode(0x07), | ||
| 17 | MlmeScanReq = opcode(0x08), | ||
| 18 | MlmeSetReq = opcode(0x09), | ||
| 19 | MlmeStartReq = opcode(0x0A), | ||
| 20 | MlmeSyncReq = opcode(0x0B), | ||
| 21 | MlmePollReq = opcode(0x0C), | ||
| 22 | MlmeDpsReq = opcode(0x0D), | ||
| 23 | MlmeSoundingReq = opcode(0x0E), | ||
| 24 | MlmeCalibrateReq = opcode(0x0F), | ||
| 25 | McpsDataReq = opcode(0x10), | ||
| 26 | McpsPurgeReq = opcode(0x11), | ||
| 27 | } | ||
diff --git a/examples/stm32wb/.cargo/config.toml b/examples/stm32wb/.cargo/config.toml index 8b6d6d754..cf62a10a0 100644 --- a/examples/stm32wb/.cargo/config.toml +++ b/examples/stm32wb/.cargo/config.toml | |||
| @@ -1,7 +1,7 @@ | |||
| 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 chip list` | 2 | # replace STM32WB55CCUx with your chip as listed in `probe-rs chip list` |
| 3 | # runner = "probe-rs run --chip STM32WB55RGVx --speed 1000 --connect-under-reset" | 3 | runner = "probe-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] |
| 7 | target = "thumbv7em-none-eabihf" | 7 | target = "thumbv7em-none-eabihf" |
diff --git a/examples/stm32wb/Cargo.toml b/examples/stm32wb/Cargo.toml index 203ca1486..109734546 100644 --- a/examples/stm32wb/Cargo.toml +++ b/examples/stm32wb/Cargo.toml | |||
| @@ -23,7 +23,7 @@ heapless = { version = "0.7.5", default-features = false } | |||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | [features] | 25 | [features] |
| 26 | default = ["ble"] | 26 | default = ["ble", "mac"] |
| 27 | mac = ["embassy-stm32-wpan/mac"] | 27 | mac = ["embassy-stm32-wpan/mac"] |
| 28 | ble = ["embassy-stm32-wpan/ble"] | 28 | ble = ["embassy-stm32-wpan/ble"] |
| 29 | 29 | ||
| @@ -36,6 +36,10 @@ name = "tl_mbox_mac" | |||
| 36 | required-features = ["mac"] | 36 | required-features = ["mac"] |
| 37 | 37 | ||
| 38 | [[bin]] | 38 | [[bin]] |
| 39 | name = "tl_mbox_mac_2" | ||
| 40 | required-features = ["mac"] | ||
| 41 | |||
| 42 | [[bin]] | ||
| 39 | name = "eddystone_beacon" | 43 | name = "eddystone_beacon" |
| 40 | required-features = ["ble"] | 44 | required-features = ["ble"] |
| 41 | 45 | ||
diff --git a/examples/stm32wb/src/bin/tl_mbox_mac_2.rs b/examples/stm32wb/src/bin/tl_mbox_mac_2.rs new file mode 100644 index 000000000..e069adf87 --- /dev/null +++ b/examples/stm32wb/src/bin/tl_mbox_mac_2.rs | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::bind_interrupts; | ||
| 8 | use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler}; | ||
| 9 | use embassy_stm32_wpan::sub::mac::commands::{AssociateRequest, MacAddress, ResetRequest, SetRequest}; | ||
| 10 | use embassy_stm32_wpan::sub::mm; | ||
| 11 | use embassy_stm32_wpan::TlMbox; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | bind_interrupts!(struct Irqs{ | ||
| 15 | IPCC_C1_RX => ReceiveInterruptHandler; | ||
| 16 | IPCC_C1_TX => TransmitInterruptHandler; | ||
| 17 | }); | ||
| 18 | |||
| 19 | #[embassy_executor::task] | ||
| 20 | async fn run_mm_queue(memory_manager: mm::MemoryManager) { | ||
| 21 | memory_manager.run_queue().await; | ||
| 22 | } | ||
| 23 | |||
| 24 | #[embassy_executor::main] | ||
| 25 | async fn main(spawner: Spawner) { | ||
| 26 | /* | ||
| 27 | How to make this work: | ||
| 28 | |||
| 29 | - Obtain a NUCLEO-STM32WB55 from your preferred supplier. | ||
| 30 | - Download and Install STM32CubeProgrammer. | ||
| 31 | - Download stm32wb5x_FUS_fw.bin, stm32wb5x_BLE_Stack_full_fw.bin, and Release_Notes.html from | ||
| 32 | gh:STMicroelectronics/STM32CubeWB@2234d97/Projects/STM32WB_Copro_Wireless_Binaries/STM32WB5x | ||
| 33 | - Open STM32CubeProgrammer | ||
| 34 | - On the right-hand pane, click "firmware upgrade" to upgrade the st-link firmware. | ||
| 35 | - Once complete, click connect to connect to the device. | ||
| 36 | - On the left hand pane, click the RSS signal icon to open "Firmware Upgrade Services". | ||
| 37 | - In the Release_Notes.html, find the memory address that corresponds to your device for the stm32wb5x_FUS_fw.bin file | ||
| 38 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 39 | - Once complete, in the Release_Notes.html, find the memory address that corresponds to your device for the | ||
| 40 | stm32wb5x_BLE_Stack_full_fw.bin file. It should not be the same memory address. | ||
| 41 | - Select that file, the memory address, "verify download", and then "Firmware Upgrade". | ||
| 42 | - Select "Start Wireless Stack". | ||
| 43 | - Disconnect from the device. | ||
| 44 | - In the examples folder for stm32wb, modify the memory.x file to match your target device. | ||
| 45 | - Run this example. | ||
| 46 | |||
| 47 | Note: extended stack versions are not supported at this time. Do not attempt to install a stack with "extended" in the name. | ||
| 48 | */ | ||
| 49 | |||
| 50 | let p = embassy_stm32::init(Default::default()); | ||
| 51 | info!("Hello World!"); | ||
| 52 | |||
| 53 | let config = Config::default(); | ||
| 54 | let mbox = TlMbox::init(p.IPCC, Irqs, config); | ||
| 55 | |||
| 56 | spawner.spawn(run_mm_queue(mbox.mm_subsystem)).unwrap(); | ||
| 57 | |||
| 58 | let sys_event = mbox.sys_subsystem.read().await; | ||
| 59 | info!("sys event: {}", sys_event.payload()); | ||
| 60 | |||
| 61 | core::mem::drop(sys_event); | ||
| 62 | |||
| 63 | let result = mbox.sys_subsystem.shci_c2_mac_802_15_4_init().await; | ||
| 64 | info!("initialized mac: {}", result); | ||
| 65 | |||
| 66 | info!("resetting"); | ||
| 67 | let response = mbox | ||
| 68 | .mac_subsystem | ||
| 69 | .send_command(ResetRequest { set_default_pib: true }) | ||
| 70 | .await; | ||
| 71 | info!("{}", response); | ||
| 72 | |||
| 73 | info!("setting extended address"); | ||
| 74 | let extended_address: u64 = 0xACDE480000000001; | ||
| 75 | defmt::debug!("{}", &extended_address as *const _ as *const u8); | ||
| 76 | let response = mbox | ||
| 77 | .mac_subsystem | ||
| 78 | .send_command(SetRequest { | ||
| 79 | pib_attribute_ptr: &extended_address as *const _ as *const u8, | ||
| 80 | pib_attribute: 0x6F, | ||
| 81 | stuffing: [0; 3], | ||
| 82 | }) | ||
| 83 | .await; | ||
| 84 | info!("{}", response); | ||
| 85 | |||
| 86 | // info!("association request"); | ||
| 87 | // mbox.mac_subsystem | ||
| 88 | // .send_command(AssociateRequest { | ||
| 89 | // channel_number: 16, | ||
| 90 | // channel_page: 0, | ||
| 91 | // coord_addr_mode: 2, | ||
| 92 | // coord_address: MacAddress { short: [0x22, 0x11] }, | ||
| 93 | // capability_information: 0x80, | ||
| 94 | // coord_pan_id: [0xAA, 0x1A], | ||
| 95 | // security_level: 0, | ||
| 96 | |||
| 97 | // key_id_mode: 0, | ||
| 98 | // key_index: 0, | ||
| 99 | // key_source: [0; 8], | ||
| 100 | // }) | ||
| 101 | // .await; | ||
| 102 | // info!("reading"); | ||
| 103 | // let result = mbox.mac_subsystem.read().await; | ||
| 104 | // info!("{}", result.payload()); | ||
| 105 | |||
| 106 | // | ||
| 107 | // info!("starting ble..."); | ||
| 108 | // mbox.ble_subsystem.t_write(0x0c, &[]).await; | ||
| 109 | // | ||
| 110 | // info!("waiting for ble..."); | ||
| 111 | // let ble_event = mbox.ble_subsystem.tl_read().await; | ||
| 112 | // | ||
| 113 | // info!("ble event: {}", ble_event.payload()); | ||
| 114 | |||
| 115 | info!("Test OK"); | ||
| 116 | cortex_m::asm::bkpt(); | ||
| 117 | } | ||
diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 5db74c7a9..bad6d3a42 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | # Before upgrading check that everything is available on all tier1 targets here: | 1 | # Before upgrading check that everything is available on all tier1 targets here: |
| 2 | # https://rust-lang.github.io/rustup-components-history | 2 | # https://rust-lang.github.io/rustup-components-history |
| 3 | [toolchain] | 3 | [toolchain] |
| 4 | channel = "nightly-2023-06-28" | 4 | channel = "nightly" |
| 5 | components = [ "rust-src", "rustfmt", "llvm-tools-preview" ] | 5 | components = [ "rust-src", "rustfmt", "llvm-tools" ] |
| 6 | targets = [ | 6 | targets = [ |
| 7 | "thumbv7em-none-eabi", | 7 | "thumbv7em-none-eabi", |
| 8 | "thumbv7m-none-eabi", | 8 | "thumbv7m-none-eabi", |
