Skip to content

make Stim::write_* methods take &mut self instead of &self #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/itm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use aligned::Aligned;
use peripheral::itm::Stim;

// NOTE assumes that `bytes` is 32-bit aligned
unsafe fn write_words(stim: &Stim, bytes: &[u32]) {
unsafe fn write_words(stim: &mut Stim, bytes: &[u32]) {
let mut p = bytes.as_ptr();
for _ in 0..bytes.len() {
while !stim.is_fifo_ready() {}
Expand All @@ -16,7 +16,7 @@ unsafe fn write_words(stim: &Stim, bytes: &[u32]) {
}
}

struct Port<'p>(&'p Stim);
struct Port<'p>(&'p mut Stim);

impl<'p> fmt::Write for Port<'p> {
fn write_str(&mut self, s: &str) -> fmt::Result {
Expand All @@ -26,7 +26,7 @@ impl<'p> fmt::Write for Port<'p> {
}

/// Writes a `buffer` to the ITM `port`
pub fn write_all(port: &Stim, buffer: &[u8]) {
pub fn write_all(port: &mut Stim, buffer: &[u8]) {
unsafe {
let mut len = buffer.len();
let mut ptr = buffer.as_ptr();
Expand Down Expand Up @@ -84,7 +84,7 @@ pub fn write_all(port: &Stim, buffer: &[u8]) {
/// // Or equivalently
/// itm::write_aligned(&itm.stim[0], &Aligned(*b"Hello, world!\n"));
/// ```
pub fn write_aligned(port: &Stim, buffer: &Aligned<u32, [u8]>) {
pub fn write_aligned(port: &mut Stim, buffer: &Aligned<u32, [u8]>) {
unsafe {
let len = buffer.len();

Expand Down Expand Up @@ -120,13 +120,13 @@ pub fn write_aligned(port: &Stim, buffer: &Aligned<u32, [u8]>) {
}

/// Writes `fmt::Arguments` to the ITM `port`
pub fn write_fmt(port: &Stim, args: fmt::Arguments) {
pub fn write_fmt(port: &mut Stim, args: fmt::Arguments) {
use core::fmt::Write;

Port(port).write_fmt(args).ok();
}

/// Writes a string to the ITM `port`
pub fn write_str(port: &Stim, string: &str) {
pub fn write_str(port: &mut Stim, string: &str) {
write_all(port, string.as_bytes())
}
6 changes: 3 additions & 3 deletions src/peripheral/itm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ pub struct Stim {

impl Stim {
/// Writes an `u8` payload into the stimulus port
pub fn write_u8(&self, value: u8) {
pub fn write_u8(&mut self, value: u8) {
unsafe { ptr::write_volatile(self.register.get() as *mut u8, value) }
}

/// Writes an `u16` payload into the stimulus port
pub fn write_u16(&self, value: u16) {
pub fn write_u16(&mut self, value: u16) {
unsafe { ptr::write_volatile(self.register.get() as *mut u16, value) }
}

/// Writes an `u32` payload into the stimulus port
pub fn write_u32(&self, value: u32) {
pub fn write_u32(&mut self, value: u32) {
unsafe { ptr::write_volatile(self.register.get(), value) }
}

Expand Down
12 changes: 9 additions & 3 deletions src/peripheral/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![allow(private_no_mangle_statics)]

use core::marker::PhantomData;
use core::ops::Deref;
use core::ops::{Deref, DerefMut};

use interrupt;

Expand Down Expand Up @@ -262,8 +262,8 @@ pub struct ITM {

impl ITM {
/// Returns a pointer to the register block
pub fn ptr() -> *const itm::RegisterBlock {
0xE000_0000 as *const _
pub fn ptr() -> *mut itm::RegisterBlock {
0xE000_0000 as *mut _
}
}

Expand All @@ -275,6 +275,12 @@ impl Deref for ITM {
}
}

impl DerefMut for ITM {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *Self::ptr() }
}
}

/// Memory Protection Unit
pub struct MPU {
_marker: PhantomData<*const ()>,
Expand Down