stm32cubeprogrammer 0.1.0

Rust bindings for the STM32CubeProgrammer API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use crate::error::{CubeProgrammerError, CubeProgrammerResult};
use derive_more::derive::{AsRef, Deref, Display, From, Into};
use num_enum::{FromPrimitive, IntoPrimitive, TryFromPrimitive};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Error codes returned by the CubeProgrammer API
#[derive(Debug, Copy, Clone, strum::Display, IntoPrimitive, FromPrimitive)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[repr(i32)]
pub enum ErrorCode {
    #[num_enum(catch_all)]
    Unknown(i32),

    DeviceNotConnected = -1,
    NoDeviceFound = -2,
    ConnectionError = -3,
    FileNotFound = -4,
    UnsupportedOperation = -5,
    UnsupportedInterface = -6,
    InsufficientMemory = -7,
    UnknownParameters = -8,
    MemoryReadError = -9,
    MemoryWriteError = -10,
    MemoryEraseError = -11,
    UnsupportedFileFormat = -12,
    RefreshRequired = -13,
    SecurityError = -14,
    FrequencyError = -15,
    RdpEnabledError = -16,
    UnknownError = -17,
}

/// CoreRegister of the target
#[derive(Debug, Copy, Clone, strum::Display, IntoPrimitive, FromPrimitive)]
#[repr(u32)]
pub enum CoreRegister {
    R0 = 0,
    R1,
    R2,
    R3,
    R4,
    R5,
    R6,
    R7,
    R8,
    R9,
    R10,
    R11,
    R12,
    SP,
    LR,
    PC,

    #[num_enum(catch_all)]
    Unknown(u32),
}

/// Return code which is mapped to an error if it is not equal to SUCCESS
/// Sometimes success is 0, sometimes it is 1
#[derive(Debug, From, Into)]
pub(crate) struct ReturnCode<const SUCCESS: i32>(pub(crate) i32);

impl<const SUCCESS: i32> ReturnCode<SUCCESS> {
    pub(crate) fn check(&self, action: crate::error::Action) -> CubeProgrammerResult<()> {
        if self.0 == SUCCESS {
            Ok(())
        } else {
            Err(CubeProgrammerError::ActionFailed {
                action,
                return_code: ErrorCode::from(self.0),
            })
        }
    }
}

pub mod probe {
    use super::*;

