aboutsummaryrefslogtreecommitdiff
path: root/embassy-hal-internal
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-hal-internal')
-rw-r--r--embassy-hal-internal/src/atomic_ring_buffer.rs14
-rw-r--r--embassy-hal-internal/src/drop.rs7
-rw-r--r--embassy-hal-internal/src/lib.rs1
-rw-r--r--embassy-hal-internal/src/macros.rs5
-rw-r--r--embassy-hal-internal/src/peripheral.rs1
-rw-r--r--embassy-hal-internal/src/ratio.rs1
6 files changed, 27 insertions, 2 deletions
diff --git a/embassy-hal-internal/src/atomic_ring_buffer.rs b/embassy-hal-internal/src/atomic_ring_buffer.rs
index ea84925c4..b4f2cec28 100644
--- a/embassy-hal-internal/src/atomic_ring_buffer.rs
+++ b/embassy-hal-internal/src/atomic_ring_buffer.rs
@@ -1,3 +1,4 @@
1//! Atomic reusable ringbuffer.
1use core::slice; 2use core::slice;
2use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; 3use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
3 4
@@ -14,8 +15,9 @@ use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
14/// One concurrent writer and one concurrent reader are supported, even at 15/// One concurrent writer and one concurrent reader are supported, even at
15/// different execution priorities (like main and irq). 16/// different execution priorities (like main and irq).
16pub struct RingBuffer { 17pub struct RingBuffer {
18 #[doc(hidden)]
17 pub buf: AtomicPtr<u8>, 19 pub buf: AtomicPtr<u8>,
18 pub len: AtomicUsize, 20 len: AtomicUsize,
19 21
20 // start and end wrap at len*2, not at len. 22 // start and end wrap at len*2, not at len.
21 // This allows distinguishing "full" and "empty". 23 // This allows distinguishing "full" and "empty".
@@ -24,11 +26,16 @@ pub struct RingBuffer {
24 // 26 //
25 // This avoids having to consider the ringbuffer "full" at len-1 instead of len. 27 // This avoids having to consider the ringbuffer "full" at len-1 instead of len.
26 // The usual solution is adding a "full" flag, but that can't be made atomic 28 // The usual solution is adding a "full" flag, but that can't be made atomic
29 #[doc(hidden)]
27 pub start: AtomicUsize, 30 pub start: AtomicUsize,
31 #[doc(hidden)]
28 pub end: AtomicUsize, 32 pub end: AtomicUsize,
29} 33}
30 34
35/// A type which can only read from a ring buffer.
31pub struct Reader<'a>(&'a RingBuffer); 36pub struct Reader<'a>(&'a RingBuffer);
37
38/// A type which can only write to a ring buffer.
32pub struct Writer<'a>(&'a RingBuffer); 39pub struct Writer<'a>(&'a RingBuffer);
33 40
34impl RingBuffer { 41impl RingBuffer {
@@ -89,10 +96,12 @@ impl RingBuffer {
89 Writer(self) 96 Writer(self)
90 } 97 }
91 98
99 /// Return length of buffer.
92 pub fn len(&self) -> usize { 100 pub fn len(&self) -> usize {
93 self.len.load(Ordering::Relaxed) 101 self.len.load(Ordering::Relaxed)
94 } 102 }
95 103
104 /// Check if buffer is full.
96 pub fn is_full(&self) -> bool { 105 pub fn is_full(&self) -> bool {
97 let len = self.len.load(Ordering::Relaxed); 106 let len = self.len.load(Ordering::Relaxed);
98 let start = self.start.load(Ordering::Relaxed); 107 let start = self.start.load(Ordering::Relaxed);
@@ -101,6 +110,7 @@ impl RingBuffer {
101 self.wrap(start + len) == end 110 self.wrap(start + len) == end
102 } 111 }
103 112
113 /// Check if buffer is empty.
104 pub fn is_empty(&self) -> bool { 114 pub fn is_empty(&self) -> bool {
105 let start = self.start.load(Ordering::Relaxed); 115 let start = self.start.load(Ordering::Relaxed);
106 let end = self.end.load(Ordering::Relaxed); 116 let end = self.end.load(Ordering::Relaxed);
@@ -238,6 +248,7 @@ impl<'a> Writer<'a> {
238 [(unsafe { buf.add(end) }, n0), (buf, n1)] 248 [(unsafe { buf.add(end) }, n0), (buf, n1)]
239 } 249 }
240 250
251 /// Mark n bytes as written and advance the write index.
241 pub fn push_done(&mut self, n: usize) { 252 pub fn push_done(&mut self, n: usize) {
242 trace!(" ringbuf: push {:?}", n); 253 trace!(" ringbuf: push {:?}", n);
243 let end = self.0.end.load(Ordering::Relaxed); 254 let end = self.0.end.load(Ordering::Relaxed);
@@ -323,6 +334,7 @@ impl<'a> Reader<'a> {
323 (unsafe { buf.add(start) }, n) 334 (unsafe { buf.add(start) }, n)
324 } 335 }
325 336
337 /// Mark n bytes as read and allow advance the read index.
326 pub fn pop_done(&mut self, n: usize) { 338 pub fn pop_done(&mut self, n: usize) {
327 trace!(" ringbuf: pop {:?}", n); 339 trace!(" ringbuf: pop {:?}", n);
328 340
diff --git a/embassy-hal-internal/src/drop.rs b/embassy-hal-internal/src/drop.rs
index 7cd16aaec..8383fcc16 100644
--- a/embassy-hal-internal/src/drop.rs
+++ b/embassy-hal-internal/src/drop.rs
@@ -1,16 +1,20 @@
1//! Types for controlling when drop is invoked.
1use core::mem; 2use core::mem;
2use core::mem::MaybeUninit; 3use core::mem::MaybeUninit;
3 4
4#[must_use = "to delay the drop handler invokation to the end of the scope"] 5/// A type to delay the drop handler invocation.
6#[must_use = "to delay the drop handler invocation to the end of the scope"]
5pub struct OnDrop<F: FnOnce()> { 7pub struct OnDrop<F: FnOnce()> {
6 f: MaybeUninit<F>, 8 f: MaybeUninit<F>,
7} 9}
8 10
9impl<F: FnOnce()> OnDrop<F> { 11impl<F: FnOnce()> OnDrop<F> {
12 /// Create a new instance.
10 pub fn new(f: F) -> Self { 13 pub fn new(f: F) -> Self {
11 Self { f: MaybeUninit::new(f) } 14 Self { f: MaybeUninit::new(f) }
12 } 15 }
13 16
17 /// Prevent drop handler from running.
14 pub fn defuse(self) { 18 pub fn defuse(self) {
15 mem::forget(self) 19 mem::forget(self)
16 } 20 }
@@ -34,6 +38,7 @@ pub struct DropBomb {
34} 38}
35 39
36impl DropBomb { 40impl DropBomb {
41 /// Create a new instance.
37 pub fn new() -> Self { 42 pub fn new() -> Self {
38 Self { _private: () } 43 Self { _private: () }
39 } 44 }
diff --git a/embassy-hal-internal/src/lib.rs b/embassy-hal-internal/src/lib.rs
index f3d59e588..89f20e993 100644
--- a/embassy-hal-internal/src/lib.rs
+++ b/embassy-hal-internal/src/lib.rs
@@ -1,6 +1,7 @@
1#![no_std] 1#![no_std]
2#![allow(clippy::new_without_default)] 2#![allow(clippy::new_without_default)]
3#![doc = include_str!("../README.md")] 3#![doc = include_str!("../README.md")]
4#![warn(missing_docs)]
4 5
5// This mod MUST go first, so that the others see its macros. 6// This mod MUST go first, so that the others see its macros.
6pub(crate) mod fmt; 7pub(crate) mod fmt;
diff --git a/embassy-hal-internal/src/macros.rs b/embassy-hal-internal/src/macros.rs
index 97df38954..07cd89487 100644
--- a/embassy-hal-internal/src/macros.rs
+++ b/embassy-hal-internal/src/macros.rs
@@ -1,3 +1,4 @@
1/// Types for the peripheral singletons.
1#[macro_export] 2#[macro_export]
2macro_rules! peripherals_definition { 3macro_rules! peripherals_definition {
3 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { 4 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
@@ -29,6 +30,7 @@ macro_rules! peripherals_definition {
29 }; 30 };
30} 31}
31 32
33/// Define the peripherals struct.
32#[macro_export] 34#[macro_export]
33macro_rules! peripherals_struct { 35macro_rules! peripherals_struct {
34 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { 36 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
@@ -87,6 +89,7 @@ macro_rules! peripherals_struct {
87 }; 89 };
88} 90}
89 91
92/// Defining peripheral type.
90#[macro_export] 93#[macro_export]
91macro_rules! peripherals { 94macro_rules! peripherals {
92 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { 95 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
@@ -105,6 +108,7 @@ macro_rules! peripherals {
105 }; 108 };
106} 109}
107 110
111/// Convenience converting into reference.
108#[macro_export] 112#[macro_export]
109macro_rules! into_ref { 113macro_rules! into_ref {
110 ($($name:ident),*) => { 114 ($($name:ident),*) => {
@@ -114,6 +118,7 @@ macro_rules! into_ref {
114 } 118 }
115} 119}
116 120
121/// Implement the peripheral trait.
117#[macro_export] 122#[macro_export]
118macro_rules! impl_peripheral { 123macro_rules! impl_peripheral {
119 ($type:ident) => { 124 ($type:ident) => {
diff --git a/embassy-hal-internal/src/peripheral.rs b/embassy-hal-internal/src/peripheral.rs
index 38b4c452e..16d49edfb 100644
--- a/embassy-hal-internal/src/peripheral.rs
+++ b/embassy-hal-internal/src/peripheral.rs
@@ -20,6 +20,7 @@ pub struct PeripheralRef<'a, T> {
20} 20}
21 21
22impl<'a, T> PeripheralRef<'a, T> { 22impl<'a, T> PeripheralRef<'a, T> {
23 /// Create a new reference to a peripheral.
23 #[inline] 24 #[inline]
24 pub fn new(inner: T) -> Self { 25 pub fn new(inner: T) -> Self {
25 Self { 26 Self {
diff --git a/embassy-hal-internal/src/ratio.rs b/embassy-hal-internal/src/ratio.rs
index 9a8808a33..91dcfd47c 100644
--- a/embassy-hal-internal/src/ratio.rs
+++ b/embassy-hal-internal/src/ratio.rs
@@ -1,3 +1,4 @@
1//! Types for dealing with rational numbers.
1use core::ops::{Add, Div, Mul}; 2use core::ops::{Add, Div, Mul};
2 3
3use num_traits::{CheckedAdd, CheckedDiv, CheckedMul}; 4use num_traits::{CheckedAdd, CheckedDiv, CheckedMul};