generated from 9names/embassy-rp-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
19-introduce-power-information (#31)
- Loading branch information
1 parent
2c5eaaf
commit e2be835
Showing
10 changed files
with
223 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
//! Tasks that make up the application as well as the resources they use. | ||
pub mod buttons; | ||
pub mod dfplayer; | ||
pub mod display; | ||
pub mod neopixel; | ||
pub mod power; | ||
pub mod resources; | ||
pub mod state; | ||
pub mod time_updater; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! # Power | ||
//! Determine the power state of the system: battery or power supply. | ||
//! Detremine the supply voltage of the system. | ||
use crate::task::resources::{Irqs, UsbPowerResources}; | ||
use crate::task::state::VBUS_CHANNEL; | ||
use crate::VsysResources; | ||
use defmt::*; | ||
use embassy_executor::Spawner; | ||
use embassy_rp::adc::{Adc, Channel, Config}; | ||
use embassy_rp::gpio::{Input, Pull}; | ||
use embassy_time::{Duration, Timer}; | ||
|
||
/// determine the power source of the system, specifically if the USB power supply is connected | ||
/// the USB power supply is connected, if the pin is high | ||
/// Note: We are using a voltage divider to detect the USB power supply through a GPIO pin. Due to the intricacies of the Pico W, | ||
/// the VBUS pin is not available for direct use (it is run through the wifi module, and there is no safe way to use wifi and the | ||
/// vbus concurrently). | ||
#[embassy_executor::task] | ||
pub async fn usb_power(_spawner: Spawner, r: UsbPowerResources) { | ||
info!("usb_power task started"); | ||
let mut vbus_in = Input::new(r.vbus_pin, Pull::None); | ||
let sender = VBUS_CHANNEL.sender(); | ||
loop { | ||
info!("usb_power task loop"); | ||
sender.send(vbus_in.is_high().into()).await; | ||
vbus_in.wait_for_any_edge().await; | ||
info!("usb_power edge detected"); | ||
} | ||
} | ||
|
||
/// measure the voltage of the Vsys rail | ||
/// this is either the battery voltage or the usb power supply voltage, if the usb power supply is connected. | ||
/// Note: We are using a voltage divider to measure the Vsys voltage through a GPIO pin. Due to the intricacies of the Pico W, | ||
/// the VSYS pin is not available for direct use (it is run through the wifi module, and there is no safe way to use wifi and the | ||
/// vsys concurrently). | ||
#[embassy_executor::task] | ||
pub async fn vsys_voltage(_spawner: Spawner, r: VsysResources) { | ||
info!("vsys_voltage task started"); | ||
let mut adc = Adc::new(r.adc, Irqs, Config::default()); | ||
let vsys_in = r.pin_27; | ||
let mut channel = Channel::new_pin(vsys_in, Pull::None); | ||
let refresh_after_secs = 600; // 10 minutes | ||
loop { | ||
// read the adc value | ||
let adc_value = adc.read(&mut channel).await.unwrap(); | ||
let voltage = (adc_value as f32) * 3.3 * 3.0 / 4096.0; | ||
|
||
info!( | ||
"vsys_voltage: adc_value: {}, voltage: {}", | ||
adc_value, voltage | ||
); | ||
Timer::after(Duration::from_secs(refresh_after_secs)).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.