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
use crate::traits::{
    wg::{
        digital::v2::{
            InputPin,
            OutputPin,
            StatefulOutputPin,
            toggleable,
        },
    }
};

use crate::typestates::{
    pin::{
        state,
        gpio::{
            direction,
            Level,
        },
        PinId,
    },
    reg_proxy::RegClusterProxy,
};

use super::Pin;

use crate::{
    raw::gpio::{
        // B,
        // W,
        CLR,
        DIRSET,
        DIRCLR,
        PIN,
        SET,
    },
    reg_cluster,
};

// reg_cluster!(B, B, raw::GPIO, b);
// reg_cluster!(W, W, raw::GPIO, w);
reg_cluster!(DIRSET, DIRSET, raw::GPIO, dirset);
reg_cluster!(DIRCLR, DIRCLR, raw::GPIO, dirclr);
reg_cluster!(PIN, PIN, raw::GPIO, pin);
reg_cluster!(SET, SET, raw::GPIO, set);
reg_cluster!(CLR, CLR, raw::GPIO, clr);


impl<T> OutputPin for Pin<T, state::Gpio<direction::Output>>
where
    T: PinId,
{
    type Error = core::convert::Infallible;

    /// Set the pin output to HIGH
    fn set_high(&mut self) -> Result<(), Self::Error> {
        self.state.set[T::PORT].write(|w| unsafe { w.setp().bits(T::MASK) });
        Ok(())
    }

    /// Set the pin output to LOW
    fn set_low(&mut self) -> Result<(), Self::Error> {
        self.state.clr[T::PORT].write(|w| unsafe { w.clrp().bits(T::MASK) });
        Ok(())
    }
}

impl<T> StatefulOutputPin for Pin<T, state::Gpio<direction::Output>>
where
    T: PinId,
{
    fn is_set_high(&self) -> Result<bool, Self::Error> {
        Ok(self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
    }

    fn is_set_low(&self) -> Result<bool, Self::Error> {
        Ok(!self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
    }
}

impl<T: PinId> toggleable::Default for Pin<T, state::Gpio<direction::Output>> {}

impl<T> InputPin for Pin<T, state::Gpio<direction::Input>>
where
    T: PinId,
{
    type Error = core::convert::Infallible;

    fn is_high(&self) -> Result<bool, Self::Error> {
        // Ok(self.state.b[T::OFFSET].b_.read().pbyte())
        Ok(self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
    }

    fn is_low(&self) -> Result<bool, Self::Error> {
        // Ok(!self.state.b.b_[T::OFFSET].read().pbyte())
        Ok(!self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
    }
}

impl<T, D> Pin<T, state::Gpio<D>>
where
    T: PinId,
    D: direction::NotOutput,
{
    pub fn into_output_high(self) -> Pin<T, state::Gpio<direction::Output>> {
        self.into_output(Level::High)
    }
    pub fn into_output_low(self) -> Pin<T, state::Gpio<direction::Output>> {
        self.into_output(Level::Low)
    }
    pub fn into_output(self, initial: Level) -> Pin<T, state::Gpio<direction::Output>> {
        match initial {
            Level::High => self.state.set[T::PORT].write(|w| unsafe { w.setp().bits(T::MASK) }),
            Level::Low => self.state.clr[T::PORT].write(|w| unsafe { w.clrp().bits(T::MASK) }),
        }

        self.state.dirset[T::PORT].write(|w| unsafe { w.dirsetp().bits(T::MASK) });

        Pin {
            id: self.id,

            state: state::Gpio {
                // b: RegClusterProxy::new(),
                // w: RegClusterProxy::new(),
                dirset: RegClusterProxy::new(),
                dirclr: RegClusterProxy::new(),
                pin: RegClusterProxy::new(),
                set: RegClusterProxy::new(),
                clr: RegClusterProxy::new(),

                _direction: direction::Output,
            },
        }
    }
}

impl<T, D> Pin<T, state::Gpio<D>>
where
    T: PinId,
    D: direction::NotInput,
{
    pub fn into_input(self) -> Pin<T, state::Gpio<direction::Input>> {

        // currently, `into_gpio_pin()` sets `.digimode().digital()` in IOCON,
        // meaning input is enabled for all pins

        self.state.dirclr[T::PORT].write(|w| unsafe { w.dirclrp().bits(T::MASK) });

        Pin {
            id: self.id,

            state: state::Gpio {
                // b: RegClusterProxy::new(),
                // w: RegClusterProxy::new(),
                dirset: RegClusterProxy::new(),
                dirclr: RegClusterProxy::new(),
                pin: RegClusterProxy::new(),
                set: RegClusterProxy::new(),
                clr: RegClusterProxy::new(),

                _direction: direction::Input,
            },
        }
    }
}


impl<T, D> Pin<T, state::Analog<D>>
where
    T: PinId,
    D: direction::NotInput,
{
    pub fn into_input(self) -> Pin<T, state::Analog<direction::Input>> {

        // currently, `into_gpio_pin()` sets `.digimode().digital()` in IOCON,
        // meaning input is enabled for all pins

        self.state.dirclr[T::PORT].write(|w| unsafe { w.dirclrp().bits(T::MASK) });

        Pin {
            id: self.id,

            state: state::Analog {
                channel: self.state.channel,
                dirclr: RegClusterProxy::new(),
                _direction: direction::Input,
            },
        }
    }
}