diff options
| author | Dániel Buga <[email protected]> | 2024-04-27 18:01:45 +0200 |
|---|---|---|
| committer | Dániel Buga <[email protected]> | 2024-04-27 18:01:45 +0200 |
| commit | 887d7e143029998e84242a864e26f44b2fccb8a3 (patch) | |
| tree | 6030c4ca10b5956a035f65ab0b24348a8db7f8fb /embassy-usb-synopsys-otg | |
| parent | 61e3ca049c604384593168037e80186c317dacbb (diff) | |
Configure MAX_EP_COUNT via const generics
Diffstat (limited to 'embassy-usb-synopsys-otg')
| -rw-r--r-- | embassy-usb-synopsys-otg/src/lib.rs | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/embassy-usb-synopsys-otg/src/lib.rs b/embassy-usb-synopsys-otg/src/lib.rs index 59be2c0be..a84f50e5a 100644 --- a/embassy-usb-synopsys-otg/src/lib.rs +++ b/embassy-usb-synopsys-otg/src/lib.rs | |||
| @@ -23,7 +23,12 @@ pub mod otg_v1; | |||
| 23 | use otg_v1::{regs, vals, Otg}; | 23 | use otg_v1::{regs, vals, Otg}; |
| 24 | 24 | ||
| 25 | /// Handle interrupts. | 25 | /// Handle interrupts. |
| 26 | pub unsafe fn on_interrupt(r: Otg, state: &State<{ MAX_EP_COUNT }>, ep_count: usize, quirk_setup_late_cnak: bool) { | 26 | pub unsafe fn on_interrupt<const MAX_EP_COUNT: usize>( |
| 27 | r: Otg, | ||
| 28 | state: &State<MAX_EP_COUNT>, | ||
| 29 | ep_count: usize, | ||
| 30 | quirk_setup_late_cnak: bool, | ||
| 31 | ) { | ||
| 27 | let ints = r.gintsts().read(); | 32 | let ints = r.gintsts().read(); |
| 28 | if ints.wkupint() || ints.usbsusp() || ints.usbrst() || ints.enumdne() || ints.otgint() || ints.srqint() { | 33 | if ints.wkupint() || ints.usbsusp() || ints.usbrst() || ints.enumdne() || ints.otgint() || ints.srqint() { |
| 29 | // Mask interrupts and notify `Bus` to process them | 34 | // Mask interrupts and notify `Bus` to process them |
| @@ -291,16 +296,16 @@ impl Default for Config { | |||
| 291 | } | 296 | } |
| 292 | 297 | ||
| 293 | /// USB OTG driver. | 298 | /// USB OTG driver. |
| 294 | pub struct Driver<'d> { | 299 | pub struct Driver<'d, const MAX_EP_COUNT: usize> { |
| 295 | config: Config, | 300 | config: Config, |
| 296 | ep_in: [Option<EndpointData>; MAX_EP_COUNT], | 301 | ep_in: [Option<EndpointData>; MAX_EP_COUNT], |
| 297 | ep_out: [Option<EndpointData>; MAX_EP_COUNT], | 302 | ep_out: [Option<EndpointData>; MAX_EP_COUNT], |
| 298 | ep_out_buffer: &'d mut [u8], | 303 | ep_out_buffer: &'d mut [u8], |
| 299 | ep_out_buffer_offset: usize, | 304 | ep_out_buffer_offset: usize, |
| 300 | instance: OtgInstance<'d>, | 305 | instance: OtgInstance<'d, MAX_EP_COUNT>, |
| 301 | } | 306 | } |
| 302 | 307 | ||
| 303 | impl<'d> Driver<'d> { | 308 | impl<'d, const MAX_EP_COUNT: usize> Driver<'d, MAX_EP_COUNT> { |
| 304 | /// Initializes the USB OTG peripheral. | 309 | /// Initializes the USB OTG peripheral. |
| 305 | /// | 310 | /// |
| 306 | /// # Arguments | 311 | /// # Arguments |
| @@ -310,7 +315,7 @@ impl<'d> Driver<'d> { | |||
| 310 | /// Endpoint allocation will fail if it is too small. | 315 | /// Endpoint allocation will fail if it is too small. |
| 311 | /// * `instance` - The USB OTG peripheral instance and its configuration. | 316 | /// * `instance` - The USB OTG peripheral instance and its configuration. |
| 312 | /// * `config` - The USB driver configuration. | 317 | /// * `config` - The USB driver configuration. |
| 313 | pub fn new(ep_out_buffer: &'d mut [u8], instance: OtgInstance<'d>, config: Config) -> Self { | 318 | pub fn new(ep_out_buffer: &'d mut [u8], instance: OtgInstance<'d, MAX_EP_COUNT>, config: Config) -> Self { |
| 314 | Self { | 319 | Self { |
| 315 | config, | 320 | config, |
| 316 | ep_in: [None; MAX_EP_COUNT], | 321 | ep_in: [None; MAX_EP_COUNT], |
| @@ -414,11 +419,11 @@ impl<'d> Driver<'d> { | |||
| 414 | } | 419 | } |
| 415 | } | 420 | } |
| 416 | 421 | ||
| 417 | impl<'d> embassy_usb_driver::Driver<'d> for Driver<'d> { | 422 | impl<'d, const MAX_EP_COUNT: usize> embassy_usb_driver::Driver<'d> for Driver<'d, MAX_EP_COUNT> { |
| 418 | type EndpointOut = Endpoint<'d, Out>; | 423 | type EndpointOut = Endpoint<'d, Out>; |
| 419 | type EndpointIn = Endpoint<'d, In>; | 424 | type EndpointIn = Endpoint<'d, In>; |
| 420 | type ControlPipe = ControlPipe<'d>; | 425 | type ControlPipe = ControlPipe<'d>; |
| 421 | type Bus = Bus<'d>; | 426 | type Bus = Bus<'d, MAX_EP_COUNT>; |
| 422 | 427 | ||
| 423 | fn alloc_endpoint_in( | 428 | fn alloc_endpoint_in( |
| 424 | &mut self, | 429 | &mut self, |
| @@ -474,15 +479,15 @@ impl<'d> embassy_usb_driver::Driver<'d> for Driver<'d> { | |||
| 474 | } | 479 | } |
| 475 | 480 | ||
| 476 | /// USB bus. | 481 | /// USB bus. |
| 477 | pub struct Bus<'d> { | 482 | pub struct Bus<'d, const MAX_EP_COUNT: usize> { |
| 478 | config: Config, | 483 | config: Config, |
| 479 | ep_in: [Option<EndpointData>; MAX_EP_COUNT], | 484 | ep_in: [Option<EndpointData>; MAX_EP_COUNT], |
| 480 | ep_out: [Option<EndpointData>; MAX_EP_COUNT], | 485 | ep_out: [Option<EndpointData>; MAX_EP_COUNT], |
| 481 | instance: OtgInstance<'d>, | 486 | instance: OtgInstance<'d, MAX_EP_COUNT>, |
| 482 | inited: bool, | 487 | inited: bool, |
| 483 | } | 488 | } |
| 484 | 489 | ||
| 485 | impl<'d> Bus<'d> { | 490 | impl<'d, const MAX_EP_COUNT: usize> Bus<'d, MAX_EP_COUNT> { |
| 486 | fn restore_irqs(&mut self) { | 491 | fn restore_irqs(&mut self) { |
| 487 | self.instance.regs.gintmsk().write(|w| { | 492 | self.instance.regs.gintmsk().write(|w| { |
| 488 | w.set_usbrst(true); | 493 | w.set_usbrst(true); |
| @@ -496,9 +501,7 @@ impl<'d> Bus<'d> { | |||
| 496 | w.set_otgint(true); | 501 | w.set_otgint(true); |
| 497 | }); | 502 | }); |
| 498 | } | 503 | } |
| 499 | } | ||
| 500 | 504 | ||
| 501 | impl<'d> Bus<'d> { | ||
| 502 | /// Returns the PHY type. | 505 | /// Returns the PHY type. |
| 503 | pub fn phy_type(&self) -> PhyType { | 506 | pub fn phy_type(&self) -> PhyType { |
| 504 | self.instance.phy_type | 507 | self.instance.phy_type |
| @@ -715,7 +718,7 @@ impl<'d> Bus<'d> { | |||
| 715 | } | 718 | } |
| 716 | } | 719 | } |
| 717 | 720 | ||
| 718 | impl<'d> embassy_usb_driver::Bus for Bus<'d> { | 721 | impl<'d, const MAX_EP_COUNT: usize> embassy_usb_driver::Bus for Bus<'d, MAX_EP_COUNT> { |
| 719 | async fn poll(&mut self) -> Event { | 722 | async fn poll(&mut self) -> Event { |
| 720 | poll_fn(move |cx| { | 723 | poll_fn(move |cx| { |
| 721 | if !self.inited { | 724 | if !self.inited { |
| @@ -1292,17 +1295,12 @@ fn ep0_mpsiz(max_packet_size: u16) -> u16 { | |||
| 1292 | } | 1295 | } |
| 1293 | } | 1296 | } |
| 1294 | 1297 | ||
| 1295 | /// The number of maximum configurable EPs. | ||
| 1296 | // TODO: this should at least be configurable, but ideally not a constant. | ||
| 1297 | // Using OtgInstance::ENDPOINT_COUNT requires feature(const_generic_expr) so just define maximum eps | ||
| 1298 | pub const MAX_EP_COUNT: usize = 9; | ||
| 1299 | |||
| 1300 | /// Hardware-dependent USB IP configuration. | 1298 | /// Hardware-dependent USB IP configuration. |
| 1301 | pub struct OtgInstance<'d> { | 1299 | pub struct OtgInstance<'d, const MAX_EP_COUNT: usize> { |
| 1302 | /// The USB peripheral. | 1300 | /// The USB peripheral. |
| 1303 | pub regs: Otg, | 1301 | pub regs: Otg, |
| 1304 | /// The USB state. | 1302 | /// The USB state. |
| 1305 | pub state: &'d State<{ MAX_EP_COUNT }>, | 1303 | pub state: &'d State<MAX_EP_COUNT>, |
| 1306 | /// FIFO depth in words. | 1304 | /// FIFO depth in words. |
| 1307 | pub fifo_depth_words: u16, | 1305 | pub fifo_depth_words: u16, |
| 1308 | /// Number of used endpoints. | 1306 | /// Number of used endpoints. |
