mcp4728 0.2.0

Platform agnostic rust driver for the MCP4728 4-channel, 12-bit I2C 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
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
//! # Rust driver for MCP4728 4-channel 12-bit I2C DAC
//!
//! This is a platform agnostic rust driver for the MCP4728 DAC using the
//! [embedded-hal](https://github.com/rust-embedded/embedded-hal) traits.
//!
//! This driver allows you to:
//!
//! - Write to a single channel and save to EEPROM
//! - Write to multiple channels and save to EEPROM
//! - Write to all channels, skipping some configuration bits and not saving to EEPROM ("fast
//!   write")
//! - Write voltage reference mode to all channels
//! - Write gain mode to all channels
//! - Write power down mode to all channels
//! - Read EEPROM and output registers for all channels
//! - Issue I2C General Call commands: reset, wake up, and software update
//!
//! There is one feature that this driver does **not** support - reading and writing the I2C address
//! of the device. Three bits of the driver can be set (from 0x60 to 0x67) but this is done by
//! toggling another pin in precise timing with the I2C message, which is not possible to do using
//! the [embedded-hal](https://github.com/rust-embedded/embedded-hal) traits.
//!
//! This crate is `#![no_std]` and does not contain unsafe code.
//!
//! ## Device
//!
//! The MCP4728 is a 4-channel, 12-bit digital-to-analog converter with nonvolatile memory (EEPROM)
//! for its output settings. The device will load its previous setting from EEPROM on startup
//! without receiving any commands. Each channel has separate voltage reference, gain, and power
//! down settings. Channels can be written individually or together.
//!
//! ### Datasheet
//!
//! [MCP4728](https://ww1.microchip.com/downloads/en/DeviceDoc/22187E.pdf)
//!
//! ## Features
//!
//! - `sync`: Adds support for the `embedded-hal`
//!   [`I2c` trait](https://docs.rs/embedded-hal/latest/embedded_hal/i2c/index.html)
//!   (enabled by default)
//! - `async`: Adds support for the `embedded-hal-async`
//!   [`I2c` trait](https://docs.rs/embedded-hal-async/1.0.0/embedded_hal_async/i2c/trait.I2c.html)
//! - `defmt`: Adds implementation of the `defmt`
//!   [`Format` trait](https://docs.rs/defmt/latest/defmt/trait.Format.html) to all types
//!
//! ## Usage
//!
//! ```no_run
//! # #[cfg(target_os = "linux")] {
//! use linux_embedded_hal::I2cdev;
//! use mcp4728::{MCP4728};
//!
//! let i2c = I2cdev::new("/dev/i2c-1").unwrap();
//! let mut dac = MCP4728::new(i2c, 0x60);
//! dac.fast_write(483, 279, 297, 590).unwrap();
//! # }
//! ```
//!
//! ### Updating the analog outputs
//!
//! Most commands write to the output register but do not necessarily update the analog output.
//! There are several ways to do so:
//!
//! - If `OutputEnableMode` is `Update`, the output will be updated after the last byte is received
//!   for each channel (not all commands can set this bit)
//! - If the LDAC pin is set low, the output will be updated after the last byte is received for
//!   each channel.
//! - If the LDAC pin transitions from high to low at any time, all channels will be updated.
//! - If a General Call Software Update command is received, all channels will be updated.
//!
//! ### Async
//!
//! This crate optionally supports async, behind the `async` feature. The API is otherwise identical.
//!
//! ```no_build
//! use mcp4728::{MCP4728Async};
//!
//! // Assume i2c implements embedded_hal_async::i2c::I2c.
//! let mut dac = MCP4728Async::new(i2c, 0x60);
//! dac.fast_write(483, 279, 297, 590).await.unwrap();
//! ```

#![cfg_attr(not(test), no_std)]

mod internal_types;
mod types;

use crate::internal_types::*;
pub use crate::types::*;

