Skip to main content

ds2484/
registers_async.rs

1use crate::{
2    DeviceConfiguration, DeviceStatus, Ds2484, Ds2484Error, Ds2484Result, OneWirePortConfiguration,
3    registers::{DEVICE_RST_CMD, DEVICE_STATUS_PTR, READ_PTR_CMD},
4    traits::Addressing,
5    traits_async::InteractAsync,
6};
7use embedded_hal_async::{
8    delay::DelayNs as DelayNsAsync,
9    i2c::{I2c as I2cAsync, SevenBitAddress as SevenBitAddressAsync},
10};
11
12impl<I: I2cAsync<SevenBitAddressAsync>, D: DelayNsAsync> Ds2484<I, D> {
13    /// Get the status of the device.
14    pub async fn get_status_async(&mut self) -> Ds2484Result<DeviceStatus, I::Error> {
15        let mut stat = DeviceStatus::default();
16        stat.async_read(self).await?;
17        Ok(stat)
18    }
19}
20
21impl<I2C: I2cAsync<SevenBitAddressAsync>, D: DelayNsAsync> Ds2484<I2C, D> {
22    /// Reset the device.
23    ///
24    /// Performs a global reset of device state machine logic. Terminates any ongoing 1-Wire
25    /// communication.
26    pub async fn bus_reset_async(&mut self) -> Ds2484Result<DeviceStatus, I2C::Error> {
27        self.i2c.write(self.addr, &[DEVICE_RST_CMD]).await?;
28        self.reset = true;
29        let mut tries = 0;
30        let mut status = [0; 1];
31        loop {
32            self.i2c.read(self.addr, &mut status).await?;
33            let status = DeviceStatus::from(status[0]);
34            if status.device_reset() || tries > self.retries {
35                break;
36            }
37            tries += 1;
38            self.delay.delay_ms(1).await;
39        }
40        let status: DeviceStatus = status[0].into();
41        if tries > self.retries {
42            Err(Ds2484Error::RetriesExceeded)
43        } else {
44            Ok(status)
45        }
46    }
47
48    pub(crate) async fn onewire_wait_async(&mut self) -> Ds2484Result<DeviceStatus, I2C::Error> {
49        let mut tries = 0;
50        let mut status = [0; 1];
51        self.i2c
52            .write(self.addr, &[READ_PTR_CMD, DEVICE_STATUS_PTR])
53            .await?;
54        loop {
55            self.i2c.read(self.addr, &mut status).await?;
56            let status = DeviceStatus::from(status[0]);
57            if !status.onewire_busy() || tries > self.retries {
58                break;
59            }
60            tries += 1;
61            if !self.overdrive {
62                self.delay.delay_ms(1).await;
63            } else {
64                self.delay.delay_us(100).await;
65            }
66        }
67        let status: DeviceStatus = status[0].into();
68        if status.onewire_busy() && tries > self.retries {
69            Err(Ds2484Error::RetriesExceeded)
70        } else {
71            Ok(status)
72        }
73    }
74}
75
76impl InteractAsync for DeviceStatus {
77    async fn async_read<I: I2cAsync<SevenBitAddressAsync>, D>(
78        &mut self,
79        dev: &mut Ds2484<I, D>,
80    ) -> Result<(), Ds2484Error<I::Error>> {
81        let mut val = [0; 1];
82        dev.i2c
83            .write_read(dev.addr, &[READ_PTR_CMD, Self::READ_PTR], &mut val)
84            .await?;
85        *self = DeviceStatus::from(val[0]);
86        Ok(())
87    }
88
89    async fn async_write<I: I2cAsync<SevenBitAddressAsync>, D>(
90        &mut self,
91        _dev: &mut Ds2484<I, D>,
92    ) -> Result<(), Ds2484Error<I::Error>> {
93        Ok(())
94    }
95}
96
97impl InteractAsync for DeviceConfiguration {
98    async fn async_read<I: I2cAsync<SevenBitAddressAsync>, D: DelayNsAsync>(
99        &mut self,
100        dev: &mut Ds2484<I, D>,
101    ) -> Result<(), Ds2484Error<I::Error>> {
102        let mut val = [0; 1];
103        dev.i2c
104            .write_read(dev.addr, &[READ_PTR_CMD, Self::READ_PTR], &mut val)
105            .await?;
106        *self = DeviceConfiguration::from(val[0]);
107        Ok(())
108    }
109
110    async fn async_write<I: I2cAsync<SevenBitAddressAsync>, D: DelayNsAsync>(
111        &mut self,
112        dev: &mut Ds2484<I, D>,
113    ) -> Result<(), Ds2484Error<I::Error>> {
114        dev.onewire_wait_async().await?;
115        let out = u8::from(*self);
116        let out = (out & 0x0f) | ((!out & 0x0f) << 4);
117        let mut val = [0; 1];
118        dev.i2c
119            .write_read(dev.addr, &[Self::WRITE_ADDR, out], &mut val)
120            .await?;
121        *self = val[0].into();
122        dev.reset = false; // Clear the reset flag after writing configuration
123        Ok(())
124    }
125}
126
127impl InteractAsync for OneWirePortConfiguration {
128    async fn async_read<I: I2cAsync<SevenBitAddressAsync>, D: DelayNsAsync>(
129        &mut self,
130        dev: &mut Ds2484<I, D>,
131    ) -> Result<(), Ds2484Error<I::Error>> {
132        let mut buf = [0; 8];
133        dev.i2c
134            .write_read(dev.addr, &[READ_PTR_CMD, Self::READ_PTR], &mut buf)
135            .await?;
136        *self = Self::from_bytes(buf);
137        Ok(())
138    }
139
140    async fn async_write<I: I2cAsync<SevenBitAddressAsync>, D: DelayNsAsync>(
141        &mut self,
142        dev: &mut Ds2484<I, D>,
143    ) -> Result<(), Ds2484Error<I::Error>> {
144        dev.onewire_wait_async().await?;
145        dev.i2c.write(dev.addr, &self.to_bytes()).await?;
146        self.async_read(dev).await
147    }
148}