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
//! I2C Master Interface
//!
//! The SiFive Inter-Integrated Circuit (I2C) Master Interface
//! is based on OpenCores® I2C Master Core.
//!
//! You can use the `I2c` interface with these I2C instances
//!
//! # I2C0
//! - SDA: Pin 12 IOF0
//! - SCL: Pin 13 IOF0
//! - Interrupt::I2C0

use e310x::{I2C0, i2c0};
use core::ops::Deref;
use core::mem;
use crate::gpio::{gpio0, IOF0};
use crate::time::Bps;
use crate::clock::Clocks;
use embedded_hal::blocking::i2c::{Read, Write, WriteRead};

/// SDA pin - DO NOT IMPLEMENT THIS TRAIT
pub unsafe trait SdaPin<I2C> {}
/// SCL pin - DO NOT IMPLEMENT THIS TRAIT
pub unsafe trait SclPin<I2C> {}

unsafe impl<T> SdaPin<I2C0> for gpio0::Pin12<IOF0<T>> { }
unsafe impl<T> SclPin<I2C0> for gpio0::Pin13<IOF0<T>> { }

/// I2C error
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
    /// Invalid peripheral state
    InvalidState,

    /// Arbitration lost
    ArbitrationLost,

    /// No ACK received
    NoAck,
}

/// Transmission speed
pub enum Speed {
    /// 100Kbps
    Normal,

    /// 400Kbps
    Fast,

    /// Custom speed
    Custom(Bps),
}


/// I2C abstraction
pub struct I2c<I2C, PINS> {
    i2c: I2C,
    pins: PINS,
}

impl<SDA, SCL> I2c<I2C0, (SDA, SCL)> {
    /// Configures an I2C peripheral
    pub fn new(i2c: I2C0, sda: SDA, scl: SCL, speed: Speed, clocks: Clocks) -> Self
    where SDA: SdaPin<I2C0>, SCL: SclPin<I2C0> {
        // Calculate prescaler value
        let desired_speed = match speed {
            Speed::Normal => 100_000,
            Speed::Fast => 400_000,
            Speed::Custom(bps) => bps.0,
        };
        let clock = clocks.tlclk().0;
        assert!(desired_speed * 5 <= clock);
        let prescaler = clock / (5 * desired_speed) - 1;
        assert!(prescaler < (1 << 16));

        // Turn off i2c
        i2c.ctr.write(|w| w.en().clear_bit().ien().clear_bit());

        // Set prescaler
        let prescaler_lo = (prescaler & 0xff) as u8;
        let prescaler_hi = ((prescaler >> 8) & 0xff) as u8;
        i2c.prer_lo.write(|w| unsafe { w.value().bits(prescaler_lo) });
        i2c.prer_hi.write(|w| unsafe { w.value().bits(prescaler_hi) });

        // Turn on i2c
        i2c.ctr.write(|w| w.en().set_bit());

        Self {
            i2c,
            pins: (sda, scl)
        }
    }
}

impl<I2C, PINS> I2c<I2C, PINS> {
    /// Releases the I2C peripheral and associated pins
    pub fn free(self) -> (I2C, PINS) {
        (self.i2c, self.pins)
    }
}

impl<I2C: Deref<Target=i2c0::RegisterBlock>, PINS> I2c<I2C, PINS> {
    fn reset(&self) {
        // ACK pending interrupt event, clear commands
        self.write_cr(|w| w.iack().set_bit());
    }

    fn write_cr<F>(&self, f: F)
    where F: FnOnce(&mut i2c0::cr::W) -> &mut i2c0::cr::W
    {
        self.i2c.cr_sr.write(|w| unsafe {
            let mut value: u32 = 0;
            f(mem::transmute(&mut value));
            w.bits(value)
        });
    }

    fn read_sr(&self) -> i2c0::sr::R {
        unsafe {
            mem::transmute(self.i2c.cr_sr.read())
        }
    }

    fn write_byte(&self, byte: u8) {
        self.i2c.txr_rxr.write(|w| unsafe {
            w.data().bits(byte)
        });
    }

    fn read_byte(&self) -> u8 {
        self.i2c.txr_rxr.read().data().bits()
    }

    fn wait_for_interrupt(&self) -> Result<(), Error> {
        loop {
            let sr = self.read_sr();

            if sr.al().bit_is_set() {
                // Set STOP
                self.write_cr(|w| w.sto().set_bit());
                self.wait_for_complete();

                return Err(Error::ArbitrationLost);
            }

            if sr.if_().bit_is_set() {
                // ACK the interrupt
                self.write_cr(|w| w.iack().set_bit());

                return Ok(());
            }
        }
    }

