|
| 1 | +use embedded_hal::blocking::delay::{DelayMs, DelayUs}; |
| 2 | +use embedded_hal::blocking::i2c::Write; |
| 3 | + |
| 4 | +use crate::{bus::DataBus, error::Result}; |
| 5 | + |
| 6 | +/// This module supports I2C backpacks with a MCP23008 IC, like |
| 7 | +/// the one from adafruit. |
| 8 | +/// Connections as follows: |
| 9 | +/// |
| 10 | +/// <table> |
| 11 | +/// <tr><th>MCP23008 pin</th><th>name</th><th>LCD pin</th></tr> |
| 12 | +/// <tr><td>0</td><td>N/C</td><td></td></tr> |
| 13 | +/// <tr><td>1</td><td>RS</td><td>4</td></tr> |
| 14 | +/// <tr><td>2</td><td>E</td><td>6</td></tr> |
| 15 | +/// <tr><td>3</td><td>DB4</td><td>11</td></tr> |
| 16 | +/// <tr><td>4</td><td>DB5</td><td>12</td></tr> |
| 17 | +/// <tr><td>5</td><td>DB6</td><td>13</td></tr> |
| 18 | +/// <tr><td>6</td><td>DB7</td><td>14</td></tr> |
| 19 | +/// <tr><td>7</td><td>Backlight</td><td></td></tr> |
| 20 | +/// </table> |
| 21 | +
|
| 22 | +pub struct I2CMCP23008Bus<I2C: Write> { |
| 23 | + i2c_bus: I2C, |
| 24 | + address: u8, |
| 25 | + backlight: u8, |
| 26 | +} |
| 27 | + |
| 28 | +const REG_IODIR: u8 = 0x00; |
| 29 | +const REG_GPIO: u8 = 0x09; |
| 30 | + |
| 31 | +impl<I2C: Write> I2CMCP23008Bus<I2C> { |
| 32 | + /// Create a new instance of the MCP23008 I2C driver. The address of those |
| 33 | + /// devices is 0b010_0xxx where x is configured by bootstrap pins. |
| 34 | + pub fn new(i2c_bus: I2C, address: u8, backlight: bool) -> Result<I2CMCP23008Bus<I2C>> { |
| 35 | + let backlight = if backlight { 0b1000_0000 } else { 0 }; |
| 36 | + let mut mcp23008 = I2CMCP23008Bus { i2c_bus, address, backlight }; |
| 37 | + // Set to reset values according to datasheet |
| 38 | + mcp23008.write_reg(REG_IODIR, 0b1111_1111)?; |
| 39 | + for reg in 0x01u8..0x0A { |
| 40 | + mcp23008.write_reg(reg, 0)?; |
| 41 | + } |
| 42 | + // Configure pins 1..=7 as outputs, see pin mapping above |
| 43 | + mcp23008.write_reg(REG_IODIR, 0b0000_0001)?; |
| 44 | + Ok(mcp23008) |
| 45 | + } |
| 46 | + |
| 47 | + /// Turns the backlight on or off based on the value of backlight. |
| 48 | + pub fn set_backlight(&mut self, backlight: bool) -> Result<()> { |
| 49 | + self.backlight = if backlight { 0b1000_0000 } else { 0 }; |
| 50 | + self.set_pins(self.backlight) |
| 51 | + } |
| 52 | + |
| 53 | + fn write_reg(&mut self, reg: u8, value: u8) -> Result<()> { |
| 54 | + let data = [reg, value]; |
| 55 | + self.i2c_bus.write(self.address, &data).map_err(|_| crate::error::Error) |
| 56 | + } |
| 57 | + |
| 58 | + fn set_pins(&mut self, pins: u8) -> Result<()> { |
| 59 | + self.write_reg(REG_GPIO, pins) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<I2C: Write> DataBus for I2CMCP23008Bus<I2C> { |
| 64 | + fn write<D: DelayUs<u16> + DelayMs<u8>>(&mut self, byte: u8, data: bool, delay: &mut D) -> Result<()> { |
| 65 | + let en = 0b0000_0100; |
| 66 | + let backlight = self.backlight; |
| 67 | + |
| 68 | + let rs = backlight | if data { 0b10 } else { 0b00 }; |
| 69 | + let upper_nibble = rs | (byte & 0xF0) >> 1; |
| 70 | + let lower_nibble = rs | (byte & 0x0F) << 3; |
| 71 | + |
| 72 | + // RS and R/W need to be set 40ns before E goes high. |
| 73 | + // R/W is tied to GND, so only do RS. |
| 74 | + self.set_pins(rs)?; |
| 75 | + delay.delay_us(1); |
| 76 | + |
| 77 | + // E can go high at the same time as changing the data pins. |
| 78 | + self.set_pins(en | upper_nibble)?; |
| 79 | + delay.delay_us(1); // E needs to be high for at least 230ns. |
| 80 | + |
| 81 | + // E is falling edge triggered and the data bits have to be valid for |
| 82 | + // 80ns before and 10ns after. |
| 83 | + self.set_pins(upper_nibble)?; |
| 84 | + delay.delay_us(1); // E's total cycle time needs to be 500ns. |
| 85 | + |
| 86 | + // Same as above for lower nibble. |
| 87 | + self.set_pins(en | lower_nibble)?; |
| 88 | + delay.delay_us(1); |
| 89 | + self.set_pins(lower_nibble)?; |
| 90 | + delay.delay_us(1); // Caller will wait for command processing. |
| 91 | + |
| 92 | + // Reset all pins other than the backlight for next time. |
| 93 | + self.set_pins(backlight)?; |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | +} |
0 commit comments