-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration option to prohibit power off and power on when charger is attached.
- Loading branch information
Showing
16 changed files
with
500 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -30,3 +30,5 @@ SECTIONS | |
} > RAM | ||
} | ||
INSERT BEFORE .data; | ||
|
||
__flash_end = ORIGIN(FLASH) + LENGTH(FLASH); |
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 |
---|---|---|
|
@@ -30,3 +30,5 @@ SECTIONS | |
} > RAM | ||
} | ||
INSERT BEFORE .data; | ||
|
||
__flash_end = ORIGIN(FLASH) + LENGTH(FLASH); |
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 |
---|---|---|
|
@@ -28,3 +28,5 @@ SECTIONS | |
} > RAM | ||
} | ||
INSERT BEFORE .data; | ||
|
||
__flash_end = ORIGIN(FLASH) + LENGTH(FLASH); |
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! Configuration. | ||
|
||
use core::{mem::MaybeUninit, ptr::addr_of_mut}; | ||
use defmt::Format; | ||
|
||
use crate::flash_data::Validate; | ||
|
||
/// Behavior when charger is attached while device is powered off. | ||
#[derive(Default, Format, Clone, Copy, PartialEq, Eq)] | ||
#[repr(u8)] | ||
pub enum ChargerAttached { | ||
/// Go into charge mode. | ||
#[default] | ||
ChargeMode = 0, | ||
/// Power on device. | ||
PowerOn = 1, | ||
} | ||
|
||
impl TryFrom<u8> for ChargerAttached { | ||
type Error = (); | ||
|
||
fn try_from(value: u8) -> Result<Self, Self::Error> { | ||
match value { | ||
0 => Ok(Self::ChargeMode), | ||
1 => Ok(Self::PowerOn), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
/// Configuration stored in flash. | ||
#[derive(Format, Clone, Copy)] | ||
#[repr(C)] | ||
pub struct Cfg { | ||
/// Behavior when charger is attached while device is powered off. | ||
pub charger_attached: ChargerAttached, | ||
/// Whether power off is prohibited. | ||
/// | ||
/// If true, power off requests are converted into resets. | ||
pub prohibit_power_off: bool, | ||
} | ||
|
||
impl Cfg { | ||
/// Log configuration change. | ||
pub fn log(&self) { | ||
defmt::info!("Configuration changed: {:?}", self); | ||
} | ||
} | ||
|
||
impl Validate for Cfg { | ||
fn validate(mut this: MaybeUninit<Self>) -> Self { | ||
unsafe { | ||
let ptr = this.as_mut_ptr(); | ||
|
||
let charger_attached_ptr = addr_of_mut!((*ptr).charger_attached) as *mut u8; | ||
if charger_attached_ptr.read() > 1 { | ||
charger_attached_ptr.write(0); | ||
} | ||
|
||
let prohibit_power_off_ptr = addr_of_mut!((*ptr).prohibit_power_off) as *mut u8; | ||
if prohibit_power_off_ptr.read() > 1 { | ||
prohibit_power_off_ptr.write(0); | ||
} | ||
|
||
this.assume_init() | ||
} | ||
} | ||
} |
Oops, something went wrong.