Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bumping to embedded-hal-1.0 #60

Open
hacknus opened this issue Oct 2, 2024 · 6 comments
Open

bumping to embedded-hal-1.0 #60

hacknus opened this issue Oct 2, 2024 · 6 comments

Comments

@hacknus
Copy link

hacknus commented Oct 2, 2024

Are there plans to bump this to embedded-hal-1.0?
I have not found an easy way to share an I2C and SPI bus between different tasks in embedded-hal-1.0 but it was super easy with shared-bus and embedded-hal-0.2 (I used freeRTOS).

Would be happy to help out!

@clintfred
Copy link

A PR exists to do this: #55

It's been sitting un-merged for quite some time, however.

@Sycrosity
Copy link

Sycrosity commented Oct 4, 2024

the PR is mine and the repo is here incase you want an alternative for the meantime with a Cargo.toml crates.io patch. However, if you are using embassy (or are fine with having embassy parts in your project), I'd recommend using an embassy_sync::Mutex instead (e.g. Mutex<NoopRawMutex, I2C<'static, esp_hal::peripherals::I2C0, Async>>)

@rursprung
Copy link
Contributor

in my understanding you don't need shared-bus for embedded-hal 1 anymore, you can use embedded-hal-bus instead. shared-bus is only needed for embedded-hal 0.2.x

probably there should be a shared-bus 0.4.0 release which drops the e-h 1.0.0-alpha.9 dependency (& support for it) and adds a fat note to the README about embedded-hal-bus?

@hacknus
Copy link
Author

hacknus commented Nov 13, 2024

Then I'm probably using it wrong (freeRTOS)...

use embedded_hal_bus::i2c;
// initialize i2c1
    let scl = gpiob.pb6;
    let sda = gpiob.pb7;
    let i2c1 = dp.I2C1.i2c(
        (scl, sda),
        i2cMode::Standard {
            frequency: 100.kHz(),
        },
        &clocks,
    );

    let i2c_ref_cell = RefCell::new(i2c1);
    
Task::new()
        .name("IR SENSOR TASK")
        .stack_size(1024)
        .priority(TaskPriority(3))
        .start(move || {
            let addr = SlaveAddr::default();
            let mut ir_sensor =
                Mlx9061x::new_mlx90614(i2c::RefCellDevice::new(&i2c_ref_cell), addr, 5).unwrap();
            let mut raw_channel1;
            let mut raw_channel2;
            let mut ambient_temperature;
            loop {
                raw_channel1 = ir_sensor.raw_ir_channel1();
                raw_channel2 = ir_sensor.raw_ir_channel2();
                ambient_temperature = ir_sensor.ambient_temperature();
    
                CurrentTask::delay(Duration::ms(100));
            }
        })
        .unwrap();

 Task::new()
        .name("IR TEMPERATURE TASK")
        .stack_size(1024)
        .priority(TaskPriority(3))
        .start(move || {
            let mut raw = TemperatureRawValue::Err(Ads122Error::Timeout);
            let mut temperature = TemperatureValue::Err(Ads122Error::Timeout);

            let i2c_addr = 0x40;
            let sensor_type = ADS122SensorType::PT1000;
            let mut temperature_sensor =
                TemperatureSensor::new(i2c_addr, i2c::RefCellDevice::new(&i2c_ref_cell));
            let mut temperature_sensor_state = temperature_sensor.init(sensor_type).is_ok();
            loop {
                 // do stuff
            }
        })
        .unwrap();

this gives me this error:


error[E0382]: use of moved value: `i2c_ref_cell`
   --> src/main.rs:229:16
    |
170 |     let i2c_ref_cell = RefCell::new(i2c1);
    |         ------------ move occurs because `i2c_ref_cell` has type `RefCell<stm32f4xx_hal::i2c::I2c<I2C1>>`, which does not implement the `Copy` trait
...
208 |         .start(move || {
    |                ------- value moved into closure here
...
211 |                 Mlx9061x::new_mlx90614(i2c::RefCellDevice::new(&i2c_ref_cell), addr, 5).unwrap();
    |                                                                 ------------ variable moved due to use in closure
...
229 |         .start(move || {
    |                ^^^^^^^ value used here after move
...
236 |                 TemperatureSensor::new(i2c_addr, i2c::RefCellDevice::new(&i2c_ref_cell));
    |                                                                           ------------ use occurs due to use in closure


@rursprung
Copy link
Contributor

i don't see you using embedded-hal-bus in your code?
but i'm no expert in this topic and haven't used freeRTOS. maybe ask in the rust-embedded matrix room, someone there might know?

@hacknus
Copy link
Author

hacknus commented Nov 13, 2024

@rursprung I'm creating a RefCellDevice in the constructor calls for the Devices:

use embedded_hal_bus::i2c;
i2c::RefCellDevice::new(&i2c_ref_cell)

Thanks, I'll look into the matrix room.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants