max11300 0.5.2

A rust-embedded driver for the MAX11300 ADC/DAC
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
722
723
724
725
726
727
728
729
730
#![cfg_attr(not(test), no_std)]

pub mod config;
mod port;

use core::future::Future;

use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::mutex::Mutex;
use embedded_hal::digital::OutputPin;
use embedded_hal_async::spi::SpiBus;
use heapless::Vec;
use seq_macro::seq;

use config::*;

pub use port::{IntoConfiguredPort, IntoMode};
pub use port::{MaxPort, Mode0Port, Multiport};

#[derive(Debug)]
pub enum Error<S, P> {
    /// SPI bus error
    Spi(S),
    /// CS pin error
    Pin(P),
    /// Connection error (device not found)
    Conn,
    /// Address error (invalid or out of bounds)
    Address,
    /// Port error (invalid or out of bounds)
    Port,
    /// Mode error (wrong port mode for method)
    Mode,
}

pub trait ConfigurePort<CONFIG, S, P> {
    fn configure_port(
        &mut self,
        port: Port,
        config: CONFIG,
    ) -> impl Future<Output = Result<(), Error<S, P>>>;
}

/// Max11300 driver
pub struct Max11300<SPI, EN> {
    channel_config: [Mode; 20],
    config: DeviceConfig,
    enable: EN,
    spi: SPI,
    /// Shadow of the current GPODAT register state (bits 0..20 used).
    /// Lets GPO writes skip the read-modify-write round-trip and makes
    /// bulk updates expressible as a single SPI write.
    gpodat: u32,
}

