capsules_extra/cyw4343/bus/mod.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 OxidOS Automotive 2025.
4
5//! Common interface for CYW4343x buses.
6//!
7//! The buses support 3 functions:
8//! F0: Standard bus function. These packets are used to write/read the chip's registers (e.g.
9//! disable overflow interrupts)
10//! F1: Backplane function. These packets are used to access the internal address space (e.g.
11//! writing the firmware)
12//! F2: WLAN function. These packets are used for WLAN data.
13//!
14//! F0 and F1 functions are used in the configuring/initialisation process of each bus
15//! implementation (setting up registers, loading binaries).
16//!
17//! F2 is used for reading/writing WLAN packets. The bus implementation should only handle
18//! sending data as F2 packets and it should expect that the SDPCM protocol headers have been added by
19//! the upper layer.
20//!
21//! The bus should be responsible for handling interrupts from the chip and notify the clients of
22//! incoming data with the `packet_available` method or with the `State::Available` state.
23
24use kernel::utilities::leasable_buffer::SubSliceMut;
25
26mod common;
27pub mod spi;
28
29/// Addresses (F0/F1/F2) are 32 bits
30pub type RegAddr = u32;
31
32/// Function of the incoming operation
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34#[repr(u8)]
35pub enum Function {
36 /// Register access
37 Bus = 0b00,
38 /// Inner address space access
39 Backplane = 0b01,
40 /// WLAN packets
41 Wlan = 0b10,
42}
43
44/// Supported transfer types
45#[derive(Debug, Clone, Copy)]
46#[repr(u8)]
47pub enum Type {
48 Read = 0b0,
49 Write = 0b1,
50}
51
52/// Length of bytes to read/write in a register
53#[derive(Debug, Clone, Copy)]
54#[repr(u8)]
55pub enum RegLen {
56 Byte = 1,
57 HalfWord = 2,
58 Word = 4,
59}
60
61/// Current state of the bus
62#[derive(Clone, Copy, Debug)]
63pub enum State {
64 /// No incoming packet available
65 Idle,
66 /// CLient should wait for a `packet_available` call
67 Incoming,
68 /// A packet is now available to be read
69 Available(usize),
70}
71
72/// Trait for a bus that is used to communicate with CYW43xx device
73pub trait CYW4343xBus<'a> {
74 /// Set the client to be used for callbacks of the `Cyw43Bus` implementation
75 fn set_client(&self, client: &'a dyn CYW4343xBusClient);
76
77 /// Initialise the bus
78 fn init(&self) -> Result<(), kernel::ErrorCode>;
79
80 /// Write a WLAN (F2) packet
81 fn write_bytes(
82 &self,
83 buffer: SubSliceMut<'static, u8>,
84 ) -> Result<(), (kernel::ErrorCode, SubSliceMut<'static, u8>)>;
85
86 /// Read a WLAN (F2) packet if available.
87 fn read_bytes(
88 &self,
89 buffer: SubSliceMut<'static, u8>,
90 len: usize,
91 ) -> Result<(), (kernel::ErrorCode, SubSliceMut<'static, u8>)>;
92
93 /// Get current bus state
94 fn state(&self) -> Result<State, kernel::ErrorCode>;
95}
96
97/// Client trait for defining callbacks on initialisation, transfer
98/// and F2 packet interrupts.
99pub trait CYW4343xBusClient {
100 /// Initialisation process is done
101 fn init_done(&self, rval: Result<(), kernel::ErrorCode>);
102
103 /// WLAN write done
104 fn write_bytes_done(
105 &self,
106 buffer: SubSliceMut<'static, u8>,
107 rval: Result<(), kernel::ErrorCode>,
108 );
109
110 /// WLAN read done
111 fn read_bytes_done(
112 &self,
113 buffer: SubSliceMut<'static, u8>,
114 rval: Result<(), kernel::ErrorCode>,
115 );
116
117 /// An F2 (WLAN) packet is available
118 fn packet_available(&self, len: usize);
119}