forked from OpenPRoT/aspeed-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.rs
More file actions
109 lines (90 loc) · 2.28 KB
/
common.rs
File metadata and controls
109 lines (90 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Licensed under the Apache-2.0 license
use crate::uart::UartController;
use core::ops::{Index, IndexMut};
use embedded_io::Write;
pub struct DummyDelay;
impl embedded_hal::delay::DelayNs for DummyDelay {
fn delay_ns(&mut self, ns: u32) {
for _ in 0..(ns / 100) {
cortex_m::asm::nop();
}
}
}
#[repr(align(32))]
pub struct DmaBuffer<const N: usize> {
pub buf: [u8; N],
}
impl<const N: usize> Default for DmaBuffer<N> {
fn default() -> Self {
Self::new()
}
}
impl<const N: usize> DmaBuffer<N> {
#[must_use]
pub const fn new() -> Self {
Self { buf: [0; N] }
}
#[must_use]
pub fn as_ptr(&self) -> *const u8 {
self.buf.as_ptr()
}
#[must_use]
pub fn as_mut_ptr(&mut self) -> *mut u8 {
self.buf.as_mut_ptr()
}
#[must_use]
pub const fn len(&self) -> usize {
N
}
#[must_use]
pub const fn is_empty(&self) -> bool {
N == 0
}
#[must_use]
pub fn as_slice(&self, start: usize, end: usize) -> &[u8] {
&self.buf[start..end]
}
pub fn as_mut_slice(&mut self, start: usize, end: usize) -> &mut [u8] {
&mut self.buf[start..end]
}
}
impl<const N: usize> Index<usize> for DmaBuffer<N> {
type Output = u8;
fn index(&self, idx: usize) -> &Self::Output {
&self.buf[idx]
}
}
impl<const N: usize> IndexMut<usize> for DmaBuffer<N> {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
&mut self.buf[idx]
}
}
pub trait Logger {
fn debug(&mut self, msg: &str);
fn error(&mut self, msg: &str);
}
// No-op implementation for production builds
pub struct NoOpLogger;
impl Logger for NoOpLogger {
fn debug(&mut self, _msg: &str) {}
fn error(&mut self, _msg: &str) {}
}
// UART logger adapter (separate concern)
pub struct UartLogger<'a> {
uart: &'a mut UartController<'a>,
}
impl<'a> UartLogger<'a> {
pub fn new(uart: &'a mut UartController<'a>) -> Self {
UartLogger { uart }
}
}
impl<'a> Logger for UartLogger<'a> {
fn debug(&mut self, msg: &str) {
writeln!(self.uart, "{msg}").ok();
write!(self.uart, "\r").ok();
}
fn error(&mut self, msg: &str) {
writeln!(self.uart, "ERROR: {msg}").ok();
write!(self.uart, "\r").ok();
}
}