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
use ak8963::{self, AK8963};

use hal::blocking::delay::DelayMs;
use hal::blocking::i2c;
use hal::blocking::spi;
use hal::digital::v2::OutputPin;

use Register;

/// MPU's I2C address (AD0 low)
const MPU_I2C_ADDR: u8 = 0x68;

/// Releasable describes a type that can be destroyed
/// with a released asset.
pub trait Releasable {
    /// The type to be released
    type Released;

    /// Release the underlying asset
    fn release(self) -> Self::Released;
}

/// An MPU communication device abstraction
///
/// This allows us to generalize the device over either an I2C or SPI peripheral
pub trait Device: Releasable {
    /// The type of error for all results
    type Error;

    /// Read many values from register
    fn read_many(&mut self,
                 reg: Register,
                 buffer: &mut [u8])
                 -> Result<(), Self::Error>;

    /// Write the provided value to register
    fn write(&mut self, reg: Register, val: u8) -> Result<(), Self::Error>;

    /// Read a single value from the register
    fn read(&mut self, reg: Register) -> Result<u8, Self::Error> {
        let buffer = &mut [0; 2];
        self.read_many(reg, buffer)?;
        Ok(buffer[1])
    }

    /// Modify the value in the register using the provided closure. The closure
    /// accepts the current value of the register, permitting conditional checks
    /// before modification.
    fn modify<F>(&mut self, reg: Register, f: F) -> Result<(), Self::Error>
        where F: FnOnce(u8) -> u8
    {
        let r = self.read(reg)?;
        self.write(reg, f(r))?;

        Ok(())
    }
}

/// A SPI device. Use a SPI device when the MPU9250 is
/// connected via SPI
pub struct SpiDevice<SPI, GPIO> {
    /// Underlying peripheral
    spi: SPI,
    /// nCS
    ncs: GPIO,
}

impl<SPI, NCS, E> SpiDevice<SPI, NCS>
    where SPI: spi::Write<u8, Error = E> + spi::Transfer<u8, Error = E>,
          NCS: OutputPin
{
    /// Create a new SpiDevice
    pub fn new(spi: SPI, ncs: NCS) -> Self {
        SpiDevice { spi,
                    ncs }
    }
}

impl<SPI, NCS, E> Releasable for SpiDevice<SPI, NCS>
    where SPI: spi::Write<u8, Error = E> + spi::Transfer<u8, Error = E>,
          NCS: OutputPin
{
    type Released = (SPI, NCS);

    fn release(self) -> (SPI, NCS) {
        (self.spi, self.ncs)
    }
}

/// SPI Error
#[derive(Debug, Copy, Clone)]
pub enum SpiError<E, E2> {
    /// Bus io error
    BusError(E),
    /// NCS error
    NCSError(E2),
}

impl<E, E2> core::convert::From<E> for SpiError<E, E2> {
    fn from(error: E) -> Self {
        SpiError::BusError(error)
    }
}

impl<SPI, NCS, E, EO> Device for SpiDevice<SPI, NCS>
    where SPI: spi::Write<u8, Error = E> + spi::Transfer<u8, Error = E>,
          NCS: OutputPin<Error = EO>
{
    type Error = SpiError<E, EO>;

    // Note: implementation is consistent with previous Mpu9250 private
    // methods. Using read and modify as default trait impls

    fn read_many(&mut self,
                 reg: Register,
                 buffer: &mut [u8])
                 -> Result<(), Self::Error> {
        buffer[0] = reg.read_address();
        self.ncs.set_low().map_err(|a| SpiError::NCSError(a))?;
        self.spi.transfer(buffer)?;
        self.ncs.set_high().map_err(|a| SpiError::NCSError(a))?;

        Ok(())
    }

    // XXX: It seems that without inline(never), when compiling
    //      with opt-level:3, ncs is out of sync with transfer.
    //      This should be probably solved in HAL or HAL impl,
    //      as device drivers should not know about such
    //      minutiæ details.
    #[inline(never)]
    fn write(&mut self, reg: Register, val: u8) -> Result<(), Self::Error> {
        self.ncs.set_low().map_err(|a| SpiError::NCSError(a))?;
        self.spi.write(&[reg.write_address(), val])?;
        self.ncs.set_high().map_err(|a| SpiError::NCSError(a))?;
        Ok(())
    }
}

