use super::*;
use crate::alloc_endpoint::{self, EndpointConfig, EndpointData};
use crate::info::ENDPOINTS;
use crate::assert_eq;
pub struct MusbDriver<'d, T: MusbInstance> {
phantom: PhantomData<&'d mut T>,
alloc: [EndpointData; ENDPOINTS.len()],
#[cfg(not(feature = "_fixed-fifo-size"))]
next_fifo_addr_8bytes: u16,
}
impl<'d, T: MusbInstance> MusbDriver<'d, T> {
pub fn new() -> Self {
#[cfg(not(feature = "_fixed-fifo-size"))]
let next_fifo_addr_8bytes = 8;
let regs = T::regs();
regs.index().write(|w| w.set_index(0));
BUS_WAKER.wake();
Self {
phantom: PhantomData,
alloc: [EndpointData {
ep_conf: EndpointConfig {
ep_type: EndpointType::Bulk,
tx_max_packet_size: 0,
rx_max_packet_size: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
tx_fifo_size_bits: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
rx_fifo_size_bits: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
tx_fifo_addr_8bytes: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
rx_fifo_addr_8bytes: 0,
},
used_tx: false,
used_rx: false,
}; ENDPOINTS.len()],
#[cfg(not(feature = "_fixed-fifo-size"))]
next_fifo_addr_8bytes,
}
}
pub fn alloc_endpoint<D: Dir>(
&mut self,
ep_type: EndpointType,
ep_addr: Option<EndpointAddress>,
max_packet_size: u16,
interval_ms: u8,
) -> Result<Endpoint<'d, T, D>, driver::EndpointAllocError> {
trace!(
"musb/alloc_ep: allocating type={:?} mps={:?} interval_ms={}, dir={:?}",
ep_type,
max_packet_size,
interval_ms,
D::dir()
);
if let Some(addr) = ep_addr {
assert_eq!(addr.direction(), D::dir(), "Wrong addr.direction");
}
let index = alloc_endpoint::alloc_endpoint(
&mut self.alloc,
#[cfg(not(feature = "_fixed-fifo-size"))] &mut self.next_fifo_addr_8bytes,
ep_type,
ep_addr.map(|addr| addr.index() as u8),
D::dir(),
max_packet_size,
)
.map_err(|_| driver::EndpointAllocError)?;
Ok(Endpoint {
_phantom: PhantomData,
info: EndpointInfo {
addr: EndpointAddress::from_parts(index as usize, D::dir()),
ep_type,
max_packet_size,
interval_ms,
},
})
}
pub fn start(
mut self,
control_max_packet_size: u16,
) -> (crate::Bus<'d, T>, crate::ControlPipe<'d, T>) {
let ep_out = self
.alloc_endpoint(EndpointType::Control, Some(0x00.into()), control_max_packet_size, 0)
.unwrap();
let ep_in = self
.alloc_endpoint(EndpointType::Control, Some(0x80.into()), control_max_packet_size, 0)
.unwrap();
trace!("musb driver: start");
let mut ep_confs = [EndpointConfig {
ep_type: EndpointType::Bulk,
tx_max_packet_size: 0,
rx_max_packet_size: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
tx_fifo_size_bits: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
rx_fifo_size_bits: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
tx_fifo_addr_8bytes: 0,
#[cfg(not(feature = "_fixed-fifo-size"))]
rx_fifo_addr_8bytes: 0,
}; ENDPOINTS.len()];
for i in 0..ENDPOINTS.len() {
ep_confs[i] = self.alloc[i].ep_conf;
}
(
Bus {
phantom: PhantomData,
ep_confs,
inited: false,
},
ControlPipe {
_phantom: PhantomData,
max_packet_size: control_max_packet_size,
ep_out,
ep_in,
},
)
}
}