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#[allow(unused)]
57/// This bit defines the automatic Zero Length Packet mode of the endpoint.
58 /// When enabled, the USB module will manage the ZLP handshake by hardware.
59 /// This bit is for IN endpoints only. When disabled the handshake should be
60 /// managed by firmware.
61#[allow(unused_variables)]
62pub fn set_auto_zlp(&mut self, enable: bool) {
63self.pcksize.set_auto_zlp(enable);
64 }
6566/// These bits contains the maximum packet size of the endpoint.
67 ///
68 /// The maximum packet size is encoded in 3 bits; this method takes any u16
69 /// below 1024B and rounds up to the lowest endpoint size value which will
70 /// accommodate `size`. Panics if a `size` > 1023 is supplied.
71pub fn set_endpoint_size(&mut self, size: u16) {
72let size = match size {
731..=8 => 0u32,
749..=16 => 1,
7517..=32 => 2,
7633..=64 => 3,
7765..=128 => 4,
78129..=256 => 5,
79257..=512 => 6,
80513..=1023 => 7,
81_ => unreachable!(),
82 };
83self.pcksize.set_size(size);
84 }
8586#[allow(unused)]
87pub fn get_endpoint_size(&self) -> u16 {
88let bits = self.pcksize.size();
89match bits {
900 => 8,
911 => 16,
922 => 32,
933 => 64,
944 => 128,
955 => 256,
966 => 512,
977 => 1023,
98_ => unreachable!(),
99 }
100 }
101102/// For IN endpoints, MULTI_PACKET_SIZE holds the total number of bytes
103 /// sent. MULTI_PACKET_SIZE should be written to zero when setting up a new
104 /// transfer.
105pub fn set_multi_packet_size(&mut self, size: u16) {
106self.pcksize.set_multi_packet_size(size.into());
107 }
108109/// For OUT endpoints, MULTI_PACKET_SIZE holds the total data
110 /// size for the complete transfer. This value must be a multiple of the
111 /// maximum packet size.
112#[allow(dead_code)]
113pub fn get_multi_packet_size(&self) -> u16 {
114self.pcksize.multi_packet_size() as u16
115 }
116117/// For IN endpoints, BYTE_COUNT holds the number of bytes to be sent in the
118 /// next IN transaction.
119 /// For OUT endpoint or SETUP endpoints, BYTE_COUNT
120 /// holds the number of bytes received upon the last OUT or SETUP
121 /// transaction.
122pub fn set_byte_count(&mut self, size: u16) {
123self.pcksize.set_byte_count(size.into());
124 }
125126/// For IN endpoints, BYTE_COUNT holds the number of bytes to be sent in the
127 /// next IN transaction.
128 /// For OUT endpoint or SETUP endpoints, BYTE_COUNT
129 /// holds the number of bytes received upon the last OUT or SETUP
130 /// transaction.
131pub fn get_byte_count(&self) -> u16 {
132self.pcksize.byte_count() as u16
133 }
134135#[allow(unused)]
136pub fn link_state(&self) -> u8 {
137// every value except 1 (L1 sleep) is reserved
138self.extreg.link_state() as u8
139 }
140141#[allow(unused)]
142/// best effort service latency
143pub fn besl(&self) -> u8 {
144self.extreg.besl() as u8
145 }
146147#[allow(unused)]
148pub fn remote_wake(&self) -> bool {
149self.extreg.remote_wake()
150 }
151152#[allow(unused)]
153/// These bits define the SUBPID field of a received extended token. These
154 /// bits are updated when the USB has answered by an handshake token
155 /// ACK to a LPM transaction
156pub fn subpid(&self) -> u8 {
157self.extreg.subpid() as u8
158 }
159160#[allow(unused)]
161/// This bit defines the Error Flow Status. This bit is set when a Error
162 /// Flow has been detected during transfer from/towards this bank. For OUT
163 /// transfer, a NAK handshake has been sent. For Isochronous OUT transfer,
164 /// an overrun condition has occurred. For IN transfer, this bit is not
165 /// valid. EPSTATUS.TRFAIL0 and EPSTATUS.TRFAIL1 should reflect the flow
166 /// errors.
167pub fn error_flow(&self) -> bool {
168self.status_bk.error_flow()
169 }
170171#[allow(unused)]
172/// This bit defines the CRC Error Status. This bit is set when a CRC
173 /// error has been detected in an isochronous OUT endpoint bank
174pub fn crc_error(&self) -> bool {
175self.status_bk.crc_error()
176 }
177178pub fn set_address(&mut self, address: *mut u8) {
179self.addr = address;
180 }
181182pub fn get_address(&self) -> *mut u8 {
183self.addr
184 }
185}
186187#[derive(Debug)]
188#[repr(C)]
189pub struct DeviceDescriptor {
190 bank: [DeviceDescBank; 2],
191}
192193impl DeviceDescriptor {
194fn new() -> Self {
195debug_assert_eq!(32, mem::size_of::<DeviceDescriptor>());
196Self {
197 bank: [DeviceDescBank::new(), DeviceDescBank::new()],
198 }
199 }
200}
201202pub struct Descriptors {
203 desc: [DeviceDescriptor; 8],
204}
205206impl Debug for Descriptors {
207fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
208for ep in 0..8 {
209write!(fmt, "\nep{}: {:?}", ep, &self.desc[ep])?;
210 }
211Ok(())
212 }
213}
214215impl Descriptors {
216pub fn new() -> Self {
217Self {
218 desc: [
219 DeviceDescriptor::new(),
220 DeviceDescriptor::new(),
221 DeviceDescriptor::new(),
222 DeviceDescriptor::new(),
223 DeviceDescriptor::new(),
224 DeviceDescriptor::new(),
225 DeviceDescriptor::new(),
226 DeviceDescriptor::new(),
227 ],
228 }
229 }
230231pub fn address(&self) -> u32 {
232&self.desc as *const _ as u32
233 }
234235pub fn bank(&mut self, idx: usize, bank: usize) -> &mut DeviceDescBank {
236&mut self.desc[idx].bank[bank]
237 }
238}
239240unsafe impl Send for DeviceDescBank {}