aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src
diff options
context:
space:
mode:
authorLasse Dalegaard <[email protected]>2023-03-04 10:36:10 +0100
committerLasse Dalegaard <[email protected]>2023-03-04 10:36:10 +0100
commit7b9075130e72e0f6fab6b07c0171e42e1b9802a1 (patch)
treed1fbde14be8dfc251ed1d9d727d4117da22fa175 /embassy-usb/src
parent78d733fc73954f774cf04e8aa5bf95801a03547d (diff)
embassy_usb: Add split() for cdc_acm
Diffstat (limited to 'embassy-usb/src')
-rw-r--r--embassy-usb/src/class/cdc_acm.rs100
1 files changed, 100 insertions, 0 deletions
diff --git a/embassy-usb/src/class/cdc_acm.rs b/embassy-usb/src/class/cdc_acm.rs
index ff82ad40d..a341e10da 100644
--- a/embassy-usb/src/class/cdc_acm.rs
+++ b/embassy-usb/src/class/cdc_acm.rs
@@ -276,6 +276,106 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
276 pub async fn wait_connection(&mut self) { 276 pub async fn wait_connection(&mut self) {
277 self.read_ep.wait_enabled().await 277 self.read_ep.wait_enabled().await
278 } 278 }
279
280 /// Split the class into a sender and receiver.
281 ///
282 /// This allows concurrently sending and receiving packets from separate tasks.
283 pub fn split(self) -> (Sender<'d, D>, Receiver<'d, D>) {
284 (
285 Sender {
286 write_ep: self.write_ep,
287 control: self.control,
288 },
289 Receiver {
290 read_ep: self.read_ep,
291 control: self.control,
292 },
293 )
294 }
295}
296
297/// CDC ACM class packet sender.
298///
299/// You can obtain a `Sender` with [`CdcAcmClass::split`]
300pub struct Sender<'d, D: Driver<'d>> {
301 write_ep: D::EndpointIn,
302 control: &'d ControlShared,
303}
304
305impl<'d, D: Driver<'d>> Sender<'d, D> {
306 /// Gets the maximum packet size in bytes.
307 pub fn max_packet_size(&self) -> u16 {
308 // The size is the same for both endpoints.
309 self.write_ep.info().max_packet_size
310 }
311
312 /// Gets the current line coding. The line coding contains information that's mainly relevant
313 /// for USB to UART serial port emulators, and can be ignored if not relevant.
314 pub fn line_coding(&self) -> LineCoding {
315 self.control.line_coding.lock(|x| x.get())
316 }
317
318 /// Gets the DTR (data terminal ready) state
319 pub fn dtr(&self) -> bool {
320 self.control.dtr.load(Ordering::Relaxed)
321 }
322
323 /// Gets the RTS (request to send) state
324 pub fn rts(&self) -> bool {
325 self.control.rts.load(Ordering::Relaxed)
326 }
327
328 /// Writes a single packet into the IN endpoint.
329 pub async fn write_packet(&mut self, data: &[u8]) -> Result<(), EndpointError> {
330 self.write_ep.write(data).await
331 }
332
333 /// Waits for the USB host to enable this interface
334 pub async fn wait_connection(&mut self) {
335 self.write_ep.wait_enabled().await
336 }
337}
338
339/// CDC ACM class packet receiver.
340///
341/// You can obtain a `Receiver` with [`CdcAcmClass::split`]
342pub struct Receiver<'d, D: Driver<'d>> {
343 read_ep: D::EndpointOut,
344 control: &'d ControlShared,
345}
346
347impl<'d, D: Driver<'d>> Receiver<'d, D> {
348 /// Gets the maximum packet size in bytes.
349 pub fn max_packet_size(&self) -> u16 {
350 // The size is the same for both endpoints.
351 self.read_ep.info().max_packet_size
352 }
353
354 /// Gets the current line coding. The line coding contains information that's mainly relevant
355 /// for USB to UART serial port emulators, and can be ignored if not relevant.
356 pub fn line_coding(&self) -> LineCoding {
357 self.control.line_coding.lock(|x| x.get())
358 }
359
360 /// Gets the DTR (data terminal ready) state
361 pub fn dtr(&self) -> bool {
362 self.control.dtr.load(Ordering::Relaxed)
363 }
364
365 /// Gets the RTS (request to send) state
366 pub fn rts(&self) -> bool {
367 self.control.rts.load(Ordering::Relaxed)
368 }
369
370 /// Reads a single packet from the OUT endpoint.
371 pub async fn read_packet(&mut self, data: &mut [u8]) -> Result<usize, EndpointError> {
372 self.read_ep.read(data).await
373 }
374
375 /// Waits for the USB host to enable this interface
376 pub async fn wait_connection(&mut self) {
377 self.read_ep.wait_enabled().await
378 }
279} 379}
280 380
281/// Number of stop bits for LineCoding 381/// Number of stop bits for LineCoding