Skip to content
Snippets Groups Projects
Commit 1499d8cc authored by Per Lindgren's avatar Per Lindgren
Browse files

debug

parent 1cf5d92a
Branches
No related tags found
No related merge requests found
Pipeline #88 canceled
[package] [package]
authors = [ authors = [
"Jorge Aparicio <japaricious@gmail.com>", "Jorge Aparicio <japaricious@gmail.com>",
"Jonathan 'theJPster' Pallant <github@thejpster.org.uk>" "Jonathan 'theJPster' Pallant <github@thejpster.org.uk>",
"Per Lindgren <per.lindgren@ltu.se>"
] ]
description = "Volatile access to memory mapped hardware registers" description = "Volatile access to memory mapped hardware registers"
documentation = "https://docs.rs/volatile-register" documentation = "https://docs.rs/volatile-register"
keywords = ["no-std", "volatile", "register"] keywords = ["no-std", "volatile", "register"]
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
name = "volatile-register" name = "volatile-register"
repository = "https://github.com/japaric/volatile-register" repository = "https://github.com/japaric/volatile-register"
version = "0.2.0" version = "0.3.0"
[dependencies] [dependencies]
vcell = "0.1.0" vcell = "0.1.0"
[features]
debug-fmt = []
#default = ["debug-fmt"]
\ No newline at end of file
...@@ -27,20 +27,47 @@ ...@@ -27,20 +27,47 @@
#![deny(missing_docs)] #![deny(missing_docs)]
#![no_std] #![no_std]
#![feature(specialization)]
extern crate vcell; extern crate vcell;
#[cfg(feature = "debug-fmt")]
use core::fmt;
#[cfg(feature = "debug-fmt")]
use core::fmt::{Debug, UpperHex};
use vcell::VolatileCell; use vcell::VolatileCell;
/// Read-Only register /// Read-Only register
pub struct RO<T> pub struct RO<T>
where T: Copy where
T: Copy,
{ {
register: VolatileCell<T>, register: VolatileCell<T>,
} }
#[cfg(feature = "debug-fmt")]
impl<T> fmt::Debug for RO<T>
where
T: Copy + Debug,
{
default fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.read())
}
}
#[cfg(feature = "debug-fmt")]
impl<T> fmt::Debug for RO<T>
where
T: Copy + Debug + UpperHex,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#X}", self.read())
}
}
impl<T> RO<T> impl<T> RO<T>
where T: Copy where
T: Copy,
{ {
/// Reads the value of the register /// Reads the value of the register
#[inline(always)] #[inline(always)]
...@@ -49,22 +76,45 @@ impl<T> RO<T> ...@@ -49,22 +76,45 @@ impl<T> RO<T>
} }
} }
#[cfg(feature = "debug-fmt")]
impl<T> fmt::Debug for RW<T>
where
T: Copy + Debug,
{
default fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.read())
}
}
#[cfg(feature = "debug-fmt")]
impl<T> fmt::Debug for RW<T>
where
T: Copy + Debug + UpperHex,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#X}", self.read())
}
}
/// Read-Write register /// Read-Write register
pub struct RW<T> pub struct RW<T>
where T: Copy where
T: Copy,
{ {
register: VolatileCell<T>, register: VolatileCell<T>,
} }
impl<T> RW<T> impl<T> RW<T>
where T: Copy where
T: Copy,
{ {
/// Performs a read-modify-write operation /// Performs a read-modify-write operation
/// ///
/// NOTE: `unsafe` because writes to a register are side effectful /// NOTE: `unsafe` because writes to a register are side effectful
#[inline(always)] #[inline(always)]
pub unsafe fn modify<F>(&self, f: F) pub unsafe fn modify<F>(&self, f: F)
where F: FnOnce(T) -> T where
F: FnOnce(T) -> T,
{ {
self.register.set(f(self.register.get())); self.register.set(f(self.register.get()));
} }
...@@ -86,13 +136,25 @@ impl<T> RW<T> ...@@ -86,13 +136,25 @@ impl<T> RW<T>
/// Write-Only register /// Write-Only register
pub struct WO<T> pub struct WO<T>
where T: Copy where
T: Copy,
{ {
register: VolatileCell<T>, register: VolatileCell<T>,
} }
#[cfg(feature = "debug-fmt")]
impl<T> fmt::Debug for WO<T>
where
T: Copy + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "- write only -") // TODO: perhaps self.register.get()
}
}
impl<T> WO<T> impl<T> WO<T>
where T: Copy where
T: Copy,
{ {
/// Writes `value` into the register /// Writes `value` into the register
/// ///
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment