diff options
Diffstat (limited to 'embassy-usb-logger')
| -rw-r--r-- | embassy-usb-logger/src/lib.rs | 75 |
1 files changed, 33 insertions, 42 deletions
diff --git a/embassy-usb-logger/src/lib.rs b/embassy-usb-logger/src/lib.rs index a057fcf32..da5ff0f36 100644 --- a/embassy-usb-logger/src/lib.rs +++ b/embassy-usb-logger/src/lib.rs | |||
| @@ -6,7 +6,7 @@ use core::fmt::Write as _; | |||
| 6 | 6 | ||
| 7 | use embassy_futures::join::join; | 7 | use embassy_futures::join::join; |
| 8 | use embassy_sync::pipe::Pipe; | 8 | use embassy_sync::pipe::Pipe; |
| 9 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | 9 | use embassy_usb::class::cdc_acm::{CdcAcmClass, Receiver, Sender, State}; |
| 10 | use embassy_usb::driver::Driver; | 10 | use embassy_usb::driver::Driver; |
| 11 | use embassy_usb::{Builder, Config}; | 11 | use embassy_usb::{Builder, Config}; |
| 12 | use log::{Metadata, Record}; | 12 | use log::{Metadata, Record}; |
| @@ -37,6 +37,9 @@ impl<'d> LoggerState<'d> { | |||
| 37 | } | 37 | } |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | /// The packet size used in the usb logger, to be used with `create_future_from_class` | ||
| 41 | pub const MAX_PACKET_SIZE: u8 = 64; | ||
| 42 | |||
| 40 | /// The logger handle, which contains a pipe with configurable size for buffering log messages. | 43 | /// The logger handle, which contains a pipe with configurable size for buffering log messages. |
| 41 | pub struct UsbLogger<const N: usize> { | 44 | pub struct UsbLogger<const N: usize> { |
| 42 | buffer: Pipe<CS, N>, | 45 | buffer: Pipe<CS, N>, |
| @@ -54,7 +57,6 @@ impl<const N: usize> UsbLogger<N> { | |||
| 54 | D: Driver<'d>, | 57 | D: Driver<'d>, |
| 55 | Self: 'd, | 58 | Self: 'd, |
| 56 | { | 59 | { |
| 57 | const MAX_PACKET_SIZE: u8 = 64; | ||
| 58 | let mut config = Config::new(0xc0de, 0xcafe); | 60 | let mut config = Config::new(0xc0de, 0xcafe); |
| 59 | config.manufacturer = Some("Embassy"); | 61 | config.manufacturer = Some("Embassy"); |
| 60 | config.product = Some("USB-serial logger"); | 62 | config.product = Some("USB-serial logger"); |
| @@ -87,57 +89,46 @@ impl<const N: usize> UsbLogger<N> { | |||
| 87 | let mut device = builder.build(); | 89 | let mut device = builder.build(); |
| 88 | loop { | 90 | loop { |
| 89 | let run_fut = device.run(); | 91 | let run_fut = device.run(); |
| 90 | let log_fut = async { | 92 | let class_fut = self.run_logger_class(&mut sender, &mut receiver); |
| 91 | let mut rx: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | 93 | join(run_fut, class_fut).await; |
| 92 | sender.wait_connection().await; | ||
| 93 | loop { | ||
| 94 | let len = self.buffer.read(&mut rx[..]).await; | ||
| 95 | let _ = sender.write_packet(&rx[..len]).await; | ||
| 96 | if len as u8 == MAX_PACKET_SIZE { | ||
| 97 | let _ = sender.write_packet(&[]).await; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | }; | ||
| 101 | let discard_fut = async { | ||
| 102 | let mut discard_buf: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | ||
| 103 | receiver.wait_connection().await; | ||
| 104 | loop { | ||
| 105 | let _ = receiver.read_packet(&mut discard_buf).await; | ||
| 106 | } | ||
| 107 | }; | ||
| 108 | join(run_fut, join(log_fut, discard_fut)).await; | ||
| 109 | } | 94 | } |
| 110 | } | 95 | } |
| 111 | 96 | ||
| 97 | async fn run_logger_class<'d, D>(&self, sender: &mut Sender<'d, D>, receiver: &mut Receiver<'d, D>) | ||
| 98 | where | ||
| 99 | D: Driver<'d>, | ||
| 100 | { | ||
| 101 | let log_fut = async { | ||
| 102 | let mut rx: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | ||
| 103 | sender.wait_connection().await; | ||
| 104 | loop { | ||
| 105 | let len = self.buffer.read(&mut rx[..]).await; | ||
| 106 | let _ = sender.write_packet(&rx[..len]).await; | ||
| 107 | if len as u8 == MAX_PACKET_SIZE { | ||
| 108 | let _ = sender.write_packet(&[]).await; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | let discard_fut = async { | ||
| 113 | let mut discard_buf: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | ||
| 114 | receiver.wait_connection().await; | ||
| 115 | loop { | ||
| 116 | let _ = receiver.read_packet(&mut discard_buf).await; | ||
| 117 | } | ||
| 118 | }; | ||
| 119 | |||
| 120 | join(log_fut, discard_fut).await; | ||
| 121 | } | ||
| 122 | |||
| 112 | /// Creates the futures needed for the logger from a given class | 123 | /// Creates the futures needed for the logger from a given class |
| 113 | /// This can be used in cases where the usb device is already in use for another connection | 124 | /// This can be used in cases where the usb device is already in use for another connection |
| 114 | pub async fn create_future_from_class<'d, D>(&'d self, class: CdcAcmClass<'d, D>) | 125 | pub async fn create_future_from_class<'d, D>(&'d self, class: CdcAcmClass<'d, D>) |
| 115 | where | 126 | where |
| 116 | D: Driver<'d>, | 127 | D: Driver<'d>, |
| 117 | { | 128 | { |
| 118 | const MAX_PACKET_SIZE: u8 = 64; | ||
| 119 | let (mut sender, mut receiver) = class.split(); | 129 | let (mut sender, mut receiver) = class.split(); |
| 120 | |||
| 121 | loop { | 130 | loop { |
| 122 | let log_fut = async { | 131 | self.run_logger_class(&mut sender, &mut receiver).await; |
| 123 | let mut rx: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | ||
| 124 | sender.wait_connection().await; | ||
| 125 | loop { | ||
| 126 | let len = self.buffer.read(&mut rx[..]).await; | ||
| 127 | let _ = sender.write_packet(&rx[..len]).await; | ||
| 128 | if len as u8 == MAX_PACKET_SIZE { | ||
| 129 | let _ = sender.write_packet(&[]).await; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | }; | ||
| 133 | let discard_fut = async { | ||
| 134 | let mut discard_buf: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | ||
| 135 | receiver.wait_connection().await; | ||
| 136 | loop { | ||
| 137 | let _ = receiver.read_packet(&mut discard_buf).await; | ||
| 138 | } | ||
| 139 | }; | ||
| 140 | join(log_fut, discard_fut).await; | ||
| 141 | } | 132 | } |
| 142 | } | 133 | } |
| 143 | } | 134 | } |
