diff options
| author | alexmoon <[email protected]> | 2022-04-13 13:09:08 -0400 |
|---|---|---|
| committer | alexmoon <[email protected]> | 2022-04-13 14:55:02 -0400 |
| commit | ff7c6b350e2338a0b1e4327f4b2eb0435468f313 (patch) | |
| tree | 1a045db7348d2562b9c8a19299467a30b62e501e /embassy-usb/src/builder.rs | |
| parent | 1d875fab2dc9d765ebf9f4b37112581301f2ed7e (diff) | |
Remove channel and make run future cancelable
Diffstat (limited to 'embassy-usb/src/builder.rs')
| -rw-r--r-- | embassy-usb/src/builder.rs | 71 |
1 files changed, 3 insertions, 68 deletions
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs index 30d31ac74..486672055 100644 --- a/embassy-usb/src/builder.rs +++ b/embassy-usb/src/builder.rs | |||
| @@ -1,9 +1,5 @@ | |||
| 1 | use embassy::blocking_mutex::raw::{NoopRawMutex, RawMutex}; | ||
| 2 | use embassy::channel::Channel; | ||
| 3 | use heapless::Vec; | 1 | use heapless::Vec; |
| 4 | 2 | ||
| 5 | use crate::DeviceCommand; | ||
| 6 | |||
| 7 | use super::control::ControlHandler; | 3 | use super::control::ControlHandler; |
| 8 | use super::descriptor::{BosWriter, DescriptorWriter}; | 4 | use super::descriptor::{BosWriter, DescriptorWriter}; |
| 9 | use super::driver::{Driver, EndpointAllocError}; | 5 | use super::driver::{Driver, EndpointAllocError}; |
| @@ -98,11 +94,6 @@ pub struct Config<'a> { | |||
| 98 | /// Default: 100mA | 94 | /// Default: 100mA |
| 99 | /// Max: 500mA | 95 | /// Max: 500mA |
| 100 | pub max_power: u16, | 96 | pub max_power: u16, |
| 101 | |||
| 102 | /// Whether the USB bus should be enabled when built. | ||
| 103 | /// | ||
| 104 | /// Default: true | ||
| 105 | pub start_enabled: bool, | ||
| 106 | } | 97 | } |
| 107 | 98 | ||
| 108 | impl<'a> Config<'a> { | 99 | impl<'a> Config<'a> { |
| @@ -122,18 +113,16 @@ impl<'a> Config<'a> { | |||
| 122 | supports_remote_wakeup: false, | 113 | supports_remote_wakeup: false, |
| 123 | composite_with_iads: false, | 114 | composite_with_iads: false, |
| 124 | max_power: 100, | 115 | max_power: 100, |
| 125 | start_enabled: true, | ||
| 126 | } | 116 | } |
| 127 | } | 117 | } |
| 128 | } | 118 | } |
| 129 | 119 | ||
| 130 | /// Used to build new [`UsbDevice`]s. | 120 | /// Used to build new [`UsbDevice`]s. |
| 131 | pub struct UsbDeviceBuilder<'d, D: Driver<'d>, M: RawMutex> { | 121 | pub struct UsbDeviceBuilder<'d, D: Driver<'d>> { |
| 132 | config: Config<'d>, | 122 | config: Config<'d>, |
| 133 | handler: Option<&'d dyn DeviceStateHandler>, | 123 | handler: Option<&'d dyn DeviceStateHandler>, |
| 134 | interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>, | 124 | interfaces: Vec<(u8, &'d mut dyn ControlHandler), MAX_INTERFACE_COUNT>, |
| 135 | control_buf: &'d mut [u8], | 125 | control_buf: &'d mut [u8], |
| 136 | commands: Option<&'d Channel<M, DeviceCommand, 1>>, | ||
| 137 | 126 | ||
| 138 | driver: D, | 127 | driver: D, |
| 139 | next_interface_number: u8, | 128 | next_interface_number: u8, |
| @@ -145,7 +134,7 @@ pub struct UsbDeviceBuilder<'d, D: Driver<'d>, M: RawMutex> { | |||
| 145 | pub bos_descriptor: BosWriter<'d>, | 134 | pub bos_descriptor: BosWriter<'d>, |
| 146 | } | 135 | } |
| 147 | 136 | ||
| 148 | impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D, NoopRawMutex> { | 137 | impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { |
| 149 | /// Creates a builder for constructing a new [`UsbDevice`]. | 138 | /// Creates a builder for constructing a new [`UsbDevice`]. |
| 150 | /// | 139 | /// |
| 151 | /// `control_buf` is a buffer used for USB control request data. It should be sized | 140 | /// `control_buf` is a buffer used for USB control request data. It should be sized |
| @@ -160,57 +149,6 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D, NoopRawMutex> { | |||
| 160 | control_buf: &'d mut [u8], | 149 | control_buf: &'d mut [u8], |
| 161 | handler: Option<&'d dyn DeviceStateHandler>, | 150 | handler: Option<&'d dyn DeviceStateHandler>, |
| 162 | ) -> Self { | 151 | ) -> Self { |
| 163 | Self::new_inner( | ||
| 164 | driver, | ||
| 165 | config, | ||
| 166 | device_descriptor_buf, | ||
| 167 | config_descriptor_buf, | ||
| 168 | bos_descriptor_buf, | ||
| 169 | control_buf, | ||
| 170 | handler, | ||
| 171 | None, | ||
| 172 | ) | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | impl<'d, D: Driver<'d>, M: RawMutex> UsbDeviceBuilder<'d, D, M> { | ||
| 177 | /// Creates a builder for constructing a new [`UsbDevice`]. | ||
| 178 | /// | ||
| 179 | /// `control_buf` is a buffer used for USB control request data. It should be sized | ||
| 180 | /// large enough for the length of the largest control request (in or out) | ||
| 181 | /// anticipated by any class added to the device. | ||
| 182 | pub fn new_with_channel( | ||
| 183 | driver: D, | ||
| 184 | config: Config<'d>, | ||
| 185 | device_descriptor_buf: &'d mut [u8], | ||
| 186 | config_descriptor_buf: &'d mut [u8], | ||
| 187 | bos_descriptor_buf: &'d mut [u8], | ||
| 188 | control_buf: &'d mut [u8], | ||
| 189 | handler: Option<&'d dyn DeviceStateHandler>, | ||
| 190 | channel: &'d Channel<M, DeviceCommand, 1>, | ||
| 191 | ) -> Self { | ||
| 192 | Self::new_inner( | ||
| 193 | driver, | ||
| 194 | config, | ||
| 195 | device_descriptor_buf, | ||
| 196 | config_descriptor_buf, | ||
| 197 | bos_descriptor_buf, | ||
| 198 | control_buf, | ||
| 199 | handler, | ||
| 200 | Some(channel), | ||
| 201 | ) | ||
| 202 | } | ||
| 203 | |||
| 204 | fn new_inner( | ||
| 205 | driver: D, | ||
| 206 | config: Config<'d>, | ||
| 207 | device_descriptor_buf: &'d mut [u8], | ||
| 208 | config_descriptor_buf: &'d mut [u8], | ||
| 209 | bos_descriptor_buf: &'d mut [u8], | ||
| 210 | control_buf: &'d mut [u8], | ||
| 211 | handler: Option<&'d dyn DeviceStateHandler>, | ||
| 212 | channel: Option<&'d Channel<M, DeviceCommand, 1>>, | ||
| 213 | ) -> Self { | ||
| 214 | // Magic values specified in USB-IF ECN on IADs. | 152 | // Magic values specified in USB-IF ECN on IADs. |
| 215 | if config.composite_with_iads | 153 | if config.composite_with_iads |
| 216 | && (config.device_class != 0xEF | 154 | && (config.device_class != 0xEF |
| @@ -243,8 +181,6 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDeviceBuilder<'d, D, M> { | |||
| 243 | config, | 181 | config, |
| 244 | interfaces: Vec::new(), | 182 | interfaces: Vec::new(), |
| 245 | control_buf, | 183 | control_buf, |
| 246 | commands: channel, | ||
| 247 | |||
| 248 | next_interface_number: 0, | 184 | next_interface_number: 0, |
| 249 | next_string_index: 4, | 185 | next_string_index: 4, |
| 250 | 186 | ||
| @@ -255,7 +191,7 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDeviceBuilder<'d, D, M> { | |||
| 255 | } | 191 | } |
| 256 | 192 | ||
| 257 | /// Creates the [`UsbDevice`] instance with the configuration in this builder. | 193 | /// Creates the [`UsbDevice`] instance with the configuration in this builder. |
| 258 | pub fn build(mut self) -> UsbDevice<'d, D, M> { | 194 | pub fn build(mut self) -> UsbDevice<'d, D> { |
| 259 | self.config_descriptor.end_configuration(); | 195 | self.config_descriptor.end_configuration(); |
| 260 | self.bos_descriptor.end_bos(); | 196 | self.bos_descriptor.end_bos(); |
| 261 | 197 | ||
| @@ -263,7 +199,6 @@ impl<'d, D: Driver<'d>, M: RawMutex> UsbDeviceBuilder<'d, D, M> { | |||
| 263 | self.driver, | 199 | self.driver, |
| 264 | self.config, | 200 | self.config, |
| 265 | self.handler, | 201 | self.handler, |
| 266 | self.commands, | ||
| 267 | self.device_descriptor.into_buf(), | 202 | self.device_descriptor.into_buf(), |
| 268 | self.config_descriptor.into_buf(), | 203 | self.config_descriptor.into_buf(), |
| 269 | self.bos_descriptor.writer.into_buf(), | 204 | self.bos_descriptor.writer.into_buf(), |
