pub struct Handle { /* private fields */ }
Expand description
An MCP device that has been opened. Supports I2C and GPIO operations.
Implementations§
Source§impl Handle
impl Handle
Sourcepub fn open_first(config: &Config) -> Result<Self>
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}
Sourcepub fn check_bus(&mut self) -> Result<()>
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}
pub fn scl_is_high(&mut self) -> Result<bool>
pub fn sda_is_high(&mut self) -> Result<bool>
Sourcepub fn write_read_address(
&mut self,
device_address: u8,
to_write: &[u8],
to_read: &mut [u8],
) -> Result<()>
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.
Sourcepub fn configure_gpio(&mut self, commands: &GpioConfig) -> Result<()>
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}
pub fn get_gpio_config(&mut self) -> Result<GpioConfig>
pub fn get_pin_values(&mut self) -> Result<PinValues>
Sourcepub fn get_device_info(&mut self) -> Result<DeviceInfo>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more