sel4_pl011_driver/
lib.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7#![no_std]
8
9use core::convert::Infallible;
10
11use embedded_hal_nb::nb;
12use embedded_hal_nb::serial;
13
14use sel4_driver_interfaces::HandleInterrupt;
15
16mod device;
17
18use device::Device;
19
20pub struct Driver {
21    device: Device,
22}
23
24unsafe impl Send for Driver {}
25unsafe impl Sync for Driver {}
26
27impl Driver {
28    #[allow(clippy::missing_safety_doc)]
29    pub const unsafe fn new_uninit(ptr: *mut ()) -> Self {
30        Self {
31            device: Device::new(ptr.cast()),
32        }
33    }
34
35    #[allow(clippy::missing_safety_doc)]
36    pub unsafe fn new(ptr: *mut ()) -> Self {
37        let mut this = Self::new_uninit(ptr);
38        this.init();
39        this
40    }
41
42    pub fn init(&mut self) {
43        self.device.init();
44    }
45}
46
47impl serial::ErrorType for Driver {
48    type Error = Infallible;
49}
50
51impl serial::Read for Driver {
52    fn read(&mut self) -> nb::Result<u8, Self::Error> {
53        self.device.get_char().ok_or(nb::Error::WouldBlock)
54    }
55}
56
57impl serial::Write for Driver {
58    fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
59        self.device.put_char(word);
60        Ok(())
61    }
62
63    fn flush(&mut self) -> nb::Result<(), Self::Error> {
64        Ok(())
65    }
66}
67
68impl HandleInterrupt for Driver {
69    fn handle_interrupt(&mut self) {
70        self.device.clear_all_interrupts()
71    }
72}