partialgit status ! async with embassy

This commit is contained in:
Vladan Popovic 2023-09-25 02:12:22 +02:00
parent 64ba58227a
commit dc5f90af49
3 changed files with 164 additions and 24 deletions

View file

@ -6,6 +6,33 @@ edition = "2021"
license = "-" license = "-"
[dependencies] [dependencies]
hal = { package = "esp32-hal", version = "0.15.0" }
esp-backtrace = { version = "0.8.0", features = ["esp32", "panic-handler", "exception-handler", "print-uart"] } esp-backtrace = { version = "0.8.0", features = ["esp32", "panic-handler", "exception-handler", "print-uart"] }
esp-println = { version = "0.6.0", features = ["esp32"] } esp-println = { version = "0.6.0", features = ["esp32"] }
atat = { path = "../atat/atat", features = ["log", "async"] }
atat_derive = { path = "../atat/atat_derive" }
serde_at = { path = "../atat/serde_at" }
heapless = "0.7.16"
embassy-time = "0.1.3"
embedded-io-async = "0.5.0"
static_cell = "1.2.0"
embassy-executor = { version = "0.3.0", features = ["nightly"] }
[dependencies.hal]
package = "esp32-hal"
git = "https://github.com/esp-rs/esp-hal/"
features = [
"async",
"embassy",
"embassy-executor-thread",
"embassy-time",
"embassy-time-timg0",
]
[dependencies.esp-hal-common]
package = "esp-hal-common"
git = "https://github.com/esp-rs/esp-hal/"
features = [
"async",
"esp32",
"esp32_40mhz",
]

View file

@ -1,46 +1,90 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)]
mod modem;
use esp_backtrace as _; use esp_backtrace as _;
use esp_println::println; use esp_println::println;
use hal::{ use hal::{
clock::ClockControl, clock::ClockControl,
gpio::{IO}, embassy::{self, executor::Executor},
peripherals::Peripherals, gpio::IO,
interrupt,
peripherals::{Peripherals, Interrupt, UART1},
prelude::*, prelude::*,
prelude::nb::block, timer::TimerGroup,
Delay, Uart,
uart::{ uart::{
TxRxPins,
config::{ config::{
Config, Config,
DataBits,
Parity, Parity,
StopBits, StopBits,
DataBits,
}, },
Uart, TxRxPins,
} },
}; };
use esp_hal_common::{UartRx, UartTx};
use atat::{asynch::AtatClient, AtatIngress, Buffers, DefaultDigester, Ingress};
use heapless::Vec;
use static_cell::StaticCell;
const READ_BUF_SIZE: usize = 64;
const INGRESS_BUF_SIZE: usize = 1024;
const URC_CAPACITY: usize = 128;
const URC_SUBSCRIBERS: usize = 3;
#[embassy_executor::task]
async fn reader(mut rx: UartRx<'static, UART1>) {
esp_println::println!("reading...");
// max message size to receive
// leave some extra space for AT-CMD characters
const MAX_BUFFER_SIZE: usize = 10 * READ_BUF_SIZE + 16;
let mut rbuf: Vec<u8, MAX_BUFFER_SIZE> = Vec::new();
let mut offset = 0;
while let Ok(len) = embedded_io_async::Read::read(&mut rx, &mut rbuf[offset..]).await {
offset += len;
if offset == 0 {
rbuf.truncate(0);
break;
}
// if set_at_cmd is used than stop reading
if len < READ_BUF_SIZE {
rbuf.truncate(offset);
break;
}
}
}
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
let peripherals = Peripherals::take(); let peripherals = Peripherals::take();
let mut system = peripherals.DPORT.split(); let mut system = peripherals.DPORT.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut delay = Delay::new(&clocks);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
embassy::init(&clocks, timer_group0.timer0);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
// LilyGo A7670E serial pins. // Power on the LilyGo A7670E module.
// LilyGo TTGO T-Call sim800l board power / reset pins. let mut power = io.pins.gpio4.into_push_pull_output();
power.set_high().ok();
let modem_uart_pins = TxRxPins::new_tx_rx( let modem_uart_pins = TxRxPins::new_tx_rx(
io.pins.gpio26.into_push_pull_output(), io.pins.gpio26.into_push_pull_output(),
io.pins.gpio27.into_floating_input(), io.pins.gpio27.into_floating_input(),
); );
let mut power = io.pins.gpio4.into_push_pull_output();
power.set_high().ok();
let config = Config { let config = Config {
baudrate: 115200, baudrate: 115200,
data_bits: DataBits::DataBits8, data_bits: DataBits::DataBits8,
@ -49,20 +93,32 @@ fn main() -> ! {
}; };
// UART interface for the GSM modem // UART interface for the GSM modem
let mut serial = Uart::new_with_config( let mut uart = Uart::new_with_config(
peripherals.UART1, peripherals.UART1,
Some(config), config,
Some(modem_uart_pins), Some(modem_uart_pins),
&clocks, &clocks,
&mut system.peripheral_clock_control, &mut system.peripheral_clock_control,
); );
uart.set_rx_fifo_full_threshold(READ_BUF_SIZE as u16).unwrap();
println!("Start"); let (tx, rx) = uart.split();
loop {
serial.write(0x42).ok();
let read = block!(serial.read()).unwrap();
println!("{}", read);
delay.delay_ms(1000_u32); static BUFFERS: Buffers<modem::Urc, INGRESS_BUF_SIZE, URC_CAPACITY, URC_SUBSCRIBERS> =
} Buffers::<modem::Urc, INGRESS_BUF_SIZE, URC_CAPACITY, URC_SUBSCRIBERS>::new();
let (ingress, client) = BUFFERS.split(
tx,
DefaultDigester::<modem::Urc>::default(),
atat::Config::default()
);
interrupt::enable(Interrupt::UART1, interrupt::Priority::Priority1).unwrap();
println!("Starting embassy executor ...");
let cell = StaticCell::new();
let executor: &'static mut Executor = cell.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(reader(rx)).unwrap();
});
} }