impl<SPI, NCS, E, EO> AK8963 for SpiDevice<SPI, NCS>
    where SPI: spi::Write<u8, Error = E> + spi::Transfer<u8, Error = E>,
          NCS: OutputPin<Error = EO>
{
    type Error = SpiError<E, EO>;

    fn init<D: DelayMs<u8>>(&mut self,
                            delay: &mut D)
                            -> Result<(), Self::Error> {
        // Isolate the auxiliary master I2C bus (AUX_CL, AUX_DA)
        // disable the slave I2C bus, make serial interface SPI only
        // reset the master I2C bus
        Device::write(self, Register::USER_CTRL, 0x32)?;
        delay.delay_ms(10);
        Ok(())
    }

    fn finalize<D: DelayMs<u8>>(&mut self,
                                delay: &mut D)
                                -> Result<(), Self::Error> {
        // set aux I2C frequency to 400 KHz (should be configurable?)
        Device::write(self, Register::I2C_MST_CTRL, 0x0d)?;

        delay.delay_ms(10);

        // configure sampling of magnetometer
        Device::write(self,
                      Register::I2C_SLV0_ADDR,
                      ak8963::I2C_ADDRESS | ak8963::R)?;
        Device::write(self,
                      Register::I2C_SLV0_REG,
                      ak8963::Register::XOUT_L.addr())?;
        Device::write(self, Register::I2C_SLV0_CTRL, 0x87)?;

        delay.delay_ms(10);
        Ok(())
    }

    fn read(&mut self, reg: ak8963::Register) -> Result<u8, Self::Error> {
        Device::write(self,
                      Register::I2C_SLV4_ADDR,
                      ak8963::I2C_ADDRESS | ak8963::R)?;
        Device::write(self, Register::I2C_SLV4_REG, reg.addr())?;

        // start transfer
        Device::write(self, Register::I2C_SLV4_CTRL, 0x80)?;

        // wait until transfer is over
        while Device::read(self, Register::I2C_MST_STATUS)? & (1 << 6) == 0 {}

        Device::read(self, Register::I2C_SLV4_DI)
    }

    fn write(&mut self,
             reg: ak8963::Register,
             value: u8)
             -> Result<(), Self::Error> {
        Device::write(self,
                      Register::I2C_SLV4_ADDR,
                      ak8963::I2C_ADDRESS | ak8963::W)?;
        Device::write(self, Register::I2C_SLV4_REG, reg.addr())?;
        Device::write(self, Register::I2C_SLV4_DO, value)?;

        // start transfer
        Device::write(self, Register::I2C_SLV4_CTRL, 0x80)?;

        // wait until transfer is over
        while Device::read(self, Register::I2C_MST_STATUS)? & (1 << 6) == 0 {}

        Ok(())
    }

    fn read_xyz(&mut self, buffer: &mut [u8; 7]) -> Result<(), Self::Error> {
        Device::read_many(self, Register::EXT_SENS_DATA_00, buffer)?;
        Ok(())
    }
}

/// An I2C device. Use I2CDevice when the
/// MPU9250 is connected via I2C
pub struct I2cDevice<I2C> {
    i2c: I2C,
}

impl<E, I2C> I2cDevice<I2C>
    where I2C: i2c::Read<Error = E>
              + i2c::Write<Error = E>
              + i2c::WriteRead<Error = E>
{
    /// Create a new I2C device
    pub fn new(i2c: I2C) -> Self {
        I2cDevice { i2c }
    }
}

impl<E, I2C> Releasable for I2cDevice<I2C>
    where I2C: i2c::Read<Error = E>
              + i2c::Write<Error = E>
              + i2c::WriteRead<Error = E>
{
    type Released = I2C;

    fn release(self) -> I2C {
        self.i2c
    }
}

impl<E, I2C> Device for I2cDevice<I2C>
    where I2C: i2c::Read<Error = E>
              + i2c::Write<Error = E>
              + i2c::WriteRead<Error = E>
{
    type Error = E;

    fn read_many(&mut self,
                 reg: Register,
                 buffer: &mut [u8])
                 -> Result<(), Self::Error> {
        self.i2c.write_read(MPU_I2C_ADDR, &[reg as u8], &mut buffer[1..])?;
        Ok(())
    }

    fn write(&mut self, reg: Register, val: u8) -> Result<(), Self::Error> {
        let buff: [u8; 2] = [reg as u8, val];
        self.i2c.write(MPU_I2C_ADDR, &buff)
    }
}