impl<SPI, EN, S, P> Max11300<SPI, EN>
where
    SPI: SpiBus<Error = S>,
    EN: OutputPin<Error = P>,
{
    /// Create a new Max11300 instance
    pub async fn try_new(
        spi: SPI,
        mut enable: EN,
        config: DeviceConfig,
    ) -> Result<Self, Error<S, P>> {
        enable.set_high().map_err(Error::Pin)?;
        let mut max = Self {
            channel_config: [Mode::Mode0(ConfigMode0); 20],
            config,
            enable,
            spi,
            gpodat: 0,
        };
        if max.read_register(REG_DEVICE_ID).await? != DEVICE_ID {
            return Err(Error::Conn);
        }
        max.write_register(REG_DEVICE_CTRL, config.as_u16()).await?;
        Ok(max)
    }

    /// Return the current DeviceConfig
    pub fn config(&self) -> DeviceConfig {
        self.config
    }

    /// Configure the input threshold for a ConfigMode1 Port
    pub async fn gpi_configure_threshold(
        &mut self,
        port: Port,
        threshold: u16,
        mode: GPIMD,
    ) -> Result<(), Error<S, P>> {
        let Mode::Mode1(_) = self.get_mode(port) else {
            return Err(Error::Mode);
        };
        self._gpi_configure_threshold(port, threshold, mode).await
    }

    /// Configure the digital output level for a ConfigMode3 Port
    pub async fn gpo_configure_level(&mut self, port: Port, level: u16) -> Result<(), Error<S, P>> {
        let Mode::Mode3(_) = self.get_mode(port) else {
            return Err(Error::Mode);
        };
        self._gpo_configure_level(port, level).await
    }

    /// Set a digital output high on a ConfigMode3 Port
    pub async fn gpo_set_high(&mut self, port: Port) -> Result<(), Error<S, P>> {
        let Mode::Mode3(_) = self.get_mode(port) else {
            return Err(Error::Mode);
        };
        self._gpo_set_high(port).await
    }

    /// Set a digital output low on a ConfigMode3 Port
    pub async fn gpo_set_low(&mut self, port: Port) -> Result<(), Error<S, P>> {
        let Mode::Mode3(_) = self.get_mode(port) else {
            return Err(Error::Mode);
        };
        self._gpo_set_low(port).await
    }

    /// Toggle digital output on a ConfigMode3 Port
    pub async fn gpo_toggle(&mut self, port: Port) -> Result<(), Error<S, P>> {
        let Mode::Mode3(_) = self.get_mode(port) else {
            return Err(Error::Mode);
        };
        self._gpo_toggle(port).await
    }

    /// Drive multiple Mode3 GPOs high in a single SPI transaction.
    ///
    /// Every port in `ports` must be configured in Mode3; otherwise this
    /// returns `Error::Mode` and the chip state is unchanged. All affected
    /// bits in the same GPODAT register word latch simultaneously at the
    /// hardware, so the rising edges are physically coincident at the pins.
    pub async fn gpo_set_high_many(&mut self, ports: &[Port]) -> Result<(), Error<S, P>> {
        let mut mask: u32 = 0;
        for &port in ports {
            let Mode::Mode3(_) = self.get_mode(port) else {
                return Err(Error::Mode);
            };
            mask |= 1 << port as usize;
        }
        self._gpo_update(mask, 0).await
    }

    /// Drive multiple Mode3 GPOs low in a single SPI transaction.
    ///
    /// Every port in `ports` must be configured in Mode3; otherwise this
    /// returns `Error::Mode` and the chip state is unchanged. All affected
    /// bits in the same GPODAT register word latch simultaneously at the
    /// hardware, so the falling edges are physically coincident at the pins.
    pub async fn gpo_set_low_many(&mut self, ports: &[Port]) -> Result<(), Error<S, P>> {
        let mut mask: u32 = 0;
        for &port in ports {
            let Mode::Mode3(_) = self.get_mode(port) else {
                return Err(Error::Mode);
            };
            mask |= 1 << port as usize;
        }
        self._gpo_update(0, mask).await
    }

    /// Set a DAC value for a ConfigMode5 Port
    pub async fn dac_set_value(&mut self, port: Port, data: u16) -> Result<(), Error<S, P>> {
        // Setting the DAC register values should be allowed in any mode as it is required
        // for some mode transitions (e.g. -> Mode3)
        self._dac_set_value(port, data).await
    }

    /// Read an ADC value from a ConfigMode7 Port
    pub async fn adc_get_value(&mut self, port: Port) -> Result<u16, Error<S, P>> {
        let Mode::Mode7(_) = self.get_mode(port) else {
            return Err(Error::Mode);
        };
        self._adc_get_value(port).await
    }

    /// Read the Interrupts from the Interrupt register
    pub async fn int_read(&mut self) -> Result<Interrupts, Error<S, P>> {
        let val = self.read_register(REG_INTERRUPT).await?;
        Ok(Interrupts::from(val))
    }

    /// Read the raw Interrupts from the Interrupt register
    pub async fn int_read_raw(&mut self) -> Result<u16, Error<S, P>> {
        let val = self.read_register(REG_INTERRUPT).await?;
        Ok(val)
    }

    /// Read the GPI Status Interrupts from the GPIST register
    pub async fn int_read_gpist(&mut self) -> Result<u32, Error<S, P>> {
        let mut data = [0_u16; 2];
        self.read_registers(REG_GPI_STATUS, &mut data).await?;
        Ok(((data[0] as u32) << 16) | data[1] as u32)
    }

    /// Get the Mode for a Port
    pub fn get_mode(&self, port: Port) -> Mode {
        self.channel_config[port.as_usize()]
    }

    async fn read_register(&mut self, address: u8) -> Result<u16, Error<S, P>> {
        if address > MAX_ADDRESS {
            return Err(Error::Address);
        }
        let mut buf = [0, 0, 0];
        self.enable.set_low().map_err(Error::Pin)?;
        self.spi
            .transfer(&mut buf, &[(address << 1) | 1])
            .await
            .map_err(Error::Spi)?;
        self.enable.set_high().map_err(Error::Pin)?;
        Ok(((buf[1] as u16) << 8) | buf[2] as u16)
    }

    async fn read_registers<'a>(
        &mut self,
        start_address: u8,
        data: &'a mut [u16],
    ) -> Result<&'a [u16], Error<S, P>> {
        if data.len() > 20 || start_address + data.len() as u8 - 1 > MAX_ADDRESS {
            return Err(Error::Address);
        }
        // Max 1 command byte + 2x20 data bytes
        let mut buf: Vec<u8, 41> = Vec::new();
        // The read buffer must be long enough for the junk byte that is read
        // while the address is being written, PLUS the actual data bytes.
        buf.resize(1 + data.len() * 2, 0).ok();
        // Read values into buf
        self.enable.set_low().map_err(Error::Pin)?;
        self.spi
            .transfer(&mut buf, &[(start_address << 1) | 1])
            .await
            .map_err(Error::Spi)?;
        self.enable.set_high().map_err(Error::Pin)?;
        // Copy to data buffer, skipping the first junk byte.
        for (i, bytes) in buf[1..].chunks_exact(2).enumerate() {
            data[i] = ((bytes[0] as u16) << 8) | bytes[1] as u16;
        }
        Ok(data)
    }

    async fn write_register(&mut self, address: u8, data: u16) -> Result<(), Error<S, P>> {
        if address > MAX_ADDRESS {
            return Err(Error::Address);
        }
        self.enable.set_low().map_err(Error::Pin)?;
        self.spi
            .write(&[address << 1, (data >> 8) as u8, (data & 0xff) as u8])
            .await
            .map_err(Error::Spi)?;
        self.enable.set_high().map_err(Error::Pin)?;
        Ok(())
    }

    async fn write_registers(
        &mut self,
        start_address: u8,
        data: &[u16],
    ) -> Result<(), Error<S, P>> {
        if data.len() > 20 || start_address + data.len() as u8 - 1 > MAX_ADDRESS {
            return Err(Error::Address);
        }
        // 1 address byte, 2x20 data bytes maximum
        let mut buf: Vec<u8, 41> = Vec::new();
        // Actual size of u16 data buffer times two plus address byte
        buf.resize(data.len() * 2 + 1, 0).ok();
        // Write instruction
        buf[0] = start_address << 1;

        let payload = &mut buf[1..];
        for (bytes, &word) in payload.chunks_exact_mut(2).zip(data.iter()) {
            let word_bytes = word.to_be_bytes();
            bytes[0] = word_bytes[0];
            bytes[1] = word_bytes[1];
        }

        self.enable.set_low().map_err(Error::Pin)?;
        self.spi.write(&buf).await.map_err(Error::Spi)?;
        self.enable.set_high().map_err(Error::Pin)?;
        Ok(())
    }

    async fn _configure_port(&mut self, port: Port, config: u16) -> Result<(), Error<S, P>> {
        self.write_register(port.as_config_addr(), config).await
    }

    async fn _gpi_configure_threshold(
        &mut self,
        port: Port,
        threshold: u16,
        mode: GPIMD,
    ) -> Result<(), Error<S, P>> {
        // Set IRQ mode for port
        let pos = port as usize % 8 * 2;
        let mut current = self.read_register(REG_IRQ_MODE).await?;
        let mut next = (mode as u16) << pos;
        current &= !(0b11 << pos);
        next |= current;
        self.write_register(REG_IRQ_MODE, next).await?;
        // Set threshold for port
        self.write_register(REG_DAC_DATA + (port as u8), threshold)
            .await
    }

    async fn _gpo_configure_level(&mut self, port: Port, level: u16) -> Result<(), Error<S, P>> {
        self.write_register(REG_DAC_DATA + (port as u8), level)
            .await
    }

    /// Apply `set` and `clear` to the GPODAT shadow and push the resulting
    /// state to the chip. Writes only the GPODAT register word(s) whose
    /// bits actually changed.
    ///
    /// All bits in the same GPODAT word latch simultaneously at the chip,
    /// so any subset of ports within one word (contiguous or not) updates
    /// atomically in hardware.
    async fn _gpo_update(&mut self, set: u32, clear: u32) -> Result<(), Error<S, P>> {
        let prev = self.gpodat;
        let next = (prev | set) & !clear;
        if prev == next {
            return Ok(());
        }
        self.gpodat = next;

        let changed = prev ^ next;
        let low_changed = (changed & 0x0000_FFFF) != 0;
        let high_changed = (changed & 0x000F_0000) != 0;

        match (low_changed, high_changed) {
            (true, true) => {
                self.write_registers(REG_GPO_DATA, &[next as u16, (next >> 16) as u16])
                    .await
            }
            (true, false) => self.write_register(REG_GPO_DATA, next as u16).await,
            (false, true) => {
                self.write_register(REG_GPO_DATA + 1, (next >> 16) as u16)
                    .await
            }
            (false, false) => Ok(()),
        }
    }

    async fn _gpo_set_high(&mut self, port: Port) -> Result<(), Error<S, P>> {
        self._gpo_update(1 << port as usize, 0).await
    }

    async fn _gpo_set_low(&mut self, port: Port) -> Result<(), Error<S, P>> {
        self._gpo_update(0, 1 << port as usize).await
    }

    async fn _gpo_toggle(&mut self, port: Port) -> Result<(), Error<S, P>> {
        let bit = 1u32 << port as usize;
        if self.gpodat & bit == 0 {
            self._gpo_update(bit, 0).await
        } else {
            self._gpo_update(0, bit).await
        }
    }

    async fn _dac_set_value(&mut self, port: Port, data: u16) -> Result<(), Error<S, P>> {
        self.write_register(REG_DAC_DATA + (port as u8), data).await
    }

    async fn _adc_get_value(&mut self, port: Port) -> Result<u16, Error<S, P>> {
        self.read_register(REG_ADC_DATA + (port as u8)).await
    }
}

