aboutsummaryrefslogtreecommitdiff
path: root/src/itm.rs
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2018-01-11 15:26:18 +0100
committerGravatar Jorge Aparicio <jorge@japaric.io> 2018-01-11 15:26:18 +0100
commitee21e7d66a2975aa358f9a69185defa9fa5e4e85 (patch)
tree166503af50042e80b3f044caac4be587689dc5da /src/itm.rs
parentbdc7ca96c5593e410c8f49025d2b0fced7607a4d (diff)
downloadcortex-m-ee21e7d66a2975aa358f9a69185defa9fa5e4e85.tar.gz
cortex-m-ee21e7d66a2975aa358f9a69185defa9fa5e4e85.tar.zst
cortex-m-ee21e7d66a2975aa358f9a69185defa9fa5e4e85.zip
make `Stim::write_*` methods take `&mut self` instead of `&self`
this prevents people from overlapping non-atomic write operations on the same stimulus port when working with generators (cooperative tasks). For example, with this change the following code won't compile ``` rust let stim = &mut ITM.stim[0]; let a = || { loop { // .. for byte in b"Hello, world!".iter() { while !stim.is_fifo_ready() { yield } stim.write_u8(*byte); } // .. } }; let b = || { loop { // .. for byte in b"The quick brown fox jumps over the lazy dog".iter() { while !stim.is_fifo_ready() { yield } stim.write_u8(*byte); } // .. } }; ``` A possible fix for the above code is to use different stimulus ports in each task (generator).
Diffstat (limited to 'src/itm.rs')
-rw-r--r--src/itm.rs12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/itm.rs b/src/itm.rs
index 5a2722d..02ada53 100644
--- a/src/itm.rs
+++ b/src/itm.rs
@@ -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() {}
@@ -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 {
@@ -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();
@@ -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();
@@ -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())
}