1use bitfield::bitfield;
2use core::fmt::{Debug, Error as FmtError, Formatter};
3use core::mem;
4use core::ptr::null_mut;
56type FmtResult = Result<(), FmtError>;
78bitfield! {
9struct PckSize(u32);
10impl Debug;
11pub byte_count, set_byte_count: 13, 0;
12pub multi_packet_size, set_multi_packet_size: 27, 14;
13pub size, set_size: 30, 28;
14pub auto_zlp, set_auto_zlp : 31;
15}
1617bitfield! {
18struct ExtReg(u16);
19impl Debug;
20pub subpid, set_subpid: 3, 0;
21pub link_state, set_link_state: 7, 4;
22pub besl, set_besl: 11, 8;
23pub remote_wake, set_remote_wake: 12;
24}
2526bitfield! {
27struct StatusBk(u8);
28impl Debug;
29pub crc_error, set_crc_error: 0;
30pub error_flow, set_error_flow: 1;
31}
3233#[repr(C)]
34#[derive(Debug)]
35pub struct DeviceDescBank {
36/// endpoint data buffer, must be 32-bit aligned
37addr: *mut u8,
38 pcksize: PckSize,
39 extreg: ExtReg,
40 status_bk: StatusBk,
41 _reserved: [u8; 5],
42}
4344impl DeviceDescBank {
45fn new() -> Self {
46debug_assert_eq!(16, mem::size_of::<DeviceDescBank>());
47Self {
48 addr: null_mut(),
49 pcksize: PckSize(0),
50 extreg: ExtReg(0),
51 status_bk: StatusBk(0),
52 _reserved: [0, 0, 0, 0, 0],
53 }
54 }
5556/// This bit defines the automatic Zero Length Packet mode of the endpoint.
57 /// When enabled, the USB module will manage the ZLP handshake by hardware.
58 /// This bit is for IN endpoints only. When disabled the handshake should be
59 /// managed by firmware.
60#[allow(unused)]
61pub fn set_auto_zlp(&mut self, enable: bool) {
62self.pcksize.set_auto_zlp(enable);
63 }
6465/// These bits contains the maximum packet size of the endpoint.
66 ///
67 /// The maximum packet size is encoded in 3 bits; this method takes any u16
68 /// below 1024B and rounds up to the lowest endpoint size value which will
69 /// accommodate `size`. Panics if a `size` > 1023 is supplied.
70pub fn set_endpoint_size(&mut self, size: u16) {
71let size = match size {
721..=8 => 0u32,
739..=16 => 1,
7417..=32 => 2,
7533..=64 => 3,
7665..=128 => 4,
77129..=256 => 5,
78257..=512 => 6,
79513..=1023 => 7,
80_ => unreachable!(),
81 };
82self.pcksize.set_size(size);
83 }
8485#[allow(unused)]
86pub fn get_endpoint_size(&self) -> u16 {
87let bits = self.pcksize.size();
88match bits {
890 => 8,
901 => 16,
912 => 32,
923 => 64,
934 => 128,
945 => 256,
956 => 512,
967 => 1023,
97_ => unreachable!(),
98 }
99 }
100101/// For IN endpoints, MULTI_PACKET_SIZE holds the total number of bytes
102 /// sent. MULTI_PACKET_SIZE should be written to zero when setting up a new
103 /// transfer.
104pub fn set_multi_packet_size(&mut self, size: u16) {
105self.pcksize.set_multi_packet_size(size.into());
106 }
107108/// For OUT endpoints, MULTI_PACKET_SIZE holds the total data
109 /// size for the complete transfer. This value must be a multiple of the
110 /// maximum packet size.
111#[allow(dead_code)]
112pub fn get_multi_packet_size(&self) -> u16 {
113self.pcksize.multi_packet_size() as u16
114 }
115116/// For IN endpoints, BYTE_COUNT holds the number of bytes to be sent in the
117 /// next IN transaction.
118 /// For OUT endpoint or SETUP endpoints, BYTE_COUNT
119 /// holds the number of bytes received upon the last OUT or SETUP
120 /// transaction.
121pub fn set_byte_count(&mut self, size: u16) {
122self.pcksize.set_byte_count(size.into());
123 }
124125/// For IN endpoints, BYTE_COUNT holds the number of bytes to be sent in the
126 /// next IN transaction.
127 /// For OUT endpoint or SETUP endpoints, BYTE_COUNT
128 /// holds the number of bytes received upon the last OUT or SETUP
129 /// transaction.
130pub fn get_byte_count(&self) -> u16 {
131self.pcksize.byte_count() as u16
132 }
133134#[allow(unused)]
135pub fn link_state(&self) -> u8 {
136// every value except 1 (L1 sleep) is reserved
137self.extreg.link_state() as u8
138 }
139140/// best effort service latency
141#[allow(unused)]
142pub fn besl(&self) -> u8 {
143self.extreg.besl() as u8
144 }
145146#[allow(unused)]
147pub fn remote_wake(&self) -> bool {
148self.extreg.remote_wake()
149 }
150151/// These bits define the SUBPID field of a received extended token. These
152 /// bits are updated when the USB has answered by an handshake token
153 /// ACK to a LPM transaction
154#[allow(unused)]
155pub fn subpid(&self) -> u8 {
156self.extreg.subpid() as u8
157 }
158159/// This bit defines the Error Flow Status. This bit is set when a Error
160 /// Flow has been detected during transfer from/towards this bank. For OUT
161 /// transfer, a NAK handshake has been sent. For Isochronous OUT transfer,
162 /// an overrun condition has occurred. For IN transfer, this bit is not
163 /// valid. EPSTATUS.TRFAIL0 and EPSTATUS.TRFAIL1 should reflect the flow
164 /// errors.
165#[allow(unused)]
166pub fn error_flow(&self) -> bool {
167self.status_bk.error_flow()
168 }
169170/// This bit defines the CRC Error Status. This bit is set when a CRC
171 /// error has been detected in an isochronous OUT endpoint bank
172#[allow(unused)]
173pub fn crc_error(&self) -> bool {
174self.status_bk.crc_error()
175 }
176177pub fn set_address(&mut self, address: *mut u8) {
178self.addr = address;
179 }
180181pub fn get_address(&self) -> *mut u8 {
182self.addr
183 }
184}
185186#[derive(Debug)]
187#[repr(C)]
188pub struct DeviceDescriptor {
189 bank: [DeviceDescBank; 2],
190}
191192impl DeviceDescriptor {
193fn new() -> Self {
194debug_assert_eq!(32, mem::size_of::<DeviceDescriptor>());
195Self {
196 bank: [DeviceDescBank::new(), DeviceDescBank::new()],
197 }
198 }
199}
200201pub struct Descriptors {
202 desc: [DeviceDescriptor; 8],
203}
204205impl Debug for Descriptors {
206fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
207for ep in 0..8 {
208write!(fmt, "\nep{}: {:?}", ep, &self.desc[ep])?;
209 }
210Ok(())
211 }
212}
213214impl Descriptors {
215pub fn new() -> Self {
216Self {
217 desc: [
218 DeviceDescriptor::new(),
219 DeviceDescriptor::new(),
220 DeviceDescriptor::new(),
221 DeviceDescriptor::new(),
222 DeviceDescriptor::new(),
223 DeviceDescriptor::new(),
224 DeviceDescriptor::new(),
225 DeviceDescriptor::new(),
226 ],
227 }
228 }
229230pub fn address(&self) -> u32 {
231&self.desc as *const _ as u32
232 }
233234pub fn bank(&mut self, idx: usize, bank: usize) -> &mut DeviceDescBank {
235&mut self.desc[idx].bank[bank]
236 }
237}
238239unsafe impl Send for DeviceDescBank {}