const ADDRESS_GENERAL_CALL: u8 = 0x00;
const COMMAND_GENERAL_CALL_RESET: u8 = 0b00000110;
const COMMAND_GENERAL_CALL_WAKE_UP: u8 = 0b00001001;
const COMMAND_GENERAL_CALL_SOFTWARE_UPDATE: u8 = 0b00001000;
const COMMAND_SINGLE_WRITE: u8 = 0b01011000;
const COMMAND_MULTI_WRITE: u8 = 0b01000000;
const COMMAND_SEQUENTIAL_WRITE: u8 = 0b01010000;
const COMMAND_WRITE_VOLTAGE_REFERENCE_MODE: u8 = 0b10000000;
const COMMAND_WRITE_GAIN_MODE: u8 = 0b11000000;
const COMMAND_WRITE_POWER_DOWN_MODE: u8 = 0b10100000;

/// MCP4728 4-channel 12-bit I2C DAC.
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[maybe_async_cfg::maybe(
    idents(MCP4728(sync, async = "MCP4728Async"),),
    sync(feature = "sync"),
    async(feature = "async")
)]
pub struct MCP4728<I> {
    i2c: I,
    address: u8,
}
/// Implementation of all commands given a generic I2CInterface.
///
/// # Errors
///
/// Any errors encountered within the I2C device will be wrapped in [`Error::I2CError`].
#[maybe_async_cfg::maybe(
    idents(
        MCP4728(sync, async = "MCP4728Async"),
        embedded_hal(sync, async = "embedded_hal_async"),
    ),
    sync(feature = "sync"),
    async(feature = "async")
)]
impl<I, E> MCP4728<I>
where
    I: embedded_hal::i2c::I2c<Error = E>,
{
    /// Creates a new [`MCP4728`] from an I2C device that implements the [`embedded_hal::i2c::I2c`]
    /// trait.
    pub fn new(i2c: I, address: u8) -> Self {
        MCP4728 { i2c, address }
    }

    async fn write_bytes(&mut self, address: u8, bytes: &[u8]) -> Result<(), Error<E>> {
        self.i2c
            .write(address, bytes)
            .await
            .map_err(Error::I2CError)
    }
    async fn write_iter<B>(&mut self, address: u8, bytes: B) -> Result<(), Error<E>>
    where
        B: IntoIterator<Item = u8>,
    {
        let mut buffer = [0; 12];
        let mut i = 0;
        for b in bytes {
            if i >= 12 {
                return Err(Error::WriteSizeExceeded);
            }
            buffer[i] = b;
            i += 1;
        }
        self.i2c
            .write(address, &buffer[0..i])
            .await
            .map_err(Error::I2CError)
    }

    /// Destroy this instance and return the inner I2C bus.
    pub fn release(self) -> I {
        self.i2c
    }
    /// Reads all registers of all channels from the device.
    ///
    /// Each channel includes both the values in EEPROM and in the input registers to the DAC, which
    /// might differ if e.g. fast_write has been used.
    pub async fn read(&mut self) -> Result<Registers, Error<E>> {
        let mut bytes = [0; 24];
        self.i2c.read(self.address, &mut bytes).await?;
        Ok(Registers {
            channel_a_input: Self::parse_bytes(&bytes[0..3]),
            channel_a_eeprom: Self::parse_bytes(&bytes[3..6]),
            channel_b_input: Self::parse_bytes(&bytes[6..9]),
            channel_b_eeprom: Self::parse_bytes(&bytes[9..12]),
            channel_c_input: Self::parse_bytes(&bytes[12..15]),
            channel_c_eeprom: Self::parse_bytes(&bytes[15..18]),
            channel_d_input: Self::parse_bytes(&bytes[18..21]),
            channel_d_eeprom: Self::parse_bytes(&bytes[21..24]),
        })
    }

    /// Issues a general call command (address 0x00) to reset the device.  All MCP4728 devices on
    /// the bus will load the values from EEPROM into the output registers and update the output
    /// voltage.
    pub async fn general_call_reset(&mut self) -> Result<(), Error<E>> {
        self.write_bytes(ADDRESS_GENERAL_CALL, &[COMMAND_GENERAL_CALL_RESET])
            .await
    }

    /// Issues a general call command (address 0x00) to wake up the device.  All MCP4728 devices on
    /// the bus will reset the power down bits and turn on all channels.
    pub async fn general_call_wake_up(&mut self) -> Result<(), Error<E>> {
        self.write_bytes(ADDRESS_GENERAL_CALL, &[COMMAND_GENERAL_CALL_WAKE_UP])
            .await
    }

    /// Issues a general call command (address 0x00) to update software.  All MCP4728 devices on the
    /// bus will immediately update the output voltage.
    pub async fn general_call_software_update(&mut self) -> Result<(), Error<E>> {
        self.write_bytes(
            ADDRESS_GENERAL_CALL,
            &[COMMAND_GENERAL_CALL_SOFTWARE_UPDATE],
        )
        .await
    }

    /// Updates the values of all four channels and sets them to be powered on (i.e.
    /// [`PowerDownMode::Normal`] is set).
    ///
    /// This command writes to the output registers directly and does not affect the EEPROM.  The
    /// voltage reference mode and gain mode are not affected.
    ///
    /// # Updating the analog outputs
    ///
    /// This command writes to the input register but does not necessarily update the analog output.
    /// There are several ways to do so:
    ///
    ///   - If the LDAC pin is set low, the output will be updated after the last byte is received
    ///     for each channel.
    ///   - If the LDAC pin transitions from high to low at any time, all channels will be updated.
    ///   - If a General Call Software Update command is received, all channels will be updated.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(target_os = "linux")] {
    /// use linux_embedded_hal::I2cdev;
    /// use mcp4728::{MCP4728};
    ///
    /// let i2c = I2cdev::new("/dev/i2c-1").unwrap();
    /// let mut dac = MCP4728::new(i2c, 0x60);
    /// dac.fast_write(483, 279, 297, 590).unwrap();
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// In addition to the internal I2C errors, this can return [`Error::ValueOutOfBounds`] if the
    /// value is out of range (greater than 4095).
    pub async fn fast_write(
        &mut self,
        val_a: u16,
        val_b: u16,
        val_c: u16,
        val_d: u16,
    ) -> Result<(), Error<E>> {
        self.fast_write_with_power_down_mode(
            (PowerDownMode::Normal, val_a),
            (PowerDownMode::Normal, val_b),
            (PowerDownMode::Normal, val_c),
            (PowerDownMode::Normal, val_d),
        )
        .await
    }

    /// Updates the values of all four channels and sets their corresponding [`PowerDownMode`]s.
    ///
    /// This command writes to the output registers directly and does not affect the EEPROM.  The
    /// voltage reference mode and gain mode are not affected.
    ///
    /// # Updating the analog outputs
    ///
    /// This command writes to the input register but does not necessarily update the analog output.
    /// There are several ways to do so:
    ///
    ///   - If the LDAC pin is set low, the output will be updated after the last byte is received
    ///     for each channel.
    ///   - If the LDAC pin transitions from high to low at any time, all channels will be updated.
    ///   - If a General Call Software Update command is received, all channels will be updated.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(target_os = "linux")] {
    /// use linux_embedded_hal::I2cdev;
    /// use mcp4728::{MCP4728, PowerDownMode};
    ///
    /// let i2c = I2cdev::new("/dev/i2c-1").unwrap();
    /// let mut dac = MCP4728::new(i2c, 0x60);
    /// dac.fast_write_with_power_down_mode(
    ///     (PowerDownMode::Normal, 100),
    ///     (PowerDownMode::Normal, 200),
    ///     (PowerDownMode::PowerDownOneK, 0),
    ///     (PowerDownMode::PowerDownOneK, 0),
    /// ).unwrap();
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// In addition to the internal I2C errors, this can return [`Error::ValueOutOfBounds`] if the
    /// value is out of range (greater than 4095).
    pub async fn fast_write_with_power_down_mode(
        &mut self,
        val_a: (PowerDownMode, u16),
        val_b: (PowerDownMode, u16),
        val_c: (PowerDownMode, u16),
        val_d: (PowerDownMode, u16),
    ) -> Result<(), Error<E>> {
        let mut bytes = [0; 8];
        for (i, &(mode, val)) in [val_a, val_b, val_c, val_d].iter().enumerate() {
            if val > 0x0fff {
                return Err(Error::ValueOutOfBounds(val));
            }
            bytes[2 * i] = (mode as u8) << 4 | val.to_be_bytes()[0];
            bytes[2 * i + 1] = val.to_be_bytes()[1];
        }
        self.write_bytes(self.address, &bytes).await
    }
    /// Updates all bits of a single channel to both the DAC input register and EEPROM.
    ///
    /// This sets the voltage reference mode, power down mode, gain mode, and value of the channel.
    ///
    /// # Updating the analog outputs
    ///
    /// This command writes to the input register but does not necessarily update the analog output.
    /// There are several ways to do so:
    ///
    ///   - If `OutputEnableMode` is `Update`, the output will be updated after the last byte is
    ///     received.
    ///   - If the LDAC pin is set low, the output will be updated after the last byte is received.
    ///   - If the LDAC pin transitions from high to low at any time, all channels will be updated.
    ///   - If a General Call Software Update command is received, all channels will be updated.
    ///
    /// # Errors
    ///
    /// In addition to the internal I2C errors, this can return [`Error::ValueOutOfBounds`] if the
    /// value is out of range (greater than 4095).
    pub async fn single_write(
        &mut self,
        channel: Channel,
        output_enable_mode: OutputEnableMode,
        channel_state: &ChannelState,
    ) -> Result<(), Error<E>> {
        // || 0 1 0 1 1 CH CH OE || VR PD PD G D D D D || D D D D D D D D ||
        // CH = Channel select
        // OE = Output enable
        // VR = Voltage reference mode
        // PD = Power down mode
        // G = Gain mode

        if channel_state.value > 0x0fff {
            return Err(Error::ValueOutOfBounds(channel_state.value));
        }
        let mut bytes = [0; 3];
        bytes[0] = COMMAND_SINGLE_WRITE | (channel as u8) << 1 | output_enable_mode as u8;
        bytes[1] = (channel_state.voltage_reference_mode as u8) << 7
            | (channel_state.power_down_mode as u8) << 5
            | (channel_state.gain_mode as u8) << 4
            | channel_state.value.to_be_bytes()[0];
        bytes[2] = channel_state.value.to_be_bytes()[1];
        self.write_bytes(self.address, &bytes).await
    }

    /// Updates all bits of 1-4 sequential channels to both the DAC input registers and EEPROM.
    ///
    /// This sets the voltage reference mode, power down mode, gain mode, and value of the channels.
    /// The channels including and after `starting_channel` will be updated (e.g. if
    /// `starting_channel` is channel b, then channels b, c and d will be updated). The number of
    /// entries in `channel_updates` must equal the number of channels that will be updated.
    ///
    /// # Updating the analog outputs
    ///
    /// This command writes to the input register but does not necessarily update the analog output.
    /// There are several ways to do so:
    ///
    ///   - If `OutputEnableMode` is `Update`, the output will be updated after the last byte is
    ///     received for each channel.
    ///   - If the LDAC pin is set low, the output will be updated after the last byte is received
    ///     for each channel.
    ///   - If the LDAC pin transitions from high to low at any time, all channels will be updated.
    ///   - If a General Call Software Update command is received, all channels will be updated.
    ///
    /// # Errors
    ///
    /// In addition to the internal I2C errors, this can return [`Error::ValueOutOfBounds`] if any
    /// value is out of range (greater than 4095) and
    /// [`Error::StartingChannelMismatch`] if there is a mismatch between the starting channel and
    /// the number of updates.
    pub async fn sequential_write(
        &mut self,
        starting_channel: Channel,
        output_enable_mode: OutputEnableMode,
        channel_updates: &[ChannelState],
    ) -> Result<(), Error<E>> {
        let expected_updates = 4 - starting_channel as usize;
        if channel_updates.len() != expected_updates {
            return Err(Error::StartingChannelMismatch);
        }
        let mut is_first_byte = true;
        let mut channel_index = 0;
        let mut byte_index = SequentialWriteByteIndex::Zero;
        let generator = core::iter::from_fn(move || {
            if channel_index >= channel_updates.len() {
                return None;
            }
            let channel_state = channel_updates.get(channel_index).unwrap();
            let byte;
            if is_first_byte {
                byte = COMMAND_SEQUENTIAL_WRITE
                    | (starting_channel as u8) << 1
                    | output_enable_mode as u8;
                is_first_byte = false;
            } else {
                byte = match byte_index {
                    SequentialWriteByteIndex::Zero => {
                        (channel_state.voltage_reference_mode as u8) << 7
                            | (channel_state.power_down_mode as u8) << 5
                            | (channel_state.gain_mode as u8) << 4
                            | channel_state.value.to_be_bytes()[0]
                    }

                    SequentialWriteByteIndex::One => channel_state.value.to_be_bytes()[1],
                };
                byte_index = byte_index.next();
                if byte_index == SequentialWriteByteIndex::Zero {
                    channel_index += 1;
                }
            }
            Some(byte)
        });

        self.write_iter(self.address, generator).await
    }

    /// Updates all bits of multiple channels to both the DAC input registers and EEPROM.
    ///
    /// This sets the voltage reference mode, power down mode, gain mode, and value of the channels.
    ///
    /// # Updating the analog outputs
    ///
    /// This command writes to the input register but does not necessarily update the analog output.
    /// There are several ways to do so:
    ///
    ///   - If `OutputEnableMode` is `Update`, the output will be updated after the last byte is
    ///     received for each channel.
    ///   - If the LDAC pin is set low, the output will be updated after the last byte is received
    ///     for each channel.
    ///   - If the LDAC pin transitions from high to low at any time, all channels will be updated.
    ///   - If a General Call Software Update command is received, all channels will be updated.
    ///
    /// # Errors
    ///
    /// In addition to the internal I2C errors, this can return [`Error::ValueOutOfBounds`] if any
    /// value is out of range (greater than 4095).
    ///
    /// This function can write an arbitrary number of updates, so it is impossible to statically
    /// allocate a buffer to write using the [`embedded_hal::i2c::I2c`] trait.  To work around this,
    /// we will use a buffer large enough to contain four writes at a time and return
    /// [`Error::WriteSizeExceeded`] if more writes are requested.  This is unlikely to be a
    /// limitation given that there are four channels.
    pub async fn multi_write(
        &mut self,
        channel_updates: &[(Channel, OutputEnableMode, ChannelState)],
    ) -> Result<(), Error<E>> {
        let mut channel_index = 0;
        let mut byte_index = MultiWriteByteIndex::Zero;
        let generator = core::iter::from_fn(move || {
            if channel_index >= channel_updates.len() {
                return None;
            }
            let (channel, output_enable_mode, channel_state) =
                channel_updates.get(channel_index).unwrap();
            let byte = match byte_index {
                MultiWriteByteIndex::Zero => {
                    COMMAND_MULTI_WRITE | (*channel as u8) << 1 | *output_enable_mode as u8
                }

                MultiWriteByteIndex::One => {
                    (channel_state.voltage_reference_mode as u8) << 7
                        | (channel_state.power_down_mode as u8) << 5
                        | (channel_state.gain_mode as u8) << 4
                        | channel_state.value.to_be_bytes()[0]
                }

                MultiWriteByteIndex::Two => channel_state.value.to_be_bytes()[1],
            };
            byte_index = byte_index.next();
            if byte_index == MultiWriteByteIndex::Zero {
                channel_index += 1;
            }
            Some(byte)
        });

        self.write_iter(self.address, generator).await
    }

    /// Writes only the voltage reference mode bits for all channels.
    ///
    /// The EEPROM data is not affected and the output of each channel is updated after the command
    /// has been received.
    pub async fn write_voltage_reference_mode(
        &mut self,
        mode_a: VoltageReferenceMode,
        mode_b: VoltageReferenceMode,
        mode_c: VoltageReferenceMode,
        mode_d: VoltageReferenceMode,
    ) -> Result<(), Error<E>> {
        let byte = COMMAND_WRITE_VOLTAGE_REFERENCE_MODE
            | (mode_a as u8) << 3
            | (mode_b as u8) << 2
            | (mode_c as u8) << 1
            | mode_d as u8;
        self.write_bytes(self.address, &[byte]).await
    }

    /// Writes only the gain mode bits for all channels.
    ///
    /// The EEPROM data is not affected and the output of each channel is updated after the command
    /// has been received.
    pub async fn write_gain_mode(
        &mut self,
        mode_a: GainMode,
        mode_b: GainMode,
        mode_c: GainMode,
        mode_d: GainMode,
    ) -> Result<(), Error<E>> {
        let byte = COMMAND_WRITE_GAIN_MODE
            | (mode_a as u8) << 3
            | (mode_b as u8) << 2
            | (mode_c as u8) << 1
            | mode_d as u8;
        self.write_bytes(self.address, &[byte]).await
    }

    /// Writes only the power down mode bits for all channels.
    ///
    /// The EEPROM data is not affected and the output of each channel is updated after the command
    /// has been received.
    pub async fn write_power_down_mode(
        &mut self,
        mode_a: PowerDownMode,
        mode_b: PowerDownMode,
        mode_c: PowerDownMode,
        mode_d: PowerDownMode,
    ) -> Result<(), Error<E>> {
        let mut bytes = [0; 2];
        bytes[0] = COMMAND_WRITE_POWER_DOWN_MODE | (mode_a as u8) << 2 | mode_b as u8;
        bytes[1] = (mode_c as u8) << 6 | (mode_d as u8) << 4;
        self.write_bytes(self.address, &bytes).await
    }

    fn parse_bytes(bytes: &[u8]) -> ChannelRegisters {
        ChannelRegisters {
            channel_state: ChannelState {
                voltage_reference_mode: VoltageReferenceMode::try_from(
                    (bytes[1] & 0b10000000) >> 7,
                )
                .unwrap(),
                power_down_mode: PowerDownMode::try_from((bytes[1] & 0b01100000) >> 5).unwrap(),
                gain_mode: GainMode::try_from((bytes[1] & 0b00010000) >> 4).unwrap(),
                value: u16::from_be_bytes([bytes[1] & 0b00001111, bytes[2]]),
            },
            ready_state: ReadyState::try_from((bytes[0] & 0b10000000) >> 7).unwrap(),
            power_state: PowerState::try_from((bytes[0] & 0b01000000) >> 6).unwrap(),
        }
    }
}

