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

"embedded_io_async::Read::read" it's these not works #1359

Closed
goiw111 opened this issue Mar 29, 2024 · 5 comments
Closed

"embedded_io_async::Read::read" it's these not works #1359

goiw111 opened this issue Mar 29, 2024 · 5 comments

Comments

@goiw111
Copy link

goiw111 commented Mar 29, 2024

Hello guys can you help me please

the code is not working for me

[output]

Finished dev [optimized + debuginfo] target(s) in 0.42s
 Running `espflash flash --monitor target/xtensa-esp32-none-elf/debug/dron-test`

[2024-03-29T18:35:34Z INFO ] Serial port: '/dev/ttyACM0'
[2024-03-29T18:35:34Z INFO ] Connecting...
[2024-03-29T18:35:35Z INFO ] Using flash stub
Chip type: esp32 (revision v1.0)
Crystal frequency: 40 MHz
Flash size: 4MB
Features: WiFi, BT, Dual Core, 240MHz, Coding Scheme None
MAC address: e8:68:e7:2b:52:80
App/part. size: 90,608/4,128,768 bytes, 2.19%
[2024-03-29T18:35:36Z INFO ] Segment at address '0x1000' has not changed, skipping write
[2024-03-29T18:35:36Z INFO ] Segment at address '0x8000' has not changed, skipping write
[00:00:02] [========================================] 25/25 0x10000 [2024-03-29T18:35:39Z INFO ] Flashing has completed!
Commands:
CTRL+R Reset chip
CTRL+C Exit

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:0x3fff0030,len:7104
load:0x40078000,len:15576
load:0x40080400,len:4
ho 8 tail 4 room 4
load:0x40080404,len:3876
entry 0x4008064c
0x4008064c - core::fmt::Arguments::new_const
at /home/sohayl/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:325
I (30) boot: ESP-IDF v5.1-beta1-378-gea5e0ff298-dirt 2nd stage bootloader
I (31) boot: compile time Jun 7 2023 07:48:23
I (33) boot: Multicore bootloader
I (37) boot: chip revision: v1.0
I (41) boot.esp32: SPI Speed : 40MHz
I (45) boot.esp32: SPI Mode : DIO
I (50) boot.esp32: SPI Flash Size : 4MB
I (54) boot: Enabling RNG early entropy source...
I (60) boot: Partition Table:
I (63) boot: ## Label Usage Type ST Offset Length
I (71) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (78) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (86) boot: 2 factory factory app 00 00 00010000 003f0000
I (93) boot: End of partition table
I (97) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=035c0h ( 13760) map
I (111) esp_image: segment 1: paddr=000135e8 vaddr=3ffc0000 size=00004h ( 4) load
I (114) esp_image: segment 2: paddr=000135f4 vaddr=40080000 size=015ech ( 5612) load
I (125) esp_image: segment 3: paddr=00014be8 vaddr=00000000 size=0b430h ( 46128)
I (147) esp_image: segment 4: paddr=00020020 vaddr=400d0020 size=061a0h ( 24992) map
I (157) boot: Loaded app from partition at offset 0x10000
I (158) boot: Disabling RNG early entropy source...
//i can write nothing here

[main.rs]

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use embassy_executor::Spawner;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, pipe::Pipe};
use esp_hal::{
clock::ClockControl,
embassy,
peripherals::{Peripherals, UART0},
prelude::*,
uart::{config::AtCmdConfig, UartRx, UartTx},
Uart,
};
use esp_backtrace as _;

// Read Buffer Size
const READ_BUF_SIZE: usize = 64;

// End of Transmission Character (Carrige Return -> 13 or 0x0D in ASCII)
const AT_CMD: u8 = 0x0D;

// Declare Pipe sync primitive to share data among Tx and Rx tasks
static DATAPIPE: Pipe<CriticalSectionRawMutex, READ_BUF_SIZE> = Pipe::new();

#[embassy_executor::task]
async fn uart_writer(mut tx: UartTx<'static, UART0>) {
// Declare write buffer to store Tx characters
let mut wbuf: [u8; READ_BUF_SIZE] = [0u8; READ_BUF_SIZE];
loop {
// Read characters from pipe into write buffer
DATAPIPE.read(&mut wbuf).await;
// Transmit/echo buffer contents over UART
embedded_io_async::Write::write(&mut tx, &wbuf)
.await
.unwrap();
// Transmit a new line
embedded_io_async::Write::write(&mut tx, &[0x0D, 0x0A])
.await
.unwrap();
// Flush transmit buffer
embedded_io_async::Write::flush(&mut tx).await.unwrap();
}
}

