kernel/hil/
bus8080.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! The 8080 Bus Interface (used for LCD)
6
7use crate::ErrorCode;
8
9/// Bus width used for address width and data width
10pub enum BusWidth {
11    Bits8,
12    Bits16LE,
13    Bits16BE,
14}
15/// The enum represents the address of a bus-attached device.
16///
17/// For addresses larger than a single byte the enum variant
18/// captures the endianess used by the device on the bus.
19/// The address is stored in the host endianess in the u16 and
20/// must be converted to the correct endianess before using the
21/// address on the bus.
22pub enum BusAddr8080 {
23    BusAddr8(u8),
24    BusAddr16BE(u16),
25    BusAddr16LE(u16),
26}
27
28impl BusWidth {
29    pub fn width_in_bytes(&self) -> usize {
30        match self {
31            BusWidth::Bits8 => 1,
32            BusWidth::Bits16BE | BusWidth::Bits16LE => 2,
33        }
34    }
35}
36
37pub trait Bus8080<'a> {
38    /// Set the address to write to
39    fn set_addr(&self, addr: BusAddr8080) -> Result<(), ErrorCode>;
40    /// Write data items to the previously set address
41    fn write(
42        &self,
43        data_width: BusWidth,
44        buffer: &'a mut [u8],
45        len: usize,
46    ) -> Result<(), (ErrorCode, &'static mut [u8])>;
47
48    /// Read data items from the previously set address
49    fn read(
50        &self,
51        data_width: BusWidth,
52        buffer: &'a mut [u8],
53        len: usize,
54    ) -> Result<(), (ErrorCode, &'static mut [u8])>;
55
56    fn set_client(&self, client: &'a dyn Client);
57}
58
59pub trait Client {
60    /// Called when set_addr, write or read are complete
61    ///
62    /// set_address does not return a buffer
63    /// write and read return a buffer
64    /// len should be set to the number of data elements written
65    fn command_complete(
66        &self,
67        buffer: Option<&'static mut [u8]>,
68        len: usize,
69        status: Result<(), ErrorCode>,
70    );
71}