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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Driver for the Microchip MCP23008 I2C GPIO extender.
//!
//! <http://www.microchip.com/wwwproducts/en/MCP23008>
//!
//! Paraphrased from the website:
//!
//! > The MCP23008 device provides 8-bit, general purpose, parallel I/O
//! > expansion for I2C bus applications. The MCP23008 has three address pins
//! > and consists of multiple 8-bit configuration registers for input, output
//! > and polarity selection. The system master can enable the I/Os as either
//! > inputs or outputs by writing the I/O configuration bits. The data for each
//! > input or output is kept in the corresponding Input or Output register. The
//! > polarity of the Input Port register can be inverted with the Polarity
//! > Inversion register. All registers can be read by the system master.
//!
//! Usage
//! -----
//! This capsule can either be used inside of the kernel or as an input to
//! the `gpio_async` capsule because it implements the `hil::gpio_async::Port`
//! trait.
//!
//! Example usage:
//!
//! ```rust
//! // Configure the MCP23008. Device address 0x20.
//! let mcp23008_i2c = static_init!(
//!     capsules::virtual_i2c::I2CDevice,
//!     capsules::virtual_i2c::I2CDevice::new(i2c_mux, 0x20));
//! let mcp23008 = static_init!(
//!     capsules::mcp23008::MCP23008<'static>,
//!     capsules::mcp23008::MCP23008::new(mcp23008_i2c,
//!                                       Some(&sam4l::gpio::PA[04]),
//!                                       &mut capsules::mcp23008::BUFFER));
//! mcp23008_i2c.set_client(mcp23008);
//! sam4l::gpio::PA[04].set_client(mcp23008);
//!
//! // Create an array of the GPIO extenders so we can pass them to an
//! // administrative layer that provides a single interface to them all.
//! let async_gpio_ports = static_init!(
//!     [&'static capsules::mcp23008::MCP23008; 1],
//!     [mcp23008]);
//!
//! // `gpio_async` is the object that manages all of the extenders.
//! let gpio_async = static_init!(
//!     capsules::gpio_async::GPIOAsync<'static, capsules::mcp23008::MCP23008<'static>>,
//!     capsules::gpio_async::GPIOAsync::new(async_gpio_ports));
//! // Setup the clients correctly.
//! for port in async_gpio_ports.iter() {
//!     port.set_client(gpio_async);
//! }
//! ```
//!
//! Note that if interrupts are not needed, a `None` can be passed in when the
//! `mcp23008` object is created.

use core::cell::Cell;
use kernel::ReturnCode;
use kernel::common::take_cell::TakeCell;
use kernel::hil;

// Buffer to use for I2C messages
pub static mut BUFFER: [u8; 7] = [0; 7];

#[allow(dead_code)]
#[derive(Debug)]
enum Registers {
    IoDir = 0x00,
    IPol = 0x01,
    GpIntEn = 0x02,
    DefVal = 0x03,
    IntCon = 0x04,
    IoCon = 0x05,
    GpPu = 0x06,
    IntF = 0x07,
    IntCap = 0x08,
    Gpio = 0x09,
    OLat = 0x0a,
}

/// States of the I2C protocol with the MCP23008.
#[derive(Clone, Copy, Debug, PartialEq)]
enum State {
    Idle,

    // Setup input/output
    SelectIoDir(u8, Direction),
    ReadIoDir(u8, Direction),
    SelectGpPu(u8, bool),
    ReadGpPu(u8, bool),
    SetGpPu(u8),
    SelectGpio(u8, PinState),
    ReadGpio(u8, PinState),
    SelectGpioToggle(u8),
    ReadGpioToggle(u8),
    SelectGpioRead(u8),
    ReadGpioRead(u8),
    EnableInterruptSettings,
    ReadInterruptSetup,
    ReadInterruptValues,

    /// Disable I2C and release buffer
    Done,
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum Direction {
    Input = 0x01,
    Output = 0x00,
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum PinState {
    High = 0x01,
    Low = 0x00,
}

pub struct MCP23008<'a> {
    i2c: &'a hil::i2c::I2CDevice,
    state: Cell<State>,
    buffer: TakeCell<'static, [u8]>,
    interrupt_pin: Option<&'static hil::gpio::Pin>,
    interrupt_settings: Cell<u32>, // Whether the pin interrupt is enabled, and what mode it's in.
    identifier: Cell<usize>,
    client: Cell<Option<&'static hil::gpio_async::Client>>,
}

impl<'a> MCP23008<'a> {
    pub fn new(
        i2c: &'a hil::i2c::I2CDevice,
        interrupt_pin: Option<&'static hil::gpio::Pin>,
        buffer: &'static mut [u8],
    ) -> MCP23008<'a> {
        MCP23008 {
            i2c: i2c,
            state: Cell::new(State::Idle),
            buffer: TakeCell::new(buffer),
            interrupt_pin: interrupt_pin,
            interrupt_settings: Cell::new(0),
            identifier: Cell::new(0),
            client: Cell::new(None),
        }
    }

