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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
//!
//! Rust bindings for the STM32CubeProgrammer API library.
//!
//! # Compatibility
//!
//! This crate is compatible with both Linux and Windows.
//!
//! # Requirements
//! This crate requires STM32CubeProgrammer (version 2.14.0 or later) to be installed.
//!
//! # Example
//!
//! The code below will discover connected STLinks on Linux, retrieve information, read and write memory, and finally program the attached device.
//!
//! ```
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Generate path to STM32CubeProgrammer folder
//!     let home_dir = std::env::var_os("HOME")
//!         .map(std::path::PathBuf::from)
//!         .expect("Failed to get home directory, $HOME variable missing");
//!     let binding = home_dir.join("Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer");
//!     let stm32prog_path = binding
//!         .to_str()
//!         .expect("Failed to join STM32CubeProgrammer path");
//!     
//!     // Load STM32CubeProgmmer API library
//!     let stm32prog = stm32cubeprog_rs::STM32CubeProg::new(stm32prog_path)?;
//!
//!     // Find connected STLinks
//!     let mut stlinks = stm32prog.discover()?;
//!     for stlink in stlinks.iter_mut() {
//!         println!("{stlink}");
//!         
//!         // Configure the reset mode and the connection mode
//!         stlink.reset_mode(stm32cubeprog_rs::DebugResetMode::HardwareReset);
//!         stlink.connection_mode(stm32cubeprog_rs::DebugConnectMode::UnderReset);
//!
//!         // Connect the STlink
//!         stm32prog.connect(stlink)?;
//!         
//!         // Fetch device information
//!         let device_info = stm32prog.device_info()?;
//!         println!("{device_info}");
//!         
//!         // Read and write register R0
//!         stm32prog.write_core_register(stm32cubeprog_rs::Register::R0, 0xAABBCCDD)?;
//!         let data = stm32prog.read_core_register(stm32cubeprog_rs::Register::R0)?;
//!         println!("R0:  0x{data:X}");
//!         
//!         // Read and write memory
//!         let data = stm32prog.read_memory8(0x1FFF7590, 16)?;
//!         println!("0x1FFF7590: {data:x?}");
//!
//!         stm32prog.write_memory8(0x20000100, data)?;
//!
//!         let data = stm32prog.read_memory32(0x1FFF7590, 4)?;
//!         println!("0x1FFF7590: {data:x?}");
//!
//!         stm32prog.write_memory32(0x20000200, data)?;
//!         
//!         // Mass erase the device
//!         stm32prog.mass_erase()?;
//!
//!         // Flash the device
//!         stm32prog.download("demo.hex", None, None, None)?;
//!         
//!         // Reset and disconnect the STLink
//!         stm32prog.reset(stlink)?;
//!         stm32prog.disconnect();
//!     }
//!
//!     Ok(())
//! }
//! ```

pub mod err;

#[cfg(unix)]
#[allow(non_camel_case_types)]
pub type wchar = u32;
#[cfg(windows)]
#[allow(non_camel_case_types)]
pub type wchar = u16;

#[repr(C)]
pub struct DisplayCallbacks {
    pub init_progress_bar: extern "C" fn(),
    pub log_message: extern "C" fn(msg_type: std::os::raw::c_int, msg: *const std::os::raw::c_int),
    pub load_bar: extern "C" fn(current: std::os::raw::c_int, total: std::os::raw::c_int),
}

extern "C" fn init_progress_bar() {}

extern "C" fn log_message(_msg_type: std::os::raw::c_int, _msg: *const std::os::raw::c_int) {}

