This commit is contained in:
goueslati
2023-07-10 16:54:48 +01:00
parent 8a811cfcf7
commit 4aca7c8811
8 changed files with 255 additions and 5 deletions

View File

@ -28,6 +28,7 @@ stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] }
stm32wb-hci = { version = "0.1.2", features = ["version-5-0"], optional = true }
[features]
default = ["stm32wb55rg", "mac", "ble"]
defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt"]
ble = ["dep:stm32wb-hci"]

View File

@ -0,0 +1,83 @@
use bit_field::BitField;
use super::opcodes::OpcodeM4ToM0;
pub trait MacCommand {
type Response;
const OPCODE: OpcodeM4ToM0;
const SIZE: usize;
fn copy_into_slice(&self, buf: &mut [u8]);
}
pub struct ResetRequest {
/// MAC PIB attributes are set to their default values or not during reset
pub set_default_pib: bool,
}
impl MacCommand for ResetRequest {
type Response = ();
const OPCODE: OpcodeM4ToM0 = OpcodeM4ToM0::MlmeResetReq;
const SIZE: usize = 4;
fn copy_into_slice(&self, buf: &mut [u8]) {
buf[0] = self.set_default_pib as u8;
}
}
#[repr(C)]
pub struct SetRequest {
pub pib_attribute_ptr: *const u8,
pub pib_attribute: u8,
pub stuffing: [u8; 3],
}
impl MacCommand for SetRequest {
type Response = ();
const OPCODE: OpcodeM4ToM0 = OpcodeM4ToM0::MlmeSetReq;
const SIZE: usize = 8;
fn copy_into_slice(&self, buf: &mut [u8]) {
let address = self.pib_attribute_ptr as usize;
// 68 ff 2 20 6f
let a = unsafe { core::slice::from_raw_parts(&self as *const _ as *const u8, Self::SIZE) };
debug!("{:#04x}", a);
unsafe { core::ptr::copy(self as *const _ as *const u8, buf as *mut _ as *mut u8, 8) };
// buf[0] = self.pib_attribute_ptr as u8;
// buf[1] = self.pib_attribute;
}
}
pub struct AssociateRequest {
pub channel_number: u8,
pub channel_page: u8,
pub coord_addr_mode: u8,
pub capability_information: u8,
pub coord_pan_id: [u8; 2],
pub security_level: u8,
pub key_id_mode: u8,
pub key_source: [u8; 8],
pub coord_address: MacAddress,
pub key_index: u8,
}
impl MacCommand for AssociateRequest {
const OPCODE: OpcodeM4ToM0 = OpcodeM4ToM0::MlmeAssociateReq;
const SIZE: usize = 25;
type Response = ();
fn copy_into_slice(&self, buf: &mut [u8]) {
let a = unsafe { core::slice::from_raw_parts(&self as *const _ as *const u8, core::mem::size_of::<Self>()) };
buf[..a.len()].copy_from_slice(a);
}
}
pub union MacAddress {
pub short: [u8; 2],
pub extended: [u8; 8],
}

View File

@ -8,12 +8,16 @@ use embassy_futures::poll_once;
use embassy_stm32::ipcc::Ipcc;
use embassy_sync::waitqueue::AtomicWaker;
use self::commands::MacCommand;
use crate::cmd::CmdPacket;
use crate::consts::TlPacketType;
use crate::evt::{EvtBox, EvtPacket};
use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER};
use crate::{channels, evt};
pub mod commands;
mod opcodes;
static MAC_WAKER: AtomicWaker = AtomicWaker::new();
static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false);
@ -77,8 +81,22 @@ impl Mac {
})
.await;
}
pub async fn send_command<T>(&self, cmd: T) -> u8
where
T: MacCommand,
{
let mut payload = [0u8; MAX_PACKET_SIZE];
cmd.copy_into_slice(&mut payload);
debug!("sending {:#x}", payload[..T::SIZE]);
self.write_and_get_response(T::OPCODE as u16, &payload[..T::SIZE]).await
}
}
const MAX_PACKET_SIZE: usize = 255;
impl evt::MemoryManager for Mac {
/// SAFETY: passing a pointer to something other than a managed event packet is UB
unsafe fn drop_event_packet(_: *mut EvtPacket) {

View File

@ -0,0 +1,27 @@
const ST_VENDOR_OGF: u16 = 0x3F;
const MAC_802_15_4_CMD_OPCODE_OFFSET: u16 = 0x280;
const fn opcode(ocf: u16) -> isize {
((ST_VENDOR_OGF << 9) | (MAC_802_15_4_CMD_OPCODE_OFFSET + ocf)) as isize
}
pub enum OpcodeM4ToM0 {
MlmeAssociateReq = opcode(0x00),
MlmeAssociateRes = opcode(0x01),
MlmeDisassociateReq = opcode(0x02),
MlmeGetReq = opcode(0x03),
MlmeGtsReq = opcode(0x04),
MlmeOrphanRes = opcode(0x05),
MlmeResetReq = opcode(0x06),
MlmeRxEnableReq = opcode(0x07),
MlmeScanReq = opcode(0x08),
MlmeSetReq = opcode(0x09),
MlmeStartReq = opcode(0x0A),
MlmeSyncReq = opcode(0x0B),
MlmePollReq = opcode(0x0C),
MlmeDpsReq = opcode(0x0D),
MlmeSoundingReq = opcode(0x0E),
MlmeCalibrateReq = opcode(0x0F),
McpsDataReq = opcode(0x10),
McpsPurgeReq = opcode(0x11),
}