    fn wait_for_read(&self) -> Result<(), Error> {
        self.wait_for_interrupt()
    }

    fn wait_for_write(&self) -> Result<(), Error> {
        self.wait_for_interrupt()?;

        if self.read_sr().rx_ack().bit_is_set() {
            // Set STOP
            self.write_cr(|w| w.sto().set_bit());
            self.wait_for_complete();

            return Err(Error::NoAck);
        }

        Ok(())
    }

    fn wait_for_complete(&self) {
        while self.read_sr().busy().bit_is_set() { }
    }
}

const FLAG_READ: u8 = 1;
const FLAG_WRITE: u8 = 0;

impl<I2C: Deref<Target=i2c0::RegisterBlock>, PINS> Read for I2c<I2C, PINS> {
    type Error = Error;

    fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
        self.reset();

        if self.read_sr().busy().bit_is_set() {
            return Err(Error::InvalidState);
        }

        // Write address + R
        self.write_byte((address << 1) + FLAG_READ);

        // Generate start condition and write command
        self.write_cr(|w| w.sta().set_bit().wr().set_bit());
        self.wait_for_write()?;

        // Read bytes
        let buffer_len = buffer.len();
        for (i, byte) in buffer.iter_mut().enumerate() {
            if i != buffer_len - 1 {
                // R + ACK
                self.write_cr(|w| w.rd().set_bit().ack().clear_bit());
            } else {
                // R + NACK + STOP
                self.write_cr(|w| w.rd().set_bit().ack().set_bit().sto().set_bit());
            }
            self.wait_for_read()?;

            *byte = self.read_byte();
        }
        Ok(())
    }
}

impl<I2C: Deref<Target=i2c0::RegisterBlock>, PINS> Write for I2c<I2C, PINS> {
    type Error = Error;

    fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
        self.reset();

        if self.read_sr().busy().bit_is_set() {
            return Err(Error::InvalidState);
        }

        // Write address + W
        self.write_byte((address << 1) + FLAG_WRITE);

        // Generate start condition and write command
        self.write_cr(|w| w.sta().set_bit().wr().set_bit());
        self.wait_for_write()?;

        // Write bytes
        for (i, byte) in bytes.iter().enumerate() {
            self.write_byte(*byte);

            if i != bytes.len() - 1 {
                self.write_cr(|w| w.wr().set_bit());
            } else {
                self.write_cr(|w| w.wr().set_bit().sto().set_bit());
            }
            self.wait_for_write()?;
        }
        Ok(())
    }
}

impl<I2C: Deref<Target=i2c0::RegisterBlock>, PINS> WriteRead for I2c<I2C, PINS> {
    type Error = Error;

    fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {
        self.reset();

        if self.read_sr().busy().bit_is_set() {
            return Err(Error::InvalidState);
        }

        if !bytes.is_empty() && buffer.is_empty() {
            self.write(address, bytes)
        } else if !buffer.is_empty() && bytes.is_empty() {
            self.read(address, buffer)
        } else if bytes.is_empty() && buffer.is_empty() {
            Ok(())
        } else {
            // Write address + W
            self.write_byte((address << 1) + FLAG_WRITE);

            // Generate start condition and write command
            self.write_cr(|w| w.sta().set_bit().wr().set_bit());
            self.wait_for_write()?;

            // Write bytes
            for byte in bytes {
                self.write_byte(*byte);

                self.write_cr(|w| w.wr().set_bit());
                self.wait_for_write()?;
            }

            // Write address + R
            self.write_byte((address << 1) + FLAG_READ);

            // Generate repeated start condition and write command
            self.write_cr(|w| w.sta().set_bit().wr().set_bit());
            self.wait_for_write()?;

            // Read bytes
            let buffer_len = buffer.len();
            for (i, byte) in buffer.iter_mut().enumerate() {
                if i != buffer_len - 1 {
                    // W + ACK
                    self.write_cr(|w| w.rd().set_bit().ack().clear_bit());
                } else {
                    // W + NACK + STOP
                    self.write_cr(|w| w.rd().set_bit().ack().set_bit().sto().set_bit());
                }
                self.wait_for_read()?;

                *byte = self.read_byte();
            }

            Ok(())
        }
    }
}