seq!(N in 0..=12 {
    impl<SPI, EN, S, P> ConfigurePort<ConfigMode~N, S, P> for Max11300<SPI, EN>
    where
        SPI: SpiBus<Error = S>,
        EN: OutputPin<Error = P>,
    {
        async fn configure_port(&mut self, port: Port, config: ConfigMode~N) -> Result<(), Error<S, P>> {
            self._configure_port(port, config.as_u16()).await?;
            self.channel_config[port.as_usize()] = Mode::Mode~N(config);
            Ok(())
        }
    }
});

pub struct ReadInterrupts<SPI: 'static, EN: 'static, M: RawMutex + 'static> {
    max: &'static Mutex<M, Max11300<SPI, EN>>,
}

impl<SPI, EN, M, S, P> ReadInterrupts<SPI, EN, M>
where
    SPI: SpiBus<Error = S>,
    EN: OutputPin<Error = P>,
    M: RawMutex,
{
    pub fn new(max: &'static Mutex<M, Max11300<SPI, EN>>) -> Self {
        Self { max }
    }

    /// Read the Interrupts from the Interrupt register
    pub async fn read(&self) -> Result<Interrupts, Error<S, P>> {
        let mut driver = self.max.lock().await;
        driver.int_read().await
    }

    /// Read the raw Interrupts from the Interrupt register
    pub async fn read_raw(&self) -> Result<u16, Error<S, P>> {
        let mut driver = self.max.lock().await;
        driver.int_read_raw().await
    }

    /// Read the GPI Status Interrupts from the GPIST register
    pub async fn read_gpist(&self) -> Result<u32, Error<S, P>> {
        let mut driver = self.max.lock().await;
        driver.int_read_gpist().await
    }
}