    /// Set the client of this MCP23008 when commands finish or interrupts
    /// occur. The `identifier` is simply passed back with the callback
    /// so that the upper layer can keep track of which device triggered.
    pub fn set_client<C: hil::gpio_async::Client>(&self, client: &'static C) {
        self.client.set(Some(client));
    }

    fn enable_host_interrupt(&self) -> ReturnCode {
        // We configure the MCP23008 to use an active high interrupt.
        // If we don't have an interrupt pin mapped to this driver then we
        // obviously can't do interrupts.
        self.interrupt_pin
            .map_or(ReturnCode::FAIL, |interrupt_pin| {
                interrupt_pin.make_input();
                interrupt_pin.enable_interrupt(0, hil::gpio::InterruptMode::RisingEdge);
                ReturnCode::SUCCESS
            })
    }

    fn set_direction(&self, pin_number: u8, direction: Direction) -> ReturnCode {
        self.buffer.take().map_or(ReturnCode::EBUSY, |buffer| {
            self.i2c.enable();

            buffer[0] = Registers::IoDir as u8;
            self.i2c.write(buffer, 1);
            self.state.set(State::SelectIoDir(pin_number, direction));

            ReturnCode::SUCCESS
        })
    }

    /// Set the pull-up on the pin also configure it to be an input.
    fn configure_pullup(&self, pin_number: u8, enabled: bool) -> ReturnCode {
        self.buffer.take().map_or(ReturnCode::EBUSY, |buffer| {
            self.i2c.enable();

            buffer[0] = Registers::IoDir as u8;
            self.i2c.write(buffer, 1);
            self.state.set(State::SelectGpPu(pin_number, enabled));

            ReturnCode::SUCCESS
        })
    }

    fn set_pin(&self, pin_number: u8, value: PinState) -> ReturnCode {
        self.buffer.take().map_or(ReturnCode::EBUSY, |buffer| {
            self.i2c.enable();

            buffer[0] = Registers::Gpio as u8;
            self.i2c.write(buffer, 1);
            self.state.set(State::SelectGpio(pin_number, value));

            ReturnCode::SUCCESS
        })
    }

    fn toggle_pin(&self, pin_number: u8) -> ReturnCode {
        self.buffer.take().map_or(ReturnCode::EBUSY, |buffer| {
            self.i2c.enable();

            buffer[0] = Registers::Gpio as u8;
            self.i2c.write(buffer, 1);
            self.state.set(State::SelectGpioToggle(pin_number));

            ReturnCode::SUCCESS
        })
    }

    fn read_pin(&self, pin_number: u8) -> ReturnCode {
        self.buffer.take().map_or(ReturnCode::EBUSY, |buffer| {
            self.i2c.enable();

            buffer[0] = Registers::Gpio as u8;
            self.i2c.write(buffer, 1);
            self.state.set(State::SelectGpioRead(pin_number));

            ReturnCode::SUCCESS
        })
    }

    fn enable_interrupt_pin(
        &self,
        pin_number: u8,
        direction: hil::gpio::InterruptMode,
    ) -> ReturnCode {
        self.buffer.take().map_or(ReturnCode::EBUSY, |buffer| {
            self.i2c.enable();

            // Mark the settings that we have for this interrupt.
            // Since the MCP23008 only seems to support level interrupts
            // and both edge interrupts, we choose to use both edge interrupts
            // and then filter here in the driver if the user only asked
            // for one direction interrupts. To do this, we need to know what
            // the user asked for.
            self.save_pin_interrupt_state(pin_number, true, direction);

            // Setup interrupt configs that are true of all interrupts
            buffer[0] = Registers::IntCon as u8;
            buffer[1] = 0; // Make all pins toggle on every change.
            buffer[2] = 0b00000010; // Make MCP23008 interrupt pin active high.
            self.i2c.write(buffer, 3);
            self.state.set(State::EnableInterruptSettings);

            ReturnCode::SUCCESS
        })
    }

    fn disable_interrupt_pin(&self, pin_number: u8) -> ReturnCode {
        self.buffer.take().map_or(ReturnCode::EBUSY, |buffer| {
            self.i2c.enable();

            // Clear this interrupt from our setup.
            self.remove_pin_interrupt_state(pin_number);

            // Just have to write the new interrupt settings.
            buffer[0] = Registers::GpIntEn as u8;
            buffer[1] = self.get_pin_interrupt_enabled_state();
            self.i2c.write(buffer, 2);
            self.state.set(State::Done);

            ReturnCode::SUCCESS
        })
    }