#[cfg(test)]
#[maybe_async_cfg::maybe(
    idents(MCP4728(sync, async = "MCP4728Async"),),
    sync(feature = "sync"),
    async(feature = "async")
)]
mod tests {
    use embedded_hal::i2c::ErrorKind;
    use embedded_hal_mock::eh1::i2c::{Mock as MockI2C, Transaction};
    #[maybe_async_cfg::only_if(sync)]
    use test as maybe_async_test;
    #[maybe_async_cfg::only_if(async)]
    use tokio::test as maybe_async_test;

    use crate::*;

    #[maybe_async_test]
    async fn fast_write() {
        let expectations = [Transaction::write(
            0x60,
            vec![0x0a, 0xaa, 0x00, 0x00, 0x0a, 0xaa, 0x00, 0x00],
        )];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728.fast_write(0x0aaa, 0x0000, 0x0aaa, 0x0000).await,
            Ok(())
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }
    #[maybe_async_test]
    async fn fast_write_i2c_error() {
        let expectations =
            [
                Transaction::write(0x60, vec![0x0a, 0xaa, 0x00, 0x00, 0x0a, 0xaa, 0x00, 0x00])
                    .with_error(ErrorKind::Other),
            ];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728.fast_write(0x0aaa, 0x0000, 0x0aaa, 0x0000).await,
            Err(Error::I2CError(ErrorKind::Other))
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn fast_write_out_of_bounds_error() {
        let expectations = [];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728.fast_write(0x1000, 0x0000, 0x0000, 0x0000).await,
            Err(Error::ValueOutOfBounds(0x1000))
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn fast_write_with_power_down_mode() {
        let expectations = [Transaction::write(
            0x60,
            vec![0x0a, 0xaa, 0x10, 0x00, 0x2a, 0xaa, 0x30, 0x00],
        )];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .fast_write_with_power_down_mode(
                    (PowerDownMode::Normal, 0x0aaa),
                    (PowerDownMode::PowerDownOneK, 0x0000),
                    (PowerDownMode::PowerDownOneHundredK, 0x0aaa),
                    (PowerDownMode::PowerDownFiveHundredK, 0x0000),
                )
                .await,
            Ok(())
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    // || 0 1 0 1 1 CH CH OE || VR PD PD G D D D D || D D D D D D D D ||
    #[maybe_async_test]
    async fn single_write_default_values() {
        let expectations = [Transaction::write(
            0x60,
            vec![0b01011010, 0b10001010, 0b10101010],
        )];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .single_write(
                    Channel::B,
                    OutputEnableMode::Update,
                    &ChannelState::new().value(0x0aaa)
                )
                .await,
            Ok(())
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn single_write_set_all_values() {
        let expectations = [Transaction::write(
            0x60,
            vec![0b01011111, 0b11111111, 0b11111111],
        )];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .single_write(
                    Channel::D,
                    OutputEnableMode::NoUpdate,
                    &ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::Internal)
                        .power_down_mode(PowerDownMode::PowerDownFiveHundredK)
                        .gain_mode(GainMode::TimesTwo)
                        .value(0x0fff)
                )
                .await,
            Ok(())
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn single_write_out_of_bounds_error() {
        let expectations = [];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .single_write(
                    Channel::B,
                    OutputEnableMode::NoUpdate,
                    &ChannelState::new().value(0xffff)
                )
                .await,
            Err(Error::ValueOutOfBounds(0xffff))
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn multi_write_set_all_values() {
        let expectations = [Transaction::write(
            0x60,
            vec![0b01000111, 0b11111111, 0b11111111],
        )];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .multi_write(&[(
                    Channel::D,
                    OutputEnableMode::NoUpdate,
                    ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::Internal)
                        .power_down_mode(PowerDownMode::PowerDownFiveHundredK)
                        .gain_mode(GainMode::TimesTwo)
                        .value(0x0fff)
                )])
                .await,
            Ok(())
        );

        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn multi_write_multiple_values() {
        let expectations = [Transaction::write(
            0x60,
            vec![
                0b01000000, 0b00000000, 0b00000001, 0b01000010, 0b00000000, 0b00000010,
            ],
        )];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .multi_write(&[
                    (
                        Channel::A,
                        OutputEnableMode::Update,
                        ChannelState::new()
                            .voltage_reference_mode(VoltageReferenceMode::External)
                            .value(0x0001)
                    ),
                    (
                        Channel::B,
                        OutputEnableMode::Update,
                        ChannelState::new()
                            .voltage_reference_mode(VoltageReferenceMode::External)
                            .value(0x0002)
                    )
                ])
                .await,
            Ok(())
        );

        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn sequential_write_multiple_values() {
        let expectations = [Transaction::write(
            0x60,
            vec![
                0b01010000, 0b10000000, 0b00000001, 0b10000000, 0b00000010, 0b10000000, 0b00000011,
                0b10000000, 0b00000100,
            ],
        )];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .sequential_write(
                    Channel::A,
                    OutputEnableMode::Update,
                    &[
                        ChannelState::new().value(0x0001),
                        ChannelState::new().value(0x0002),
                        ChannelState::new().value(0x0003),
                        ChannelState::new().value(0x0004),
                    ]
                )
                .await,
            Ok(())
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn sequential_write_too_many_values() {
        let expectations = [];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .sequential_write(
                    Channel::B,
                    OutputEnableMode::Update,
                    &[
                        ChannelState::new().value(0x0001),
                        ChannelState::new().value(0x0002),
                        ChannelState::new().value(0x0003),
                        ChannelState::new().value(0x0004),
                    ]
                )
                .await,
            Err(Error::StartingChannelMismatch)
        );

        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn write_voltage_reference_mode() {
        let expectations = [Transaction::write(0x60, vec![0b10000101])];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .write_voltage_reference_mode(
                    VoltageReferenceMode::External,
                    VoltageReferenceMode::Internal,
                    VoltageReferenceMode::External,
                    VoltageReferenceMode::Internal,
                )
                .await,
            Ok(())
        );

        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn write_gain_mode() {
        let expectations = [Transaction::write(0x60, vec![0b11000101])];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .write_gain_mode(
                    GainMode::TimesOne,
                    GainMode::TimesTwo,
                    GainMode::TimesOne,
                    GainMode::TimesTwo,
                )
                .await,
            Ok(())
        );

        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn write_power_down_mode() {
        let expectations = [Transaction::write(0x60, vec![0b10100001, 0b10110000])];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728
                .write_power_down_mode(
                    PowerDownMode::Normal,
                    PowerDownMode::PowerDownOneK,
                    PowerDownMode::PowerDownOneHundredK,
                    PowerDownMode::PowerDownFiveHundredK,
                )
                .await,
            Ok(())
        );

        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn read() {
        #[rustfmt::skip]
            let bytes = vec![
                0b10000000, 0b00000000, 0b00000000,
                0b01000000, 0b11111111, 0b11111111,
                0b11000000, 0b01010101, 0b01010101,
                0b10000000, 0b00000000, 0b00000000,
                0b10000000, 0b00000000, 0b00000000,
                0b10000000, 0b00000000, 0b00000000,
                0b10000000, 0b00000000, 0b00000000,
                0b10000000, 0b00000000, 0b00000000,
            ];
        let expectations = [Transaction::read(0x60, bytes)];
        let mut i2c = MockI2C::new(&expectations);
        let mut mcp4728 = MCP4728::new(i2c, 0x60);

        assert_eq!(
            mcp4728.read().await,
            Ok(Registers {
                channel_a_input: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::External),
                    ready_state: ReadyState::Ready,
                    power_state: PowerState::Off
                },
                channel_a_eeprom: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::Internal)
                        .power_down_mode(PowerDownMode::PowerDownFiveHundredK)
                        .gain_mode(GainMode::TimesTwo)
                        .value(0x0fff),
                    ready_state: ReadyState::Busy,
                    power_state: PowerState::On
                },
                channel_b_input: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::External)
                        .power_down_mode(PowerDownMode::PowerDownOneHundredK)
                        .gain_mode(GainMode::TimesTwo)
                        .value(0x0555),
                    ready_state: ReadyState::Ready,
                    power_state: PowerState::On
                },
                channel_b_eeprom: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::External),
                    ready_state: ReadyState::Ready,
                    power_state: PowerState::Off
                },
                channel_c_input: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::External),
                    ready_state: ReadyState::Ready,
                    power_state: PowerState::Off
                },
                channel_c_eeprom: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::External),
                    ready_state: ReadyState::Ready,
                    power_state: PowerState::Off
                },
                channel_d_input: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::External),
                    ready_state: ReadyState::Ready,
                    power_state: PowerState::Off
                },
                channel_d_eeprom: ChannelRegisters {
                    channel_state: ChannelState::new()
                        .voltage_reference_mode(VoltageReferenceMode::External),
                    ready_state: ReadyState::Ready,
                    power_state: PowerState::Off
                },
            })
        );
        i2c = mcp4728.release();

        i2c.done(); // Verify expectations.
    }

    #[maybe_async_test]
    async fn read_fuzz() {
        for i in 0..=u8::MAX {
            let bytes = vec![i; 24];
            let expectations = [Transaction::read(0x60, bytes)];
            let mut i2c = MockI2C::new(&expectations);
            let mut mcp4728 = MCP4728::new(i2c, 0x60);

            assert_eq!(mcp4728.read().await.is_ok(), true);
            i2c = mcp4728.release();

            i2c.done(); // Verify expectations.
        }
    }
}