flipperzero 0.16.0

Rust for Flipper Zero
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
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
//! I2C interface for the Flipper Zero.

use core::fmt;

use flipperzero_sys as sys;

use crate::furi::time::FuriDuration;

/// The address of the internal LED controller.
///
/// This is an [LP5562].
///
/// [LP5562]: https://www.ti.com/lit/ds/symlink/lp5562.pdf
pub const INTERNAL_LED_CONTROLLER: DeviceAddress = DeviceAddress::new(0x30);

/// The address of the internal battery fuel gauge.
///
/// This is a [BQ27220].
///
/// [BQ27220]: https://www.ti.com/lit/ds/symlink/bq27220.pdf
pub const INTERNAL_BATTERY_FUEL_GAUGE: DeviceAddress = DeviceAddress::new(0x55);

/// The address of the internal battery charger.
///
/// This is a [BQ25896].
///
/// [BQ25896]: https://www.ti.com/lit/ds/symlink/bq25896.pdf
pub const INTERNAL_BATTERY_CHARGER: DeviceAddress = DeviceAddress::new(0x6B);

/// A 7-bit I2C device address.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct DeviceAddress(u8);

impl fmt::Debug for DeviceAddress {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("DeviceAddress")
            .field(&(self.0 >> 1))
            .finish()
    }
}

impl ufmt::uDebug for DeviceAddress {
    #[inline]
    fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
    where
        W: ufmt::uWrite + ?Sized,
    {
        f.debug_tuple("DeviceAddress")?
            .field(&(self.0 >> 1))?
            .finish()
    }
}

impl DeviceAddress {
    /// Constructs a `DeviceAddress` from its 7-bit value.
    ///
    /// The upper bit of `addr` is ignored; `0x00` and `0x80` are treated as the same
    /// address.
    pub const fn new(addr: u8) -> Self {
        // The SDK takes addresses in 8-bit form, so we store them that way to reduce the
        // number of places we need to handle the shift.
        Self(addr << 1)
    }
}

/// A handle to an I2C bus.
#[derive(Clone, Copy)]
enum BusKind {
    Internal,
    External,
}

/// An I2C bus on the Flipper Zero.
#[derive(Clone, Copy)]
pub struct Bus(BusKind);

impl Bus {
    /// The internal (power) I2C bus.
    ///
    /// This has three devices:
    /// - [`INTERNAL_LED_CONTROLLER`]
    /// - [`INTERNAL_BATTERY_FUEL_GAUGE`]
    /// - [`INTERNAL_BATTERY_CHARGER`]
    pub const INTERNAL: Self = Self(BusKind::Internal);

    /// The external I2C bus.
    ///
    /// - Connect `SCL` to pin `C0`.
    /// - Connect `SDA` to pin `C1`.
    ///
    /// # Warning
    ///
    /// Only connect 3.3V peripherals directly to your Flipper Zero, or you risk damaging
    /// it. For I2C devices that operate at different voltages, use a level shifter.
    pub const EXTERNAL: Self = Self(BusKind::External);

    /// Acquires a handle to the given I2C bus.
    ///
    /// Blocks indefinitely until the bus is available.
    pub fn acquire(self) -> BusHandle {
        // SAFETY: We block until we acquire a handle to the selected bus, so nothing else
        // will be using it while we have a raw pointer to it. We don't convert this to a
        // `&'static mut` reference because this will be disallowed in Rust 2024 edition.
        BusHandle::acquire(match self.0 {
            BusKind::Internal => &raw const sys::furi_hal_i2c_handle_power,
            BusKind::External => &raw const sys::furi_hal_i2c_handle_external,
        })
    }

    /// Acquires a handle to the given I2C bus and then runs the given function.
    ///
    /// Blocks indefinitely until the bus is available.
    pub fn with_handle<T>(self, f: impl FnOnce(BusHandle) -> T) -> T {
        f(self.acquire())
    }
}

/// A handle to an I2C bus on the Flipper Zero.
pub struct BusHandle {
    handle: *const sys::FuriHalI2cBusHandle,
}

