aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-06-18 08:37:26 -0500
committerxoviat <[email protected]>2023-06-18 08:37:26 -0500
commit7177e7ea1aae2eb6973816a158e714b4fcd5b9a6 (patch)
treee295ca183b78d18a1ec414cd8c0cf977fc83cee8
parentadaed307b4d81ed09611e496524b1d96ad04c60d (diff)
stm32/wpan: cleanup and expand shci
-rw-r--r--embassy-stm32-wpan/src/evt.rs65
-rw-r--r--embassy-stm32-wpan/src/shci.rs348
-rw-r--r--embassy-stm32-wpan/src/sys.rs35
-rw-r--r--tests/stm32/src/bin/tl_mbox.rs8
4 files changed, 349 insertions, 107 deletions
diff --git a/embassy-stm32-wpan/src/evt.rs b/embassy-stm32-wpan/src/evt.rs
index 3a9d03576..a7b99e540 100644
--- a/embassy-stm32-wpan/src/evt.rs
+++ b/embassy-stm32-wpan/src/evt.rs
@@ -62,6 +62,7 @@ pub struct EvtSerial {
62} 62}
63 63
64#[derive(Copy, Clone, Default)] 64#[derive(Copy, Clone, Default)]
65#[repr(C, packed)]
65pub struct EvtStub { 66pub struct EvtStub {
66 pub kind: u8, 67 pub kind: u8,
67 pub evt_code: u8, 68 pub evt_code: u8,
@@ -114,7 +115,7 @@ impl EvtBox {
114 } 115 }
115 } 116 }
116 117
117 pub fn payload<'a>(&self) -> &'a [u8] { 118 pub fn payload<'a>(&'a self) -> &'a [u8] {
118 unsafe { 119 unsafe {
119 let p_payload_len = &(*self.ptr).evt_serial.evt.payload_len as *const u8; 120 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; 121 let p_payload = &(*self.ptr).evt_serial.evt.payload as *const u8;
@@ -124,72 +125,10 @@ impl EvtBox {
124 slice::from_raw_parts(p_payload, payload_len as usize) 125 slice::from_raw_parts(p_payload, payload_len as usize)
125 } 126 }
126 } 127 }
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 // }
187} 128}
188 129
189impl Drop for EvtBox { 130impl Drop for EvtBox {
190 fn drop(&mut self) { 131 fn drop(&mut self) {
191 trace!("evt box drop packet");
192
193 unsafe { mm::MemoryManager::drop_event_packet(self.ptr) }; 132 unsafe { mm::MemoryManager::drop_event_packet(self.ptr) };
194 } 133 }
195} 134}
diff --git a/embassy-stm32-wpan/src/shci.rs b/embassy-stm32-wpan/src/shci.rs
index cdf027d5e..f4e9ba84c 100644
--- a/embassy-stm32-wpan/src/shci.rs
+++ b/embassy-stm32-wpan/src/shci.rs
@@ -2,38 +2,345 @@ use core::{mem, slice};
2 2
3use super::{TL_CS_EVT_SIZE, TL_EVT_HEADER_SIZE, TL_PACKET_HEADER_SIZE}; 3use super::{TL_CS_EVT_SIZE, TL_EVT_HEADER_SIZE, TL_PACKET_HEADER_SIZE};
4 4
5pub const SCHI_OPCODE_BLE_INIT: u16 = 0xfc66; 5const SHCI_OGF: u16 = 0x3F;
6 6
7#[derive(Debug, Clone, Copy)] 7const fn opcode(ogf: u16, ocf: u16) -> isize {
8 ((ogf << 10) + ocf) as isize
9}
10
11#[allow(dead_code)]
12#[cfg_attr(feature = "defmt", derive(defmt::Format))]
13pub enum SchiCommandStatus {
14 ShciSuccess = 0x00,
15 ShciUnknownCmd = 0x01,
16 ShciMemoryCapacityExceededErrCode = 0x07,
17 ShciErrUnsupportedFeature = 0x11,
18 ShciErrInvalidHciCmdParams = 0x12,
19 ShciErrInvalidParams = 0x42, /* only used for release < v1.13.0 */
20 ShciErrInvalidParamsV2 = 0x92, /* available for release >= v1.13.0 */
21 ShciFusCmdNotSupported = 0xFF,
22}
23
24impl TryFrom<u8> for SchiCommandStatus {
25 type Error = ();
26
27 fn try_from(v: u8) -> Result<Self, Self::Error> {
28 match v {
29 x if x == SchiCommandStatus::ShciSuccess as u8 => Ok(SchiCommandStatus::ShciSuccess),
30 x if x == SchiCommandStatus::ShciUnknownCmd as u8 => Ok(SchiCommandStatus::ShciUnknownCmd),
31 x if x == SchiCommandStatus::ShciMemoryCapacityExceededErrCode as u8 => {
32 Ok(SchiCommandStatus::ShciMemoryCapacityExceededErrCode)
33 }
34 x if x == SchiCommandStatus::ShciErrUnsupportedFeature as u8 => {
35 Ok(SchiCommandStatus::ShciErrUnsupportedFeature)
36 }
37 x if x == SchiCommandStatus::ShciErrInvalidHciCmdParams as u8 => {
38 Ok(SchiCommandStatus::ShciErrInvalidHciCmdParams)
39 }
40 x if x == SchiCommandStatus::ShciErrInvalidParams as u8 => Ok(SchiCommandStatus::ShciErrInvalidParams), /* only used for release < v1.13.0 */
41 x if x == SchiCommandStatus::ShciErrInvalidParamsV2 as u8 => Ok(SchiCommandStatus::ShciErrInvalidParamsV2), /* available for release >= v1.13.0 */
42 x if x == SchiCommandStatus::ShciFusCmdNotSupported as u8 => Ok(SchiCommandStatus::ShciFusCmdNotSupported),
43 _ => Err(()),
44 }
45 }
46}
47
48#[allow(dead_code)]
49#[cfg_attr(feature = "defmt", derive(defmt::Format))]
50pub enum ShciOpcode {
51 // 0x50 reserved
52 // 0x51 reserved
53 FusGetState = opcode(SHCI_OGF, 0x52),
54 // 0x53 reserved
55 FusFirmwareUpgrade = opcode(SHCI_OGF, 0x54),
56 FusFirmwareDelete = opcode(SHCI_OGF, 0x55),
57 FusUpdateAuthKey = opcode(SHCI_OGF, 0x56),
58 FusLockAuthKey = opcode(SHCI_OGF, 0x57),
59 FusStoreUserKey = opcode(SHCI_OGF, 0x58),
60 FusLoadUserKey = opcode(SHCI_OGF, 0x59),
61 FusStartWirelessStack = opcode(SHCI_OGF, 0x5a),
62 // 0x5b reserved
63 // 0x5c reserved
64 FusLockUserKey = opcode(SHCI_OGF, 0x5d),
65 FusUnloadUserKey = opcode(SHCI_OGF, 0x5e),
66 FusActivateAntirollback = opcode(SHCI_OGF, 0x5f),
67 // 0x60 reserved
68 // 0x61 reserved
69 // 0x62 reserved
70 // 0x63 reserved
71 // 0x64 reserved
72 // 0x65 reserved
73 BleInit = opcode(SHCI_OGF, 0x66),
74 ThreadInit = opcode(SHCI_OGF, 0x67),
75 DebugInit = opcode(SHCI_OGF, 0x68),
76 FlashEraseActivity = opcode(SHCI_OGF, 0x69),
77 ConcurrentSetMode = opcode(SHCI_OGF, 0x6a),
78 FlashStoreData = opcode(SHCI_OGF, 0x6b),
79 FlashEraseData = opcode(SHCI_OGF, 0x6c),
80 RadioAllowLowPower = opcode(SHCI_OGF, 0x6d),
81 Mac802_15_4Init = opcode(SHCI_OGF, 0x6e),
82 ReInit = opcode(SHCI_OGF, 0x6f),
83 ZigbeeInit = opcode(SHCI_OGF, 0x70),
84 LldTestsInit = opcode(SHCI_OGF, 0x71),
85 ExtraConfig = opcode(SHCI_OGF, 0x72),
86 SetFlashActivityControl = opcode(SHCI_OGF, 0x73),
87 BleLldInit = opcode(SHCI_OGF, 0x74),
88 Config = opcode(SHCI_OGF, 0x75),
89 ConcurrentGetNextBleEvtTime = opcode(SHCI_OGF, 0x76),
90 ConcurrentEnableNext802_15_4EvtNotification = opcode(SHCI_OGF, 0x77),
91 Mac802_15_4DeInit = opcode(SHCI_OGF, 0x78),
92}
93
94pub const SHCI_C2_CONFIG_EVTMASK1_BIT0_ERROR_NOTIF_ENABLE: u8 = 1 << 0;
95pub const SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE: u8 = 1 << 1;
96pub const SHCI_C2_CONFIG_EVTMASK1_BIT2_THREAD_NVM_RAM_UPDATE_ENABLE: u8 = 1 << 2;
97pub const SHCI_C2_CONFIG_EVTMASK1_BIT3_NVM_START_WRITE_ENABLE: u8 = 1 << 3;
98pub const SHCI_C2_CONFIG_EVTMASK1_BIT4_NVM_END_WRITE_ENABLE: u8 = 1 << 4;
99pub const SHCI_C2_CONFIG_EVTMASK1_BIT5_NVM_START_ERASE_ENABLE: u8 = 1 << 5;
100pub const SHCI_C2_CONFIG_EVTMASK1_BIT6_NVM_END_ERASE_ENABLE: u8 = 1 << 6;
101
102#[derive(Clone, Copy)]
103#[repr(C, packed)]
104pub struct ShciConfigParam {
105 pub payload_cmd_size: u8,
106 pub config: u8,
107 pub event_mask: u8,
108 pub spare: u8,
109 pub ble_nvm_ram_address: u32,
110 pub thread_nvm_ram_address: u32,
111 pub revision_id: u16,
112 pub device_id: u16,
113}
114
115impl ShciConfigParam {
116 pub fn payload<'a>(&self) -> &'a [u8] {
117 unsafe { slice::from_raw_parts(self as *const _ as *const u8, mem::size_of::<Self>()) }
118 }
119}
120
121impl Default for ShciConfigParam {
122 fn default() -> Self {
123 Self {
124 payload_cmd_size: (mem::size_of::<Self>() - 1) as u8,
125 config: 0,
126 event_mask: SHCI_C2_CONFIG_EVTMASK1_BIT0_ERROR_NOTIF_ENABLE
127 + SHCI_C2_CONFIG_EVTMASK1_BIT1_BLE_NVM_RAM_UPDATE_ENABLE
128 + SHCI_C2_CONFIG_EVTMASK1_BIT2_THREAD_NVM_RAM_UPDATE_ENABLE
129 + SHCI_C2_CONFIG_EVTMASK1_BIT3_NVM_START_WRITE_ENABLE
130 + SHCI_C2_CONFIG_EVTMASK1_BIT4_NVM_END_WRITE_ENABLE
131 + SHCI_C2_CONFIG_EVTMASK1_BIT5_NVM_START_ERASE_ENABLE
132 + SHCI_C2_CONFIG_EVTMASK1_BIT6_NVM_END_ERASE_ENABLE,
133 spare: 0,
134 ble_nvm_ram_address: 0,
135 thread_nvm_ram_address: 0,
136 revision_id: 0,
137 device_id: 0,
138 }
139 }
140}
141
142#[derive(Clone, Copy)]
8#[repr(C, packed)] 143#[repr(C, packed)]
9pub struct ShciBleInitCmdParam { 144pub struct ShciBleInitCmdParam {
10 /// NOT USED CURRENTLY 145 /// NOT USED - shall be set to 0
11 pub p_ble_buffer_address: u32, 146 pub p_ble_buffer_address: u32,
12 147 /// NOT USED - shall be set to 0
13 /// Size of the Buffer allocated in pBleBufferAddress
14 pub ble_buffer_size: u32, 148 pub ble_buffer_size: u32,
15 149 /// Maximum number of attribute records related to all the required characteristics (excluding the services)
150 /// that can be stored in the GATT database, for the specific BLE user application.
151 /// For each characteristic, the number of attribute records goes from two to five depending on the characteristic properties:
152 /// - minimum of two (one for declaration and one for the value)
153 /// - add one more record for each additional property: notify or indicate, broadcast, extended property.
154 /// The total calculated value must be increased by 9, due to the records related to the standard attribute profile and
155 /// GAP service characteristics, and automatically added when initializing GATT and GAP layers
156 /// - Min value: <number of user attributes> + 9
157 /// - Max value: depending on the GATT database defined by user application
16 pub num_attr_record: u16, 158 pub num_attr_record: u16,
159 /// Defines the maximum number of services that can be stored in the GATT database. Note that the GAP and GATT services
160 /// are automatically added at initialization so this parameter must be the number of user services increased by two.
161 /// - Min value: <number of user service> + 2
162 /// - Max value: depending GATT database defined by user application
17 pub num_attr_serv: u16, 163 pub num_attr_serv: u16,
164 /// NOTE: This parameter is ignored by the CPU2 when the parameter "Options" is set to "LL_only" ( see Options description in that structure )
165 ///
166 /// Size of the storage area for the attribute values.
167 /// Each characteristic contributes to the attrValueArrSize value as follows:
168 /// - Characteristic value length plus:
169 /// + 5 bytes if characteristic UUID is 16 bits
170 /// + 19 bytes if characteristic UUID is 128 bits
171 /// + 2 bytes if characteristic has a server configuration descriptor
172 /// + 2 bytes * NumOfLinks if the characteristic has a client configuration descriptor
173 /// + 2 bytes if the characteristic has extended properties
174 /// Each descriptor contributes to the attrValueArrSize value as follows:
175 /// - Descriptor length
18 pub attr_value_arr_size: u16, 176 pub attr_value_arr_size: u16,
177 /// Maximum number of BLE links supported
178 /// - Min value: 1
179 /// - Max value: 8
19 pub num_of_links: u8, 180 pub num_of_links: u8,
181 /// Disable/enable the extended packet length BLE 5.0 feature
182 /// - Disable: 0
183 /// - Enable: 1
20 pub extended_packet_length_enable: u8, 184 pub extended_packet_length_enable: u8,
21 pub pr_write_list_size: u8, 185 /// NOTE: This parameter is ignored by the CPU2 when the parameter "Options" is set to "LL_only" ( see Options description in that structure )
22 pub mb_lock_count: u8, 186 ///
23 187 /// Maximum number of supported "prepare write request"
188 /// - Min value: given by the macro DEFAULT_PREP_WRITE_LIST_SIZE
189 /// - Max value: a value higher than the minimum required can be specified, but it is not recommended
190 pub prepare_write_list_size: u8,
191 /// NOTE: This parameter is overwritten by the CPU2 with an hardcoded optimal value when the parameter "Options" is set to "LL_only"
192 /// ( see Options description in that structure )
193 ///
194 /// Number of allocated memory blocks for the BLE stack
195 /// - Min value: given by the macro MBLOCKS_CALC
196 /// - Max value: a higher value can improve data throughput performance, but uses more memory
197 pub block_count: u8,
198 /// NOTE: This parameter is ignored by the CPU2 when the parameter "Options" is set to "LL_only" ( see Options description in that structure )
199 ///
200 /// Maximum ATT MTU size supported
201 /// - Min value: 23
202 /// - Max value: 512
24 pub att_mtu: u16, 203 pub att_mtu: u16,
204 /// The sleep clock accuracy (ppm value) that used in BLE connected slave mode to calculate the window widening
205 /// (in combination with the sleep clock accuracy sent by master in CONNECT_REQ PDU),
206 /// refer to BLE 5.0 specifications - Vol 6 - Part B - chap 4.5.7 and 4.2.2
207 /// - Min value: 0
208 /// - Max value: 500 (worst possible admitted by specification)
25 pub slave_sca: u16, 209 pub slave_sca: u16,
210 /// The sleep clock accuracy handled in master mode. It is used to determine the connection and advertising events timing.
211 /// It is transmitted to the slave in CONNEC_REQ PDU used by the slave to calculate the window widening,
212 /// see SlaveSca and Bluetooth Core Specification v5.0 Vol 6 - Part B - chap 4.5.7 and 4.2.2
213 /// Possible values:
214 /// - 251 ppm to 500 ppm: 0
215 /// - 151 ppm to 250 ppm: 1
216 /// - 101 ppm to 150 ppm: 2
217 /// - 76 ppm to 100 ppm: 3
218 /// - 51 ppm to 75 ppm: 4
219 /// - 31 ppm to 50 ppm: 5
220 /// - 21 ppm to 30 ppm: 6
221 /// - 0 ppm to 20 ppm: 7
26 pub master_sca: u8, 222 pub master_sca: u8,
223 /// Some information for Low speed clock mapped in bits field
224 /// - bit 0:
225 /// - 1: Calibration for the RF system wakeup clock source
226 /// - 0: No calibration for the RF system wakeup clock source
227 /// - bit 1:
228 /// - 1: STM32W5M Module device
229 /// - 0: Other devices as STM32WBxx SOC, STM32WB1M module
230 /// - bit 2:
231 /// - 1: HSE/1024 Clock config
232 /// - 0: LSE Clock config
27 pub ls_source: u8, 233 pub ls_source: u8,
234 /// This parameter determines the maximum duration of a slave connection event. When this duration is reached the slave closes
235 /// the current connections event (whatever is the CE_length parameter specified by the master in HCI_CREATE_CONNECTION HCI command),
236 /// expressed in units of 625/256 µs (~2.44 µs)
237 /// - Min value: 0 (if 0 is specified, the master and slave perform only a single TX-RX exchange per connection event)
238 /// - Max value: 1638400 (4000 ms). A higher value can be specified (max 0xFFFFFFFF) but results in a maximum connection time
239 /// of 4000 ms as specified. In this case the parameter is not applied, and the predicted CE length calculated on slave is not shortened
28 pub max_conn_event_length: u32, 240 pub max_conn_event_length: u32,
241 /// Startup time of the high speed (16 or 32 MHz) crystal oscillator in units of 625/256 µs (~2.44 µs).
242 /// - Min value: 0
243 /// - Max value: 820 (~2 ms). A higher value can be specified, but the value that implemented in stack is forced to ~2 ms
29 pub hs_startup_time: u16, 244 pub hs_startup_time: u16,
245 /// Viterbi implementation in BLE LL reception.
246 /// - 0: Enable
247 /// - 1: Disable
30 pub viterbi_enable: u8, 248 pub viterbi_enable: u8,
31 pub ll_only: u8, 249 /// - bit 0:
250 /// - 1: LL only
251 /// - 0: LL + host
252 /// - bit 1:
253 /// - 1: no service change desc.
254 /// - 0: with service change desc.
255 /// - bit 2:
256 /// - 1: device name Read-Only
257 /// - 0: device name R/W
258 /// - bit 3:
259 /// - 1: extended advertizing supported
260 /// - 0: extended advertizing not supported
261 /// - bit 4:
262 /// - 1: CS Algo #2 supported
263 /// - 0: CS Algo #2 not supported
264 /// - bit 5:
265 /// - 1: Reduced GATT database in NVM
266 /// - 0: Full GATT database in NVM
267 /// - bit 6:
268 /// - 1: GATT caching is used
269 /// - 0: GATT caching is not used
270 /// - bit 7:
271 /// - 1: LE Power Class 1
272 /// - 0: LE Power Classe 2-3
273 /// - other bits: complete with Options_extension flag
274 pub options: u8,
275 /// Reserved for future use - shall be set to 0
32 pub hw_version: u8, 276 pub hw_version: u8,
277 // /**
278 // * Maximum number of connection-oriented channels in initiator mode.
279 // * Range: 0 .. 64
280 // */
281 // pub max_coc_initiator_nbr: u8,
282 //
283 // /**
284 // * Minimum transmit power in dBm supported by the Controller.
285 // * Range: -127 .. 20
286 // */
287 // pub min_tx_power: i8,
288 //
289 // /**
290 // * Maximum transmit power in dBm supported by the Controller.
291 // * Range: -127 .. 20
292 // */
293 // pub max_tx_power: i8,
294 //
295 // /**
296 // * RX model configuration
297 // * - bit 0: 1: agc_rssi model improved vs RF blockers 0: Legacy agc_rssi model
298 // * - other bits: reserved ( shall be set to 0)
299 // */
300 // pub rx_model_config: u8,
301 //
302 // /* Maximum number of advertising sets.
303 // * Range: 1 .. 8 with limitation:
304 // * This parameter is linked to max_adv_data_len such as both compliant with allocated Total memory computed with BLE_EXT_ADV_BUFFER_SIZE based
305 // * on Max Extended advertising configuration supported.
306 // * This parameter is considered by the CPU2 when Options has SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV flag set
307 // */
308 // pub max_adv_set_nbr: u8,
309 //
310 // /* Maximum advertising data length (in bytes)
311 // * Range: 31 .. 1650 with limitation:
312 // * This parameter is linked to max_adv_set_nbr such as both compliant with allocated Total memory computed with BLE_EXT_ADV_BUFFER_SIZE based
313 // * on Max Extended advertising configuration supported.
314 // * This parameter is considered by the CPU2 when Options has SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV flag set
315 // */
316 // pub max_adv_data_len: u16,
317 //
318 // /* RF TX Path Compensation Value (16-bit signed integer). Units: 0.1 dB.
319 // * Range: -1280 .. 1280
320 // */
321 // pub tx_path_compens: i16,
322 //
323 // /* RF RX Path Compensation Value (16-bit signed integer). Units: 0.1 dB.
324 // * Range: -1280 .. 1280
325 // */
326 // pub rx_path_compens: i16,
327 //
328 // /* BLE core specification version (8-bit unsigned integer).
329 // * values as: 11(5.2), 12(5.3)
330 // */
331 // pub ble_core_version: u8,
332 //
333 // /**
334 // * Options flags extension
335 // * - bit 0: 1: appearance Writable 0: appearance Read-Only
336 // * - bit 1: 1: Enhanced ATT supported 0: Enhanced ATT not supported
337 // * - other bits: reserved ( shall be set to 0)
338 // */
339 // pub options_extension: u8,
33} 340}
34 341
35impl ShciBleInitCmdParam { 342impl ShciBleInitCmdParam {
36 pub fn payload<'a>(&self) -> &'a [u8] { 343 pub fn payload<'a>(&'a self) -> &'a [u8] {
37 unsafe { slice::from_raw_parts(self as *const _ as *const u8, mem::size_of::<Self>()) } 344 unsafe { slice::from_raw_parts(self as *const _ as *const u8, mem::size_of::<Self>()) }
38 } 345 }
39} 346}
@@ -48,8 +355,8 @@ impl Default for ShciBleInitCmdParam {
48 attr_value_arr_size: 1344, 355 attr_value_arr_size: 1344,
49 num_of_links: 2, 356 num_of_links: 2,
50 extended_packet_length_enable: 1, 357 extended_packet_length_enable: 1,
51 pr_write_list_size: 0x3A, 358 prepare_write_list_size: 0x3A,
52 mb_lock_count: 0x79, 359 block_count: 0x79,
53 att_mtu: 156, 360 att_mtu: 156,
54 slave_sca: 500, 361 slave_sca: 500,
55 master_sca: 0, 362 master_sca: 0,
@@ -57,25 +364,12 @@ impl Default for ShciBleInitCmdParam {
57 max_conn_event_length: 0xFFFFFFFF, 364 max_conn_event_length: 0xFFFFFFFF,
58 hs_startup_time: 0x148, 365 hs_startup_time: 0x148,
59 viterbi_enable: 1, 366 viterbi_enable: 1,
60 ll_only: 0, 367 options: 0,
61 hw_version: 0, 368 hw_version: 0,
62 } 369 }
63 } 370 }
64} 371}
65 372
66#[derive(Debug, Clone, Copy, Default)]
67#[repr(C, packed)]
68pub struct ShciHeader {
69 metadata: [u32; 3],
70}
71
72#[derive(Debug, Clone, Copy)]
73#[repr(C, packed)]
74pub struct ShciBleInitCmdPacket {
75 pub header: ShciHeader,
76 pub param: ShciBleInitCmdParam,
77}
78
79pub const TL_BLE_EVT_CS_PACKET_SIZE: usize = TL_EVT_HEADER_SIZE + TL_CS_EVT_SIZE; 373pub const TL_BLE_EVT_CS_PACKET_SIZE: usize = TL_EVT_HEADER_SIZE + TL_CS_EVT_SIZE;
80#[allow(dead_code)] // Not used currently but reserved 374#[allow(dead_code)] // Not used currently but reserved
81const TL_BLE_EVT_CS_BUFFER_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_BLE_EVT_CS_PACKET_SIZE; 375const TL_BLE_EVT_CS_BUFFER_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_BLE_EVT_CS_PACKET_SIZE;
diff --git a/embassy-stm32-wpan/src/sys.rs b/embassy-stm32-wpan/src/sys.rs
index a185cd4f1..5fe09a20f 100644
--- a/embassy-stm32-wpan/src/sys.rs
+++ b/embassy-stm32-wpan/src/sys.rs
@@ -1,9 +1,10 @@
1use core::marker::PhantomData; 1use core::marker::PhantomData;
2use core::ptr;
2 3
3use crate::cmd::CmdPacket; 4use crate::cmd::CmdPacket;
4use crate::consts::TlPacketType; 5use crate::consts::TlPacketType;
5use crate::evt::EvtBox; 6use crate::evt::{CcEvt, EvtBox, EvtPacket};
6use crate::shci::{ShciBleInitCmdParam, SCHI_OPCODE_BLE_INIT}; 7use crate::shci::{SchiCommandStatus, ShciBleInitCmdParam, ShciOpcode};
7use crate::tables::{SysTable, WirelessFwInfoTable}; 8use crate::tables::{SysTable, WirelessFwInfoTable};
8use crate::unsafe_linked_list::LinkedListNode; 9use crate::unsafe_linked_list::LinkedListNode;
9use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE}; 10use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE};
@@ -39,21 +40,29 @@ impl Sys {
39 } 40 }
40 } 41 }
41 42
42 pub fn write(&self, opcode: u16, payload: &[u8]) { 43 pub async fn write(&self, opcode: ShciOpcode, payload: &[u8]) {
43 unsafe { 44 Ipcc::send(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL, || unsafe {
44 CmdPacket::write_into(SYS_CMD_BUF.as_mut_ptr(), TlPacketType::SysCmd, opcode, payload); 45 CmdPacket::write_into(SYS_CMD_BUF.as_mut_ptr(), TlPacketType::SysCmd, opcode as u16, payload);
45 }
46 }
47
48 pub async fn shci_c2_ble_init(&self, param: ShciBleInitCmdParam) {
49 debug!("sending SHCI");
50
51 Ipcc::send(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL, || {
52 self.write(SCHI_OPCODE_BLE_INIT, param.payload());
53 }) 46 })
54 .await; 47 .await;
48 }
55 49
50 /// `HW_IPCC_SYS_CmdEvtNot`
51 pub async fn write_and_get_response(&self, opcode: ShciOpcode, payload: &[u8]) -> SchiCommandStatus {
52 self.write(opcode, payload).await;
56 Ipcc::flush(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL).await; 53 Ipcc::flush(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL).await;
54
55 unsafe {
56 let p_event_packet = SYS_CMD_BUF.as_ptr() as *const EvtPacket;
57 let p_command_event = &((*p_event_packet).evt_serial.evt.payload) as *const _ as *const CcEvt;
58 let p_payload = &((*p_command_event).payload) as *const u8;
59
60 ptr::read_volatile(p_payload).try_into().unwrap()
61 }
62 }
63
64 pub async fn shci_c2_ble_init(&self, param: ShciBleInitCmdParam) -> SchiCommandStatus {
65 self.write_and_get_response(ShciOpcode::BleInit, param.payload()).await
57 } 66 }
58 67
59 /// `HW_IPCC_SYS_EvtNot` 68 /// `HW_IPCC_SYS_EvtNot`
diff --git a/tests/stm32/src/bin/tl_mbox.rs b/tests/stm32/src/bin/tl_mbox.rs
index f6641ae31..f47a89b6f 100644
--- a/tests/stm32/src/bin/tl_mbox.rs
+++ b/tests/stm32/src/bin/tl_mbox.rs
@@ -39,7 +39,7 @@ async fn main(spawner: Spawner) {
39 let ready_event = mbox.sys_subsystem.read().await; 39 let ready_event = mbox.sys_subsystem.read().await;
40 let _ = poll_once(mbox.sys_subsystem.read()); // clear rx not 40 let _ = poll_once(mbox.sys_subsystem.read()); // clear rx not
41 41
42 info!("coprocessor ready {}", ready_event.payload()); 42 info!("sys event {:x} : {:x}", ready_event.stub().kind, ready_event.payload());
43 43
44 // test memory manager 44 // test memory manager
45 mem::drop(ready_event); 45 mem::drop(ready_event);
@@ -59,7 +59,8 @@ async fn main(spawner: Spawner) {
59 59
60 Timer::after(Duration::from_millis(50)).await; 60 Timer::after(Duration::from_millis(50)).await;
61 61
62 mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await; 62 let result = mbox.sys_subsystem.shci_c2_ble_init(Default::default()).await;
63 info!("subsystem initialization: {}", result);
63 64
64 info!("starting ble..."); 65 info!("starting ble...");
65 mbox.ble_subsystem.write(0x0c, &[]).await; 66 mbox.ble_subsystem.write(0x0c, &[]).await;
@@ -67,9 +68,8 @@ async fn main(spawner: Spawner) {
67 info!("waiting for ble..."); 68 info!("waiting for ble...");
68 let ble_event = mbox.ble_subsystem.read().await; 69 let ble_event = mbox.ble_subsystem.read().await;
69 70
70 info!("ble event: {}", ble_event.payload()); 71 info!("ble event {:x} : {:x}", ble_event.stub().kind, ble_event.payload());
71 72
72 Timer::after(Duration::from_millis(150)).await;
73 info!("Test OK"); 73 info!("Test OK");
74 cortex_m::asm::bkpt(); 74 cortex_m::asm::bkpt();
75} 75}