impl<I2C, E> AK8963 for I2cDevice<I2C>
    where I2C: i2c::Read<Error = E>
              + i2c::Write<Error = E>
              + i2c::WriteRead<Error = E>
{
    type Error = E;

    fn init<D: DelayMs<u8>>(&mut self,
                            delay: &mut D)
                            -> Result<(), Self::Error> {
        Device::write(self, Register::USER_CTRL, 0)?;
        delay.delay_ms(10);

        const LATCH_INT_EN: u8 = 1 << 5;
        const INT_ANYRD_CLEAR: u8 = 1 << 4;
        const ACTL_ACTIVE_LOW: u8 = 1 << 7;
        const BYPASS_EN: u8 = 1 << 1;
        Device::write(self,
                      Register::INT_PIN_CFG,
                      LATCH_INT_EN
                      | INT_ANYRD_CLEAR
                      | ACTL_ACTIVE_LOW
                      | BYPASS_EN)?;
        delay.delay_ms(10);

        Ok(())
    }

    fn read(&mut self, reg: ak8963::Register) -> Result<u8, Self::Error> {
        let mut buffer = [0; 1];
        self.i2c.write_read(ak8963::I2C_ADDRESS, &[reg.addr()], &mut buffer)?;
        Ok(buffer[0])
    }

    fn write(&mut self,
             reg: ak8963::Register,
             value: u8)
             -> Result<(), Self::Error> {
        let buff: [u8; 2] = [reg.addr(), value];
        self.i2c.write(ak8963::I2C_ADDRESS, &buff)
    }

    fn read_xyz(&mut self, buffer: &mut [u8; 7]) -> Result<(), Self::Error> {
        // We need to leave the zeroth byte as a zero to conform with the
        // SPI device behaviors. We also want to read past the data bytes
        // to read register ST2. We're required to read ST2 after each
        // reading, otherwise the magnetometer blocks sampling. We can
        // achieve this in one I2C transaction
        self.i2c.write_read(ak8963::I2C_ADDRESS,
                             &[ak8963::Register::XOUT_L.addr()],
                             buffer)?;

        buffer[..].rotate_right(1);
        buffer[0] = 0; // Zero out ST2 afer rotation
        Ok(())
    }
}

/// The trait describes how to aquire 9 degrees-of-freedom
/// measurements (plug a temperature reading) from an MPU
pub trait NineDOFDevice:
    Device + AK8963<Error = <Self as Device>::Error>
{
    /// Perform a 9DOF reading
    ///
    /// The trait assumes a contiguous reading of (x, y, z) accelerometer,
    /// temperature, (x,y,z) gyroscope, and (x, y, z) magnetometer.
    /// Essentially, this is the layout of a single SPI read transaction.
    /// Any other implementors are required to meet this layout.
    fn read_9dof(&mut self,
                 reg: Register,
                 buffer: &mut [u8; 21])
                 -> Result<(), <Self as Device>::Error>;
}

impl<SPI, NCS, E, EO> NineDOFDevice for SpiDevice<SPI, NCS>
    where SPI: spi::Write<u8, Error = E> + spi::Transfer<u8, Error = E>,
          NCS: OutputPin<Error = EO>
{
    fn read_9dof(&mut self,
                 reg: Register,
                 buffer: &mut [u8; 21])
                 -> Result<(), <Self as Device>::Error> {
        self.read_many(reg, buffer)?;
        Ok(())
    }
}

impl<I2C, E> NineDOFDevice for I2cDevice<I2C>
    where I2C: i2c::Read<Error = E>
              + i2c::Write<Error = E>
              + i2c::WriteRead<Error = E>
{
    fn read_9dof(&mut self,
                 reg: Register,
                 buffer: &mut [u8; 21])
                 -> Result<(), <Self as Device>::Error> {
        Device::read_many(self, reg, &mut buffer[0..15])?;

        let xyz_buffer = &mut [0; 7];
        AK8963::read_xyz(self, xyz_buffer)?;
        buffer[15..21].copy_from_slice(&xyz_buffer[1..7]);

        Ok(())
    }
}