impl Drop for BusHandle {
    fn drop(&mut self) {
        unsafe { sys::furi_hal_i2c_release(self.handle) };
    }
}

impl BusHandle {
    /// Acquires a handle to the given I2C bus.
    ///
    /// Blocks indefinitely until the Flipper Zero bus is locally available.
    fn acquire(handle: *const sys::FuriHalI2cBusHandle) -> Self {
        unsafe { sys::furi_hal_i2c_acquire(handle) };
        Self { handle }
    }

    /// Enumerates the devices that are present and ready on this bus.
    pub fn enumerate_devices(
        &mut self,
        per_device_timeout: FuriDuration,
    ) -> impl Iterator<Item = DeviceAddress> + '_ {
        (0x00..0x80).filter_map(move |addr| {
            let device = DeviceAddress::new(addr);
            self.is_device_ready(device, per_device_timeout)
                .then_some(device)
        })
    }

    /// Checks if the device with address `i2c_addr` is present and ready on the bus.
    ///
    /// Returns `true` if the device is present and ready, false otherwise.
    pub fn is_device_ready(&mut self, device: DeviceAddress, timeout: FuriDuration) -> bool {
        unsafe {
            sys::furi_hal_i2c_is_device_ready(self.handle, device.0, timeout.as_millis() as u32)
        }
    }

    /// Reads the 8-bit register at `reg_addr` on `device`.
    pub fn read_u8(
        &mut self,
        device: DeviceAddress,
        reg_addr: u8,
        timeout: FuriDuration,
    ) -> Result<u8, Error> {
        let mut data = 0;
        if unsafe {
            sys::furi_hal_i2c_read_reg_8(
                self.handle,
                device.0,
                reg_addr,
                &mut data,
                timeout.as_millis() as u32,
            )
        } {
            Ok(data)
        } else {
            Err(Error::TransferFailed)
        }
    }

    /// Reads the 16-bit register at `reg_addr` on `device`.
    pub fn read_u16(
        &mut self,
        device: DeviceAddress,
        reg_addr: u8,
        timeout: FuriDuration,
    ) -> Result<u16, Error> {
        let mut data = 0;
        if unsafe {
            sys::furi_hal_i2c_read_reg_16(
                self.handle,
                device.0,
                reg_addr,
                &mut data,
                timeout.as_millis() as u32,
            )
        } {
            Ok(data)
        } else {
            Err(Error::TransferFailed)
        }
    }

    /// Reads `device`'s memory starting at `mem_addr` into the given buffer.
    pub fn read_exact(
        &mut self,
        device: DeviceAddress,
        mem_addr: u8,
        buf: &mut [u8],
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        if unsafe {
            sys::furi_hal_i2c_read_mem(
                self.handle,
                device.0,
                mem_addr,
                buf.as_mut_ptr(),
                buf.len(),
                timeout.as_millis() as u32,
            )
        } {
            Ok(())
        } else {
            Err(Error::TransferFailed)
        }
    }

    /// Writes the given value into the 8-bit register at `reg_addr` on `device`.
    pub fn write_u8(
        &mut self,
        device: DeviceAddress,
        reg_addr: u8,
        data: u8,
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        if unsafe {
            sys::furi_hal_i2c_write_reg_8(
                self.handle,
                device.0,
                reg_addr,
                data,
                timeout.as_millis() as u32,
            )
        } {
            Ok(())
        } else {
            Err(Error::TransferFailed)
        }
    }

    /// Writes the given value into the 16-bit register at `reg_addr` on `device`.
    pub fn write_u16(
        &mut self,
        device: DeviceAddress,
        reg_addr: u8,
        data: u16,
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        if unsafe {
            sys::furi_hal_i2c_write_reg_16(
                self.handle,
                device.0,
                reg_addr,
                data,
                timeout.as_millis() as u32,
            )
        } {
            Ok(())
        } else {
            Err(Error::TransferFailed)
        }
    }

    /// Writes the given data into `device`'s memory starting at `mem_addr`.
    pub fn write_all(
        &mut self,
        device: DeviceAddress,
        mem_addr: u8,
        data: &[u8],
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        if unsafe {
            sys::furi_hal_i2c_write_mem(
                self.handle,
                device.0,
                mem_addr,
                data.as_ptr(),
                data.len(),
                timeout.as_millis() as u32,
            )
        } {
            Ok(())
        } else {
            Err(Error::TransferFailed)
        }
    }

    /// Reads data from `device` and writes it to the `data` buffer.
    pub fn tx(
        &mut self,
        device: DeviceAddress,
        data: &[u8],
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        unsafe {
            sys::furi_hal_i2c_tx(
                self.handle,
                device.0,
                data.as_ptr(),
                data.len(),
                timeout.as_millis() as u32,
            )
        }
        .then_some(())
        .ok_or(Error::TransferFailed)
    }

    /// Writes the given data to `device`.
    pub fn rx(
        &mut self,
        device: DeviceAddress,
        data: &mut [u8],
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        unsafe {
            sys::furi_hal_i2c_rx(
                self.handle,
                device.0,
                data.as_mut_ptr(),
                data.len(),
                timeout.as_millis() as u32,
            )
        }
        .then_some(())
        .ok_or(Error::TransferFailed)
    }

    /// Writes the data in `write` to `device` and then reads from it into the `read` buffer.
    pub fn trx(
        &mut self,
        device: DeviceAddress,
        write: &[u8],
        read: &mut [u8],
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        unsafe {
            sys::furi_hal_i2c_trx(
                self.handle,
                device.0,
                write.as_ptr(),
                write.len(),
                read.as_mut_ptr(),
                read.len(),
                timeout.as_millis() as u32,
            )
        }
        .then_some(())
        .ok_or(Error::TransferFailed)
    }

    /// Execute the provided operations on the I2C bus.
    ///
    /// Transaction contract:
    /// - Before executing the first operation an ST is sent automatically. This is followed by SAD+R/W as appropriate.
    /// - Data from adjacent operations of the same type are sent after each other without an SP or SR.
    /// - Between adjacent operations of a different type an SR and SAD+R/W is sent.
    /// - After executing the last operation an SP is sent automatically.
    /// - If the last operation is a `Read` the master does not send an acknowledge for the last byte.
    ///
    /// - `ST` = start condition
    /// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
    /// - `SR` = repeated start condition
    /// - `SP` = stop condition
    pub fn transaction(
        &mut self,
        device: DeviceAddress,
        operations: &mut [Operation],
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        self.transaction_impl(device, operations, timeout)
    }

    // This is similar to `trx`, the only difference being that it sends a RESTART condition
    // between the two transfers instead of STOP + START
    #[cfg(any(feature = "embedded-hal", feature = "embedded-hal-0"))]
    fn write_read_impl(
        &mut self,
        device: DeviceAddress,
        write: &[u8],
        read: &mut [u8],
        timeout: FuriDuration,
    ) -> Result<(), Error> {
        unsafe {
            sys::furi_hal_i2c_tx_ext(
                self.handle,
                device.0.into(),
                false,
                write.as_ptr(),
                write.len(),
                sys::FuriHalI2cBeginStart,
                sys::FuriHalI2cEndAwaitRestart,
                timeout.as_millis() as u32,
            ) && sys::furi_hal_i2c_rx_ext(
                self.handle,
                device.0.into(),
                false,
                read.as_mut_ptr(),
                read.len(),
                sys::FuriHalI2cBeginRestart,
                sys::FuriHalI2cEndStop,
                timeout.as_millis() as u32,
            )
        }
        .then_some(())
        .ok_or(Error::TransferFailed)
    }

    // This function is generic to allow the implementation of both versions of the embedded_hal
    // transaction traits
    fn transaction_impl<'a, O>(
        &mut self,
        device: DeviceAddress,
        operations: &mut [O],
        timeout: FuriDuration,
    ) -> Result<(), Error>
    where
        O: OperationLike + 'a,
    {
        use sys::{
            FuriHalI2cBeginRestart as BeginRestart, FuriHalI2cBeginResume as BeginResume,
            FuriHalI2cBeginStart as BeginStart, FuriHalI2cEndAwaitRestart as EndAwaitRestart,
            FuriHalI2cEndPause as EndPause, FuriHalI2cEndStop as EndStop,
        };

        let mut operations = operations.iter_mut().peekable();
        let mut start = BeginStart;
        let address = device.0.into();

        while let Some(op) = operations.next() {
            let (end, next_start) = match (op.kind(), operations.peek().map(|next| next.kind())) {
                (OperationKind::Read, Some(OperationKind::Read))
                | (OperationKind::Write, Some(OperationKind::Write)) => (EndPause, BeginResume),
                (_, Some(_)) => (EndAwaitRestart, BeginRestart),
                (_, None) => (EndStop, BeginStart),
            };

            let result = unsafe {
                match op.as_op() {
                    Operation::Read(buffer) => flipperzero_sys::furi_hal_i2c_rx_ext(
                        self.handle,
                        address,
                        false,
                        buffer.as_mut_ptr(),
                        buffer.len(),
                        start,
                        end,
                        timeout.as_millis() as u32,
                    ),
                    Operation::Write(buffer) => flipperzero_sys::furi_hal_i2c_tx_ext(
                        self.handle,
                        address,
                        false,
                        buffer.as_ptr(),
                        buffer.len(),
                        start,
                        end,
                        timeout.as_millis() as u32,
                    ),
                }
            };

            if !result {
                return Err(Error::TransferFailed);
            }

            start = next_start;
        }

        Ok(())
    }
}

#[derive(Debug, PartialEq)]
pub enum Error {
    TransferFailed,
}

/// I2C operation.
///
/// Several operations can be combined as part of a transaction.
#[derive(Debug, PartialEq, Eq)]
pub enum Operation<'a> {
    /// Read data into the provided buffer
    Read(&'a mut [u8]),
    /// Write data from the provided buffer
    Write(&'a [u8]),
}

// These exist to allow compatibility with both embedded_hal 1.0 and 0.2 versions of the Operation
// enum

enum OperationKind {
    Read,
    Write,
}

trait OperationLike {
    fn as_op(&mut self) -> Operation<'_>;
    fn kind(&self) -> OperationKind;
}

impl OperationLike for Operation<'_> {
    fn as_op(&mut self) -> Operation<'_> {
        match self {
            Operation::Read(buffer) => Operation::Read(buffer),
            Operation::Write(buffer) => Operation::Write(buffer),
        }
    }

    fn kind(&self) -> OperationKind {
        match self {
            Operation::Read(_) => OperationKind::Read,
            Operation::Write(_) => OperationKind::Write,
        }
    }
}

// embedded_hal specific

/// An I2C bus implementing the embedded-hal traits
///
/// It acquires and releases a handle to the underlying bus for each function, similar to
/// [embedded-hal-bus](https://docs.rs/embedded-hal-bus/0.1.0-rc.1/embedded_hal_bus/index.html)'
/// [MutexDevice](https://docs.rs/embedded-hal-bus/0.1.0-rc.1/embedded_hal_bus/i2c/struct.MutexDevice.html).
/// It uses the same timeout duration for each operation.
#[cfg(any(feature = "embedded-hal", feature = "embedded-hal-0"))]
pub struct EmbeddedHalBus {
    bus: Bus,
    /// The timeout used for each operation
    timeout: FuriDuration,
}

