diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-03-28 02:20:01 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-06 05:38:11 +0200 |
| commit | a2f5763a67441e5e6c981407938c21c58e9eb669 (patch) | |
| tree | 48f5d58eaf04e6a85da706e6960ac96cf5cd02ab /embassy-usb | |
| parent | a062baae3837b453f0c05d0730ba465c6d2c2446 (diff) | |
usb: add `add_class` to builder, so that `FooBarClass::new(&mut builder)` can set up everything.
Diffstat (limited to 'embassy-usb')
| -rw-r--r-- | embassy-usb/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-usb/src/builder.rs | 15 | ||||
| -rw-r--r-- | embassy-usb/src/lib.rs | 7 |
3 files changed, 19 insertions, 4 deletions
diff --git a/embassy-usb/Cargo.toml b/embassy-usb/Cargo.toml index 5a5a6d7ab..af5986c15 100644 --- a/embassy-usb/Cargo.toml +++ b/embassy-usb/Cargo.toml | |||
| @@ -10,3 +10,4 @@ defmt = { version = "0.3", optional = true } | |||
| 10 | log = { version = "0.4.14", optional = true } | 10 | log = { version = "0.4.14", optional = true } |
| 11 | cortex-m = "0.7.3" | 11 | cortex-m = "0.7.3" |
| 12 | num-traits = { version = "0.2.14", default-features = false } | 12 | num-traits = { version = "0.2.14", default-features = false } |
| 13 | heapless = "0.7.10" \ No newline at end of file | ||
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs index bcb838ff5..491acf4d3 100644 --- a/embassy-usb/src/builder.rs +++ b/embassy-usb/src/builder.rs | |||
| @@ -1,8 +1,11 @@ | |||
| 1 | use heapless::Vec; | ||
| 2 | |||
| 1 | use super::class::UsbClass; | 3 | use super::class::UsbClass; |
| 2 | use super::descriptor::{BosWriter, DescriptorWriter}; | 4 | use super::descriptor::{BosWriter, DescriptorWriter}; |
| 3 | use super::driver::{Driver, EndpointAllocError}; | 5 | use super::driver::{Driver, EndpointAllocError}; |
| 4 | use super::types::*; | 6 | use super::types::*; |
| 5 | use super::UsbDevice; | 7 | use super::UsbDevice; |
| 8 | use super::MAX_CLASS_COUNT; | ||
| 6 | 9 | ||
| 7 | #[derive(Debug, Copy, Clone)] | 10 | #[derive(Debug, Copy, Clone)] |
| 8 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 11 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -116,6 +119,7 @@ impl<'a> Config<'a> { | |||
| 116 | /// Used to build new [`UsbDevice`]s. | 119 | /// Used to build new [`UsbDevice`]s. |
| 117 | pub struct UsbDeviceBuilder<'d, D: Driver<'d>> { | 120 | pub struct UsbDeviceBuilder<'d, D: Driver<'d>> { |
| 118 | config: Config<'d>, | 121 | config: Config<'d>, |
| 122 | classes: Vec<&'d mut dyn UsbClass, MAX_CLASS_COUNT>, | ||
| 119 | 123 | ||
| 120 | bus: D, | 124 | bus: D, |
| 121 | next_interface_number: u8, | 125 | next_interface_number: u8, |
| @@ -165,6 +169,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { | |||
| 165 | UsbDeviceBuilder { | 169 | UsbDeviceBuilder { |
| 166 | bus, | 170 | bus, |
| 167 | config, | 171 | config, |
| 172 | classes: Vec::new(), | ||
| 168 | next_interface_number: 0, | 173 | next_interface_number: 0, |
| 169 | next_string_index: 4, | 174 | next_string_index: 4, |
| 170 | 175 | ||
| @@ -175,7 +180,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { | |||
| 175 | } | 180 | } |
| 176 | 181 | ||
| 177 | /// Creates the [`UsbDevice`] instance with the configuration in this builder. | 182 | /// Creates the [`UsbDevice`] instance with the configuration in this builder. |
| 178 | pub fn build(mut self, classes: &'d mut [&'d mut dyn UsbClass]) -> UsbDevice<'d, D> { | 183 | pub fn build(mut self) -> UsbDevice<'d, D> { |
| 179 | self.config_descriptor.end_configuration(); | 184 | self.config_descriptor.end_configuration(); |
| 180 | self.bos_descriptor.end_bos(); | 185 | self.bos_descriptor.end_bos(); |
| 181 | 186 | ||
| @@ -185,10 +190,16 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { | |||
| 185 | self.device_descriptor.into_buf(), | 190 | self.device_descriptor.into_buf(), |
| 186 | self.config_descriptor.into_buf(), | 191 | self.config_descriptor.into_buf(), |
| 187 | self.bos_descriptor.writer.into_buf(), | 192 | self.bos_descriptor.writer.into_buf(), |
| 188 | classes, | 193 | self.classes, |
| 189 | ) | 194 | ) |
| 190 | } | 195 | } |
| 191 | 196 | ||
| 197 | pub fn add_class(&mut self, class: &'d mut dyn UsbClass) { | ||
| 198 | if self.classes.push(class).is_err() { | ||
| 199 | panic!("max class count reached") | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 192 | /// Allocates a new interface number. | 203 | /// Allocates a new interface number. |
| 193 | pub fn alloc_interface(&mut self) -> InterfaceNumber { | 204 | pub fn alloc_interface(&mut self) -> InterfaceNumber { |
| 194 | let number = self.next_interface_number; | 205 | let number = self.next_interface_number; |
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs index ff3930afa..2c00e8817 100644 --- a/embassy-usb/src/lib.rs +++ b/embassy-usb/src/lib.rs | |||
| @@ -14,6 +14,7 @@ pub mod types; | |||
| 14 | mod util; | 14 | mod util; |
| 15 | 15 | ||
| 16 | use class::ControlInRequestStatus; | 16 | use class::ControlInRequestStatus; |
| 17 | use heapless::Vec; | ||
| 17 | 18 | ||
| 18 | use self::class::{RequestStatus, UsbClass}; | 19 | use self::class::{RequestStatus, UsbClass}; |
| 19 | use self::control::*; | 20 | use self::control::*; |
| @@ -53,6 +54,8 @@ pub const CONFIGURATION_VALUE: u8 = 1; | |||
| 53 | /// The default value for bAlternateSetting for all interfaces. | 54 | /// The default value for bAlternateSetting for all interfaces. |
| 54 | pub const DEFAULT_ALTERNATE_SETTING: u8 = 0; | 55 | pub const DEFAULT_ALTERNATE_SETTING: u8 = 0; |
| 55 | 56 | ||
| 57 | pub const MAX_CLASS_COUNT: usize = 4; | ||
| 58 | |||
| 56 | pub struct UsbDevice<'d, D: Driver<'d>> { | 59 | pub struct UsbDevice<'d, D: Driver<'d>> { |
| 57 | bus: D::Bus, | 60 | bus: D::Bus, |
| 58 | control: D::ControlPipe, | 61 | control: D::ControlPipe, |
| @@ -67,7 +70,7 @@ pub struct UsbDevice<'d, D: Driver<'d>> { | |||
| 67 | self_powered: bool, | 70 | self_powered: bool, |
| 68 | pending_address: u8, | 71 | pending_address: u8, |
| 69 | 72 | ||
| 70 | classes: &'d mut [&'d mut dyn UsbClass], | 73 | classes: Vec<&'d mut dyn UsbClass, MAX_CLASS_COUNT>, |
| 71 | } | 74 | } |
| 72 | 75 | ||
| 73 | impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | 76 | impl<'d, D: Driver<'d>> UsbDevice<'d, D> { |
| @@ -77,7 +80,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { | |||
| 77 | device_descriptor: &'d [u8], | 80 | device_descriptor: &'d [u8], |
| 78 | config_descriptor: &'d [u8], | 81 | config_descriptor: &'d [u8], |
| 79 | bos_descriptor: &'d [u8], | 82 | bos_descriptor: &'d [u8], |
| 80 | classes: &'d mut [&'d mut dyn UsbClass], | 83 | classes: Vec<&'d mut dyn UsbClass, MAX_CLASS_COUNT>, |
| 81 | ) -> Self { | 84 | ) -> Self { |
| 82 | let control = driver | 85 | let control = driver |
| 83 | .alloc_control_pipe(config.max_packet_size_0 as u16) | 86 | .alloc_control_pipe(config.max_packet_size_0 as u16) |
