MCP23017 with ESP32 and nostd rust address problem
Posted: Thu Jun 20, 2024 10:58 am
Hi all, this is my first post
I am playing around with an ESP32-C3 and Rust, and can't seem to figure out this problem.
I have an MCP23017 which I connecting via I2C. I am roughly following this tutorial https://dev.to/theembeddedrustacean/esp ... anner-54mg.
I have the port setting pins A0-A2 all on low. I use a 10K resistor to pull up the RESET pin, and also 2K resistors to pull up data and clock lines of the I2C (i read somewhere there that this should help..).
The port scanner *sometimes* finds a device on address 33/0x21, and sometimes not all all. When all A0-A2 are set to low, the device should be on address 0x20 though. I tried various breadboards, jumper wires and also another MCP23017, and always got the same result. When I am putting only A1 to high, the device is at address 35. When I put A2 to high, it is at 37. When A0 is high, the scanner never finds the device. I'm not sure what I can try more
Cargo.toml
main.rs
I am playing around with an ESP32-C3 and Rust, and can't seem to figure out this problem.
I have an MCP23017 which I connecting via I2C. I am roughly following this tutorial https://dev.to/theembeddedrustacean/esp ... anner-54mg.
I have the port setting pins A0-A2 all on low. I use a 10K resistor to pull up the RESET pin, and also 2K resistors to pull up data and clock lines of the I2C (i read somewhere there that this should help..).
The port scanner *sometimes* finds a device on address 33/0x21, and sometimes not all all. When all A0-A2 are set to low, the device should be on address 0x20 though. I tried various breadboards, jumper wires and also another MCP23017, and always got the same result. When I am putting only A1 to high, the device is at address 35. When I put A2 to high, it is at 37. When A0 is high, the scanner never finds the device. I'm not sure what I can try more
Cargo.toml
Code: Select all
[package]
name = "tryout-9-mcp23017"
version = "0.1.0"
authors = ["tilman <tilman>"]
edition = "2021"
license = "MIT OR Apache-2.0"
[dependencies]
esp-backtrace = { version = "0.12.0", features = [
"esp32c3",
"exception-handler",
"panic-handler",
"println",
] }
esp-hal = { version = "0.18.0", features = [ "esp32c3" ] }
esp-println = { version = "0.9.1", features = ["esp32c3", "log"] }
log = { version = "0.4.21" }
mcp230xx = { git = "https://github.com/quartiq/mcp230xx.git", rev = "dcc66f4"}
[profile.dev]
# Rust debug is too slow.
# For debug builds always builds with some optimization
opt-level = "s"
[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false
Code: Select all
#![no_std]
#![no_main]
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl, delay::Delay, i2c::I2C, peripherals::{self, Peripherals}, prelude::*, system::SystemControl};
use esp_println::println;
use mcp230xx::Mcp23017;
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::max(system.clock_control).freeze();
let delay = Delay::new(&clocks);
let io = esp_hal::gpio::Io::new(peripherals.GPIO, peripherals.IO_MUX);
let scl = io.pins.gpio3;
let sda = io.pins.gpio2;
let mut i2c0 = I2C::new(
peripherals.I2C0,
sda,
scl,
100u32.kHz(),
&clocks,
None
);
let pin = Mcp23017::A7;
pub const ADDR: u8 = 0x20; // default addr
println!("addr: {ADDR}");
// let mut u = Mcp230xx::<I2C<'_, I2C0, Blocking>, Mcp23017>::new_default(i2c0).unwrap();
// let mut u:Mcp230xx::<I2C<'_, I2C0, Blocking>, Mcp23017> = Mcp230xx::new(i2c0, 33).unwrap();
// u.set_direction(pin, Direction::Output).unwrap();
// u.set_gpio(pin, Level::High).unwrap();
for addr in 1..=127 {
// Scan Address
let res = i2c0.read(addr as u8, &mut [0]);
// Check and Print Result
match res {
Ok(_) => println!("Device Found at Address {}", addr as u8),
Err(_) => (),
}
}
println!("Done scanning");
loop {
}
}