    /// Helper function for keeping track of which interrupts are currently
    /// enabled.
    fn save_pin_interrupt_state(
        &self,
        pin_number: u8,
        enabled: bool,
        direction: hil::gpio::InterruptMode,
    ) {
        let mut current_state = self.interrupt_settings.get();
        // Clear out existing settings
        current_state &= !(0x0F << (4 * pin_number));
        // Generate new settings
        let new_settings = (((enabled as u8) | ((direction as u8) << 1)) & 0x0F) as u32;
        // Update settings
        current_state |= new_settings << (4 * pin_number);
        self.interrupt_settings.set(current_state);
    }

    fn remove_pin_interrupt_state(&self, pin_number: u8) {
        let new_settings = self.interrupt_settings.get() & !(0x0F << (4 * pin_number));
        self.interrupt_settings.set(new_settings);
    }

    /// Create an 8 bit bitmask of which interrupts are enabled.
    fn get_pin_interrupt_enabled_state(&self) -> u8 {
        let current_state = self.interrupt_settings.get();
        let mut interrupts_enabled: u8 = 0;
        for i in 0..8 {
            if ((current_state >> (4 * i)) & 0x01) == 0x01 {
                interrupts_enabled |= 1 << i;
            }
        }
        interrupts_enabled
    }

    fn check_pin_interrupt_enabled(&self, pin_number: u8) -> bool {
        (self.interrupt_settings.get() >> (pin_number * 4)) & 0x01 == 0x01
    }

    fn get_pin_interrupt_direction(&self, pin_number: u8) -> hil::gpio::InterruptMode {
        let direction = self.interrupt_settings.get() >> ((pin_number * 4) + 1) & 0x03;
        match direction {
            0 => hil::gpio::InterruptMode::RisingEdge,
            1 => hil::gpio::InterruptMode::FallingEdge,
            _ => hil::gpio::InterruptMode::EitherEdge,
        }
    }
}

impl<'a> hil::i2c::I2CClient for MCP23008<'a> {
    fn command_complete(&self, buffer: &'static mut [u8], _error: hil::i2c::Error) {
        match self.state.get() {
            State::SelectIoDir(pin_number, direction) => {
                self.i2c.read(buffer, 1);
                self.state.set(State::ReadIoDir(pin_number, direction));
            }
            State::ReadIoDir(pin_number, direction) => {
                if direction == Direction::Input {
                    buffer[1] = buffer[0] | (1 << pin_number);
                } else {
                    buffer[1] = buffer[0] & !(1 << pin_number);
                }
                buffer[0] = Registers::IoDir as u8;
                self.i2c.write(buffer, 2);
                self.state.set(State::Done);
            }
            State::SelectGpPu(pin_number, enabled) => {
                self.i2c.read(buffer, 7);
                self.state.set(State::ReadGpPu(pin_number, enabled));
            }
            State::ReadGpPu(pin_number, enabled) => {
                // Make sure the pin is enabled.
                buffer[1] = buffer[0] | (1 << pin_number);
                // Configure the pullup status and save it in the buffer.
                let pullup = match enabled {
                    true => buffer[6] | (1 << pin_number),
                    false => buffer[6] & !(1 << pin_number),
                };
                buffer[0] = Registers::IoDir as u8;
                self.i2c.write(buffer, 2);
                self.state.set(State::SetGpPu(pullup));
            }
            State::SetGpPu(pullup) => {
                // Now write the pull up settings to the chip.
                buffer[0] = Registers::GpPu as u8;
                buffer[1] = pullup;
                self.i2c.write(buffer, 2);
                self.state.set(State::Done);
            }
            State::SelectGpio(pin_number, value) => {
                self.i2c.read(buffer, 1);
                self.state.set(State::ReadGpio(pin_number, value));
            }
            State::ReadGpio(pin_number, value) => {
                buffer[1] = match value {
                    PinState::High => buffer[0] | (1 << pin_number),
                    PinState::Low => buffer[0] & !(1 << pin_number),
                };
                buffer[0] = Registers::Gpio as u8;
                self.i2c.write(buffer, 2);
                self.state.set(State::Done);
            }
            State::SelectGpioToggle(pin_number) => {
                self.i2c.read(buffer, 1);
                self.state.set(State::ReadGpioToggle(pin_number));
            }
            State::ReadGpioToggle(pin_number) => {
                buffer[1] = buffer[0] ^ (1 << pin_number);
                buffer[0] = Registers::Gpio as u8;
                self.i2c.write(buffer, 2);
                self.state.set(State::Done);
            }
            State::SelectGpioRead(pin_number) => {
                self.i2c.read(buffer, 1);
                self.state.set(State::ReadGpioRead(pin_number));
            }
            State::ReadGpioRead(pin_number) => {
                let pin_value = (buffer[0] >> pin_number) & 0x01;

                self.client.get().map(|client| {
                    client.done(pin_value as usize);
                });

                self.buffer.replace(buffer);
                self.i2c.disable();
                self.state.set(State::Idle);
            }
            State::EnableInterruptSettings => {
                // Rather than read the current interrupts and write those
                // back, just write the entire register with our saved state.
                buffer[0] = Registers::GpIntEn as u8;
                buffer[1] = self.get_pin_interrupt_enabled_state();
                self.i2c.write(buffer, 2);
                self.state.set(State::Done);
            }
            State::ReadInterruptSetup => {
                // Now read the interrupt flags and the state of the lines
                self.i2c.read(buffer, 3);
                self.state.set(State::ReadInterruptValues);
            }
            State::ReadInterruptValues => {
                let interrupt_flags = buffer[0];
                let pins_status = buffer[2];
                // Check each bit to see if that pin triggered an interrupt.
                for i in 0..8 {
                    // Check that this pin is actually enabled.
                    if !self.check_pin_interrupt_enabled(i) {
                        continue;
                    }
                    if (interrupt_flags >> i) & 0x01 == 0x01 {
                        // Use the GPIO register to determine which way the
                        // interrupt went.
                        let pin_status = (pins_status >> i) & 0x01;
                        let interrupt_direction = self.get_pin_interrupt_direction(i);
                        // Check to see if this was an interrupt we want
                        // to report.
                        let fire_interrupt = match interrupt_direction {
                            hil::gpio::InterruptMode::EitherEdge => true,
                            hil::gpio::InterruptMode::RisingEdge => pin_status == 0x01,
                            hil::gpio::InterruptMode::FallingEdge => pin_status == 0x00,
                        };
                        if fire_interrupt {
                            // Signal this interrupt to the application.
                            self.client.get().map(|client| {
                                // Return both the pin that interrupted and
                                // the identifier that was passed for
                                // enable_interrupt.
                                client.fired(i as usize, self.identifier.get());
                            });
                            break;
                        }
                    }
                }
                self.buffer.replace(buffer);
                self.i2c.disable();
                self.state.set(State::Idle);
            }
            State::Done => {
                self.client.get().map(|client| {
                    client.done(0);
                });

                self.buffer.replace(buffer);
                self.i2c.disable();
                self.state.set(State::Idle);
            }
            _ => {}
        }
    }
}

