| 1 | //! Instrumentation Trace Macrocell |
| 2 | //! |
| 3 | //! **NOTE** This module is only available on ARMv7-M and newer. |
| 4 | |
| 5 | use core::{fmt, ptr, slice}; |
| 6 | |
| 7 | use crate::peripheral::itm::Stim; |
| 8 | |
| 9 | // NOTE assumes that `bytes` is 32-bit aligned |
| 10 | unsafe fn write_words(stim: &mut Stim, bytes: &[u32]) { |
| 11 | let mut p: *const u32 = bytes.as_ptr(); |
| 12 | for _ in 0..bytes.len() { |
| 13 | while !stim.is_fifo_ready() {} |
| 14 | stim.write_u32(ptr::read(src:p)); |
| 15 | p = p.offset(count:1); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /// Writes an aligned byte slice to the ITM. |
| 20 | /// |
| 21 | /// `buffer` must be 4-byte aligned. |
| 22 | unsafe fn write_aligned_impl(port: &mut Stim, buffer: &[u8]) { |
| 23 | let len = buffer.len(); |
| 24 | |
| 25 | if len == 0 { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | let split = len & !0b11; |
| 30 | #[allow (clippy::cast_ptr_alignment)] |
| 31 | write_words( |
| 32 | port, |
| 33 | slice::from_raw_parts(buffer.as_ptr() as *const u32, split >> 2), |
| 34 | ); |
| 35 | |
| 36 | // 3 bytes or less left |
| 37 | let mut left = len & 0b11; |
| 38 | let mut ptr = buffer.as_ptr().add(split); |
| 39 | |
| 40 | // at least 2 bytes left |
| 41 | if left > 1 { |
| 42 | while !port.is_fifo_ready() {} |
| 43 | |
| 44 | #[allow (clippy::cast_ptr_alignment)] |
| 45 | port.write_u16(ptr::read(ptr as *const u16)); |
| 46 | |
| 47 | ptr = ptr.offset(2); |
| 48 | left -= 2; |
| 49 | } |
| 50 | |
| 51 | // final byte |
| 52 | if left == 1 { |
| 53 | while !port.is_fifo_ready() {} |
| 54 | port.write_u8(*ptr); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | struct Port<'p>(&'p mut Stim); |
| 59 | |
| 60 | impl<'p> fmt::Write for Port<'p> { |
| 61 | #[inline ] |
| 62 | fn write_str(&mut self, s: &str) -> fmt::Result { |
| 63 | write_all(self.0, buffer:s.as_bytes()); |
| 64 | Ok(()) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// A wrapper type that aligns its contents on a 4-Byte boundary. |
| 69 | /// |
| 70 | /// ITM transfers are most efficient when the data is 4-Byte-aligned. This type provides an easy |
| 71 | /// way to accomplish and enforce such an alignment. |
| 72 | #[repr (align(4))] |
| 73 | pub struct Aligned<T: ?Sized>(pub T); |
| 74 | |
| 75 | /// Writes `buffer` to an ITM port. |
| 76 | #[allow (clippy::missing_inline_in_public_items)] |
| 77 | pub fn write_all(port: &mut Stim, buffer: &[u8]) { |
| 78 | unsafe { |
| 79 | let mut len = buffer.len(); |
| 80 | let mut ptr = buffer.as_ptr(); |
| 81 | |
| 82 | if len == 0 { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // 0x01 OR 0x03 |
| 87 | if ptr as usize % 2 == 1 { |
| 88 | while !port.is_fifo_ready() {} |
| 89 | port.write_u8(*ptr); |
| 90 | |
| 91 | // 0x02 OR 0x04 |
| 92 | ptr = ptr.offset(1); |
| 93 | len -= 1; |
| 94 | } |
| 95 | |
| 96 | // 0x02 |
| 97 | if ptr as usize % 4 == 2 { |
| 98 | if len > 1 { |
| 99 | // at least 2 bytes |
| 100 | while !port.is_fifo_ready() {} |
| 101 | |
| 102 | // We checked the alignment above, so this is safe |
| 103 | #[allow (clippy::cast_ptr_alignment)] |
| 104 | port.write_u16(ptr::read(ptr as *const u16)); |
| 105 | |
| 106 | // 0x04 |
| 107 | ptr = ptr.offset(2); |
| 108 | len -= 2; |
| 109 | } else { |
| 110 | if len == 1 { |
| 111 | // last byte |
| 112 | while !port.is_fifo_ready() {} |
| 113 | port.write_u8(*ptr); |
| 114 | } |
| 115 | |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // The remaining data is 4-byte aligned, but might not be a multiple of 4 bytes |
| 121 | write_aligned_impl(port, slice::from_raw_parts(ptr, len)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// Writes a 4-byte aligned `buffer` to an ITM port. |
| 126 | /// |
| 127 | /// # Examples |
| 128 | /// |
| 129 | /// ```no_run |
| 130 | /// # use cortex_m::{itm::{self, Aligned}, peripheral::ITM}; |
| 131 | /// # let port = unsafe { &mut (*ITM::PTR).stim[0] }; |
| 132 | /// let mut buffer = Aligned([0; 14]); |
| 133 | /// |
| 134 | /// buffer.0.copy_from_slice(b"Hello, world! \n" ); |
| 135 | /// |
| 136 | /// itm::write_aligned(port, &buffer); |
| 137 | /// |
| 138 | /// // Or equivalently |
| 139 | /// itm::write_aligned(port, &Aligned(*b"Hello, world! \n" )); |
| 140 | /// ``` |
| 141 | #[allow (clippy::missing_inline_in_public_items)] |
| 142 | pub fn write_aligned(port: &mut Stim, buffer: &Aligned<[u8]>) { |
| 143 | unsafe { write_aligned_impl(port, &buffer.0) } |
| 144 | } |
| 145 | |
| 146 | /// Writes `fmt::Arguments` to the ITM `port` |
| 147 | #[inline ] |
| 148 | pub fn write_fmt(port: &mut Stim, args: fmt::Arguments) { |
| 149 | use core::fmt::Write; |
| 150 | |
| 151 | Port(port).write_fmt(args).ok(); |
| 152 | } |
| 153 | |
| 154 | /// Writes a string to the ITM `port` |
| 155 | #[inline ] |
| 156 | pub fn write_str(port: &mut Stim, string: &str) { |
| 157 | write_all(port, buffer:string.as_bytes()) |
| 158 | } |
| 159 | |