diff --git a/services/com/src/main.rs b/services/com/src/main.rs index 671ae2bdb..755fe9044 100644 --- a/services/com/src/main.rs +++ b/services/com/src/main.rs @@ -101,6 +101,7 @@ mod implementation { pub fn suspend(&mut self) { self.susres.suspend(); self.csr.wo(utra::com::EV_ENABLE, 0); + self.csr.wo(utra::com::EV_PENDING, 0xFFFF_FFFF); } pub fn resume(&mut self) { self.susres.resume(); diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index 7890b379d..4557ebf70 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -227,14 +227,18 @@ mod implementation { self.uartmux_cache = self.gpio_csr.rf(UARTSEL_UARTSEL).into(); self.gpio_csr.wfo(utra::gpio::UARTSEL_UARTSEL, 0); // set the kernel UART so we can catch KPs on boot - self.gpio_susres.suspend(); self.event_susres.suspend(); - self.power_susres.suspend(); - // this happens after suspend, so these disables are "lost" upon resume and replaced with the normal running values - self.gpio_csr.wo(utra::gpio::EV_ENABLE, 0); self.event_csr.wo(utra::btevents::EV_ENABLE, 0); + self.event_csr.wo(utra::btevents::EV_PENDING, 0xFFFF_FFFF); // really make sure we don't have any spurious events in the queue + + self.gpio_susres.suspend(); + self.gpio_csr.wo(utra::gpio::EV_ENABLE, 0); + self.gpio_csr.wo(utra::gpio::EV_PENDING, 0xFFFF_FFFF); + + self.power_susres.suspend(); self.power_csr.wo(utra::power::EV_ENABLE, 0); + self.power_csr.wo(utra::power::EV_PENDING, 0xFFFF_FFFF); } pub fn resume(&mut self) { self.power_susres.resume(); @@ -736,7 +740,7 @@ fn xmain() -> ! { // register a suspend/resume listener let sr_cid = xous::connect(llio_sid).expect("couldn't create suspend callback connection"); - let mut susres = susres::Susres::new(None, &xns, Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); + let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Late), &xns, Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); let mut latest_activity = 0; let mut usb_cb_conns: [Option; 32] = [None; 32]; diff --git a/services/net/src/connection_manager.rs b/services/net/src/connection_manager.rs index 2641e84f9..66c9d01ed 100644 --- a/services/net/src/connection_manager.rs +++ b/services/net/src/connection_manager.rs @@ -13,11 +13,12 @@ use locales::t; use crate::ComIntSources; #[allow(dead_code)] -const BOOT_POLL_INTERVAL_MS: usize = 3_758; // a slightly faster poll during boot so we acquire wifi faster once PDDB is mounted +const BOOT_POLL_INTERVAL_MS: usize = 4_758; // a slightly faster poll during boot so we acquire wifi faster once PDDB is mounted /// this is shared externally so other functions (e.g. in status bar) that want to query the net manager know how long to back off, otherwise the status query will block #[allow(dead_code)] const POLL_INTERVAL_MS: usize = 7_151; // stagger slightly off of an integer-seconds interval to even out loads. impacts rssi update frequency. -const INTERVALS_BEFORE_RETRY: usize = 3; // how many poll intervals we'll wait before we give up and try a new AP +const INTERVALS_BEFORE_RETRY: usize = 3; // how many poll intervals we'll wait before we give up and try a new AP +const SCAN_COUNT_MAX: usize = 5; #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum ConnectionManagerOpcode { @@ -88,6 +89,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc::new(); // we're throwing away the RSSI for now and just going by name let mut ssid_attempted = HashSet::::new(); let mut wait_count = 0; + let mut scan_count = 0; let run_sid = xous::create_server().unwrap(); let run_cid = xous::connect(run_sid).unwrap(); @@ -162,6 +164,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc { @@ -209,7 +213,6 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc { - com.set_ssid_scanning(false).unwrap(); scan_state = SsidScanState::Idle; activity_interval.store(0, Ordering::SeqCst); WifiState::WaitDhcp @@ -221,7 +224,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc WifiState::Error, ConnectResult::Pending => WifiState::Error, }; - log::debug!("comint new wifi state: {:?}", wifi_state); + log::info!("comint new wifi state: {:?}", wifi_state); } ComIntSources::Disconnect => { log::info!("{:?}", source); @@ -301,24 +304,27 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc { - if scan_state == SsidScanState::Scanning { - com.set_ssid_scanning(false).unwrap(); - scan_state = SsidScanState::Idle; - } - if let Some(ssid) = get_next_ssid(&mut ssid_list, &mut ssid_attempted, ap_list) { - let mut wpa_pw_file = pddb.get(AP_DICT_NAME, &ssid, None, false, false, None, Some(||{})).expect("couldn't retrieve AP password"); - let mut wp_pw_raw = [0u8; com::api::WF200_PASS_MAX_LEN]; - if let Ok(readlen) = wpa_pw_file.read(&mut wp_pw_raw) { - let pw = std::str::from_utf8(&wp_pw_raw[..readlen]).expect("password was not valid utf-8"); - log::info!("Attempting wifi connection: {}", ssid); - com.wlan_set_ssid(&ssid).expect("couldn't set SSID"); - com.wlan_set_pass(pw).expect("couldn't set password"); - com.wlan_join().expect("couldn't issue join command"); - wifi_state = WifiState::Connecting; + if (scan_state == SsidScanState::Idle) || scan_count > SCAN_COUNT_MAX { + scan_count = 0; + // wait until we're done scanning before trying to connect + if let Some(ssid) = get_next_ssid(&mut ssid_list, &mut ssid_attempted, ap_list) { + let mut wpa_pw_file = pddb.get(AP_DICT_NAME, &ssid, None, false, false, None, Some(||{})).expect("couldn't retrieve AP password"); + let mut wp_pw_raw = [0u8; com::api::WF200_PASS_MAX_LEN]; + if let Ok(readlen) = wpa_pw_file.read(&mut wp_pw_raw) { + let pw = std::str::from_utf8(&wp_pw_raw[..readlen]).expect("password was not valid utf-8"); + log::info!("Attempting wifi connection: {}", ssid); + com.wlan_set_ssid(&ssid).expect("couldn't set SSID"); + com.wlan_set_pass(pw).expect("couldn't set password"); + com.wlan_join().expect("couldn't issue join command"); + wifi_state = WifiState::Connecting; + } + } else { + // no SSIDs available, scan again + com.set_ssid_scanning(true).unwrap(); + scan_state = SsidScanState::Scanning; } } else { - com.set_ssid_scanning(true).unwrap(); - scan_state = SsidScanState::Scanning; + scan_count += 1; } } WifiState::WaitDhcp | WifiState::Connecting => { diff --git a/services/net/src/main.rs b/services/net/src/main.rs index 087d2a486..999a9fdb5 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -188,7 +188,7 @@ fn xmain() -> ! { log::trace!("ready to accept requests"); // register a suspend/resume listener let sr_cid = xous::connect(net_sid).expect("couldn't create suspend callback connection"); - let mut susres = susres::Susres::new(None, &xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); + let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Early), &xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); // kick off the connection manager thread let cm_sid = xous::create_server().expect("couldn't create connection manager server"); @@ -1023,8 +1023,21 @@ fn xmain() -> ! { xous::return_scalar(msg.sender, 1).unwrap(); } Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { - // handle an suspend/resume state stuff here. right now, it's a NOP + com_int_list.clear(); + com.ints_enable(&com_int_list); // disable all the interrupts + susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); + // re-enable the interrupts + com_int_list.clear(); + com_int_list.push(ComIntSources::WlanIpConfigUpdate); + com_int_list.push(ComIntSources::WlanRxReady); + com_int_list.push(ComIntSources::BatteryCritical); + com_int_list.push(ComIntSources::Connect); + com_int_list.push(ComIntSources::Disconnect); + com_int_list.push(ComIntSources::WlanSsidScanUpdate); + com_int_list.push(ComIntSources::WlanSsidScanFinished); + com_int_list.push(ComIntSources::WfxErr); + com.ints_enable(&com_int_list); }), Some(Opcode::Quit) => { log::warn!("quit received"); diff --git a/services/susres/src/main.rs b/services/susres/src/main.rs index bc814017f..0392058ed 100644 --- a/services/susres/src/main.rs +++ b/services/susres/src/main.rs @@ -84,7 +84,7 @@ mod implementation { if let Some(cf) = sr.cacheflush { let cf_ptr = cf.as_ptr() as *mut u32; for i in 0..(cf.len() / 4) { - unsafe {cf_ptr.add(i).write_volatile(0); } + unsafe {cf_ptr.add(i).write_volatile(0xacdc_acdc); } } } @@ -221,26 +221,6 @@ mod implementation { } pub fn do_suspend(&mut self, forced: bool) { - // allocate memory for the cache flush - if self.cacheflush.is_none() { - self.cacheflush = Some ( - xous::syscall::map_memory( - None, - None, - 512 * 1024, // L2 cache is 128k, but emperically it doesn't fully flush until 512k is written. Maybe has to do with write-back behavior?? - xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::RESERVE, - ).expect("couldn't allocate RAM for cache flushing") - ); - // the RESERVE flag should pre-allocate the pages, but for good measure - // we write all the pages to make sure they are at a defined value - if let Some(cf) = self.cacheflush { - let cf_ptr = cf.as_ptr() as *mut u32; - for i in 0..(cf.len() / 4) { - unsafe {cf_ptr.add(i).write_volatile(0); } - } - } - } - #[cfg(feature = "debugprint")] println!("Stopping preemption"); // stop pre-emption @@ -344,6 +324,26 @@ mod implementation { index += range; } + // allocate memory for the cache flush + if self.cacheflush.is_none() { + self.cacheflush = Some ( + xous::syscall::map_memory( + None, + None, + 512 * 1024, // L2 cache is 128k, but emperically it doesn't fully flush until 512k is written. Maybe has to do with write-back behavior?? + xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::RESERVE, + ).expect("couldn't allocate RAM for cache flushing") + ); + // the RESERVE flag should pre-allocate the pages, but for good measure + // we write all the pages to make sure they are at a defined value + if let Some(cf) = self.cacheflush { + let cf_ptr = cf.as_ptr() as *mut u32; + for i in 0..(cf.len() / 4) { + unsafe {cf_ptr.add(i).write_volatile(0x0bad_0bad); } + } + } + } + #[cfg(feature = "debugprint")] println!("Triggering suspend interrupt"); // trigger an interrupt to process the final suspend bits