seq!(N in 0..20 {
    pub struct Ports<SPI: 'static, EN: 'static, M: RawMutex + 'static> {
        pub interrupts: ReadInterrupts<SPI, EN, M>,
        #(
            pub port~N: Mode0Port<SPI, EN, M>,
        )*
    }
});

seq!(N in 0..20 {
    impl<SPI, EN, M> Ports<SPI, EN, M>
    where
        SPI: SpiBus,
        EN: OutputPin,
        M: RawMutex,
    {
        pub fn new(max: &'static Mutex<M, Max11300<SPI, EN>>) -> Self {
            Self {
                interrupts: ReadInterrupts::new(max),
                #(
                    port~N: Mode0Port::new(Port::P~N, max),
                )*
            }
        }
    }
});

#[cfg(test)]
mod tests {
    use crate::{
        config::{ConfigMode1, ConfigMode5, ConfigMode7, ADCRANGE, AVR, DACRANGE, NSAMPLES},
        port::{IntoConfiguredPort, IntoMode},
    };

    use super::*;
    use embassy_sync::blocking_mutex::raw::NoopRawMutex;
    use embedded_hal_mock::eh1::spi::{Mock as SpiMock, Transaction as SpiTransaction};
    use embedded_hal_mock::{
        common::Generic,
        eh1::digital::{Mock as PinMock, State as PinState, Transaction as PinTransaction},
    };
    use static_cell::StaticCell;

