diff options
| -rw-r--r-- | embassy-stm32-wpan/src/cmd.rs | 54 | ||||
| -rw-r--r-- | embassy-stm32-wpan/src/evt.rs | 137 | ||||
| -rw-r--r-- | examples/stm32wb/src/bin/tl_mbox_tx_rx.rs | 4 | ||||
| -rw-r--r-- | tests/stm32/src/bin/tl_mbox.rs | 14 |
4 files changed, 113 insertions, 96 deletions
diff --git a/embassy-stm32-wpan/src/cmd.rs b/embassy-stm32-wpan/src/cmd.rs index 581e5019b..edca82390 100644 --- a/embassy-stm32-wpan/src/cmd.rs +++ b/embassy-stm32-wpan/src/cmd.rs | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | use core::ptr; | 1 | use core::ptr; |
| 2 | 2 | ||
| 3 | use crate::consts::TlPacketType; | 3 | use crate::consts::TlPacketType; |
| 4 | use crate::evt::{EvtPacket, EvtSerial}; | 4 | use crate::PacketHeader; |
| 5 | use crate::{PacketHeader, TL_EVT_HEADER_SIZE}; | ||
| 6 | 5 | ||
| 7 | #[derive(Copy, Clone)] | 6 | #[derive(Copy, Clone)] |
| 8 | #[repr(C, packed)] | 7 | #[repr(C, packed)] |
| @@ -60,31 +59,6 @@ impl CmdPacket { | |||
| 60 | 59 | ||
| 61 | ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len()); | 60 | ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len()); |
| 62 | } | 61 | } |
| 63 | |||
| 64 | /// Writes an underlying CmdPacket into the provided buffer. | ||
| 65 | /// Returns a number of bytes that were written. | ||
| 66 | /// Returns an error if event kind is unknown or if provided buffer size is not enough. | ||
| 67 | #[allow(clippy::result_unit_err)] | ||
| 68 | pub fn write(&self, buf: &mut [u8]) -> Result<usize, ()> { | ||
| 69 | unsafe { | ||
| 70 | let cmd_ptr: *const CmdPacket = self; | ||
| 71 | let self_as_evt_ptr: *const EvtPacket = cmd_ptr.cast(); | ||
| 72 | let evt_serial: *const EvtSerial = &(*self_as_evt_ptr).evt_serial; | ||
| 73 | |||
| 74 | let acl_data: *const AclDataPacket = cmd_ptr.cast(); | ||
| 75 | let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial; | ||
| 76 | let acl_serial_buf: *const u8 = acl_serial.cast(); | ||
| 77 | |||
| 78 | let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE; | ||
| 79 | if len > buf.len() { | ||
| 80 | return Err(()); | ||
| 81 | } | ||
| 82 | |||
| 83 | core::ptr::copy(acl_serial_buf, buf.as_mut_ptr(), len); | ||
| 84 | |||
| 85 | Ok(len) | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | 62 | } |
| 89 | 63 | ||
| 90 | #[derive(Copy, Clone)] | 64 | #[derive(Copy, Clone)] |
| @@ -98,7 +72,33 @@ pub struct AclDataSerial { | |||
| 98 | 72 | ||
| 99 | #[derive(Copy, Clone)] | 73 | #[derive(Copy, Clone)] |
| 100 | #[repr(C, packed)] | 74 | #[repr(C, packed)] |
| 75 | pub struct AclDataSerialStub { | ||
| 76 | pub ty: u8, | ||
| 77 | pub handle: u16, | ||
| 78 | pub length: u16, | ||
| 79 | } | ||
| 80 | |||
| 81 | #[derive(Copy, Clone)] | ||
| 82 | #[repr(C, packed)] | ||
| 101 | pub struct AclDataPacket { | 83 | pub struct AclDataPacket { |
| 102 | pub header: PacketHeader, | 84 | pub header: PacketHeader, |
| 103 | pub acl_data_serial: AclDataSerial, | 85 | pub acl_data_serial: AclDataSerial, |
| 104 | } | 86 | } |
| 87 | |||
| 88 | impl AclDataPacket { | ||
| 89 | pub unsafe fn write_into(cmd_buf: *mut AclDataPacket, packet_type: TlPacketType, handle: u16, payload: &[u8]) { | ||
| 90 | let p_cmd_serial = &mut (*cmd_buf).acl_data_serial as *mut _ as *mut AclDataSerialStub; | ||
| 91 | let p_payload = &mut (*cmd_buf).acl_data_serial.acl_data as *mut _; | ||
| 92 | |||
| 93 | ptr::write_volatile( | ||
| 94 | p_cmd_serial, | ||
| 95 | AclDataSerialStub { | ||
| 96 | ty: packet_type as u8, | ||
| 97 | handle: handle, | ||
| 98 | length: payload.len() as u16, | ||
| 99 | }, | ||
| 100 | ); | ||
| 101 | |||
| 102 | ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len()); | ||
| 103 | } | ||
| 104 | } | ||
diff --git a/embassy-stm32-wpan/src/evt.rs b/embassy-stm32-wpan/src/evt.rs index 82f73a6f8..3a9d03576 100644 --- a/embassy-stm32-wpan/src/evt.rs +++ b/embassy-stm32-wpan/src/evt.rs | |||
| @@ -1,8 +1,6 @@ | |||
| 1 | use core::mem::MaybeUninit; | 1 | use core::{ptr, slice}; |
| 2 | 2 | ||
| 3 | use super::cmd::{AclDataPacket, AclDataSerial}; | 3 | use super::PacketHeader; |
| 4 | use super::consts::TlPacketType; | ||
| 5 | use super::{PacketHeader, TL_EVT_HEADER_SIZE}; | ||
| 6 | use crate::mm; | 4 | use crate::mm; |
| 7 | 5 | ||
| 8 | /** | 6 | /** |
| @@ -63,6 +61,12 @@ pub struct EvtSerial { | |||
| 63 | pub evt: Evt, | 61 | pub evt: Evt, |
| 64 | } | 62 | } |
| 65 | 63 | ||
| 64 | #[derive(Copy, Clone, Default)] | ||
| 65 | pub struct EvtStub { | ||
| 66 | pub kind: u8, | ||
| 67 | pub evt_code: u8, | ||
| 68 | } | ||
| 69 | |||
| 66 | /// This format shall be used for all events (asynchronous and command response) reported | 70 | /// This format shall be used for all events (asynchronous and command response) reported |
| 67 | /// by the CPU2 except for the command response of a system command where the header is not there | 71 | /// by the CPU2 except for the command response of a system command where the header is not there |
| 68 | /// and the format to be used shall be `EvtSerial`. | 72 | /// and the format to be used shall be `EvtSerial`. |
| @@ -101,72 +105,85 @@ impl EvtBox { | |||
| 101 | Self { ptr } | 105 | Self { ptr } |
| 102 | } | 106 | } |
| 103 | 107 | ||
| 104 | /// copies event data from inner pointer and returns an event structure | 108 | /// Returns information about the event |
| 105 | pub fn evt(&self) -> EvtPacket { | 109 | pub fn stub(&self) -> EvtStub { |
| 106 | let mut evt = MaybeUninit::uninit(); | ||
| 107 | unsafe { | ||
| 108 | self.ptr.copy_to(evt.as_mut_ptr(), 1); | ||
| 109 | evt.assume_init() | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | /// writes an underlying [`EvtPacket`] into the provided buffer. | ||
| 114 | /// Returns the number of bytes that were written. | ||
| 115 | /// Returns an error if event kind is unknown or if provided buffer size is not enough. | ||
| 116 | #[allow(clippy::result_unit_err)] | ||
| 117 | pub fn write(&self, buf: &mut [u8]) -> Result<usize, ()> { | ||
| 118 | unsafe { | 110 | unsafe { |
| 119 | let evt_kind = TlPacketType::try_from((*self.ptr).evt_serial.kind)?; | 111 | let p_evt_stub = &(*self.ptr).evt_serial as *const _ as *const EvtStub; |
| 120 | 112 | ||
| 121 | let evt_data: *const EvtPacket = self.ptr.cast(); | 113 | ptr::read_volatile(p_evt_stub) |
| 122 | let evt_serial: *const EvtSerial = &(*evt_data).evt_serial; | ||
| 123 | let evt_serial_buf: *const u8 = evt_serial.cast(); | ||
| 124 | |||
| 125 | let acl_data: *const AclDataPacket = self.ptr.cast(); | ||
| 126 | let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial; | ||
| 127 | let acl_serial_buf: *const u8 = acl_serial.cast(); | ||
| 128 | |||
| 129 | if let TlPacketType::AclData = evt_kind { | ||
| 130 | let len = (*acl_serial).length as usize + 5; | ||
| 131 | if len > buf.len() { | ||
| 132 | return Err(()); | ||
| 133 | } | ||
| 134 | |||
| 135 | core::ptr::copy(evt_serial_buf, buf.as_mut_ptr(), len); | ||
| 136 | |||
| 137 | Ok(len) | ||
| 138 | } else { | ||
| 139 | let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE; | ||
| 140 | if len > buf.len() { | ||
| 141 | return Err(()); | ||
| 142 | } | ||
| 143 | |||
| 144 | core::ptr::copy(acl_serial_buf, buf.as_mut_ptr(), len); | ||
| 145 | |||
| 146 | Ok(len) | ||
| 147 | } | ||
| 148 | } | 114 | } |
| 149 | } | 115 | } |
| 150 | 116 | ||
| 151 | /// returns the size of a buffer required to hold this event | 117 | pub fn payload<'a>(&self) -> &'a [u8] { |
| 152 | #[allow(clippy::result_unit_err)] | ||
| 153 | pub fn size(&self) -> Result<usize, ()> { | ||
| 154 | unsafe { | 118 | unsafe { |
| 155 | let evt_kind = TlPacketType::try_from((*self.ptr).evt_serial.kind)?; | 119 | let p_payload_len = &(*self.ptr).evt_serial.evt.payload_len as *const u8; |
| 120 | let p_payload = &(*self.ptr).evt_serial.evt.payload as *const u8; | ||
| 156 | 121 | ||
| 157 | let evt_data: *const EvtPacket = self.ptr.cast(); | 122 | let payload_len = ptr::read_volatile(p_payload_len); |
| 158 | let evt_serial: *const EvtSerial = &(*evt_data).evt_serial; | ||
| 159 | 123 | ||
| 160 | let acl_data: *const AclDataPacket = self.ptr.cast(); | 124 | slice::from_raw_parts(p_payload, payload_len as usize) |
| 161 | let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial; | ||
| 162 | |||
| 163 | if let TlPacketType::AclData = evt_kind { | ||
| 164 | Ok((*acl_serial).length as usize + 5) | ||
| 165 | } else { | ||
| 166 | Ok((*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE) | ||
| 167 | } | ||
| 168 | } | 125 | } |
| 169 | } | 126 | } |
| 127 | |||
| 128 | // TODO: bring back acl | ||
| 129 | |||
| 130 | // /// writes an underlying [`EvtPacket`] into the provided buffer. | ||
| 131 | // /// Returns the number of bytes that were written. | ||
| 132 | // /// Returns an error if event kind is unknown or if provided buffer size is not enough. | ||
| 133 | // #[allow(clippy::result_unit_err)] | ||
| 134 | // pub fn write(&self, buf: &mut [u8]) -> Result<usize, ()> { | ||
| 135 | // unsafe { | ||
| 136 | // let evt_kind = TlPacketType::try_from((*self.ptr).evt_serial.kind)?; | ||
| 137 | // | ||
| 138 | // let evt_data: *const EvtPacket = self.ptr.cast(); | ||
| 139 | // let evt_serial: *const EvtSerial = &(*evt_data).evt_serial; | ||
| 140 | // let evt_serial_buf: *const u8 = evt_serial.cast(); | ||
| 141 | // | ||
| 142 | // let acl_data: *const AclDataPacket = self.ptr.cast(); | ||
| 143 | // let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial; | ||
| 144 | // let acl_serial_buf: *const u8 = acl_serial.cast(); | ||
| 145 | // | ||
| 146 | // if let TlPacketType::AclData = evt_kind { | ||
| 147 | // let len = (*acl_serial).length as usize + 5; | ||
| 148 | // if len > buf.len() { | ||
| 149 | // return Err(()); | ||
| 150 | // } | ||
| 151 | // | ||
| 152 | // core::ptr::copy(evt_serial_buf, buf.as_mut_ptr(), len); | ||
| 153 | // | ||
| 154 | // Ok(len) | ||
| 155 | // } else { | ||
| 156 | // let len = (*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE; | ||
| 157 | // if len > buf.len() { | ||
| 158 | // return Err(()); | ||
| 159 | // } | ||
| 160 | // | ||
| 161 | // core::ptr::copy(acl_serial_buf, buf.as_mut_ptr(), len); | ||
| 162 | // | ||
| 163 | // Ok(len) | ||
| 164 | // } | ||
| 165 | // } | ||
| 166 | // } | ||
| 167 | // | ||
| 168 | // /// returns the size of a buffer required to hold this event | ||
| 169 | // #[allow(clippy::result_unit_err)] | ||
| 170 | // pub fn size(&self) -> Result<usize, ()> { | ||
| 171 | // unsafe { | ||
| 172 | // let evt_kind = TlPacketType::try_from((*self.ptr).evt_serial.kind)?; | ||
| 173 | // | ||
| 174 | // let evt_data: *const EvtPacket = self.ptr.cast(); | ||
| 175 | // let evt_serial: *const EvtSerial = &(*evt_data).evt_serial; | ||
| 176 | // | ||
| 177 | // let acl_data: *const AclDataPacket = self.ptr.cast(); | ||
| 178 | // let acl_serial: *const AclDataSerial = &(*acl_data).acl_data_serial; | ||
| 179 | // | ||
| 180 | // if let TlPacketType::AclData = evt_kind { | ||
| 181 | // Ok((*acl_serial).length as usize + 5) | ||
| 182 | // } else { | ||
| 183 | // Ok((*evt_serial).evt.payload_len as usize + TL_EVT_HEADER_SIZE) | ||
| 184 | // } | ||
| 185 | // } | ||
| 186 | // } | ||
| 170 | } | 187 | } |
| 171 | 188 | ||
| 172 | impl Drop for EvtBox { | 189 | impl Drop for EvtBox { |
diff --git a/examples/stm32wb/src/bin/tl_mbox_tx_rx.rs b/examples/stm32wb/src/bin/tl_mbox_tx_rx.rs index 91a0f9c0a..84a4f78e4 100644 --- a/examples/stm32wb/src/bin/tl_mbox_tx_rx.rs +++ b/examples/stm32wb/src/bin/tl_mbox_tx_rx.rs | |||
| @@ -48,7 +48,6 @@ async fn main(_spawner: Spawner) { | |||
| 48 | let config = Config::default(); | 48 | let config = Config::default(); |
| 49 | let _ = TlMbox::init(p.IPCC, Irqs, config); | 49 | let _ = TlMbox::init(p.IPCC, Irqs, config); |
| 50 | 50 | ||
| 51 | let mut rx_buf = [0u8; 500]; | ||
| 52 | Sys::shci_c2_ble_init(Default::default()).await; | 51 | Sys::shci_c2_ble_init(Default::default()).await; |
| 53 | 52 | ||
| 54 | info!("starting ble..."); | 53 | info!("starting ble..."); |
| @@ -56,9 +55,8 @@ async fn main(_spawner: Spawner) { | |||
| 56 | 55 | ||
| 57 | info!("waiting for ble..."); | 56 | info!("waiting for ble..."); |
| 58 | let ble_event = Ble::read().await; | 57 | let ble_event = Ble::read().await; |
| 59 | ble_event.write(&mut rx_buf).unwrap(); | ||
| 60 | 58 | ||
| 61 | info!("ble event: {}", rx_buf); | 59 | info!("ble event: {}", ble_event.payload()); |
| 62 | 60 | ||
| 63 | info!("Test OK"); | 61 | info!("Test OK"); |
| 64 | cortex_m::asm::bkpt(); | 62 | cortex_m::asm::bkpt(); |
diff --git a/tests/stm32/src/bin/tl_mbox.rs b/tests/stm32/src/bin/tl_mbox.rs index bb38204b6..259889e35 100644 --- a/tests/stm32/src/bin/tl_mbox.rs +++ b/tests/stm32/src/bin/tl_mbox.rs | |||
| @@ -6,6 +6,8 @@ | |||
| 6 | #[path = "../common.rs"] | 6 | #[path = "../common.rs"] |
| 7 | mod common; | 7 | mod common; |
| 8 | 8 | ||
| 9 | use core::mem; | ||
| 10 | |||
| 9 | use common::*; | 11 | use common::*; |
| 10 | use embassy_executor::Spawner; | 12 | use embassy_executor::Spawner; |
| 11 | use embassy_futures::poll_once; | 13 | use embassy_futures::poll_once; |
| @@ -36,12 +38,13 @@ async fn main(spawner: Spawner) { | |||
| 36 | let config = Config::default(); | 38 | let config = Config::default(); |
| 37 | let mbox = TlMbox::init(p.IPCC, Irqs, config); | 39 | let mbox = TlMbox::init(p.IPCC, Irqs, config); |
| 38 | 40 | ||
| 39 | let mut rx_buf = [0u8; 500]; | ||
| 40 | let ready_event = Sys::read().await; | 41 | let ready_event = Sys::read().await; |
| 41 | let _ = poll_once(Sys::read()); // clear rx not | 42 | let _ = poll_once(Sys::read()); // clear rx not |
| 42 | ready_event.write(&mut rx_buf).unwrap(); | ||
| 43 | 43 | ||
| 44 | info!("coprocessor ready {}", rx_buf); | 44 | info!("coprocessor ready {}", ready_event.payload()); |
| 45 | |||
| 46 | // test memory manager | ||
| 47 | mem::drop(ready_event); | ||
| 45 | 48 | ||
| 46 | loop { | 49 | loop { |
| 47 | let wireless_fw_info = mbox.wireless_fw_info(); | 50 | let wireless_fw_info = mbox.wireless_fw_info(); |
| @@ -74,11 +77,10 @@ async fn main(spawner: Spawner) { | |||
| 74 | 77 | ||
| 75 | info!("waiting for ble..."); | 78 | info!("waiting for ble..."); |
| 76 | let ble_event = Ble::read().await; | 79 | let ble_event = Ble::read().await; |
| 77 | ble_event.write(&mut rx_buf).unwrap(); | ||
| 78 | 80 | ||
| 79 | info!("ble event: {}", rx_buf); | 81 | info!("ble event: {}", ble_event.payload()); |
| 80 | 82 | ||
| 81 | // Timer::after(Duration::from_secs(3)).await; | 83 | Timer::after(Duration::from_millis(150)).await; |
| 82 | info!("Test OK"); | 84 | info!("Test OK"); |
| 83 | cortex_m::asm::bkpt(); | 85 | cortex_m::asm::bkpt(); |
| 84 | } | 86 | } |