extern "C" fn load_bar(_current: std::os::raw::c_int, _total: std::os::raw::c_int) {}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum Verbosity {
    Level0 = 0,
    Level1 = 1,
    Level2 = 2,
    Level3 = 3,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum DebugPort {
    Jtag = 0,
    Swd = 1,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum DebugConnectMode {
    Normal = 0,
    HotPlug = 1,
    UnderReset = 2,
    PowerDown = 3,
    PreReset = 4,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum DebugResetMode {
    SoftwareReset = 0,
    HardwareReset = 1,
    CoreReset = 2,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Frequencies {
    pub jtag_freq: [std::os::raw::c_uint; 12usize],
    pub jtag_freq_number: std::os::raw::c_uint,
    pub swd_freq: [std::os::raw::c_uint; 12usize],
    pub swd_freq_number: std::os::raw::c_uint,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DeviceGeneralInfo {
    pub device_id: ::std::os::raw::c_ushort,
    pub flash_size: ::std::os::raw::c_int,
    pub bootloader_version: ::std::os::raw::c_int,
    pub category: [::std::os::raw::c_char; 4usize],
    pub cpu: [::std::os::raw::c_char; 20usize],
    pub name: [::std::os::raw::c_char; 100usize],
    pub series: [::std::os::raw::c_char; 100usize],
    pub description: [::std::os::raw::c_char; 150usize],
    pub revision_id: [::std::os::raw::c_char; 8usize],
    pub board: [::std::os::raw::c_char; 100usize],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DebugConnectParameters {
    pub dbg_port: DebugPort,
    pub index: std::os::raw::c_int,
    pub serial_number: [std::os::raw::c_char; 33usize],
    pub firmware_version: [std::os::raw::c_char; 20usize],
    pub target_voltage: [std::os::raw::c_char; 5usize],
    pub access_port_number: std::os::raw::c_int,
    pub access_port: std::os::raw::c_int,
    pub connection_mode: DebugConnectMode,
    pub reset_mode: DebugResetMode,
    pub is_old_firmware: std::os::raw::c_int,
    pub freq: Frequencies,
    pub frequency: std::os::raw::c_int,
    pub is_bridge: std::os::raw::c_int,
    pub shared: std::os::raw::c_int,
    pub board: [std::os::raw::c_char; 100usize],
    pub dbg_sleep: std::os::raw::c_int,
    pub speed: std::os::raw::c_int,
}

type SetLoaderPath = unsafe extern "C" fn(path: *const std::os::raw::c_char);
type SetDisplayCallbacks = unsafe extern "C" fn(c: DisplayCallbacks);
type SetVerbosityLevel = unsafe extern "C" fn(level: Verbosity);
type GetStLinkList = unsafe extern "C" fn(
    debug_connect_parameters: *mut *mut DebugConnectParameters,
    shared: std::os::raw::c_int,
) -> std::os::raw::c_int;
type ConnectStLink =
    unsafe extern "C" fn(debug_connect_parameters: DebugConnectParameters) -> std::os::raw::c_int;
type Disconnect = unsafe extern "C" fn();
type Reset = unsafe extern "C" fn(reset_mode: DebugResetMode) -> std::os::raw::c_int;
type MassErase = unsafe extern "C" fn() -> std::os::raw::c_int;
type DownloadFile = unsafe extern "C" fn(
    file_path: *const wchar,
    address: std::os::raw::c_uint,
    skip_erase: std::os::raw::c_uint,
    verify: std::os::raw::c_uint,
    path: *const wchar,
) -> std::os::raw::c_int;
type GetDeviceGeneralInfo = unsafe extern "C" fn() -> *mut DeviceGeneralInfo;
type ReadMemory = unsafe extern "C" fn(
    address: ::std::os::raw::c_uint,
    data: *mut *mut ::std::os::raw::c_uchar,
    size: ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int;
type WriteMemory = unsafe extern "C" fn(
    address: ::std::os::raw::c_uint,
    data: *mut ::std::os::raw::c_uchar,
    size: ::std::os::raw::c_uint,
) -> ::std::os::raw::c_int;
type ReadCoreRegister = unsafe extern "C" fn(
    register: std::os::raw::c_uint,
    data: *mut std::os::raw::c_uint,
) -> ::std::os::raw::c_int;
type WriteCoreRegister = unsafe extern "C" fn(
    register: std::os::raw::c_uint,
    data: std::os::raw::c_uint,
) -> ::std::os::raw::c_int;

#[cfg(unix)]
pub struct VTable {
    set_loaders_path: libloading::os::unix::Symbol<SetLoaderPath>,
    set_display_callbacks: libloading::os::unix::Symbol<SetDisplayCallbacks>,
    set_verbosity_level: libloading::os::unix::Symbol<SetVerbosityLevel>,
    get_stlink_list: libloading::os::unix::Symbol<GetStLinkList>,
    connect_stlink: libloading::os::unix::Symbol<ConnectStLink>,
    disconnect: libloading::os::unix::Symbol<Disconnect>,
    reset: libloading::os::unix::Symbol<Reset>,
    mass_erase: libloading::os::unix::Symbol<MassErase>,
    download_file: libloading::os::unix::Symbol<DownloadFile>,
    get_device_general_info: libloading::os::unix::Symbol<GetDeviceGeneralInfo>,
    read_memory: libloading::os::unix::Symbol<ReadMemory>,
    write_memory: libloading::os::unix::Symbol<WriteMemory>,
    read_core_register: libloading::os::unix::Symbol<ReadCoreRegister>,
    write_core_register: libloading::os::unix::Symbol<WriteCoreRegister>,
}

#[cfg(windows)]
pub struct VTable {
    set_loaders_path: libloading::os::windows::Symbol<SetLoaderPath>,
    set_display_callbacks: libloading::os::windows::Symbol<SetDisplayCallbacks>,
    set_verbosity_level: libloading::os::windows::Symbol<SetVerbosityLevel>,
    get_stlink_list: libloading::os::windows::Symbol<GetStLinkList>,
    connect_stlink: libloading::os::windows::Symbol<ConnectStLink>,
    disconnect: libloading::os::windows::Symbol<Disconnect>,
    reset: libloading::os::windows::Symbol<Reset>,
    mass_erase: libloading::os::windows::Symbol<MassErase>,
    download_file: libloading::os::windows::Symbol<DownloadFile>,
    get_device_general_info: libloading::os::windows::Symbol<GetDeviceGeneralInfo>,
    read_memory: libloading::os::windows::Symbol<ReadMemory>,
    write_memory: libloading::os::windows::Symbol<WriteMemory>,
    read_core_register: libloading::os::windows::Symbol<ReadCoreRegister>,
    write_core_register: libloading::os::windows::Symbol<WriteCoreRegister>,
}

impl VTable {
    fn new(library: &libloading::Library) -> Result<Self, err::Error> {
        let set_loaders_path: libloading::Symbol<SetLoaderPath> =
            unsafe { library.get(b"setLoadersPath\0")? };
        let set_loaders_path = unsafe { set_loaders_path.into_raw() };
        let set_display_callbacks: libloading::Symbol<SetDisplayCallbacks> =
            unsafe { library.get(b"setDisplayCallbacks\0")? };
        let set_display_callbacks = unsafe { set_display_callbacks.into_raw() };
        let set_verbosity_level: libloading::Symbol<SetVerbosityLevel> =
            unsafe { library.get(b"setVerbosityLevel\0")? };
        let set_verbosity_level = unsafe { set_verbosity_level.into_raw() };
        let get_stlink_list: libloading::Symbol<GetStLinkList> =
            unsafe { library.get(b"getStLinkList\0")? };
        let get_stlink_list = unsafe { get_stlink_list.into_raw() };
        let connect_stlink: libloading::Symbol<ConnectStLink> =
            unsafe { library.get(b"connectStLink\0")? };
        let connect_stlink = unsafe { connect_stlink.into_raw() };
        let disconnect: libloading::Symbol<Disconnect> = unsafe { library.get(b"disconnect\0")? };
        let disconnect = unsafe { disconnect.into_raw() };
        let reset: libloading::Symbol<Reset> = unsafe { library.get(b"reset\0")? };
        let reset = unsafe { reset.into_raw() };
        let mass_erase: libloading::Symbol<MassErase> = unsafe { library.get(b"massErase\0")? };
        let mass_erase = unsafe { mass_erase.into_raw() };
        let download_file: libloading::Symbol<DownloadFile> =
            unsafe { library.get(b"downloadFile\0")? };
        let download_file = unsafe { download_file.into_raw() };
        let get_device_general_info: libloading::Symbol<GetDeviceGeneralInfo> =
            unsafe { library.get(b"getDeviceGeneralInf\0")? };
        let get_device_general_info = unsafe { get_device_general_info.into_raw() };
        let read_memory: libloading::Symbol<ReadMemory> = unsafe { library.get(b"readMemory\0")? };
        let read_memory = unsafe { read_memory.into_raw() };
        let write_memory: libloading::Symbol<WriteMemory> =
            unsafe { library.get(b"writeMemory\0")? };
        let write_memory = unsafe { write_memory.into_raw() };
        let read_core_register: libloading::Symbol<ReadCoreRegister> =
            unsafe { library.get(b"readCortexReg\0")? };
        let read_core_register = unsafe { read_core_register.into_raw() };
        let write_core_register: libloading::Symbol<WriteCoreRegister> =
            unsafe { library.get(b"writeCortexRegistres\0")? };
        let write_core_register = unsafe { write_core_register.into_raw() };

        Ok(VTable {
            set_loaders_path,
            set_display_callbacks,
            set_verbosity_level,
            get_stlink_list,
            connect_stlink,
            disconnect,
            reset,
            mass_erase,
            download_file,
            get_device_general_info,
            read_memory,
            write_memory,
            read_core_register,
            write_core_register,
        })
    }
}

#[derive(Debug, Clone)]
pub struct STLink {
    debug_connect_parameters: DebugConnectParameters,
}

impl STLink {
    pub fn serial_number(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.debug_connect_parameters
                .serial_number
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn firmware_version(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.debug_connect_parameters
                .firmware_version
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn board(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.debug_connect_parameters
                .board
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn reset_mode(&mut self, reset_mode: DebugResetMode) {
        self.debug_connect_parameters.reset_mode = reset_mode;
    }

    pub fn connection_mode(&mut self, connection_mode: DebugConnectMode) {
        self.debug_connect_parameters.connection_mode = connection_mode
    }
}

impl std::fmt::Display for STLink {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "Serial Number: {}, Firmware Version: {}, Board: {}",
            self.serial_number().unwrap_or("undefined".into()),
            self.firmware_version().unwrap_or("undefined".into()),
            self.board().unwrap_or("undefined".into()),
        )
    }
}

#[derive(Debug, Clone)]
pub struct DeviceInfo {
    device_general_info: DeviceGeneralInfo,
}

impl DeviceInfo {
    pub fn category(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.device_general_info
                .category
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn cpu(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.device_general_info
                .cpu
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn name(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.device_general_info
                .name
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn series(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.device_general_info
                .series
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn description(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.device_general_info
                .description
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn revision_id(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.device_general_info
                .revision_id
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn board(&self) -> Result<String, err::Error> {
        Ok(String::from_utf8(
            self.device_general_info
                .board
                .iter()
                .map(|&c| c as u8)
                .collect(),
        )?)
    }

    pub fn device_id(&self) -> i32 {
        self.device_general_info.device_id.into()
    }

    pub fn flash_size(&self) -> i32 {
        self.device_general_info.flash_size
    }

    pub fn bootloader_version(&self) -> i32 {
        self.device_general_info.bootloader_version
    }
}

impl std::fmt::Display for DeviceInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "Category: {}, Cpu: {}, Name: {}, Series: {}, Description: {}, Revision Id: {}, Board: {}, Device Id: 0x{:X}, Flash Size: 0x{:X}, Bootloader Version: 0x{:X}",
            self.category().unwrap_or("undefined".into()),
            self.cpu().unwrap_or("undefined".into()),
            self.name().unwrap_or("undefined".into()),
            self.series().unwrap_or("undefined".into()),
            self.description().unwrap_or("undefined".into()),
            self.revision_id().unwrap_or("undefined".into()),
            self.board().unwrap_or("undefined".into()),
            self.device_id(),
            self.flash_size(),
            self.bootloader_version()
        )
    }
}

pub struct STM32CubeProg {
    #[allow(dead_code)]
    library: libloading::Library,
    vtable: VTable,
}

pub enum Register {
    R0,
    R1,
    R2,
    R3,
    R4,
    R5,
    R6,
    R7,
    R8,
    R9,
    R10,
    R11,
    R12,
    SP,
    LR,
    PC,
}

impl From<Register> for u32 {
    fn from(register: Register) -> Self {
        match register {
            Register::R0 => 0,
            Register::R1 => 1,
            Register::R2 => 2,
            Register::R3 => 3,
            Register::R4 => 4,
            Register::R5 => 5,
            Register::R6 => 6,
            Register::R7 => 7,
            Register::R8 => 8,
            Register::R9 => 9,
            Register::R10 => 10,
            Register::R11 => 11,
            Register::R12 => 12,
            Register::SP => 13,
            Register::LR => 14,
            Register::PC => 15,
        }
    }
}

impl STM32CubeProg {
    #[cfg(unix)]
    fn library_path(path: &std::path::Path) -> std::path::PathBuf {
        path.join("lib/libCubeProgrammer_API.so")
    }

    #[cfg(windows)]
    fn library_path(path: &std::path::Path) -> std::path::PathBuf {
        path.join("api\\lib\\CubeProgrammer_API.dll")
    }

    #[cfg(unix)]
    fn flashloader_path(path: &std::path::Path) -> std::path::PathBuf {
        path.join("bin")
    }

    #[cfg(windows)]
    fn flashloader_path(path: &std::path::Path) -> std::path::PathBuf {
        path.join("bin")
    }

    #[cfg(unix)]
    fn load_library(path: &std::path::Path) -> Result<libloading::Library, err::Error> {
        let library: libloading::Library =
            unsafe { libloading::os::unix::Library::new(path)?.into() };
        Ok(library)
    }

    #[cfg(windows)]
    fn load_library(path: &std::path::Path) -> Result<libloading::Library, err::Error> {
        let library: libloading::Library = unsafe {
            libloading::os::windows::Library::load_with_flags(
                path,
                libloading::os::windows::LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
                    | libloading::os::windows::LOAD_LIBRARY_SEARCH_SYSTEM32
                    | libloading::os::windows::LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
            )?
            .into()
        };
        Ok(library)
    }

    pub fn new<P: AsRef<std::path::Path>>(path: P) -> Result<Self, err::Error> {
        let library_path = Self::library_path(path.as_ref());
        let library = Self::load_library(library_path.as_ref())?;
        let vtable = VTable::new(&library)?;

        unsafe {
            (vtable.set_loaders_path)(
                Self::flashloader_path(path.as_ref())
                    .as_os_str()
                    .as_encoded_bytes()
                    .as_ptr() as *const i8,
            )
        };

        let cb: DisplayCallbacks = DisplayCallbacks {
            init_progress_bar: init_progress_bar,
            log_message: log_message,
            load_bar: load_bar,
        };

        unsafe { (vtable.set_display_callbacks)(cb) };

        unsafe { (vtable.set_verbosity_level)(Verbosity::Level0) };

        Ok(STM32CubeProg { library, vtable })
    }

    pub fn discover(&self) -> Result<Vec<STLink>, err::Error> {
        let mut debug_connect_parameters = std::ptr::null_mut();
        let stlink_count =
            unsafe { (self.vtable.get_stlink_list)(&mut debug_connect_parameters, 0) };

        let params_slice =
            unsafe { std::slice::from_raw_parts(debug_connect_parameters, stlink_count as usize) };

        params_slice
            .iter()
            .map(|param| -> Result<STLink, err::Error> {
                let debug_connect_parameters = param.clone();
                Ok(STLink {
                    debug_connect_parameters,
                })
            })
            .collect::<Result<Vec<STLink>, err::Error>>()
    }

    pub fn connect(&self, stlink: &STLink) -> Result<(), err::Error> {
        let error = unsafe { (self.vtable.connect_stlink)(stlink.debug_connect_parameters) };
        if error == 0 {
            Ok(())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn disconnect(&self) {
        unsafe { (self.vtable.disconnect)() };
    }

    pub fn reset(&self, stlink: &STLink) -> Result<(), err::Error> {
        let error = unsafe { (self.vtable.reset)(stlink.debug_connect_parameters.reset_mode) };
        if error == 0 {
            Ok(())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn mass_erase(&self) -> Result<(), err::Error> {
        let error = unsafe { (self.vtable.mass_erase)() };
        if error == 0 {
            Ok(())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn download<P: AsRef<std::path::Path>>(
        &self,
        path: P,
        address: Option<u32>,
        skip_erase: Option<bool>,
        verify: Option<bool>,
    ) -> Result<(), err::Error> {
        let c_path = widestring::WideCString::from_os_str(
            std::fs::canonicalize(path.as_ref())?.as_os_str(),
        )?;

        let error = unsafe {
            (self.vtable.download_file)(
                c_path.as_ptr(),
                address.unwrap_or(0),
                skip_erase.unwrap_or(true).into(),
                verify.unwrap_or(true).into(),
                std::ptr::null(),
            )
        };
        if error == 0 {
            Ok(())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn device_info(&self) -> Result<DeviceInfo, err::Error> {
        let device_general_info = unsafe { (self.vtable.get_device_general_info)().as_ref() };

        match device_general_info {
            Some(value) => Ok(DeviceInfo {
                device_general_info: value.clone(),
            }),
            None => Err(err::CubeProgrammerError::NoDeviceFound.into()),
        }
    }

    pub fn read_core_register(&self, register: Register) -> Result<u32, err::Error> {
        let mut data = 0;
        let error = unsafe { (self.vtable.read_core_register)(register.into(), &mut data) };
        if error == 0 {
            Ok(data)
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn write_core_register(&self, register: Register, data: u32) -> Result<(), err::Error> {
        let error = unsafe { (self.vtable.write_core_register)(register.into(), data) };
        if error == 0 {
            Ok(())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn read_memory8(&self, address: u32, size: u32) -> Result<Vec<u8>, err::Error> {
        let mut data = std::ptr::null_mut();
        let error = unsafe { (self.vtable.read_memory)(address, &mut data, size) };
        if error == 0 {
            let data: &mut [u8] = unsafe { core::slice::from_raw_parts_mut(data, size as usize) };
            Ok(data.to_vec())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn read_memory32(&self, address: u32, size: u32) -> Result<Vec<u32>, err::Error> {
        let mut data = std::ptr::null_mut();
        let error = unsafe { (self.vtable.read_memory)(address, &mut data, size * 4) };
        if error == 0 {
            let data: &mut [u8] =
                unsafe { core::slice::from_raw_parts_mut(data, (size * 4) as usize) };
            data.chunks(4)
                .map(|chunk| -> Result<u32, err::Error> {
                    let chunk = std::convert::TryInto::try_into(chunk)?;
                    Ok(u32::from_le_bytes(chunk))
                })
                .collect::<Result<Vec<u32>, err::Error>>()
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn write_memory8(&self, address: u32, data: Vec<u8>) -> Result<(), err::Error> {
        let size: u32 = std::convert::TryInto::try_into(data.len())?;

        let error = unsafe { (self.vtable.write_memory)(address, data.clone().as_mut_ptr(), size) };
        if error == 0 {
            Ok(())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }

    pub fn write_memory32(&self, address: u32, data_u32: Vec<u32>) -> Result<(), err::Error> {
        let size: u32 = std::convert::TryInto::try_into(data_u32.len())?;
        let mut data_u8: Vec<u8> = Vec::new();
        for &num in &data_u32 {
            data_u8.extend_from_slice(&num.to_le_bytes());
        }

        let error = unsafe { (self.vtable.write_memory)(address, data_u8.as_mut_ptr(), size * 4) };
        if error == 0 {
            Ok(())
        } else {
            Err(err::CubeProgrammerError::from(error).into())
        }
    }
}