    type MockMax = Max11300<Generic<SpiTransaction<u8>>, Generic<PinTransaction>>;

    static MAX0: StaticCell<Mutex<NoopRawMutex, MockMax>> = StaticCell::new();
    static MAX1: StaticCell<Mutex<NoopRawMutex, MockMax>> = StaticCell::new();
    static MAX2: StaticCell<Mutex<NoopRawMutex, MockMax>> = StaticCell::new();
    static MAX3: StaticCell<Mutex<NoopRawMutex, MockMax>> = StaticCell::new();

    #[tokio::test]
    async fn into_configured() {
        let config = DeviceConfig::default();
        let pin_expectations = [
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
        ];
        let spi_expectations = [
            // connection check
            SpiTransaction::transfer(vec![1], vec![0x0, 0x04, 0x24]),
            // write default configuration
            SpiTransaction::write_vec(vec![0x10 << 1, 0, 0]),
        ];
        let mut pin = PinMock::new(&pin_expectations);
        let mut spi = SpiMock::new(&spi_expectations);
        let _max = Max11300::try_new(spi.clone(), pin.clone(), config)
            .await
            .unwrap();
        pin.done();
        spi.done();
    }

    #[tokio::test]
    async fn config_modes() {
        let config = DeviceConfig::default();
        let pin_expectations = [
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
        ];
        let spi_expectations = [
            // connection check
            SpiTransaction::transfer(vec![1], vec![0x0, 0x04, 0x24]),
            // write default configuration
            SpiTransaction::write_vec(vec![0x10 << 1, 0, 0]),
            // configure port
            SpiTransaction::write_vec(vec![0x25 << 1, 16, 0]),
            // reconfigure port
            SpiTransaction::write_vec(vec![0x25 << 1, 113, 192]),
        ];
        let mut pin = PinMock::new(&pin_expectations);
        let mut spi = SpiMock::new(&spi_expectations);
        let max = Max11300::try_new(spi.clone(), pin.clone(), config)
            .await
            .unwrap();

        let max = MAX0.init(Mutex::new(max));
        let ports = Ports::new(max);

        // Configure port 5 for the first time
        let port5 = ports.port5.into_configured_port(ConfigMode1).await.unwrap();
        // Reconfigure port 5
        port5
            .into_mode(ConfigMode7(
                AVR::InternalRef,
                ADCRANGE::Rg0_10v,
                NSAMPLES::Samples64,
            ))
            .await
            .unwrap();
        pin.done();
        spi.done();
    }

    #[tokio::test]
    async fn config_mode_5() {
        let config = DeviceConfig::default();
        let pin_expectations = [
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
        ];
        let spi_expectations = [
            // connection check
            SpiTransaction::transfer(vec![1], vec![0x0, 0x04, 0x24]),
            // write default configuration
            SpiTransaction::write_vec(vec![0x10 << 1, 0, 0]),
            // configure port
            SpiTransaction::write_vec(vec![0x25 << 1, 81, 0]),
            // set value on port
            SpiTransaction::write_vec(vec![0x65 << 1, 0, 42]),
        ];
        let mut pin = PinMock::new(&pin_expectations);
        let mut spi = SpiMock::new(&spi_expectations);
        let max = Max11300::try_new(spi.clone(), pin.clone(), config)
            .await
            .unwrap();

        let max = MAX1.init(Mutex::new(max));
        let ports = Ports::new(max);

        let port5 = ports
            .port5
            .into_configured_port(ConfigMode5(DACRANGE::Rg0_10v))
            .await
            .unwrap();
        port5.set_value(42).await.unwrap();
        pin.done();
        spi.done();
    }

