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