#[embassy_executor::task]
async fn uart_reader(mut rx: UartRx<'static, UART0>) {
// Declare read buffer to store Rx characters
let mut rbuf: [u8; READ_BUF_SIZE] = [0u8; READ_BUF_SIZE];
loop {
// Read characters from UART into read buffer until EOT
let r = embedded_io_async::Read::read(&mut rx, &mut rbuf[0..]).await;
match r {
Ok(len) => {
// If read succeeds then write recieved characters to pipe
DATAPIPE.write_all(&rbuf[..len]).await;
}
Err(e) => esp_println::println!("RX Error: {:?}", e),
}
}
}

#[main]
async fn main(spawner: Spawner) {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

// Initialize Embassy with needed timers
let timer_group0 = esp_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0);

// Initialize and configure UART0
let mut uart0 = Uart::new(peripherals.UART0, &clocks);
uart0.set_at_cmd(AtCmdConfig::new(None, None, None, AT_CMD, None));
uart0
    .set_rx_fifo_full_threshold(READ_BUF_SIZE as u16)
    .unwrap();
// Split UART0 to create seperate Tx and Rx handles
let (tx, rx) = uart0.split();

// Spawn Tx and Rx tasks
spawner.spawn(uart_reader(rx)).ok();
spawner.spawn(uart_writer(tx)).ok();

}
[cargo.toml]

[package]
name = "dron-test"
version = "0.1.0"
authors = ["sohayl chahmot [email protected]"]
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
esp-backtrace = { version = "0.11.0", features = [
"esp32",
"exception-handler",
"panic-handler",
"println",
] }
esp-hal = { version = "0.16.1", features = [ "esp32","embassy","embassy-time-timg0","async","embassy-executor-thread","embassy-integrated-timers"] }
embassy-executor = { version = "0.5.0", features = ["task-arena-size-8192","nightly"] }
embassy-net = { version = "0.4.0", features = ["tcp", "udp", "dhcpv4", "medium-ethernet"] }
esp-wifi = { version = "0.4.0", features = [ "esp32","embassy-net","wifi","wifi-default","ble"] }
esp-println = { version = "0.9.0", features = ["esp32"] }
embedded-hal-async = "1.0.0"
static_cell = { version = "1.2.0", features = ["nightly"] }
embassy-time = "0.3.0"
embedded-io-async = { version = "0.6.0" }
bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "9371d7d4d510ba5c936c1eef96674f8fd4f63e8a", features = [
"macros","async"
]}
heapless = "0.8.0"
embassy-sync = "=0.5.0"
[profile.dev]

Rust debug is too slow.

For debug builds always builds with some optimization

opt-level = "s"
[profile.dev.package.esp-wifi]
opt-level = 3

[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false

@goiw111
Copy link
Author

goiw111 commented Mar 29, 2024

i think the issue is coming from this function "embedded _io_async::Read::read"

@goiw111 goiw111 changed the title The spawned task is not running "embedded_io_async::Read::read" it's these not works Mar 29, 2024
@MabezDev
Copy link
Member

Could you ensure that your code is properly formatted, put it in a gist if that's easier? Could you also explain in bullet points, what you want to do, what you expect to happen and what is happening?

@goiw111
Copy link
Author

goiw111 commented Mar 30, 2024

Could you ensure that your code is properly formatted, put it in a gist if that's easier? Could you also explain in bullet points, what you want to do, what you expect to happen and what is happening?

This is the same code that I used. https://dev.to/apollolabsbin/embassy-on-esp-uart-echo-44fe .
I changed to this 'block!(serial1.read());'
It works now

@MabezDev
Copy link
Member

I changed to this 'block!(serial1.read());'

You won't want to block in an async application. What about the async API doesn't work? I need program output, expected output, how you are testing it etc.

@MabezDev
Copy link
Member

MabezDev commented May 2, 2024

Closing, feel free to open with more information and a minimal reproduction of the issue :).

@MabezDev MabezDev closed this as completed May 2, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in esp-rs May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants