Struct Handle

Source
pub struct Handle { /* private fields */ }
Expand description

An MCP device that has been opened. Supports I2C and GPIO operations.

Implementations§

Source§

impl Handle

Source

pub fn open_first(config: &Config) -> Result<Self>

A shortcut that just opens the first supported device that we can find.

Examples found in repository?
examples/scan_bus.rs (line 21)
14fn run() -> mcp2221::Result<()> {
15    let mut config = mcp2221::Config::default();
16    config.i2c_speed_hz = 400_000;
17    // For talking to a peripheral we might want a higher timeout, but for
18    // scanning the bus, a short timeout is good since it allows us to scan all
19    // addresses more quickly.
20    config.timeout = Duration::from_millis(10);
21    let mut dev = mcp2221::Handle::open_first(&config)?;
22
23    // Set GPIO pin 0 high. This is useful if your I2C bus goes through a level
24    // shifter and you need to enable that level shifter in order to use the I2C
25    // bus. It also serves as an example of using GPIO.
26    let mut gpio_config = mcp2221::GpioConfig::default();
27    gpio_config.set_direction(0, mcp2221::Direction::Output);
28    gpio_config.set_value(0, true);
29    dev.configure_gpio(&gpio_config)?;
30
31    // Before we start, SDA and SCL should be high. If they're not, then either
32    // the pull-up resistors are missing, the bus isn't properly connected or
33    // something on the bus is holding them low. In any case, we won't be able
34    // to operate.
35    dev.check_bus()?;
36
37    println!("{}", dev.get_device_info()?);
38
39    for base_address in (0..=127).step_by(16) {
40        for offset in 0..=15 {
41            let address = base_address + offset;
42            match dev.read(address, &mut [0u8]) {
43                Ok(_) => print!("0x{:02x}", address),
44                Err(_) => print!(" -- "),
45            }
46        }
47        println!();
48    }
49
50    Ok(())
51}
Source

pub fn check_bus(&mut self) -> Result<()>

Can be called on startup to check that the bus is idle. Returns an error if either SCL or SDA is being held low by something on the bus.

Examples found in repository?
examples/scan_bus.rs (line 35)
14fn run() -> mcp2221::Result<()> {
15    let mut config = mcp2221::Config::default();
16    config.i2c_speed_hz = 400_000;
17    // For talking to a peripheral we might want a higher timeout, but for
18    // scanning the bus, a short timeout is good since it allows us to scan all
19    // addresses more quickly.
20    config.timeout = Duration::from_millis(10);
21    let mut dev = mcp2221::Handle::open_first(&config)?;
22
23    // Set GPIO pin 0 high. This is useful if your I2C bus goes through a level
24    // shifter and you need to enable that level shifter in order to use the I2C
25    // bus. It also serves as an example of using GPIO.
26    let mut gpio_config = mcp2221::GpioConfig::default();
27    gpio_config.set_direction(0, mcp2221::Direction::Output);
28    gpio_config.set_value(0, true);
29    dev.configure_gpio(&gpio_config)?;
30
31    // Before we start, SDA and SCL should be high. If they're not, then either
32    // the pull-up resistors are missing, the bus isn't properly connected or
33    // something on the bus is holding them low. In any case, we won't be able
34    // to operate.
35    dev.check_bus()?;
36
37    println!("{}", dev.get_device_info()?);
38
39    for base_address in (0..=127).step_by(16) {
40        for offset in 0..=15 {
41            let address = base_address + offset;
42            match dev.read(address, &mut [0u8]) {
43                Ok(_) => print!("0x{:02x}", address),
44                Err(_) => print!(" -- "),
45            }
46        }
47        println!();
48    }
49
50    Ok(())
51}
Source

pub fn scl_is_high(&mut self) -> Result<bool>

Source

pub fn sda_is_high(&mut self) -> Result<bool>

Source

pub fn write_read_address( &mut self, device_address: u8, to_write: &[u8], to_read: &mut [u8], ) -> Result<()>

Sends START to device_address, then writes to_write. If to_read is non-empty, sends START again, then reads into to_read. Finally sends STOP.

Source

pub fn configure_gpio(&mut self, commands: &GpioConfig) -> Result<()>