57
src/modem/mod.rs Normal file
View file

@ -0,0 +1,57 @@
use embassy_time::{Instant, Duration};
use atat::{
atat_derive::{AtatCmd, AtatResp, AtatUrc},
asynch::Client,
ResponseChannel,
Config,
};
use embedded_io_async::Write;
use heapless::String;
#[derive(Clone, AtatResp)]
pub struct NoResponse;
#[derive(Clone, AtatCmd)]
#[at_cmd("+CSGT", NoResponse)]
pub struct SetGreetingText<'a> {
#[at_arg(position = 0, len = 32)]
pub text: &'a str,
}
#[derive(Clone, AtatCmd)]
#[at_cmd("AT", GreetingText)]
pub struct GetGreetingText;
#[derive(Clone, AtatResp)]
pub struct GreetingText {
#[at_arg(position = 0)]
pub text: String<64>,
}
#[derive(Clone, AtatCmd)]
#[at_cmd("+CGMI", ManufacturerIdText)]
pub struct GetManufacturerId;
#[derive(Clone, AtatResp)]
pub struct ManufacturerIdText {
#[at_arg(position = 0)]
pub text: String<32>,
}
#[derive(Clone, AtatResp)]
pub struct MessageWaitingIndication;
#[derive(Clone, AtatUrc)]
pub enum Urc {
#[at_urc("+UMWI")]
MessageWaitingIndication(MessageWaitingIndication),
}
type ModemClient<'a, W> = Client<'a, W, 256>;
pub fn setup_modem<W>(tx: W, res_channel: &'_ ResponseChannel<256>) -> ModemClient<'_, W>
where W: Write {
let config = Config::new();
let client: ModemClient<W> = Client::new(tx, res_channel, config);
client
}