esp-rs / esp-idf-hal

embedded-hal implementation for Rust on ESP32 and ESP-IDF
https://docs.esp-rs.org/esp-idf-hal/
Apache License 2.0
462 stars 171 forks source link

Error is not propagated with ? #6

Closed andresv closed 3 years ago

andresv commented 3 years ago

ESP32 TTGO, rust-1.55-dev.

Notice how serial is created and its error is propagated with ?. Main should print error if something happens in run().

#![allow(clippy::single_component_path_imports)]

use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::{Condvar, Mutex};
use std::{cell::RefCell, env, sync::atomic::*, sync::Arc, thread, time::*};

use anyhow::*;
use log::*;

use embedded_svc::anyerror::*;
use embedded_svc::io;

use esp_idf_svc::nvs::*;
use esp_idf_svc::sysloop::*;

use esp_idf_hal::delay;
use esp_idf_hal::gpio;
use esp_idf_hal::prelude::*;
use esp_idf_hal::serial;
use esp_idf_hal::spi;

use esp_idf_sys;
use esp_idf_sys::{esp, EspError};

use display_interface_spi::SPIInterfaceNoCS;

use embedded_graphics::mono_font::{ascii::FONT_10X20, MonoTextStyle};
use embedded_graphics::pixelcolor::*;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::*;
use embedded_graphics::text::*;

use st7789;

thread_local! {
    static TLS: RefCell<u32> = RefCell::new(13);
}

fn main() -> Result<()> {
    // bind the log crate to the ESP Logging facilities
    esp_idf_svc::log::EspLogger::initialize_default();
    env::set_var("RUST_BACKTRACE", "1");

    if let Err(e) = run() {
        error!("run err: {:?}", e);
    }

    loop {
        info!("X");
        thread::sleep(Duration::from_secs(1));
    }
    Ok(())
}

fn run() -> Result<()> {
    use std::fmt::Write;
    let peripherals = Peripherals::take().unwrap();
    let pins = peripherals.pins;

    let config = serial::config::Config::default().baudrate(Hertz(115_200));

    info!("setup serial");
    let mut serial: serial::Serial<_, _, _> = serial::Serial::new(
        peripherals.uart1,
        serial::Pins {
            tx: pins.gpio27.into_output()?,
            rx: pins.gpio26.into_input()?,
            cts: None,
            rts: None,
        },
        config,
    )?;

    // match serial::Serial::new(
    //     peripherals.uart1,
    //     serial::Pins {
    //         tx: pins.gpio27.into_output()?,
    //         rx: pins.gpio26.into_input()?,
    //         cts: Option::<gpio::Gpio21<gpio::Unknown>>::None,
    //         rts: Option::<gpio::Gpio22<gpio::Unknown>>::None,
    //     },
    //     config,
    // ) {
    //     Ok(serial) => {}
    //     Err(e) => error!("serial {:?}", e),
    // }

    info!("spin..");
    let mut s: usize = 0;
    //loop {
    while s < 10 {
        info!("secs {}", s);
        //writeln!(serial, "{}", format!("secs {}", s)).unwrap();

        thread::sleep(Duration::from_secs(1));
        s += 1;
    }

    Ok(())
}

Log:

cargo espflash /dev/tty.usbserial-0201CA3F
...
...
...

cargo espmonitor /dev/tty.usbserial-0201CA3F                                                                                                    22s  ●  ~/D/y/esp32rogue
ESPMonitor 0.5.2

Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