#[cfg(any(feature = "embedded-hal", feature = "embedded-hal-0"))]
impl EmbeddedHalBus {
    pub fn new(bus: Bus, timeout: FuriDuration) -> Self {
        Self { bus, timeout }
    }

    pub fn set_timeout(&mut self, timeout: FuriDuration) {
        self.timeout = timeout
    }
}

// embedded_hal 1.0 implementations

#[cfg(feature = "embedded-hal")]
impl embedded_hal::i2c::Error for Error {
    fn kind(&self) -> embedded_hal::i2c::ErrorKind {
        embedded_hal::i2c::ErrorKind::Other
    }
}

#[cfg(feature = "embedded-hal")]
impl embedded_hal::i2c::ErrorType for EmbeddedHalBus {
    type Error = Error;
}

#[cfg(feature = "embedded-hal")]
impl OperationLike for embedded_hal::i2c::Operation<'_> {
    fn as_op(&mut self) -> Operation<'_> {
        match self {
            embedded_hal::i2c::Operation::Read(buffer) => Operation::Read(buffer),
            embedded_hal::i2c::Operation::Write(buffer) => Operation::Write(buffer),
        }
    }

    fn kind(&self) -> OperationKind {
        match self {
            embedded_hal::i2c::Operation::Read(_) => OperationKind::Read,
            embedded_hal::i2c::Operation::Write(_) => OperationKind::Write,
        }
    }
}