    #[tokio::test]
    async fn config_mode_7() {
        let config = DeviceConfig::default();
        let pin_expectations = [
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
        ];
        let spi_expectations = [
            // connection check
            SpiTransaction::transfer(vec![1], vec![0x0, 0x04, 0x24]),
            // write default configuration
            SpiTransaction::write_vec(vec![0x10 << 1, 0, 0]),
            // configure port
            SpiTransaction::write_vec(vec![0x25 << 1, 113, 192]),
            // read value on port
            SpiTransaction::transfer(vec![(0x45 << 1) | 1], vec![0x0, 0x1, 0x1]),
        ];
        let mut pin = PinMock::new(&pin_expectations);
        let mut spi = SpiMock::new(&spi_expectations);
        let max = Max11300::try_new(spi.clone(), pin.clone(), config)
            .await
            .unwrap();

        let max = MAX2.init(Mutex::new(max));
        let ports = Ports::new(max);

        let port5 = ports
            .port5
            .into_configured_port(ConfigMode7(
                AVR::InternalRef,
                ADCRANGE::Rg0_10v,
                NSAMPLES::Samples64,
            ))
            .await
            .unwrap();
        let val = port5.get_value().await.unwrap();
        assert_eq!(val, 257);
        pin.done();
        spi.done();
    }

    #[tokio::test]
    async fn config_mode_7_direct() {
        let config = DeviceConfig::default();
        let pin_expectations = [
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
        ];
        let spi_expectations = [
            // connection check
            SpiTransaction::transfer(vec![1], vec![0x0, 0x04, 0x24]),
            // write default configuration
            SpiTransaction::write_vec(vec![0x10 << 1, 0, 0]),
            // configure port
            SpiTransaction::write_vec(vec![0x25 << 1, 113, 192]),
            // read value on port
            SpiTransaction::transfer(vec![(0x45 << 1) | 1], vec![0x0, 0x1, 0x1]),
        ];
        let mut pin = PinMock::new(&pin_expectations);
        let mut spi = SpiMock::new(&spi_expectations);
        let mut max = Max11300::try_new(spi.clone(), pin.clone(), config)
            .await
            .unwrap();

        let port = Port::try_from(5).unwrap();
        max.configure_port(
            port,
            ConfigMode7(AVR::InternalRef, ADCRANGE::Rg0_10v, NSAMPLES::Samples64),
        )
        .await
        .unwrap();

        let val = max.adc_get_value(port).await.unwrap();

        assert_eq!(val, 257);
        pin.done();
        spi.done();
    }

    #[tokio::test]
    async fn multiport() {
        let config = DeviceConfig::default();
        let pin_expectations = [
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
            PinTransaction::set(PinState::Low),
            PinTransaction::set(PinState::High),
        ];
        let spi_expectations = [
            // connection check
            SpiTransaction::transfer(vec![1], vec![0x0, 0x04, 0x24]),
            // write default configuration
            SpiTransaction::write_vec(vec![0x10 << 1, 0, 0]),
            // configure ports
            SpiTransaction::write_vec(vec![0x25 << 1, 81, 0]),
            SpiTransaction::write_vec(vec![0x26 << 1, 81, 0]),
            // set value on port
            SpiTransaction::write_vec(vec![0x65 << 1, 0, 42, 0, 43]),
        ];
        let mut pin = PinMock::new(&pin_expectations);
        let mut spi = SpiMock::new(&spi_expectations);
        let max = Max11300::try_new(spi.clone(), pin.clone(), config)
            .await
            .unwrap();

        let max = MAX3.init(Mutex::new(max));
        let ports = Ports::new(max);

        let port5 = ports
            .port5
            .into_configured_port(ConfigMode5(DACRANGE::Rg0_10v))
            .await
            .unwrap();
        let port6 = ports
            .port6
            .into_configured_port(ConfigMode5(DACRANGE::Rg0_10v))
            .await
            .unwrap();
        let mut mp = Multiport::new([port5, port6]).unwrap();
        mp.set_values(&[42, 43]).await.unwrap();
        pin.done();
        spi.done();
    }
}