    #[derive(
        Debug, Default, Clone, Copy, PartialEq, IntoPrimitive, TryFromPrimitive, strum::Display,
    )]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    #[cfg_attr(windows, repr(i32))]
    #[cfg_attr(unix, repr(u32))]
    /// Debug protocol for the target connection
    pub enum Protocol {
        Jtag,
        #[default]
        Swd,
    }

    #[derive(
        Debug, Default, Clone, Copy, PartialEq, IntoPrimitive, TryFromPrimitive, strum::Display,
    )]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    #[cfg_attr(windows, repr(i32))]
    #[cfg_attr(unix, repr(u32))]
    /// Reset mode for the target connection
    pub enum ResetMode {
        Software,
        #[default]
        Hardware,
        Core,
    }

    #[derive(
        Debug, Default, Clone, Copy, PartialEq, IntoPrimitive, TryFromPrimitive, strum::Display,
    )]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    #[cfg_attr(windows, repr(i32))]
    #[cfg_attr(unix, repr(u32))]
    /// Connection mode for the target connection
    pub enum ConnectionMode {
        #[default]
        Normal,
        HotPlug,
        UnderReset,
        PowerDown,
        HardwareResetPulse,
    }

    /// Frequency for the target connection
    #[derive(Debug, Default, Clone, PartialEq)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    pub enum Frequency {
        Low,
        Medium,
        High,
        #[default]
        Highest,

        Custom(u32),
    }

    #[derive(Debug, Clone, PartialEq)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    /// Connection parameters for the target connection
    pub struct ConnectionParameters {
        pub frequency: Frequency,
        pub reset_mode: ResetMode,
        pub connection_mode: ConnectionMode,
    }

    impl Default for ConnectionParameters {
        fn default() -> Self {
            Self {
                frequency: Frequency::Highest,
                reset_mode: ResetMode::Hardware,
                connection_mode: ConnectionMode::Normal,
            }
        }
    }

    #[derive(Debug, Clone, Deref, From, AsRef, Into, Hash, PartialEq, Eq, Display)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    /// The serial of a probe
    pub struct Serial(String);

    impl std::str::FromStr for Serial {
        type Err = CubeProgrammerError;

        fn from_str(s: &str) -> Result<Self, Self::Err> {
            if s.is_empty() {
                return Err(CubeProgrammerError::TypeConversion {
                    message: "Cannot convert empty string to serial".to_string(),
                    source: crate::error::TypeConversionError::NullError,
                });
            }

            Ok(Serial(s.to_string()))
        }
    }

    #[derive(Debug, Clone, Deref)]
    #[repr(transparent)]
    /// Transparent wrapper around the [`stm32cubeprogrammer_sys::debugConnectParameters`]
    pub(crate) struct Probe(pub(crate) stm32cubeprogrammer_sys::debugConnectParameters);

    impl Probe {
        /// Create a modified version of connect parameters
        pub(crate) fn new(
            probe: &Probe,
            protocol: &Protocol,
            connect_parameters: &ConnectionParameters,
        ) -> Self {
            let mut debug_probe = probe.clone();

            debug_probe.set_debug_protocol(*protocol);
            debug_probe.set_reset_mode(connect_parameters.reset_mode);
            debug_probe.set_connection_mode(connect_parameters.connection_mode);
            debug_probe.set_shared(false);

            let frequency = match (&connect_parameters.frequency, debug_probe.debug_port()) {
                (Frequency::Custom(custom_frequency), _) => Some(*custom_frequency),
                (Frequency::Low, Protocol::Jtag) => debug_probe.0.freq.jtagFreq.get(3).copied(),
                (Frequency::Low, Protocol::Swd) => debug_probe.0.freq.swdFreq.get(3).copied(),
                (Frequency::Medium, Protocol::Jtag) => debug_probe.0.freq.jtagFreq.get(2).copied(),
                (Frequency::Medium, Protocol::Swd) => debug_probe.0.freq.swdFreq.get(2).copied(),
                (Frequency::High, Protocol::Jtag) => debug_probe.0.freq.jtagFreq.get(1).copied(),
                (Frequency::High, Protocol::Swd) => debug_probe.0.freq.swdFreq.get(1).copied(),
                (Frequency::Highest, Protocol::Jtag) => {
                    debug_probe.0.freq.jtagFreq.first().copied()
                }
                (Frequency::Highest, Protocol::Swd) => debug_probe.0.freq.swdFreq.first().copied(),
            };

            debug_assert!(frequency.is_some());
            debug_probe.0.frequency = frequency.expect("Cannot get frequency") as i32;

            debug_probe
        }

        pub(crate) fn serial_number(&self) -> &str {
            crate::utility::c_char_slice_to_string(self.0.serialNumber.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
        }

        pub(crate) fn board(&self) -> &str {
            crate::utility::c_char_slice_to_string(self.0.board.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
        }

        pub(crate) fn firmware_version(&self) -> &str {
            crate::utility::c_char_slice_to_string(self.0.firmwareVersion.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
        }

        pub(crate) fn debug_port(&self) -> Protocol {
            Protocol::try_from(self.0.dbgPort).expect("Cannot convert debug port")
        }

        pub(crate) fn connection_mode(&self) -> ConnectionMode {
            ConnectionMode::try_from(self.0.connectionMode).expect("Cannot convert connection mode")
        }

        pub(crate) fn reset_mode(&self) -> ResetMode {
            ResetMode::try_from(self.0.resetMode).expect("Cannot convert reset mode")
        }

        pub(crate) fn shared(&self) -> bool {
            self.0.shared != 0
        }

        pub(crate) fn set_debug_protocol(&mut self, protocol: Protocol) {
            self.0.dbgPort = protocol.into();
        }

        pub(crate) fn set_connection_mode(&mut self, connection_mode: ConnectionMode) {
            self.0.connectionMode = connection_mode.into();
        }

        pub(crate) fn set_reset_mode(&mut self, reset_mode: ResetMode) {
            self.0.resetMode = reset_mode.into();
        }

        pub(crate) fn set_shared(&mut self, shared: bool) {
            self.0.shared = if shared { 1 } else { 0 };
        }
    }

    impl std::fmt::Display for Probe {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(
            f,
            "STLink (Serial: {}), Board: {}, Firmware version: {}, Debug port: {}, Connection mode: {}, Reset mode: {}, Frequency: {} Hz, Shared: {}",
            self.serial_number(),
            self.board(),
            self.firmware_version(),
            self.debug_port(),
            self.connection_mode(),
            self.reset_mode(),
            self.0.frequency,
            self.shared()
        )
        }
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
/// Information about the target device
pub struct GeneralInformation {
    pub device_id: u32,
    pub flash_size: u32,
    pub bootloader_version: u32,
    pub device_type: String,
    pub cpu: String,
    pub name: String,
    pub series: String,
    pub description: String,
    pub revision_id: String,
    pub probe_board: String,
    pub fus_support: bool,
}

impl From<stm32cubeprogrammer_sys::generalInf> for GeneralInformation {
    fn from(value: stm32cubeprogrammer_sys::generalInf) -> Self {
        let name = crate::utility::c_char_slice_to_string(value.name.as_ref())
            .unwrap_or("Unknown")
            .trim_matches('\0')
            .to_string();

        GeneralInformation {
            device_id: value.deviceId as u32,
            flash_size: value.flashSize as u32,
            bootloader_version: value.bootloaderVersion as u32,
            device_type: crate::utility::c_char_slice_to_string(value.type_.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
                .to_string(),
            cpu: crate::utility::c_char_slice_to_string(value.cpu.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
                .to_string(),
            name: name.clone(),
            series: crate::utility::c_char_slice_to_string(value.series.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
                .to_string(),
            description: crate::utility::c_char_slice_to_string(value.description.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
                .to_string(),
            revision_id: crate::utility::c_char_slice_to_string(value.revisionId.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
                .to_string(),
            probe_board: crate::utility::c_char_slice_to_string(value.board.as_ref())
                .unwrap_or("Unknown")
                .trim_matches('\0')
                .to_string(),
            fus_support: crate::utility::target_supports_fus(&name),
        }
    }
}

impl std::fmt::Display for GeneralInformation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Target information (Device ID: {}, Flash size: {}, Bootloader version: {}, Device type: {}, CPU: {}, Name: {}, Series: {}, Description: {}, Revision ID: {}, Board: {})",
            self.device_id,
            self.flash_size,
            self.bootloader_version,
            self.device_type,
            self.cpu,
            self.name,
            self.series,
            self.description,
            self.revision_id,
            self.probe_board
        )
    }
}

pub mod fus {
    use super::*;

    #[derive(Copy, Clone, Debug, Default)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    /// Version of the FUS
    pub struct Version {
        pub major: u8,
        pub minor: u8,
        pub sub: u8,
        pub r#type: Option<u8>,
    }

    impl PartialEq for Version {
        fn eq(&self, other: &Self) -> bool {
            if let Some(r#type) = self.r#type {
                // Compare the type as well
                if let Some(other_type) = other.r#type {
                    self.major == other.major
                        && self.minor == other.minor
                        && self.sub == other.sub
                        && r#type == other_type
                } else {
                    false
                }
            } else {
                // Do not compare the type
                self.major == other.major && self.minor == other.minor && self.sub == other.sub
            }
        }
    }

    impl std::fmt::Display for Version {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            if let Some(r#type) = self.r#type {
                write!(f, "{}.{}.{}.{}", self.major, self.minor, self.sub, r#type)
            } else {
                write!(f, "{}.{}.{}", self.major, self.minor, self.sub)
            }
        }
    }

    impl std::str::FromStr for Version {
        type Err = CubeProgrammerError;

        fn from_str(s: &str) -> Result<Self, Self::Err> {
            let parts = s.split('.');

            if parts.clone().count() == 3 {
                if let Ok(converted) = parts
                    .map(|x| x.parse::<u8>())
                    .collect::<Result<Vec<u8>, _>>()
                {
                    return Ok(Version {
                        major: converted[0],
                        minor: converted[1],
                        sub: converted[2],
                        r#type: None,
                    });
                }
            } else if parts.clone().count() == 4 {
                if let Ok(converted) = parts
                    .map(|x| x.parse::<u8>())
                    .collect::<Result<Vec<u8>, _>>()
                {
                    return Ok(Version {
                        major: converted[0],
                        minor: converted[1],
                        sub: converted[2],
                        r#type: Some(converted[3]),
                    });
                }
            }

            Err(CubeProgrammerError::TypeConversion {
                message: format!("Cannot convert \"{}\" to a version. Expecting the following format \"u8.u8.u8\" e.g. \"1.2.3\"", s),
                source:  crate::error::TypeConversionError::VersionError
            })
        }
    }

    #[derive(Copy, Clone, Debug, Default)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
    /// Information about the FUS. This is read from the target after a successful connection to the FUS
    pub struct Information {
        pub wireless_stack_version: Version,
        pub fus_version: Version,
        pub uid64: u64,
        pub device_id: u16,
    }

    impl std::fmt::Display for Information {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(
                f,
                "Wireless stack version: {}, FUS version: {}, UUID64: {:X}, Device ID: {:X}",
                self.wireless_stack_version, self.fus_version, self.uid64, self.device_id
            )
        }
    }
}