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

Cant assign IO (esp_hal::gpio::IO) to a struct or static for later use #1510

Closed
Gibbz opened this issue Apr 25, 2024 · 1 comment
Closed

Comments

@Gibbz
Copy link

Gibbz commented Apr 25, 2024

I'm not sure if i am doing this the right way. But I want to store the reference to IO in my function to either a struct, or static so that I can use it from other modules to access the gpio pins. When I try to assign it it complains that the method does not implement copy....

If i comment out the i2c code it works, but I assume ill have a similar issue in the future....

Edit: the same happens with peripherals if I try to store that too.

Error:

use of partially moved value: `io`
partial move occurs because `io.pins.gpio2` has type `GpioPin<esp_hal::gpio::Unknown, 2>`, which does not implement the `Copy` trait

minimal example:

use esp_hal::gpio::IO;

pub struct System {
    pub io: IO,
}


impl System  {
    pub fn init() -> Self {
        let peripherals = Peripherals::take();
        let system = peripherals.SYSTEM.split();
        let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
        let myio = IO::new(peripherals.GPIO, peripherals.IO_MUX);

        // Initialize and configure I2C0
        let mut i2c0 = I2C::new_async(
            peripherals.I2C0,
            myio.pins.gpio3,
            myio.pins.gpio2,
            100u32.kHz(),
            &clocks,
        );
        Self {
            io: myio, // <-- error here **
        }
    }
}

@MabezDev
Copy link
Member

Hey @Gibbz! Rust is stopping you from moving a value in which some of its sub fields have been moved. To combat this you should also move the pins you want for later and store those in the struct instead :).

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

No branches or pull requests

2 participants