sel4_driver_interfaces/serial/
mod.rs

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
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::cell::RefCell;
use core::ops::Deref;

use lock_api::{Mutex, RawMutex};

use crate::{WrappedMutex, WrappedRefCell, WrappedRefCellError};

pub use embedded_hal_nb::nb;
pub use embedded_hal_nb::serial::{Error, ErrorKind, ErrorType, Read, Write};

mod write_buffered;

pub use write_buffered::WriteBuffered;

// // //

impl<E: Error> Error for WrappedRefCellError<E> {
    fn kind(&self) -> ErrorKind {
        match self {
            Self::Contention => ErrorKind::Other,
            Self::Other(err) => err.kind(),
        }
    }
}

impl<T: Deref<Target = RefCell<U>>, U: ErrorType> ErrorType for &WrappedRefCell<T> {
    type Error = WrappedRefCellError<U::Error>;
}

impl<Word: Copy, T: Deref<Target = RefCell<U>>, U: Read<Word>> Read<Word> for &WrappedRefCell<T> {
    fn read(&mut self) -> nb::Result<Word, Self::Error> {
        self.try_borrow_mut()?
            .read()
            .map_err(|err| err.map(WrappedRefCellError::Other))
    }
}

impl<Word: Copy, T: Deref<Target = RefCell<U>>, U: Write<Word>> Write<Word> for &WrappedRefCell<T> {
    fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> {
        self.try_borrow_mut()?
            .write(word)
            .map_err(|err| err.map(WrappedRefCellError::Other))
    }

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        self.try_borrow_mut()?
            .flush()
            .map_err(|err| err.map(WrappedRefCellError::Other))
    }
}

impl<R: RawMutex, T: Deref<Target = Mutex<R, U>>, U: ErrorType> ErrorType for &WrappedMutex<T> {
    type Error = U::Error;
}

impl<R: RawMutex, Word: Copy, T: Deref<Target = Mutex<R, U>>, U: Read<Word>> Read<Word>
    for &WrappedMutex<T>
{
    fn read(&mut self) -> nb::Result<Word, Self::Error> {
        self.0.lock().read()
    }
}

impl<R: RawMutex, Word: Copy, T: Deref<Target = Mutex<R, U>>, U: Write<Word>> Write<Word>
    for &WrappedMutex<T>
{
    fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> {
        self.0.lock().write(word)
    }

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        self.0.lock().flush()
    }
}