Examples found in repository?
examples/scan_bus.rs (line 29)
14fn run() -> mcp2221::Result<()> {
15    let mut config = mcp2221::Config::default();
16    config.i2c_speed_hz = 400_000;
17    // For talking to a peripheral we might want a higher timeout, but for
18    // scanning the bus, a short timeout is good since it allows us to scan all
19    // addresses more quickly.
20    config.timeout = Duration::from_millis(10);
21    let mut dev = mcp2221::Handle::open_first(&config)?;
22
23    // Set GPIO pin 0 high. This is useful if your I2C bus goes through a level
24    // shifter and you need to enable that level shifter in order to use the I2C
25    // bus. It also serves as an example of using GPIO.
26    let mut gpio_config = mcp2221::GpioConfig::default();
27    gpio_config.set_direction(0, mcp2221::Direction::Output);
28    gpio_config.set_value(0, true);
29    dev.configure_gpio(&gpio_config)?;
30
31    // Before we start, SDA and SCL should be high. If they're not, then either
32    // the pull-up resistors are missing, the bus isn't properly connected or
33    // something on the bus is holding them low. In any case, we won't be able
34    // to operate.
35    dev.check_bus()?;
36
37    println!("{}", dev.get_device_info()?);
38
39    for base_address in (0..=127).step_by(16) {
40        for offset in 0..=15 {
41            let address = base_address + offset;
42            match dev.read(address, &mut [0u8]) {
43                Ok(_) => print!("0x{:02x}", address),
44                Err(_) => print!(" -- "),
45            }
46        }
47        println!();
48    }
49
50    Ok(())
51}
Source

pub fn get_gpio_config(&mut self) -> Result<GpioConfig>

Source

pub fn get_pin_values(&mut self) -> Result<PinValues>

Source

pub fn get_device_info(&mut self) -> Result<DeviceInfo>

Returns information about the device.

Examples found in repository?
examples/scan_bus.rs (line 37)
14fn run() -> mcp2221::Result<()> {
15    let mut config = mcp2221::Config::default();
16    config.i2c_speed_hz = 400_000;
17    // For talking to a peripheral we might want a higher timeout, but for
18    // scanning the bus, a short timeout is good since it allows us to scan all
19    // addresses more quickly.
20    config.timeout = Duration::from_millis(10);
21    let mut dev = mcp2221::Handle::open_first(&config)?;
22
23    // Set GPIO pin 0 high. This is useful if your I2C bus goes through a level
24    // shifter and you need to enable that level shifter in order to use the I2C
25    // bus. It also serves as an example of using GPIO.
26    let mut gpio_config = mcp2221::GpioConfig::default();
27    gpio_config.set_direction(0, mcp2221::Direction::Output);
28    gpio_config.set_value(0, true);
29    dev.configure_gpio(&gpio_config)?;
30
31    // Before we start, SDA and SCL should be high. If they're not, then either
32    // the pull-up resistors are missing, the bus isn't properly connected or
33    // something on the bus is holding them low. In any case, we won't be able
34    // to operate.
35    dev.check_bus()?;
36
37    println!("{}", dev.get_device_info()?);
38
39    for base_address in (0..=127).step_by(16) {
40        for offset in 0..=15 {
41            let address = base_address + offset;
42            match dev.read(address, &mut [0u8]) {
43                Ok(_) => print!("0x{:02x}", address),
44                Err(_) => print!(" -- "),
45            }
46        }
47        println!();
48    }
49
50    Ok(())
51}

Trait Implementations§

Source§

impl Read for Handle

Source§

type Error = Error

Error type
Source§

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

Reads enough bytes from slave with address to fill buffer Read more
Source§

impl Write for Handle

Source§

type Error = Error

Error type
Source§

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

Writes bytes to slave with address address Read more
Source§

impl WriteRead for Handle

Source§

type Error = Error

Error type
Source§

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

Writes bytes to slave with address address and then reads enough bytes to fill buffer in a single transaction Read more

Auto Trait Implementations§

§

impl Freeze for Handle

§

impl RefUnwindSafe for Handle

§

impl Send for Handle

§

impl Sync for Handle

§

impl Unpin for Handle

§

impl UnwindSafe for Handle

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.