#Struct with UART instance members.

18 messages · Page 1 of 1 (latest)

weary niche
#

Hello. I would like to create a struct / impl which takes two instances of UART peripherals as arguments.
Looking into the source code of the hal, it seems that the trait I need to use is Instance but I have failed to implement this.

To explain further, here is where the instances are created in my main.rs file.

let mut serial0 = Uart::new_with_config(peripherals.UART0, config, Some(sZeroPins), &clocks);
let mut serial1 = Uart::new_with_config(peripherals.UART1, config, Some(sOnePins), &clocks);

The object I would like to create would be instantiated like so:

let mut network = Network::new(serial0, serial1);

When I try to do this, the compiler gives me the following error:

the trait `_esp_hal_uart_Instance` is not implemented for `esp32c3_hal::Uart<'_, esp32c3_hal::peripherals::UART0>

This is the code in the module I am creating:

use esp32c3_hal::uart::*;

pub struct Network<INPUT, OUTPUT> 
where
    INPUT: Instance,
    OUTPUT: Instance
{
    input: INPUT,
    output: OUTPUT
}

impl<INPUT, OUTPUT> Network<INPUT, OUTPUT> 
where
    INPUT: Instance,
    OUTPUT: Instance
{
    pub fn new(input: INPUT, output: OUTPUT) -> Self {
        Network {
            input,
            output
        }
    }
}

When I try to call functions such as this:

for b in b"Forwarded CMD\n" {
    self.input.write(*b).ok
}

I get the following error:

impl<INPUT, OUTPUT> Network<INPUT, OUTPUT>
   |      ----- method `write` not found for this type parameter
...
41 |                         self.input.write(*b).ok();
   |                                    ^^^^^ method not found in `INPUT`

The Instance trait is defined here: https://github.com/esp-rs/esp-hal/blob/main/esp-hal/src/uart.rs#L930

#

I have tried adding Instance + embedded_hal::serial::Write<u8> and many other variations / failed attempts. Everything I try seems to lead to more errors and more confusion. As I continue to learn Rust, I am trying to divide my code into modules and separate files. Doing everything in main.rs is not a problem because I don't have to pass objects as parameters to functions in other files, which requires knowing exactly what these objects are to define them properly. I did successfully implement a struct / impl that accepts InputPin and OutputPin traits but these UART objects have been tricky for me.

Any information / help would be greatly appreciated.

lusty quiver
#

SCREAMING_SNAKE is for statics and constants so i'd recommend changing th enames of those generic params

let me look at the trait 1 second

lusty quiver
#

nothing about Instance says that write needs to be present

#

theres a bunch of other functions called write in many places, u need to find the one u want

weary niche
# lusty quiver look here https://docs.rs/esp32-hal/latest/esp32_hal/uart/trait.Instance.html

Thank you. I tried using esp32c3_hal::uart::Instance.
I have removed any calls to write and read for now, so I can move on to that problem later.
The error I get with esp32c3_hal::uart::Instance using my new() function is this:

73 |     let mut network = Network::new(serial0, serial1);
   |                       ------------ ^^^^^^^ the trait `_esp_hal_uart_Instance` is not implemented for `esp32c3_hal::Uart<'_, esp32c3_hal::peripherals::UART0>`
lusty quiver
#

this doesn't really mean anything to me out of context

weary niche
# lusty quiver is it this new here

Yes, that is the one. I am looking here. The naming conventions are confusing to me. I am using esp32c3_hal / esp_hal. This doc is from esp32_hal but it seems to be what I am looking for: https://docs.rs/esp32-hal/latest/esp32_hal/peripherals/struct.UART0.html#impl-_esp_hal_uart_Instance-for-UART0
So, from what I am reading here, it seems the esp_hal_uart_Instance trait is implemented for esp32::peripherals::UART0 but not for esp32c3_hal::peripherals::UART0 which is what I am using. Based on the error message anyway.

weary niche
lusty quiver
#

if u want the docs to be 100% applicable yes but

#

u dont have to

#

u can read the source code of 0.15 to see the doc comments there