Opening /dev/tty.usbserial-0201CA3F with speed 115200
WARNING: Flash image /Users/andres/Development/yaak/esp32rogue/target/xtensa-esp32-none-elf/debug/esp32rogue does not exist (you may need to build it)
Resetting device... done
ets Jun  8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0048,len:12
ho 0 tail 12 room 4
load:0x3fff0054,len:4800
load:0x40078000,len:17448
load:0x4007c428,len:4840
entry 0x4007c6a0
I (162) cpu_start: Pro cpu up.
I (162) cpu_start: Starting app cpu, entry point is 0x400816b4
I (0) cpu_start: App cpu up.
I (176) cpu_start: Pro cpu start user code
I (177) cpu_start: cpu freq: 160000000
I (177) cpu_start: Application information:
I (181) cpu_start: Project name:     esp-idf
I (186) cpu_start: App version:      b8b0138
I (191) cpu_start: Compile time:     Oct 21 2021 14:26:10
I (197) cpu_start: ELF file SHA256:  0000000000000000...
I (203) cpu_start: ESP-IDF:          4.3.0
I (208) heap_init: Initializing. RAM available for dynamic allocation:
I (215) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (221) heap_init: At 3FFB3508 len 0002CAF8 (178 KiB): DRAM
I (227) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (234) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (240) heap_init: At 4008CFB4 len 0001304C (76 KiB): IRAM
I (247) spi_flash: detected chip: generic
I (251) spi_flash: flash io: dio

And the same thing if this is used:

match serial::Serial::new(
    peripherals.uart1,
    serial::Pins {
        tx: pins.gpio27.into_output()?,
        rx: pins.gpio26.into_input()?,
        cts: Option::<gpio::Gpio21<gpio::Unknown>>::None,
        rts: Option::<gpio::Gpio22<gpio::Unknown>>::None,
    },
    config,
) {
    Ok(serial) => {}
    Err(e) => error!("serial {:?}", e),
}

Log:

cargo espflash /dev/tty.usbserial-0201CA3F
...
...
...

cargo espmonitor /dev/tty.usbserial-0201CA3F

ESPMonitor 0.5.2

Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

Opening /dev/tty.usbserial-0201CA3F with speed 115200
WARNING: Flash image /Users/andres/Development/yaak/esp32rogue/target/xtensa-esp32-none-elf/debug/esp32rogue does not exist (you may need to build it)
Resetting device... done
ets Jun  8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0048,len:12
ho 0 tail 12 room 4
load:0x3fff0054,len:4800
load:0x40078000,len:17448
load:0x4007c428,len:4840
entry 0x4007c6a0
I (162) cpu_start: Pro cpu up.
I (162) cpu_start: Starting app cpu, entry point is 0x400816b4
I (0) cpu_start: App cpu up.
I (177) cpu_start: Pro cpu start user code
I (177) cpu_start: cpu freq: 160000000
I (177) cpu_start: Application information:
I (181) cpu_start: Project name:     esp-idf
I (186) cpu_start: App version:      b8b0138
I (191) cpu_start: Compile time:     Oct 21 2021 14:26:10
I (197) cpu_start: ELF file SHA256:  0000000000000000...
I (203) cpu_start: ESP-IDF:          4.3.0
I (208) heap_init: Initializing. RAM available for dynamic allocation:
I (215) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (221) heap_init: At 3FFB3508 len 0002CAF8 (178 KiB): DRAM
I (227) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (234) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (240) heap_init: At 4008CFB4 len 0001304C (76 KiB): IRAM
I (247) spi_flash: detected chip: generic
I (251) spi_flash: flash io: dio
I (256) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (267) esp32rogue: setup serial
E (267) uart: uart_driver_install(1323): uart rx buffer length error
E (277) esp32rogue: serial EspError(-1)
I (287) esp32rogue: spin..
I (287) esp32rogue: secs 0
I (1287) esp32rogue: secs 1
I (2287) esp32rogue: secs 2
I (3287) esp32rogue: secs 3

Notice that this time it printed uart error message and we also see log line from main loop.

andresv commented 3 years ago

It was caused by bugs in serial driver. This fixes it: https://github.com/esp-rs/esp-idf-hal/pull/7/commits/f935e3f3c8c97156b42ea4d7d3282da446e7b03f

It seems calling wrong args in there caused havoc everywhere.