#[cfg(feature = "embedded-hal")]
impl embedded_hal::i2c::I2c for EmbeddedHalBus {
    fn transaction(
        &mut self,
        address: u8,
        operations: &mut [embedded_hal::i2c::Operation<'_>],
    ) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .transaction_impl(DeviceAddress::new(address), operations, self.timeout)
    }

    fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .rx(DeviceAddress::new(address), read, self.timeout)
    }

    fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .tx(DeviceAddress::new(address), write, self.timeout)
    }

    fn write_read(
        &mut self,
        address: u8,
        write: &[u8],
        read: &mut [u8],
    ) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .write_read_impl(DeviceAddress::new(address), write, read, self.timeout)
    }
}

// embedded_hal 0.2 implementations

#[cfg(feature = "embedded-hal-0")]
impl OperationLike for embedded_hal_0::blocking::i2c::Operation<'_> {
    fn as_op(&mut self) -> Operation<'_> {
        match self {
            embedded_hal_0::blocking::i2c::Operation::Read(buffer) => Operation::Read(buffer),
            embedded_hal_0::blocking::i2c::Operation::Write(buffer) => Operation::Write(buffer),
        }
    }

    fn kind(&self) -> OperationKind {
        match self {
            embedded_hal_0::blocking::i2c::Operation::Read(_) => OperationKind::Read,
            embedded_hal_0::blocking::i2c::Operation::Write(_) => OperationKind::Write,
        }
    }
}

#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::blocking::i2c::Read for EmbeddedHalBus {
    type Error = Error;

    fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .rx(DeviceAddress::new(address), buffer, self.timeout)
    }
}

#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::blocking::i2c::Write for EmbeddedHalBus {
    type Error = Error;

    fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .tx(DeviceAddress::new(address), bytes, self.timeout)
    }
}

#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::blocking::i2c::WriteRead for EmbeddedHalBus {
    type Error = Error;

    fn write_read(
        &mut self,
        address: u8,
        bytes: &[u8],
        buffer: &mut [u8],
    ) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .write_read_impl(DeviceAddress::new(address), bytes, buffer, self.timeout)
    }
}

#[cfg(feature = "embedded-hal-0")]
impl embedded_hal_0::blocking::i2c::Transactional for EmbeddedHalBus {
    type Error = Error;

    fn exec(
        &mut self,
        address: u8,
        operations: &mut [embedded_hal_0::blocking::i2c::Operation<'_>],
    ) -> Result<(), Self::Error> {
        self.bus
            .acquire()
            .transaction_impl(DeviceAddress::new(address), operations, self.timeout)
    }
}

#[flipperzero_test::tests]
mod tests {
    use super::{
        Bus, DeviceAddress, INTERNAL_BATTERY_CHARGER, INTERNAL_BATTERY_FUEL_GAUGE,
        INTERNAL_LED_CONTROLLER,
    };
    use crate::furi::time::FuriDuration;

    #[test]
    fn enumerate_devices() {
        const INTERNAL_DEVICES: &[DeviceAddress] = &[
            INTERNAL_LED_CONTROLLER,
            INTERNAL_BATTERY_FUEL_GAUGE,
            INTERNAL_BATTERY_CHARGER,
        ];

        let mut bus = Bus::INTERNAL.acquire();
        for (i, device) in bus
            .enumerate_devices(FuriDuration::from_millis(50))
            .enumerate()
        {
            if let Some(&expected) = INTERNAL_DEVICES.get(i) {
                assert_eq!(expected, device);
            }
        }
    }
}