Closed pdgilbert closed 8 months ago
For completeness, here are error messages when compiling with the section I think should work:
The problem is this line in your Cargo.toml
:
embedded-hal-bus = { git = "https://github.com/rust-embedded/embedded-hal/" }
You can't mix and match dependencies from crates.io
and git
. This import causes a second embedded-hal
to be imported, which defines a (from the compiler's viewpoint) different I2c
trait than the STM32 crates implement, or the device crates expect.
The fix is as simple as replacing it with a version from crates.io
:
embedded-hal-bus = "0.1"
Thank you @bugadani . When I use
embedded-hal = "1.0"
embedded-hal-bus = "0.1"
the code compiles without error using the section I thought should work. I am still not passing the bus address to ssd1306
. If I change from
let interface = I2CDisplayInterface::new(ssd_rcd);
to
let interface = I2CDisplayInterface::new(ssd_rcd, 0x3C);
I get
error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> examples/ads_ina_ssd.rs:256:21
|
256 | ...nterface = I2CDisplayInterface::new(ssd_rcd, 0x3C); // check address
| ^^^^^^^^^^^^^^^^^^^^^^^^ ------
| | |
| | unexpected argument of type `{integer}`
| help: remove the extra argument
|
note: associated function defined here
--> /home/paul/.cargo/git/checkouts/ssd1306-407b1a92e31533f1/0bae3a6/src/i2c_interface.rs:13:12
|
13 | pub fn new<I>(i2c: I) -> I2CInterface<I>
| ^^^
Also, if I specify both hals from git:
embedded-hal = { git = "https://github.com/rust-embedded/embedded-hal/" }
embedded-hal-bus = { git = "https://github.com/rust-embedded/embedded-hal/" }
I get errors as before. I guess this is probably due to the git versions of ads1x1x
, ina219
, and ssd1306
all using the release version of embedded-hal
. It seems mixing and matching traits is more sensitive to versions than mixing and matching types.
Does this mean that all dependencies in a project must use the same version of embedded-hal
?
And, does it mean that update rules will become a problem? For example ads1x1x
specifies embedded-hal = "1"
while ssd1306
specifies embedded-hal = "1.0.0"
. Eventually some device crates will specify '1.0.1'. If I understand cargo update
, which is questionable, this can result in a mix of versions.
If the last is true, perhaps there should be a preferred way to specify the dependency to help avoid mixing versions?
Rust has no method overloading and new only takes one argument. Why do you expect it to work?
What you want is new_custom_address
(although new
already uses 0x3C
as the address, so why do you want to set it to the same value?)
Thanks @bugadani . The fog is beginning to clear, there were a few things I did not understand correctly.
Using
embedded-hal_1.0.0
I have been trying to replaceshared_bus
in several examples. @rursprung pointed out the this can be done withembedded-hal-bus
. I am finding it is not a simple replacement.I have been using
RefCellDevice
. The code below compiles withstm32f4xx_hal
and withstm32h7xx-hal
brancheh-v1.0
as updated by @richardeoin. It uses cratesads1x1x
(git = "https://github.com/eldruin/ads1x1x-rs" )ina219
(git = "https://github.com/tdittr/ina219", branch = "add-configuration-register")ssd1306
(git = "https://github.com/bugadani/ssd1306", branch = "ehal1" )all of which use
embedded-hal_1.0.0
.I have not tried to run on hardware. I am fairly sure it will not work, based on my confusion and the implication of the GIGO principle. (And also the fact that it does not pass the bus address to
ssd1306
.)My coding effort has been guided by
but I am still confused. That is probably because I am missing some basic point everyone thinks is so obvious that there is no need to mention it.
I would very much appreciate any explanations/comments/advice. My main confusion is over what parts of the implementation should be done in device crates and what needs to be done in user code. I have a poor understand of what trait implementation coherence and the orphan rule imply for where things must be done in the hals, device crates, and user code.
The code compiles with a section that does more of the implementation than I think is needed in user code. Comments in the code indicate that section as well as something closer to what I think the user should need. Some other questions are also indicated in comments.
Click to expand code
``` #![deny(unsafe_code)] #![no_std] #![no_main] #![feature(type_alias_impl_trait)] #[cfg(debug_assertions)] use panic_semihosting as _; #[cfg(not(debug_assertions))] use panic_halt as _; use cortex_m_rt::entry; use embedded_hal::delay::DelayNs; ///////////////////// ads use ads1x1x::{Ads1x1x, channel, ChannelSelection, DynamicOneShot, FullScaleRange, SlaveAddr}; ///////////////////// ina use ina219::{address::{Address, Pin}, measurements::BusVoltage, SyncIna219}; ///////////////////// ssd use ssd1306::{mode::BufferedGraphicsMode, prelude::*, I2CDisplayInterface, Ssd1306}; const DISPLAYSIZE:ssd1306::prelude::DisplaySize128x32 = DisplaySize128x32; const VPIX:i32 = 12; // vertical pixels for a line, including space use core::fmt::Write; use embedded_graphics::{ mono_font::{ascii::FONT_6X10 as FONT, MonoTextStyle, MonoTextStyleBuilder}, pixelcolor::BinaryColor, prelude::*, text::{Baseline, Text}, }; ///////////////////// hals #[cfg(feature = "stm32f4xx")] pub use stm32f4xx_hal::{ pac::{Peripherals, I2C1}, rcc::{Clocks, RccExt}, pac::{TIM2, TIM5}, timer::Delay, timer::TimerExt, gpio::GpioExt, prelude::*, block, }; #[cfg(feature = "stm32f4xx")] pub fn setup_from_dp(dp: Peripherals) -> ( impl I2c( voltage: BusVoltage, a_mv: i16, b_mv: [i16; 2], text_style: MonoTextStyle,
display: &mut Ssd1306>,
) -> ()
where
S: DisplaySize,
{
let mut lines: [heapless::String<32>; 3] = [
heapless::String::new(),
heapless::String::new(),
heapless::String::new(),
];
write!(lines[0], "ina V: {:?} mv? ", voltage).unwrap();
write!(lines[1], "ads_a V: {} mv ", a_mv).unwrap();
write!(lines[2], "ads_b A0: {} mv. ads_b A1: {} mv.", b_mv[0], b_mv[1]).unwrap();
display.clear_buffer();
for (i, line) in lines.iter().enumerate() {
// start from 0 requires that the top is used for font baseline
Text::with_baseline(
line,
Point::new(0, i as i32 * VPIX),
text_style,
Baseline::Top,
)
.draw(&mut *display)
.unwrap();
}
display.flush().unwrap();
()
}
////////////////////////////////////////////////////////////////////////////////
#[entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
let (i2cset, mut delay) = setup_from_dp(dp);
let i2cset_ref_cell = RefCell::new(i2cset);
let adc_a_rcd = RefCellDevice::new(&i2cset_ref_cell);
let adc_b_rcd = RefCellDevice::new(&i2cset_ref_cell);
let ina_rcd = RefCellDevice::new(&i2cset_ref_cell);
let ssd_rcd = RefCellDevice::new(&i2cset_ref_cell);
///////////////////// ads
let mut adc_a = Ads1x1x::new_ads1015(adc_a_rcd, SlaveAddr::Alternative(false, false)); //addr = GND
let mut adc_b = Ads1x1x::new_ads1015(adc_b_rcd, SlaveAddr::Alternative(false, true)); //addr = V
// set FullScaleRange to measure expected max voltage.
adc_a.set_full_scale_range(FullScaleRange::Within4_096V).unwrap();
adc_b.set_full_scale_range(FullScaleRange::Within4_096V).unwrap();
///////////////////// ina
let mut ina = SyncIna219::new( ina_rcd, Address::from_pins(Pin::Gnd, Pin::Gnd)).unwrap();
///////////////////// ssd
//let interface = I2CDisplayInterface::new(ssd_rcd, 0x3C); // check address
let interface = I2CDisplayInterface::new(ssd_rcd); // COMPILES BUT NOT USING ADDRESS ??
let mut disp = Ssd1306::new(interface, DISPLAYSIZE, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
disp.init().unwrap();
let text_style = MonoTextStyleBuilder::new()
.font(&FONT)
.text_color(BinaryColor::On)
.build();
Text::with_baseline(
"Display initialized ...",
Point::zero(),
text_style,
Baseline::Top,
)
.draw(&mut disp)
.unwrap();
///////////////////// measure and display in loop
loop {
let voltage = ina.bus_voltage().unwrap();
let a_mv = block!(DynamicOneShot::read(&mut adc_a, ChannelSelection::SingleA0)).unwrap_or(8091);
let values_b = [
block!(adc_b.read(channel::SingleA0)).unwrap_or(8091),
block!(adc_b.read(channel::SingleA1)).unwrap_or(8091),
];
display(voltage, a_mv, values_b, text_style, &mut disp);
delay.delay_ms(2000); // sleep for 2s
}
}
```
Click to expand Cargo.toml
``` [package] authors = ["pdGilbert"] categories = ["embedded", "no-std"] description = "ads, ina, ssd, embedded-hal-bus example" keywords = ["driver", "i2c", "embedded-hal-bus", "example"] license = "MIT OR Apache-2.0" name = "embedded-hal-bus_example" version = "0.0.1" edition = "2021" [dependencies] stm32f4xx-hal = { version = "0.20.0", optional = true } stm32h7xx-hal = { git = "https://github.com/stm32-rs/stm32h7xx-hal", optional = true , branch = "eh-v1.0"} ssd1306 = { git = "https://github.com/bugadani/ssd1306", branch = "ehal1" } ads1x1x = { git = "https://github.com/eldruin/ads1x1x-rs" } ina219 = { git = "https://github.com/tdittr/ina219", branch = "add-configuration-register" } # has eh1.0.0 embedded-hal = "1.0" embedded-hal-bus = { git = "https://github.com/rust-embedded/embedded-hal/" } embedded-graphics = ">=0.7" heapless = "0.7" cortex-m-rt = ">=0.7.0" panic-semihosting = { version = ">=0.5.2" } [features] stm32f4xx = ["stm32f4xx-hal", ] stm32h7xx = ["stm32h7xx-hal/rt", ] stm32f401 = ["stm32f4xx-hal/stm32f401" ] stm32f411 = ["stm32f4xx-hal/stm32f411" ] stm32h742 = ["stm32h7xx-hal/stm32h742" ] ```Click to expand compile results with stm32f4xx_hal
``` $ cargo build --no-default-features --target thumbv7em-none-eabihf --features stm32f401,stm32f4xx --example ads_ina_ssd Compiling proc-macro2 v1.0.78 Compiling nb v1.1.0 Compiling semver-parser v0.7.0 Compiling unicode-ident v1.0.12 Compiling cortex-m v0.7.7 Compiling embedded-hal v1.0.0 Compiling cortex-m-rt v0.7.3 Compiling critical-section v1.1.2 Compiling nb v0.1.3 Compiling syn v1.0.109 Compiling embedded-hal-async v1.0.0 Compiling void v1.0.2 Compiling semver v1.0.21 Compiling semver v0.9.0 Compiling rustc_version v0.2.3 Compiling vcell v0.1.3 Compiling volatile-register v0.2.2 Compiling embedded-hal v0.2.7 Compiling autocfg v1.1.0 Compiling az v1.2.1 Compiling byteorder v1.5.0 Compiling bitfield v0.13.2 Compiling bare-metal v0.2.5 Compiling stable_deref_trait v1.2.0 Compiling stm32f4 v0.15.1 Compiling gcd v2.3.0 Compiling rustc_version v0.4.0 Compiling display-interface v0.5.0 Compiling quote v1.0.35 Compiling powerfmt v0.2.0 Compiling cortex-m-semihosting v0.5.0 Compiling num-traits v0.2.17 Compiling syn v2.0.48 Compiling deranged v0.3.11 Compiling heapless v0.7.17 Compiling fugit v0.3.7 Compiling embedded-hal-bus v0.1.0 (https://github.com/rust-embedded/embedded-hal/#04b4f189) Compiling ssd1306 v0.8.4 (https://github.com/bugadani/ssd1306?branch=ehal1#0bae3a66) Compiling bare-metal v1.0.0 Compiling litrs v0.4.1 Compiling byte-slice-cast v1.2.2 Compiling ina219 v0.2.0 (https://github.com/tdittr/ina219?branch=add-configuration-register#0b13baf2) Compiling embedded-graphics-core v0.4.0 Compiling stm32f4xx-hal v0.20.0 Compiling time-core v0.1.2 Compiling time v0.3.31 Compiling float-cmp v0.9.0 Compiling display-interface-spi v0.5.0 Compiling fugit-timer v0.1.3 Compiling document-features v0.2.8 Compiling display-interface-i2c v0.5.0 Compiling embedded-dma v0.2.0 Compiling hash32 v0.2.1 Compiling embedded-hal-nb v1.0.0 Compiling embedded-hal v1.0.0 (https://github.com/rust-embedded/embedded-hal/#04b4f189) Compiling micromath v2.1.0 Compiling rand_core v0.6.4 Compiling embedded-storage v0.3.1 Compiling embedded-graphics v0.8.1 Compiling panic-semihosting v0.6.0 Compiling ads1x1x v0.2.2 (https://github.com/eldruin/ads1x1x-rs#5cd36134) Compiling enumflags2_derive v0.7.8 Compiling cortex-m-rt-macros v0.7.0 Compiling enumflags2 v0.7.8 Compiling embedded-hal-bus_example v0.0.1 (/ads_ina_ssd) Finished dev [unoptimized + debuginfo] target(s) in 19.69s ```Click to expand compile results with stm32h7xx_hal
``` $ cargo build --no-default-features --target thumbv7em-none-eabihf --features stm32h742,stm32h7xx --example ads_ina_ssd Compiling nb v0.1.3 Compiling paste v1.0.14 Compiling stm32h7 v0.15.1 Compiling cast v0.3.0 Compiling embedded-io v0.6.1 Compiling embedded-hal v0.2.7 Compiling cortex-m v0.7.7 Compiling cortex-m-semihosting v0.5.0 Compiling panic-semihosting v0.6.0 Compiling stm32h7xx-hal v0.15.1 (https://github.com/stm32-rs/stm32h7xx-hal?branch=eh-v1.0#b05811ce) Compiling embedded-hal-bus_example v0.0.1 (/ads_ina_ssd) Finished dev [unoptimized + debuginfo] target(s) in 22.18s ```