1#![no_std]
2#![doc = include_str!("../README.md")]
3
4use device_driver::{AsyncRegisterInterface, FieldsetMetadata, RegisterInterfaceBase};
5use embedded_hal_async::{i2c::I2c, spi::SpiDevice};
6
7mod sealed {
10 pub trait Chip {}
11 pub trait ChannelMode {}
12}
13
14pub trait Chip: sealed::Chip {}
16
17pub struct Tla252x;
21
22pub struct Ads7x38;
25
26pub struct Ads7x28;
29
30impl sealed::Chip for Tla252x {}
31impl sealed::Chip for Ads7x38 {}
32impl sealed::Chip for Ads7x28 {}
33impl Chip for Tla252x {}
34impl Chip for Ads7x38 {}
35impl Chip for Ads7x28 {}
36
37pub trait HasStats: Chip {}
40impl HasStats for Ads7x38 {}
41impl HasStats for Ads7x28 {}
42
43pub trait HasRmsZcd: HasStats {}
46impl HasRmsZcd for Ads7x28 {}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52#[cfg_attr(feature = "defmt", derive(defmt::Format))]
53pub enum ZcdGpoValue {
54 Rise0Fall0 = 0,
55 Rise0Fall1 = 1,
56 Rise1Fall0 = 2,
57 Rise1Fall1 = 3,
58}
59
60impl From<u8> for ZcdGpoValue {
61 fn from(v: u8) -> Self {
62 match v & 0x3 {
63 0 => Self::Rise0Fall0,
64 1 => Self::Rise0Fall1,
65 2 => Self::Rise1Fall0,
66 _ => Self::Rise1Fall1,
67 }
68 }
69}
70
71impl From<ZcdGpoValue> for u8 {
72 fn from(v: ZcdGpoValue) -> Self {
73 v as u8
74 }
75}
76
77device_driver::compile!(
80 options: "--rust-defmt-feature=defmt",
81 manifest: "device.ddsl",
82);
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
89#[cfg_attr(feature = "defmt", derive(defmt::Format))]
90pub enum Address {
91 X10 = 0x10,
93 X11 = 0x11,
95 X12 = 0x12,
97 X13 = 0x13,
99 X14 = 0x14,
101 X15 = 0x15,
103 X16 = 0x16,
105 X17 = 0x17,
107}
108
109const OPCODE_READ: u8 = 0x10;
112const OPCODE_WRITE: u8 = 0x08;
113const REG_MANUAL_CH_SEL: u8 = 0x11;
115
116pub struct I2cInterface<BUS> {
118 i2c: BUS,
119 addr: Address,
120}
121
122impl<BUS: I2c> RegisterInterfaceBase for I2cInterface<BUS> {
123 type Error = BUS::Error;
124 type AddressType = u8;
125}
126
127impl<BUS: I2c> AsyncRegisterInterface for I2cInterface<BUS> {
128 async fn write_register(
129 &mut self,
130 address: u8,
131 data: &mut [u8],
132 _metadata: &FieldsetMetadata,
133 ) -> Result<(), Self::Error> {
134 self.i2c
135 .write(self.addr as u8, &[OPCODE_WRITE, address, data[0]])
136 .await
137 }
138
139 async fn read_register(
140 &mut self,
141 address: u8,
142 data: &mut [u8],
143 _metadata: &FieldsetMetadata,
144 ) -> Result<(), Self::Error> {
145 self.i2c
146 .write_read(self.addr as u8, &[OPCODE_READ, address], data)
147 .await
148 }
149}
150
151pub struct SpiInterface<SPI> {
153 spi: SPI,
154}
155
156impl<SPI: SpiDevice> RegisterInterfaceBase for SpiInterface<SPI> {
157 type Error = SPI::Error;
158 type AddressType = u8;
159}
160
161impl<SPI: SpiDevice> AsyncRegisterInterface for SpiInterface<SPI> {
162 async fn write_register(
163 &mut self,
164 address: u8,
165 data: &mut [u8],
166 _metadata: &FieldsetMetadata,
167 ) -> Result<(), Self::Error> {
168 self.spi.write(&[OPCODE_WRITE, address, data[0]]).await
170 }
171
172 async fn read_register(
173 &mut self,
174 address: u8,
175 data: &mut [u8],
176 _metadata: &FieldsetMetadata,
177 ) -> Result<(), Self::Error> {
178 self.spi.write(&[OPCODE_READ, address, 0x00]).await?;
181 self.spi.read(&mut data[..1]).await
183 }
184}
185
186#[allow(async_fn_in_trait)]
204pub trait CanConvert: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface {
205 async fn read_conversion_raw(&mut self, channel: u8) -> Result<u16, Self::Error>;
207
208 async fn read_sequence_raw(&mut self, out: &mut [u8]) -> Result<(), Self::Error>;
216}
217
218impl<BUS: I2c> CanConvert for I2cInterface<BUS> {
219 async fn read_conversion_raw(&mut self, channel: u8) -> Result<u16, BUS::Error> {
220 self.write_register(
222 REG_MANUAL_CH_SEL,
223 &mut [channel & 0x0F],
224 &device_driver::FieldsetMetadata::DEFAULT,
225 )
226 .await?;
227 let mut buf = [0u8; 2];
229 self.i2c.read(self.addr as u8, &mut buf).await?;
230 Ok((buf[0] as u16) << 8 | buf[1] as u16)
231 }
232
233 async fn read_sequence_raw(&mut self, out: &mut [u8]) -> Result<(), BUS::Error> {
234 self.i2c.read(self.addr as u8, out).await
237 }
238}
239
240impl<SPI: SpiDevice> CanConvert for SpiInterface<SPI> {
241 async fn read_conversion_raw(&mut self, channel: u8) -> Result<u16, SPI::Error> {
242 let mut buf = [(channel & 0x1F) << 3, 0x00];
245 self.spi.transfer_in_place(&mut buf).await?;
246 Ok((buf[0] as u16) << 8 | buf[1] as u16)
247 }
248
249 async fn read_sequence_raw(&mut self, out: &mut [u8]) -> Result<(), SPI::Error> {
250 self.spi.transfer_in_place(out).await
252 }
253}
254
255pub struct Unconfigured;
259pub struct AnalogIn;
261pub struct DigitalIn;
263pub struct DigitalOut<DRIVE>(core::marker::PhantomData<DRIVE>);
265pub struct OpenDrain;
267pub struct PushPull;
269
270pub trait ChannelMode: sealed::ChannelMode {
278 const IS_ANALOG: bool;
280}
281
282impl sealed::ChannelMode for Unconfigured {}
283impl ChannelMode for Unconfigured {
284 const IS_ANALOG: bool = false;
285}
286impl sealed::ChannelMode for AnalogIn {}
287impl ChannelMode for AnalogIn {
288 const IS_ANALOG: bool = true;
289}
290impl sealed::ChannelMode for DigitalIn {}
291impl ChannelMode for DigitalIn {
292 const IS_ANALOG: bool = false;
293}
294impl<DRIVE> sealed::ChannelMode for DigitalOut<DRIVE> {}
295impl<DRIVE> ChannelMode for DigitalOut<DRIVE> {
296 const IS_ANALOG: bool = false;
297}
298
299pub struct Driver<
307 IF,
308 CHIP,
309 C0 = Unconfigured,
310 C1 = Unconfigured,
311 C2 = Unconfigured,
312 C3 = Unconfigured,
313 C4 = Unconfigured,
314 C5 = Unconfigured,
315 C6 = Unconfigured,
316 C7 = Unconfigured,
317> {
318 pub device: Device<IF>,
319 _chip: core::marker::PhantomData<CHIP>,
320 #[allow(clippy::type_complexity)]
325 _channels: core::marker::PhantomData<(C0, C1, C2, C3, C4, C5, C6, C7)>,
326}
327
328impl<BUS: I2c, CHIP: Chip> Driver<I2cInterface<BUS>, CHIP> {
329 pub fn new(i2c: BUS, addr: Address) -> Self {
331 Self {
332 device: Device::new(I2cInterface { i2c, addr }),
333 _chip: core::marker::PhantomData,
334 _channels: core::marker::PhantomData,
335 }
336 }
337}
338
339impl<SPI: SpiDevice, CHIP: Chip> Driver<SpiInterface<SPI>, CHIP> {
340 pub fn new(spi: SPI) -> Self {
342 Self {
343 device: Device::new(SpiInterface { spi }),
344 _chip: core::marker::PhantomData,
345 _channels: core::marker::PhantomData,
346 }
347 }
348}
349
350#[must_use = "staged channel configuration does nothing until `.apply()` is called"]
369pub struct ConfigureChannels<
370 IF,
371 CHIP,
372 C0 = Unconfigured,
373 C1 = Unconfigured,
374 C2 = Unconfigured,
375 C3 = Unconfigured,
376 C4 = Unconfigured,
377 C5 = Unconfigured,
378 C6 = Unconfigured,
379 C7 = Unconfigured,
380> {
381 device: Device<IF>,
382 pin_cfg_value: u8,
383 pin_cfg_mask: u8,
384 gpio_cfg_value: u8,
385 gpio_cfg_mask: u8,
386 gpo_drive_value: u8,
387 gpo_drive_mask: u8,
388 _chip: core::marker::PhantomData<CHIP>,
389 #[allow(clippy::type_complexity)]
394 _channels: core::marker::PhantomData<(C0, C1, C2, C3, C4, C5, C6, C7)>,
395}
396
397impl<IF, CHIP: Chip, C0, C1, C2, C3, C4, C5, C6, C7>
398 Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
399{
400 pub fn configure(self) -> ConfigureChannels<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7> {
403 ConfigureChannels {
404 device: self.device,
405 pin_cfg_value: 0,
406 pin_cfg_mask: 0,
407 gpio_cfg_value: 0,
408 gpio_cfg_mask: 0,
409 gpo_drive_value: 0,
410 gpo_drive_mask: 0,
411 _chip: core::marker::PhantomData,
412 _channels: core::marker::PhantomData,
413 }
414 }
415}
416
417impl<
418 IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
419 CHIP: Chip,
420 C0,
421 C1,
422 C2,
423 C3,
424 C4,
425 C5,
426 C6,
427 C7,
428> ConfigureChannels<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
429{
430 pub async fn apply(
433 mut self,
434 ) -> Result<Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>, IF::Error> {
435 let (mask, value) = (self.pin_cfg_mask, self.pin_cfg_value);
436 if mask != 0 {
437 self.device
438 .pin_cfg()
439 .modify_async(|r| r.set_pin_cfg((r.pin_cfg() & !mask) | (value & mask)))
440 .await?;
441 }
442 let (mask, value) = (self.gpio_cfg_mask, self.gpio_cfg_value);
443 if mask != 0 {
444 self.device
445 .gpio_cfg()
446 .modify_async(|r| r.set_gpio_cfg((r.gpio_cfg() & !mask) | (value & mask)))
447 .await?;
448 }
449 let (mask, value) = (self.gpo_drive_mask, self.gpo_drive_value);
450 if mask != 0 {
451 self.device
452 .gpo_drive_cfg()
453 .modify_async(|r| r.set_gpo_drive_cfg((r.gpo_drive_cfg() & !mask) | (value & mask)))
454 .await?;
455 }
456 Ok(Driver {
457 device: self.device,
458 _chip: core::marker::PhantomData,
459 _channels: core::marker::PhantomData,
460 })
461 }
462}
463
464impl<
467 IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
468 CHIP: Chip,
469 C0,
470 C1,
471 C2,
472 C3,
473 C4,
474 C5,
475 C6,
476 C7,
477> Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
478{
479 pub async fn reset(mut self) -> Result<Driver<IF, CHIP>, IF::Error> {
482 self.device
483 .general_cfg()
484 .write_with_zero_async(|r| r.set_rst(true))
485 .await?;
486 Ok(Driver {
487 device: self.device,
488 _chip: core::marker::PhantomData,
489 _channels: core::marker::PhantomData,
490 })
491 }
492
493 pub async fn calibrate(&mut self) -> Result<(), IF::Error> {
495 self.device
496 .general_cfg()
497 .modify_async(|r| r.set_cal(true))
498 .await?;
499 Ok(())
500 }
501
502 pub async fn browned_out(&mut self) -> Result<bool, IF::Error> {
515 Ok(self.device.system_status().read_async().await?.bor())
516 }
517
518 pub async fn clear_bor(&mut self) -> Result<(), IF::Error> {
520 self.device
521 .system_status()
522 .write_with_zero_async(|r| r.set_bor(true))
523 .await?;
524 Ok(())
525 }
526
527 pub async fn set_oversampling(&mut self, ratio: OversamplingRatio) -> Result<(), IF::Error> {
529 self.device
530 .osr_cfg()
531 .modify_async(|r| r.set_osr(ratio))
532 .await?;
533 Ok(())
534 }
535}
536
537impl<IF: CanConvert, CHIP: Chip, C0, C1, C2, C3, C4, C5, C6, C7>
540 Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
541where
542 C0: ChannelMode,
543 C1: ChannelMode,
544 C2: ChannelMode,
545 C3: ChannelMode,
546 C4: ChannelMode,
547 C5: ChannelMode,
548 C6: ChannelMode,
549 C7: ChannelMode,
550{
551 pub const ANALOG_CHANNEL_MASK: u8 = (C0::IS_ANALOG as u8)
554 | (C1::IS_ANALOG as u8) << 1
555 | (C2::IS_ANALOG as u8) << 2
556 | (C3::IS_ANALOG as u8) << 3
557 | (C4::IS_ANALOG as u8) << 4
558 | (C5::IS_ANALOG as u8) << 5
559 | (C6::IS_ANALOG as u8) << 6
560 | (C7::IS_ANALOG as u8) << 7;
561
562 pub async fn read_analog_sequence(&mut self) -> Result<[Option<u16>; 8], IF::Error> {
592 let mask = Self::ANALOG_CHANNEL_MASK;
593 let mut results = [None; 8];
594 if mask == 0 {
595 return Ok(results);
596 }
597
598 let count = mask.count_ones() as usize;
599 let mut buf = [0u8; 16];
600 self.run_analog_sequence(&mut buf[..count * 2]).await?;
601
602 for (slot, channel) in (0..8u8).filter(|c| mask & (1 << c) != 0).enumerate() {
603 let hi = buf[slot * 2] as u16;
604 let lo = buf[slot * 2 + 1] as u16;
605 results[channel as usize] = Some(hi << 8 | lo);
606 }
607 Ok(results)
608 }
609
610 pub async fn verify_analog_sequence_order(&mut self) -> Result<bool, IF::Error> {
625 let mask = Self::ANALOG_CHANNEL_MASK;
626 if mask == 0 {
627 return Ok(true);
628 }
629 let count = mask.count_ones() as usize;
630
631 let saved_osr = self.device.osr_cfg().read_async().await?.osr();
632 self.device
633 .osr_cfg()
634 .modify_async(|r| r.set_osr(OversamplingRatio::Osr1))
635 .await?;
636 self.device
637 .data_cfg()
638 .modify_async(|r| r.set_append_status(AppendStatus::ChannelId))
639 .await?;
640
641 let mut buf = [0u8; 16];
642 let sequence = self.run_analog_sequence(&mut buf[..count * 2]).await;
643
644 self.device
648 .data_cfg()
649 .modify_async(|r| r.set_append_status(AppendStatus::None))
650 .await?;
651 self.device
652 .osr_cfg()
653 .modify_async(|r| r.set_osr(saved_osr))
654 .await?;
655 sequence?;
656
657 let ordered = (0..8u8)
658 .filter(|c| mask & (1 << c) != 0)
659 .enumerate()
660 .all(|(slot, channel)| buf[slot * 2 + 1] & 0x0F == channel);
661 Ok(ordered)
662 }
663
664 async fn run_analog_sequence(&mut self, out: &mut [u8]) -> Result<(), IF::Error> {
667 self.device
668 .auto_seq_ch_sel()
669 .write_async(|r| r.set_auto_seq_ch_sel(Self::ANALOG_CHANNEL_MASK))
670 .await?;
671 self.device
672 .sequence_cfg()
673 .write_async(|r| {
674 r.set_seq_mode(SeqMode::AutoSequence);
675 r.set_seq_start(true);
676 })
677 .await?;
678
679 let read = self.device.interface.read_sequence_raw(out).await;
680
681 self.device
684 .sequence_cfg()
685 .write_async(|r| {
686 r.set_seq_mode(SeqMode::Manual);
687 r.set_seq_start(false);
688 })
689 .await?;
690 read
691 }
692}
693
694impl<
697 IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
698 CHIP: HasStats,
699 C0,
700 C1,
701 C2,
702 C3,
703 C4,
704 C5,
705 C6,
706 C7,
707> Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
708{
709 pub async fn enable_dwc(&mut self) -> Result<(), IF::Error> {
711 self.device
712 .general_cfg()
713 .modify_async(|r| r.set_dwc_en(true))
714 .await?;
715 Ok(())
716 }
717
718 pub async fn disable_dwc(&mut self) -> Result<(), IF::Error> {
720 self.device
721 .general_cfg()
722 .modify_async(|r| r.set_dwc_en(false))
723 .await?;
724 Ok(())
725 }
726
727 pub async fn configure_alert_pin(
729 &mut self,
730 push_pull: bool,
731 logic: AlertLogic,
732 ) -> Result<(), IF::Error> {
733 self.device
734 .alert_pin_cfg()
735 .write_with_zero_async(|r| {
736 r.set_alert_drive(push_pull);
737 r.set_alert_logic(logic);
738 })
739 .await?;
740 Ok(())
741 }
742
743 pub async fn set_alert_channel_mask(&mut self, mask: u8) -> Result<(), IF::Error> {
745 self.device
746 .alert_ch_sel()
747 .write_with_zero_async(|r| r.set_alert_ch_sel(mask))
748 .await?;
749 Ok(())
750 }
751
752 pub async fn event_flags(&mut self) -> Result<u8, IF::Error> {
754 Ok(self.device.event_flag().read_async().await?.event_flag())
755 }
756
757 pub async fn event_high_flags(&mut self) -> Result<u8, IF::Error> {
759 Ok(self
760 .device
761 .event_high_flag()
762 .read_async()
763 .await?
764 .event_high_flag())
765 }
766
767 pub async fn event_low_flags(&mut self) -> Result<u8, IF::Error> {
769 Ok(self
770 .device
771 .event_low_flag()
772 .read_async()
773 .await?
774 .event_low_flag())
775 }
776
777 pub async fn clear_event_high_flags(&mut self, mask: u8) -> Result<(), IF::Error> {
779 self.device
780 .event_high_flag()
781 .write_with_zero_async(|r| r.set_event_high_flag(mask))
782 .await?;
783 Ok(())
784 }
785
786 pub async fn clear_event_low_flags(&mut self, mask: u8) -> Result<(), IF::Error> {
788 self.device
789 .event_low_flag()
790 .write_with_zero_async(|r| r.set_event_low_flag(mask))
791 .await?;
792 Ok(())
793 }
794}
795
796impl<
799 IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
800 CHIP: HasRmsZcd,
801 C0,
802 C1,
803 C2,
804 C3,
805 C4,
806 C5,
807 C6,
808 C7,
809> Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
810{
811 pub async fn configure_rms(
818 &mut self,
819 channel: RmsChannelId,
820 dc_sub: bool,
821 samples: RmsSampleCount,
822 ) -> Result<(), IF::Error> {
823 self.device
824 .rms_cfg()
825 .write_with_zero_async(|r| {
826 r.set_rms_chid(channel);
827 r.set_rms_dc_sub(dc_sub);
828 r.set_rms_samples(samples);
829 })
830 .await?;
831 Ok(())
832 }
833
834 pub async fn read_rms(&mut self) -> Result<u16, IF::Error> {
836 let lsb = self.device.rms_lsb().read_async().await?.value();
837 let msb = self.device.rms_msb().read_async().await?.value();
838 Ok(((msb as u16) << 8) | lsb as u16)
839 }
840
841 pub async fn configure_zcd_blanking(
846 &mut self,
847 multiplier_8x: bool,
848 blanking: u8,
849 ) -> Result<(), IF::Error> {
850 self.device
851 .zcd_blanking_cfg()
852 .write_with_zero_async(|r| {
853 r.set_mult_en(multiplier_8x);
854 r.set_zcd_blanking(blanking & 0x7F);
855 })
856 .await?;
857 Ok(())
858 }
859
860 pub async fn set_zcd_gpo_ch0_ch3(
862 &mut self,
863 ch0: ZcdGpoValue,
864 ch1: ZcdGpoValue,
865 ch2: ZcdGpoValue,
866 ch3: ZcdGpoValue,
867 ) -> Result<(), IF::Error> {
868 self.device
869 .gpo_value_zcd_cfg_ch_0_ch_3()
870 .write_with_zero_async(|r| {
871 r.set_ch_0(ch0);
872 r.set_ch_1(ch1);
873 r.set_ch_2(ch2);
874 r.set_ch_3(ch3);
875 })
876 .await?;
877 Ok(())
878 }
879
880 pub async fn set_zcd_gpo_ch4_ch7(
882 &mut self,
883 ch4: ZcdGpoValue,
884 ch5: ZcdGpoValue,
885 ch6: ZcdGpoValue,
886 ch7: ZcdGpoValue,
887 ) -> Result<(), IF::Error> {
888 self.device
889 .gpo_value_zcd_cfg_ch_4_ch_7()
890 .write_with_zero_async(|r| {
891 r.set_ch_4(ch4);
892 r.set_ch_5(ch5);
893 r.set_ch_6(ch6);
894 r.set_ch_7(ch7);
895 })
896 .await?;
897 Ok(())
898 }
899
900 pub async fn set_zcd_gpo_update_mask(&mut self, mask: u8) -> Result<(), IF::Error> {
902 self.device
903 .gpo_zcd_update_en()
904 .write_with_zero_async(|r| r.set_gpo_zcd_update_en(mask))
905 .await?;
906 Ok(())
907 }
908}
909
910macro_rules! impl_channel {
919 ($n:literal, ($($pre:ident),*), ($($post:ident),*)) => {
920 pastey::paste! {
921 impl<IF, CHIP: Chip, [<C $n>] $(, $pre)* $(, $post)*>
925 ConfigureChannels<IF, CHIP, $($pre,)* [<C $n>], $($post,)*>
926 {
927 #[doc = concat!("Stage channel ", stringify!($n), " as an analog input.")]
928 pub fn [<ch $n _as_analog>](mut self)
929 -> ConfigureChannels<IF, CHIP, $($pre,)* AnalogIn, $($post,)*>
930 {
931 self.pin_cfg_mask |= 1u8 << $n;
932 self.pin_cfg_value &= !(1u8 << $n);
933 ConfigureChannels {
934 device: self.device,
935 pin_cfg_value: self.pin_cfg_value,
936 pin_cfg_mask: self.pin_cfg_mask,
937 gpio_cfg_value: self.gpio_cfg_value,
938 gpio_cfg_mask: self.gpio_cfg_mask,
939 gpo_drive_value: self.gpo_drive_value,
940 gpo_drive_mask: self.gpo_drive_mask,
941 _chip: core::marker::PhantomData,
942 _channels: core::marker::PhantomData,
943 }
944 }
945
946 #[doc = concat!("Stage channel ", stringify!($n), " as a digital input (GPIO).")]
947 pub fn [<ch $n _as_digital_in>](mut self)
948 -> ConfigureChannels<IF, CHIP, $($pre,)* DigitalIn, $($post,)*>
949 {
950 self.pin_cfg_mask |= 1u8 << $n;
951 self.pin_cfg_value |= 1u8 << $n;
952 self.gpio_cfg_mask |= 1u8 << $n;
953 self.gpio_cfg_value &= !(1u8 << $n);
954 ConfigureChannels {
955 device: self.device,
956 pin_cfg_value: self.pin_cfg_value,
957 pin_cfg_mask: self.pin_cfg_mask,
958 gpio_cfg_value: self.gpio_cfg_value,
959 gpio_cfg_mask: self.gpio_cfg_mask,
960 gpo_drive_value: self.gpo_drive_value,
961 gpo_drive_mask: self.gpo_drive_mask,
962 _chip: core::marker::PhantomData,
963 _channels: core::marker::PhantomData,
964 }
965 }
966
967 #[doc = concat!("Stage channel ", stringify!($n), " as a push-pull digital output (GPIO).")]
968 pub fn [<ch $n _as_digital_out_push_pull>](mut self)
969 -> ConfigureChannels<IF, CHIP, $($pre,)* DigitalOut<PushPull>, $($post,)*>
970 {
971 self.pin_cfg_mask |= 1u8 << $n;
972 self.pin_cfg_value |= 1u8 << $n;
973 self.gpio_cfg_mask |= 1u8 << $n;
974 self.gpio_cfg_value |= 1u8 << $n;
975 self.gpo_drive_mask |= 1u8 << $n;
976 self.gpo_drive_value |= 1u8 << $n;
977 ConfigureChannels {
978 device: self.device,
979 pin_cfg_value: self.pin_cfg_value,
980 pin_cfg_mask: self.pin_cfg_mask,
981 gpio_cfg_value: self.gpio_cfg_value,
982 gpio_cfg_mask: self.gpio_cfg_mask,
983 gpo_drive_value: self.gpo_drive_value,
984 gpo_drive_mask: self.gpo_drive_mask,
985 _chip: core::marker::PhantomData,
986 _channels: core::marker::PhantomData,
987 }
988 }
989
990 #[doc = concat!("Stage channel ", stringify!($n), " as an open-drain digital output (GPIO).")]
991 pub fn [<ch $n _as_digital_out_open_drain>](mut self)
992 -> ConfigureChannels<IF, CHIP, $($pre,)* DigitalOut<OpenDrain>, $($post,)*>
993 {
994 self.pin_cfg_mask |= 1u8 << $n;
995 self.pin_cfg_value |= 1u8 << $n;
996 self.gpio_cfg_mask |= 1u8 << $n;
997 self.gpio_cfg_value |= 1u8 << $n;
998 self.gpo_drive_mask |= 1u8 << $n;
999 self.gpo_drive_value &= !(1u8 << $n);
1000 ConfigureChannels {
1001 device: self.device,
1002 pin_cfg_value: self.pin_cfg_value,
1003 pin_cfg_mask: self.pin_cfg_mask,
1004 gpio_cfg_value: self.gpio_cfg_value,
1005 gpio_cfg_mask: self.gpio_cfg_mask,
1006 gpo_drive_value: self.gpo_drive_value,
1007 gpo_drive_mask: self.gpo_drive_mask,
1008 _chip: core::marker::PhantomData,
1009 _channels: core::marker::PhantomData,
1010 }
1011 }
1012 }
1013
1014 impl<IF: CanConvert, CHIP: Chip $(, $pre)* $(, $post)*>
1024 Driver<IF, CHIP, $($pre,)* AnalogIn, $($post,)*>
1025 {
1026 pub async fn [<read_ch $n>](&mut self) -> Result<u16, IF::Error> {
1027 self.device.interface.read_conversion_raw($n).await
1028 }
1029 }
1030
1031 impl<IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface, CHIP: HasStats $(, $pre)* $(, $post)*>
1034 Driver<IF, CHIP, $($pre,)* AnalogIn, $($post,)*>
1035 {
1036 #[doc = concat!("Trigger a conversion on channel ", stringify!($n), " without SCL stretching and")]
1037 #[doc = concat!("OSR_DONE, then reads the result from the RECENT_CH", stringify!($n), " registers.")]
1041 pub async fn [<read_ch $n _polled>](&mut self) -> Result<u16, IF::Error> {
1046 self.device.general_cfg().modify_async(|r| r.set_stats_en(true)).await?;
1047 self.device.manual_ch_sel().write_async(|r| {
1048 r.set_manual_chid(ChannelId::[<Ain $n>])
1049 }).await?;
1050 self.device.general_cfg().modify_async(|r| r.set_cnvst(true)).await?;
1051 loop {
1052 if self.device.system_status().read_async().await?.osr_done() {
1053 break;
1054 }
1055 }
1056 self.device.system_status().write_with_zero_async(|r| {
1057 r.set_osr_done(true)
1058 }).await?;
1059 let lsb = self.device.recent_ch_lsb().read_at_async($n).await?.value();
1060 let msb = self.device.recent_ch_msb().read_at_async($n).await?.value();
1061 Ok(((msb as u16) << 8) | lsb as u16)
1062 }
1063
1064 #[doc = concat!("Set the high and low alert thresholds for channel ", stringify!($n), " (12-bit, 0-4095).")]
1065 pub async fn [<set_ch $n _thresholds>](
1066 &mut self,
1067 high: u16,
1068 low: u16,
1069 ) -> Result<(), IF::Error> {
1070 self.device
1071 .high_th_ch()
1072 .write_with_zero_at_async($n, |r| r.set_high_threshold_msb((high >> 4) as u8))
1073 .await?;
1074 self.device
1075 .hysteresis_ch()
1076 .modify_at_async($n, |r| r.set_high_threshold_lsb((high & 0xF) as u8))
1077 .await?;
1078 self.device
1079 .low_th_ch()
1080 .write_with_zero_at_async($n, |r| r.set_low_threshold_msb((low >> 4) as u8))
1081 .await?;
1082 self.device
1083 .event_count_ch()
1084 .modify_at_async($n, |r| r.set_low_threshold_lsb((low & 0xF) as u8))
1085 .await?;
1086 Ok(())
1087 }
1088
1089 #[doc = concat!("Set the hysteresis for channel ", stringify!($n), " (4-bit; effective value = `hyst << 3` LSBs).")]
1090 pub async fn [<set_ch $n _hysteresis>](
1091 &mut self,
1092 hyst: u8,
1093 ) -> Result<(), IF::Error> {
1094 self.device
1095 .hysteresis_ch()
1096 .modify_at_async($n, |r| r.set_hysteresis(hyst & 0xF))
1097 .await?;
1098 Ok(())
1099 }
1100
1101 #[doc = concat!("Set the event count for channel ", stringify!($n), " (4-bit; alert fires after `count + 1`")]
1102 pub async fn [<set_ch $n _event_count>](
1104 &mut self,
1105 count: u8,
1106 ) -> Result<(), IF::Error> {
1107 self.device
1108 .event_count_ch()
1109 .modify_at_async($n, |r| r.set_event_count(count & 0xF))
1110 .await?;
1111 Ok(())
1112 }
1113 }
1114
1115 impl<IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface, CHIP: Chip $(, $pre)* $(, $post)*>
1118 Driver<IF, CHIP, $($pre,)* DigitalIn, $($post,)*>
1119 {
1120 pub async fn [<is_ch $n _high>](&mut self) -> Result<bool, IF::Error> {
1121 Ok(self.device.gpi_value().read_async().await?.gpi_value() & (1u8 << $n) != 0)
1122 }
1123 }
1124
1125 impl<IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface, CHIP: Chip, D $(, $pre)* $(, $post)*>
1128 Driver<IF, CHIP, $($pre,)* DigitalOut<D>, $($post,)*>
1129 {
1130 pub async fn [<write_ch $n>](&mut self, high: bool) -> Result<(), IF::Error> {
1131 self.device.gpo_value().modify_async(|r| {
1132 let v = r.gpo_value();
1133 r.set_gpo_value(if high { v | (1u8 << $n) } else { v & !(1u8 << $n) })
1134 }).await?;
1135 Ok(())
1136 }
1137 }
1138 }
1139 }
1140}
1141
1142impl_channel!(0, (), (C1, C2, C3, C4, C5, C6, C7));
1143impl_channel!(1, (C0), (C2, C3, C4, C5, C6, C7));
1144impl_channel!(2, (C0, C1), (C3, C4, C5, C6, C7));
1145impl_channel!(3, (C0, C1, C2), (C4, C5, C6, C7));
1146impl_channel!(4, (C0, C1, C2, C3), (C5, C6, C7));
1147impl_channel!(5, (C0, C1, C2, C3, C4), (C6, C7));
1148impl_channel!(6, (C0, C1, C2, C3, C4, C5), (C7));
1149impl_channel!(7, (C0, C1, C2, C3, C4, C5, C6), ());
1150
1151macro_rules! chip_alias {
1154 ($bus:ident : $(#[$attr:meta])* $name:ident, $chip:ty) => {
1155 pastey::paste! {
1156 $(#[$attr])*
1157 pub type $name<
1158 BUS,
1159 C0 = Unconfigured,
1160 C1 = Unconfigured,
1161 C2 = Unconfigured,
1162 C3 = Unconfigured,
1163 C4 = Unconfigured,
1164 C5 = Unconfigured,
1165 C6 = Unconfigured,
1166 C7 = Unconfigured,
1167 > = Driver<[<$bus:camel Interface>]<BUS>, $chip, C0, C1, C2, C3, C4, C5, C6, C7>;
1168 }
1169 };
1170}
1171
1172chip_alias!(i2c: Tla2528, Tla252x);
1173chip_alias!(spi: Tla2518, Tla252x);
1174chip_alias!(i2c: Ads7138, Ads7x38);
1175chip_alias!(spi: Ads7038, Ads7x38);
1176chip_alias!(spi:
1177 Ads7038H,
1181 Ads7x38
1182);
1183chip_alias!(i2c: Ads7128, Ads7x28);
1184chip_alias!(spi: Ads7028, Ads7x28);