impl<'a> hil::gpio::Client for MCP23008<'a> {
    fn fired(&self, _: usize) {
        self.buffer.take().map(|buffer| {
            self.i2c.enable();

            // Need to read the IntF register which marks which pins
            // interrupted.
            buffer[0] = Registers::IntF as u8;
            self.i2c.write(buffer, 1);
            self.state.set(State::ReadInterruptSetup);
        });
    }
}

impl<'a> hil::gpio_async::Port for MCP23008<'a> {
    fn disable(&self, pin: usize) -> ReturnCode {
        // Best we can do is make this an input.
        self.set_direction(pin as u8, Direction::Input)
    }

    fn make_output(&self, pin: usize) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        self.set_direction(pin as u8, Direction::Output)
    }

    fn make_input(&self, pin: usize, mode: hil::gpio::InputMode) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        match mode {
            hil::gpio::InputMode::PullUp => self.configure_pullup(pin as u8, true),
            hil::gpio::InputMode::PullDown => {
                // No support for this
                self.configure_pullup(pin as u8, false)
            }
            hil::gpio::InputMode::PullNone => self.configure_pullup(pin as u8, false),
        }
    }

    fn read(&self, pin: usize) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        self.read_pin(pin as u8)
    }

    fn toggle(&self, pin: usize) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        self.toggle_pin(pin as u8)
    }

    fn set(&self, pin: usize) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        self.set_pin(pin as u8, PinState::High)
    }

    fn clear(&self, pin: usize) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        self.set_pin(pin as u8, PinState::Low)
    }

    fn enable_interrupt(
        &self,
        pin: usize,
        mode: hil::gpio::InterruptMode,
        identifier: usize,
    ) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        let ret = self.enable_host_interrupt();
        match ret {
            ReturnCode::SUCCESS => {
                self.identifier.set(identifier);
                self.enable_interrupt_pin(pin as u8, mode)
            }
            _ => ret,
        }
    }

    fn disable_interrupt(&self, pin: usize) -> ReturnCode {
        if pin > 7 {
            return ReturnCode::EINVAL;
        }
        self.disable_interrupt_pin(pin as u8)
    }
}