use crate::lib_bitfield;
use crate::result::{Error, Result};
use super::{ExtCsd, ExtCsdIndex};
lib_bitfield! {
pub BackgroundOperationsEnable(u8): u8 {
pub auto: 1;
pub manual: 0;
}
}
impl BackgroundOperationsEnable {
pub const MASK: u8 = 0x3;
pub const DEFAULT: u8 = 0;
pub const fn new() -> Self {
Self(Self::DEFAULT)
}
pub const fn from_inner(val: u8) -> Self {
Self(val & Self::MASK)
}
pub const fn try_from_inner(val: u8) -> Result<Self> {
Ok(Self::from_inner(val))
}
pub const fn into_inner(self) -> u8 {
self.0
}
}
impl Default for BackgroundOperationsEnable {
fn default() -> Self {
Self::new()
}
}
impl From<u8> for BackgroundOperationsEnable {
fn from(val: u8) -> Self {
Self::from_inner(val)
}
}
impl From<BackgroundOperationsEnable> for u8 {
fn from(val: BackgroundOperationsEnable) -> Self {
val.into_inner()
}
}
impl ExtCsd {
pub const fn background_operations_enable(&self) -> BackgroundOperationsEnable {
BackgroundOperationsEnable::from_inner(self.0[ExtCsdIndex::BkopsEn.into_inner()])
}
pub fn set_background_operations_enable(&mut self, val: BackgroundOperationsEnable) {
self.0[ExtCsdIndex::BkopsEn.into_inner()] = val.into_inner();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_background_operations_enable() {
let mut ext_csd = ExtCsd::new();
assert_eq!(
ext_csd.background_operations_enable(),
BackgroundOperationsEnable::new()
);
(0..=u8::MAX)
.map(BackgroundOperationsEnable::from_inner)
.for_each(|ops| {
ext_csd.set_background_operations_enable(ops);
assert_eq!(ext_csd.background_operations_enable(), ops);
});
}
}