use super::*;
use crate::alloc_endpoint::{self, EndpointConfig, EndpointData};
pub struct MusbDriver<'d, T: MusbInstance> {
phantom: PhantomData<&'d mut T>,
alloc: [EndpointData; ENDPOINTS_NUM],
}
impl<'d, T: MusbInstance> MusbDriver<'d, T> {
pub fn new() -> Self {
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_fifo_size_dword: 1,
rx_max_fifo_size_dword: 1,
},
used_tx: false,
used_rx: false,
}; ENDPOINTS_NUM],
}
}
pub fn alloc_endpoint<D: Dir>(
&mut self,
ep_type: EndpointType,
max_packet_size: u16,
interval_ms: u8,
ep_index: Option<u8>,
) -> Result<Endpoint<'d, T, D>, driver::EndpointAllocError> {
trace!(
"allocating type={:?} mps={:?} interval_ms={}, dir={:?}",
ep_type,
max_packet_size,
interval_ms,
D::dir()
);
let index = alloc_endpoint::alloc_endpoint(
&mut self.alloc,
ep_type,
ep_index,
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, control_max_packet_size, 0, Some(0))
.unwrap();
let ep_in = self
.alloc_endpoint(EndpointType::Control, control_max_packet_size, 0, Some(0))
.unwrap();
trace!("enabled");
let mut ep_confs = [EndpointConfig {
ep_type: EndpointType::Bulk,
tx_max_fifo_size_dword: 1,
rx_max_fifo_size_dword: 1,
}; ENDPOINTS_NUM];
for i in 0..ENDPOINTS_NUM {
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,
},
)
}
}