Remove current_millis (#2304)

This commit is contained in:
Dániel Buga 2024-10-08 16:04:59 +02:00 committed by GitHub
parent 81f93698b0
commit f26eef646a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 174 additions and 154 deletions

View File

@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `Efuse::read_bit` (#2259) - Added `Efuse::read_bit` (#2259)
- Limited SPI slave support for ESP32 (Modes 1 and 3 only) (#2278) - Limited SPI slave support for ESP32 (Modes 1 and 3 only) (#2278)
- Added `Rtc::disable_rom_message_printing` (S3 and H2 only) (#2280) - Added `Rtc::disable_rom_message_printing` (S3 and H2 only) (#2280)
- Added `esp_hal::time::{Duration, Instant}` (#2304)
### Changed ### Changed

View File

@ -2,6 +2,16 @@
//! //!
//! The `time` module offers a way to get the system now. //! The `time` module offers a way to get the system now.
/// Represents a duration of time.
///
/// The resolution is 1 microsecond, represented as a 64-bit unsigned integer.
pub type Duration = fugit::Duration<u64, 1, 1_000_000>;
/// Represents an instant in time.
///
/// The resolution is 1 microsecond, represented as a 64-bit unsigned integer.
pub type Instant = fugit::Instant<u64, 1, 1_000_000>;
/// Provides time since system start in microseconds precision. /// Provides time since system start in microseconds precision.
/// ///
/// The counter wont measure time in sleep-mode. /// The counter wont measure time in sleep-mode.
@ -10,7 +20,7 @@
#[cfg_attr(esp32, doc = "36_558 years")] #[cfg_attr(esp32, doc = "36_558 years")]
#[cfg_attr(esp32s2, doc = "7_311 years")] #[cfg_attr(esp32s2, doc = "7_311 years")]
#[cfg_attr(not(any(esp32, esp32s2)), doc = "more than 7 years")] #[cfg_attr(not(any(esp32, esp32s2)), doc = "more than 7 years")]
pub fn now() -> fugit::Instant<u64, 1, 1_000_000> { pub fn now() -> Instant {
#[cfg(esp32)] #[cfg(esp32)]
let (ticks, div) = { let (ticks, div) = {
// on ESP32 use LACT // on ESP32 use LACT
@ -45,7 +55,7 @@ pub fn now() -> fugit::Instant<u64, 1, 1_000_000> {
) )
}; };
fugit::Instant::<u64, 1, 1_000_000>::from_ticks(ticks / div) Instant::from_ticks(ticks / div)
} }
#[cfg(esp32)] #[cfg(esp32)]

View File

@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed the `embedded-svc` traits and feature (#2235) - Removed the `embedded-svc` traits and feature (#2235)
- Removed the `log` feature from default features (#2253) - Removed the `log` feature from default features (#2253)
- Removed the `enumset` feature (#2297) - Removed the `enumset` feature (#2297)
- Removed `esp_wifi::current_millis` (#2304)
## 0.9.1 - 2024-09-03 ## 0.9.1 - 2024-09-03

View File

@ -93,7 +93,7 @@ pub extern "C" fn esp_wifi_allocate_from_internal_ram(size: usize) -> *mut u8 {
It's important to allocate from internal memory (i.e. not PSRAM) It's important to allocate from internal memory (i.e. not PSRAM)
### Tunable parameters are now set via esp-config ## Tunable parameters are now set via esp-config
We've replaced usage of `cfg_toml` with [esp-config](https://docs.rs/esp-config). Please remove any esp-wifi entries from `cfg.toml` and migrate the key value pairs to the `[env]` section of `.cargo/config.toml`. We've replaced usage of `cfg_toml` with [esp-config](https://docs.rs/esp-config). Please remove any esp-wifi entries from `cfg.toml` and migrate the key value pairs to the `[env]` section of `.cargo/config.toml`.
@ -104,9 +104,18 @@ We've replaced usage of `cfg_toml` with [esp-config](https://docs.rs/esp-config)
+ ESP_WIFI_RX_QUEUE_SIZE=40 + ESP_WIFI_RX_QUEUE_SIZE=40
``` ```
### `wifi-logs` feature renamed to `sys-logs` ## `wifi-logs` feature renamed to `sys-logs`
```diff ```diff
- features = ["wifi-logs"] - features = ["wifi-logs"]
+ features = ["sys-logs"] + features = ["sys-logs"]
``` ```
## Removed `esp_wifi::current_millis`
You can use `esp_hal::time::now()` instead.
```diff
- let now = esp_wifi::current_millis();
+ let now = time::now().duration_since_epoch().to_millis();
```

View File

@ -671,7 +671,7 @@ unsafe extern "C" fn ble_npl_time_ms_to_ticks(
unsafe extern "C" fn ble_npl_time_get() -> u32 { unsafe extern "C" fn ble_npl_time_get() -> u32 {
trace!("ble_npl_time_get"); trace!("ble_npl_time_get");
crate::current_millis() as u32 esp_hal::time::now().duration_since_epoch().to_millis() as u32
} }
unsafe extern "C" fn ble_npl_callout_set_arg( unsafe extern "C" fn ble_npl_callout_set_arg(

View File

@ -163,14 +163,15 @@ pub mod tasks;
pub(crate) mod memory_fence; pub(crate) mod memory_fence;
use timer::{get_systimer_count, ticks_to_millis};
#[cfg(all(feature = "wifi", any(feature = "tcp", feature = "udp")))] #[cfg(all(feature = "wifi", any(feature = "tcp", feature = "udp")))]
pub mod wifi_interface; pub mod wifi_interface;
/// Return the current systimer time in milliseconds // [esp_hal::time::now()] as a smoltcp [`Instant]`
pub fn current_millis() -> u64 { #[cfg(feature = "smoltcp")]
ticks_to_millis(get_systimer_count()) fn timestamp() -> smoltcp::time::Instant {
smoltcp::time::Instant::from_micros(
esp_hal::time::now().duration_since_epoch().to_micros() as i64
)
} }
// this is just to verify that we use the correct defaults in `build.rs` // this is just to verify that we use the correct defaults in `build.rs`

View File

@ -1497,7 +1497,7 @@ pub unsafe extern "C" fn log_writev(
/// ///
/// ************************************************************************* /// *************************************************************************
pub unsafe extern "C" fn log_timestamp() -> u32 { pub unsafe extern "C" fn log_timestamp() -> u32 {
crate::current_millis() as u32 esp_hal::time::now().duration_since_epoch().to_millis() as u32
} }
/// ************************************************************************** /// **************************************************************************

View File

@ -4,12 +4,11 @@
use smoltcp::socket::dhcpv4::Socket as Dhcpv4Socket; use smoltcp::socket::dhcpv4::Socket as Dhcpv4Socket;
use smoltcp::{ use smoltcp::{
iface::{Config, Interface, SocketSet, SocketStorage}, iface::{Config, Interface, SocketSet, SocketStorage},
time::Instant,
wire::{EthernetAddress, HardwareAddress}, wire::{EthernetAddress, HardwareAddress},
}; };
use super::{WifiApDevice, WifiController, WifiDevice, WifiDeviceMode, WifiError, WifiStaDevice}; use super::{WifiApDevice, WifiController, WifiDevice, WifiDeviceMode, WifiError, WifiStaDevice};
use crate::{current_millis, EspWifiInitialization}; use crate::{timestamp, EspWifiInitialization};
fn setup_iface<'a, MODE: WifiDeviceMode>( fn setup_iface<'a, MODE: WifiDeviceMode>(
device: &mut WifiDevice<'_, MODE>, device: &mut WifiDevice<'_, MODE>,
@ -20,11 +19,7 @@ fn setup_iface<'a, MODE: WifiDeviceMode>(
let hw_address = HardwareAddress::Ethernet(EthernetAddress::from_bytes(&mac)); let hw_address = HardwareAddress::Ethernet(EthernetAddress::from_bytes(&mac));
let config = Config::new(hw_address); let config = Config::new(hw_address);
let iface = Interface::new( let iface = Interface::new(config, device, timestamp());
config,
device,
Instant::from_millis(current_millis() as i64),
);
#[allow(unused_mut)] #[allow(unused_mut)]
let mut socket_set = SocketSet::new(storage); let mut socket_set = SocketSet::new(storage);

View File

@ -17,7 +17,7 @@ use smoltcp::{
}; };
use crate::{ use crate::{
current_millis, timestamp,
wifi::{ipv4, WifiDevice, WifiDeviceMode}, wifi::{ipv4, WifiDevice, WifiDeviceMode},
}; };
@ -511,11 +511,6 @@ impl Display for WifiStackError {
} }
} }
/// [current_millis] as an Instant
pub fn timestamp() -> Instant {
Instant::from_millis(current_millis() as i64)
}
impl<MODE: WifiDeviceMode> WifiStack<'_, MODE> { impl<MODE: WifiDeviceMode> WifiStack<'_, MODE> {
pub fn get_iface_configuration(&self) -> Result<ipv4::Configuration, WifiStackError> { pub fn get_iface_configuration(&self) -> Result<ipv4::Configuration, WifiStackError> {
Ok(self.network_config.borrow().clone()) Ok(self.network_config.borrow().clone())

View File

@ -16,10 +16,14 @@
use embedded_io::*; use embedded_io::*;
use esp_alloc as _; use esp_alloc as _;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_hal::{
prelude::*,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::{print, println}; use esp_println::{print, println};
use esp_wifi::{ use esp_wifi::{
current_millis,
init, init,
wifi::{ wifi::{
utils::create_network_interface, utils::create_network_interface,
@ -57,7 +61,8 @@ fn main() -> ! {
let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let (iface, device, mut controller, sockets) = let (iface, device, mut controller, sockets) =
create_network_interface(&init, &mut wifi, WifiApDevice, &mut socket_set_entries).unwrap(); create_network_interface(&init, &mut wifi, WifiApDevice, &mut socket_set_entries).unwrap();
let mut wifi_stack = WifiStack::new(iface, device, sockets, current_millis); let now = || time::now().duration_since_epoch().to_millis();
let mut wifi_stack = WifiStack::new(iface, device, sockets, now);
let client_config = Configuration::AccessPoint(AccessPointConfiguration { let client_config = Configuration::AccessPoint(AccessPointConfiguration {
ssid: "esp-wifi".try_into().unwrap(), ssid: "esp-wifi".try_into().unwrap(),
@ -107,26 +112,21 @@ fn main() -> ! {
println!("Connected"); println!("Connected");
let mut time_out = false; let mut time_out = false;
let wait_end = current_millis() + 20 * 1000; let deadline = time::now() + Duration::secs(20);
let mut buffer = [0u8; 1024]; let mut buffer = [0u8; 1024];
let mut pos = 0; let mut pos = 0;
loop { while let Ok(len) = socket.read(&mut buffer[pos..]) {
if let Ok(len) = socket.read(&mut buffer[pos..]) { let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
let to_print =
unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
if to_print.contains("\r\n\r\n") { if to_print.contains("\r\n\r\n") {
print!("{}", to_print); print!("{}", to_print);
println!(); println!();
break;
}
pos += len;
} else {
break; break;
} }
if current_millis() > wait_end { pos += len;
if time::now() > deadline {
println!("Timeout"); println!("Timeout");
time_out = true; time_out = true;
break; break;
@ -155,8 +155,8 @@ fn main() -> ! {
println!(); println!();
} }
let wait_end = current_millis() + 5 * 1000; let deadline = time::now() + Duration::secs(5);
while current_millis() < wait_end { while time::now() < deadline {
socket.work(); socket.work();
} }
} }

View File

@ -17,10 +17,14 @@
use embedded_io::*; use embedded_io::*;
use esp_alloc as _; use esp_alloc as _;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_hal::{
prelude::*,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::{print, println}; use esp_println::{print, println};
use esp_wifi::{ use esp_wifi::{
current_millis,
init, init,
wifi::{ wifi::{
utils::{create_ap_sta_network_interface, ApStaInterface}, utils::{create_ap_sta_network_interface, ApStaInterface},
@ -81,8 +85,9 @@ fn main() -> ! {
) )
.unwrap(); .unwrap();
let mut wifi_ap_stack = WifiStack::new(ap_interface, ap_device, ap_socket_set, current_millis); let now = || time::now().duration_since_epoch().to_millis();
let wifi_sta_stack = WifiStack::new(sta_interface, sta_device, sta_socket_set, current_millis); let mut wifi_ap_stack = WifiStack::new(ap_interface, ap_device, ap_socket_set, now);
let wifi_sta_stack = WifiStack::new(sta_interface, sta_device, sta_socket_set, now);
let client_config = Configuration::Mixed( let client_config = Configuration::Mixed(
ClientConfiguration { ClientConfiguration {
@ -156,7 +161,7 @@ fn main() -> ! {
println!("Connected"); println!("Connected");
let mut time_out = false; let mut time_out = false;
let wait_end = current_millis() + 20 * 1000; let deadline = time::now() + Duration::secs(20);
let mut buffer = [0u8; 1024]; let mut buffer = [0u8; 1024];
let mut pos = 0; let mut pos = 0;
loop { loop {
@ -175,7 +180,7 @@ fn main() -> ! {
break; break;
} }
if current_millis() > wait_end { if time::now() > deadline {
println!("Timeout"); println!("Timeout");
time_out = true; time_out = true;
break; break;
@ -195,7 +200,7 @@ fn main() -> ! {
.unwrap(); .unwrap();
sta_socket.flush().unwrap(); sta_socket.flush().unwrap();
let wait_end = current_millis() + 20 * 1000; let deadline = time::now() + Duration::secs(20);
loop { loop {
let mut buffer = [0u8; 512]; let mut buffer = [0u8; 512];
if let Ok(len) = sta_socket.read(&mut buffer) { if let Ok(len) = sta_socket.read(&mut buffer) {
@ -205,7 +210,7 @@ fn main() -> ! {
break; break;
} }
if current_millis() > wait_end { if time::now() > deadline {
println!("Timeout"); println!("Timeout");
break; break;
} }
@ -221,8 +226,8 @@ fn main() -> ! {
println!(); println!();
} }
let wait_end = current_millis() + 5 * 1000; let deadline = time::now() + Duration::secs(5);
while current_millis() < wait_end { while time::now() < deadline {
ap_socket.work(); ap_socket.work();
} }
} }

View File

@ -16,10 +16,15 @@
use embedded_io::*; use embedded_io::*;
use esp_alloc as _; use esp_alloc as _;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{delay::Delay, prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_hal::{
delay::Delay,
prelude::*,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::println; use esp_println::println;
use esp_wifi::{ use esp_wifi::{
current_millis,
init, init,
wifi::{ wifi::{
utils::create_network_interface, utils::create_network_interface,
@ -41,7 +46,7 @@ const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD"); const PASSWORD: &str = env!("PASSWORD");
const HOST_IP: &str = env!("HOST_IP"); const HOST_IP: &str = env!("HOST_IP");
const TEST_DURATION: usize = 15; const TEST_DURATION: Duration = Duration::secs(15);
const RX_BUFFER_SIZE: usize = 16384; const RX_BUFFER_SIZE: usize = 16384;
const TX_BUFFER_SIZE: usize = 16384; const TX_BUFFER_SIZE: usize = 16384;
const IO_BUFFER_SIZE: usize = 1024; const IO_BUFFER_SIZE: usize = 1024;
@ -76,7 +81,8 @@ fn main() -> ! {
let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let (iface, device, mut controller, sockets) = let (iface, device, mut controller, sockets) =
create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap();
let wifi_stack = WifiStack::new(iface, device, sockets, current_millis); let now = || time::now().duration_since_epoch().to_millis();
let wifi_stack = WifiStack::new(iface, device, sockets, now);
let client_config = Configuration::Client(ClientConfiguration { let client_config = Configuration::Client(ClientConfiguration {
ssid: SSID.try_into().unwrap(), ssid: SSID.try_into().unwrap(),
@ -103,13 +109,9 @@ fn main() -> ! {
// wait to get connected // wait to get connected
println!("Wait to get connected"); println!("Wait to get connected");
loop { loop {
let res = controller.is_connected(); match controller.is_connected() {
match res { Ok(true) => break,
Ok(connected) => { Ok(false) => {}
if connected {
break;
}
}
Err(err) => { Err(err) => {
println!("{:?}", err); println!("{:?}", err);
loop {} loop {}
@ -162,21 +164,21 @@ fn test_download<'a>(
let mut buf = [0; IO_BUFFER_SIZE]; let mut buf = [0; IO_BUFFER_SIZE];
let mut total = 0; let mut total = 0;
let wait_end = current_millis() + (TEST_DURATION as u64 * 1000); let deadline = time::now() + TEST_DURATION;
loop { loop {
socket.work(); socket.work();
if let Ok(len) = socket.read(&mut buf) { if let Ok(len) = socket.read(&mut buf) {
total += len; total += len as u64;
} else { } else {
break; break;
} }
if current_millis() > wait_end { if time::now() > deadline {
break; break;
} }
} }
let kbps = (total + 512) / 1024 / TEST_DURATION; let kbps = (total + 512) / 1024 / TEST_DURATION.to_secs();
println!("download: {} kB/s", kbps); println!("download: {} kB/s", kbps);
socket.disconnect(); socket.disconnect();
@ -196,21 +198,21 @@ fn test_upload<'a>(
let buf = [0; IO_BUFFER_SIZE]; let buf = [0; IO_BUFFER_SIZE];
let mut total = 0; let mut total = 0;
let wait_end = current_millis() + (TEST_DURATION as u64 * 1000); let deadline = time::now() + TEST_DURATION;
loop { loop {
socket.work(); socket.work();
if let Ok(len) = socket.write(&buf) { if let Ok(len) = socket.write(&buf) {
total += len; total += len as u64;
} else { } else {
break; break;
} }
if current_millis() > wait_end { if time::now() > deadline {
break; break;
} }
} }
let kbps = (total + 512) / 1024 / TEST_DURATION; let kbps = (total + 512) / 1024 / TEST_DURATION.to_secs();
println!("upload: {} kB/s", kbps); println!("upload: {} kB/s", kbps);
socket.disconnect(); socket.disconnect();
@ -231,7 +233,7 @@ fn test_upload_download<'a>(
let mut rx_buf = [0; IO_BUFFER_SIZE]; let mut rx_buf = [0; IO_BUFFER_SIZE];
let mut total = 0; let mut total = 0;
let wait_end = current_millis() + (TEST_DURATION as u64 * 1000); let deadline = time::now() + TEST_DURATION;
loop { loop {
socket.work(); socket.work();
if let Err(_) = socket.write(&tx_buf) { if let Err(_) = socket.write(&tx_buf) {
@ -241,17 +243,17 @@ fn test_upload_download<'a>(
socket.work(); socket.work();
if let Ok(len) = socket.read(&mut rx_buf) { if let Ok(len) = socket.read(&mut rx_buf) {
total += len; total += len as u64;
} else { } else {
break; break;
} }
if current_millis() > wait_end { if time::now() > deadline {
break; break;
} }
} }
let kbps = (total + 512) / 1024 / TEST_DURATION; let kbps = (total + 512) / 1024 / TEST_DURATION.to_secs();
println!("upload+download: {} kB/s", kbps); println!("upload+download: {} kB/s", kbps);
socket.disconnect(); socket.disconnect();

View File

@ -28,6 +28,7 @@ use esp_hal::{
gpio::{Input, Io, Pull}, gpio::{Input, Io, Pull},
prelude::*, prelude::*,
rng::Rng, rng::Rng,
time,
timer::timg::TimerGroup, timer::timg::TimerGroup,
}; };
use esp_println::println; use esp_println::println;
@ -68,9 +69,10 @@ fn main() -> ! {
let mut bluetooth = peripherals.BT; let mut bluetooth = peripherals.BT;
let now = || time::now().duration_since_epoch().to_millis();
loop { loop {
let connector = BleConnector::new(&init, &mut bluetooth); let connector = BleConnector::new(&init, &mut bluetooth);
let hci = HciConnector::new(connector, esp_wifi::current_millis); let hci = HciConnector::new(connector, now);
let mut ble = Ble::new(&hci); let mut ble = Ble::new(&hci);
println!("{:?}", ble.init()); println!("{:?}", ble.init());

View File

@ -27,11 +27,15 @@ use bleps::{
use embedded_io::*; use embedded_io::*;
use esp_alloc as _; use esp_alloc as _;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_hal::{
prelude::*,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::{print, println}; use esp_println::{print, println};
use esp_wifi::{ use esp_wifi::{
ble::controller::BleConnector, ble::controller::BleConnector,
current_millis,
init, init,
wifi::{utils::create_network_interface, ClientConfiguration, Configuration, WifiStaDevice}, wifi::{utils::create_network_interface, ClientConfiguration, Configuration, WifiStaDevice},
wifi_interface::WifiStack, wifi_interface::WifiStack,
@ -90,7 +94,9 @@ fn main() -> ! {
let mut socket_set_entries: [SocketStorage; 2] = Default::default(); let mut socket_set_entries: [SocketStorage; 2] = Default::default();
let (iface, device, mut controller, sockets) = let (iface, device, mut controller, sockets) =
create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap();
let wifi_stack = WifiStack::new(iface, device, sockets, current_millis);
let now = || time::now().duration_since_epoch().to_millis();
let wifi_stack = WifiStack::new(iface, device, sockets, now);
let client_config = Configuration::Client(ClientConfiguration { let client_config = Configuration::Client(ClientConfiguration {
ssid: SSID.try_into().unwrap(), ssid: SSID.try_into().unwrap(),
@ -108,13 +114,9 @@ fn main() -> ! {
// wait to get connected // wait to get connected
println!("Wait to get connected"); println!("Wait to get connected");
loop { loop {
let res = controller.is_connected(); match controller.is_connected() {
match res { Ok(true) => break,
Ok(connected) => { Ok(false) => {}
if connected {
break;
}
}
Err(err) => { Err(err) => {
println!("{:?}", err); println!("{:?}", err);
loop {} loop {}
@ -135,7 +137,7 @@ fn main() -> ! {
} }
let connector = BleConnector::new(&init, bluetooth); let connector = BleConnector::new(&init, bluetooth);
let hci = HciConnector::new(connector, esp_wifi::current_millis); let hci = HciConnector::new(connector, now);
let mut ble = Ble::new(&hci); let mut ble = Ble::new(&hci);
println!("{:?}", ble.init()); println!("{:?}", ble.init());
@ -174,17 +176,13 @@ fn main() -> ! {
.unwrap(); .unwrap();
socket.flush().unwrap(); socket.flush().unwrap();
let wait_end = current_millis() + 20 * 1000; let deadline = time::now() + Duration::secs(20);
loop { let mut buffer = [0u8; 128];
let mut buffer = [0u8; 128]; while let Ok(len) = socket.read(&mut buffer) {
if let Ok(len) = socket.read(&mut buffer) { let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) };
let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) }; print!("{}", to_print);
print!("{}", to_print);
} else {
break;
}
if current_millis() > wait_end { if time::now() > deadline {
println!("Timeout"); println!("Timeout");
break; break;
} }
@ -193,8 +191,8 @@ fn main() -> ! {
socket.disconnect(); socket.disconnect();
let wait_end = current_millis() + 5 * 1000; let deadline = time::now() + Duration::secs(5);
while current_millis() < wait_end { while time::now() < deadline {
socket.work(); socket.work();
} }
} }

View File

@ -16,10 +16,14 @@ extern crate alloc;
use embedded_io::*; use embedded_io::*;
use esp_alloc as _; use esp_alloc as _;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_hal::{
prelude::*,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::{print, println}; use esp_println::{print, println};
use esp_wifi::{ use esp_wifi::{
current_millis,
init, init,
wifi::{ wifi::{
utils::create_network_interface, utils::create_network_interface,
@ -65,7 +69,8 @@ fn main() -> ! {
let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let (iface, device, mut controller, sockets) = let (iface, device, mut controller, sockets) =
create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap();
let wifi_stack = WifiStack::new(iface, device, sockets, current_millis); let now = || time::now().duration_since_epoch().to_millis();
let wifi_stack = WifiStack::new(iface, device, sockets, now);
let client_config = Configuration::Client(ClientConfiguration { let client_config = Configuration::Client(ClientConfiguration {
ssid: SSID.try_into().unwrap(), ssid: SSID.try_into().unwrap(),
@ -92,13 +97,9 @@ fn main() -> ! {
// wait to get connected // wait to get connected
println!("Wait to get connected"); println!("Wait to get connected");
loop { loop {
let res = controller.is_connected(); match controller.is_connected() {
match res { Ok(true) => break,
Ok(connected) => { Ok(false) => {}
if connected {
break;
}
}
Err(err) => { Err(err) => {
println!("{:?}", err); println!("{:?}", err);
loop {} loop {}
@ -137,17 +138,13 @@ fn main() -> ! {
.unwrap(); .unwrap();
socket.flush().unwrap(); socket.flush().unwrap();
let wait_end = current_millis() + 20 * 1000; let deadline = time::now() + Duration::secs(20);
loop { let mut buffer = [0u8; 512];
let mut buffer = [0u8; 512]; while let Ok(len) = socket.read(&mut buffer) {
if let Ok(len) = socket.read(&mut buffer) { let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) };
let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) }; print!("{}", to_print);
print!("{}", to_print);
} else {
break;
}
if current_millis() > wait_end { if time::now() > deadline {
println!("Timeout"); println!("Timeout");
break; break;
} }
@ -156,8 +153,8 @@ fn main() -> ! {
socket.disconnect(); socket.disconnect();
let wait_end = current_millis() + 5 * 1000; let deadline = time::now() + Duration::secs(5);
while current_millis() < wait_end { while time::now() < deadline {
socket.work(); socket.work();
} }
} }

View File

@ -31,6 +31,7 @@ use esp_hal::{
gpio::{Input, Io, Pull}, gpio::{Input, Io, Pull},
prelude::*, prelude::*,
rng::Rng, rng::Rng,
time,
timer::timg::TimerGroup, timer::timg::TimerGroup,
}; };
use esp_println::println; use esp_println::println;
@ -80,7 +81,9 @@ async fn main(_spawner: Spawner) -> ! {
let mut bluetooth = peripherals.BT; let mut bluetooth = peripherals.BT;
let connector = BleConnector::new(&init, &mut bluetooth); let connector = BleConnector::new(&init, &mut bluetooth);
let mut ble = Ble::new(connector, esp_wifi::current_millis);
let now = || time::now().duration_since_epoch().to_millis();
let mut ble = Ble::new(connector, now);
println!("Connector created"); println!("Connector created");
let pin_ref = RefCell::new(button); let pin_ref = RefCell::new(button);

View File

@ -10,10 +10,14 @@
use esp_alloc as _; use esp_alloc as _;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_hal::{
prelude::*,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::println; use esp_println::println;
use esp_wifi::{ use esp_wifi::{
current_millis,
esp_now::{PeerInfo, BROADCAST_ADDRESS}, esp_now::{PeerInfo, BROADCAST_ADDRESS},
init, init,
EspWifiInitFor, EspWifiInitFor,
@ -45,7 +49,7 @@ fn main() -> ! {
println!("esp-now version {}", esp_now.get_version().unwrap()); println!("esp-now version {}", esp_now.get_version().unwrap());
let mut next_send_time = current_millis() + 5 * 1000; let mut next_send_time = time::now() + Duration::secs(5);
loop { loop {
let r = esp_now.receive(); let r = esp_now.receive();
if let Some(r) = r { if let Some(r) = r {
@ -70,8 +74,8 @@ fn main() -> ! {
} }
} }
if current_millis() >= next_send_time { if time::now() >= next_send_time {
next_send_time = current_millis() + 5 * 1000; next_send_time = time::now() + Duration::secs(5);
println!("Send"); println!("Send");
let status = esp_now let status = esp_now
.send(&BROADCAST_ADDRESS, b"0123456789") .send(&BROADCAST_ADDRESS, b"0123456789")

View File

@ -15,10 +15,14 @@
use embedded_io::*; use embedded_io::*;
use esp_alloc as _; use esp_alloc as _;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_hal::{
prelude::*,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::{print, println}; use esp_println::{print, println};
use esp_wifi::{ use esp_wifi::{
current_millis,
init, init,
wifi::{ wifi::{
utils::create_network_interface, utils::create_network_interface,
@ -63,7 +67,9 @@ fn main() -> ! {
let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let (iface, device, mut controller, sockets) = let (iface, device, mut controller, sockets) =
create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap();
let mut wifi_stack = WifiStack::new(iface, device, sockets, current_millis);
let now = || time::now().duration_since_epoch().to_millis();
let mut wifi_stack = WifiStack::new(iface, device, sockets, now);
let client_config = Configuration::Client(ClientConfiguration { let client_config = Configuration::Client(ClientConfiguration {
ssid: SSID.try_into().unwrap(), ssid: SSID.try_into().unwrap(),
@ -90,13 +96,9 @@ fn main() -> ! {
// wait to get connected // wait to get connected
println!("Wait to get connected"); println!("Wait to get connected");
loop { loop {
let res = controller.is_connected(); match controller.is_connected() {
match res { Ok(true) => break,
Ok(connected) => { Ok(false) => {}
if connected {
break;
}
}
Err(err) => { Err(err) => {
println!("{:?}", err); println!("{:?}", err);
loop {} loop {}
@ -145,26 +147,21 @@ fn main() -> ! {
println!("Connected"); println!("Connected");
let mut time_out = false; let mut time_out = false;
let wait_end = current_millis() + 20 * 1000; let deadline = time::now() + Duration::secs(20);
let mut buffer = [0u8; 1024]; let mut buffer = [0u8; 1024];
let mut pos = 0; let mut pos = 0;
loop { while let Ok(len) = socket.read(&mut buffer[pos..]) {
if let Ok(len) = socket.read(&mut buffer[pos..]) { let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
let to_print =
unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
if to_print.contains("\r\n\r\n") { if to_print.contains("\r\n\r\n") {
print!("{}", to_print); print!("{}", to_print);
println!(); println!();
break;
}
pos += len;
} else {
break; break;
} }
if current_millis() > wait_end { pos += len;
if time::now() > deadline {
println!("Timeout"); println!("Timeout");
time_out = true; time_out = true;
break; break;
@ -192,8 +189,8 @@ fn main() -> ! {
println!(); println!();
} }
let wait_end = current_millis() + 5 * 1000; let deadline = time::now() + Duration::secs(5);
while current_millis() < wait_end { while time::now() < deadline {
socket.work(); socket.work();
} }
} }