Gu-Cho
Gu-Cho

Reputation: 11

Referencing an object in Rust

I'd like to reference an object slow_uart which was defined outside of the loop:

fn main() -> ! {
    // .....
    // Initializations 
   
    let slow_bdrate = <Rate<u32, 1, 1>>::Hz(120000);
    let slow_uart = UartConfig::new(
        slow_bdrate,
        uart::DataBits::Eight,
        None,
        uart::StopBits::One,
    );

    let data: u8 = 0;
    let uart_peripheral = UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS);

    loop {
        
       uart_peripheral.enable(slow_uart, clocks.peripheral_clock.freq())
            .unwrap();

        uart_peripheral.write_full_blocking(&data);

        delay.delay_ms(100);
    }
}

I'm new to Rust, but is there any other way of accesing objects outside of the loop? Compiler suggests using a Copy() trait but seems like it is not implemented for this function. Can I reference it using a something similar to pointers in C++? What are the good practices here?

I tried moving uart_peripheral into the loop but them I had to move all other instances of its arguments as well, which I would rather not do.

Help much appriciated

Edit

Here's and output from a compiler:

error[E0382]: use of moved value: `uart_peripheral`
   --> blinky.rs:131:9
    |
112 |     let mut uart_peripheral = UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS);
    |         -------- move occurs because `uart_peripheral` has type `UartPeripheral<rp2040_hal::uart::Disabled, UART0, (rp2040_hal::gpio::Pin<Gpio0, FunctionUart, PullDown>, rp2040_hal::gpio::Pin<Gpio1, FunctionUart, PullDown>)>`, which does not implement the `Copy` trait
...
126 |     loop {
    |     ---- inside of this loop
...
131 |         uart_peripheral.enable(slow_uart, clocks.peripheral_clock.freq())
    |         ^^^^ value moved here, in previous iteration of loop

error[E0382]: use of moved value: `slow_uart`
   --> blinky.rs:131:21
    |
98  |     let slow_uart = UartConfig::new(
    |         --------- move occurs because `slow_uart` has type `UartConfig`, which does not implement the `Copy` trait
...
126 |     loop {
    |     ---- inside of this loop
...
131 |         uart_peripheral.enable(slow_uart, clocks.peripheral_clock.freq())
    |                     ^^^^^^^^^ value moved here, in previous iteration of loop

Upvotes: 0

Views: 69

Answers (1)

Finomnis
Finomnis

Reputation: 22476

As the code you posted is not reproducible, I will answer, but with the discalimer that the posted code is not tested. So there might still be other mistakes in there.

The issue you are facing is that the UartPeripheral::enable function seems to consume the peripheral object and resturns a new, enabled object. This can only be done once, and is also only required once.

So if you simply move the enable out of your loop and use the returned enabled object instead of the original one for write_full_blocking, it should work in theory. Again, not tested, as your code is not fully reproducible.

fn main() -> ! {
    // .....
    // Initializations 
   
    let slow_bdrate = <Rate<u32, 1, 1>>::Hz(120000);
    let slow_uart = UartConfig::new(
        slow_bdrate,
        uart::DataBits::Eight,
        None,
        uart::StopBits::One,
    );

    let data: u8 = 0;
    let uart_peripheral = UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS);

    let enabled_uart_peripheral = uart_peripheral.enable(slow_uart, locks.peripheral_clock.freq()).unwrap();

    loop {
        enabled_uart_peripheral.write_full_blocking(&data);

        delay.delay_ms(100);
    }
}

Upvotes: 2

Related Questions